mips.c (mips16_rewrite_pool_refs_info): Delete.
[platform/upstream/gcc.git] / gcc / config / mips / mips.c
1 /* Subroutines used for MIPS code generation.
2    Copyright (C) 1989-2014 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 "varasm.h"
38 #include "stringpool.h"
39 #include "stor-layout.h"
40 #include "calls.h"
41 #include "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "input.h"
46 #include "function.h"
47 #include "expr.h"
48 #include "optabs.h"
49 #include "libfuncs.h"
50 #include "flags.h"
51 #include "reload.h"
52 #include "tm_p.h"
53 #include "ggc.h"
54 #include "gstab.h"
55 #include "hash-table.h"
56 #include "debug.h"
57 #include "target.h"
58 #include "target-def.h"
59 #include "common/common-target.h"
60 #include "langhooks.h"
61 #include "sched-int.h"
62 #include "basic-block.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-fold.h"
66 #include "tree-eh.h"
67 #include "gimple-expr.h"
68 #include "is-a.h"
69 #include "gimple.h"
70 #include "gimplify.h"
71 #include "bitmap.h"
72 #include "diagnostic.h"
73 #include "target-globals.h"
74 #include "opts.h"
75 #include "tree-pass.h"
76 #include "context.h"
77 #include "cgraph.h"
78 #include "builtins.h"
79 #include "rtl-iter.h"
80
81 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF.  */
82 #define UNSPEC_ADDRESS_P(X)                                     \
83   (GET_CODE (X) == UNSPEC                                       \
84    && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST                       \
85    && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
86
87 /* Extract the symbol or label from UNSPEC wrapper X.  */
88 #define UNSPEC_ADDRESS(X) \
89   XVECEXP (X, 0, 0)
90
91 /* Extract the symbol type from UNSPEC wrapper X.  */
92 #define UNSPEC_ADDRESS_TYPE(X) \
93   ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
94
95 /* The maximum distance between the top of the stack frame and the
96    value $sp has when we save and restore registers.
97
98    The value for normal-mode code must be a SMALL_OPERAND and must
99    preserve the maximum stack alignment.  We therefore use a value
100    of 0x7ff0 in this case.
101
102    microMIPS LWM and SWM support 12-bit offsets (from -0x800 to 0x7ff),
103    so we use a maximum of 0x7f0 for TARGET_MICROMIPS.
104
105    MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
106    up to 0x7f8 bytes and can usually save or restore all the registers
107    that we need to save or restore.  (Note that we can only use these
108    instructions for o32, for which the stack alignment is 8 bytes.)
109
110    We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
111    RESTORE are not available.  We can then use unextended instructions
112    to save and restore registers, and to allocate and deallocate the top
113    part of the frame.  */
114 #define MIPS_MAX_FIRST_STACK_STEP                                       \
115   (!TARGET_COMPRESSION ? 0x7ff0                                         \
116    : TARGET_MICROMIPS || GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8          \
117    : TARGET_64BIT ? 0x100 : 0x400)
118
119 /* True if INSN is a mips.md pattern or asm statement.  */
120 /* ???  This test exists through the compiler, perhaps it should be
121         moved to rtl.h.  */
122 #define USEFUL_INSN_P(INSN)                                             \
123   (NONDEBUG_INSN_P (INSN)                                               \
124    && GET_CODE (PATTERN (INSN)) != USE                                  \
125    && GET_CODE (PATTERN (INSN)) != CLOBBER)
126
127 /* If INSN is a delayed branch sequence, return the first instruction
128    in the sequence, otherwise return INSN itself.  */
129 #define SEQ_BEGIN(INSN)                                                 \
130   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE               \
131    ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN), 0, 0))                 \
132    : (INSN))
133
134 /* Likewise for the last instruction in a delayed branch sequence.  */
135 #define SEQ_END(INSN)                                                   \
136   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE               \
137    ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN),                        \
138                                  0,                                     \
139                                  XVECLEN (PATTERN (INSN), 0) - 1))      \
140    : (INSN))
141
142 /* Execute the following loop body with SUBINSN set to each instruction
143    between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive.  */
144 #define FOR_EACH_SUBINSN(SUBINSN, INSN)                                 \
145   for ((SUBINSN) = SEQ_BEGIN (INSN);                                    \
146        (SUBINSN) != NEXT_INSN (SEQ_END (INSN));                         \
147        (SUBINSN) = NEXT_INSN (SUBINSN))
148
149 /* True if bit BIT is set in VALUE.  */
150 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
151
152 /* Return the opcode for a ptr_mode load of the form:
153
154        l[wd]    DEST, OFFSET(BASE).  */
155 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE)       \
156   (((ptr_mode == DImode ? 0x37 : 0x23) << 26)   \
157    | ((BASE) << 21)                             \
158    | ((DEST) << 16)                             \
159    | (OFFSET))
160
161 /* Return the opcode to move register SRC into register DEST.  */
162 #define MIPS_MOVE(DEST, SRC)            \
163   ((TARGET_64BIT ? 0x2d : 0x21)         \
164    | ((DEST) << 11)                     \
165    | ((SRC) << 21))
166
167 /* Return the opcode for:
168
169        lui      DEST, VALUE.  */
170 #define MIPS_LUI(DEST, VALUE) \
171   ((0xf << 26) | ((DEST) << 16) | (VALUE))
172
173 /* Return the opcode to jump to register DEST.  */
174 #define MIPS_JR(DEST) \
175   (((DEST) << 21) | 0x8)
176
177 /* Return the opcode for:
178
179        bal     . + (1 + OFFSET) * 4.  */
180 #define MIPS_BAL(OFFSET) \
181   ((0x1 << 26) | (0x11 << 16) | (OFFSET))
182
183 /* Return the usual opcode for a nop.  */
184 #define MIPS_NOP 0
185
186 /* Classifies an address.
187
188    ADDRESS_REG
189        A natural register + offset address.  The register satisfies
190        mips_valid_base_register_p and the offset is a const_arith_operand.
191
192    ADDRESS_LO_SUM
193        A LO_SUM rtx.  The first operand is a valid base register and
194        the second operand is a symbolic address.
195
196    ADDRESS_CONST_INT
197        A signed 16-bit constant address.
198
199    ADDRESS_SYMBOLIC:
200        A constant symbolic address.  */
201 enum mips_address_type {
202   ADDRESS_REG,
203   ADDRESS_LO_SUM,
204   ADDRESS_CONST_INT,
205   ADDRESS_SYMBOLIC
206 };
207
208 /* Macros to create an enumeration identifier for a function prototype.  */
209 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
210 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
211 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
212 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
213
214 /* Classifies the prototype of a built-in function.  */
215 enum mips_function_type {
216 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
217 #include "config/mips/mips-ftypes.def"
218 #undef DEF_MIPS_FTYPE
219   MIPS_MAX_FTYPE_MAX
220 };
221
222 /* Specifies how a built-in function should be converted into rtl.  */
223 enum mips_builtin_type {
224   /* The function corresponds directly to an .md pattern.  The return
225      value is mapped to operand 0 and the arguments are mapped to
226      operands 1 and above.  */
227   MIPS_BUILTIN_DIRECT,
228
229   /* The function corresponds directly to an .md pattern.  There is no return
230      value and the arguments are mapped to operands 0 and above.  */
231   MIPS_BUILTIN_DIRECT_NO_TARGET,
232
233   /* The function corresponds to a comparison instruction followed by
234      a mips_cond_move_tf_ps pattern.  The first two arguments are the
235      values to compare and the second two arguments are the vector
236      operands for the movt.ps or movf.ps instruction (in assembly order).  */
237   MIPS_BUILTIN_MOVF,
238   MIPS_BUILTIN_MOVT,
239
240   /* The function corresponds to a V2SF comparison instruction.  Operand 0
241      of this instruction is the result of the comparison, which has mode
242      CCV2 or CCV4.  The function arguments are mapped to operands 1 and
243      above.  The function's return value is an SImode boolean that is
244      true under the following conditions:
245
246      MIPS_BUILTIN_CMP_ANY: one of the registers is true
247      MIPS_BUILTIN_CMP_ALL: all of the registers are true
248      MIPS_BUILTIN_CMP_LOWER: the first register is true
249      MIPS_BUILTIN_CMP_UPPER: the second register is true.  */
250   MIPS_BUILTIN_CMP_ANY,
251   MIPS_BUILTIN_CMP_ALL,
252   MIPS_BUILTIN_CMP_UPPER,
253   MIPS_BUILTIN_CMP_LOWER,
254
255   /* As above, but the instruction only sets a single $fcc register.  */
256   MIPS_BUILTIN_CMP_SINGLE,
257
258   /* For generating bposge32 branch instructions in MIPS32 DSP ASE.  */
259   MIPS_BUILTIN_BPOSGE32
260 };
261
262 /* Invoke MACRO (COND) for each C.cond.fmt condition.  */
263 #define MIPS_FP_CONDITIONS(MACRO) \
264   MACRO (f),    \
265   MACRO (un),   \
266   MACRO (eq),   \
267   MACRO (ueq),  \
268   MACRO (olt),  \
269   MACRO (ult),  \
270   MACRO (ole),  \
271   MACRO (ule),  \
272   MACRO (sf),   \
273   MACRO (ngle), \
274   MACRO (seq),  \
275   MACRO (ngl),  \
276   MACRO (lt),   \
277   MACRO (nge),  \
278   MACRO (le),   \
279   MACRO (ngt)
280
281 /* Enumerates the codes above as MIPS_FP_COND_<X>.  */
282 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
283 enum mips_fp_condition {
284   MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
285 };
286 #undef DECLARE_MIPS_COND
287
288 /* Index X provides the string representation of MIPS_FP_COND_<X>.  */
289 #define STRINGIFY(X) #X
290 static const char *const mips_fp_conditions[] = {
291   MIPS_FP_CONDITIONS (STRINGIFY)
292 };
293 #undef STRINGIFY
294
295 /* A class used to control a comdat-style stub that we output in each
296    translation unit that needs it.  */
297 class mips_one_only_stub {
298 public:
299   virtual ~mips_one_only_stub () {}
300
301   /* Return the name of the stub.  */
302   virtual const char *get_name () = 0;
303
304   /* Output the body of the function to asm_out_file.  */
305   virtual void output_body () = 0;
306 };
307
308 /* Tuning information that is automatically derived from other sources
309    (such as the scheduler).  */
310 static struct {
311   /* The architecture and tuning settings that this structure describes.  */
312   enum processor arch;
313   enum processor tune;
314
315   /* True if this structure describes MIPS16 settings.  */
316   bool mips16_p;
317
318   /* True if the structure has been initialized.  */
319   bool initialized_p;
320
321   /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
322      when optimizing for speed.  */
323   bool fast_mult_zero_zero_p;
324 } mips_tuning_info;
325
326 /* Information about a function's frame layout.  */
327 struct GTY(())  mips_frame_info {
328   /* The size of the frame in bytes.  */
329   HOST_WIDE_INT total_size;
330
331   /* The number of bytes allocated to variables.  */
332   HOST_WIDE_INT var_size;
333
334   /* The number of bytes allocated to outgoing function arguments.  */
335   HOST_WIDE_INT args_size;
336
337   /* The number of bytes allocated to the .cprestore slot, or 0 if there
338      is no such slot.  */
339   HOST_WIDE_INT cprestore_size;
340
341   /* Bit X is set if the function saves or restores GPR X.  */
342   unsigned int mask;
343
344   /* Likewise FPR X.  */
345   unsigned int fmask;
346
347   /* Likewise doubleword accumulator X ($acX).  */
348   unsigned int acc_mask;
349
350   /* The number of GPRs, FPRs, doubleword accumulators and COP0
351      registers saved.  */
352   unsigned int num_gp;
353   unsigned int num_fp;
354   unsigned int num_acc;
355   unsigned int num_cop0_regs;
356
357   /* The offset of the topmost GPR, FPR, accumulator and COP0-register
358      save slots from the top of the frame, or zero if no such slots are
359      needed.  */
360   HOST_WIDE_INT gp_save_offset;
361   HOST_WIDE_INT fp_save_offset;
362   HOST_WIDE_INT acc_save_offset;
363   HOST_WIDE_INT cop0_save_offset;
364
365   /* Likewise, but giving offsets from the bottom of the frame.  */
366   HOST_WIDE_INT gp_sp_offset;
367   HOST_WIDE_INT fp_sp_offset;
368   HOST_WIDE_INT acc_sp_offset;
369   HOST_WIDE_INT cop0_sp_offset;
370
371   /* Similar, but the value passed to _mcount.  */
372   HOST_WIDE_INT ra_fp_offset;
373
374   /* The offset of arg_pointer_rtx from the bottom of the frame.  */
375   HOST_WIDE_INT arg_pointer_offset;
376
377   /* The offset of hard_frame_pointer_rtx from the bottom of the frame.  */
378   HOST_WIDE_INT hard_frame_pointer_offset;
379 };
380
381 struct GTY(())  machine_function {
382   /* The next floating-point condition-code register to allocate
383      for ISA_HAS_8CC targets, relative to ST_REG_FIRST.  */
384   unsigned int next_fcc;
385
386   /* The register returned by mips16_gp_pseudo_reg; see there for details.  */
387   rtx mips16_gp_pseudo_rtx;
388
389   /* The number of extra stack bytes taken up by register varargs.
390      This area is allocated by the callee at the very top of the frame.  */
391   int varargs_size;
392
393   /* The current frame information, calculated by mips_compute_frame_info.  */
394   struct mips_frame_info frame;
395
396   /* The register to use as the function's global pointer, or INVALID_REGNUM
397      if the function doesn't need one.  */
398   unsigned int global_pointer;
399
400   /* How many instructions it takes to load a label into $AT, or 0 if
401      this property hasn't yet been calculated.  */
402   unsigned int load_label_num_insns;
403
404   /* True if mips_adjust_insn_length should ignore an instruction's
405      hazard attribute.  */
406   bool ignore_hazard_length_p;
407
408   /* True if the whole function is suitable for .set noreorder and
409      .set nomacro.  */
410   bool all_noreorder_p;
411
412   /* True if the function has "inflexible" and "flexible" references
413      to the global pointer.  See mips_cfun_has_inflexible_gp_ref_p
414      and mips_cfun_has_flexible_gp_ref_p for details.  */
415   bool has_inflexible_gp_insn_p;
416   bool has_flexible_gp_insn_p;
417
418   /* True if the function's prologue must load the global pointer
419      value into pic_offset_table_rtx and store the same value in
420      the function's cprestore slot (if any).  Even if this value
421      is currently false, we may decide to set it to true later;
422      see mips_must_initialize_gp_p () for details.  */
423   bool must_initialize_gp_p;
424
425   /* True if the current function must restore $gp after any potential
426      clobber.  This value is only meaningful during the first post-epilogue
427      split_insns pass; see mips_must_initialize_gp_p () for details.  */
428   bool must_restore_gp_when_clobbered_p;
429
430   /* True if this is an interrupt handler.  */
431   bool interrupt_handler_p;
432
433   /* True if this is an interrupt handler that uses shadow registers.  */
434   bool use_shadow_register_set_p;
435
436   /* True if this is an interrupt handler that should keep interrupts
437      masked.  */
438   bool keep_interrupts_masked_p;
439
440   /* True if this is an interrupt handler that should use DERET
441      instead of ERET.  */
442   bool use_debug_exception_return_p;
443 };
444
445 /* Information about a single argument.  */
446 struct mips_arg_info {
447   /* True if the argument is passed in a floating-point register, or
448      would have been if we hadn't run out of registers.  */
449   bool fpr_p;
450
451   /* The number of words passed in registers, rounded up.  */
452   unsigned int reg_words;
453
454   /* For EABI, the offset of the first register from GP_ARG_FIRST or
455      FP_ARG_FIRST.  For other ABIs, the offset of the first register from
456      the start of the ABI's argument structure (see the CUMULATIVE_ARGS
457      comment for details).
458
459      The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
460      on the stack.  */
461   unsigned int reg_offset;
462
463   /* The number of words that must be passed on the stack, rounded up.  */
464   unsigned int stack_words;
465
466   /* The offset from the start of the stack overflow area of the argument's
467      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
468   unsigned int stack_offset;
469 };
470
471 /* Information about an address described by mips_address_type.
472
473    ADDRESS_CONST_INT
474        No fields are used.
475
476    ADDRESS_REG
477        REG is the base register and OFFSET is the constant offset.
478
479    ADDRESS_LO_SUM
480        REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
481        is the type of symbol it references.
482
483    ADDRESS_SYMBOLIC
484        SYMBOL_TYPE is the type of symbol that the address references.  */
485 struct mips_address_info {
486   enum mips_address_type type;
487   rtx reg;
488   rtx offset;
489   enum mips_symbol_type symbol_type;
490 };
491
492 /* One stage in a constant building sequence.  These sequences have
493    the form:
494
495         A = VALUE[0]
496         A = A CODE[1] VALUE[1]
497         A = A CODE[2] VALUE[2]
498         ...
499
500    where A is an accumulator, each CODE[i] is a binary rtl operation
501    and each VALUE[i] is a constant integer.  CODE[0] is undefined.  */
502 struct mips_integer_op {
503   enum rtx_code code;
504   unsigned HOST_WIDE_INT value;
505 };
506
507 /* The largest number of operations needed to load an integer constant.
508    The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
509    When the lowest bit is clear, we can try, but reject a sequence with
510    an extra SLL at the end.  */
511 #define MIPS_MAX_INTEGER_OPS 7
512
513 /* Information about a MIPS16e SAVE or RESTORE instruction.  */
514 struct mips16e_save_restore_info {
515   /* The number of argument registers saved by a SAVE instruction.
516      0 for RESTORE instructions.  */
517   unsigned int nargs;
518
519   /* Bit X is set if the instruction saves or restores GPR X.  */
520   unsigned int mask;
521
522   /* The total number of bytes to allocate.  */
523   HOST_WIDE_INT size;
524 };
525
526 /* Costs of various operations on the different architectures.  */
527
528 struct mips_rtx_cost_data
529 {
530   unsigned short fp_add;
531   unsigned short fp_mult_sf;
532   unsigned short fp_mult_df;
533   unsigned short fp_div_sf;
534   unsigned short fp_div_df;
535   unsigned short int_mult_si;
536   unsigned short int_mult_di;
537   unsigned short int_div_si;
538   unsigned short int_div_di;
539   unsigned short branch_cost;
540   unsigned short memory_latency;
541 };
542
543 /* Global variables for machine-dependent things.  */
544
545 /* The -G setting, or the configuration's default small-data limit if
546    no -G option is given.  */
547 static unsigned int mips_small_data_threshold;
548
549 /* The number of file directives written by mips_output_filename.  */
550 int num_source_filenames;
551
552 /* The name that appeared in the last .file directive written by
553    mips_output_filename, or "" if mips_output_filename hasn't
554    written anything yet.  */
555 const char *current_function_file = "";
556
557 /* Arrays that map GCC register numbers to debugger register numbers.  */
558 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
559 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
560
561 /* Information about the current function's epilogue, used only while
562    expanding it.  */
563 static struct {
564   /* A list of queued REG_CFA_RESTORE notes.  */
565   rtx cfa_restores;
566
567   /* The CFA is currently defined as CFA_REG + CFA_OFFSET.  */
568   rtx cfa_reg;
569   HOST_WIDE_INT cfa_offset;
570
571   /* The offset of the CFA from the stack pointer while restoring
572      registers.  */
573   HOST_WIDE_INT cfa_restore_sp_offset;
574 } mips_epilogue;
575
576 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs.  */
577 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
578 struct mips_asm_switch mips_nomacro = { "macro", 0 };
579 struct mips_asm_switch mips_noat = { "at", 0 };
580
581 /* True if we're writing out a branch-likely instruction rather than a
582    normal branch.  */
583 static bool mips_branch_likely;
584
585 /* The current instruction-set architecture.  */
586 enum processor mips_arch;
587 const struct mips_cpu_info *mips_arch_info;
588
589 /* The processor that we should tune the code for.  */
590 enum processor mips_tune;
591 const struct mips_cpu_info *mips_tune_info;
592
593 /* The ISA level associated with mips_arch.  */
594 int mips_isa;
595
596 /* The ISA revision level.  This is 0 for MIPS I to V and N for
597    MIPS{32,64}rN.  */
598 int mips_isa_rev;
599
600 /* The architecture selected by -mipsN, or null if -mipsN wasn't used.  */
601 static const struct mips_cpu_info *mips_isa_option_info;
602
603 /* Which cost information to use.  */
604 static const struct mips_rtx_cost_data *mips_cost;
605
606 /* The ambient target flags, excluding MASK_MIPS16.  */
607 static int mips_base_target_flags;
608
609 /* The default compression mode.  */
610 unsigned int mips_base_compression_flags;
611
612 /* The ambient values of other global variables.  */
613 static int mips_base_schedule_insns; /* flag_schedule_insns */
614 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
615 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
616 static int mips_base_align_loops; /* align_loops */
617 static int mips_base_align_jumps; /* align_jumps */
618 static int mips_base_align_functions; /* align_functions */
619
620 /* Index [M][R] is true if register R is allowed to hold a value of mode M.  */
621 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
622
623 /* Index C is true if character C is a valid PRINT_OPERAND punctation
624    character.  */
625 static bool mips_print_operand_punct[256];
626
627 static GTY (()) int mips_output_filename_first_time = 1;
628
629 /* mips_split_p[X] is true if symbols of type X can be split by
630    mips_split_symbol.  */
631 bool mips_split_p[NUM_SYMBOL_TYPES];
632
633 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
634    can be split by mips_split_symbol.  */
635 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
636
637 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
638    forced into a PC-relative constant pool.  */
639 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
640
641 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
642    appears in a LO_SUM.  It can be null if such LO_SUMs aren't valid or
643    if they are matched by a special .md file pattern.  */
644 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
645
646 /* Likewise for HIGHs.  */
647 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
648
649 /* Target state for MIPS16.  */
650 struct target_globals *mips16_globals;
651
652 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
653    and returned from mips_sched_reorder2.  */
654 static int cached_can_issue_more;
655
656 /* The stubs for various MIPS16 support functions, if used.   */
657 static mips_one_only_stub *mips16_rdhwr_stub;
658 static mips_one_only_stub *mips16_get_fcsr_stub;
659 static mips_one_only_stub *mips16_set_fcsr_stub;
660
661 /* Index R is the smallest register class that contains register R.  */
662 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
663   LEA_REGS,        LEA_REGS,        M16_STORE_REGS,  V1_REG,
664   M16_STORE_REGS,  M16_STORE_REGS,  M16_STORE_REGS,  M16_STORE_REGS,
665   LEA_REGS,        LEA_REGS,        LEA_REGS,        LEA_REGS,
666   LEA_REGS,        LEA_REGS,        LEA_REGS,        LEA_REGS,
667   M16_REGS,        M16_STORE_REGS,  LEA_REGS,        LEA_REGS,
668   LEA_REGS,        LEA_REGS,        LEA_REGS,        LEA_REGS,
669   T_REG,           PIC_FN_ADDR_REG, LEA_REGS,        LEA_REGS,
670   LEA_REGS,        M16_SP_REGS,     LEA_REGS,        LEA_REGS,
671
672   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
673   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
674   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
675   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
676   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
677   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
678   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
679   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
680   MD0_REG,      MD1_REG,        NO_REGS,        ST_REGS,
681   ST_REGS,      ST_REGS,        ST_REGS,        ST_REGS,
682   ST_REGS,      ST_REGS,        ST_REGS,        NO_REGS,
683   NO_REGS,      FRAME_REGS,     FRAME_REGS,     NO_REGS,
684   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
685   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
686   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
687   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
688   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
689   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
690   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
691   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
692   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
693   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
694   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
695   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
696   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
697   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
698   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
699   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
700   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
701   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
702   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
703   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
704   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
705   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
706   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
707   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
708   DSP_ACC_REGS, DSP_ACC_REGS,   DSP_ACC_REGS,   DSP_ACC_REGS,
709   DSP_ACC_REGS, DSP_ACC_REGS,   ALL_REGS,       ALL_REGS,
710   ALL_REGS,     ALL_REGS,       ALL_REGS,       ALL_REGS
711 };
712
713 /* The value of TARGET_ATTRIBUTE_TABLE.  */
714 static const struct attribute_spec mips_attribute_table[] = {
715   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
716        om_diagnostic } */
717   { "long_call",   0, 0, false, true,  true,  NULL, false },
718   { "far",         0, 0, false, true,  true,  NULL, false },
719   { "near",        0, 0, false, true,  true,  NULL, false },
720   /* We would really like to treat "mips16" and "nomips16" as type
721      attributes, but GCC doesn't provide the hooks we need to support
722      the right conversion rules.  As declaration attributes, they affect
723      code generation but don't carry other semantics.  */
724   { "mips16",      0, 0, true,  false, false, NULL, false },
725   { "nomips16",    0, 0, true,  false, false, NULL, false },
726   { "micromips",   0, 0, true,  false, false, NULL, false },
727   { "nomicromips", 0, 0, true,  false, false, NULL, false },
728   { "nocompression", 0, 0, true,  false, false, NULL, false },
729   /* Allow functions to be specified as interrupt handlers */
730   { "interrupt",   0, 0, false, true,  true, NULL, false },
731   { "use_shadow_register_set",  0, 0, false, true,  true, NULL, false },
732   { "keep_interrupts_masked",   0, 0, false, true,  true, NULL, false },
733   { "use_debug_exception_return", 0, 0, false, true,  true, NULL, false },
734   { NULL,          0, 0, false, false, false, NULL, false }
735 };
736 \f
737 /* A table describing all the processors GCC knows about; see
738    mips-cpus.def for details.  */
739 static const struct mips_cpu_info mips_cpu_info_table[] = {
740 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
741   { NAME, CPU, ISA, FLAGS },
742 #include "mips-cpus.def"
743 #undef MIPS_CPU
744 };
745
746 /* Default costs.  If these are used for a processor we should look
747    up the actual costs.  */
748 #define DEFAULT_COSTS COSTS_N_INSNS (6),  /* fp_add */       \
749                       COSTS_N_INSNS (7),  /* fp_mult_sf */   \
750                       COSTS_N_INSNS (8),  /* fp_mult_df */   \
751                       COSTS_N_INSNS (23), /* fp_div_sf */    \
752                       COSTS_N_INSNS (36), /* fp_div_df */    \
753                       COSTS_N_INSNS (10), /* int_mult_si */  \
754                       COSTS_N_INSNS (10), /* int_mult_di */  \
755                       COSTS_N_INSNS (69), /* int_div_si */   \
756                       COSTS_N_INSNS (69), /* int_div_di */   \
757                                        2, /* branch_cost */  \
758                                        4  /* memory_latency */
759
760 /* Floating-point costs for processors without an FPU.  Just assume that
761    all floating-point libcalls are very expensive.  */
762 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */       \
763                       COSTS_N_INSNS (256), /* fp_mult_sf */   \
764                       COSTS_N_INSNS (256), /* fp_mult_df */   \
765                       COSTS_N_INSNS (256), /* fp_div_sf */    \
766                       COSTS_N_INSNS (256)  /* fp_div_df */
767
768 /* Costs to use when optimizing for size.  */
769 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
770   COSTS_N_INSNS (1),            /* fp_add */
771   COSTS_N_INSNS (1),            /* fp_mult_sf */
772   COSTS_N_INSNS (1),            /* fp_mult_df */
773   COSTS_N_INSNS (1),            /* fp_div_sf */
774   COSTS_N_INSNS (1),            /* fp_div_df */
775   COSTS_N_INSNS (1),            /* int_mult_si */
776   COSTS_N_INSNS (1),            /* int_mult_di */
777   COSTS_N_INSNS (1),            /* int_div_si */
778   COSTS_N_INSNS (1),            /* int_div_di */
779                    2,           /* branch_cost */
780                    4            /* memory_latency */
781 };
782
783 /* Costs to use when optimizing for speed, indexed by processor.  */
784 static const struct mips_rtx_cost_data
785   mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
786   { /* R3000 */
787     COSTS_N_INSNS (2),            /* fp_add */
788     COSTS_N_INSNS (4),            /* fp_mult_sf */
789     COSTS_N_INSNS (5),            /* fp_mult_df */
790     COSTS_N_INSNS (12),           /* fp_div_sf */
791     COSTS_N_INSNS (19),           /* fp_div_df */
792     COSTS_N_INSNS (12),           /* int_mult_si */
793     COSTS_N_INSNS (12),           /* int_mult_di */
794     COSTS_N_INSNS (35),           /* int_div_si */
795     COSTS_N_INSNS (35),           /* int_div_di */
796                      1,           /* branch_cost */
797                      4            /* memory_latency */
798   },
799   { /* 4KC */
800     SOFT_FP_COSTS,
801     COSTS_N_INSNS (6),            /* int_mult_si */
802     COSTS_N_INSNS (6),            /* int_mult_di */
803     COSTS_N_INSNS (36),           /* int_div_si */
804     COSTS_N_INSNS (36),           /* int_div_di */
805                      1,           /* branch_cost */
806                      4            /* memory_latency */
807   },
808   { /* 4KP */
809     SOFT_FP_COSTS,
810     COSTS_N_INSNS (36),           /* int_mult_si */
811     COSTS_N_INSNS (36),           /* int_mult_di */
812     COSTS_N_INSNS (37),           /* int_div_si */
813     COSTS_N_INSNS (37),           /* int_div_di */
814                      1,           /* branch_cost */
815                      4            /* memory_latency */
816   },
817   { /* 5KC */
818     SOFT_FP_COSTS,
819     COSTS_N_INSNS (4),            /* int_mult_si */
820     COSTS_N_INSNS (11),           /* int_mult_di */
821     COSTS_N_INSNS (36),           /* int_div_si */
822     COSTS_N_INSNS (68),           /* int_div_di */
823                      1,           /* branch_cost */
824                      4            /* memory_latency */
825   },
826   { /* 5KF */
827     COSTS_N_INSNS (4),            /* fp_add */
828     COSTS_N_INSNS (4),            /* fp_mult_sf */
829     COSTS_N_INSNS (5),            /* fp_mult_df */
830     COSTS_N_INSNS (17),           /* fp_div_sf */
831     COSTS_N_INSNS (32),           /* fp_div_df */
832     COSTS_N_INSNS (4),            /* int_mult_si */
833     COSTS_N_INSNS (11),           /* int_mult_di */
834     COSTS_N_INSNS (36),           /* int_div_si */
835     COSTS_N_INSNS (68),           /* int_div_di */
836                      1,           /* branch_cost */
837                      4            /* memory_latency */
838   },
839   { /* 20KC */
840     COSTS_N_INSNS (4),            /* fp_add */
841     COSTS_N_INSNS (4),            /* fp_mult_sf */
842     COSTS_N_INSNS (5),            /* fp_mult_df */
843     COSTS_N_INSNS (17),           /* fp_div_sf */
844     COSTS_N_INSNS (32),           /* fp_div_df */
845     COSTS_N_INSNS (4),            /* int_mult_si */
846     COSTS_N_INSNS (7),            /* int_mult_di */
847     COSTS_N_INSNS (42),           /* int_div_si */
848     COSTS_N_INSNS (72),           /* int_div_di */
849                      1,           /* branch_cost */
850                      4            /* memory_latency */
851   },
852   { /* 24KC */
853     SOFT_FP_COSTS,
854     COSTS_N_INSNS (5),            /* int_mult_si */
855     COSTS_N_INSNS (5),            /* int_mult_di */
856     COSTS_N_INSNS (41),           /* int_div_si */
857     COSTS_N_INSNS (41),           /* int_div_di */
858                      1,           /* branch_cost */
859                      4            /* memory_latency */
860   },
861   { /* 24KF2_1 */
862     COSTS_N_INSNS (8),            /* fp_add */
863     COSTS_N_INSNS (8),            /* fp_mult_sf */
864     COSTS_N_INSNS (10),           /* fp_mult_df */
865     COSTS_N_INSNS (34),           /* fp_div_sf */
866     COSTS_N_INSNS (64),           /* fp_div_df */
867     COSTS_N_INSNS (5),            /* int_mult_si */
868     COSTS_N_INSNS (5),            /* int_mult_di */
869     COSTS_N_INSNS (41),           /* int_div_si */
870     COSTS_N_INSNS (41),           /* int_div_di */
871                      1,           /* branch_cost */
872                      4            /* memory_latency */
873   },
874   { /* 24KF1_1 */
875     COSTS_N_INSNS (4),            /* fp_add */
876     COSTS_N_INSNS (4),            /* fp_mult_sf */
877     COSTS_N_INSNS (5),            /* fp_mult_df */
878     COSTS_N_INSNS (17),           /* fp_div_sf */
879     COSTS_N_INSNS (32),           /* fp_div_df */
880     COSTS_N_INSNS (5),            /* int_mult_si */
881     COSTS_N_INSNS (5),            /* int_mult_di */
882     COSTS_N_INSNS (41),           /* int_div_si */
883     COSTS_N_INSNS (41),           /* int_div_di */
884                      1,           /* branch_cost */
885                      4            /* memory_latency */
886   },
887   { /* 74KC */
888     SOFT_FP_COSTS,
889     COSTS_N_INSNS (5),            /* int_mult_si */
890     COSTS_N_INSNS (5),            /* int_mult_di */
891     COSTS_N_INSNS (41),           /* int_div_si */
892     COSTS_N_INSNS (41),           /* int_div_di */
893                      1,           /* branch_cost */
894                      4            /* memory_latency */
895   },
896   { /* 74KF2_1 */
897     COSTS_N_INSNS (8),            /* fp_add */
898     COSTS_N_INSNS (8),            /* fp_mult_sf */
899     COSTS_N_INSNS (10),           /* fp_mult_df */
900     COSTS_N_INSNS (34),           /* fp_div_sf */
901     COSTS_N_INSNS (64),           /* fp_div_df */
902     COSTS_N_INSNS (5),            /* int_mult_si */
903     COSTS_N_INSNS (5),            /* int_mult_di */
904     COSTS_N_INSNS (41),           /* int_div_si */
905     COSTS_N_INSNS (41),           /* int_div_di */
906                      1,           /* branch_cost */
907                      4            /* memory_latency */
908   },
909   { /* 74KF1_1 */
910     COSTS_N_INSNS (4),            /* fp_add */
911     COSTS_N_INSNS (4),            /* fp_mult_sf */
912     COSTS_N_INSNS (5),            /* fp_mult_df */
913     COSTS_N_INSNS (17),           /* fp_div_sf */
914     COSTS_N_INSNS (32),           /* fp_div_df */
915     COSTS_N_INSNS (5),            /* int_mult_si */
916     COSTS_N_INSNS (5),            /* int_mult_di */
917     COSTS_N_INSNS (41),           /* int_div_si */
918     COSTS_N_INSNS (41),           /* int_div_di */
919                      1,           /* branch_cost */
920                      4            /* memory_latency */
921   },
922   { /* 74KF3_2 */
923     COSTS_N_INSNS (6),            /* fp_add */
924     COSTS_N_INSNS (6),            /* fp_mult_sf */
925     COSTS_N_INSNS (7),            /* fp_mult_df */
926     COSTS_N_INSNS (25),           /* fp_div_sf */
927     COSTS_N_INSNS (48),           /* fp_div_df */
928     COSTS_N_INSNS (5),            /* int_mult_si */
929     COSTS_N_INSNS (5),            /* int_mult_di */
930     COSTS_N_INSNS (41),           /* int_div_si */
931     COSTS_N_INSNS (41),           /* int_div_di */
932                      1,           /* branch_cost */
933                      4            /* memory_latency */
934   },
935   { /* Loongson-2E */
936     DEFAULT_COSTS
937   },
938   { /* Loongson-2F */
939     DEFAULT_COSTS
940   },
941   { /* Loongson-3A */
942     DEFAULT_COSTS
943   },
944   { /* M4k */
945     DEFAULT_COSTS
946   },
947     /* Octeon */
948   {
949     SOFT_FP_COSTS,
950     COSTS_N_INSNS (5),            /* int_mult_si */
951     COSTS_N_INSNS (5),            /* int_mult_di */
952     COSTS_N_INSNS (72),           /* int_div_si */
953     COSTS_N_INSNS (72),           /* int_div_di */
954                      1,           /* branch_cost */
955                      4            /* memory_latency */
956   },
957     /* Octeon II */
958   {
959     SOFT_FP_COSTS,
960     COSTS_N_INSNS (6),            /* int_mult_si */
961     COSTS_N_INSNS (6),            /* int_mult_di */
962     COSTS_N_INSNS (18),           /* int_div_si */
963     COSTS_N_INSNS (35),           /* int_div_di */
964                      4,           /* branch_cost */
965                      4            /* memory_latency */
966   },
967   { /* R3900 */
968     COSTS_N_INSNS (2),            /* fp_add */
969     COSTS_N_INSNS (4),            /* fp_mult_sf */
970     COSTS_N_INSNS (5),            /* fp_mult_df */
971     COSTS_N_INSNS (12),           /* fp_div_sf */
972     COSTS_N_INSNS (19),           /* fp_div_df */
973     COSTS_N_INSNS (2),            /* int_mult_si */
974     COSTS_N_INSNS (2),            /* int_mult_di */
975     COSTS_N_INSNS (35),           /* int_div_si */
976     COSTS_N_INSNS (35),           /* int_div_di */
977                      1,           /* branch_cost */
978                      4            /* memory_latency */
979   },
980   { /* R6000 */
981     COSTS_N_INSNS (3),            /* fp_add */
982     COSTS_N_INSNS (5),            /* fp_mult_sf */
983     COSTS_N_INSNS (6),            /* fp_mult_df */
984     COSTS_N_INSNS (15),           /* fp_div_sf */
985     COSTS_N_INSNS (16),           /* fp_div_df */
986     COSTS_N_INSNS (17),           /* int_mult_si */
987     COSTS_N_INSNS (17),           /* int_mult_di */
988     COSTS_N_INSNS (38),           /* int_div_si */
989     COSTS_N_INSNS (38),           /* int_div_di */
990                      2,           /* branch_cost */
991                      6            /* memory_latency */
992   },
993   { /* R4000 */
994      COSTS_N_INSNS (6),           /* fp_add */
995      COSTS_N_INSNS (7),           /* fp_mult_sf */
996      COSTS_N_INSNS (8),           /* fp_mult_df */
997      COSTS_N_INSNS (23),          /* fp_div_sf */
998      COSTS_N_INSNS (36),          /* fp_div_df */
999      COSTS_N_INSNS (10),          /* int_mult_si */
1000      COSTS_N_INSNS (10),          /* int_mult_di */
1001      COSTS_N_INSNS (69),          /* int_div_si */
1002      COSTS_N_INSNS (69),          /* int_div_di */
1003                       2,          /* branch_cost */
1004                       6           /* memory_latency */
1005   },
1006   { /* R4100 */
1007     DEFAULT_COSTS
1008   },
1009   { /* R4111 */
1010     DEFAULT_COSTS
1011   },
1012   { /* R4120 */
1013     DEFAULT_COSTS
1014   },
1015   { /* R4130 */
1016     /* The only costs that appear to be updated here are
1017        integer multiplication.  */
1018     SOFT_FP_COSTS,
1019     COSTS_N_INSNS (4),            /* int_mult_si */
1020     COSTS_N_INSNS (6),            /* int_mult_di */
1021     COSTS_N_INSNS (69),           /* int_div_si */
1022     COSTS_N_INSNS (69),           /* int_div_di */
1023                      1,           /* branch_cost */
1024                      4            /* memory_latency */
1025   },
1026   { /* R4300 */
1027     DEFAULT_COSTS
1028   },
1029   { /* R4600 */
1030     DEFAULT_COSTS
1031   },
1032   { /* R4650 */
1033     DEFAULT_COSTS
1034   },
1035   { /* R4700 */
1036     DEFAULT_COSTS
1037   },
1038   { /* R5000 */
1039     COSTS_N_INSNS (6),            /* fp_add */
1040     COSTS_N_INSNS (4),            /* fp_mult_sf */
1041     COSTS_N_INSNS (5),            /* fp_mult_df */
1042     COSTS_N_INSNS (23),           /* fp_div_sf */
1043     COSTS_N_INSNS (36),           /* fp_div_df */
1044     COSTS_N_INSNS (5),            /* int_mult_si */
1045     COSTS_N_INSNS (5),            /* int_mult_di */
1046     COSTS_N_INSNS (36),           /* int_div_si */
1047     COSTS_N_INSNS (36),           /* int_div_di */
1048                      1,           /* branch_cost */
1049                      4            /* memory_latency */
1050   },
1051   { /* R5400 */
1052     COSTS_N_INSNS (6),            /* fp_add */
1053     COSTS_N_INSNS (5),            /* fp_mult_sf */
1054     COSTS_N_INSNS (6),            /* fp_mult_df */
1055     COSTS_N_INSNS (30),           /* fp_div_sf */
1056     COSTS_N_INSNS (59),           /* fp_div_df */
1057     COSTS_N_INSNS (3),            /* int_mult_si */
1058     COSTS_N_INSNS (4),            /* int_mult_di */
1059     COSTS_N_INSNS (42),           /* int_div_si */
1060     COSTS_N_INSNS (74),           /* int_div_di */
1061                      1,           /* branch_cost */
1062                      4            /* memory_latency */
1063   },
1064   { /* R5500 */
1065     COSTS_N_INSNS (6),            /* fp_add */
1066     COSTS_N_INSNS (5),            /* fp_mult_sf */
1067     COSTS_N_INSNS (6),            /* fp_mult_df */
1068     COSTS_N_INSNS (30),           /* fp_div_sf */
1069     COSTS_N_INSNS (59),           /* fp_div_df */
1070     COSTS_N_INSNS (5),            /* int_mult_si */
1071     COSTS_N_INSNS (9),            /* int_mult_di */
1072     COSTS_N_INSNS (42),           /* int_div_si */
1073     COSTS_N_INSNS (74),           /* int_div_di */
1074                      1,           /* branch_cost */
1075                      4            /* memory_latency */
1076   },
1077   { /* R5900 */
1078     COSTS_N_INSNS (4),            /* fp_add */
1079     COSTS_N_INSNS (4),            /* fp_mult_sf */
1080     COSTS_N_INSNS (256),          /* fp_mult_df */
1081     COSTS_N_INSNS (8),            /* fp_div_sf */
1082     COSTS_N_INSNS (256),          /* fp_div_df */
1083     COSTS_N_INSNS (4),            /* int_mult_si */
1084     COSTS_N_INSNS (256),          /* int_mult_di */
1085     COSTS_N_INSNS (37),           /* int_div_si */
1086     COSTS_N_INSNS (256),          /* int_div_di */
1087                      1,           /* branch_cost */
1088                      4            /* memory_latency */
1089   },
1090   { /* R7000 */
1091     /* The only costs that are changed here are
1092        integer multiplication.  */
1093     COSTS_N_INSNS (6),            /* fp_add */
1094     COSTS_N_INSNS (7),            /* fp_mult_sf */
1095     COSTS_N_INSNS (8),            /* fp_mult_df */
1096     COSTS_N_INSNS (23),           /* fp_div_sf */
1097     COSTS_N_INSNS (36),           /* fp_div_df */
1098     COSTS_N_INSNS (5),            /* int_mult_si */
1099     COSTS_N_INSNS (9),            /* int_mult_di */
1100     COSTS_N_INSNS (69),           /* int_div_si */
1101     COSTS_N_INSNS (69),           /* int_div_di */
1102                      1,           /* branch_cost */
1103                      4            /* memory_latency */
1104   },
1105   { /* R8000 */
1106     DEFAULT_COSTS
1107   },
1108   { /* R9000 */
1109     /* The only costs that are changed here are
1110        integer multiplication.  */
1111     COSTS_N_INSNS (6),            /* fp_add */
1112     COSTS_N_INSNS (7),            /* fp_mult_sf */
1113     COSTS_N_INSNS (8),            /* fp_mult_df */
1114     COSTS_N_INSNS (23),           /* fp_div_sf */
1115     COSTS_N_INSNS (36),           /* fp_div_df */
1116     COSTS_N_INSNS (3),            /* int_mult_si */
1117     COSTS_N_INSNS (8),            /* int_mult_di */
1118     COSTS_N_INSNS (69),           /* int_div_si */
1119     COSTS_N_INSNS (69),           /* int_div_di */
1120                      1,           /* branch_cost */
1121                      4            /* memory_latency */
1122   },
1123   { /* R1x000 */
1124     COSTS_N_INSNS (2),            /* fp_add */
1125     COSTS_N_INSNS (2),            /* fp_mult_sf */
1126     COSTS_N_INSNS (2),            /* fp_mult_df */
1127     COSTS_N_INSNS (12),           /* fp_div_sf */
1128     COSTS_N_INSNS (19),           /* fp_div_df */
1129     COSTS_N_INSNS (5),            /* int_mult_si */
1130     COSTS_N_INSNS (9),            /* int_mult_di */
1131     COSTS_N_INSNS (34),           /* int_div_si */
1132     COSTS_N_INSNS (66),           /* int_div_di */
1133                      1,           /* branch_cost */
1134                      4            /* memory_latency */
1135   },
1136   { /* SB1 */
1137     /* These costs are the same as the SB-1A below.  */
1138     COSTS_N_INSNS (4),            /* fp_add */
1139     COSTS_N_INSNS (4),            /* fp_mult_sf */
1140     COSTS_N_INSNS (4),            /* fp_mult_df */
1141     COSTS_N_INSNS (24),           /* fp_div_sf */
1142     COSTS_N_INSNS (32),           /* fp_div_df */
1143     COSTS_N_INSNS (3),            /* int_mult_si */
1144     COSTS_N_INSNS (4),            /* int_mult_di */
1145     COSTS_N_INSNS (36),           /* int_div_si */
1146     COSTS_N_INSNS (68),           /* int_div_di */
1147                      1,           /* branch_cost */
1148                      4            /* memory_latency */
1149   },
1150   { /* SB1-A */
1151     /* These costs are the same as the SB-1 above.  */
1152     COSTS_N_INSNS (4),            /* fp_add */
1153     COSTS_N_INSNS (4),            /* fp_mult_sf */
1154     COSTS_N_INSNS (4),            /* fp_mult_df */
1155     COSTS_N_INSNS (24),           /* fp_div_sf */
1156     COSTS_N_INSNS (32),           /* fp_div_df */
1157     COSTS_N_INSNS (3),            /* int_mult_si */
1158     COSTS_N_INSNS (4),            /* int_mult_di */
1159     COSTS_N_INSNS (36),           /* int_div_si */
1160     COSTS_N_INSNS (68),           /* int_div_di */
1161                      1,           /* branch_cost */
1162                      4            /* memory_latency */
1163   },
1164   { /* SR71000 */
1165     DEFAULT_COSTS
1166   },
1167   { /* XLR */
1168     SOFT_FP_COSTS,
1169     COSTS_N_INSNS (8),            /* int_mult_si */
1170     COSTS_N_INSNS (8),            /* int_mult_di */
1171     COSTS_N_INSNS (72),           /* int_div_si */
1172     COSTS_N_INSNS (72),           /* int_div_di */
1173                      1,           /* branch_cost */
1174                      4            /* memory_latency */
1175   },
1176   { /* XLP */
1177     /* These costs are the same as 5KF above.  */
1178     COSTS_N_INSNS (4),            /* fp_add */
1179     COSTS_N_INSNS (4),            /* fp_mult_sf */
1180     COSTS_N_INSNS (5),            /* fp_mult_df */
1181     COSTS_N_INSNS (17),           /* fp_div_sf */
1182     COSTS_N_INSNS (32),           /* fp_div_df */
1183     COSTS_N_INSNS (4),            /* int_mult_si */
1184     COSTS_N_INSNS (11),           /* int_mult_di */
1185     COSTS_N_INSNS (36),           /* int_div_si */
1186     COSTS_N_INSNS (68),           /* int_div_di */
1187                      1,           /* branch_cost */
1188                      4            /* memory_latency */
1189   },
1190   { /* P5600 */
1191     COSTS_N_INSNS (4),            /* fp_add */
1192     COSTS_N_INSNS (5),            /* fp_mult_sf */
1193     COSTS_N_INSNS (5),            /* fp_mult_df */
1194     COSTS_N_INSNS (17),           /* fp_div_sf */
1195     COSTS_N_INSNS (17),           /* fp_div_df */
1196     COSTS_N_INSNS (5),            /* int_mult_si */
1197     COSTS_N_INSNS (5),            /* int_mult_di */
1198     COSTS_N_INSNS (8),            /* int_div_si */
1199     COSTS_N_INSNS (8),            /* int_div_di */
1200                     2,            /* branch_cost */
1201                    10             /* memory_latency */
1202   }
1203 };
1204 \f
1205 static rtx mips_find_pic_call_symbol (rtx_insn *, rtx, bool);
1206 static int mips_register_move_cost (enum machine_mode, reg_class_t,
1207                                     reg_class_t);
1208 static unsigned int mips_function_arg_boundary (enum machine_mode, const_tree);
1209 \f
1210 struct mips16_flip_traits : default_hashmap_traits
1211 {
1212   static hashval_t hash (const char *s) { return htab_hash_string (s); }
1213   static bool
1214   equal_keys (const char *a, const char *b)
1215   {
1216     return !strcmp (a, b);
1217   }
1218 };
1219
1220 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1221    for -mflip_mips16.  It maps decl names onto a boolean mode setting.  */
1222 static GTY (()) hash_map<const char *, bool, mips16_flip_traits> *
1223   mflip_mips16_htab;
1224
1225 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1226    mode, false if it should next add an attribute for the opposite mode.  */
1227 static GTY(()) bool mips16_flipper;
1228
1229 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1230    for -mflip-mips16.  Return true if it should use "mips16" and false if
1231    it should use "nomips16".  */
1232
1233 static bool
1234 mflip_mips16_use_mips16_p (tree decl)
1235 {
1236   const char *name;
1237   bool base_is_mips16 = (mips_base_compression_flags & MASK_MIPS16) != 0;
1238
1239   /* Use the opposite of the command-line setting for anonymous decls.  */
1240   if (!DECL_NAME (decl))
1241     return !base_is_mips16;
1242
1243   if (!mflip_mips16_htab)
1244     mflip_mips16_htab
1245       = hash_map<const char *, bool, mips16_flip_traits>::create_ggc (37);
1246
1247   name = IDENTIFIER_POINTER (DECL_NAME (decl));
1248
1249   bool existed;
1250   bool *slot = &mflip_mips16_htab->get_or_insert (name, &existed);
1251   if (!existed)
1252     {
1253       mips16_flipper = !mips16_flipper;
1254       *slot = mips16_flipper ? !base_is_mips16 : base_is_mips16;
1255     }
1256   return *slot;
1257 }
1258 \f
1259 /* Predicates to test for presence of "near" and "far"/"long_call"
1260    attributes on the given TYPE.  */
1261
1262 static bool
1263 mips_near_type_p (const_tree type)
1264 {
1265   return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1266 }
1267
1268 static bool
1269 mips_far_type_p (const_tree type)
1270 {
1271   return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1272           || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1273 }
1274
1275
1276 /* Check if the interrupt attribute is set for a function.  */
1277
1278 static bool
1279 mips_interrupt_type_p (tree type)
1280 {
1281   return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1282 }
1283
1284 /* Check if the attribute to use shadow register set is set for a function.  */
1285
1286 static bool
1287 mips_use_shadow_register_set_p (tree type)
1288 {
1289   return lookup_attribute ("use_shadow_register_set",
1290                            TYPE_ATTRIBUTES (type)) != NULL;
1291 }
1292
1293 /* Check if the attribute to keep interrupts masked is set for a function.  */
1294
1295 static bool
1296 mips_keep_interrupts_masked_p (tree type)
1297 {
1298   return lookup_attribute ("keep_interrupts_masked",
1299                            TYPE_ATTRIBUTES (type)) != NULL;
1300 }
1301
1302 /* Check if the attribute to use debug exception return is set for
1303    a function.  */
1304
1305 static bool
1306 mips_use_debug_exception_return_p (tree type)
1307 {
1308   return lookup_attribute ("use_debug_exception_return",
1309                            TYPE_ATTRIBUTES (type)) != NULL;
1310 }
1311
1312 /* Return the set of compression modes that are explicitly required
1313    by the attributes in ATTRIBUTES.  */
1314
1315 static unsigned int
1316 mips_get_compress_on_flags (tree attributes)
1317 {
1318   unsigned int flags = 0;
1319
1320   if (lookup_attribute ("mips16", attributes) != NULL)
1321     flags |= MASK_MIPS16;
1322
1323   if (lookup_attribute ("micromips", attributes) != NULL)
1324     flags |= MASK_MICROMIPS;
1325
1326   return flags;
1327 }
1328
1329 /* Return the set of compression modes that are explicitly forbidden
1330    by the attributes in ATTRIBUTES.  */
1331
1332 static unsigned int
1333 mips_get_compress_off_flags (tree attributes)
1334 {
1335   unsigned int flags = 0;
1336
1337   if (lookup_attribute ("nocompression", attributes) != NULL)
1338     flags |= MASK_MIPS16 | MASK_MICROMIPS;
1339
1340   if (lookup_attribute ("nomips16", attributes) != NULL)
1341     flags |= MASK_MIPS16;
1342
1343   if (lookup_attribute ("nomicromips", attributes) != NULL)
1344     flags |= MASK_MICROMIPS;
1345
1346   return flags;
1347 }
1348
1349 /* Return the compression mode that should be used for function DECL.
1350    Return the ambient setting if DECL is null.  */
1351
1352 static unsigned int
1353 mips_get_compress_mode (tree decl)
1354 {
1355   unsigned int flags, force_on;
1356
1357   flags = mips_base_compression_flags;
1358   if (decl)
1359     {
1360       /* Nested functions must use the same frame pointer as their
1361          parent and must therefore use the same ISA mode.  */
1362       tree parent = decl_function_context (decl);
1363       if (parent)
1364         decl = parent;
1365       force_on = mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1366       if (force_on)
1367         return force_on;
1368       flags &= ~mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1369     }
1370   return flags;
1371 }
1372
1373 /* Return the attribute name associated with MASK_MIPS16 and MASK_MICROMIPS
1374    flags FLAGS.  */
1375
1376 static const char *
1377 mips_get_compress_on_name (unsigned int flags)
1378 {
1379   if (flags == MASK_MIPS16)
1380     return "mips16";
1381   return "micromips";
1382 }
1383
1384 /* Return the attribute name that forbids MASK_MIPS16 and MASK_MICROMIPS
1385    flags FLAGS.  */
1386
1387 static const char *
1388 mips_get_compress_off_name (unsigned int flags)
1389 {
1390   if (flags == MASK_MIPS16)
1391     return "nomips16";
1392   if (flags == MASK_MICROMIPS)
1393     return "nomicromips";
1394   return "nocompression";
1395 }
1396
1397 /* Implement TARGET_COMP_TYPE_ATTRIBUTES.  */
1398
1399 static int
1400 mips_comp_type_attributes (const_tree type1, const_tree type2)
1401 {
1402   /* Disallow mixed near/far attributes.  */
1403   if (mips_far_type_p (type1) && mips_near_type_p (type2))
1404     return 0;
1405   if (mips_near_type_p (type1) && mips_far_type_p (type2))
1406     return 0;
1407   return 1;
1408 }
1409
1410 /* Implement TARGET_INSERT_ATTRIBUTES.  */
1411
1412 static void
1413 mips_insert_attributes (tree decl, tree *attributes)
1414 {
1415   const char *name;
1416   unsigned int compression_flags, nocompression_flags;
1417
1418   /* Check for "mips16" and "nomips16" attributes.  */
1419   compression_flags = mips_get_compress_on_flags (*attributes);
1420   nocompression_flags = mips_get_compress_off_flags (*attributes);
1421
1422   if (TREE_CODE (decl) != FUNCTION_DECL)
1423     {
1424       if (nocompression_flags)
1425         error ("%qs attribute only applies to functions",
1426                mips_get_compress_off_name (nocompression_flags));
1427
1428       if (compression_flags)
1429         error ("%qs attribute only applies to functions",
1430                mips_get_compress_on_name (nocompression_flags));
1431     }
1432   else
1433     {
1434       compression_flags |= mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1435       nocompression_flags |=
1436         mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1437
1438       if (compression_flags && nocompression_flags)
1439         error ("%qE cannot have both %qs and %qs attributes",
1440                DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
1441                mips_get_compress_off_name (nocompression_flags));
1442
1443       if (compression_flags & MASK_MIPS16
1444           && compression_flags & MASK_MICROMIPS)
1445         error ("%qE cannot have both %qs and %qs attributes",
1446                DECL_NAME (decl), "mips16", "micromips");
1447
1448       if (TARGET_FLIP_MIPS16
1449           && !DECL_ARTIFICIAL (decl)
1450           && compression_flags == 0
1451           && nocompression_flags == 0)
1452         {
1453           /* Implement -mflip-mips16.  If DECL has neither a "nomips16" nor a
1454              "mips16" attribute, arbitrarily pick one.  We must pick the same
1455              setting for duplicate declarations of a function.  */
1456           name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1457           *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1458           name = "nomicromips";
1459           *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1460         }
1461     }
1462 }
1463
1464 /* Implement TARGET_MERGE_DECL_ATTRIBUTES.  */
1465
1466 static tree
1467 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1468 {
1469   unsigned int diff;
1470
1471   diff = (mips_get_compress_on_flags (DECL_ATTRIBUTES (olddecl))
1472           ^ mips_get_compress_on_flags (DECL_ATTRIBUTES (newdecl)));
1473   if (diff)
1474     error ("%qE redeclared with conflicting %qs attributes",
1475            DECL_NAME (newdecl), mips_get_compress_on_name (diff));
1476
1477   diff = (mips_get_compress_off_flags (DECL_ATTRIBUTES (olddecl))
1478           ^ mips_get_compress_off_flags (DECL_ATTRIBUTES (newdecl)));
1479   if (diff)
1480     error ("%qE redeclared with conflicting %qs attributes",
1481            DECL_NAME (newdecl), mips_get_compress_off_name (diff));
1482
1483   return merge_attributes (DECL_ATTRIBUTES (olddecl),
1484                            DECL_ATTRIBUTES (newdecl));
1485 }
1486
1487 /* Implement TARGET_CAN_INLINE_P.  */
1488
1489 static bool
1490 mips_can_inline_p (tree caller, tree callee)
1491 {
1492   if (mips_get_compress_mode (callee) != mips_get_compress_mode (caller))
1493     return false;
1494   return default_target_can_inline_p (caller, callee);
1495 }
1496 \f
1497 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1498    and *OFFSET_PTR.  Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise.  */
1499
1500 static void
1501 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1502 {
1503   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1504     {
1505       *base_ptr = XEXP (x, 0);
1506       *offset_ptr = INTVAL (XEXP (x, 1));
1507     }
1508   else
1509     {
1510       *base_ptr = x;
1511       *offset_ptr = 0;
1512     }
1513 }
1514 \f
1515 static unsigned int mips_build_integer (struct mips_integer_op *,
1516                                         unsigned HOST_WIDE_INT);
1517
1518 /* A subroutine of mips_build_integer, with the same interface.
1519    Assume that the final action in the sequence should be a left shift.  */
1520
1521 static unsigned int
1522 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1523 {
1524   unsigned int i, shift;
1525
1526   /* Shift VALUE right until its lowest bit is set.  Shift arithmetically
1527      since signed numbers are easier to load than unsigned ones.  */
1528   shift = 0;
1529   while ((value & 1) == 0)
1530     value /= 2, shift++;
1531
1532   i = mips_build_integer (codes, value);
1533   codes[i].code = ASHIFT;
1534   codes[i].value = shift;
1535   return i + 1;
1536 }
1537
1538 /* As for mips_build_shift, but assume that the final action will be
1539    an IOR or PLUS operation.  */
1540
1541 static unsigned int
1542 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1543 {
1544   unsigned HOST_WIDE_INT high;
1545   unsigned int i;
1546
1547   high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1548   if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1549     {
1550       /* The constant is too complex to load with a simple LUI/ORI pair,
1551          so we want to give the recursive call as many trailing zeros as
1552          possible.  In this case, we know bit 16 is set and that the
1553          low 16 bits form a negative number.  If we subtract that number
1554          from VALUE, we will clear at least the lowest 17 bits, maybe more.  */
1555       i = mips_build_integer (codes, CONST_HIGH_PART (value));
1556       codes[i].code = PLUS;
1557       codes[i].value = CONST_LOW_PART (value);
1558     }
1559   else
1560     {
1561       /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1562          bits gives a value with at least 17 trailing zeros.  */
1563       i = mips_build_integer (codes, high);
1564       codes[i].code = IOR;
1565       codes[i].value = value & 0xffff;
1566     }
1567   return i + 1;
1568 }
1569
1570 /* Fill CODES with a sequence of rtl operations to load VALUE.
1571    Return the number of operations needed.  */
1572
1573 static unsigned int
1574 mips_build_integer (struct mips_integer_op *codes,
1575                     unsigned HOST_WIDE_INT value)
1576 {
1577   if (SMALL_OPERAND (value)
1578       || SMALL_OPERAND_UNSIGNED (value)
1579       || LUI_OPERAND (value))
1580     {
1581       /* The value can be loaded with a single instruction.  */
1582       codes[0].code = UNKNOWN;
1583       codes[0].value = value;
1584       return 1;
1585     }
1586   else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1587     {
1588       /* Either the constant is a simple LUI/ORI combination or its
1589          lowest bit is set.  We don't want to shift in this case.  */
1590       return mips_build_lower (codes, value);
1591     }
1592   else if ((value & 0xffff) == 0)
1593     {
1594       /* The constant will need at least three actions.  The lowest
1595          16 bits are clear, so the final action will be a shift.  */
1596       return mips_build_shift (codes, value);
1597     }
1598   else
1599     {
1600       /* The final action could be a shift, add or inclusive OR.
1601          Rather than use a complex condition to select the best
1602          approach, try both mips_build_shift and mips_build_lower
1603          and pick the one that gives the shortest sequence.
1604          Note that this case is only used once per constant.  */
1605       struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1606       unsigned int cost, alt_cost;
1607
1608       cost = mips_build_shift (codes, value);
1609       alt_cost = mips_build_lower (alt_codes, value);
1610       if (alt_cost < cost)
1611         {
1612           memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1613           cost = alt_cost;
1614         }
1615       return cost;
1616     }
1617 }
1618 \f
1619 /* Implement TARGET_LEGITIMATE_CONSTANT_P.  */
1620
1621 static bool
1622 mips_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1623 {
1624   return mips_const_insns (x) > 0;
1625 }
1626 \f
1627 /* Return a SYMBOL_REF for a MIPS16 function called NAME.  */
1628
1629 static rtx
1630 mips16_stub_function (const char *name)
1631 {
1632   rtx x;
1633
1634   x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1635   SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1636   return x;
1637 }
1638
1639 /* Return a legitimate call address for STUB, given that STUB is a MIPS16
1640    support function.  */
1641
1642 static rtx
1643 mips16_stub_call_address (mips_one_only_stub *stub)
1644 {
1645   rtx fn = mips16_stub_function (stub->get_name ());
1646   SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
1647   if (!call_insn_operand (fn, VOIDmode))
1648     fn = force_reg (Pmode, fn);
1649   return fn;
1650 }
1651 \f
1652 /* A stub for moving the thread pointer into TLS_GET_TP_REGNUM.  */
1653
1654 class mips16_rdhwr_one_only_stub : public mips_one_only_stub
1655 {
1656   virtual const char *get_name ();
1657   virtual void output_body ();
1658 };
1659
1660 const char *
1661 mips16_rdhwr_one_only_stub::get_name ()
1662 {
1663   return "__mips16_rdhwr";
1664 }
1665
1666 void
1667 mips16_rdhwr_one_only_stub::output_body ()
1668 {
1669   fprintf (asm_out_file,
1670            "\t.set\tpush\n"
1671            "\t.set\tmips32r2\n"
1672            "\t.set\tnoreorder\n"
1673            "\trdhwr\t$3,$29\n"
1674            "\t.set\tpop\n"
1675            "\tj\t$31\n");
1676 }
1677
1678 /* A stub for moving the FCSR into GET_FCSR_REGNUM.  */
1679 class mips16_get_fcsr_one_only_stub : public mips_one_only_stub
1680 {
1681   virtual const char *get_name ();
1682   virtual void output_body ();
1683 };
1684
1685 const char *
1686 mips16_get_fcsr_one_only_stub::get_name ()
1687 {
1688   return "__mips16_get_fcsr";
1689 }
1690
1691 void
1692 mips16_get_fcsr_one_only_stub::output_body ()
1693 {
1694   fprintf (asm_out_file,
1695            "\tcfc1\t%s,$31\n"
1696            "\tj\t$31\n", reg_names[GET_FCSR_REGNUM]);
1697 }
1698
1699 /* A stub for moving SET_FCSR_REGNUM into the FCSR.  */
1700 class mips16_set_fcsr_one_only_stub : public mips_one_only_stub
1701 {
1702   virtual const char *get_name ();
1703   virtual void output_body ();
1704 };
1705
1706 const char *
1707 mips16_set_fcsr_one_only_stub::get_name ()
1708 {
1709   return "__mips16_set_fcsr";
1710 }
1711
1712 void
1713 mips16_set_fcsr_one_only_stub::output_body ()
1714 {
1715   fprintf (asm_out_file,
1716            "\tctc1\t%s,$31\n"
1717            "\tj\t$31\n", reg_names[SET_FCSR_REGNUM]);
1718 }
1719 \f
1720 /* Return true if symbols of type TYPE require a GOT access.  */
1721
1722 static bool
1723 mips_got_symbol_type_p (enum mips_symbol_type type)
1724 {
1725   switch (type)
1726     {
1727     case SYMBOL_GOT_PAGE_OFST:
1728     case SYMBOL_GOT_DISP:
1729       return true;
1730
1731     default:
1732       return false;
1733     }
1734 }
1735
1736 /* Return true if X is a thread-local symbol.  */
1737
1738 static bool
1739 mips_tls_symbol_p (rtx x)
1740 {
1741   return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1742 }
1743
1744 /* Return true if SYMBOL_REF X is associated with a global symbol
1745    (in the STB_GLOBAL sense).  */
1746
1747 static bool
1748 mips_global_symbol_p (const_rtx x)
1749 {
1750   const_tree decl = SYMBOL_REF_DECL (x);
1751
1752   if (!decl)
1753     return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1754
1755   /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1756      or weak symbols.  Relocations in the object file will be against
1757      the target symbol, so it's that symbol's binding that matters here.  */
1758   return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1759 }
1760
1761 /* Return true if function X is a libgcc MIPS16 stub function.  */
1762
1763 static bool
1764 mips16_stub_function_p (const_rtx x)
1765 {
1766   return (GET_CODE (x) == SYMBOL_REF
1767           && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1768 }
1769
1770 /* Return true if function X is a locally-defined and locally-binding
1771    MIPS16 function.  */
1772
1773 static bool
1774 mips16_local_function_p (const_rtx x)
1775 {
1776   return (GET_CODE (x) == SYMBOL_REF
1777           && SYMBOL_REF_LOCAL_P (x)
1778           && !SYMBOL_REF_EXTERNAL_P (x)
1779           && (mips_get_compress_mode (SYMBOL_REF_DECL (x)) & MASK_MIPS16));
1780 }
1781
1782 /* Return true if SYMBOL_REF X binds locally.  */
1783
1784 static bool
1785 mips_symbol_binds_local_p (const_rtx x)
1786 {
1787   return (SYMBOL_REF_DECL (x)
1788           ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1789           : SYMBOL_REF_LOCAL_P (x));
1790 }
1791
1792 /* Return true if rtx constants of mode MODE should be put into a small
1793    data section.  */
1794
1795 static bool
1796 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1797 {
1798   return (!TARGET_EMBEDDED_DATA
1799           && TARGET_LOCAL_SDATA
1800           && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1801 }
1802
1803 /* Return true if X should not be moved directly into register $25.
1804    We need this because many versions of GAS will treat "la $25,foo" as
1805    part of a call sequence and so allow a global "foo" to be lazily bound.  */
1806
1807 bool
1808 mips_dangerous_for_la25_p (rtx x)
1809 {
1810   return (!TARGET_EXPLICIT_RELOCS
1811           && TARGET_USE_GOT
1812           && GET_CODE (x) == SYMBOL_REF
1813           && mips_global_symbol_p (x));
1814 }
1815
1816 /* Return true if calls to X might need $25 to be valid on entry.  */
1817
1818 bool
1819 mips_use_pic_fn_addr_reg_p (const_rtx x)
1820 {
1821   if (!TARGET_USE_PIC_FN_ADDR_REG)
1822     return false;
1823
1824   /* MIPS16 stub functions are guaranteed not to use $25.  */
1825   if (mips16_stub_function_p (x))
1826     return false;
1827
1828   if (GET_CODE (x) == SYMBOL_REF)
1829     {
1830       /* If PLTs and copy relocations are available, the static linker
1831          will make sure that $25 is valid on entry to the target function.  */
1832       if (TARGET_ABICALLS_PIC0)
1833         return false;
1834
1835       /* Locally-defined functions use absolute accesses to set up
1836          the global pointer.  */
1837       if (TARGET_ABSOLUTE_ABICALLS
1838           && mips_symbol_binds_local_p (x)
1839           && !SYMBOL_REF_EXTERNAL_P (x))
1840         return false;
1841     }
1842
1843   return true;
1844 }
1845
1846 /* Return the method that should be used to access SYMBOL_REF or
1847    LABEL_REF X in context CONTEXT.  */
1848
1849 static enum mips_symbol_type
1850 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1851 {
1852   if (TARGET_RTP_PIC)
1853     return SYMBOL_GOT_DISP;
1854
1855   if (GET_CODE (x) == LABEL_REF)
1856     {
1857       /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
1858          code and if we know that the label is in the current function's
1859          text section.  LABEL_REFs are used for jump tables as well as
1860          text labels, so we must check whether jump tables live in the
1861          text section.  */
1862       if (TARGET_MIPS16_SHORT_JUMP_TABLES
1863           && !LABEL_REF_NONLOCAL_P (x))
1864         return SYMBOL_PC_RELATIVE;
1865
1866       if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1867         return SYMBOL_GOT_PAGE_OFST;
1868
1869       return SYMBOL_ABSOLUTE;
1870     }
1871
1872   gcc_assert (GET_CODE (x) == SYMBOL_REF);
1873
1874   if (SYMBOL_REF_TLS_MODEL (x))
1875     return SYMBOL_TLS;
1876
1877   if (CONSTANT_POOL_ADDRESS_P (x))
1878     {
1879       if (TARGET_MIPS16_TEXT_LOADS)
1880         return SYMBOL_PC_RELATIVE;
1881
1882       if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1883         return SYMBOL_PC_RELATIVE;
1884
1885       if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1886         return SYMBOL_GP_RELATIVE;
1887     }
1888
1889   /* Do not use small-data accesses for weak symbols; they may end up
1890      being zero.  */
1891   if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1892     return SYMBOL_GP_RELATIVE;
1893
1894   /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1895      is in effect.  */
1896   if (TARGET_ABICALLS_PIC2
1897       && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1898     {
1899       /* There are three cases to consider:
1900
1901             - o32 PIC (either with or without explicit relocs)
1902             - n32/n64 PIC without explicit relocs
1903             - n32/n64 PIC with explicit relocs
1904
1905          In the first case, both local and global accesses will use an
1906          R_MIPS_GOT16 relocation.  We must correctly predict which of
1907          the two semantics (local or global) the assembler and linker
1908          will apply.  The choice depends on the symbol's binding rather
1909          than its visibility.
1910
1911          In the second case, the assembler will not use R_MIPS_GOT16
1912          relocations, but it chooses between local and global accesses
1913          in the same way as for o32 PIC.
1914
1915          In the third case we have more freedom since both forms of
1916          access will work for any kind of symbol.  However, there seems
1917          little point in doing things differently.  */
1918       if (mips_global_symbol_p (x))
1919         return SYMBOL_GOT_DISP;
1920
1921       return SYMBOL_GOT_PAGE_OFST;
1922     }
1923
1924   return SYMBOL_ABSOLUTE;
1925 }
1926
1927 /* Classify the base of symbolic expression X, given that X appears in
1928    context CONTEXT.  */
1929
1930 static enum mips_symbol_type
1931 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1932 {
1933   rtx offset;
1934
1935   split_const (x, &x, &offset);
1936   if (UNSPEC_ADDRESS_P (x))
1937     return UNSPEC_ADDRESS_TYPE (x);
1938
1939   return mips_classify_symbol (x, context);
1940 }
1941
1942 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1943    is the alignment in bytes of SYMBOL_REF X.  */
1944
1945 static bool
1946 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1947 {
1948   HOST_WIDE_INT align;
1949
1950   align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1951   return IN_RANGE (offset, 0, align - 1);
1952 }
1953
1954 /* Return true if X is a symbolic constant that can be used in context
1955    CONTEXT.  If it is, store the type of the symbol in *SYMBOL_TYPE.  */
1956
1957 bool
1958 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1959                           enum mips_symbol_type *symbol_type)
1960 {
1961   rtx offset;
1962
1963   split_const (x, &x, &offset);
1964   if (UNSPEC_ADDRESS_P (x))
1965     {
1966       *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1967       x = UNSPEC_ADDRESS (x);
1968     }
1969   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1970     {
1971       *symbol_type = mips_classify_symbol (x, context);
1972       if (*symbol_type == SYMBOL_TLS)
1973         return false;
1974     }
1975   else
1976     return false;
1977
1978   if (offset == const0_rtx)
1979     return true;
1980
1981   /* Check whether a nonzero offset is valid for the underlying
1982      relocations.  */
1983   switch (*symbol_type)
1984     {
1985     case SYMBOL_ABSOLUTE:
1986     case SYMBOL_64_HIGH:
1987     case SYMBOL_64_MID:
1988     case SYMBOL_64_LOW:
1989       /* If the target has 64-bit pointers and the object file only
1990          supports 32-bit symbols, the values of those symbols will be
1991          sign-extended.  In this case we can't allow an arbitrary offset
1992          in case the 32-bit value X + OFFSET has a different sign from X.  */
1993       if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1994         return offset_within_block_p (x, INTVAL (offset));
1995
1996       /* In other cases the relocations can handle any offset.  */
1997       return true;
1998
1999     case SYMBOL_PC_RELATIVE:
2000       /* Allow constant pool references to be converted to LABEL+CONSTANT.
2001          In this case, we no longer have access to the underlying constant,
2002          but the original symbol-based access was known to be valid.  */
2003       if (GET_CODE (x) == LABEL_REF)
2004         return true;
2005
2006       /* Fall through.  */
2007
2008     case SYMBOL_GP_RELATIVE:
2009       /* Make sure that the offset refers to something within the
2010          same object block.  This should guarantee that the final
2011          PC- or GP-relative offset is within the 16-bit limit.  */
2012       return offset_within_block_p (x, INTVAL (offset));
2013
2014     case SYMBOL_GOT_PAGE_OFST:
2015     case SYMBOL_GOTOFF_PAGE:
2016       /* If the symbol is global, the GOT entry will contain the symbol's
2017          address, and we will apply a 16-bit offset after loading it.
2018          If the symbol is local, the linker should provide enough local
2019          GOT entries for a 16-bit offset, but larger offsets may lead
2020          to GOT overflow.  */
2021       return SMALL_INT (offset);
2022
2023     case SYMBOL_TPREL:
2024     case SYMBOL_DTPREL:
2025       /* There is no carry between the HI and LO REL relocations, so the
2026          offset is only valid if we know it won't lead to such a carry.  */
2027       return mips_offset_within_alignment_p (x, INTVAL (offset));
2028
2029     case SYMBOL_GOT_DISP:
2030     case SYMBOL_GOTOFF_DISP:
2031     case SYMBOL_GOTOFF_CALL:
2032     case SYMBOL_GOTOFF_LOADGP:
2033     case SYMBOL_TLSGD:
2034     case SYMBOL_TLSLDM:
2035     case SYMBOL_GOTTPREL:
2036     case SYMBOL_TLS:
2037     case SYMBOL_HALF:
2038       return false;
2039     }
2040   gcc_unreachable ();
2041 }
2042 \f
2043 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
2044    single instruction.  We rely on the fact that, in the worst case,
2045    all instructions involved in a MIPS16 address calculation are usually
2046    extended ones.  */
2047
2048 static int
2049 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
2050 {
2051   if (mips_use_pcrel_pool_p[(int) type])
2052     {
2053       if (mode == MAX_MACHINE_MODE)
2054         /* LEAs will be converted into constant-pool references by
2055            mips_reorg.  */
2056         type = SYMBOL_PC_RELATIVE;
2057       else
2058         /* The constant must be loaded and then dereferenced.  */
2059         return 0;
2060     }
2061
2062   switch (type)
2063     {
2064     case SYMBOL_ABSOLUTE:
2065       /* When using 64-bit symbols, we need 5 preparatory instructions,
2066          such as:
2067
2068              lui     $at,%highest(symbol)
2069              daddiu  $at,$at,%higher(symbol)
2070              dsll    $at,$at,16
2071              daddiu  $at,$at,%hi(symbol)
2072              dsll    $at,$at,16
2073
2074          The final address is then $at + %lo(symbol).  With 32-bit
2075          symbols we just need a preparatory LUI for normal mode and
2076          a preparatory LI and SLL for MIPS16.  */
2077       return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
2078
2079     case SYMBOL_GP_RELATIVE:
2080       /* Treat GP-relative accesses as taking a single instruction on
2081          MIPS16 too; the copy of $gp can often be shared.  */
2082       return 1;
2083
2084     case SYMBOL_PC_RELATIVE:
2085       /* PC-relative constants can be only be used with ADDIUPC,
2086          DADDIUPC, LWPC and LDPC.  */
2087       if (mode == MAX_MACHINE_MODE
2088           || GET_MODE_SIZE (mode) == 4
2089           || GET_MODE_SIZE (mode) == 8)
2090         return 1;
2091
2092       /* The constant must be loaded using ADDIUPC or DADDIUPC first.  */
2093       return 0;
2094
2095     case SYMBOL_GOT_DISP:
2096       /* The constant will have to be loaded from the GOT before it
2097          is used in an address.  */
2098       if (mode != MAX_MACHINE_MODE)
2099         return 0;
2100
2101       /* Fall through.  */
2102
2103     case SYMBOL_GOT_PAGE_OFST:
2104       /* Unless -funit-at-a-time is in effect, we can't be sure whether the
2105          local/global classification is accurate.  The worst cases are:
2106
2107          (1) For local symbols when generating o32 or o64 code.  The assembler
2108              will use:
2109
2110                  lw           $at,%got(symbol)
2111                  nop
2112
2113              ...and the final address will be $at + %lo(symbol).
2114
2115          (2) For global symbols when -mxgot.  The assembler will use:
2116
2117                  lui     $at,%got_hi(symbol)
2118                  (d)addu $at,$at,$gp
2119
2120              ...and the final address will be $at + %got_lo(symbol).  */
2121       return 3;
2122
2123     case SYMBOL_GOTOFF_PAGE:
2124     case SYMBOL_GOTOFF_DISP:
2125     case SYMBOL_GOTOFF_CALL:
2126     case SYMBOL_GOTOFF_LOADGP:
2127     case SYMBOL_64_HIGH:
2128     case SYMBOL_64_MID:
2129     case SYMBOL_64_LOW:
2130     case SYMBOL_TLSGD:
2131     case SYMBOL_TLSLDM:
2132     case SYMBOL_DTPREL:
2133     case SYMBOL_GOTTPREL:
2134     case SYMBOL_TPREL:
2135     case SYMBOL_HALF:
2136       /* A 16-bit constant formed by a single relocation, or a 32-bit
2137          constant formed from a high 16-bit relocation and a low 16-bit
2138          relocation.  Use mips_split_p to determine which.  32-bit
2139          constants need an "lui; addiu" sequence for normal mode and
2140          an "li; sll; addiu" sequence for MIPS16 mode.  */
2141       return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
2142
2143     case SYMBOL_TLS:
2144       /* We don't treat a bare TLS symbol as a constant.  */
2145       return 0;
2146     }
2147   gcc_unreachable ();
2148 }
2149
2150 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
2151    to load symbols of type TYPE into a register.  Return 0 if the given
2152    type of symbol cannot be used as an immediate operand.
2153
2154    Otherwise, return the number of instructions needed to load or store
2155    values of mode MODE to or from addresses of type TYPE.  Return 0 if
2156    the given type of symbol is not valid in addresses.
2157
2158    In both cases, instruction counts are based off BASE_INSN_LENGTH.  */
2159
2160 static int
2161 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
2162 {
2163   return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
2164 }
2165 \f
2166 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
2167
2168 static bool
2169 mips_cannot_force_const_mem (enum machine_mode mode, rtx x)
2170 {
2171   enum mips_symbol_type type;
2172   rtx base, offset;
2173
2174   /* There is no assembler syntax for expressing an address-sized
2175      high part.  */
2176   if (GET_CODE (x) == HIGH)
2177     return true;
2178
2179   /* As an optimization, reject constants that mips_legitimize_move
2180      can expand inline.
2181
2182      Suppose we have a multi-instruction sequence that loads constant C
2183      into register R.  If R does not get allocated a hard register, and
2184      R is used in an operand that allows both registers and memory
2185      references, reload will consider forcing C into memory and using
2186      one of the instruction's memory alternatives.  Returning false
2187      here will force it to use an input reload instead.  */
2188   if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
2189     return true;
2190
2191   split_const (x, &base, &offset);
2192   if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
2193     {
2194       /* See whether we explicitly want these symbols in the pool.  */
2195       if (mips_use_pcrel_pool_p[(int) type])
2196         return false;
2197
2198       /* The same optimization as for CONST_INT.  */
2199       if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2200         return true;
2201
2202       /* If MIPS16 constant pools live in the text section, they should
2203          not refer to anything that might need run-time relocation.  */
2204       if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2205         return true;
2206     }
2207
2208   /* TLS symbols must be computed by mips_legitimize_move.  */
2209   if (tls_referenced_p (x))
2210     return true;
2211
2212   return false;
2213 }
2214
2215 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  We can't use blocks for
2216    constants when we're using a per-function constant pool.  */
2217
2218 static bool
2219 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2220                                 const_rtx x ATTRIBUTE_UNUSED)
2221 {
2222   return !TARGET_MIPS16_PCREL_LOADS;
2223 }
2224 \f
2225 /* Return true if register REGNO is a valid base register for mode MODE.
2226    STRICT_P is true if REG_OK_STRICT is in effect.  */
2227
2228 int
2229 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
2230                                bool strict_p)
2231 {
2232   if (!HARD_REGISTER_NUM_P (regno))
2233     {
2234       if (!strict_p)
2235         return true;
2236       regno = reg_renumber[regno];
2237     }
2238
2239   /* These fake registers will be eliminated to either the stack or
2240      hard frame pointer, both of which are usually valid base registers.
2241      Reload deals with the cases where the eliminated form isn't valid.  */
2242   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2243     return true;
2244
2245   /* In MIPS16 mode, the stack pointer can only address word and doubleword
2246      values, nothing smaller.  */
2247   if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2248     return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2249
2250   return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2251 }
2252
2253 /* Return true if X is a valid base register for mode MODE.
2254    STRICT_P is true if REG_OK_STRICT is in effect.  */
2255
2256 static bool
2257 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
2258 {
2259   if (!strict_p && GET_CODE (x) == SUBREG)
2260     x = SUBREG_REG (x);
2261
2262   return (REG_P (x)
2263           && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2264 }
2265
2266 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2267    can address a value of mode MODE.  */
2268
2269 static bool
2270 mips_valid_offset_p (rtx x, enum machine_mode mode)
2271 {
2272   /* Check that X is a signed 16-bit number.  */
2273   if (!const_arith_operand (x, Pmode))
2274     return false;
2275
2276   /* We may need to split multiword moves, so make sure that every word
2277      is accessible.  */
2278   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2279       && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2280     return false;
2281
2282   return true;
2283 }
2284
2285 /* Return true if a LO_SUM can address a value of mode MODE when the
2286    LO_SUM symbol has type SYMBOL_TYPE.  */
2287
2288 static bool
2289 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, enum machine_mode mode)
2290 {
2291   /* Check that symbols of type SYMBOL_TYPE can be used to access values
2292      of mode MODE.  */
2293   if (mips_symbol_insns (symbol_type, mode) == 0)
2294     return false;
2295
2296   /* Check that there is a known low-part relocation.  */
2297   if (mips_lo_relocs[symbol_type] == NULL)
2298     return false;
2299
2300   /* We may need to split multiword moves, so make sure that each word
2301      can be accessed without inducing a carry.  This is mainly needed
2302      for o64, which has historically only guaranteed 64-bit alignment
2303      for 128-bit types.  */
2304   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2305       && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2306     return false;
2307
2308   return true;
2309 }
2310
2311 /* Return true if X is a valid address for machine mode MODE.  If it is,
2312    fill in INFO appropriately.  STRICT_P is true if REG_OK_STRICT is in
2313    effect.  */
2314
2315 static bool
2316 mips_classify_address (struct mips_address_info *info, rtx x,
2317                        enum machine_mode mode, bool strict_p)
2318 {
2319   switch (GET_CODE (x))
2320     {
2321     case REG:
2322     case SUBREG:
2323       info->type = ADDRESS_REG;
2324       info->reg = x;
2325       info->offset = const0_rtx;
2326       return mips_valid_base_register_p (info->reg, mode, strict_p);
2327
2328     case PLUS:
2329       info->type = ADDRESS_REG;
2330       info->reg = XEXP (x, 0);
2331       info->offset = XEXP (x, 1);
2332       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2333               && mips_valid_offset_p (info->offset, mode));
2334
2335     case LO_SUM:
2336       info->type = ADDRESS_LO_SUM;
2337       info->reg = XEXP (x, 0);
2338       info->offset = XEXP (x, 1);
2339       /* We have to trust the creator of the LO_SUM to do something vaguely
2340          sane.  Target-independent code that creates a LO_SUM should also
2341          create and verify the matching HIGH.  Target-independent code that
2342          adds an offset to a LO_SUM must prove that the offset will not
2343          induce a carry.  Failure to do either of these things would be
2344          a bug, and we are not required to check for it here.  The MIPS
2345          backend itself should only create LO_SUMs for valid symbolic
2346          constants, with the high part being either a HIGH or a copy
2347          of _gp. */
2348       info->symbol_type
2349         = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2350       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2351               && mips_valid_lo_sum_p (info->symbol_type, mode));
2352
2353     case CONST_INT:
2354       /* Small-integer addresses don't occur very often, but they
2355          are legitimate if $0 is a valid base register.  */
2356       info->type = ADDRESS_CONST_INT;
2357       return !TARGET_MIPS16 && SMALL_INT (x);
2358
2359     case CONST:
2360     case LABEL_REF:
2361     case SYMBOL_REF:
2362       info->type = ADDRESS_SYMBOLIC;
2363       return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2364                                         &info->symbol_type)
2365               && mips_symbol_insns (info->symbol_type, mode) > 0
2366               && !mips_split_p[info->symbol_type]);
2367
2368     default:
2369       return false;
2370     }
2371 }
2372
2373 /* Implement TARGET_LEGITIMATE_ADDRESS_P.  */
2374
2375 static bool
2376 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2377 {
2378   struct mips_address_info addr;
2379
2380   return mips_classify_address (&addr, x, mode, strict_p);
2381 }
2382
2383 /* Return true if X is a legitimate $sp-based address for mode MDOE.  */
2384
2385 bool
2386 mips_stack_address_p (rtx x, enum machine_mode mode)
2387 {
2388   struct mips_address_info addr;
2389
2390   return (mips_classify_address (&addr, x, mode, false)
2391           && addr.type == ADDRESS_REG
2392           && addr.reg == stack_pointer_rtx);
2393 }
2394
2395 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2396    address instruction.  Note that such addresses are not considered
2397    legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2398    is so restricted.  */
2399
2400 static bool
2401 mips_lwxs_address_p (rtx addr)
2402 {
2403   if (ISA_HAS_LWXS
2404       && GET_CODE (addr) == PLUS
2405       && REG_P (XEXP (addr, 1)))
2406     {
2407       rtx offset = XEXP (addr, 0);
2408       if (GET_CODE (offset) == MULT
2409           && REG_P (XEXP (offset, 0))
2410           && CONST_INT_P (XEXP (offset, 1))
2411           && INTVAL (XEXP (offset, 1)) == 4)
2412         return true;
2413     }
2414   return false;
2415 }
2416
2417 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load 
2418    indexed address instruction.  Note that such addresses are
2419    not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2420    sense, because their use is so restricted.  */
2421
2422 static bool
2423 mips_lx_address_p (rtx addr, enum machine_mode mode)
2424 {
2425   if (GET_CODE (addr) != PLUS
2426       || !REG_P (XEXP (addr, 0))
2427       || !REG_P (XEXP (addr, 1)))
2428     return false;
2429   if (ISA_HAS_LBX && mode == QImode)
2430     return true;
2431   if (ISA_HAS_LHX && mode == HImode)
2432     return true;
2433   if (ISA_HAS_LWX && mode == SImode)
2434     return true;
2435   if (ISA_HAS_LDX && mode == DImode)
2436     return true;
2437   return false;
2438 }
2439 \f
2440 /* Return true if a value at OFFSET bytes from base register BASE can be
2441    accessed using an unextended MIPS16 instruction.  MODE is the mode of
2442    the value.
2443
2444    Usually the offset in an unextended instruction is a 5-bit field.
2445    The offset is unsigned and shifted left once for LH and SH, twice
2446    for LW and SW, and so on.  An exception is LWSP and SWSP, which have
2447    an 8-bit immediate field that's shifted left twice.  */
2448
2449 static bool
2450 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
2451                                unsigned HOST_WIDE_INT offset)
2452 {
2453   if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2454     {
2455       if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2456         return offset < 256U * GET_MODE_SIZE (mode);
2457       return offset < 32U * GET_MODE_SIZE (mode);
2458     }
2459   return false;
2460 }
2461
2462 /* Return the number of instructions needed to load or store a value
2463    of mode MODE at address X, assuming that BASE_INSN_LENGTH is the
2464    length of one instruction.  Return 0 if X isn't valid for MODE.
2465    Assume that multiword moves may need to be split into word moves
2466    if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2467    enough.  */
2468
2469 int
2470 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
2471 {
2472   struct mips_address_info addr;
2473   int factor;
2474
2475   /* BLKmode is used for single unaligned loads and stores and should
2476      not count as a multiword mode.  (GET_MODE_SIZE (BLKmode) is pretty
2477      meaningless, so we have to single it out as a special case one way
2478      or the other.)  */
2479   if (mode != BLKmode && might_split_p)
2480     factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2481   else
2482     factor = 1;
2483
2484   if (mips_classify_address (&addr, x, mode, false))
2485     switch (addr.type)
2486       {
2487       case ADDRESS_REG:
2488         if (TARGET_MIPS16
2489             && !mips16_unextended_reference_p (mode, addr.reg,
2490                                                UINTVAL (addr.offset)))
2491           return factor * 2;
2492         return factor;
2493
2494       case ADDRESS_LO_SUM:
2495         return TARGET_MIPS16 ? factor * 2 : factor;
2496
2497       case ADDRESS_CONST_INT:
2498         return factor;
2499
2500       case ADDRESS_SYMBOLIC:
2501         return factor * mips_symbol_insns (addr.symbol_type, mode);
2502       }
2503   return 0;
2504 }
2505
2506 /* Return true if X fits within an unsigned field of BITS bits that is
2507    shifted left SHIFT bits before being used.  */
2508
2509 bool
2510 mips_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2511 {
2512   return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2513 }
2514
2515 /* Return true if X fits within a signed field of BITS bits that is
2516    shifted left SHIFT bits before being used.  */
2517
2518 bool
2519 mips_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2520 {
2521   x += 1 << (bits + shift - 1);
2522   return mips_unsigned_immediate_p (x, bits, shift);
2523 }
2524
2525 /* Return true if X is legitimate for accessing values of mode MODE,
2526    if it is based on a MIPS16 register, and if the offset satisfies
2527    OFFSET_PREDICATE.  */
2528
2529 bool
2530 m16_based_address_p (rtx x, enum machine_mode mode,
2531                      insn_operand_predicate_fn offset_predicate)
2532 {
2533   struct mips_address_info addr;
2534
2535   return (mips_classify_address (&addr, x, mode, false)
2536           && addr.type == ADDRESS_REG
2537           && M16_REG_P (REGNO (addr.reg))
2538           && offset_predicate (addr.offset, mode));
2539 }
2540
2541 /* Return true if X is a legitimate address that conforms to the requirements
2542    for a microMIPS LWSP or SWSP insn.  */
2543
2544 bool
2545 lwsp_swsp_address_p (rtx x, enum machine_mode mode)
2546 {
2547   struct mips_address_info addr;
2548
2549   return (mips_classify_address (&addr, x, mode, false)
2550           && addr.type == ADDRESS_REG
2551           && REGNO (addr.reg) == STACK_POINTER_REGNUM
2552           && uw5_operand (addr.offset, mode));
2553 }
2554
2555 /* Return true if X is a legitimate address with a 12-bit offset.
2556    MODE is the mode of the value being accessed.  */
2557
2558 bool
2559 umips_12bit_offset_address_p (rtx x, enum machine_mode mode)
2560 {
2561   struct mips_address_info addr;
2562
2563   return (mips_classify_address (&addr, x, mode, false)
2564           && addr.type == ADDRESS_REG
2565           && CONST_INT_P (addr.offset)
2566           && UMIPS_12BIT_OFFSET_P (INTVAL (addr.offset)));
2567 }
2568
2569 /* Return the number of instructions needed to load constant X,
2570    assuming that BASE_INSN_LENGTH is the length of one instruction.
2571    Return 0 if X isn't a valid constant.  */
2572
2573 int
2574 mips_const_insns (rtx x)
2575 {
2576   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2577   enum mips_symbol_type symbol_type;
2578   rtx offset;
2579
2580   switch (GET_CODE (x))
2581     {
2582     case HIGH:
2583       if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2584                                      &symbol_type)
2585           || !mips_split_p[symbol_type])
2586         return 0;
2587
2588       /* This is simply an LUI for normal mode.  It is an extended
2589          LI followed by an extended SLL for MIPS16.  */
2590       return TARGET_MIPS16 ? 4 : 1;
2591
2592     case CONST_INT:
2593       if (TARGET_MIPS16)
2594         /* Unsigned 8-bit constants can be loaded using an unextended
2595            LI instruction.  Unsigned 16-bit constants can be loaded
2596            using an extended LI.  Negative constants must be loaded
2597            using LI and then negated.  */
2598         return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2599                 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2600                 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2601                 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2602                 : 0);
2603
2604       return mips_build_integer (codes, INTVAL (x));
2605
2606     case CONST_DOUBLE:
2607     case CONST_VECTOR:
2608       /* Allow zeros for normal mode, where we can use $0.  */
2609       return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2610
2611     case CONST:
2612       if (CONST_GP_P (x))
2613         return 1;
2614
2615       /* See if we can refer to X directly.  */
2616       if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2617         return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2618
2619       /* Otherwise try splitting the constant into a base and offset.
2620          If the offset is a 16-bit value, we can load the base address
2621          into a register and then use (D)ADDIU to add in the offset.
2622          If the offset is larger, we can load the base and offset
2623          into separate registers and add them together with (D)ADDU.
2624          However, the latter is only possible before reload; during
2625          and after reload, we must have the option of forcing the
2626          constant into the pool instead.  */
2627       split_const (x, &x, &offset);
2628       if (offset != 0)
2629         {
2630           int n = mips_const_insns (x);
2631           if (n != 0)
2632             {
2633               if (SMALL_INT (offset))
2634                 return n + 1;
2635               else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2636                 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2637             }
2638         }
2639       return 0;
2640
2641     case SYMBOL_REF:
2642     case LABEL_REF:
2643       return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2644                                 MAX_MACHINE_MODE);
2645
2646     default:
2647       return 0;
2648     }
2649 }
2650
2651 /* X is a doubleword constant that can be handled by splitting it into
2652    two words and loading each word separately.  Return the number of
2653    instructions required to do this, assuming that BASE_INSN_LENGTH
2654    is the length of one instruction.  */
2655
2656 int
2657 mips_split_const_insns (rtx x)
2658 {
2659   unsigned int low, high;
2660
2661   low = mips_const_insns (mips_subword (x, false));
2662   high = mips_const_insns (mips_subword (x, true));
2663   gcc_assert (low > 0 && high > 0);
2664   return low + high;
2665 }
2666
2667 /* Return the number of instructions needed to implement INSN,
2668    given that it loads from or stores to MEM.  Assume that
2669    BASE_INSN_LENGTH is the length of one instruction.  */
2670
2671 int
2672 mips_load_store_insns (rtx mem, rtx_insn *insn)
2673 {
2674   enum machine_mode mode;
2675   bool might_split_p;
2676   rtx set;
2677
2678   gcc_assert (MEM_P (mem));
2679   mode = GET_MODE (mem);
2680
2681   /* Try to prove that INSN does not need to be split.  */
2682   might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2683   if (might_split_p)
2684     {
2685       set = single_set (insn);
2686       if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
2687         might_split_p = false;
2688     }
2689
2690   return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2691 }
2692
2693 /* Return the number of instructions needed for an integer division,
2694    assuming that BASE_INSN_LENGTH is the length of one instruction.  */
2695
2696 int
2697 mips_idiv_insns (void)
2698 {
2699   int count;
2700
2701   count = 1;
2702   if (TARGET_CHECK_ZERO_DIV)
2703     {
2704       if (GENERATE_DIVIDE_TRAPS)
2705         count++;
2706       else
2707         count += 2;
2708     }
2709
2710   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2711     count++;
2712   return count;
2713 }
2714 \f
2715 /* Emit a move from SRC to DEST.  Assume that the move expanders can
2716    handle all moves if !can_create_pseudo_p ().  The distinction is
2717    important because, unlike emit_move_insn, the move expanders know
2718    how to force Pmode objects into the constant pool even when the
2719    constant pool address is not itself legitimate.  */
2720
2721 rtx_insn *
2722 mips_emit_move (rtx dest, rtx src)
2723 {
2724   return (can_create_pseudo_p ()
2725           ? emit_move_insn (dest, src)
2726           : emit_move_insn_1 (dest, src));
2727 }
2728
2729 /* Emit a move from SRC to DEST, splitting compound moves into individual
2730    instructions.  SPLIT_TYPE is the type of split to perform.  */
2731
2732 static void
2733 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
2734 {
2735   if (mips_split_move_p (dest, src, split_type))
2736     mips_split_move (dest, src, split_type);
2737   else
2738     mips_emit_move (dest, src);
2739 }
2740
2741 /* Emit an instruction of the form (set TARGET (CODE OP0)).  */
2742
2743 static void
2744 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2745 {
2746   emit_insn (gen_rtx_SET (VOIDmode, target,
2747                           gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2748 }
2749
2750 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2751    Return that new register.  */
2752
2753 static rtx
2754 mips_force_unary (enum machine_mode mode, enum rtx_code code, rtx op0)
2755 {
2756   rtx reg;
2757
2758   reg = gen_reg_rtx (mode);
2759   mips_emit_unary (code, reg, op0);
2760   return reg;
2761 }
2762
2763 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)).  */
2764
2765 void
2766 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2767 {
2768   emit_insn (gen_rtx_SET (VOIDmode, target,
2769                           gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2770 }
2771
2772 /* Compute (CODE OP0 OP1) and store the result in a new register
2773    of mode MODE.  Return that new register.  */
2774
2775 static rtx
2776 mips_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2777 {
2778   rtx reg;
2779
2780   reg = gen_reg_rtx (mode);
2781   mips_emit_binary (code, reg, op0, op1);
2782   return reg;
2783 }
2784
2785 /* Copy VALUE to a register and return that register.  If new pseudos
2786    are allowed, copy it into a new register, otherwise use DEST.  */
2787
2788 static rtx
2789 mips_force_temporary (rtx dest, rtx value)
2790 {
2791   if (can_create_pseudo_p ())
2792     return force_reg (Pmode, value);
2793   else
2794     {
2795       mips_emit_move (dest, value);
2796       return dest;
2797     }
2798 }
2799
2800 /* Emit a call sequence with call pattern PATTERN and return the call
2801    instruction itself (which is not necessarily the last instruction
2802    emitted).  ORIG_ADDR is the original, unlegitimized address,
2803    ADDR is the legitimized form, and LAZY_P is true if the call
2804    address is lazily-bound.  */
2805
2806 static rtx_insn *
2807 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2808 {
2809   rtx_insn *insn;
2810   rtx reg;
2811
2812   insn = emit_call_insn (pattern);
2813
2814   if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2815     {
2816       /* MIPS16 JALRs only take MIPS16 registers.  If the target
2817          function requires $25 to be valid on entry, we must copy it
2818          there separately.  The move instruction can be put in the
2819          call's delay slot.  */
2820       reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2821       emit_insn_before (gen_move_insn (reg, addr), insn);
2822       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2823     }
2824
2825   if (lazy_p)
2826     /* Lazy-binding stubs require $gp to be valid on entry.  */
2827     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2828
2829   if (TARGET_USE_GOT)
2830     {
2831       /* See the comment above load_call<mode> for details.  */
2832       use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2833                gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2834       emit_insn (gen_update_got_version ());
2835     }
2836
2837   if (TARGET_MIPS16
2838       && TARGET_EXPLICIT_RELOCS
2839       && TARGET_CALL_CLOBBERED_GP)
2840     {
2841       rtx post_call_tmp_reg = gen_rtx_REG (word_mode, POST_CALL_TMP_REG);
2842       clobber_reg (&CALL_INSN_FUNCTION_USAGE (insn), post_call_tmp_reg);
2843     }
2844
2845   return insn;
2846 }
2847 \f
2848 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2849    then add CONST_INT OFFSET to the result.  */
2850
2851 static rtx
2852 mips_unspec_address_offset (rtx base, rtx offset,
2853                             enum mips_symbol_type symbol_type)
2854 {
2855   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2856                          UNSPEC_ADDRESS_FIRST + symbol_type);
2857   if (offset != const0_rtx)
2858     base = gen_rtx_PLUS (Pmode, base, offset);
2859   return gen_rtx_CONST (Pmode, base);
2860 }
2861
2862 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2863    type SYMBOL_TYPE.  */
2864
2865 rtx
2866 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2867 {
2868   rtx base, offset;
2869
2870   split_const (address, &base, &offset);
2871   return mips_unspec_address_offset (base, offset, symbol_type);
2872 }
2873
2874 /* If OP is an UNSPEC address, return the address to which it refers,
2875    otherwise return OP itself.  */
2876
2877 rtx
2878 mips_strip_unspec_address (rtx op)
2879 {
2880   rtx base, offset;
2881
2882   split_const (op, &base, &offset);
2883   if (UNSPEC_ADDRESS_P (base))
2884     op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2885   return op;
2886 }
2887
2888 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2889    high part to BASE and return the result.  Just return BASE otherwise.
2890    TEMP is as for mips_force_temporary.
2891
2892    The returned expression can be used as the first operand to a LO_SUM.  */
2893
2894 static rtx
2895 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2896                          enum mips_symbol_type symbol_type)
2897 {
2898   if (mips_split_p[symbol_type])
2899     {
2900       addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2901       addr = mips_force_temporary (temp, addr);
2902       base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2903     }
2904   return base;
2905 }
2906 \f
2907 /* Return an instruction that copies $gp into register REG.  We want
2908    GCC to treat the register's value as constant, so that its value
2909    can be rematerialized on demand.  */
2910
2911 static rtx
2912 gen_load_const_gp (rtx reg)
2913 {
2914   return PMODE_INSN (gen_load_const_gp, (reg));
2915 }
2916
2917 /* Return a pseudo register that contains the value of $gp throughout
2918    the current function.  Such registers are needed by MIPS16 functions,
2919    for which $gp itself is not a valid base register or addition operand.  */
2920
2921 static rtx
2922 mips16_gp_pseudo_reg (void)
2923 {
2924   if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2925     {
2926       rtx_insn *scan;
2927
2928       cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2929
2930       push_topmost_sequence ();
2931
2932       scan = get_insns ();
2933       while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2934         scan = NEXT_INSN (scan);
2935
2936       rtx set = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2937       rtx_insn *insn = emit_insn_after (set, scan);
2938       INSN_LOCATION (insn) = 0;
2939
2940       pop_topmost_sequence ();
2941     }
2942
2943   return cfun->machine->mips16_gp_pseudo_rtx;
2944 }
2945
2946 /* Return a base register that holds pic_offset_table_rtx.
2947    TEMP, if nonnull, is a scratch Pmode base register.  */
2948
2949 rtx
2950 mips_pic_base_register (rtx temp)
2951 {
2952   if (!TARGET_MIPS16)
2953     return pic_offset_table_rtx;
2954
2955   if (currently_expanding_to_rtl)
2956     return mips16_gp_pseudo_reg ();
2957
2958   if (can_create_pseudo_p ())
2959     temp = gen_reg_rtx (Pmode);
2960
2961   if (TARGET_USE_GOT)
2962     /* The first post-reload split exposes all references to $gp
2963        (both uses and definitions).  All references must remain
2964        explicit after that point.
2965
2966        It is safe to introduce uses of $gp at any time, so for
2967        simplicity, we do that before the split too.  */
2968     mips_emit_move (temp, pic_offset_table_rtx);
2969   else
2970     emit_insn (gen_load_const_gp (temp));
2971   return temp;
2972 }
2973
2974 /* Return the RHS of a load_call<mode> insn.  */
2975
2976 static rtx
2977 mips_unspec_call (rtx reg, rtx symbol)
2978 {
2979   rtvec vec;
2980
2981   vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2982   return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2983 }
2984
2985 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2986    reference.  Return NULL_RTX otherwise.  */
2987
2988 static rtx
2989 mips_strip_unspec_call (rtx src)
2990 {
2991   if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
2992     return mips_strip_unspec_address (XVECEXP (src, 0, 1));
2993   return NULL_RTX;
2994 }
2995
2996 /* Create and return a GOT reference of type TYPE for address ADDR.
2997    TEMP, if nonnull, is a scratch Pmode base register.  */
2998
2999 rtx
3000 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
3001 {
3002   rtx base, high, lo_sum_symbol;
3003
3004   base = mips_pic_base_register (temp);
3005
3006   /* If we used the temporary register to load $gp, we can't use
3007      it for the high part as well.  */
3008   if (temp != NULL && reg_overlap_mentioned_p (base, temp))
3009     temp = NULL;
3010
3011   high = mips_unspec_offset_high (temp, base, addr, type);
3012   lo_sum_symbol = mips_unspec_address (addr, type);
3013
3014   if (type == SYMBOL_GOTOFF_CALL)
3015     return mips_unspec_call (high, lo_sum_symbol);
3016   else
3017     return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
3018 }
3019
3020 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
3021    it appears in a MEM of that mode.  Return true if ADDR is a legitimate
3022    constant in that context and can be split into high and low parts.
3023    If so, and if LOW_OUT is nonnull, emit the high part and store the
3024    low part in *LOW_OUT.  Leave *LOW_OUT unchanged otherwise.
3025
3026    TEMP is as for mips_force_temporary and is used to load the high
3027    part into a register.
3028
3029    When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
3030    a legitimize SET_SRC for an .md pattern, otherwise the low part
3031    is guaranteed to be a legitimate address for mode MODE.  */
3032
3033 bool
3034 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
3035 {
3036   enum mips_symbol_context context;
3037   enum mips_symbol_type symbol_type;
3038   rtx high;
3039
3040   context = (mode == MAX_MACHINE_MODE
3041              ? SYMBOL_CONTEXT_LEA
3042              : SYMBOL_CONTEXT_MEM);
3043   if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
3044     {
3045       addr = XEXP (addr, 0);
3046       if (mips_symbolic_constant_p (addr, context, &symbol_type)
3047           && mips_symbol_insns (symbol_type, mode) > 0
3048           && mips_split_hi_p[symbol_type])
3049         {
3050           if (low_out)
3051             switch (symbol_type)
3052               {
3053               case SYMBOL_GOT_PAGE_OFST:
3054                 /* The high part of a page/ofst pair is loaded from the GOT.  */
3055                 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
3056                 break;
3057
3058               default:
3059                 gcc_unreachable ();
3060               }
3061           return true;
3062         }
3063     }
3064   else
3065     {
3066       if (mips_symbolic_constant_p (addr, context, &symbol_type)
3067           && mips_symbol_insns (symbol_type, mode) > 0
3068           && mips_split_p[symbol_type])
3069         {
3070           if (low_out)
3071             switch (symbol_type)
3072               {
3073               case SYMBOL_GOT_DISP:
3074                 /* SYMBOL_GOT_DISP symbols are loaded from the GOT.  */
3075                 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
3076                 break;
3077
3078               case SYMBOL_GP_RELATIVE:
3079                 high = mips_pic_base_register (temp);
3080                 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3081                 break;
3082
3083               default:
3084                 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
3085                 high = mips_force_temporary (temp, high);
3086                 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3087                 break;
3088               }
3089           return true;
3090         }
3091     }
3092   return false;
3093 }
3094
3095 /* Return a legitimate address for REG + OFFSET.  TEMP is as for
3096    mips_force_temporary; it is only needed when OFFSET is not a
3097    SMALL_OPERAND.  */
3098
3099 static rtx
3100 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
3101 {
3102   if (!SMALL_OPERAND (offset))
3103     {
3104       rtx high;
3105
3106       if (TARGET_MIPS16)
3107         {
3108           /* Load the full offset into a register so that we can use
3109              an unextended instruction for the address itself.  */
3110           high = GEN_INT (offset);
3111           offset = 0;
3112         }
3113       else
3114         {
3115           /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
3116              The addition inside the macro CONST_HIGH_PART may cause an
3117              overflow, so we need to force a sign-extension check.  */
3118           high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
3119           offset = CONST_LOW_PART (offset);
3120         }
3121       high = mips_force_temporary (temp, high);
3122       reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
3123     }
3124   return plus_constant (Pmode, reg, offset);
3125 }
3126 \f
3127 /* The __tls_get_attr symbol.  */
3128 static GTY(()) rtx mips_tls_symbol;
3129
3130 /* Return an instruction sequence that calls __tls_get_addr.  SYM is
3131    the TLS symbol we are referencing and TYPE is the symbol type to use
3132    (either global dynamic or local dynamic).  V0 is an RTX for the
3133    return value location.  */
3134
3135 static rtx
3136 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
3137 {
3138   rtx insn, loc, a0;
3139
3140   a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3141
3142   if (!mips_tls_symbol)
3143     mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
3144
3145   loc = mips_unspec_address (sym, type);
3146
3147   start_sequence ();
3148
3149   emit_insn (gen_rtx_SET (Pmode, a0,
3150                           gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
3151   insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
3152                            const0_rtx, NULL_RTX, false);
3153   RTL_CONST_CALL_P (insn) = 1;
3154   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
3155   insn = get_insns ();
3156
3157   end_sequence ();
3158
3159   return insn;
3160 }
3161
3162 /* Return a pseudo register that contains the current thread pointer.  */
3163
3164 rtx
3165 mips_expand_thread_pointer (rtx tp)
3166 {
3167   rtx fn;
3168
3169   if (TARGET_MIPS16)
3170     {
3171       if (!mips16_rdhwr_stub)
3172         mips16_rdhwr_stub = new mips16_rdhwr_one_only_stub ();
3173       fn = mips16_stub_call_address (mips16_rdhwr_stub);
3174       emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
3175     }
3176   else
3177     emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
3178   return tp;
3179 }
3180
3181 static rtx
3182 mips_get_tp (void)
3183 {
3184   return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
3185 }
3186
3187 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
3188    its address.  The return value will be both a valid address and a valid
3189    SET_SRC (either a REG or a LO_SUM).  */
3190
3191 static rtx
3192 mips_legitimize_tls_address (rtx loc)
3193 {
3194   rtx dest, insn, v0, tp, tmp1, tmp2, eqv, offset;
3195   enum tls_model model;
3196
3197   model = SYMBOL_REF_TLS_MODEL (loc);
3198   /* Only TARGET_ABICALLS code can have more than one module; other
3199      code must be be static and should not use a GOT.  All TLS models
3200      reduce to local exec in this situation.  */
3201   if (!TARGET_ABICALLS)
3202     model = TLS_MODEL_LOCAL_EXEC;
3203
3204   switch (model)
3205     {
3206     case TLS_MODEL_GLOBAL_DYNAMIC:
3207       v0 = gen_rtx_REG (Pmode, GP_RETURN);
3208       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
3209       dest = gen_reg_rtx (Pmode);
3210       emit_libcall_block (insn, dest, v0, loc);
3211       break;
3212
3213     case TLS_MODEL_LOCAL_DYNAMIC:
3214       v0 = gen_rtx_REG (Pmode, GP_RETURN);
3215       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
3216       tmp1 = gen_reg_rtx (Pmode);
3217
3218       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3219          share the LDM result with other LD model accesses.  */
3220       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3221                             UNSPEC_TLS_LDM);
3222       emit_libcall_block (insn, tmp1, v0, eqv);
3223
3224       offset = mips_unspec_address (loc, SYMBOL_DTPREL);
3225       if (mips_split_p[SYMBOL_DTPREL])
3226         {
3227           tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
3228           dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3229         }
3230       else
3231         dest = expand_binop (Pmode, add_optab, tmp1, offset,
3232                              0, 0, OPTAB_DIRECT);
3233       break;
3234
3235     case TLS_MODEL_INITIAL_EXEC:
3236       tp = mips_get_tp ();
3237       tmp1 = gen_reg_rtx (Pmode);
3238       tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
3239       if (Pmode == DImode)
3240         emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
3241       else
3242         emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
3243       dest = gen_reg_rtx (Pmode);
3244       emit_insn (gen_add3_insn (dest, tmp1, tp));
3245       break;
3246
3247     case TLS_MODEL_LOCAL_EXEC:
3248       tmp1 = mips_get_tp ();
3249       offset = mips_unspec_address (loc, SYMBOL_TPREL);
3250       if (mips_split_p[SYMBOL_TPREL])
3251         {
3252           tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
3253           dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3254         }
3255       else
3256         dest = expand_binop (Pmode, add_optab, tmp1, offset,
3257                              0, 0, OPTAB_DIRECT);
3258       break;
3259
3260     default:
3261       gcc_unreachable ();
3262     }
3263   return dest;
3264 }
3265 \f
3266 /* Implement "TARGET = __builtin_mips_get_fcsr ()" for MIPS16,
3267    using a stub.  */
3268
3269 void
3270 mips16_expand_get_fcsr (rtx target)
3271 {
3272   if (!mips16_get_fcsr_stub)
3273     mips16_get_fcsr_stub = new mips16_get_fcsr_one_only_stub ();
3274   rtx fn = mips16_stub_call_address (mips16_get_fcsr_stub);
3275   emit_insn (PMODE_INSN (gen_mips_get_fcsr_mips16, (fn)));
3276   emit_move_insn (target, gen_rtx_REG (SImode, GET_FCSR_REGNUM));
3277 }
3278
3279 /* Implement __builtin_mips_set_fcsr (TARGET) for MIPS16, using a stub.  */
3280
3281 void
3282 mips16_expand_set_fcsr (rtx newval)
3283 {
3284   if (!mips16_set_fcsr_stub)
3285     mips16_set_fcsr_stub = new mips16_set_fcsr_one_only_stub ();
3286   rtx fn = mips16_stub_call_address (mips16_set_fcsr_stub);
3287   emit_move_insn (gen_rtx_REG (SImode, SET_FCSR_REGNUM), newval);
3288   emit_insn (PMODE_INSN (gen_mips_set_fcsr_mips16, (fn)));
3289 }
3290 \f
3291 /* If X is not a valid address for mode MODE, force it into a register.  */
3292
3293 static rtx
3294 mips_force_address (rtx x, enum machine_mode mode)
3295 {
3296   if (!mips_legitimate_address_p (mode, x, false))
3297     x = force_reg (Pmode, x);
3298   return x;
3299 }
3300
3301 /* This function is used to implement LEGITIMIZE_ADDRESS.  If X can
3302    be legitimized in a way that the generic machinery might not expect,
3303    return a new address, otherwise return NULL.  MODE is the mode of
3304    the memory being accessed.  */
3305
3306 static rtx
3307 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3308                          enum machine_mode mode)
3309 {
3310   rtx base, addr;
3311   HOST_WIDE_INT offset;
3312
3313   if (mips_tls_symbol_p (x))
3314     return mips_legitimize_tls_address (x);
3315
3316   /* See if the address can split into a high part and a LO_SUM.  */
3317   if (mips_split_symbol (NULL, x, mode, &addr))
3318     return mips_force_address (addr, mode);
3319
3320   /* Handle BASE + OFFSET using mips_add_offset.  */
3321   mips_split_plus (x, &base, &offset);
3322   if (offset != 0)
3323     {
3324       if (!mips_valid_base_register_p (base, mode, false))
3325         base = copy_to_mode_reg (Pmode, base);
3326       addr = mips_add_offset (NULL, base, offset);
3327       return mips_force_address (addr, mode);
3328     }
3329
3330   return x;
3331 }
3332
3333 /* Load VALUE into DEST.  TEMP is as for mips_force_temporary.  */
3334
3335 void
3336 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3337 {
3338   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3339   enum machine_mode mode;
3340   unsigned int i, num_ops;
3341   rtx x;
3342
3343   mode = GET_MODE (dest);
3344   num_ops = mips_build_integer (codes, value);
3345
3346   /* Apply each binary operation to X.  Invariant: X is a legitimate
3347      source operand for a SET pattern.  */
3348   x = GEN_INT (codes[0].value);
3349   for (i = 1; i < num_ops; i++)
3350     {
3351       if (!can_create_pseudo_p ())
3352         {
3353           emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3354           x = temp;
3355         }
3356       else
3357         x = force_reg (mode, x);
3358       x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3359     }
3360
3361   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3362 }
3363
3364 /* Subroutine of mips_legitimize_move.  Move constant SRC into register
3365    DEST given that SRC satisfies immediate_operand but doesn't satisfy
3366    move_operand.  */
3367
3368 static void
3369 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
3370 {
3371   rtx base, offset;
3372
3373   /* Split moves of big integers into smaller pieces.  */
3374   if (splittable_const_int_operand (src, mode))
3375     {
3376       mips_move_integer (dest, dest, INTVAL (src));
3377       return;
3378     }
3379
3380   /* Split moves of symbolic constants into high/low pairs.  */
3381   if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3382     {
3383       emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3384       return;
3385     }
3386
3387   /* Generate the appropriate access sequences for TLS symbols.  */
3388   if (mips_tls_symbol_p (src))
3389     {
3390       mips_emit_move (dest, mips_legitimize_tls_address (src));
3391       return;
3392     }
3393
3394   /* If we have (const (plus symbol offset)), and that expression cannot
3395      be forced into memory, load the symbol first and add in the offset.
3396      In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3397      forced into memory, as it usually produces better code.  */
3398   split_const (src, &base, &offset);
3399   if (offset != const0_rtx
3400       && (targetm.cannot_force_const_mem (mode, src)
3401           || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3402     {
3403       base = mips_force_temporary (dest, base);
3404       mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3405       return;
3406     }
3407
3408   src = force_const_mem (mode, src);
3409
3410   /* When using explicit relocs, constant pool references are sometimes
3411      not legitimate addresses.  */
3412   mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3413   mips_emit_move (dest, src);
3414 }
3415
3416 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3417    sequence that is valid.  */
3418
3419 bool
3420 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
3421 {
3422   if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3423     {
3424       mips_emit_move (dest, force_reg (mode, src));
3425       return true;
3426     }
3427
3428   /* We need to deal with constants that would be legitimate
3429      immediate_operands but aren't legitimate move_operands.  */
3430   if (CONSTANT_P (src) && !move_operand (src, mode))
3431     {
3432       mips_legitimize_const_move (mode, dest, src);
3433       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3434       return true;
3435     }
3436   return false;
3437 }
3438 \f
3439 /* Return true if value X in context CONTEXT is a small-data address
3440    that can be rewritten as a LO_SUM.  */
3441
3442 static bool
3443 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3444 {
3445   enum mips_symbol_type symbol_type;
3446
3447   return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3448           && !mips_split_p[SYMBOL_GP_RELATIVE]
3449           && mips_symbolic_constant_p (x, context, &symbol_type)
3450           && symbol_type == SYMBOL_GP_RELATIVE);
3451 }
3452
3453 /* Return true if OP refers to small data symbols directly, not through
3454    a LO_SUM.  CONTEXT is the context in which X appears.  */
3455
3456 static int
3457 mips_small_data_pattern_1 (rtx x, enum mips_symbol_context context)
3458 {
3459   subrtx_var_iterator::array_type array;
3460   FOR_EACH_SUBRTX_VAR (iter, array, x, ALL)
3461     {
3462       rtx x = *iter;
3463
3464       /* Ignore things like "g" constraints in asms.  We make no particular
3465          guarantee about which symbolic constants are acceptable as asm operands
3466          versus which must be forced into a GPR.  */
3467       if (GET_CODE (x) == LO_SUM || GET_CODE (x) == ASM_OPERANDS)
3468         iter.skip_subrtxes ();
3469       else if (MEM_P (x))
3470         {
3471           if (mips_small_data_pattern_1 (XEXP (x, 0), SYMBOL_CONTEXT_MEM))
3472             return true;
3473           iter.skip_subrtxes ();
3474         }
3475       else if (mips_rewrite_small_data_p (x, context))
3476         return true;
3477     }
3478   return false;
3479 }
3480
3481 /* Return true if OP refers to small data symbols directly, not through
3482    a LO_SUM.  */
3483
3484 bool
3485 mips_small_data_pattern_p (rtx op)
3486 {
3487   return mips_small_data_pattern_1 (op, SYMBOL_CONTEXT_LEA);
3488 }
3489
3490 /* Rewrite *LOC so that it refers to small data using explicit
3491    relocations.  CONTEXT is the context in which *LOC appears.  */
3492
3493 static void
3494 mips_rewrite_small_data_1 (rtx *loc, enum mips_symbol_context context)
3495 {
3496   subrtx_ptr_iterator::array_type array;
3497   FOR_EACH_SUBRTX_PTR (iter, array, loc, ALL)
3498     {
3499       rtx *loc = *iter;
3500       if (MEM_P (*loc))
3501         {
3502           mips_rewrite_small_data_1 (&XEXP (*loc, 0), SYMBOL_CONTEXT_MEM);
3503           iter.skip_subrtxes ();
3504         }
3505       else if (mips_rewrite_small_data_p (*loc, context))
3506         {
3507           *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3508           iter.skip_subrtxes ();
3509         }
3510       else if (GET_CODE (*loc) == LO_SUM)
3511         iter.skip_subrtxes ();
3512     }
3513 }
3514
3515 /* Rewrite instruction pattern PATTERN so that it refers to small data
3516    using explicit relocations.  */
3517
3518 rtx
3519 mips_rewrite_small_data (rtx pattern)
3520 {
3521   pattern = copy_insn (pattern);
3522   mips_rewrite_small_data_1 (&pattern, SYMBOL_CONTEXT_LEA);
3523   return pattern;
3524 }
3525 \f
3526 /* The cost of loading values from the constant pool.  It should be
3527    larger than the cost of any constant we want to synthesize inline.  */
3528 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3529
3530 /* Return the cost of X when used as an operand to the MIPS16 instruction
3531    that implements CODE.  Return -1 if there is no such instruction, or if
3532    X is not a valid immediate operand for it.  */
3533
3534 static int
3535 mips16_constant_cost (int code, HOST_WIDE_INT x)
3536 {
3537   switch (code)
3538     {
3539     case ASHIFT:
3540     case ASHIFTRT:
3541     case LSHIFTRT:
3542       /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3543          other shifts are extended.  The shift patterns truncate the shift
3544          count to the right size, so there are no out-of-range values.  */
3545       if (IN_RANGE (x, 1, 8))
3546         return 0;
3547       return COSTS_N_INSNS (1);
3548
3549     case PLUS:
3550       if (IN_RANGE (x, -128, 127))
3551         return 0;
3552       if (SMALL_OPERAND (x))
3553         return COSTS_N_INSNS (1);
3554       return -1;
3555
3556     case LEU:
3557       /* Like LE, but reject the always-true case.  */
3558       if (x == -1)
3559         return -1;
3560     case LE:
3561       /* We add 1 to the immediate and use SLT.  */
3562       x += 1;
3563     case XOR:
3564       /* We can use CMPI for an xor with an unsigned 16-bit X.  */
3565     case LT:
3566     case LTU:
3567       if (IN_RANGE (x, 0, 255))
3568         return 0;
3569       if (SMALL_OPERAND_UNSIGNED (x))
3570         return COSTS_N_INSNS (1);
3571       return -1;
3572
3573     case EQ:
3574     case NE:
3575       /* Equality comparisons with 0 are cheap.  */
3576       if (x == 0)
3577         return 0;
3578       return -1;
3579
3580     default:
3581       return -1;
3582     }
3583 }
3584
3585 /* Return true if there is a non-MIPS16 instruction that implements CODE
3586    and if that instruction accepts X as an immediate operand.  */
3587
3588 static int
3589 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3590 {
3591   switch (code)
3592     {
3593     case ASHIFT:
3594     case ASHIFTRT:
3595     case LSHIFTRT:
3596       /* All shift counts are truncated to a valid constant.  */
3597       return true;
3598
3599     case ROTATE:
3600     case ROTATERT:
3601       /* Likewise rotates, if the target supports rotates at all.  */
3602       return ISA_HAS_ROR;
3603
3604     case AND:
3605     case IOR:
3606     case XOR:
3607       /* These instructions take 16-bit unsigned immediates.  */
3608       return SMALL_OPERAND_UNSIGNED (x);
3609
3610     case PLUS:
3611     case LT:
3612     case LTU:
3613       /* These instructions take 16-bit signed immediates.  */
3614       return SMALL_OPERAND (x);
3615
3616     case EQ:
3617     case NE:
3618     case GT:
3619     case GTU:
3620       /* The "immediate" forms of these instructions are really
3621          implemented as comparisons with register 0.  */
3622       return x == 0;
3623
3624     case GE:
3625     case GEU:
3626       /* Likewise, meaning that the only valid immediate operand is 1.  */
3627       return x == 1;
3628
3629     case LE:
3630       /* We add 1 to the immediate and use SLT.  */
3631       return SMALL_OPERAND (x + 1);
3632
3633     case LEU:
3634       /* Likewise SLTU, but reject the always-true case.  */
3635       return SMALL_OPERAND (x + 1) && x + 1 != 0;
3636
3637     case SIGN_EXTRACT:
3638     case ZERO_EXTRACT:
3639       /* The bit position and size are immediate operands.  */
3640       return ISA_HAS_EXT_INS;
3641
3642     default:
3643       /* By default assume that $0 can be used for 0.  */
3644       return x == 0;
3645     }
3646 }
3647
3648 /* Return the cost of binary operation X, given that the instruction
3649    sequence for a word-sized or smaller operation has cost SINGLE_COST
3650    and that the sequence of a double-word operation has cost DOUBLE_COST.
3651    If SPEED is true, optimize for speed otherwise optimize for size.  */
3652
3653 static int
3654 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3655 {
3656   int cost;
3657
3658   if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3659     cost = double_cost;
3660   else
3661     cost = single_cost;
3662   return (cost
3663           + set_src_cost (XEXP (x, 0), speed)
3664           + rtx_cost (XEXP (x, 1), GET_CODE (x), 1, speed));
3665 }
3666
3667 /* Return the cost of floating-point multiplications of mode MODE.  */
3668
3669 static int
3670 mips_fp_mult_cost (enum machine_mode mode)
3671 {
3672   return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3673 }
3674
3675 /* Return the cost of floating-point divisions of mode MODE.  */
3676
3677 static int
3678 mips_fp_div_cost (enum machine_mode mode)
3679 {
3680   return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3681 }
3682
3683 /* Return the cost of sign-extending OP to mode MODE, not including the
3684    cost of OP itself.  */
3685
3686 static int
3687 mips_sign_extend_cost (enum machine_mode mode, rtx op)
3688 {
3689   if (MEM_P (op))
3690     /* Extended loads are as cheap as unextended ones.  */
3691     return 0;
3692
3693   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3694     /* A sign extension from SImode to DImode in 64-bit mode is free.  */
3695     return 0;
3696
3697   if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3698     /* We can use SEB or SEH.  */
3699     return COSTS_N_INSNS (1);
3700
3701   /* We need to use a shift left and a shift right.  */
3702   return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3703 }
3704
3705 /* Return the cost of zero-extending OP to mode MODE, not including the
3706    cost of OP itself.  */
3707
3708 static int
3709 mips_zero_extend_cost (enum machine_mode mode, rtx op)
3710 {
3711   if (MEM_P (op))
3712     /* Extended loads are as cheap as unextended ones.  */
3713     return 0;
3714
3715   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3716     /* We need a shift left by 32 bits and a shift right by 32 bits.  */
3717     return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3718
3719   if (GENERATE_MIPS16E)
3720     /* We can use ZEB or ZEH.  */
3721     return COSTS_N_INSNS (1);
3722
3723   if (TARGET_MIPS16)
3724     /* We need to load 0xff or 0xffff into a register and use AND.  */
3725     return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3726
3727   /* We can use ANDI.  */
3728   return COSTS_N_INSNS (1);
3729 }
3730
3731 /* Return the cost of moving between two registers of mode MODE,
3732    assuming that the move will be in pieces of at most UNITS bytes.  */
3733
3734 static int
3735 mips_set_reg_reg_piece_cost (enum machine_mode mode, unsigned int units)
3736 {
3737   return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3738 }
3739
3740 /* Return the cost of moving between two registers of mode MODE.  */
3741
3742 static int
3743 mips_set_reg_reg_cost (enum machine_mode mode)
3744 {
3745   switch (GET_MODE_CLASS (mode))
3746     {
3747     case MODE_CC:
3748       return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3749
3750     case MODE_FLOAT:
3751     case MODE_COMPLEX_FLOAT:
3752     case MODE_VECTOR_FLOAT:
3753       if (TARGET_HARD_FLOAT)
3754         return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3755       /* Fall through */
3756
3757     default:
3758       return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3759     }
3760 }
3761
3762 /* Implement TARGET_RTX_COSTS.  */
3763
3764 static bool
3765 mips_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3766                 int *total, bool speed)
3767 {
3768   enum machine_mode mode = GET_MODE (x);
3769   bool float_mode_p = FLOAT_MODE_P (mode);
3770   int cost;
3771   rtx addr;
3772
3773   /* The cost of a COMPARE is hard to define for MIPS.  COMPAREs don't
3774      appear in the instruction stream, and the cost of a comparison is
3775      really the cost of the branch or scc condition.  At the time of
3776      writing, GCC only uses an explicit outer COMPARE code when optabs
3777      is testing whether a constant is expensive enough to force into a
3778      register.  We want optabs to pass such constants through the MIPS
3779      expanders instead, so make all constants very cheap here.  */
3780   if (outer_code == COMPARE)
3781     {
3782       gcc_assert (CONSTANT_P (x));
3783       *total = 0;
3784       return true;
3785     }
3786
3787   switch (code)
3788     {
3789     case CONST_INT:
3790       /* Treat *clear_upper32-style ANDs as having zero cost in the
3791          second operand.  The cost is entirely in the first operand.
3792
3793          ??? This is needed because we would otherwise try to CSE
3794          the constant operand.  Although that's the right thing for
3795          instructions that continue to be a register operation throughout
3796          compilation, it is disastrous for instructions that could
3797          later be converted into a memory operation.  */
3798       if (TARGET_64BIT
3799           && outer_code == AND
3800           && UINTVAL (x) == 0xffffffff)
3801         {
3802           *total = 0;
3803           return true;
3804         }
3805
3806       if (TARGET_MIPS16)
3807         {
3808           cost = mips16_constant_cost (outer_code, INTVAL (x));
3809           if (cost >= 0)
3810             {
3811               *total = cost;
3812               return true;
3813             }
3814         }
3815       else
3816         {
3817           /* When not optimizing for size, we care more about the cost
3818              of hot code, and hot code is often in a loop.  If a constant
3819              operand needs to be forced into a register, we will often be
3820              able to hoist the constant load out of the loop, so the load
3821              should not contribute to the cost.  */
3822           if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3823             {
3824               *total = 0;
3825               return true;
3826             }
3827         }
3828       /* Fall through.  */
3829
3830     case CONST:
3831     case SYMBOL_REF:
3832     case LABEL_REF:
3833     case CONST_DOUBLE:
3834       if (force_to_mem_operand (x, VOIDmode))
3835         {
3836           *total = COSTS_N_INSNS (1);
3837           return true;
3838         }
3839       cost = mips_const_insns (x);
3840       if (cost > 0)
3841         {
3842           /* If the constant is likely to be stored in a GPR, SETs of
3843              single-insn constants are as cheap as register sets; we
3844              never want to CSE them.
3845
3846              Don't reduce the cost of storing a floating-point zero in
3847              FPRs.  If we have a zero in an FPR for other reasons, we
3848              can get better cfg-cleanup and delayed-branch results by
3849              using it consistently, rather than using $0 sometimes and
3850              an FPR at other times.  Also, moves between floating-point
3851              registers are sometimes cheaper than (D)MTC1 $0.  */
3852           if (cost == 1
3853               && outer_code == SET
3854               && !(float_mode_p && TARGET_HARD_FLOAT))
3855             cost = 0;
3856           /* When non-MIPS16 code loads a constant N>1 times, we rarely
3857              want to CSE the constant itself.  It is usually better to
3858              have N copies of the last operation in the sequence and one
3859              shared copy of the other operations.  (Note that this is
3860              not true for MIPS16 code, where the final operation in the
3861              sequence is often an extended instruction.)
3862
3863              Also, if we have a CONST_INT, we don't know whether it is
3864              for a word or doubleword operation, so we cannot rely on
3865              the result of mips_build_integer.  */
3866           else if (!TARGET_MIPS16
3867                    && (outer_code == SET || mode == VOIDmode))
3868             cost = 1;
3869           *total = COSTS_N_INSNS (cost);
3870           return true;
3871         }
3872       /* The value will need to be fetched from the constant pool.  */
3873       *total = CONSTANT_POOL_COST;
3874       return true;
3875
3876     case MEM:
3877       /* If the address is legitimate, return the number of
3878          instructions it needs.  */
3879       addr = XEXP (x, 0);
3880       cost = mips_address_insns (addr, mode, true);
3881       if (cost > 0)
3882         {
3883           *total = COSTS_N_INSNS (cost + 1);
3884           return true;
3885         }
3886       /* Check for a scaled indexed address.  */
3887       if (mips_lwxs_address_p (addr)
3888           || mips_lx_address_p (addr, mode))
3889         {
3890           *total = COSTS_N_INSNS (2);
3891           return true;
3892         }
3893       /* Otherwise use the default handling.  */
3894       return false;
3895
3896     case FFS:
3897       *total = COSTS_N_INSNS (6);
3898       return false;
3899
3900     case NOT:
3901       *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3902       return false;
3903
3904     case AND:
3905       /* Check for a *clear_upper32 pattern and treat it like a zero
3906          extension.  See the pattern's comment for details.  */
3907       if (TARGET_64BIT
3908           && mode == DImode
3909           && CONST_INT_P (XEXP (x, 1))
3910           && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3911         {
3912           *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3913                     + set_src_cost (XEXP (x, 0), speed));
3914           return true;
3915         }
3916       if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
3917         {
3918           rtx op = XEXP (x, 0);
3919           if (GET_CODE (op) == ASHIFT
3920               && CONST_INT_P (XEXP (op, 1))
3921               && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
3922             {
3923               *total = COSTS_N_INSNS (1) + set_src_cost (XEXP (op, 0), speed);
3924               return true;
3925             }
3926         }
3927       /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
3928          a single instruction.  */
3929       if (!TARGET_MIPS16
3930           && GET_CODE (XEXP (x, 0)) == NOT
3931           && GET_CODE (XEXP (x, 1)) == NOT)
3932         {
3933           cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
3934           *total = (COSTS_N_INSNS (cost)
3935                     + set_src_cost (XEXP (XEXP (x, 0), 0), speed)
3936                     + set_src_cost (XEXP (XEXP (x, 1), 0), speed));
3937           return true;
3938         }
3939             
3940       /* Fall through.  */
3941
3942     case IOR:
3943     case XOR:
3944       /* Double-word operations use two single-word operations.  */
3945       *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3946                                  speed);
3947       return true;
3948
3949     case ASHIFT:
3950     case ASHIFTRT:
3951     case LSHIFTRT:
3952     case ROTATE:
3953     case ROTATERT:
3954       if (CONSTANT_P (XEXP (x, 1)))
3955         *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3956                                    speed);
3957       else
3958         *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
3959                                    speed);
3960       return true;
3961
3962     case ABS:
3963       if (float_mode_p)
3964         *total = mips_cost->fp_add;
3965       else
3966         *total = COSTS_N_INSNS (4);
3967       return false;
3968
3969     case LO_SUM:
3970       /* Low-part immediates need an extended MIPS16 instruction.  */
3971       *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3972                 + set_src_cost (XEXP (x, 0), speed));
3973       return true;
3974
3975     case LT:
3976     case LTU:
3977     case LE:
3978     case LEU:
3979     case GT:
3980     case GTU:
3981     case GE:
3982     case GEU:
3983     case EQ:
3984     case NE:
3985     case UNORDERED:
3986     case LTGT:
3987       /* Branch comparisons have VOIDmode, so use the first operand's
3988          mode instead.  */
3989       mode = GET_MODE (XEXP (x, 0));
3990       if (FLOAT_MODE_P (mode))
3991         {
3992           *total = mips_cost->fp_add;
3993           return false;
3994         }
3995       *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3996                                  speed);
3997       return true;
3998
3999     case MINUS:
4000       if (float_mode_p
4001           && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
4002           && TARGET_FUSED_MADD
4003           && !HONOR_NANS (mode)
4004           && !HONOR_SIGNED_ZEROS (mode))
4005         {
4006           /* See if we can use NMADD or NMSUB.  See mips.md for the
4007              associated patterns.  */
4008           rtx op0 = XEXP (x, 0);
4009           rtx op1 = XEXP (x, 1);
4010           if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
4011             {
4012               *total = (mips_fp_mult_cost (mode)
4013                         + set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
4014                         + set_src_cost (XEXP (op0, 1), speed)
4015                         + set_src_cost (op1, speed));
4016               return true;
4017             }
4018           if (GET_CODE (op1) == MULT)
4019             {
4020               *total = (mips_fp_mult_cost (mode)
4021                         + set_src_cost (op0, speed)
4022                         + set_src_cost (XEXP (op1, 0), speed)
4023                         + set_src_cost (XEXP (op1, 1), speed));
4024               return true;
4025             }
4026         }
4027       /* Fall through.  */
4028
4029     case PLUS:
4030       if (float_mode_p)
4031         {
4032           /* If this is part of a MADD or MSUB, treat the PLUS as
4033              being free.  */
4034           if ((ISA_HAS_FP_MADD4_MSUB4 || ISA_HAS_FP_MADD3_MSUB3)
4035               && TARGET_FUSED_MADD
4036               && GET_CODE (XEXP (x, 0)) == MULT)
4037             *total = 0;
4038           else
4039             *total = mips_cost->fp_add;
4040           return false;
4041         }
4042
4043       /* Double-word operations require three single-word operations and
4044          an SLTU.  The MIPS16 version then needs to move the result of
4045          the SLTU from $24 to a MIPS16 register.  */
4046       *total = mips_binary_cost (x, COSTS_N_INSNS (1),
4047                                  COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
4048                                  speed);
4049       return true;
4050
4051     case NEG:
4052       if (float_mode_p
4053           && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
4054           && TARGET_FUSED_MADD
4055           && !HONOR_NANS (mode)
4056           && HONOR_SIGNED_ZEROS (mode))
4057         {
4058           /* See if we can use NMADD or NMSUB.  See mips.md for the
4059              associated patterns.  */
4060           rtx op = XEXP (x, 0);
4061           if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
4062               && GET_CODE (XEXP (op, 0)) == MULT)
4063             {
4064               *total = (mips_fp_mult_cost (mode)
4065                         + set_src_cost (XEXP (XEXP (op, 0), 0), speed)
4066                         + set_src_cost (XEXP (XEXP (op, 0), 1), speed)
4067                         + set_src_cost (XEXP (op, 1), speed));
4068               return true;
4069             }
4070         }
4071
4072       if (float_mode_p)
4073         *total = mips_cost->fp_add;
4074       else
4075         *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
4076       return false;
4077
4078     case MULT:
4079       if (float_mode_p)
4080         *total = mips_fp_mult_cost (mode);
4081       else if (mode == DImode && !TARGET_64BIT)
4082         /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
4083            where the mulsidi3 always includes an MFHI and an MFLO.  */
4084         *total = (speed
4085                   ? mips_cost->int_mult_si * 3 + 6
4086                   : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
4087       else if (!speed)
4088         *total = COSTS_N_INSNS (ISA_HAS_MUL3 ? 1 : 2) + 1;
4089       else if (mode == DImode)
4090         *total = mips_cost->int_mult_di;
4091       else
4092         *total = mips_cost->int_mult_si;
4093       return false;
4094
4095     case DIV:
4096       /* Check for a reciprocal.  */
4097       if (float_mode_p
4098           && ISA_HAS_FP_RECIP_RSQRT (mode)
4099           && flag_unsafe_math_optimizations
4100           && XEXP (x, 0) == CONST1_RTX (mode))
4101         {
4102           if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
4103             /* An rsqrt<mode>a or rsqrt<mode>b pattern.  Count the
4104                division as being free.  */
4105             *total = set_src_cost (XEXP (x, 1), speed);
4106           else
4107             *total = (mips_fp_div_cost (mode)
4108                       + set_src_cost (XEXP (x, 1), speed));
4109           return true;
4110         }
4111       /* Fall through.  */
4112
4113     case SQRT:
4114     case MOD:
4115       if (float_mode_p)
4116         {
4117           *total = mips_fp_div_cost (mode);
4118           return false;
4119         }
4120       /* Fall through.  */
4121
4122     case UDIV:
4123     case UMOD:
4124       if (!speed)
4125         {
4126           /* It is our responsibility to make division by a power of 2
4127              as cheap as 2 register additions if we want the division
4128              expanders to be used for such operations; see the setting
4129              of sdiv_pow2_cheap in optabs.c.  Using (D)DIV for MIPS16
4130              should always produce shorter code than using
4131              expand_sdiv2_pow2.  */
4132           if (TARGET_MIPS16
4133               && CONST_INT_P (XEXP (x, 1))
4134               && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
4135             {
4136               *total = COSTS_N_INSNS (2) + set_src_cost (XEXP (x, 0), speed);
4137               return true;
4138             }
4139           *total = COSTS_N_INSNS (mips_idiv_insns ());
4140         }
4141       else if (mode == DImode)
4142         *total = mips_cost->int_div_di;
4143       else
4144         *total = mips_cost->int_div_si;
4145       return false;
4146
4147     case SIGN_EXTEND:
4148       *total = mips_sign_extend_cost (mode, XEXP (x, 0));
4149       return false;
4150
4151     case ZERO_EXTEND:
4152       if (outer_code == SET
4153           && ISA_HAS_BADDU
4154           && (GET_CODE (XEXP (x, 0)) == TRUNCATE
4155               || GET_CODE (XEXP (x, 0)) == SUBREG)
4156           && GET_MODE (XEXP (x, 0)) == QImode
4157           && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
4158         {
4159           *total = set_src_cost (XEXP (XEXP (x, 0), 0), speed);
4160           return true;
4161         }
4162       *total = mips_zero_extend_cost (mode, XEXP (x, 0));
4163       return false;
4164
4165     case FLOAT:
4166     case UNSIGNED_FLOAT:
4167     case FIX:
4168     case FLOAT_EXTEND:
4169     case FLOAT_TRUNCATE:
4170       *total = mips_cost->fp_add;
4171       return false;
4172
4173     case SET:
4174       if (register_operand (SET_DEST (x), VOIDmode)
4175           && reg_or_0_operand (SET_SRC (x), VOIDmode))
4176         {
4177           *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4178           return true;
4179         }
4180       return false;
4181
4182     default:
4183       return false;
4184     }
4185 }
4186
4187 /* Implement TARGET_ADDRESS_COST.  */
4188
4189 static int
4190 mips_address_cost (rtx addr, enum machine_mode mode,
4191                    addr_space_t as ATTRIBUTE_UNUSED,
4192                    bool speed ATTRIBUTE_UNUSED)
4193 {
4194   return mips_address_insns (addr, mode, false);
4195 }
4196 \f
4197 /* Information about a single instruction in a multi-instruction
4198    asm sequence.  */
4199 struct mips_multi_member {
4200   /* True if this is a label, false if it is code.  */
4201   bool is_label_p;
4202
4203   /* The output_asm_insn format of the instruction.  */
4204   const char *format;
4205
4206   /* The operands to the instruction.  */
4207   rtx operands[MAX_RECOG_OPERANDS];
4208 };
4209 typedef struct mips_multi_member mips_multi_member;
4210
4211 /* The instructions that make up the current multi-insn sequence.  */
4212 static vec<mips_multi_member> mips_multi_members;
4213
4214 /* How many instructions (as opposed to labels) are in the current
4215    multi-insn sequence.  */
4216 static unsigned int mips_multi_num_insns;
4217
4218 /* Start a new multi-insn sequence.  */
4219
4220 static void
4221 mips_multi_start (void)
4222 {
4223   mips_multi_members.truncate (0);
4224   mips_multi_num_insns = 0;
4225 }
4226
4227 /* Add a new, uninitialized member to the current multi-insn sequence.  */
4228
4229 static struct mips_multi_member *
4230 mips_multi_add (void)
4231 {
4232   mips_multi_member empty;
4233   return mips_multi_members.safe_push (empty);
4234 }
4235
4236 /* Add a normal insn with the given asm format to the current multi-insn
4237    sequence.  The other arguments are a null-terminated list of operands.  */
4238
4239 static void
4240 mips_multi_add_insn (const char *format, ...)
4241 {
4242   struct mips_multi_member *member;
4243   va_list ap;
4244   unsigned int i;
4245   rtx op;
4246
4247   member = mips_multi_add ();
4248   member->is_label_p = false;
4249   member->format = format;
4250   va_start (ap, format);
4251   i = 0;
4252   while ((op = va_arg (ap, rtx)))
4253     member->operands[i++] = op;
4254   va_end (ap);
4255   mips_multi_num_insns++;
4256 }
4257
4258 /* Add the given label definition to the current multi-insn sequence.
4259    The definition should include the colon.  */
4260
4261 static void
4262 mips_multi_add_label (const char *label)
4263 {
4264   struct mips_multi_member *member;
4265
4266   member = mips_multi_add ();
4267   member->is_label_p = true;
4268   member->format = label;
4269 }
4270
4271 /* Return the index of the last member of the current multi-insn sequence.  */
4272
4273 static unsigned int
4274 mips_multi_last_index (void)
4275 {
4276   return mips_multi_members.length () - 1;
4277 }
4278
4279 /* Add a copy of an existing instruction to the current multi-insn
4280    sequence.  I is the index of the instruction that should be copied.  */
4281
4282 static void
4283 mips_multi_copy_insn (unsigned int i)
4284 {
4285   struct mips_multi_member *member;
4286
4287   member = mips_multi_add ();
4288   memcpy (member, &mips_multi_members[i], sizeof (*member));
4289   gcc_assert (!member->is_label_p);
4290 }
4291
4292 /* Change the operand of an existing instruction in the current
4293    multi-insn sequence.  I is the index of the instruction,
4294    OP is the index of the operand, and X is the new value.  */
4295
4296 static void
4297 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4298 {
4299   mips_multi_members[i].operands[op] = x;
4300 }
4301
4302 /* Write out the asm code for the current multi-insn sequence.  */
4303
4304 static void
4305 mips_multi_write (void)
4306 {
4307   struct mips_multi_member *member;
4308   unsigned int i;
4309
4310   FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4311     if (member->is_label_p)
4312       fprintf (asm_out_file, "%s\n", member->format);
4313     else
4314       output_asm_insn (member->format, member->operands);
4315 }
4316 \f
4317 /* Return one word of double-word value OP, taking into account the fixed
4318    endianness of certain registers.  HIGH_P is true to select the high part,
4319    false to select the low part.  */
4320
4321 rtx
4322 mips_subword (rtx op, bool high_p)
4323 {
4324   unsigned int byte, offset;
4325   enum machine_mode mode;
4326
4327   mode = GET_MODE (op);
4328   if (mode == VOIDmode)
4329     mode = TARGET_64BIT ? TImode : DImode;
4330
4331   if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4332     byte = UNITS_PER_WORD;
4333   else
4334     byte = 0;
4335
4336   if (FP_REG_RTX_P (op))
4337     {
4338       /* Paired FPRs are always ordered little-endian.  */
4339       offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4340       return gen_rtx_REG (word_mode, REGNO (op) + offset);
4341     }
4342
4343   if (MEM_P (op))
4344     return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4345
4346   return simplify_gen_subreg (word_mode, op, mode, byte);
4347 }
4348
4349 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4350    SPLIT_TYPE is the condition under which moves should be split.  */
4351
4352 static bool
4353 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4354 {
4355   return ((split_type != SPLIT_FOR_SPEED
4356            || mips_tuning_info.fast_mult_zero_zero_p)
4357           && src == const0_rtx
4358           && REG_P (dest)
4359           && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4360           && (ISA_HAS_DSP_MULT
4361               ? ACC_REG_P (REGNO (dest))
4362               : MD_REG_P (REGNO (dest))));
4363 }
4364
4365 /* Return true if a move from SRC to DEST should be split into two.
4366    SPLIT_TYPE describes the split condition.  */
4367
4368 bool
4369 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4370 {
4371   /* Check whether the move can be done using some variant of MULT $0,$0.  */
4372   if (mips_mult_move_p (dest, src, split_type))
4373     return false;
4374
4375   /* FPR-to-FPR moves can be done in a single instruction, if they're
4376      allowed at all.  */
4377   unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4378   if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4379     return false;
4380
4381   /* Check for floating-point loads and stores.  */
4382   if (size == 8 && ISA_HAS_LDC1_SDC1)
4383     {
4384       if (FP_REG_RTX_P (dest) && MEM_P (src))
4385         return false;
4386       if (FP_REG_RTX_P (src) && MEM_P (dest))
4387         return false;
4388     }
4389
4390   /* Otherwise split all multiword moves.  */
4391   return size > UNITS_PER_WORD;
4392 }
4393
4394 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
4395    SPLIT_TYPE describes the split condition.  */
4396
4397 void
4398 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type)
4399 {
4400   rtx low_dest;
4401
4402   gcc_checking_assert (mips_split_move_p (dest, src, split_type));
4403   if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4404     {
4405       if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4406         emit_insn (gen_move_doubleword_fprdi (dest, src));
4407       else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4408         emit_insn (gen_move_doubleword_fprdf (dest, src));
4409       else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4410         emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4411       else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4412         emit_insn (gen_move_doubleword_fprv2si (dest, src));
4413       else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4414         emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4415       else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4416         emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4417       else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4418         emit_insn (gen_move_doubleword_fprtf (dest, src));
4419       else
4420         gcc_unreachable ();
4421     }
4422   else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4423     {
4424       low_dest = mips_subword (dest, false);
4425       mips_emit_move (low_dest, mips_subword (src, false));
4426       if (TARGET_64BIT)
4427         emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4428       else
4429         emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4430     }
4431   else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4432     {
4433       mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4434       if (TARGET_64BIT)
4435         emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4436       else
4437         emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4438     }
4439   else
4440     {
4441       /* The operation can be split into two normal moves.  Decide in
4442          which order to do them.  */
4443       low_dest = mips_subword (dest, false);
4444       if (REG_P (low_dest)
4445           && reg_overlap_mentioned_p (low_dest, src))
4446         {
4447           mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4448           mips_emit_move (low_dest, mips_subword (src, false));
4449         }
4450       else
4451         {
4452           mips_emit_move (low_dest, mips_subword (src, false));
4453           mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4454         }
4455     }
4456 }
4457
4458 /* Return the split type for instruction INSN.  */
4459
4460 static enum mips_split_type
4461 mips_insn_split_type (rtx insn)
4462 {
4463   basic_block bb = BLOCK_FOR_INSN (insn);
4464   if (bb)
4465     {
4466       if (optimize_bb_for_speed_p (bb))
4467         return SPLIT_FOR_SPEED;
4468       else
4469         return SPLIT_FOR_SIZE;
4470     }
4471   /* Once CFG information has been removed, we should trust the optimization
4472      decisions made by previous passes and only split where necessary.  */
4473   return SPLIT_IF_NECESSARY;
4474 }
4475
4476 /* Return true if a move from SRC to DEST in INSN should be split.  */
4477
4478 bool
4479 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
4480 {
4481   return mips_split_move_p (dest, src, mips_insn_split_type (insn));
4482 }
4483
4484 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
4485    holds.  */
4486
4487 void
4488 mips_split_move_insn (rtx dest, rtx src, rtx insn)
4489 {
4490   mips_split_move (dest, src, mips_insn_split_type (insn));
4491 }
4492 \f
4493 /* Return the appropriate instructions to move SRC into DEST.  Assume
4494    that SRC is operand 1 and DEST is operand 0.  */
4495
4496 const char *
4497 mips_output_move (rtx dest, rtx src)
4498 {
4499   enum rtx_code dest_code, src_code;
4500   enum machine_mode mode;
4501   enum mips_symbol_type symbol_type;
4502   bool dbl_p;
4503
4504   dest_code = GET_CODE (dest);
4505   src_code = GET_CODE (src);
4506   mode = GET_MODE (dest);
4507   dbl_p = (GET_MODE_SIZE (mode) == 8);
4508
4509   if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
4510     return "#";
4511
4512   if ((src_code == REG && GP_REG_P (REGNO (src)))
4513       || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4514     {
4515       if (dest_code == REG)
4516         {
4517           if (GP_REG_P (REGNO (dest)))
4518             return "move\t%0,%z1";
4519
4520           if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
4521             {
4522               if (ISA_HAS_DSP_MULT)
4523                 return "mult\t%q0,%.,%.";
4524               else
4525                 return "mult\t%.,%.";
4526             }
4527
4528           /* Moves to HI are handled by special .md insns.  */
4529           if (REGNO (dest) == LO_REGNUM)
4530             return "mtlo\t%z1";
4531
4532           if (DSP_ACC_REG_P (REGNO (dest)))
4533             {
4534               static char retval[] = "mt__\t%z1,%q0";
4535
4536               retval[2] = reg_names[REGNO (dest)][4];
4537               retval[3] = reg_names[REGNO (dest)][5];
4538               return retval;
4539             }
4540
4541           if (FP_REG_P (REGNO (dest)))
4542             return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4543
4544           if (ALL_COP_REG_P (REGNO (dest)))
4545             {
4546               static char retval[] = "dmtc_\t%z1,%0";
4547
4548               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4549               return dbl_p ? retval : retval + 1;
4550             }
4551         }
4552       if (dest_code == MEM)
4553         switch (GET_MODE_SIZE (mode))
4554           {
4555           case 1: return "sb\t%z1,%0";
4556           case 2: return "sh\t%z1,%0";
4557           case 4: return "sw\t%z1,%0";
4558           case 8: return "sd\t%z1,%0";
4559           }
4560     }
4561   if (dest_code == REG && GP_REG_P (REGNO (dest)))
4562     {
4563       if (src_code == REG)
4564         {
4565           /* Moves from HI are handled by special .md insns.  */
4566           if (REGNO (src) == LO_REGNUM)
4567             {
4568               /* When generating VR4120 or VR4130 code, we use MACC and
4569                  DMACC instead of MFLO.  This avoids both the normal
4570                  MIPS III HI/LO hazards and the errata related to
4571                  -mfix-vr4130.  */
4572               if (ISA_HAS_MACCHI)
4573                 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4574               return "mflo\t%0";
4575             }
4576
4577           if (DSP_ACC_REG_P (REGNO (src)))
4578             {
4579               static char retval[] = "mf__\t%0,%q1";
4580
4581               retval[2] = reg_names[REGNO (src)][4];
4582               retval[3] = reg_names[REGNO (src)][5];
4583               return retval;
4584             }
4585
4586           if (FP_REG_P (REGNO (src)))
4587             return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4588
4589           if (ALL_COP_REG_P (REGNO (src)))
4590             {
4591               static char retval[] = "dmfc_\t%0,%1";
4592
4593               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4594               return dbl_p ? retval : retval + 1;
4595             }
4596         }
4597
4598       if (src_code == MEM)
4599         switch (GET_MODE_SIZE (mode))
4600           {
4601           case 1: return "lbu\t%0,%1";
4602           case 2: return "lhu\t%0,%1";
4603           case 4: return "lw\t%0,%1";
4604           case 8: return "ld\t%0,%1";
4605           }
4606
4607       if (src_code == CONST_INT)
4608         {
4609           /* Don't use the X format for the operand itself, because that
4610              will give out-of-range numbers for 64-bit hosts and 32-bit
4611              targets.  */
4612           if (!TARGET_MIPS16)
4613             return "li\t%0,%1\t\t\t# %X1";
4614
4615           if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4616             return "li\t%0,%1";
4617
4618           if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4619             return "#";
4620         }
4621
4622       if (src_code == HIGH)
4623         return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4624
4625       if (CONST_GP_P (src))
4626         return "move\t%0,%1";
4627
4628       if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4629           && mips_lo_relocs[symbol_type] != 0)
4630         {
4631           /* A signed 16-bit constant formed by applying a relocation
4632              operator to a symbolic address.  */
4633           gcc_assert (!mips_split_p[symbol_type]);
4634           return "li\t%0,%R1";
4635         }
4636
4637       if (symbolic_operand (src, VOIDmode))
4638         {
4639           gcc_assert (TARGET_MIPS16
4640                       ? TARGET_MIPS16_TEXT_LOADS
4641                       : !TARGET_EXPLICIT_RELOCS);
4642           return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4643         }
4644     }
4645   if (src_code == REG && FP_REG_P (REGNO (src)))
4646     {
4647       if (dest_code == REG && FP_REG_P (REGNO (dest)))
4648         {
4649           if (GET_MODE (dest) == V2SFmode)
4650             return "mov.ps\t%0,%1";
4651           else
4652             return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4653         }
4654
4655       if (dest_code == MEM)
4656         return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4657     }
4658   if (dest_code == REG && FP_REG_P (REGNO (dest)))
4659     {
4660       if (src_code == MEM)
4661         return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4662     }
4663   if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4664     {
4665       static char retval[] = "l_c_\t%0,%1";
4666
4667       retval[1] = (dbl_p ? 'd' : 'w');
4668       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4669       return retval;
4670     }
4671   if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4672     {
4673       static char retval[] = "s_c_\t%1,%0";
4674
4675       retval[1] = (dbl_p ? 'd' : 'w');
4676       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4677       return retval;
4678     }
4679   gcc_unreachable ();
4680 }
4681 \f
4682 /* Return true if CMP1 is a suitable second operand for integer ordering
4683    test CODE.  See also the *sCC patterns in mips.md.  */
4684
4685 static bool
4686 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4687 {
4688   switch (code)
4689     {
4690     case GT:
4691     case GTU:
4692       return reg_or_0_operand (cmp1, VOIDmode);
4693
4694     case GE:
4695     case GEU:
4696       return !TARGET_MIPS16 && cmp1 == const1_rtx;
4697
4698     case LT:
4699     case LTU:
4700       return arith_operand (cmp1, VOIDmode);
4701
4702     case LE:
4703       return sle_operand (cmp1, VOIDmode);
4704
4705     case LEU:
4706       return sleu_operand (cmp1, VOIDmode);
4707
4708     default:
4709       gcc_unreachable ();
4710     }
4711 }
4712
4713 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4714    integer ordering test *CODE, or if an equivalent combination can
4715    be formed by adjusting *CODE and *CMP1.  When returning true, update
4716    *CODE and *CMP1 with the chosen code and operand, otherwise leave
4717    them alone.  */
4718
4719 static bool
4720 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4721                                   enum machine_mode mode)
4722 {
4723   HOST_WIDE_INT plus_one;
4724
4725   if (mips_int_order_operand_ok_p (*code, *cmp1))
4726     return true;
4727
4728   if (CONST_INT_P (*cmp1))
4729     switch (*code)
4730       {
4731       case LE:
4732         plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4733         if (INTVAL (*cmp1) < plus_one)
4734           {
4735             *code = LT;
4736             *cmp1 = force_reg (mode, GEN_INT (plus_one));
4737             return true;
4738           }
4739         break;
4740
4741       case LEU:
4742         plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4743         if (plus_one != 0)
4744           {
4745             *code = LTU;
4746             *cmp1 = force_reg (mode, GEN_INT (plus_one));
4747             return true;
4748           }
4749         break;
4750
4751       default:
4752         break;
4753       }
4754   return false;
4755 }
4756
4757 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4758    in TARGET.  CMP0 and TARGET are register_operands.  If INVERT_PTR
4759    is nonnull, it's OK to set TARGET to the inverse of the result and
4760    flip *INVERT_PTR instead.  */
4761
4762 static void
4763 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4764                           rtx target, rtx cmp0, rtx cmp1)
4765 {
4766   enum machine_mode mode;
4767
4768   /* First see if there is a MIPS instruction that can do this operation.
4769      If not, try doing the same for the inverse operation.  If that also
4770      fails, force CMP1 into a register and try again.  */
4771   mode = GET_MODE (cmp0);
4772   if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4773     mips_emit_binary (code, target, cmp0, cmp1);
4774   else
4775     {
4776       enum rtx_code inv_code = reverse_condition (code);
4777       if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4778         {
4779           cmp1 = force_reg (mode, cmp1);
4780           mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4781         }
4782       else if (invert_ptr == 0)
4783         {
4784           rtx inv_target;
4785
4786           inv_target = mips_force_binary (GET_MODE (target),
4787                                           inv_code, cmp0, cmp1);
4788           mips_emit_binary (XOR, target, inv_target, const1_rtx);
4789         }
4790       else
4791         {
4792           *invert_ptr = !*invert_ptr;
4793           mips_emit_binary (inv_code, target, cmp0, cmp1);
4794         }
4795     }
4796 }
4797
4798 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4799    The register will have the same mode as CMP0.  */
4800
4801 static rtx
4802 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4803 {
4804   if (cmp1 == const0_rtx)
4805     return cmp0;
4806
4807   if (uns_arith_operand (cmp1, VOIDmode))
4808     return expand_binop (GET_MODE (cmp0), xor_optab,
4809                          cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4810
4811   return expand_binop (GET_MODE (cmp0), sub_optab,
4812                        cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4813 }
4814
4815 /* Convert *CODE into a code that can be used in a floating-point
4816    scc instruction (C.cond.fmt).  Return true if the values of
4817    the condition code registers will be inverted, with 0 indicating
4818    that the condition holds.  */
4819
4820 static bool
4821 mips_reversed_fp_cond (enum rtx_code *code)
4822 {
4823   switch (*code)
4824     {
4825     case NE:
4826     case LTGT:
4827     case ORDERED:
4828       *code = reverse_condition_maybe_unordered (*code);
4829       return true;
4830
4831     default:
4832       return false;
4833     }
4834 }
4835
4836 /* Allocate a floating-point condition-code register of mode MODE.
4837
4838    These condition code registers are used for certain kinds
4839    of compound operation, such as compare and branches, vconds,
4840    and built-in functions.  At expand time, their use is entirely
4841    controlled by MIPS-specific code and is entirely internal
4842    to these compound operations.
4843
4844    We could (and did in the past) expose condition-code values
4845    as pseudo registers and leave the register allocator to pick
4846    appropriate registers.  The problem is that it is not practically
4847    possible for the rtl optimizers to guarantee that no spills will
4848    be needed, even when AVOID_CCMODE_COPIES is defined.  We would
4849    therefore need spill and reload sequences to handle the worst case.
4850
4851    Although such sequences do exist, they are very expensive and are
4852    not something we'd want to use.  This is especially true of CCV2 and
4853    CCV4, where all the shuffling would greatly outweigh whatever benefit
4854    the vectorization itself provides.
4855
4856    The main benefit of having more than one condition-code register
4857    is to allow the pipelining of operations, especially those involving
4858    comparisons and conditional moves.  We don't really expect the
4859    registers to be live for long periods, and certainly never want
4860    them to be live across calls.
4861
4862    Also, there should be no penalty attached to using all the available
4863    registers.  They are simply bits in the same underlying FPU control
4864    register.
4865
4866    We therefore expose the hardware registers from the outset and use
4867    a simple round-robin allocation scheme.  */
4868
4869 static rtx
4870 mips_allocate_fcc (enum machine_mode mode)
4871 {
4872   unsigned int regno, count;
4873
4874   gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
4875
4876   if (mode == CCmode)
4877     count = 1;
4878   else if (mode == CCV2mode)
4879     count = 2;
4880   else if (mode == CCV4mode)
4881     count = 4;
4882   else
4883     gcc_unreachable ();
4884
4885   cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
4886   if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
4887     cfun->machine->next_fcc = 0;
4888   regno = ST_REG_FIRST + cfun->machine->next_fcc;
4889   cfun->machine->next_fcc += count;
4890   return gen_rtx_REG (mode, regno);
4891 }
4892
4893 /* Convert a comparison into something that can be used in a branch or
4894    conditional move.  On entry, *OP0 and *OP1 are the values being
4895    compared and *CODE is the code used to compare them.
4896
4897    Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
4898    If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
4899    otherwise any standard branch condition can be used.  The standard branch
4900    conditions are:
4901
4902       - EQ or NE between two registers.
4903       - any comparison between a register and zero.  */
4904
4905 static void
4906 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
4907 {
4908   rtx cmp_op0 = *op0;
4909   rtx cmp_op1 = *op1;
4910
4911   if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
4912     {
4913       if (!need_eq_ne_p && *op1 == const0_rtx)
4914         ;
4915       else if (*code == EQ || *code == NE)
4916         {
4917           if (need_eq_ne_p)
4918             {
4919               *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
4920               *op1 = const0_rtx;
4921             }
4922           else
4923             *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
4924         }
4925       else
4926         {
4927           /* The comparison needs a separate scc instruction.  Store the
4928              result of the scc in *OP0 and compare it against zero.  */
4929           bool invert = false;
4930           *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
4931           mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
4932           *code = (invert ? EQ : NE);
4933           *op1 = const0_rtx;
4934         }
4935     }
4936   else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
4937     {
4938       *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
4939       mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
4940       *code = NE;
4941       *op1 = const0_rtx;
4942     }
4943   else
4944     {
4945       enum rtx_code cmp_code;
4946
4947       /* Floating-point tests use a separate C.cond.fmt comparison to
4948          set a condition code register.  The branch or conditional move
4949          will then compare that register against zero.
4950
4951          Set CMP_CODE to the code of the comparison instruction and
4952          *CODE to the code that the branch or move should use.  */
4953       cmp_code = *code;
4954       *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
4955       *op0 = (ISA_HAS_8CC
4956               ? mips_allocate_fcc (CCmode)
4957               : gen_rtx_REG (CCmode, FPSW_REGNUM));
4958       *op1 = const0_rtx;
4959       mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
4960     }
4961 }
4962 \f
4963 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
4964    and OPERAND[3].  Store the result in OPERANDS[0].
4965
4966    On 64-bit targets, the mode of the comparison and target will always be
4967    SImode, thus possibly narrower than that of the comparison's operands.  */
4968
4969 void
4970 mips_expand_scc (rtx operands[])
4971 {
4972   rtx target = operands[0];
4973   enum rtx_code code = GET_CODE (operands[1]);
4974   rtx op0 = operands[2];
4975   rtx op1 = operands[3];
4976
4977   gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
4978
4979   if (code == EQ || code == NE)
4980     {
4981       if (ISA_HAS_SEQ_SNE
4982           && reg_imm10_operand (op1, GET_MODE (op1)))
4983         mips_emit_binary (code, target, op0, op1);
4984       else
4985         {
4986           rtx zie = mips_zero_if_equal (op0, op1);
4987           mips_emit_binary (code, target, zie, const0_rtx);
4988         }
4989     }
4990   else
4991     mips_emit_int_order_test (code, 0, target, op0, op1);
4992 }
4993
4994 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
4995    CODE and jump to OPERANDS[3] if the condition holds.  */
4996
4997 void
4998 mips_expand_conditional_branch (rtx *operands)
4999 {
5000   enum rtx_code code = GET_CODE (operands[0]);
5001   rtx op0 = operands[1];
5002   rtx op1 = operands[2];
5003   rtx condition;
5004
5005   mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
5006   condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
5007   emit_jump_insn (gen_condjump (condition, operands[3]));
5008 }
5009
5010 /* Implement:
5011
5012    (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
5013    (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS))  */
5014
5015 void
5016 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
5017                        enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
5018 {
5019   rtx cmp_result;
5020   bool reversed_p;
5021
5022   reversed_p = mips_reversed_fp_cond (&cond);
5023   cmp_result = mips_allocate_fcc (CCV2mode);
5024   emit_insn (gen_scc_ps (cmp_result,
5025                          gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
5026   if (reversed_p)
5027     emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
5028                                          cmp_result));
5029   else
5030     emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
5031                                          cmp_result));
5032 }
5033
5034 /* Perform the comparison in OPERANDS[1].  Move OPERANDS[2] into OPERANDS[0]
5035    if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0].  */
5036
5037 void
5038 mips_expand_conditional_move (rtx *operands)
5039 {
5040   rtx cond;
5041   enum rtx_code code = GET_CODE (operands[1]);
5042   rtx op0 = XEXP (operands[1], 0);
5043   rtx op1 = XEXP (operands[1], 1);
5044
5045   mips_emit_compare (&code, &op0, &op1, true);
5046   cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
5047   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
5048                           gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
5049                                                 operands[2], operands[3])));
5050 }
5051
5052 /* Perform the comparison in COMPARISON, then trap if the condition holds.  */
5053
5054 void
5055 mips_expand_conditional_trap (rtx comparison)
5056 {
5057   rtx op0, op1;
5058   enum machine_mode mode;
5059   enum rtx_code code;
5060
5061   /* MIPS conditional trap instructions don't have GT or LE flavors,
5062      so we must swap the operands and convert to LT and GE respectively.  */
5063   code = GET_CODE (comparison);
5064   switch (code)
5065     {
5066     case GT:
5067     case LE:
5068     case GTU:
5069     case LEU:
5070       code = swap_condition (code);
5071       op0 = XEXP (comparison, 1);
5072       op1 = XEXP (comparison, 0);
5073       break;
5074
5075     default:
5076       op0 = XEXP (comparison, 0);
5077       op1 = XEXP (comparison, 1);
5078       break;
5079     }
5080
5081   mode = GET_MODE (XEXP (comparison, 0));
5082   op0 = force_reg (mode, op0);
5083   if (!arith_operand (op1, mode))
5084     op1 = force_reg (mode, op1);
5085
5086   emit_insn (gen_rtx_TRAP_IF (VOIDmode,
5087                               gen_rtx_fmt_ee (code, mode, op0, op1),
5088                               const0_rtx));
5089 }
5090 \f
5091 /* Initialize *CUM for a call to a function of type FNTYPE.  */
5092
5093 void
5094 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
5095 {
5096   memset (cum, 0, sizeof (*cum));
5097   cum->prototype = (fntype && prototype_p (fntype));
5098   cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
5099 }
5100
5101 /* Fill INFO with information about a single argument.  CUM is the
5102    cumulative state for earlier arguments.  MODE is the mode of this
5103    argument and TYPE is its type (if known).  NAMED is true if this
5104    is a named (fixed) argument rather than a variable one.  */
5105
5106 static void
5107 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
5108                    enum machine_mode mode, const_tree type, bool named)
5109 {
5110   bool doubleword_aligned_p;
5111   unsigned int num_bytes, num_words, max_regs;
5112
5113   /* Work out the size of the argument.  */
5114   num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5115   num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5116
5117   /* Decide whether it should go in a floating-point register, assuming
5118      one is free.  Later code checks for availability.
5119
5120      The checks against UNITS_PER_FPVALUE handle the soft-float and
5121      single-float cases.  */
5122   switch (mips_abi)
5123     {
5124     case ABI_EABI:
5125       /* The EABI conventions have traditionally been defined in terms
5126          of TYPE_MODE, regardless of the actual type.  */
5127       info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
5128                       || mode == V2SFmode)
5129                      && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5130       break;
5131
5132     case ABI_32:
5133     case ABI_O64:
5134       /* Only leading floating-point scalars are passed in
5135          floating-point registers.  We also handle vector floats the same
5136          say, which is OK because they are not covered by the standard ABI.  */
5137       info->fpr_p = (!cum->gp_reg_found
5138                      && cum->arg_number < 2
5139                      && (type == 0
5140                          || SCALAR_FLOAT_TYPE_P (type)
5141                          || VECTOR_FLOAT_TYPE_P (type))
5142                      && (GET_MODE_CLASS (mode) == MODE_FLOAT
5143                          || mode == V2SFmode)
5144                      && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5145       break;
5146
5147     case ABI_N32:
5148     case ABI_64:
5149       /* Scalar, complex and vector floating-point types are passed in
5150          floating-point registers, as long as this is a named rather
5151          than a variable argument.  */
5152       info->fpr_p = (named
5153                      && (type == 0 || FLOAT_TYPE_P (type))
5154                      && (GET_MODE_CLASS (mode) == MODE_FLOAT
5155                          || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5156                          || mode == V2SFmode)
5157                      && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
5158
5159       /* ??? According to the ABI documentation, the real and imaginary
5160          parts of complex floats should be passed in individual registers.
5161          The real and imaginary parts of stack arguments are supposed
5162          to be contiguous and there should be an extra word of padding
5163          at the end.
5164
5165          This has two problems.  First, it makes it impossible to use a
5166          single "void *" va_list type, since register and stack arguments
5167          are passed differently.  (At the time of writing, MIPSpro cannot
5168          handle complex float varargs correctly.)  Second, it's unclear
5169          what should happen when there is only one register free.
5170
5171          For now, we assume that named complex floats should go into FPRs
5172          if there are two FPRs free, otherwise they should be passed in the
5173          same way as a struct containing two floats.  */
5174       if (info->fpr_p
5175           && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5176           && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
5177         {
5178           if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
5179             info->fpr_p = false;
5180           else
5181             num_words = 2;
5182         }
5183       break;
5184
5185     default:
5186       gcc_unreachable ();
5187     }
5188
5189   /* See whether the argument has doubleword alignment.  */
5190   doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
5191                           > BITS_PER_WORD);
5192
5193   /* Set REG_OFFSET to the register count we're interested in.
5194      The EABI allocates the floating-point registers separately,
5195      but the other ABIs allocate them like integer registers.  */
5196   info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
5197                       ? cum->num_fprs
5198                       : cum->num_gprs);
5199
5200   /* Advance to an even register if the argument is doubleword-aligned.  */
5201   if (doubleword_aligned_p)
5202     info->reg_offset += info->reg_offset & 1;
5203
5204   /* Work out the offset of a stack argument.  */
5205   info->stack_offset = cum->stack_words;
5206   if (doubleword_aligned_p)
5207     info->stack_offset += info->stack_offset & 1;
5208
5209   max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
5210
5211   /* Partition the argument between registers and stack.  */
5212   info->reg_words = MIN (num_words, max_regs);
5213   info->stack_words = num_words - info->reg_words;
5214 }
5215
5216 /* INFO describes a register argument that has the normal format for the
5217    argument's mode.  Return the register it uses, assuming that FPRs are
5218    available if HARD_FLOAT_P.  */
5219
5220 static unsigned int
5221 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
5222 {
5223   if (!info->fpr_p || !hard_float_p)
5224     return GP_ARG_FIRST + info->reg_offset;
5225   else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
5226     /* In o32, the second argument is always passed in $f14
5227        for TARGET_DOUBLE_FLOAT, regardless of whether the
5228        first argument was a word or doubleword.  */
5229     return FP_ARG_FIRST + 2;
5230   else
5231     return FP_ARG_FIRST + info->reg_offset;
5232 }
5233
5234 /* Implement TARGET_STRICT_ARGUMENT_NAMING.  */
5235
5236 static bool
5237 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
5238 {
5239   return !TARGET_OLDABI;
5240 }
5241
5242 /* Implement TARGET_FUNCTION_ARG.  */
5243
5244 static rtx
5245 mips_function_arg (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   /* We will be called with a mode of VOIDmode after the last argument
5252      has been seen.  Whatever we return will be passed to the call expander.
5253      If we need a MIPS16 fp_code, return a REG with the code stored as
5254      the mode.  */
5255   if (mode == VOIDmode)
5256     {
5257       if (TARGET_MIPS16 && cum->fp_code != 0)
5258         return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
5259       else
5260         return NULL;
5261     }
5262
5263   mips_get_arg_info (&info, cum, mode, type, named);
5264
5265   /* Return straight away if the whole argument is passed on the stack.  */
5266   if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
5267     return NULL;
5268
5269   /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
5270      contains a double in its entirety, then that 64-bit chunk is passed
5271      in a floating-point register.  */
5272   if (TARGET_NEWABI
5273       && TARGET_HARD_FLOAT
5274       && named
5275       && type != 0
5276       && TREE_CODE (type) == RECORD_TYPE
5277       && TYPE_SIZE_UNIT (type)
5278       && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
5279     {
5280       tree field;
5281
5282       /* First check to see if there is any such field.  */
5283       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5284         if (TREE_CODE (field) == FIELD_DECL
5285             && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5286             && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
5287             && tree_fits_shwi_p (bit_position (field))
5288             && int_bit_position (field) % BITS_PER_WORD == 0)
5289           break;
5290
5291       if (field != 0)
5292         {
5293           /* Now handle the special case by returning a PARALLEL
5294              indicating where each 64-bit chunk goes.  INFO.REG_WORDS
5295              chunks are passed in registers.  */
5296           unsigned int i;
5297           HOST_WIDE_INT bitpos;
5298           rtx ret;
5299
5300           /* assign_parms checks the mode of ENTRY_PARM, so we must
5301              use the actual mode here.  */
5302           ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
5303
5304           bitpos = 0;
5305           field = TYPE_FIELDS (type);
5306           for (i = 0; i < info.reg_words; i++)
5307             {
5308               rtx reg;
5309
5310               for (; field; field = DECL_CHAIN (field))
5311                 if (TREE_CODE (field) == FIELD_DECL
5312                     && int_bit_position (field) >= bitpos)
5313                   break;
5314
5315               if (field
5316                   && int_bit_position (field) == bitpos
5317                   && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5318                   && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
5319                 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
5320               else
5321                 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
5322
5323               XVECEXP (ret, 0, i)
5324                 = gen_rtx_EXPR_LIST (VOIDmode, reg,
5325                                      GEN_INT (bitpos / BITS_PER_UNIT));
5326
5327               bitpos += BITS_PER_WORD;
5328             }
5329           return ret;
5330         }
5331     }
5332
5333   /* Handle the n32/n64 conventions for passing complex floating-point
5334      arguments in FPR pairs.  The real part goes in the lower register
5335      and the imaginary part goes in the upper register.  */
5336   if (TARGET_NEWABI
5337       && info.fpr_p
5338       && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5339     {
5340       rtx real, imag;
5341       enum machine_mode inner;
5342       unsigned int regno;
5343
5344       inner = GET_MODE_INNER (mode);
5345       regno = FP_ARG_FIRST + info.reg_offset;
5346       if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
5347         {
5348           /* Real part in registers, imaginary part on stack.  */
5349           gcc_assert (info.stack_words == info.reg_words);
5350           return gen_rtx_REG (inner, regno);
5351         }
5352       else
5353         {
5354           gcc_assert (info.stack_words == 0);
5355           real = gen_rtx_EXPR_LIST (VOIDmode,
5356                                     gen_rtx_REG (inner, regno),
5357                                     const0_rtx);
5358           imag = gen_rtx_EXPR_LIST (VOIDmode,
5359                                     gen_rtx_REG (inner,
5360                                                  regno + info.reg_words / 2),
5361                                     GEN_INT (GET_MODE_SIZE (inner)));
5362           return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
5363         }
5364     }
5365
5366   return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
5367 }
5368
5369 /* Implement TARGET_FUNCTION_ARG_ADVANCE.  */
5370
5371 static void
5372 mips_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
5373                            const_tree type, bool named)
5374 {
5375   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5376   struct mips_arg_info info;
5377
5378   mips_get_arg_info (&info, cum, mode, type, named);
5379
5380   if (!info.fpr_p)
5381     cum->gp_reg_found = true;
5382
5383   /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
5384      an explanation of what this code does.  It assumes that we're using
5385      either the o32 or the o64 ABI, both of which pass at most 2 arguments
5386      in FPRs.  */
5387   if (cum->arg_number < 2 && info.fpr_p)
5388     cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
5389
5390   /* Advance the register count.  This has the effect of setting
5391      num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
5392      argument required us to skip the final GPR and pass the whole
5393      argument on the stack.  */
5394   if (mips_abi != ABI_EABI || !info.fpr_p)
5395     cum->num_gprs = info.reg_offset + info.reg_words;
5396   else if (info.reg_words > 0)
5397     cum->num_fprs += MAX_FPRS_PER_FMT;
5398
5399   /* Advance the stack word count.  */
5400   if (info.stack_words > 0)
5401     cum->stack_words = info.stack_offset + info.stack_words;
5402
5403   cum->arg_number++;
5404 }
5405
5406 /* Implement TARGET_ARG_PARTIAL_BYTES.  */
5407
5408 static int
5409 mips_arg_partial_bytes (cumulative_args_t cum,
5410                         enum machine_mode mode, tree type, bool named)
5411 {
5412   struct mips_arg_info info;
5413
5414   mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
5415   return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
5416 }
5417
5418 /* Implement TARGET_FUNCTION_ARG_BOUNDARY.  Every parameter gets at
5419    least PARM_BOUNDARY bits of alignment, but will be given anything up
5420    to STACK_BOUNDARY bits if the type requires it.  */
5421
5422 static unsigned int
5423 mips_function_arg_boundary (enum machine_mode mode, const_tree type)
5424 {
5425   unsigned int alignment;
5426
5427   alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5428   if (alignment < PARM_BOUNDARY)
5429     alignment = PARM_BOUNDARY;
5430   if (alignment > STACK_BOUNDARY)
5431     alignment = STACK_BOUNDARY;
5432   return alignment;
5433 }
5434
5435 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5436    upward rather than downward.  In other words, return true if the
5437    first byte of the stack slot has useful data, false if the last
5438    byte does.  */
5439
5440 bool
5441 mips_pad_arg_upward (enum machine_mode mode, const_tree type)
5442 {
5443   /* On little-endian targets, the first byte of every stack argument
5444      is passed in the first byte of the stack slot.  */
5445   if (!BYTES_BIG_ENDIAN)
5446     return true;
5447
5448   /* Otherwise, integral types are padded downward: the last byte of a
5449      stack argument is passed in the last byte of the stack slot.  */
5450   if (type != 0
5451       ? (INTEGRAL_TYPE_P (type)
5452          || POINTER_TYPE_P (type)
5453          || FIXED_POINT_TYPE_P (type))
5454       : (SCALAR_INT_MODE_P (mode)
5455          || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5456     return false;
5457
5458   /* Big-endian o64 pads floating-point arguments downward.  */
5459   if (mips_abi == ABI_O64)
5460     if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5461       return false;
5462
5463   /* Other types are padded upward for o32, o64, n32 and n64.  */
5464   if (mips_abi != ABI_EABI)
5465     return true;
5466
5467   /* Arguments smaller than a stack slot are padded downward.  */
5468   if (mode != BLKmode)
5469     return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5470   else
5471     return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5472 }
5473
5474 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...).  Return !BYTES_BIG_ENDIAN
5475    if the least significant byte of the register has useful data.  Return
5476    the opposite if the most significant byte does.  */
5477
5478 bool
5479 mips_pad_reg_upward (enum machine_mode mode, tree type)
5480 {
5481   /* No shifting is required for floating-point arguments.  */
5482   if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5483     return !BYTES_BIG_ENDIAN;
5484
5485   /* Otherwise, apply the same padding to register arguments as we do
5486      to stack arguments.  */
5487   return mips_pad_arg_upward (mode, type);
5488 }
5489
5490 /* Return nonzero when an argument must be passed by reference.  */
5491
5492 static bool
5493 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5494                         enum machine_mode mode, const_tree type,
5495                         bool named ATTRIBUTE_UNUSED)
5496 {
5497   if (mips_abi == ABI_EABI)
5498     {
5499       int size;
5500
5501       /* ??? How should SCmode be handled?  */
5502       if (mode == DImode || mode == DFmode
5503           || mode == DQmode || mode == UDQmode
5504           || mode == DAmode || mode == UDAmode)
5505         return 0;
5506
5507       size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5508       return size == -1 || size > UNITS_PER_WORD;
5509     }
5510   else
5511     {
5512       /* If we have a variable-sized parameter, we have no choice.  */
5513       return targetm.calls.must_pass_in_stack (mode, type);
5514     }
5515 }
5516
5517 /* Implement TARGET_CALLEE_COPIES.  */
5518
5519 static bool
5520 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
5521                     enum machine_mode mode ATTRIBUTE_UNUSED,
5522                     const_tree type ATTRIBUTE_UNUSED, bool named)
5523 {
5524   return mips_abi == ABI_EABI && named;
5525 }
5526 \f
5527 /* See whether VALTYPE is a record whose fields should be returned in
5528    floating-point registers.  If so, return the number of fields and
5529    list them in FIELDS (which should have two elements).  Return 0
5530    otherwise.
5531
5532    For n32 & n64, a structure with one or two fields is returned in
5533    floating-point registers as long as every field has a floating-point
5534    type.  */
5535
5536 static int
5537 mips_fpr_return_fields (const_tree valtype, tree *fields)
5538 {
5539   tree field;
5540   int i;
5541
5542   if (!TARGET_NEWABI)
5543     return 0;
5544
5545   if (TREE_CODE (valtype) != RECORD_TYPE)
5546     return 0;
5547
5548   i = 0;
5549   for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5550     {
5551       if (TREE_CODE (field) != FIELD_DECL)
5552         continue;
5553
5554       if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5555         return 0;
5556
5557       if (i == 2)
5558         return 0;
5559
5560       fields[i++] = field;
5561     }
5562   return i;
5563 }
5564
5565 /* Implement TARGET_RETURN_IN_MSB.  For n32 & n64, we should return
5566    a value in the most significant part of $2/$3 if:
5567
5568       - the target is big-endian;
5569
5570       - the value has a structure or union type (we generalize this to
5571         cover aggregates from other languages too); and
5572
5573       - the structure is not returned in floating-point registers.  */
5574
5575 static bool
5576 mips_return_in_msb (const_tree valtype)
5577 {
5578   tree fields[2];
5579
5580   return (TARGET_NEWABI
5581           && TARGET_BIG_ENDIAN
5582           && AGGREGATE_TYPE_P (valtype)
5583           && mips_fpr_return_fields (valtype, fields) == 0);
5584 }
5585
5586 /* Return true if the function return value MODE will get returned in a
5587    floating-point register.  */
5588
5589 static bool
5590 mips_return_mode_in_fpr_p (enum machine_mode mode)
5591 {
5592   return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5593            || mode == V2SFmode
5594            || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5595           && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5596 }
5597
5598 /* Return the representation of an FPR return register when the
5599    value being returned in FP_RETURN has mode VALUE_MODE and the
5600    return type itself has mode TYPE_MODE.  On NewABI targets,
5601    the two modes may be different for structures like:
5602
5603        struct __attribute__((packed)) foo { float f; }
5604
5605    where we return the SFmode value of "f" in FP_RETURN, but where
5606    the structure itself has mode BLKmode.  */
5607
5608 static rtx
5609 mips_return_fpr_single (enum machine_mode type_mode,
5610                         enum machine_mode value_mode)
5611 {
5612   rtx x;
5613
5614   x = gen_rtx_REG (value_mode, FP_RETURN);
5615   if (type_mode != value_mode)
5616     {
5617       x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5618       x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5619     }
5620   return x;
5621 }
5622
5623 /* Return a composite value in a pair of floating-point registers.
5624    MODE1 and OFFSET1 are the mode and byte offset for the first value,
5625    likewise MODE2 and OFFSET2 for the second.  MODE is the mode of the
5626    complete value.
5627
5628    For n32 & n64, $f0 always holds the first value and $f2 the second.
5629    Otherwise the values are packed together as closely as possible.  */
5630
5631 static rtx
5632 mips_return_fpr_pair (enum machine_mode mode,
5633                       enum machine_mode mode1, HOST_WIDE_INT offset1,
5634                       enum machine_mode mode2, HOST_WIDE_INT offset2)
5635 {
5636   int inc;
5637
5638   inc = (TARGET_NEWABI ? 2 : MAX_FPRS_PER_FMT);
5639   return gen_rtx_PARALLEL
5640     (mode,
5641      gen_rtvec (2,
5642                 gen_rtx_EXPR_LIST (VOIDmode,
5643                                    gen_rtx_REG (mode1, FP_RETURN),
5644                                    GEN_INT (offset1)),
5645                 gen_rtx_EXPR_LIST (VOIDmode,
5646                                    gen_rtx_REG (mode2, FP_RETURN + inc),
5647                                    GEN_INT (offset2))));
5648
5649 }
5650
5651 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
5652    For normal calls, VALTYPE is the return type and MODE is VOIDmode.
5653    For libcalls, VALTYPE is null and MODE is the mode of the return value.  */
5654
5655 static rtx
5656 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
5657                        enum machine_mode mode)
5658 {
5659   if (valtype)
5660     {
5661       tree fields[2];
5662       int unsigned_p;
5663       const_tree func;
5664
5665       if (fn_decl_or_type && DECL_P (fn_decl_or_type))
5666         func = fn_decl_or_type;
5667       else
5668         func = NULL;
5669
5670       mode = TYPE_MODE (valtype);
5671       unsigned_p = TYPE_UNSIGNED (valtype);
5672
5673       /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5674          return values, promote the mode here too.  */
5675       mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5676
5677       /* Handle structures whose fields are returned in $f0/$f2.  */
5678       switch (mips_fpr_return_fields (valtype, fields))
5679         {
5680         case 1:
5681           return mips_return_fpr_single (mode,
5682                                          TYPE_MODE (TREE_TYPE (fields[0])));
5683
5684         case 2:
5685           return mips_return_fpr_pair (mode,
5686                                        TYPE_MODE (TREE_TYPE (fields[0])),
5687                                        int_byte_position (fields[0]),
5688                                        TYPE_MODE (TREE_TYPE (fields[1])),
5689                                        int_byte_position (fields[1]));
5690         }
5691
5692       /* If a value is passed in the most significant part of a register, see
5693          whether we have to round the mode up to a whole number of words.  */
5694       if (mips_return_in_msb (valtype))
5695         {
5696           HOST_WIDE_INT size = int_size_in_bytes (valtype);
5697           if (size % UNITS_PER_WORD != 0)
5698             {
5699               size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5700               mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5701             }
5702         }
5703
5704       /* For EABI, the class of return register depends entirely on MODE.
5705          For example, "struct { some_type x; }" and "union { some_type x; }"
5706          are returned in the same way as a bare "some_type" would be.
5707          Other ABIs only use FPRs for scalar, complex or vector types.  */
5708       if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5709         return gen_rtx_REG (mode, GP_RETURN);
5710     }
5711
5712   if (!TARGET_MIPS16)
5713     {
5714       /* Handle long doubles for n32 & n64.  */
5715       if (mode == TFmode)
5716         return mips_return_fpr_pair (mode,
5717                                      DImode, 0,
5718                                      DImode, GET_MODE_SIZE (mode) / 2);
5719
5720       if (mips_return_mode_in_fpr_p (mode))
5721         {
5722           if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5723             return mips_return_fpr_pair (mode,
5724                                          GET_MODE_INNER (mode), 0,
5725                                          GET_MODE_INNER (mode),
5726                                          GET_MODE_SIZE (mode) / 2);
5727           else
5728             return gen_rtx_REG (mode, FP_RETURN);
5729         }
5730     }
5731
5732   return gen_rtx_REG (mode, GP_RETURN);
5733 }
5734
5735 /* Implement TARGET_FUNCTION_VALUE.  */
5736
5737 static rtx
5738 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
5739                      bool outgoing ATTRIBUTE_UNUSED)
5740 {
5741   return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5742 }
5743
5744 /* Implement TARGET_LIBCALL_VALUE.  */
5745
5746 static rtx
5747 mips_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5748 {
5749   return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
5750 }
5751
5752 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5753
5754    On the MIPS, R2 R3 and F0 F2 are the only register thus used.
5755    Currently, R2 and F0 are only implemented here (C has no complex type).  */
5756
5757 static bool
5758 mips_function_value_regno_p (const unsigned int regno)
5759 {
5760   if (regno == GP_RETURN
5761       || regno == FP_RETURN
5762       || (LONG_DOUBLE_TYPE_SIZE == 128
5763           && FP_RETURN != GP_RETURN
5764           && regno == FP_RETURN + 2))
5765     return true;
5766
5767   return false;
5768 }
5769
5770 /* Implement TARGET_RETURN_IN_MEMORY.  Under the o32 and o64 ABIs,
5771    all BLKmode objects are returned in memory.  Under the n32, n64
5772    and embedded ABIs, small structures are returned in a register.
5773    Objects with varying size must still be returned in memory, of
5774    course.  */
5775
5776 static bool
5777 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5778 {
5779   return (TARGET_OLDABI
5780           ? TYPE_MODE (type) == BLKmode
5781           : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5782 }
5783 \f
5784 /* Implement TARGET_SETUP_INCOMING_VARARGS.  */
5785
5786 static void
5787 mips_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
5788                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
5789                              int no_rtl)
5790 {
5791   CUMULATIVE_ARGS local_cum;
5792   int gp_saved, fp_saved;
5793
5794   /* The caller has advanced CUM up to, but not beyond, the last named
5795      argument.  Advance a local copy of CUM past the last "real" named
5796      argument, to find out how many registers are left over.  */
5797   local_cum = *get_cumulative_args (cum);
5798   mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
5799                              true);
5800
5801   /* Found out how many registers we need to save.  */
5802   gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
5803   fp_saved = (EABI_FLOAT_VARARGS_P
5804               ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
5805               : 0);
5806
5807   if (!no_rtl)
5808     {
5809       if (gp_saved > 0)
5810         {
5811           rtx ptr, mem;
5812
5813           ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
5814                                REG_PARM_STACK_SPACE (cfun->decl)
5815                                - gp_saved * UNITS_PER_WORD);
5816           mem = gen_frame_mem (BLKmode, ptr);
5817           set_mem_alias_set (mem, get_varargs_alias_set ());
5818
5819           move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
5820                                mem, gp_saved);
5821         }
5822       if (fp_saved > 0)
5823         {
5824           /* We can't use move_block_from_reg, because it will use
5825              the wrong mode.  */
5826           enum machine_mode mode;
5827           int off, i;
5828
5829           /* Set OFF to the offset from virtual_incoming_args_rtx of
5830              the first float register.  The FP save area lies below
5831              the integer one, and is aligned to UNITS_PER_FPVALUE bytes.  */
5832           off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
5833           off -= fp_saved * UNITS_PER_FPREG;
5834
5835           mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
5836
5837           for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
5838                i += MAX_FPRS_PER_FMT)
5839             {
5840               rtx ptr, mem;
5841
5842               ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
5843               mem = gen_frame_mem (mode, ptr);
5844               set_mem_alias_set (mem, get_varargs_alias_set ());
5845               mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
5846               off += UNITS_PER_HWFPVALUE;
5847             }
5848         }
5849     }
5850   if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
5851     cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
5852                                    + fp_saved * UNITS_PER_FPREG);
5853 }
5854
5855 /* Implement TARGET_BUILTIN_VA_LIST.  */
5856
5857 static tree
5858 mips_build_builtin_va_list (void)
5859 {
5860   if (EABI_FLOAT_VARARGS_P)
5861     {
5862       /* We keep 3 pointers, and two offsets.
5863
5864          Two pointers are to the overflow area, which starts at the CFA.
5865          One of these is constant, for addressing into the GPR save area
5866          below it.  The other is advanced up the stack through the
5867          overflow region.
5868
5869          The third pointer is to the bottom of the GPR save area.
5870          Since the FPR save area is just below it, we can address
5871          FPR slots off this pointer.
5872
5873          We also keep two one-byte offsets, which are to be subtracted
5874          from the constant pointers to yield addresses in the GPR and
5875          FPR save areas.  These are downcounted as float or non-float
5876          arguments are used, and when they get to zero, the argument
5877          must be obtained from the overflow region.  */
5878       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
5879       tree array, index;
5880
5881       record = lang_hooks.types.make_type (RECORD_TYPE);
5882
5883       f_ovfl = build_decl (BUILTINS_LOCATION,
5884                            FIELD_DECL, get_identifier ("__overflow_argptr"),
5885                            ptr_type_node);
5886       f_gtop = build_decl (BUILTINS_LOCATION,
5887                            FIELD_DECL, get_identifier ("__gpr_top"),
5888                            ptr_type_node);
5889       f_ftop = build_decl (BUILTINS_LOCATION,
5890                            FIELD_DECL, get_identifier ("__fpr_top"),
5891                            ptr_type_node);
5892       f_goff = build_decl (BUILTINS_LOCATION,
5893                            FIELD_DECL, get_identifier ("__gpr_offset"),
5894                            unsigned_char_type_node);
5895       f_foff = build_decl (BUILTINS_LOCATION,
5896                            FIELD_DECL, get_identifier ("__fpr_offset"),
5897                            unsigned_char_type_node);
5898       /* Explicitly pad to the size of a pointer, so that -Wpadded won't
5899          warn on every user file.  */
5900       index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
5901       array = build_array_type (unsigned_char_type_node,
5902                                 build_index_type (index));
5903       f_res = build_decl (BUILTINS_LOCATION,
5904                           FIELD_DECL, get_identifier ("__reserved"), array);
5905
5906       DECL_FIELD_CONTEXT (f_ovfl) = record;
5907       DECL_FIELD_CONTEXT (f_gtop) = record;
5908       DECL_FIELD_CONTEXT (f_ftop) = record;
5909       DECL_FIELD_CONTEXT (f_goff) = record;
5910       DECL_FIELD_CONTEXT (f_foff) = record;
5911       DECL_FIELD_CONTEXT (f_res) = record;
5912
5913       TYPE_FIELDS (record) = f_ovfl;
5914       DECL_CHAIN (f_ovfl) = f_gtop;
5915       DECL_CHAIN (f_gtop) = f_ftop;
5916       DECL_CHAIN (f_ftop) = f_goff;
5917       DECL_CHAIN (f_goff) = f_foff;
5918       DECL_CHAIN (f_foff) = f_res;
5919
5920       layout_type (record);
5921       return record;
5922     }
5923   else
5924     /* Otherwise, we use 'void *'.  */
5925     return ptr_type_node;
5926 }
5927
5928 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
5929
5930 static void
5931 mips_va_start (tree valist, rtx nextarg)
5932 {
5933   if (EABI_FLOAT_VARARGS_P)
5934     {
5935       const CUMULATIVE_ARGS *cum;
5936       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5937       tree ovfl, gtop, ftop, goff, foff;
5938       tree t;
5939       int gpr_save_area_size;
5940       int fpr_save_area_size;
5941       int fpr_offset;
5942
5943       cum = &crtl->args.info;
5944       gpr_save_area_size
5945         = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5946       fpr_save_area_size
5947         = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5948
5949       f_ovfl = TYPE_FIELDS (va_list_type_node);
5950       f_gtop = DECL_CHAIN (f_ovfl);
5951       f_ftop = DECL_CHAIN (f_gtop);
5952       f_goff = DECL_CHAIN (f_ftop);
5953       f_foff = DECL_CHAIN (f_goff);
5954
5955       ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5956                      NULL_TREE);
5957       gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
5958                      NULL_TREE);
5959       ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
5960                      NULL_TREE);
5961       goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
5962                      NULL_TREE);
5963       foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
5964                      NULL_TREE);
5965
5966       /* Emit code to initialize OVFL, which points to the next varargs
5967          stack argument.  CUM->STACK_WORDS gives the number of stack
5968          words used by named arguments.  */
5969       t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5970       if (cum->stack_words > 0)
5971         t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
5972       t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5973       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5974
5975       /* Emit code to initialize GTOP, the top of the GPR save area.  */
5976       t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5977       t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5978       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5979
5980       /* Emit code to initialize FTOP, the top of the FPR save area.
5981          This address is gpr_save_area_bytes below GTOP, rounded
5982          down to the next fp-aligned boundary.  */
5983       t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5984       fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5985       fpr_offset &= -UNITS_PER_FPVALUE;
5986       if (fpr_offset)
5987         t = fold_build_pointer_plus_hwi (t, -fpr_offset);
5988       t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5989       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5990
5991       /* Emit code to initialize GOFF, the offset from GTOP of the
5992          next GPR argument.  */
5993       t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
5994                   build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
5995       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5996
5997       /* Likewise emit code to initialize FOFF, the offset from FTOP
5998          of the next FPR argument.  */
5999       t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
6000                   build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
6001       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6002     }
6003   else
6004     {
6005       nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
6006       std_expand_builtin_va_start (valist, nextarg);
6007     }
6008 }
6009
6010 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
6011    types as well.  */
6012
6013 static tree
6014 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6015                                gimple_seq *post_p)
6016 {
6017   tree addr, t, type_size, rounded_size, valist_tmp;
6018   unsigned HOST_WIDE_INT align, boundary;
6019   bool indirect;
6020
6021   indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
6022   if (indirect)
6023     type = build_pointer_type (type);
6024
6025   align = PARM_BOUNDARY / BITS_PER_UNIT;
6026   boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
6027
6028   /* When we align parameter on stack for caller, if the parameter
6029      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
6030      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
6031      here with caller.  */
6032   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
6033     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
6034
6035   boundary /= BITS_PER_UNIT;
6036
6037   /* Hoist the valist value into a temporary for the moment.  */
6038   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
6039
6040   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
6041      requires greater alignment, we must perform dynamic alignment.  */
6042   if (boundary > align)
6043     {
6044       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6045                   fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
6046       gimplify_and_add (t, pre_p);
6047
6048       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6049                   fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
6050                                valist_tmp,
6051                                build_int_cst (TREE_TYPE (valist), -boundary)));
6052       gimplify_and_add (t, pre_p);
6053     }
6054   else
6055     boundary = align;
6056
6057   /* If the actual alignment is less than the alignment of the type,
6058      adjust the type accordingly so that we don't assume strict alignment
6059      when dereferencing the pointer.  */
6060   boundary *= BITS_PER_UNIT;
6061   if (boundary < TYPE_ALIGN (type))
6062     {
6063       type = build_variant_type_copy (type);
6064       TYPE_ALIGN (type) = boundary;
6065     }
6066
6067   /* Compute the rounded size of the type.  */
6068   type_size = size_in_bytes (type);
6069   rounded_size = round_up (type_size, align);
6070
6071   /* Reduce rounded_size so it's sharable with the postqueue.  */
6072   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
6073
6074   /* Get AP.  */
6075   addr = valist_tmp;
6076   if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
6077     {
6078       /* Small args are padded downward.  */
6079       t = fold_build2_loc (input_location, GT_EXPR, sizetype,
6080                        rounded_size, size_int (align));
6081       t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
6082                        size_binop (MINUS_EXPR, rounded_size, type_size));
6083       addr = fold_build_pointer_plus (addr, t);
6084     }
6085
6086   /* Compute new value for AP.  */
6087   t = fold_build_pointer_plus (valist_tmp, rounded_size);
6088   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
6089   gimplify_and_add (t, pre_p);
6090
6091   addr = fold_convert (build_pointer_type (type), addr);
6092
6093   if (indirect)
6094     addr = build_va_arg_indirect_ref (addr);
6095
6096   return build_va_arg_indirect_ref (addr);
6097 }
6098
6099 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
6100
6101 static tree
6102 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6103                            gimple_seq *post_p)
6104 {
6105   tree addr;
6106   bool indirect_p;
6107
6108   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
6109   if (indirect_p)
6110     type = build_pointer_type (type);
6111
6112   if (!EABI_FLOAT_VARARGS_P)
6113     addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
6114   else
6115     {
6116       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
6117       tree ovfl, top, off, align;
6118       HOST_WIDE_INT size, rsize, osize;
6119       tree t, u;
6120
6121       f_ovfl = TYPE_FIELDS (va_list_type_node);
6122       f_gtop = DECL_CHAIN (f_ovfl);
6123       f_ftop = DECL_CHAIN (f_gtop);
6124       f_goff = DECL_CHAIN (f_ftop);
6125       f_foff = DECL_CHAIN (f_goff);
6126
6127       /* Let:
6128
6129          TOP be the top of the GPR or FPR save area;
6130          OFF be the offset from TOP of the next register;
6131          ADDR_RTX be the address of the argument;
6132          SIZE be the number of bytes in the argument type;
6133          RSIZE be the number of bytes used to store the argument
6134            when it's in the register save area; and
6135          OSIZE be the number of bytes used to store it when it's
6136            in the stack overflow area.
6137
6138          The code we want is:
6139
6140          1: off &= -rsize;        // round down
6141          2: if (off != 0)
6142          3:   {
6143          4:     addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
6144          5:     off -= rsize;
6145          6:   }
6146          7: else
6147          8:   {
6148          9:     ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
6149          10:    addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
6150          11:    ovfl += osize;
6151          14:  }
6152
6153          [1] and [9] can sometimes be optimized away.  */
6154
6155       ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6156                      NULL_TREE);
6157       size = int_size_in_bytes (type);
6158
6159       if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
6160           && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
6161         {
6162           top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
6163                         unshare_expr (valist), f_ftop, NULL_TREE);
6164           off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
6165                         unshare_expr (valist), f_foff, NULL_TREE);
6166
6167           /* When va_start saves FPR arguments to the stack, each slot
6168              takes up UNITS_PER_HWFPVALUE bytes, regardless of the
6169              argument's precision.  */
6170           rsize = UNITS_PER_HWFPVALUE;
6171
6172           /* Overflow arguments are padded to UNITS_PER_WORD bytes
6173              (= PARM_BOUNDARY bits).  This can be different from RSIZE
6174              in two cases:
6175
6176              (1) On 32-bit targets when TYPE is a structure such as:
6177
6178              struct s { float f; };
6179
6180              Such structures are passed in paired FPRs, so RSIZE
6181              will be 8 bytes.  However, the structure only takes
6182              up 4 bytes of memory, so OSIZE will only be 4.
6183
6184              (2) In combinations such as -mgp64 -msingle-float
6185              -fshort-double.  Doubles passed in registers will then take
6186              up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
6187              stack take up UNITS_PER_WORD bytes.  */
6188           osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
6189         }
6190       else
6191         {
6192           top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
6193                         unshare_expr (valist), f_gtop, NULL_TREE);
6194           off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
6195                         unshare_expr (valist), f_goff, NULL_TREE);
6196           rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6197           if (rsize > UNITS_PER_WORD)
6198             {
6199               /* [1] Emit code for: off &= -rsize.      */
6200               t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
6201                           build_int_cst (TREE_TYPE (off), -rsize));
6202               gimplify_assign (unshare_expr (off), t, pre_p);
6203             }
6204           osize = rsize;
6205         }
6206
6207       /* [2] Emit code to branch if off == 0.  */
6208       t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
6209                   build_int_cst (TREE_TYPE (off), 0));
6210       addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
6211
6212       /* [5] Emit code for: off -= rsize.  We do this as a form of
6213          post-decrement not available to C.  */
6214       t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
6215       t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
6216
6217       /* [4] Emit code for:
6218          addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0).  */
6219       t = fold_convert (sizetype, t);
6220       t = fold_build1 (NEGATE_EXPR, sizetype, t);
6221       t = fold_build_pointer_plus (top, t);
6222       if (BYTES_BIG_ENDIAN && rsize > size)
6223         t = fold_build_pointer_plus_hwi (t, rsize - size);
6224       COND_EXPR_THEN (addr) = t;
6225
6226       if (osize > UNITS_PER_WORD)
6227         {
6228           /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize.  */
6229           t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
6230           u = build_int_cst (TREE_TYPE (t), -osize);
6231           t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
6232           align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
6233                           unshare_expr (ovfl), t);
6234         }
6235       else
6236         align = NULL;
6237
6238       /* [10, 11] Emit code for:
6239          addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
6240          ovfl += osize.  */
6241       u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
6242       t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
6243       if (BYTES_BIG_ENDIAN && osize > size)
6244         t = fold_build_pointer_plus_hwi (t, osize - size);
6245
6246       /* String [9] and [10, 11] together.  */
6247       if (align)
6248         t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
6249       COND_EXPR_ELSE (addr) = t;
6250
6251       addr = fold_convert (build_pointer_type (type), addr);
6252       addr = build_va_arg_indirect_ref (addr);
6253     }
6254
6255   if (indirect_p)
6256     addr = build_va_arg_indirect_ref (addr);
6257
6258   return addr;
6259 }
6260 \f
6261 /* Declare a unique, locally-binding function called NAME, then start
6262    its definition.  */
6263
6264 static void
6265 mips_start_unique_function (const char *name)
6266 {
6267   tree decl;
6268
6269   decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
6270                      get_identifier (name),
6271                      build_function_type_list (void_type_node, NULL_TREE));
6272   DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
6273                                    NULL_TREE, void_type_node);
6274   TREE_PUBLIC (decl) = 1;
6275   TREE_STATIC (decl) = 1;
6276
6277   cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
6278
6279   targetm.asm_out.unique_section (decl, 0);
6280   switch_to_section (get_named_section (decl, NULL, 0));
6281
6282   targetm.asm_out.globalize_label (asm_out_file, name);
6283   fputs ("\t.hidden\t", asm_out_file);
6284   assemble_name (asm_out_file, name);
6285   putc ('\n', asm_out_file);
6286 }
6287
6288 /* Start a definition of function NAME.  MIPS16_P indicates whether the
6289    function contains MIPS16 code.  */
6290
6291 static void
6292 mips_start_function_definition (const char *name, bool mips16_p)
6293 {
6294   if (mips16_p)
6295     fprintf (asm_out_file, "\t.set\tmips16\n");
6296   else
6297     fprintf (asm_out_file, "\t.set\tnomips16\n");
6298
6299   if (TARGET_MICROMIPS)
6300     fprintf (asm_out_file, "\t.set\tmicromips\n");
6301 #ifdef HAVE_GAS_MICROMIPS
6302   else
6303     fprintf (asm_out_file, "\t.set\tnomicromips\n");
6304 #endif
6305
6306   if (!flag_inhibit_size_directive)
6307     {
6308       fputs ("\t.ent\t", asm_out_file);
6309       assemble_name (asm_out_file, name);
6310       fputs ("\n", asm_out_file);
6311     }
6312
6313   ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
6314
6315   /* Start the definition proper.  */
6316   assemble_name (asm_out_file, name);
6317   fputs (":\n", asm_out_file);
6318 }
6319
6320 /* End a function definition started by mips_start_function_definition.  */
6321
6322 static void
6323 mips_end_function_definition (const char *name)
6324 {
6325   if (!flag_inhibit_size_directive)
6326     {
6327       fputs ("\t.end\t", asm_out_file);
6328       assemble_name (asm_out_file, name);
6329       fputs ("\n", asm_out_file);
6330     }
6331 }
6332
6333 /* If *STUB_PTR points to a stub, output a comdat-style definition for it,
6334    then free *STUB_PTR.  */
6335
6336 static void
6337 mips_finish_stub (mips_one_only_stub **stub_ptr)
6338 {
6339   mips_one_only_stub *stub = *stub_ptr;
6340   if (!stub)
6341     return;
6342
6343   const char *name = stub->get_name ();
6344   mips_start_unique_function (name);
6345   mips_start_function_definition (name, false);
6346   stub->output_body ();
6347   mips_end_function_definition (name);
6348   delete stub;
6349   *stub_ptr = 0;
6350 }
6351 \f
6352 /* Return true if calls to X can use R_MIPS_CALL* relocations.  */
6353
6354 static bool
6355 mips_ok_for_lazy_binding_p (rtx x)
6356 {
6357   return (TARGET_USE_GOT
6358           && GET_CODE (x) == SYMBOL_REF
6359           && !SYMBOL_REF_BIND_NOW_P (x)
6360           && !mips_symbol_binds_local_p (x));
6361 }
6362
6363 /* Load function address ADDR into register DEST.  TYPE is as for
6364    mips_expand_call.  Return true if we used an explicit lazy-binding
6365    sequence.  */
6366
6367 static bool
6368 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
6369 {
6370   /* If we're generating PIC, and this call is to a global function,
6371      try to allow its address to be resolved lazily.  This isn't
6372      possible for sibcalls when $gp is call-saved because the value
6373      of $gp on entry to the stub would be our caller's gp, not ours.  */
6374   if (TARGET_EXPLICIT_RELOCS
6375       && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
6376       && mips_ok_for_lazy_binding_p (addr))
6377     {
6378       addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
6379       emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
6380       return true;
6381     }
6382   else
6383     {
6384       mips_emit_move (dest, addr);
6385       return false;
6386     }
6387 }
6388 \f
6389 struct local_alias_traits : default_hashmap_traits
6390 {
6391   static hashval_t hash (rtx);
6392   static bool equal_keys (rtx, rtx);
6393 };
6394
6395 /* Each locally-defined hard-float MIPS16 function has a local symbol
6396    associated with it.  This hash table maps the function symbol (FUNC)
6397    to the local symbol (LOCAL). */
6398 static GTY (()) hash_map<rtx, rtx, local_alias_traits> *mips16_local_aliases;
6399
6400 /* Hash table callbacks for mips16_local_aliases.  */
6401
6402 hashval_t
6403 local_alias_traits::hash (rtx func)
6404 {
6405   return htab_hash_string (XSTR (func, 0));
6406 }
6407
6408 bool
6409 local_alias_traits::equal_keys (rtx func1, rtx func2)
6410 {
6411   return rtx_equal_p (func1, func2);
6412 }
6413
6414 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
6415    Return a local alias for it, creating a new one if necessary.  */
6416
6417 static rtx
6418 mips16_local_alias (rtx func)
6419 {
6420   /* Create the hash table if this is the first call.  */
6421   if (mips16_local_aliases == NULL)
6422     mips16_local_aliases
6423       = hash_map<rtx, rtx, local_alias_traits>::create_ggc (37);
6424
6425   /* Look up the function symbol, creating a new entry if need be.  */
6426   bool existed;
6427   rtx *slot = &mips16_local_aliases->get_or_insert (func, &existed);
6428   gcc_assert (slot != NULL);
6429
6430   if (!existed)
6431     {
6432       const char *func_name, *local_name;
6433       rtx local;
6434
6435       /* Create a new SYMBOL_REF for the local symbol.  The choice of
6436          __fn_local_* is based on the __fn_stub_* names that we've
6437          traditionally used for the non-MIPS16 stub.  */
6438       func_name = targetm.strip_name_encoding (XSTR (func, 0));
6439       local_name = ACONCAT (("__fn_local_", func_name, NULL));
6440       local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
6441       SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
6442
6443       /* Create a new structure to represent the mapping.  */
6444       *slot = local;
6445     }
6446   return *slot;
6447 }
6448 \f
6449 /* A chained list of functions for which mips16_build_call_stub has already
6450    generated a stub.  NAME is the name of the function and FP_RET_P is true
6451    if the function returns a value in floating-point registers.  */
6452 struct mips16_stub {
6453   struct mips16_stub *next;
6454   char *name;
6455   bool fp_ret_p;
6456 };
6457 static struct mips16_stub *mips16_stubs;
6458
6459 /* Return the two-character string that identifies floating-point
6460    return mode MODE in the name of a MIPS16 function stub.  */
6461
6462 static const char *
6463 mips16_call_stub_mode_suffix (enum machine_mode mode)
6464 {
6465   if (mode == SFmode)
6466     return "sf";
6467   else if (mode == DFmode)
6468     return "df";
6469   else if (mode == SCmode)
6470     return "sc";
6471   else if (mode == DCmode)
6472     return "dc";
6473   else if (mode == V2SFmode)
6474     return "df";
6475   else
6476     gcc_unreachable ();
6477 }
6478
6479 /* Write instructions to move a 32-bit value between general register
6480    GPREG and floating-point register FPREG.  DIRECTION is 't' to move
6481    from GPREG to FPREG and 'f' to move in the opposite direction.  */
6482
6483 static void
6484 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6485 {
6486   fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6487            reg_names[gpreg], reg_names[fpreg]);
6488 }
6489
6490 /* Likewise for 64-bit values.  */
6491
6492 static void
6493 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6494 {
6495   if (TARGET_64BIT)
6496     fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
6497              reg_names[gpreg], reg_names[fpreg]);
6498   else if (TARGET_FLOAT64)
6499     {
6500       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6501                reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6502       fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
6503                reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
6504     }
6505   else
6506     {
6507       /* Move the least-significant word.  */
6508       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6509                reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6510       /* ...then the most significant word.  */
6511       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6512                reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
6513     }
6514 }
6515
6516 /* Write out code to move floating-point arguments into or out of
6517    general registers.  FP_CODE is the code describing which arguments
6518    are present (see the comment above the definition of CUMULATIVE_ARGS
6519    in mips.h).  DIRECTION is as for mips_output_32bit_xfer.  */
6520
6521 static void
6522 mips_output_args_xfer (int fp_code, char direction)
6523 {
6524   unsigned int gparg, fparg, f;
6525   CUMULATIVE_ARGS cum;
6526
6527   /* This code only works for o32 and o64.  */
6528   gcc_assert (TARGET_OLDABI);
6529
6530   mips_init_cumulative_args (&cum, NULL);
6531
6532   for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6533     {
6534       enum machine_mode mode;
6535       struct mips_arg_info info;
6536
6537       if ((f & 3) == 1)
6538         mode = SFmode;
6539       else if ((f & 3) == 2)
6540         mode = DFmode;
6541       else
6542         gcc_unreachable ();
6543
6544       mips_get_arg_info (&info, &cum, mode, NULL, true);
6545       gparg = mips_arg_regno (&info, false);
6546       fparg = mips_arg_regno (&info, true);
6547
6548       if (mode == SFmode)
6549         mips_output_32bit_xfer (direction, gparg, fparg);
6550       else
6551         mips_output_64bit_xfer (direction, gparg, fparg);
6552
6553       mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
6554     }
6555 }
6556
6557 /* Write a MIPS16 stub for the current function.  This stub is used
6558    for functions which take arguments in the floating-point registers.
6559    It is normal-mode code that moves the floating-point arguments
6560    into the general registers and then jumps to the MIPS16 code.  */
6561
6562 static void
6563 mips16_build_function_stub (void)
6564 {
6565   const char *fnname, *alias_name, *separator;
6566   char *secname, *stubname;
6567   tree stubdecl;
6568   unsigned int f;
6569   rtx symbol, alias;
6570
6571   /* Create the name of the stub, and its unique section.  */
6572   symbol = XEXP (DECL_RTL (current_function_decl), 0);
6573   alias = mips16_local_alias (symbol);
6574
6575   fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6576   alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6577   secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6578   stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6579
6580   /* Build a decl for the stub.  */
6581   stubdecl = build_decl (BUILTINS_LOCATION,
6582                          FUNCTION_DECL, get_identifier (stubname),
6583                          build_function_type_list (void_type_node, NULL_TREE));
6584   set_decl_section_name (stubdecl, secname);
6585   DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6586                                        RESULT_DECL, NULL_TREE, void_type_node);
6587
6588   /* Output a comment.  */
6589   fprintf (asm_out_file, "\t# Stub function for %s (",
6590            current_function_name ());
6591   separator = "";
6592   for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6593     {
6594       fprintf (asm_out_file, "%s%s", separator,
6595                (f & 3) == 1 ? "float" : "double");
6596       separator = ", ";
6597     }
6598   fprintf (asm_out_file, ")\n");
6599
6600   /* Start the function definition.  */
6601   assemble_start_function (stubdecl, stubname);
6602   mips_start_function_definition (stubname, false);
6603
6604   /* If generating pic2 code, either set up the global pointer or
6605      switch to pic0.  */
6606   if (TARGET_ABICALLS_PIC2)
6607     {
6608       if (TARGET_ABSOLUTE_ABICALLS)
6609         fprintf (asm_out_file, "\t.option\tpic0\n");
6610       else
6611         {
6612           output_asm_insn ("%(.cpload\t%^%)", NULL);
6613           /* Emit an R_MIPS_NONE relocation to tell the linker what the
6614              target function is.  Use a local GOT access when loading the
6615              symbol, to cut down on the number of unnecessary GOT entries
6616              for stubs that aren't needed.  */
6617           output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6618           symbol = alias;
6619         }
6620     }
6621
6622   /* Load the address of the MIPS16 function into $25.  Do this first so
6623      that targets with coprocessor interlocks can use an MFC1 to fill the
6624      delay slot.  */
6625   output_asm_insn ("la\t%^,%0", &symbol);
6626
6627   /* Move the arguments from floating-point registers to general registers.  */
6628   mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6629
6630   /* Jump to the MIPS16 function.  */
6631   output_asm_insn ("jr\t%^", NULL);
6632
6633   if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6634     fprintf (asm_out_file, "\t.option\tpic2\n");
6635
6636   mips_end_function_definition (stubname);
6637
6638   /* If the linker needs to create a dynamic symbol for the target
6639      function, it will associate the symbol with the stub (which,
6640      unlike the target function, follows the proper calling conventions).
6641      It is therefore useful to have a local alias for the target function,
6642      so that it can still be identified as MIPS16 code.  As an optimization,
6643      this symbol can also be used for indirect MIPS16 references from
6644      within this file.  */
6645   ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6646
6647   switch_to_section (function_section (current_function_decl));
6648 }
6649
6650 /* The current function is a MIPS16 function that returns a value in an FPR.
6651    Copy the return value from its soft-float to its hard-float location.
6652    libgcc2 has special non-MIPS16 helper functions for each case.  */
6653
6654 static void
6655 mips16_copy_fpr_return_value (void)
6656 {
6657   rtx fn, insn, retval;
6658   tree return_type;
6659   enum machine_mode return_mode;
6660   const char *name;
6661
6662   return_type = DECL_RESULT (current_function_decl);
6663   return_mode = DECL_MODE (return_type);
6664
6665   name = ACONCAT (("__mips16_ret_",
6666                    mips16_call_stub_mode_suffix (return_mode),
6667                    NULL));
6668   fn = mips16_stub_function (name);
6669
6670   /* The function takes arguments in $2 (and possibly $3), so calls
6671      to it cannot be lazily bound.  */
6672   SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6673
6674   /* Model the call as something that takes the GPR return value as
6675      argument and returns an "updated" value.  */
6676   retval = gen_rtx_REG (return_mode, GP_RETURN);
6677   insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6678                            const0_rtx, NULL_RTX, false);
6679   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6680 }
6681
6682 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6683    RETVAL is the location of the return value, or null if this is
6684    a "call" rather than a "call_value".  ARGS_SIZE is the size of the
6685    arguments and FP_CODE is the code built by mips_function_arg;
6686    see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6687
6688    There are three alternatives:
6689
6690    - If a stub was needed, emit the call and return the call insn itself.
6691
6692    - If we can avoid using a stub by redirecting the call, set *FN_PTR
6693      to the new target and return null.
6694
6695    - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6696      unmodified.
6697
6698    A stub is needed for calls to functions that, in normal mode,
6699    receive arguments in FPRs or return values in FPRs.  The stub
6700    copies the arguments from their soft-float positions to their
6701    hard-float positions, calls the real function, then copies the
6702    return value from its hard-float position to its soft-float
6703    position.
6704
6705    We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6706    If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6707    automatically redirects the JAL to the stub, otherwise the JAL
6708    continues to call FN directly.  */
6709
6710 static rtx_insn *
6711 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6712 {
6713   const char *fnname;
6714   bool fp_ret_p;
6715   struct mips16_stub *l;
6716   rtx_insn *insn;
6717   rtx pattern, fn;
6718
6719   /* We don't need to do anything if we aren't in MIPS16 mode, or if
6720      we were invoked with the -msoft-float option.  */
6721   if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
6722     return NULL;
6723
6724   /* Figure out whether the value might come back in a floating-point
6725      register.  */
6726   fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
6727
6728   /* We don't need to do anything if there were no floating-point
6729      arguments and the value will not be returned in a floating-point
6730      register.  */
6731   if (fp_code == 0 && !fp_ret_p)
6732     return NULL;
6733
6734   /* We don't need to do anything if this is a call to a special
6735      MIPS16 support function.  */
6736   fn = *fn_ptr;
6737   if (mips16_stub_function_p (fn))
6738     return NULL;
6739
6740   /* If we're calling a locally-defined MIPS16 function, we know that
6741      it will return values in both the "soft-float" and "hard-float"
6742      registers.  There is no need to use a stub to move the latter
6743      to the former.  */
6744   if (fp_code == 0 && mips16_local_function_p (fn))
6745     return NULL;
6746
6747   /* This code will only work for o32 and o64 abis.  The other ABI's
6748      require more sophisticated support.  */
6749   gcc_assert (TARGET_OLDABI);
6750
6751   /* If we're calling via a function pointer, use one of the magic
6752      libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
6753      Each stub expects the function address to arrive in register $2.  */
6754   if (GET_CODE (fn) != SYMBOL_REF
6755       || !call_insn_operand (fn, VOIDmode))
6756     {
6757       char buf[30];
6758       rtx stub_fn, addr;
6759       rtx_insn *insn;
6760       bool lazy_p;
6761
6762       /* If this is a locally-defined and locally-binding function,
6763          avoid the stub by calling the local alias directly.  */
6764       if (mips16_local_function_p (fn))
6765         {
6766           *fn_ptr = mips16_local_alias (fn);
6767           return NULL;
6768         }
6769
6770       /* Create a SYMBOL_REF for the libgcc.a function.  */
6771       if (fp_ret_p)
6772         sprintf (buf, "__mips16_call_stub_%s_%d",
6773                  mips16_call_stub_mode_suffix (GET_MODE (retval)),
6774                  fp_code);
6775       else
6776         sprintf (buf, "__mips16_call_stub_%d", fp_code);
6777       stub_fn = mips16_stub_function (buf);
6778
6779       /* The function uses $2 as an argument, so calls to it
6780          cannot be lazily bound.  */
6781       SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6782
6783       /* Load the target function into $2.  */
6784       addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6785       lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
6786
6787       /* Emit the call.  */
6788       insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
6789                                args_size, NULL_RTX, lazy_p);
6790
6791       /* Tell GCC that this call does indeed use the value of $2.  */
6792       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
6793
6794       /* If we are handling a floating-point return value, we need to
6795          save $18 in the function prologue.  Putting a note on the
6796          call will mean that df_regs_ever_live_p ($18) will be true if the
6797          call is not eliminated, and we can check that in the prologue
6798          code.  */
6799       if (fp_ret_p)
6800         CALL_INSN_FUNCTION_USAGE (insn) =
6801           gen_rtx_EXPR_LIST (VOIDmode,
6802                              gen_rtx_CLOBBER (VOIDmode,
6803                                               gen_rtx_REG (word_mode, 18)),
6804                              CALL_INSN_FUNCTION_USAGE (insn));
6805
6806       return insn;
6807     }
6808
6809   /* We know the function we are going to call.  If we have already
6810      built a stub, we don't need to do anything further.  */
6811   fnname = targetm.strip_name_encoding (XSTR (fn, 0));
6812   for (l = mips16_stubs; l != NULL; l = l->next)
6813     if (strcmp (l->name, fnname) == 0)
6814       break;
6815
6816   if (l == NULL)
6817     {
6818       const char *separator;
6819       char *secname, *stubname;
6820       tree stubid, stubdecl;
6821       unsigned int f;
6822
6823       /* If the function does not return in FPRs, the special stub
6824          section is named
6825              .mips16.call.FNNAME
6826
6827          If the function does return in FPRs, the stub section is named
6828              .mips16.call.fp.FNNAME
6829
6830          Build a decl for the stub.  */
6831       secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
6832                           fnname, NULL));
6833       stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
6834                            fnname, NULL));
6835       stubid = get_identifier (stubname);
6836       stubdecl = build_decl (BUILTINS_LOCATION,
6837                              FUNCTION_DECL, stubid,
6838                              build_function_type_list (void_type_node,
6839                                                        NULL_TREE));
6840       set_decl_section_name (stubdecl, secname);
6841       DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6842                                            RESULT_DECL, NULL_TREE,
6843                                            void_type_node);
6844
6845       /* Output a comment.  */
6846       fprintf (asm_out_file, "\t# Stub function to call %s%s (",
6847                (fp_ret_p
6848                 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
6849                 : ""),
6850                fnname);
6851       separator = "";
6852       for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6853         {
6854           fprintf (asm_out_file, "%s%s", separator,
6855                    (f & 3) == 1 ? "float" : "double");
6856           separator = ", ";
6857         }
6858       fprintf (asm_out_file, ")\n");
6859
6860       /* Start the function definition.  */
6861       assemble_start_function (stubdecl, stubname);
6862       mips_start_function_definition (stubname, false);
6863
6864       if (fp_ret_p)
6865         {
6866           fprintf (asm_out_file, "\t.cfi_startproc\n");
6867
6868           /* Create a fake CFA 4 bytes below the stack pointer.
6869              This works around unwinders (like libgcc's) that expect
6870              the CFA for non-signal frames to be unique.  */
6871           fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
6872
6873           /* "Save" $sp in itself so we don't use the fake CFA.
6874              This is: DW_CFA_val_expression r29, { DW_OP_reg29 }.  */
6875           fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
6876         }
6877       else
6878         {
6879           /* Load the address of the MIPS16 function into $25.  Do this
6880              first so that targets with coprocessor interlocks can use
6881              an MFC1 to fill the delay slot.  */
6882           if (TARGET_EXPLICIT_RELOCS)
6883             {
6884               output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
6885               output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
6886             }
6887           else
6888             output_asm_insn ("la\t%^,%0", &fn);
6889         }
6890
6891       /* Move the arguments from general registers to floating-point
6892          registers.  */
6893       mips_output_args_xfer (fp_code, 't');
6894
6895       if (fp_ret_p)
6896         {
6897           /* Save the return address in $18 and call the non-MIPS16 function.
6898              The stub's caller knows that $18 might be clobbered, even though
6899              $18 is usually a call-saved register.  */
6900           fprintf (asm_out_file, "\tmove\t%s,%s\n",
6901                    reg_names[GP_REG_FIRST + 18], reg_names[RETURN_ADDR_REGNUM]);
6902           output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
6903           fprintf (asm_out_file, "\t.cfi_register 31,18\n");
6904
6905           /* Move the result from floating-point registers to
6906              general registers.  */
6907           switch (GET_MODE (retval))
6908             {
6909             case SCmode:
6910               mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
6911                                       TARGET_BIG_ENDIAN
6912                                       ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6913                                       : FP_REG_FIRST);
6914               mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
6915                                       TARGET_LITTLE_ENDIAN
6916                                       ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6917                                       : FP_REG_FIRST);
6918               if (GET_MODE (retval) == SCmode && TARGET_64BIT)
6919                 {
6920                   /* On 64-bit targets, complex floats are returned in
6921                      a single GPR, such that "sd" on a suitably-aligned
6922                      target would store the value correctly.  */
6923                   fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6924                            reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6925                            reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6926                   fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6927                            reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
6928                            reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
6929                   fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
6930                            reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6931                            reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6932                   fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
6933                            reg_names[GP_RETURN],
6934                            reg_names[GP_RETURN],
6935                            reg_names[GP_RETURN + 1]);
6936                 }
6937               break;
6938
6939             case SFmode:
6940               mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6941               break;
6942
6943             case DCmode:
6944               mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
6945                                       FP_REG_FIRST + MAX_FPRS_PER_FMT);
6946               /* Fall though.  */
6947             case DFmode:
6948             case V2SFmode:
6949               mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6950               break;
6951
6952             default:
6953               gcc_unreachable ();
6954             }
6955           fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
6956           fprintf (asm_out_file, "\t.cfi_endproc\n");
6957         }
6958       else
6959         {
6960           /* Jump to the previously-loaded address.  */
6961           output_asm_insn ("jr\t%^", NULL);
6962         }
6963
6964 #ifdef ASM_DECLARE_FUNCTION_SIZE
6965       ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
6966 #endif
6967
6968       mips_end_function_definition (stubname);
6969
6970       /* Record this stub.  */
6971       l = XNEW (struct mips16_stub);
6972       l->name = xstrdup (fnname);
6973       l->fp_ret_p = fp_ret_p;
6974       l->next = mips16_stubs;
6975       mips16_stubs = l;
6976     }
6977
6978   /* If we expect a floating-point return value, but we've built a
6979      stub which does not expect one, then we're in trouble.  We can't
6980      use the existing stub, because it won't handle the floating-point
6981      value.  We can't build a new stub, because the linker won't know
6982      which stub to use for the various calls in this object file.
6983      Fortunately, this case is illegal, since it means that a function
6984      was declared in two different ways in a single compilation.  */
6985   if (fp_ret_p && !l->fp_ret_p)
6986     error ("cannot handle inconsistent calls to %qs", fnname);
6987
6988   if (retval == NULL_RTX)
6989     pattern = gen_call_internal_direct (fn, args_size);
6990   else
6991     pattern = gen_call_value_internal_direct (retval, fn, args_size);
6992   insn = mips_emit_call_insn (pattern, fn, fn, false);
6993
6994   /* If we are calling a stub which handles a floating-point return
6995      value, we need to arrange to save $18 in the prologue.  We do this
6996      by marking the function call as using the register.  The prologue
6997      will later see that it is used, and emit code to save it.  */
6998   if (fp_ret_p)
6999     CALL_INSN_FUNCTION_USAGE (insn) =
7000       gen_rtx_EXPR_LIST (VOIDmode,
7001                          gen_rtx_CLOBBER (VOIDmode,
7002                                           gen_rtx_REG (word_mode, 18)),
7003                          CALL_INSN_FUNCTION_USAGE (insn));
7004
7005   return insn;
7006 }
7007 \f
7008 /* Expand a call of type TYPE.  RESULT is where the result will go (null
7009    for "call"s and "sibcall"s), ADDR is the address of the function,
7010    ARGS_SIZE is the size of the arguments and AUX is the value passed
7011    to us by mips_function_arg.  LAZY_P is true if this call already
7012    involves a lazily-bound function address (such as when calling
7013    functions through a MIPS16 hard-float stub).
7014
7015    Return the call itself.  */
7016
7017 rtx_insn *
7018 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
7019                   rtx args_size, rtx aux, bool lazy_p)
7020 {
7021   rtx orig_addr, pattern;
7022   rtx_insn *insn;
7023   int fp_code;
7024
7025   fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
7026   insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
7027   if (insn)
7028     {
7029       gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
7030       return insn;
7031     }
7032
7033   orig_addr = addr;
7034   if (!call_insn_operand (addr, VOIDmode))
7035     {
7036       if (type == MIPS_CALL_EPILOGUE)
7037         addr = MIPS_EPILOGUE_TEMP (Pmode);
7038       else
7039         addr = gen_reg_rtx (Pmode);
7040       lazy_p |= mips_load_call_address (type, addr, orig_addr);
7041     }
7042
7043   if (result == 0)
7044     {
7045       rtx (*fn) (rtx, rtx);
7046
7047       if (type == MIPS_CALL_SIBCALL)
7048         fn = gen_sibcall_internal;
7049       else
7050         fn = gen_call_internal;
7051
7052       pattern = fn (addr, args_size);
7053     }
7054   else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
7055     {
7056       /* Handle return values created by mips_return_fpr_pair.  */
7057       rtx (*fn) (rtx, rtx, rtx, rtx);
7058       rtx reg1, reg2;
7059
7060       if (type == MIPS_CALL_SIBCALL)
7061         fn = gen_sibcall_value_multiple_internal;
7062       else
7063         fn = gen_call_value_multiple_internal;
7064
7065       reg1 = XEXP (XVECEXP (result, 0, 0), 0);
7066       reg2 = XEXP (XVECEXP (result, 0, 1), 0);
7067       pattern = fn (reg1, addr, args_size, reg2);
7068     }
7069   else
7070     {
7071       rtx (*fn) (rtx, rtx, rtx);
7072
7073       if (type == MIPS_CALL_SIBCALL)
7074         fn = gen_sibcall_value_internal;
7075       else
7076         fn = gen_call_value_internal;
7077
7078       /* Handle return values created by mips_return_fpr_single.  */
7079       if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
7080         result = XEXP (XVECEXP (result, 0, 0), 0);
7081       pattern = fn (result, addr, args_size);
7082     }
7083
7084   return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
7085 }
7086
7087 /* Split call instruction INSN into a $gp-clobbering call and
7088    (where necessary) an instruction to restore $gp from its save slot.
7089    CALL_PATTERN is the pattern of the new call.  */
7090
7091 void
7092 mips_split_call (rtx insn, rtx call_pattern)
7093 {
7094   emit_call_insn (call_pattern);
7095   if (!find_reg_note (insn, REG_NORETURN, 0))
7096     mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode,
7097                                                       POST_CALL_TMP_REG));
7098 }
7099
7100 /* Return true if a call to DECL may need to use JALX.  */
7101
7102 static bool
7103 mips_call_may_need_jalx_p (tree decl)
7104 {
7105   /* If the current translation unit would use a different mode for DECL,
7106      assume that the call needs JALX.  */
7107   if (mips_get_compress_mode (decl) != TARGET_COMPRESSION)
7108     return true;
7109
7110   /* mips_get_compress_mode is always accurate for locally-binding
7111      functions in the current translation unit.  */
7112   if (!DECL_EXTERNAL (decl) && targetm.binds_local_p (decl))
7113     return false;
7114
7115   /* When -minterlink-compressed is in effect, assume that functions
7116      could use a different encoding mode unless an attribute explicitly
7117      tells us otherwise.  */
7118   if (TARGET_INTERLINK_COMPRESSED)
7119     {
7120       if (!TARGET_COMPRESSION
7121           && mips_get_compress_off_flags (DECL_ATTRIBUTES (decl)) ==0)
7122         return true;
7123       if (TARGET_COMPRESSION
7124           && mips_get_compress_on_flags (DECL_ATTRIBUTES (decl)) == 0)
7125         return true;
7126     }
7127
7128   return false;
7129 }
7130
7131 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL.  */
7132
7133 static bool
7134 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
7135 {
7136   if (!TARGET_SIBCALLS)
7137     return false;
7138
7139   /* Interrupt handlers need special epilogue code and therefore can't
7140      use sibcalls.  */
7141   if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
7142     return false;
7143
7144   /* Direct Js are only possible to functions that use the same ISA encoding.
7145      There is no JX counterpoart of JALX.  */
7146   if (decl
7147       && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode)
7148       && mips_call_may_need_jalx_p (decl))
7149     return false;
7150
7151   /* Sibling calls should not prevent lazy binding.  Lazy-binding stubs
7152      require $gp to be valid on entry, so sibcalls can only use stubs
7153      if $gp is call-clobbered.  */
7154   if (decl
7155       && TARGET_CALL_SAVED_GP
7156       && !TARGET_ABICALLS_PIC0
7157       && !targetm.binds_local_p (decl))
7158     return false;
7159
7160   /* Otherwise OK.  */
7161   return true;
7162 }
7163 \f
7164 /* Implement MOVE_BY_PIECES_P.  */
7165
7166 bool
7167 mips_move_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7168 {
7169   if (HAVE_movmemsi)
7170     {
7171       /* movmemsi is meant to generate code that is at least as good as
7172          move_by_pieces.  However, movmemsi effectively uses a by-pieces
7173          implementation both for moves smaller than a word and for
7174          word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
7175          bytes.  We should allow the tree-level optimisers to do such
7176          moves by pieces, as it often exposes other optimization
7177          opportunities.  We might as well continue to use movmemsi at
7178          the rtl level though, as it produces better code when
7179          scheduling is disabled (such as at -O).  */
7180       if (currently_expanding_to_rtl)
7181         return false;
7182       if (align < BITS_PER_WORD)
7183         return size < UNITS_PER_WORD;
7184       return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
7185     }
7186   /* The default value.  If this becomes a target hook, we should
7187      call the default definition instead.  */
7188   return (move_by_pieces_ninsns (size, align, MOVE_MAX_PIECES + 1)
7189           < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()));
7190 }
7191
7192 /* Implement STORE_BY_PIECES_P.  */
7193
7194 bool
7195 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7196 {
7197   /* Storing by pieces involves moving constants into registers
7198      of size MIN (ALIGN, BITS_PER_WORD), then storing them.
7199      We need to decide whether it is cheaper to load the address of
7200      constant data into a register and use a block move instead.  */
7201
7202   /* If the data is only byte aligned, then:
7203
7204      (a1) A block move of less than 4 bytes would involve three 3 LBs and
7205           3 SBs.  We might as well use 3 single-instruction LIs and 3 SBs
7206           instead.
7207
7208      (a2) A block move of 4 bytes from aligned source data can use an
7209           LW/SWL/SWR sequence.  This is often better than the 4 LIs and
7210           4 SBs that we would generate when storing by pieces.  */
7211   if (align <= BITS_PER_UNIT)
7212     return size < 4;
7213
7214   /* If the data is 2-byte aligned, then:
7215
7216      (b1) A block move of less than 4 bytes would use a combination of LBs,
7217           LHs, SBs and SHs.  We get better code by using single-instruction
7218           LIs, SBs and SHs instead.
7219
7220      (b2) A block move of 4 bytes from aligned source data would again use
7221           an LW/SWL/SWR sequence.  In most cases, loading the address of
7222           the source data would require at least one extra instruction.
7223           It is often more efficient to use 2 single-instruction LIs and
7224           2 SHs instead.
7225
7226      (b3) A block move of up to 3 additional bytes would be like (b1).
7227
7228      (b4) A block move of 8 bytes from aligned source data can use two
7229           LW/SWL/SWR sequences or a single LD/SDL/SDR sequence.  Both
7230           sequences are better than the 4 LIs and 4 SHs that we'd generate
7231           when storing by pieces.
7232
7233      The reasoning for higher alignments is similar:
7234
7235      (c1) A block move of less than 4 bytes would be the same as (b1).
7236
7237      (c2) A block move of 4 bytes would use an LW/SW sequence.  Again,
7238           loading the address of the source data would typically require
7239           at least one extra instruction.  It is generally better to use
7240           LUI/ORI/SW instead.
7241
7242      (c3) A block move of up to 3 additional bytes would be like (b1).
7243
7244      (c4) A block move of 8 bytes can use two LW/SW sequences or a single
7245           LD/SD sequence, and in these cases we've traditionally preferred
7246           the memory copy over the more bulky constant moves.  */
7247   return size < 8;
7248 }
7249
7250 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
7251    Assume that the areas do not overlap.  */
7252
7253 static void
7254 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
7255 {
7256   HOST_WIDE_INT offset, delta;
7257   unsigned HOST_WIDE_INT bits;
7258   int i;
7259   enum machine_mode mode;
7260   rtx *regs;
7261
7262   /* Work out how many bits to move at a time.  If both operands have
7263      half-word alignment, it is usually better to move in half words.
7264      For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
7265      and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
7266      Otherwise move word-sized chunks.  */
7267   if (MEM_ALIGN (src) == BITS_PER_WORD / 2
7268       && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
7269     bits = BITS_PER_WORD / 2;
7270   else
7271     bits = BITS_PER_WORD;
7272
7273   mode = mode_for_size (bits, MODE_INT, 0);
7274   delta = bits / BITS_PER_UNIT;
7275
7276   /* Allocate a buffer for the temporary registers.  */
7277   regs = XALLOCAVEC (rtx, length / delta);
7278
7279   /* Load as many BITS-sized chunks as possible.  Use a normal load if
7280      the source has enough alignment, otherwise use left/right pairs.  */
7281   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7282     {
7283       regs[i] = gen_reg_rtx (mode);
7284       if (MEM_ALIGN (src) >= bits)
7285         mips_emit_move (regs[i], adjust_address (src, mode, offset));
7286       else
7287         {
7288           rtx part = adjust_address (src, BLKmode, offset);
7289           set_mem_size (part, delta);
7290           if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
7291             gcc_unreachable ();
7292         }
7293     }
7294
7295   /* Copy the chunks to the destination.  */
7296   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7297     if (MEM_ALIGN (dest) >= bits)
7298       mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
7299     else
7300       {
7301         rtx part = adjust_address (dest, BLKmode, offset);
7302         set_mem_size (part, delta);
7303         if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
7304           gcc_unreachable ();
7305       }
7306
7307   /* Mop up any left-over bytes.  */
7308   if (offset < length)
7309     {
7310       src = adjust_address (src, BLKmode, offset);
7311       dest = adjust_address (dest, BLKmode, offset);
7312       move_by_pieces (dest, src, length - offset,
7313                       MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
7314     }
7315 }
7316
7317 /* Helper function for doing a loop-based block operation on memory
7318    reference MEM.  Each iteration of the loop will operate on LENGTH
7319    bytes of MEM.
7320
7321    Create a new base register for use within the loop and point it to
7322    the start of MEM.  Create a new memory reference that uses this
7323    register.  Store them in *LOOP_REG and *LOOP_MEM respectively.  */
7324
7325 static void
7326 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
7327                        rtx *loop_reg, rtx *loop_mem)
7328 {
7329   *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
7330
7331   /* Although the new mem does not refer to a known location,
7332      it does keep up to LENGTH bytes of alignment.  */
7333   *loop_mem = change_address (mem, BLKmode, *loop_reg);
7334   set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
7335 }
7336
7337 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
7338    bytes at a time.  LENGTH must be at least BYTES_PER_ITER.  Assume that
7339    the memory regions do not overlap.  */
7340
7341 static void
7342 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
7343                       HOST_WIDE_INT bytes_per_iter)
7344 {
7345   rtx_code_label *label;
7346   rtx src_reg, dest_reg, final_src, test;
7347   HOST_WIDE_INT leftover;
7348
7349   leftover = length % bytes_per_iter;
7350   length -= leftover;
7351
7352   /* Create registers and memory references for use within the loop.  */
7353   mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
7354   mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
7355
7356   /* Calculate the value that SRC_REG should have after the last iteration
7357      of the loop.  */
7358   final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
7359                                    0, 0, OPTAB_WIDEN);
7360
7361   /* Emit the start of the loop.  */
7362   label = gen_label_rtx ();
7363   emit_label (label);
7364
7365   /* Emit the loop body.  */
7366   mips_block_move_straight (dest, src, bytes_per_iter);
7367
7368   /* Move on to the next block.  */
7369   mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
7370   mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
7371
7372   /* Emit the loop condition.  */
7373   test = gen_rtx_NE (VOIDmode, src_reg, final_src);
7374   if (Pmode == DImode)
7375     emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
7376   else
7377     emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
7378
7379   /* Mop up any left-over bytes.  */
7380   if (leftover)
7381     mips_block_move_straight (dest, src, leftover);
7382 }
7383
7384 /* Expand a movmemsi instruction, which copies LENGTH bytes from
7385    memory reference SRC to memory reference DEST.  */
7386
7387 bool
7388 mips_expand_block_move (rtx dest, rtx src, rtx length)
7389 {
7390   if (CONST_INT_P (length))
7391     {
7392       if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
7393         {
7394           mips_block_move_straight (dest, src, INTVAL (length));
7395           return true;
7396         }
7397       else if (optimize)
7398         {
7399           mips_block_move_loop (dest, src, INTVAL (length),
7400                                 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
7401           return true;
7402         }
7403     }
7404   return false;
7405 }
7406 \f
7407 /* Expand a loop of synci insns for the address range [BEGIN, END).  */
7408
7409 void
7410 mips_expand_synci_loop (rtx begin, rtx end)
7411 {
7412   rtx inc, cmp_result, mask, length;
7413   rtx_code_label *label, *end_label;
7414
7415   /* Create end_label.  */
7416   end_label = gen_label_rtx ();
7417
7418   /* Check if begin equals end.  */
7419   cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
7420   emit_jump_insn (gen_condjump (cmp_result, end_label));
7421
7422   /* Load INC with the cache line size (rdhwr INC,$1).  */
7423   inc = gen_reg_rtx (Pmode);
7424   emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
7425
7426   /* Check if inc is 0.  */
7427   cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
7428   emit_jump_insn (gen_condjump (cmp_result, end_label));
7429
7430   /* Calculate mask.  */
7431   mask = mips_force_unary (Pmode, NEG, inc);
7432
7433   /* Mask out begin by mask.  */
7434   begin = mips_force_binary (Pmode, AND, begin, mask);
7435
7436   /* Calculate length.  */
7437   length = mips_force_binary (Pmode, MINUS, end, begin);
7438
7439   /* Loop back to here.  */
7440     label = gen_label_rtx ();
7441   emit_label (label);
7442
7443   emit_insn (gen_synci (begin));
7444
7445   /* Update length.  */
7446   mips_emit_binary (MINUS, length, length, inc);
7447
7448   /* Update begin.  */
7449   mips_emit_binary (PLUS, begin, begin, inc);
7450
7451   /* Check if length is greater than 0.  */
7452   cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
7453   emit_jump_insn (gen_condjump (cmp_result, label));
7454
7455   emit_label (end_label);
7456 }
7457 \f
7458 /* Expand a QI or HI mode atomic memory operation.
7459
7460    GENERATOR contains a pointer to the gen_* function that generates
7461    the SI mode underlying atomic operation using masks that we
7462    calculate.
7463
7464    RESULT is the return register for the operation.  Its value is NULL
7465    if unused.
7466
7467    MEM is the location of the atomic access.
7468
7469    OLDVAL is the first operand for the operation.
7470
7471    NEWVAL is the optional second operand for the operation.  Its value
7472    is NULL if unused.  */
7473
7474 void
7475 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
7476                          rtx result, rtx mem, rtx oldval, rtx newval)
7477 {
7478   rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
7479   rtx unshifted_mask_reg, mask, inverted_mask, si_op;
7480   rtx res = NULL;
7481   enum machine_mode mode;
7482
7483   mode = GET_MODE (mem);
7484
7485   /* Compute the address of the containing SImode value.  */
7486   orig_addr = force_reg (Pmode, XEXP (mem, 0));
7487   memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
7488                                   force_reg (Pmode, GEN_INT (-4)));
7489
7490   /* Create a memory reference for it.  */
7491   memsi = gen_rtx_MEM (SImode, memsi_addr);
7492   set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
7493   MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
7494
7495   /* Work out the byte offset of the QImode or HImode value,
7496      counting from the least significant byte.  */
7497   shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
7498   if (TARGET_BIG_ENDIAN)
7499     mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
7500
7501   /* Multiply by eight to convert the shift value from bytes to bits.  */
7502   mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
7503
7504   /* Make the final shift an SImode value, so that it can be used in
7505      SImode operations.  */
7506   shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
7507
7508   /* Set MASK to an inclusive mask of the QImode or HImode value.  */
7509   unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
7510   unshifted_mask_reg = force_reg (SImode, unshifted_mask);
7511   mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
7512
7513   /* Compute the equivalent exclusive mask.  */
7514   inverted_mask = gen_reg_rtx (SImode);
7515   emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
7516                           gen_rtx_NOT (SImode, mask)));
7517
7518   /* Shift the old value into place.  */
7519   if (oldval != const0_rtx)
7520     {
7521       oldval = convert_modes (SImode, mode, oldval, true);
7522       oldval = force_reg (SImode, oldval);
7523       oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
7524     }
7525
7526   /* Do the same for the new value.  */
7527   if (newval && newval != const0_rtx)
7528     {
7529       newval = convert_modes (SImode, mode, newval, true);
7530       newval = force_reg (SImode, newval);
7531       newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
7532     }
7533
7534   /* Do the SImode atomic access.  */
7535   if (result)
7536     res = gen_reg_rtx (SImode);
7537   if (newval)
7538     si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
7539   else if (result)
7540     si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
7541   else
7542     si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
7543
7544   emit_insn (si_op);
7545
7546   if (result)
7547     {
7548       /* Shift and convert the result.  */
7549       mips_emit_binary (AND, res, res, mask);
7550       mips_emit_binary (LSHIFTRT, res, res, shiftsi);
7551       mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
7552     }
7553 }
7554
7555 /* Return true if it is possible to use left/right accesses for a
7556    bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
7557    When returning true, update *LEFT and *RIGHT as follows:
7558
7559    *LEFT is a QImode reference to the first byte if big endian or
7560    the last byte if little endian.  This address can be used in the
7561    left-side instructions (LWL, SWL, LDL, SDL).
7562
7563    *RIGHT is a QImode reference to the opposite end of the field and
7564    can be used in the patterning right-side instruction.  */
7565
7566 static bool
7567 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
7568                         rtx *left, rtx *right)
7569 {
7570   rtx first, last;
7571
7572   /* Check that the size is valid.  */
7573   if (width != 32 && (!TARGET_64BIT || width != 64))
7574     return false;
7575
7576   /* We can only access byte-aligned values.  Since we are always passed
7577      a reference to the first byte of the field, it is not necessary to
7578      do anything with BITPOS after this check.  */
7579   if (bitpos % BITS_PER_UNIT != 0)
7580     return false;
7581
7582   /* Reject aligned bitfields: we want to use a normal load or store
7583      instead of a left/right pair.  */
7584   if (MEM_ALIGN (op) >= width)
7585     return false;
7586
7587   /* Get references to both ends of the field.  */
7588   first = adjust_address (op, QImode, 0);
7589   last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
7590
7591   /* Allocate to LEFT and RIGHT according to endianness.  LEFT should
7592      correspond to the MSB and RIGHT to the LSB.  */
7593   if (TARGET_BIG_ENDIAN)
7594     *left = first, *right = last;
7595   else
7596     *left = last, *right = first;
7597
7598   return true;
7599 }
7600
7601 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
7602    DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
7603    the operation is the equivalent of:
7604
7605       (set DEST (*_extract SRC WIDTH BITPOS))
7606
7607    Return true on success.  */
7608
7609 bool
7610 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
7611                                    HOST_WIDE_INT bitpos, bool unsigned_p)
7612 {
7613   rtx left, right, temp;
7614   rtx dest1 = NULL_RTX;
7615
7616   /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
7617      be a DImode, create a new temp and emit a zero extend at the end.  */
7618   if (GET_MODE (dest) == DImode
7619       && REG_P (dest)
7620       && GET_MODE_BITSIZE (SImode) == width)
7621     {
7622       dest1 = dest;
7623       dest = gen_reg_rtx (SImode);
7624     }
7625
7626   if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
7627     return false;
7628
7629   temp = gen_reg_rtx (GET_MODE (dest));
7630   if (GET_MODE (dest) == DImode)
7631     {
7632       emit_insn (gen_mov_ldl (temp, src, left));
7633       emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
7634     }
7635   else
7636     {
7637       emit_insn (gen_mov_lwl (temp, src, left));
7638       emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
7639     }
7640
7641   /* If we were loading 32bits and the original register was DI then
7642      sign/zero extend into the orignal dest.  */
7643   if (dest1)
7644     {
7645       if (unsigned_p)
7646         emit_insn (gen_zero_extendsidi2 (dest1, dest));
7647       else
7648         emit_insn (gen_extendsidi2 (dest1, dest));
7649     }
7650   return true;
7651 }
7652
7653 /* Try to use left/right stores to expand an "ins" pattern.  DEST, WIDTH,
7654    BITPOS and SRC are the operands passed to the expander; the operation
7655    is the equivalent of:
7656
7657        (set (zero_extract DEST WIDTH BITPOS) SRC)
7658
7659    Return true on success.  */
7660
7661 bool
7662 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
7663                                     HOST_WIDE_INT bitpos)
7664 {
7665   rtx left, right;
7666   enum machine_mode mode;
7667
7668   if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
7669     return false;
7670
7671   mode = mode_for_size (width, MODE_INT, 0);
7672   src = gen_lowpart (mode, src);
7673   if (mode == DImode)
7674     {
7675       emit_insn (gen_mov_sdl (dest, src, left));
7676       emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7677     }
7678   else
7679     {
7680       emit_insn (gen_mov_swl (dest, src, left));
7681       emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7682     }
7683   return true;
7684 }
7685
7686 /* Return true if X is a MEM with the same size as MODE.  */
7687
7688 bool
7689 mips_mem_fits_mode_p (enum machine_mode mode, rtx x)
7690 {
7691   return (MEM_P (x)
7692           && MEM_SIZE_KNOWN_P (x)
7693           && MEM_SIZE (x) == GET_MODE_SIZE (mode));
7694 }
7695
7696 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7697    source of an "ext" instruction or the destination of an "ins"
7698    instruction.  OP must be a register operand and the following
7699    conditions must hold:
7700
7701      0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7702      0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7703      0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7704
7705    Also reject lengths equal to a word as they are better handled
7706    by the move patterns.  */
7707
7708 bool
7709 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7710 {
7711   if (!ISA_HAS_EXT_INS
7712       || !register_operand (op, VOIDmode)
7713       || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7714     return false;
7715
7716   if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7717     return false;
7718
7719   if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7720     return false;
7721
7722   return true;
7723 }
7724
7725 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7726    operation if MAXLEN is the maxium length of consecutive bits that
7727    can make up MASK.  MODE is the mode of the operation.  See
7728    mask_low_and_shift_len for the actual definition.  */
7729
7730 bool
7731 mask_low_and_shift_p (enum machine_mode mode, rtx mask, rtx shift, int maxlen)
7732 {
7733   return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7734 }
7735
7736 /* Return true iff OP1 and OP2 are valid operands together for the
7737    *and<MODE>3 and *and<MODE>3_mips16 patterns.  For the cases to consider,
7738    see the table in the comment before the pattern.  */
7739
7740 bool
7741 and_operands_ok (enum machine_mode mode, rtx op1, rtx op2)
7742 {
7743   return (memory_operand (op1, mode)
7744           ? and_load_operand (op2, mode)
7745           : and_reg_operand (op2, mode));
7746 }
7747
7748 /* The canonical form of a mask-low-and-shift-left operation is
7749    (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7750    cleared.  Thus we need to shift MASK to the right before checking if it
7751    is a valid mask value.  MODE is the mode of the operation.  If true
7752    return the length of the mask, otherwise return -1.  */
7753
7754 int
7755 mask_low_and_shift_len (enum machine_mode mode, rtx mask, rtx shift)
7756 {
7757   HOST_WIDE_INT shval;
7758
7759   shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7760   return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7761 }
7762 \f
7763 /* Return true if -msplit-addresses is selected and should be honored.
7764
7765    -msplit-addresses is a half-way house between explicit relocations
7766    and the traditional assembler macros.  It can split absolute 32-bit
7767    symbolic constants into a high/lo_sum pair but uses macros for other
7768    sorts of access.
7769
7770    Like explicit relocation support for REL targets, it relies
7771    on GNU extensions in the assembler and the linker.
7772
7773    Although this code should work for -O0, it has traditionally
7774    been treated as an optimization.  */
7775
7776 static bool
7777 mips_split_addresses_p (void)
7778 {
7779   return (TARGET_SPLIT_ADDRESSES
7780           && optimize
7781           && !TARGET_MIPS16
7782           && !flag_pic
7783           && !ABI_HAS_64BIT_SYMBOLS);
7784 }
7785
7786 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs.  */
7787
7788 static void
7789 mips_init_relocs (void)
7790 {
7791   memset (mips_split_p, '\0', sizeof (mips_split_p));
7792   memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
7793   memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
7794   memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
7795   memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
7796
7797   if (TARGET_MIPS16_PCREL_LOADS)
7798     mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
7799   else
7800     {
7801       if (ABI_HAS_64BIT_SYMBOLS)
7802         {
7803           if (TARGET_EXPLICIT_RELOCS)
7804             {
7805               mips_split_p[SYMBOL_64_HIGH] = true;
7806               mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
7807               mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
7808
7809               mips_split_p[SYMBOL_64_MID] = true;
7810               mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
7811               mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
7812
7813               mips_split_p[SYMBOL_64_LOW] = true;
7814               mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
7815               mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
7816
7817               mips_split_p[SYMBOL_ABSOLUTE] = true;
7818               mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7819             }
7820         }
7821       else
7822         {
7823           if (TARGET_EXPLICIT_RELOCS
7824               || mips_split_addresses_p ()
7825               || TARGET_MIPS16)
7826             {
7827               mips_split_p[SYMBOL_ABSOLUTE] = true;
7828               mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
7829               mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7830             }
7831         }
7832     }
7833
7834   if (TARGET_MIPS16)
7835     {
7836       /* The high part is provided by a pseudo copy of $gp.  */
7837       mips_split_p[SYMBOL_GP_RELATIVE] = true;
7838       mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
7839     }
7840   else if (TARGET_EXPLICIT_RELOCS)
7841     /* Small data constants are kept whole until after reload,
7842        then lowered by mips_rewrite_small_data.  */
7843     mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
7844
7845   if (TARGET_EXPLICIT_RELOCS)
7846     {
7847       mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
7848       if (TARGET_NEWABI)
7849         {
7850           mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
7851           mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
7852         }
7853       else
7854         {
7855           mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
7856           mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
7857         }
7858       if (TARGET_MIPS16)
7859         /* Expose the use of $28 as soon as possible.  */
7860         mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
7861
7862       if (TARGET_XGOT)
7863         {
7864           /* The HIGH and LO_SUM are matched by special .md patterns.  */
7865           mips_split_p[SYMBOL_GOT_DISP] = true;
7866
7867           mips_split_p[SYMBOL_GOTOFF_DISP] = true;
7868           mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
7869           mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
7870
7871           mips_split_p[SYMBOL_GOTOFF_CALL] = true;
7872           mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
7873           mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
7874         }
7875       else
7876         {
7877           if (TARGET_NEWABI)
7878             mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
7879           else
7880             mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
7881           mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
7882           if (TARGET_MIPS16)
7883             /* Expose the use of $28 as soon as possible.  */
7884             mips_split_p[SYMBOL_GOT_DISP] = true;
7885         }
7886     }
7887
7888   if (TARGET_NEWABI)
7889     {
7890       mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
7891       mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
7892       mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
7893     }
7894
7895   mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
7896   mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
7897
7898   if (TARGET_MIPS16_PCREL_LOADS)
7899     {
7900       mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
7901       mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
7902     }
7903   else
7904     {
7905       mips_split_p[SYMBOL_DTPREL] = true;
7906       mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
7907       mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
7908
7909       mips_split_p[SYMBOL_TPREL] = true;
7910       mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
7911       mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
7912     }
7913
7914   mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
7915   mips_lo_relocs[SYMBOL_HALF] = "%half(";
7916 }
7917
7918 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
7919    in context CONTEXT.  RELOCS is the array of relocations to use.  */
7920
7921 static void
7922 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
7923                           const char **relocs)
7924 {
7925   enum mips_symbol_type symbol_type;
7926   const char *p;
7927
7928   symbol_type = mips_classify_symbolic_expression (op, context);
7929   gcc_assert (relocs[symbol_type]);
7930
7931   fputs (relocs[symbol_type], file);
7932   output_addr_const (file, mips_strip_unspec_address (op));
7933   for (p = relocs[symbol_type]; *p != 0; p++)
7934     if (*p == '(')
7935       fputc (')', file);
7936 }
7937
7938 /* Start a new block with the given asm switch enabled.  If we need
7939    to print a directive, emit PREFIX before it and SUFFIX after it.  */
7940
7941 static void
7942 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
7943                         const char *prefix, const char *suffix)
7944 {
7945   if (asm_switch->nesting_level == 0)
7946     fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
7947   asm_switch->nesting_level++;
7948 }
7949
7950 /* Likewise, but end a block.  */
7951
7952 static void
7953 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
7954                        const char *prefix, const char *suffix)
7955 {
7956   gcc_assert (asm_switch->nesting_level);
7957   asm_switch->nesting_level--;
7958   if (asm_switch->nesting_level == 0)
7959     fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
7960 }
7961
7962 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
7963    that either print a complete line or print nothing.  */
7964
7965 void
7966 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
7967 {
7968   mips_push_asm_switch_1 (asm_switch, "\t", "\n");
7969 }
7970
7971 void
7972 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
7973 {
7974   mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
7975 }
7976
7977 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
7978    The punctuation characters are:
7979
7980    '('  Start a nested ".set noreorder" block.
7981    ')'  End a nested ".set noreorder" block.
7982    '['  Start a nested ".set noat" block.
7983    ']'  End a nested ".set noat" block.
7984    '<'  Start a nested ".set nomacro" block.
7985    '>'  End a nested ".set nomacro" block.
7986    '*'  Behave like %(%< if generating a delayed-branch sequence.
7987    '#'  Print a nop if in a ".set noreorder" block.
7988    '/'  Like '#', but do nothing within a delayed-branch sequence.
7989    '?'  Print "l" if mips_branch_likely is true
7990    '~'  Print a nop if mips_branch_likely is true
7991    '.'  Print the name of the register with a hard-wired zero (zero or $0).
7992    '@'  Print the name of the assembler temporary register (at or $1).
7993    '^'  Print the name of the pic call-through register (t9 or $25).
7994    '+'  Print the name of the gp register (usually gp or $28).
7995    '$'  Print the name of the stack pointer register (sp or $29).
7996    ':'  Print "c" to use the compact version if the delay slot is a nop.
7997    '!'  Print "s" to use the short version if the delay slot contains a
7998         16-bit instruction.
7999
8000    See also mips_init_print_operand_pucnt.  */
8001
8002 static void
8003 mips_print_operand_punctuation (FILE *file, int ch)
8004 {
8005   switch (ch)
8006     {
8007     case '(':
8008       mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
8009       break;
8010
8011     case ')':
8012       mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
8013       break;
8014
8015     case '[':
8016       mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
8017       break;
8018
8019     case ']':
8020       mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
8021       break;
8022
8023     case '<':
8024       mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
8025       break;
8026
8027     case '>':
8028       mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
8029       break;
8030
8031     case '*':
8032       if (final_sequence != 0)
8033         {
8034           mips_print_operand_punctuation (file, '(');
8035           mips_print_operand_punctuation (file, '<');
8036         }
8037       break;
8038
8039     case '#':
8040       if (mips_noreorder.nesting_level > 0)
8041         fputs ("\n\tnop", file);
8042       break;
8043
8044     case '/':
8045       /* Print an extra newline so that the delayed insn is separated
8046          from the following ones.  This looks neater and is consistent
8047          with non-nop delayed sequences.  */
8048       if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
8049         fputs ("\n\tnop\n", file);
8050       break;
8051
8052     case '?':
8053       if (mips_branch_likely)
8054         putc ('l', file);
8055       break;
8056
8057     case '~':
8058       if (mips_branch_likely)
8059         fputs ("\n\tnop", file);
8060       break;
8061
8062     case '.':
8063       fputs (reg_names[GP_REG_FIRST + 0], file);
8064       break;
8065
8066     case '@':
8067       fputs (reg_names[AT_REGNUM], file);
8068       break;
8069
8070     case '^':
8071       fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
8072       break;
8073
8074     case '+':
8075       fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
8076       break;
8077
8078     case '$':
8079       fputs (reg_names[STACK_POINTER_REGNUM], file);
8080       break;
8081
8082     case ':':
8083       /* When final_sequence is 0, the delay slot will be a nop.  We can
8084          use the compact version for microMIPS.  */
8085       if (final_sequence == 0)
8086         putc ('c', file);
8087       break;
8088
8089     case '!':
8090       /* If the delay slot instruction is short, then use the
8091          compact version.  */
8092       if (final_sequence == 0
8093           || get_attr_length (final_sequence->insn (1)) == 2)
8094         putc ('s', file);
8095       break;
8096
8097     default:
8098       gcc_unreachable ();
8099       break;
8100     }
8101 }
8102
8103 /* Initialize mips_print_operand_punct.  */
8104
8105 static void
8106 mips_init_print_operand_punct (void)
8107 {
8108   const char *p;
8109
8110   for (p = "()[]<>*#/?~.@^+$:!"; *p; p++)
8111     mips_print_operand_punct[(unsigned char) *p] = true;
8112 }
8113
8114 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
8115    associated with condition CODE.  Print the condition part of the
8116    opcode to FILE.  */
8117
8118 static void
8119 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
8120 {
8121   switch (code)
8122     {
8123     case EQ:
8124     case NE:
8125     case GT:
8126     case GE:
8127     case LT:
8128     case LE:
8129     case GTU:
8130     case GEU:
8131     case LTU:
8132     case LEU:
8133       /* Conveniently, the MIPS names for these conditions are the same
8134          as their RTL equivalents.  */
8135       fputs (GET_RTX_NAME (code), file);
8136       break;
8137
8138     default:
8139       output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8140       break;
8141     }
8142 }
8143
8144 /* Likewise floating-point branches.  */
8145
8146 static void
8147 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
8148 {
8149   switch (code)
8150     {
8151     case EQ:
8152       fputs ("c1f", file);
8153       break;
8154
8155     case NE:
8156       fputs ("c1t", file);
8157       break;
8158
8159     default:
8160       output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8161       break;
8162     }
8163 }
8164
8165 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P.  */
8166
8167 static bool
8168 mips_print_operand_punct_valid_p (unsigned char code)
8169 {
8170   return mips_print_operand_punct[code];
8171 }
8172
8173 /* Implement TARGET_PRINT_OPERAND.  The MIPS-specific operand codes are:
8174
8175    'X'  Print CONST_INT OP in hexadecimal format.
8176    'x'  Print the low 16 bits of CONST_INT OP in hexadecimal format.
8177    'd'  Print CONST_INT OP in decimal.
8178    'm'  Print one less than CONST_INT OP in decimal.
8179    'h'  Print the high-part relocation associated with OP, after stripping
8180           any outermost HIGH.
8181    'R'  Print the low-part relocation associated with OP.
8182    'C'  Print the integer branch condition for comparison OP.
8183    'N'  Print the inverse of the integer branch condition for comparison OP.
8184    'F'  Print the FPU branch condition for comparison OP.
8185    'W'  Print the inverse of the FPU branch condition for comparison OP.
8186    'T'  Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
8187               'z' for (eq:?I ...), 'n' for (ne:?I ...).
8188    't'  Like 'T', but with the EQ/NE cases reversed
8189    'Y'  Print mips_fp_conditions[INTVAL (OP)]
8190    'Z'  Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
8191    'q'  Print a DSP accumulator register.
8192    'D'  Print the second part of a double-word register or memory operand.
8193    'L'  Print the low-order register in a double-word register operand.
8194    'M'  Print high-order register in a double-word register operand.
8195    'z'  Print $0 if OP is zero, otherwise print OP normally.
8196    'b'  Print the address of a memory operand, without offset.  */
8197
8198 static void
8199 mips_print_operand (FILE *file, rtx op, int letter)
8200 {
8201   enum rtx_code code;
8202
8203   if (mips_print_operand_punct_valid_p (letter))
8204     {
8205       mips_print_operand_punctuation (file, letter);
8206       return;
8207     }
8208
8209   gcc_assert (op);
8210   code = GET_CODE (op);
8211
8212   switch (letter)
8213     {
8214     case 'X':
8215       if (CONST_INT_P (op))
8216         fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
8217       else
8218         output_operand_lossage ("invalid use of '%%%c'", letter);
8219       break;
8220
8221     case 'x':
8222       if (CONST_INT_P (op))
8223         fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
8224       else
8225         output_operand_lossage ("invalid use of '%%%c'", letter);
8226       break;
8227
8228     case 'd':
8229       if (CONST_INT_P (op))
8230         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
8231       else
8232         output_operand_lossage ("invalid use of '%%%c'", letter);
8233       break;
8234
8235     case 'm':
8236       if (CONST_INT_P (op))
8237         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
8238       else
8239         output_operand_lossage ("invalid use of '%%%c'", letter);
8240       break;
8241
8242     case 'h':
8243       if (code == HIGH)
8244         op = XEXP (op, 0);
8245       mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
8246       break;
8247
8248     case 'R':
8249       mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
8250       break;
8251
8252     case 'C':
8253       mips_print_int_branch_condition (file, code, letter);
8254       break;
8255
8256     case 'N':
8257       mips_print_int_branch_condition (file, reverse_condition (code), letter);
8258       break;
8259
8260     case 'F':
8261       mips_print_float_branch_condition (file, code, letter);
8262       break;
8263
8264     case 'W':
8265       mips_print_float_branch_condition (file, reverse_condition (code),
8266                                          letter);
8267       break;
8268
8269     case 'T':
8270     case 't':
8271       {
8272         int truth = (code == NE) == (letter == 'T');
8273         fputc ("zfnt"[truth * 2 + ST_REG_P (REGNO (XEXP (op, 0)))], file);
8274       }
8275       break;
8276
8277     case 'Y':
8278       if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
8279         fputs (mips_fp_conditions[UINTVAL (op)], file);
8280       else
8281         output_operand_lossage ("'%%%c' is not a valid operand prefix",
8282                                 letter);
8283       break;
8284
8285     case 'Z':
8286       if (ISA_HAS_8CC)
8287         {
8288           mips_print_operand (file, op, 0);
8289           fputc (',', file);
8290         }
8291       break;
8292
8293     case 'q':
8294       if (code == REG && MD_REG_P (REGNO (op)))
8295         fprintf (file, "$ac0");
8296       else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
8297         fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
8298       else
8299         output_operand_lossage ("invalid use of '%%%c'", letter);
8300       break;
8301
8302     default:
8303       switch (code)
8304         {
8305         case REG:
8306           {
8307             unsigned int regno = REGNO (op);
8308             if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
8309                 || (letter == 'L' && TARGET_BIG_ENDIAN)
8310                 || letter == 'D')
8311               regno++;
8312             else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
8313               output_operand_lossage ("invalid use of '%%%c'", letter);
8314             /* We need to print $0 .. $31 for COP0 registers.  */
8315             if (COP0_REG_P (regno))
8316               fprintf (file, "$%s", &reg_names[regno][4]);
8317             else
8318               fprintf (file, "%s", reg_names[regno]);
8319           }
8320           break;
8321
8322         case MEM:
8323           if (letter == 'D')
8324             output_address (plus_constant (Pmode, XEXP (op, 0), 4));
8325           else if (letter == 'b')
8326             {
8327               gcc_assert (REG_P (XEXP (op, 0)));
8328               mips_print_operand (file, XEXP (op, 0), 0);
8329             }
8330           else if (letter && letter != 'z')
8331             output_operand_lossage ("invalid use of '%%%c'", letter);
8332           else
8333             output_address (XEXP (op, 0));
8334           break;
8335
8336         default:
8337           if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
8338             fputs (reg_names[GP_REG_FIRST], file);
8339           else if (letter && letter != 'z')
8340             output_operand_lossage ("invalid use of '%%%c'", letter);
8341           else if (CONST_GP_P (op))
8342             fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
8343           else
8344             output_addr_const (file, mips_strip_unspec_address (op));
8345           break;
8346         }
8347     }
8348 }
8349
8350 /* Implement TARGET_PRINT_OPERAND_ADDRESS.  */
8351
8352 static void
8353 mips_print_operand_address (FILE *file, rtx x)
8354 {
8355   struct mips_address_info addr;
8356
8357   if (mips_classify_address (&addr, x, word_mode, true))
8358     switch (addr.type)
8359       {
8360       case ADDRESS_REG:
8361         mips_print_operand (file, addr.offset, 0);
8362         fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8363         return;
8364
8365       case ADDRESS_LO_SUM:
8366         mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
8367                                   mips_lo_relocs);
8368         fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8369         return;
8370
8371       case ADDRESS_CONST_INT:
8372         output_addr_const (file, x);
8373         fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
8374         return;
8375
8376       case ADDRESS_SYMBOLIC:
8377         output_addr_const (file, mips_strip_unspec_address (x));
8378         return;
8379       }
8380   gcc_unreachable ();
8381 }
8382 \f
8383 /* Implement TARGET_ENCODE_SECTION_INFO.  */
8384
8385 static void
8386 mips_encode_section_info (tree decl, rtx rtl, int first)
8387 {
8388   default_encode_section_info (decl, rtl, first);
8389
8390   if (TREE_CODE (decl) == FUNCTION_DECL)
8391     {
8392       rtx symbol = XEXP (rtl, 0);
8393       tree type = TREE_TYPE (decl);
8394
8395       /* Encode whether the symbol is short or long.  */
8396       if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
8397           || mips_far_type_p (type))
8398         SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
8399     }
8400 }
8401
8402 /* Implement TARGET_SELECT_RTX_SECTION.  */
8403
8404 static section *
8405 mips_select_rtx_section (enum machine_mode mode, rtx x,
8406                          unsigned HOST_WIDE_INT align)
8407 {
8408   /* ??? Consider using mergeable small data sections.  */
8409   if (mips_rtx_constant_in_small_data_p (mode))
8410     return get_named_section (NULL, ".sdata", 0);
8411
8412   return default_elf_select_rtx_section (mode, x, align);
8413 }
8414
8415 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
8416
8417    The complication here is that, with the combination TARGET_ABICALLS
8418    && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
8419    absolute addresses, and should therefore not be included in the
8420    read-only part of a DSO.  Handle such cases by selecting a normal
8421    data section instead of a read-only one.  The logic apes that in
8422    default_function_rodata_section.  */
8423
8424 static section *
8425 mips_function_rodata_section (tree decl)
8426 {
8427   if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
8428     return default_function_rodata_section (decl);
8429
8430   if (decl && DECL_SECTION_NAME (decl))
8431     {
8432       const char *name = DECL_SECTION_NAME (decl);
8433       if (DECL_COMDAT_GROUP (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
8434         {
8435           char *rname = ASTRDUP (name);
8436           rname[14] = 'd';
8437           return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
8438         }
8439       else if (flag_function_sections
8440                && flag_data_sections
8441                && strncmp (name, ".text.", 6) == 0)
8442         {
8443           char *rname = ASTRDUP (name);
8444           memcpy (rname + 1, "data", 4);
8445           return get_section (rname, SECTION_WRITE, decl);
8446         }
8447     }
8448   return data_section;
8449 }
8450
8451 /* Implement TARGET_IN_SMALL_DATA_P.  */
8452
8453 static bool
8454 mips_in_small_data_p (const_tree decl)
8455 {
8456   unsigned HOST_WIDE_INT size;
8457
8458   if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
8459     return false;
8460
8461   /* We don't yet generate small-data references for -mabicalls
8462      or VxWorks RTP code.  See the related -G handling in
8463      mips_option_override.  */
8464   if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
8465     return false;
8466
8467   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
8468     {
8469       const char *name;
8470
8471       /* Reject anything that isn't in a known small-data section.  */
8472       name = DECL_SECTION_NAME (decl);
8473       if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
8474         return false;
8475
8476       /* If a symbol is defined externally, the assembler will use the
8477          usual -G rules when deciding how to implement macros.  */
8478       if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
8479         return true;
8480     }
8481   else if (TARGET_EMBEDDED_DATA)
8482     {
8483       /* Don't put constants into the small data section: we want them
8484          to be in ROM rather than RAM.  */
8485       if (TREE_CODE (decl) != VAR_DECL)
8486         return false;
8487
8488       if (TREE_READONLY (decl)
8489           && !TREE_SIDE_EFFECTS (decl)
8490           && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
8491         return false;
8492     }
8493
8494   /* Enforce -mlocal-sdata.  */
8495   if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
8496     return false;
8497
8498   /* Enforce -mextern-sdata.  */
8499   if (!TARGET_EXTERN_SDATA && DECL_P (decl))
8500     {
8501       if (DECL_EXTERNAL (decl))
8502         return false;
8503       if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
8504         return false;
8505     }
8506
8507   /* We have traditionally not treated zero-sized objects as small data,
8508      so this is now effectively part of the ABI.  */
8509   size = int_size_in_bytes (TREE_TYPE (decl));
8510   return size > 0 && size <= mips_small_data_threshold;
8511 }
8512
8513 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P.  We don't want to use
8514    anchors for small data: the GP register acts as an anchor in that
8515    case.  We also don't want to use them for PC-relative accesses,
8516    where the PC acts as an anchor.  */
8517
8518 static bool
8519 mips_use_anchors_for_symbol_p (const_rtx symbol)
8520 {
8521   switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
8522     {
8523     case SYMBOL_PC_RELATIVE:
8524     case SYMBOL_GP_RELATIVE:
8525       return false;
8526
8527     default:
8528       return default_use_anchors_for_symbol_p (symbol);
8529     }
8530 }
8531 \f
8532 /* The MIPS debug format wants all automatic variables and arguments
8533    to be in terms of the virtual frame pointer (stack pointer before
8534    any adjustment in the function), while the MIPS 3.0 linker wants
8535    the frame pointer to be the stack pointer after the initial
8536    adjustment.  So, we do the adjustment here.  The arg pointer (which
8537    is eliminated) points to the virtual frame pointer, while the frame
8538    pointer (which may be eliminated) points to the stack pointer after
8539    the initial adjustments.  */
8540
8541 HOST_WIDE_INT
8542 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
8543 {
8544   rtx offset2 = const0_rtx;
8545   rtx reg = eliminate_constant_term (addr, &offset2);
8546
8547   if (offset == 0)
8548     offset = INTVAL (offset2);
8549
8550   if (reg == stack_pointer_rtx
8551       || reg == frame_pointer_rtx
8552       || reg == hard_frame_pointer_rtx)
8553     {
8554       offset -= cfun->machine->frame.total_size;
8555       if (reg == hard_frame_pointer_rtx)
8556         offset += cfun->machine->frame.hard_frame_pointer_offset;
8557     }
8558
8559   return offset;
8560 }
8561 \f
8562 /* Implement ASM_OUTPUT_EXTERNAL.  */
8563
8564 void
8565 mips_output_external (FILE *file, tree decl, const char *name)
8566 {
8567   default_elf_asm_output_external (file, decl, name);
8568
8569   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8570      set in order to avoid putting out names that are never really
8571      used. */
8572   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
8573     {
8574       if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
8575         {
8576           /* When using assembler macros, emit .extern directives for
8577              all small-data externs so that the assembler knows how
8578              big they are.
8579
8580              In most cases it would be safe (though pointless) to emit
8581              .externs for other symbols too.  One exception is when an
8582              object is within the -G limit but declared by the user to
8583              be in a section other than .sbss or .sdata.  */
8584           fputs ("\t.extern\t", file);
8585           assemble_name (file, name);
8586           fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
8587                    int_size_in_bytes (TREE_TYPE (decl)));
8588         }
8589     }
8590 }
8591
8592 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME.  */
8593
8594 static void
8595 mips_output_filename (FILE *stream, const char *name)
8596 {
8597   /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
8598      directives.  */
8599   if (write_symbols == DWARF2_DEBUG)
8600     return;
8601   else if (mips_output_filename_first_time)
8602     {
8603       mips_output_filename_first_time = 0;
8604       num_source_filenames += 1;
8605       current_function_file = name;
8606       fprintf (stream, "\t.file\t%d ", num_source_filenames);
8607       output_quoted_string (stream, name);
8608       putc ('\n', stream);
8609     }
8610   /* If we are emitting stabs, let dbxout.c handle this (except for
8611      the mips_output_filename_first_time case).  */
8612   else if (write_symbols == DBX_DEBUG)
8613     return;
8614   else if (name != current_function_file
8615            && strcmp (name, current_function_file) != 0)
8616     {
8617       num_source_filenames += 1;
8618       current_function_file = name;
8619       fprintf (stream, "\t.file\t%d ", num_source_filenames);
8620       output_quoted_string (stream, name);
8621       putc ('\n', stream);
8622     }
8623 }
8624
8625 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
8626
8627 static void ATTRIBUTE_UNUSED
8628 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
8629 {
8630   switch (size)
8631     {
8632     case 4:
8633       fputs ("\t.dtprelword\t", file);
8634       break;
8635
8636     case 8:
8637       fputs ("\t.dtpreldword\t", file);
8638       break;
8639
8640     default:
8641       gcc_unreachable ();
8642     }
8643   output_addr_const (file, x);
8644   fputs ("+0x8000", file);
8645 }
8646
8647 /* Implement TARGET_DWARF_REGISTER_SPAN.  */
8648
8649 static rtx
8650 mips_dwarf_register_span (rtx reg)
8651 {
8652   rtx high, low;
8653   enum machine_mode mode;
8654
8655   /* By default, GCC maps increasing register numbers to increasing
8656      memory locations, but paired FPRs are always little-endian,
8657      regardless of the prevailing endianness.  */
8658   mode = GET_MODE (reg);
8659   if (FP_REG_P (REGNO (reg))
8660       && TARGET_BIG_ENDIAN
8661       && MAX_FPRS_PER_FMT > 1
8662       && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8663     {
8664       gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
8665       high = mips_subword (reg, true);
8666       low = mips_subword (reg, false);
8667       return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
8668     }
8669
8670   return NULL_RTX;
8671 }
8672
8673 /* DSP ALU can bypass data with no delays for the following pairs. */
8674 enum insn_code dspalu_bypass_table[][2] =
8675 {
8676   {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
8677   {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
8678   {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
8679   {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
8680   {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
8681   {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
8682   {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
8683   {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
8684 };
8685
8686 int
8687 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
8688 {
8689   int i;
8690   int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
8691   enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
8692   enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
8693
8694   for (i = 0; i < num_bypass; i++)
8695     {
8696       if (out_icode == dspalu_bypass_table[i][0]
8697           && in_icode == dspalu_bypass_table[i][1])
8698        return true;
8699     }
8700
8701   return false;
8702 }
8703 /* Implement ASM_OUTPUT_ASCII.  */
8704
8705 void
8706 mips_output_ascii (FILE *stream, const char *string, size_t len)
8707 {
8708   size_t i;
8709   int cur_pos;
8710
8711   cur_pos = 17;
8712   fprintf (stream, "\t.ascii\t\"");
8713   for (i = 0; i < len; i++)
8714     {
8715       int c;
8716
8717       c = (unsigned char) string[i];
8718       if (ISPRINT (c))
8719         {
8720           if (c == '\\' || c == '\"')
8721             {
8722               putc ('\\', stream);
8723               cur_pos++;
8724             }
8725           putc (c, stream);
8726           cur_pos++;
8727         }
8728       else
8729         {
8730           fprintf (stream, "\\%03o", c);
8731           cur_pos += 4;
8732         }
8733
8734       if (cur_pos > 72 && i+1 < len)
8735         {
8736           cur_pos = 17;
8737           fprintf (stream, "\"\n\t.ascii\t\"");
8738         }
8739     }
8740   fprintf (stream, "\"\n");
8741 }
8742
8743 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
8744    Update *ADDR with the operand that should be printed.  */
8745
8746 const char *
8747 mips_output_tls_reloc_directive (rtx *addr)
8748 {
8749   enum mips_symbol_type type;
8750
8751   type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
8752   *addr = mips_strip_unspec_address (*addr);
8753   switch (type)
8754     {
8755     case SYMBOL_DTPREL:
8756       return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
8757
8758     case SYMBOL_TPREL:
8759       return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
8760
8761     default:
8762       gcc_unreachable ();
8763     }
8764 }
8765
8766 /* Emit either a label, .comm, or .lcomm directive.  When using assembler
8767    macros, mark the symbol as written so that mips_asm_output_external
8768    won't emit an .extern for it.  STREAM is the output file, NAME is the
8769    name of the symbol, INIT_STRING is the string that should be written
8770    before the symbol and FINAL_STRING is the string that should be
8771    written after it.  FINAL_STRING is a printf format that consumes the
8772    remaining arguments.  */
8773
8774 void
8775 mips_declare_object (FILE *stream, const char *name, const char *init_string,
8776                      const char *final_string, ...)
8777 {
8778   va_list ap;
8779
8780   fputs (init_string, stream);
8781   assemble_name (stream, name);
8782   va_start (ap, final_string);
8783   vfprintf (stream, final_string, ap);
8784   va_end (ap);
8785
8786   if (!TARGET_EXPLICIT_RELOCS)
8787     {
8788       tree name_tree = get_identifier (name);
8789       TREE_ASM_WRITTEN (name_tree) = 1;
8790     }
8791 }
8792
8793 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
8794    NAME is the name of the object and ALIGN is the required alignment
8795    in bytes.  TAKES_ALIGNMENT_P is true if the directive takes a third
8796    alignment argument.  */
8797
8798 void
8799 mips_declare_common_object (FILE *stream, const char *name,
8800                             const char *init_string,
8801                             unsigned HOST_WIDE_INT size,
8802                             unsigned int align, bool takes_alignment_p)
8803 {
8804   if (!takes_alignment_p)
8805     {
8806       size += (align / BITS_PER_UNIT) - 1;
8807       size -= size % (align / BITS_PER_UNIT);
8808       mips_declare_object (stream, name, init_string,
8809                            "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
8810     }
8811   else
8812     mips_declare_object (stream, name, init_string,
8813                          "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
8814                          size, align / BITS_PER_UNIT);
8815 }
8816
8817 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON.  This is usually the same as the
8818    elfos.h version, but we also need to handle -muninit-const-in-rodata.  */
8819
8820 void
8821 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
8822                                  unsigned HOST_WIDE_INT size,
8823                                  unsigned int align)
8824 {
8825   /* If the target wants uninitialized const declarations in
8826      .rdata then don't put them in .comm.  */
8827   if (TARGET_EMBEDDED_DATA
8828       && TARGET_UNINIT_CONST_IN_RODATA
8829       && TREE_CODE (decl) == VAR_DECL
8830       && TREE_READONLY (decl)
8831       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
8832     {
8833       if (TREE_PUBLIC (decl) && DECL_NAME (decl))
8834         targetm.asm_out.globalize_label (stream, name);
8835
8836       switch_to_section (readonly_data_section);
8837       ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
8838       mips_declare_object (stream, name, "",
8839                            ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
8840                            size);
8841     }
8842   else
8843     mips_declare_common_object (stream, name, "\n\t.comm\t",
8844                                 size, align, true);
8845 }
8846
8847 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
8848 extern int size_directive_output;
8849
8850 /* Implement ASM_DECLARE_OBJECT_NAME.  This is like most of the standard ELF
8851    definitions except that it uses mips_declare_object to emit the label.  */
8852
8853 void
8854 mips_declare_object_name (FILE *stream, const char *name,
8855                           tree decl ATTRIBUTE_UNUSED)
8856 {
8857 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
8858   ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
8859 #endif
8860
8861   size_directive_output = 0;
8862   if (!flag_inhibit_size_directive && DECL_SIZE (decl))
8863     {
8864       HOST_WIDE_INT size;
8865
8866       size_directive_output = 1;
8867       size = int_size_in_bytes (TREE_TYPE (decl));
8868       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8869     }
8870
8871   mips_declare_object (stream, name, "", ":\n");
8872 }
8873
8874 /* Implement ASM_FINISH_DECLARE_OBJECT.  This is generic ELF stuff.  */
8875
8876 void
8877 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
8878 {
8879   const char *name;
8880
8881   name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8882   if (!flag_inhibit_size_directive
8883       && DECL_SIZE (decl) != 0
8884       && !at_end
8885       && top_level
8886       && DECL_INITIAL (decl) == error_mark_node
8887       && !size_directive_output)
8888     {
8889       HOST_WIDE_INT size;
8890
8891       size_directive_output = 1;
8892       size = int_size_in_bytes (TREE_TYPE (decl));
8893       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8894     }
8895 }
8896 #endif
8897 \f
8898 /* Return the FOO in the name of the ".mdebug.FOO" section associated
8899    with the current ABI.  */
8900
8901 static const char *
8902 mips_mdebug_abi_name (void)
8903 {
8904   switch (mips_abi)
8905     {
8906     case ABI_32:
8907       return "abi32";
8908     case ABI_O64:
8909       return "abiO64";
8910     case ABI_N32:
8911       return "abiN32";
8912     case ABI_64:
8913       return "abi64";
8914     case ABI_EABI:
8915       return TARGET_64BIT ? "eabi64" : "eabi32";
8916     default:
8917       gcc_unreachable ();
8918     }
8919 }
8920
8921 /* Implement TARGET_ASM_FILE_START.  */
8922
8923 static void
8924 mips_file_start (void)
8925 {
8926   default_file_start ();
8927
8928   /* Generate a special section to describe the ABI switches used to
8929      produce the resultant binary.  */
8930
8931   /* Record the ABI itself.  Modern versions of binutils encode
8932      this information in the ELF header flags, but GDB needs the
8933      information in order to correctly debug binaries produced by
8934      older binutils.  See the function mips_gdbarch_init in
8935      gdb/mips-tdep.c.  */
8936   fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
8937            mips_mdebug_abi_name ());
8938
8939   /* There is no ELF header flag to distinguish long32 forms of the
8940      EABI from long64 forms.  Emit a special section to help tools
8941      such as GDB.  Do the same for o64, which is sometimes used with
8942      -mlong64.  */
8943   if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
8944     fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
8945              "\t.previous\n", TARGET_LONG64 ? 64 : 32);
8946
8947   /* Record the NaN encoding.  */
8948   if (HAVE_AS_NAN || mips_nan != MIPS_IEEE_754_DEFAULT)
8949     fprintf (asm_out_file, "\t.nan\t%s\n",
8950              mips_nan == MIPS_IEEE_754_2008 ? "2008" : "legacy");
8951
8952 #ifdef HAVE_AS_GNU_ATTRIBUTE
8953   {
8954     int attr;
8955
8956     /* No floating-point operations, -mno-float.  */
8957     if (TARGET_NO_FLOAT)
8958       attr = 0;
8959     /* Soft-float code, -msoft-float.  */
8960     else if (!TARGET_HARD_FLOAT_ABI)
8961       attr = 3;
8962     /* Single-float code, -msingle-float.  */
8963     else if (!TARGET_DOUBLE_FLOAT)
8964       attr = 2;
8965     /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64.  */
8966     else if (!TARGET_64BIT && TARGET_FLOAT64)
8967       attr = 4;
8968     /* Regular FP code, FP regs same size as GP regs, -mdouble-float.  */
8969     else
8970       attr = 1;
8971
8972     fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
8973   }
8974 #endif
8975
8976   /* If TARGET_ABICALLS, tell GAS to generate -KPIC code.  */
8977   if (TARGET_ABICALLS)
8978     {
8979       fprintf (asm_out_file, "\t.abicalls\n");
8980       if (TARGET_ABICALLS_PIC0)
8981         fprintf (asm_out_file, "\t.option\tpic0\n");
8982     }
8983
8984   if (flag_verbose_asm)
8985     fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
8986              ASM_COMMENT_START,
8987              mips_small_data_threshold, mips_arch_info->name, mips_isa);
8988 }
8989
8990 /* Implement TARGET_ASM_CODE_END.  */
8991
8992 static void
8993 mips_code_end (void)
8994 {
8995   mips_finish_stub (&mips16_rdhwr_stub);
8996   mips_finish_stub (&mips16_get_fcsr_stub);
8997   mips_finish_stub (&mips16_set_fcsr_stub);
8998 }
8999 \f
9000 /* Make the last instruction frame-related and note that it performs
9001    the operation described by FRAME_PATTERN.  */
9002
9003 static void
9004 mips_set_frame_expr (rtx frame_pattern)
9005 {
9006   rtx_insn *insn;
9007
9008   insn = get_last_insn ();
9009   RTX_FRAME_RELATED_P (insn) = 1;
9010   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9011                                       frame_pattern,
9012                                       REG_NOTES (insn));
9013 }
9014
9015 /* Return a frame-related rtx that stores REG at MEM.
9016    REG must be a single register.  */
9017
9018 static rtx
9019 mips_frame_set (rtx mem, rtx reg)
9020 {
9021   rtx set;
9022
9023   set = gen_rtx_SET (VOIDmode, mem, reg);
9024   RTX_FRAME_RELATED_P (set) = 1;
9025
9026   return set;
9027 }
9028
9029 /* Record that the epilogue has restored call-saved register REG.  */
9030
9031 static void
9032 mips_add_cfa_restore (rtx reg)
9033 {
9034   mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
9035                                                mips_epilogue.cfa_restores);
9036 }
9037 \f
9038 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
9039    mips16e_s2_s8_regs[X], it must also save the registers in indexes
9040    X + 1 onwards.  Likewise mips16e_a0_a3_regs.  */
9041 static const unsigned char mips16e_s2_s8_regs[] = {
9042   30, 23, 22, 21, 20, 19, 18
9043 };
9044 static const unsigned char mips16e_a0_a3_regs[] = {
9045   4, 5, 6, 7
9046 };
9047
9048 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
9049    ordered from the uppermost in memory to the lowest in memory.  */
9050 static const unsigned char mips16e_save_restore_regs[] = {
9051   31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
9052 };
9053
9054 /* Return the index of the lowest X in the range [0, SIZE) for which
9055    bit REGS[X] is set in MASK.  Return SIZE if there is no such X.  */
9056
9057 static unsigned int
9058 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
9059                              unsigned int size)
9060 {
9061   unsigned int i;
9062
9063   for (i = 0; i < size; i++)
9064     if (BITSET_P (mask, regs[i]))
9065       break;
9066
9067   return i;
9068 }
9069
9070 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
9071    is the number of set bits.  If *MASK_PTR contains REGS[X] for some X
9072    in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
9073    is true for all indexes (X, SIZE).  */
9074
9075 static void
9076 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
9077                         unsigned int size, unsigned int *num_regs_ptr)
9078 {
9079   unsigned int i;
9080
9081   i = mips16e_find_first_register (*mask_ptr, regs, size);
9082   for (i++; i < size; i++)
9083     if (!BITSET_P (*mask_ptr, regs[i]))
9084       {
9085         *num_regs_ptr += 1;
9086         *mask_ptr |= 1 << regs[i];
9087       }
9088 }
9089
9090 /* Return a simplified form of X using the register values in REG_VALUES.
9091    REG_VALUES[R] is the last value assigned to hard register R, or null
9092    if R has not been modified.
9093
9094    This function is rather limited, but is good enough for our purposes.  */
9095
9096 static rtx
9097 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
9098 {
9099   x = avoid_constant_pool_reference (x);
9100
9101   if (UNARY_P (x))
9102     {
9103       rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9104       return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
9105                                  x0, GET_MODE (XEXP (x, 0)));
9106     }
9107
9108   if (ARITHMETIC_P (x))
9109     {
9110       rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9111       rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
9112       return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
9113     }
9114
9115   if (REG_P (x)
9116       && reg_values[REGNO (x)]
9117       && !rtx_unstable_p (reg_values[REGNO (x)]))
9118     return reg_values[REGNO (x)];
9119
9120   return x;
9121 }
9122
9123 /* Return true if (set DEST SRC) stores an argument register into its
9124    caller-allocated save slot, storing the number of that argument
9125    register in *REGNO_PTR if so.  REG_VALUES is as for
9126    mips16e_collect_propagate_value.  */
9127
9128 static bool
9129 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
9130                                  unsigned int *regno_ptr)
9131 {
9132   unsigned int argno, regno;
9133   HOST_WIDE_INT offset, required_offset;
9134   rtx addr, base;
9135
9136   /* Check that this is a word-mode store.  */
9137   if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
9138     return false;
9139
9140   /* Check that the register being saved is an unmodified argument
9141      register.  */
9142   regno = REGNO (src);
9143   if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
9144     return false;
9145   argno = regno - GP_ARG_FIRST;
9146
9147   /* Check whether the address is an appropriate stack-pointer or
9148      frame-pointer access.  */
9149   addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
9150   mips_split_plus (addr, &base, &offset);
9151   required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
9152   if (base == hard_frame_pointer_rtx)
9153     required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
9154   else if (base != stack_pointer_rtx)
9155     return false;
9156   if (offset != required_offset)
9157     return false;
9158
9159   *regno_ptr = regno;
9160   return true;
9161 }
9162
9163 /* A subroutine of mips_expand_prologue, called only when generating
9164    MIPS16e SAVE instructions.  Search the start of the function for any
9165    instructions that save argument registers into their caller-allocated
9166    save slots.  Delete such instructions and return a value N such that
9167    saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
9168    instructions redundant.  */
9169
9170 static unsigned int
9171 mips16e_collect_argument_saves (void)
9172 {
9173   rtx reg_values[FIRST_PSEUDO_REGISTER];
9174   rtx_insn *insn, *next;
9175   rtx set, dest, src;
9176   unsigned int nargs, regno;
9177
9178   push_topmost_sequence ();
9179   nargs = 0;
9180   memset (reg_values, 0, sizeof (reg_values));
9181   for (insn = get_insns (); insn; insn = next)
9182     {
9183       next = NEXT_INSN (insn);
9184       if (NOTE_P (insn) || DEBUG_INSN_P (insn))
9185         continue;
9186
9187       if (!INSN_P (insn))
9188         break;
9189
9190       set = PATTERN (insn);
9191       if (GET_CODE (set) != SET)
9192         break;
9193
9194       dest = SET_DEST (set);
9195       src = SET_SRC (set);
9196       if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
9197         {
9198           if (!BITSET_P (cfun->machine->frame.mask, regno))
9199             {
9200               delete_insn (insn);
9201               nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
9202             }
9203         }
9204       else if (REG_P (dest) && GET_MODE (dest) == word_mode)
9205         reg_values[REGNO (dest)]
9206           = mips16e_collect_propagate_value (src, reg_values);
9207       else
9208         break;
9209     }
9210   pop_topmost_sequence ();
9211
9212   return nargs;
9213 }
9214
9215 /* Return a move between register REGNO and memory location SP + OFFSET.
9216    REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
9217    Make the move a load if RESTORE_P, otherwise make it a store.  */
9218
9219 static rtx
9220 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
9221                           HOST_WIDE_INT offset, unsigned int regno)
9222 {
9223   rtx reg, mem;
9224
9225   mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
9226                                               offset));
9227   reg = gen_rtx_REG (SImode, regno);
9228   if (restore_p)
9229     {
9230       mips_add_cfa_restore (reg);
9231       return gen_rtx_SET (VOIDmode, reg, mem);
9232     }
9233   if (reg_parm_p)
9234     return gen_rtx_SET (VOIDmode, mem, reg);
9235   return mips_frame_set (mem, reg);
9236 }
9237
9238 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
9239    The instruction must:
9240
9241      - Allocate or deallocate SIZE bytes in total; SIZE is known
9242        to be nonzero.
9243
9244      - Save or restore as many registers in *MASK_PTR as possible.
9245        The instruction saves the first registers at the top of the
9246        allocated area, with the other registers below it.
9247
9248      - Save NARGS argument registers above the allocated area.
9249
9250    (NARGS is always zero if RESTORE_P.)
9251
9252    The SAVE and RESTORE instructions cannot save and restore all general
9253    registers, so there may be some registers left over for the caller to
9254    handle.  Destructively modify *MASK_PTR so that it contains the registers
9255    that still need to be saved or restored.  The caller can save these
9256    registers in the memory immediately below *OFFSET_PTR, which is a
9257    byte offset from the bottom of the allocated stack area.  */
9258
9259 static rtx
9260 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
9261                             HOST_WIDE_INT *offset_ptr, unsigned int nargs,
9262                             HOST_WIDE_INT size)
9263 {
9264   rtx pattern, set;
9265   HOST_WIDE_INT offset, top_offset;
9266   unsigned int i, regno;
9267   int n;
9268
9269   gcc_assert (cfun->machine->frame.num_fp == 0);
9270
9271   /* Calculate the number of elements in the PARALLEL.  We need one element
9272      for the stack adjustment, one for each argument register save, and one
9273      for each additional register move.  */
9274   n = 1 + nargs;
9275   for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9276     if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
9277       n++;
9278
9279   /* Create the final PARALLEL.  */
9280   pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
9281   n = 0;
9282
9283   /* Add the stack pointer adjustment.  */
9284   set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9285                      plus_constant (Pmode, stack_pointer_rtx,
9286                                     restore_p ? size : -size));
9287   RTX_FRAME_RELATED_P (set) = 1;
9288   XVECEXP (pattern, 0, n++) = set;
9289
9290   /* Stack offsets in the PARALLEL are relative to the old stack pointer.  */
9291   top_offset = restore_p ? size : 0;
9292
9293   /* Save the arguments.  */
9294   for (i = 0; i < nargs; i++)
9295     {
9296       offset = top_offset + i * UNITS_PER_WORD;
9297       set = mips16e_save_restore_reg (restore_p, true, offset,
9298                                       GP_ARG_FIRST + i);
9299       XVECEXP (pattern, 0, n++) = set;
9300     }
9301
9302   /* Then fill in the other register moves.  */
9303   offset = top_offset;
9304   for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9305     {
9306       regno = mips16e_save_restore_regs[i];
9307       if (BITSET_P (*mask_ptr, regno))
9308         {
9309           offset -= UNITS_PER_WORD;
9310           set = mips16e_save_restore_reg (restore_p, false, offset, regno);
9311           XVECEXP (pattern, 0, n++) = set;
9312           *mask_ptr &= ~(1 << regno);
9313         }
9314     }
9315
9316   /* Tell the caller what offset it should use for the remaining registers.  */
9317   *offset_ptr = size + (offset - top_offset);
9318
9319   gcc_assert (n == XVECLEN (pattern, 0));
9320
9321   return pattern;
9322 }
9323
9324 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
9325    pointer.  Return true if PATTERN matches the kind of instruction
9326    generated by mips16e_build_save_restore.  If INFO is nonnull,
9327    initialize it when returning true.  */
9328
9329 bool
9330 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
9331                                 struct mips16e_save_restore_info *info)
9332 {
9333   unsigned int i, nargs, mask, extra;
9334   HOST_WIDE_INT top_offset, save_offset, offset;
9335   rtx set, reg, mem, base;
9336   int n;
9337
9338   if (!GENERATE_MIPS16E_SAVE_RESTORE)
9339     return false;
9340
9341   /* Stack offsets in the PARALLEL are relative to the old stack pointer.  */
9342   top_offset = adjust > 0 ? adjust : 0;
9343
9344   /* Interpret all other members of the PARALLEL.  */
9345   save_offset = top_offset - UNITS_PER_WORD;
9346   mask = 0;
9347   nargs = 0;
9348   i = 0;
9349   for (n = 1; n < XVECLEN (pattern, 0); n++)
9350     {
9351       /* Check that we have a SET.  */
9352       set = XVECEXP (pattern, 0, n);
9353       if (GET_CODE (set) != SET)
9354         return false;
9355
9356       /* Check that the SET is a load (if restoring) or a store
9357          (if saving).  */
9358       mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
9359       if (!MEM_P (mem))
9360         return false;
9361
9362       /* Check that the address is the sum of the stack pointer and a
9363          possibly-zero constant offset.  */
9364       mips_split_plus (XEXP (mem, 0), &base, &offset);
9365       if (base != stack_pointer_rtx)
9366         return false;
9367
9368       /* Check that SET's other operand is a register.  */
9369       reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
9370       if (!REG_P (reg))
9371         return false;
9372
9373       /* Check for argument saves.  */
9374       if (offset == top_offset + nargs * UNITS_PER_WORD
9375           && REGNO (reg) == GP_ARG_FIRST + nargs)
9376         nargs++;
9377       else if (offset == save_offset)
9378         {
9379           while (mips16e_save_restore_regs[i++] != REGNO (reg))
9380             if (i == ARRAY_SIZE (mips16e_save_restore_regs))
9381               return false;
9382
9383           mask |= 1 << REGNO (reg);
9384           save_offset -= UNITS_PER_WORD;
9385         }
9386       else
9387         return false;
9388     }
9389
9390   /* Check that the restrictions on register ranges are met.  */
9391   extra = 0;
9392   mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
9393                           ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
9394   mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
9395                           ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
9396   if (extra != 0)
9397     return false;
9398
9399   /* Make sure that the topmost argument register is not saved twice.
9400      The checks above ensure that the same is then true for the other
9401      argument registers.  */
9402   if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
9403     return false;
9404
9405   /* Pass back information, if requested.  */
9406   if (info)
9407     {
9408       info->nargs = nargs;
9409       info->mask = mask;
9410       info->size = (adjust > 0 ? adjust : -adjust);
9411     }
9412
9413   return true;
9414 }
9415
9416 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
9417    for the register range [MIN_REG, MAX_REG].  Return a pointer to
9418    the null terminator.  */
9419
9420 static char *
9421 mips16e_add_register_range (char *s, unsigned int min_reg,
9422                             unsigned int max_reg)
9423 {
9424   if (min_reg != max_reg)
9425     s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
9426   else
9427     s += sprintf (s, ",%s", reg_names[min_reg]);
9428   return s;
9429 }
9430
9431 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
9432    PATTERN and ADJUST are as for mips16e_save_restore_pattern_p.  */
9433
9434 const char *
9435 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
9436 {
9437   static char buffer[300];
9438
9439   struct mips16e_save_restore_info info;
9440   unsigned int i, end;
9441   char *s;
9442
9443   /* Parse the pattern.  */
9444   if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
9445     gcc_unreachable ();
9446
9447   /* Add the mnemonic.  */
9448   s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
9449   s += strlen (s);
9450
9451   /* Save the arguments.  */
9452   if (info.nargs > 1)
9453     s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
9454                   reg_names[GP_ARG_FIRST + info.nargs - 1]);
9455   else if (info.nargs == 1)
9456     s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
9457
9458   /* Emit the amount of stack space to allocate or deallocate.  */
9459   s += sprintf (s, "%d", (int) info.size);
9460
9461   /* Save or restore $16.  */
9462   if (BITSET_P (info.mask, 16))
9463     s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
9464
9465   /* Save or restore $17.  */
9466   if (BITSET_P (info.mask, 17))
9467     s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
9468
9469   /* Save or restore registers in the range $s2...$s8, which
9470      mips16e_s2_s8_regs lists in decreasing order.  Note that this
9471      is a software register range; the hardware registers are not
9472      numbered consecutively.  */
9473   end = ARRAY_SIZE (mips16e_s2_s8_regs);
9474   i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
9475   if (i < end)
9476     s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
9477                                     mips16e_s2_s8_regs[i]);
9478
9479   /* Save or restore registers in the range $a0...$a3.  */
9480   end = ARRAY_SIZE (mips16e_a0_a3_regs);
9481   i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
9482   if (i < end)
9483     s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
9484                                     mips16e_a0_a3_regs[end - 1]);
9485
9486   /* Save or restore $31.  */
9487   if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
9488     s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
9489
9490   return buffer;
9491 }
9492 \f
9493 /* Return true if the current function returns its value in a floating-point
9494    register in MIPS16 mode.  */
9495
9496 static bool
9497 mips16_cfun_returns_in_fpr_p (void)
9498 {
9499   tree return_type = DECL_RESULT (current_function_decl);
9500   return (TARGET_MIPS16
9501           && TARGET_HARD_FLOAT_ABI
9502           && !aggregate_value_p (return_type, current_function_decl)
9503           && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
9504 }
9505
9506 /* Return true if predicate PRED is true for at least one instruction.
9507    Cache the result in *CACHE, and assume that the result is true
9508    if *CACHE is already true.  */
9509
9510 static bool
9511 mips_find_gp_ref (bool *cache, bool (*pred) (rtx_insn *))
9512 {
9513   rtx_insn *insn;
9514
9515   if (!*cache)
9516     {
9517       push_topmost_sequence ();
9518       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9519         if (USEFUL_INSN_P (insn) && pred (insn))
9520           {
9521             *cache = true;
9522             break;
9523           }
9524       pop_topmost_sequence ();
9525     }
9526   return *cache;
9527 }
9528
9529 /* Return true if INSN refers to the global pointer in an "inflexible" way.
9530    See mips_cfun_has_inflexible_gp_ref_p for details.  */
9531
9532 static bool
9533 mips_insn_has_inflexible_gp_ref_p (rtx_insn *insn)
9534 {
9535   /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
9536      indicate that the target could be a traditional MIPS
9537      lazily-binding stub.  */
9538   return find_reg_fusage (insn, USE, pic_offset_table_rtx);
9539 }
9540
9541 /* Return true if the current function refers to the global pointer
9542    in a way that forces $28 to be valid.  This means that we can't
9543    change the choice of global pointer, even for NewABI code.
9544
9545    One example of this (and one which needs several checks) is that
9546    $28 must be valid when calling traditional MIPS lazy-binding stubs.
9547    (This restriction does not apply to PLTs.)  */
9548
9549 static bool
9550 mips_cfun_has_inflexible_gp_ref_p (void)
9551 {
9552   /* If the function has a nonlocal goto, $28 must hold the correct
9553      global pointer for the target function.  That is, the target
9554      of the goto implicitly uses $28.  */
9555   if (crtl->has_nonlocal_goto)
9556     return true;
9557
9558   if (TARGET_ABICALLS_PIC2)
9559     {
9560       /* Symbolic accesses implicitly use the global pointer unless
9561          -mexplicit-relocs is in effect.  JAL macros to symbolic addresses
9562          might go to traditional MIPS lazy-binding stubs.  */
9563       if (!TARGET_EXPLICIT_RELOCS)
9564         return true;
9565
9566       /* FUNCTION_PROFILER includes a JAL to _mcount, which again
9567          can be lazily-bound.  */
9568       if (crtl->profile)
9569         return true;
9570
9571       /* MIPS16 functions that return in FPRs need to call an
9572          external libgcc routine.  This call is only made explict
9573          during mips_expand_epilogue, and it too might be lazily bound.  */
9574       if (mips16_cfun_returns_in_fpr_p ())
9575         return true;
9576     }
9577
9578   return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
9579                            mips_insn_has_inflexible_gp_ref_p);
9580 }
9581
9582 /* Return true if INSN refers to the global pointer in a "flexible" way.
9583    See mips_cfun_has_flexible_gp_ref_p for details.  */
9584
9585 static bool
9586 mips_insn_has_flexible_gp_ref_p (rtx_insn *insn)
9587 {
9588   return (get_attr_got (insn) != GOT_UNSET
9589           || mips_small_data_pattern_p (PATTERN (insn))
9590           || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
9591 }
9592
9593 /* Return true if the current function references the global pointer,
9594    but if those references do not inherently require the global pointer
9595    to be $28.  Assume !mips_cfun_has_inflexible_gp_ref_p ().  */
9596
9597 static bool
9598 mips_cfun_has_flexible_gp_ref_p (void)
9599 {
9600   /* Reload can sometimes introduce constant pool references
9601      into a function that otherwise didn't need them.  For example,
9602      suppose we have an instruction like:
9603
9604         (set (reg:DF R1) (float:DF (reg:SI R2)))
9605
9606      If R2 turns out to be a constant such as 1, the instruction may
9607      have a REG_EQUAL note saying that R1 == 1.0.  Reload then has
9608      the option of using this constant if R2 doesn't get allocated
9609      to a register.
9610
9611      In cases like these, reload will have added the constant to the
9612      pool but no instruction will yet refer to it.  */
9613   if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
9614     return true;
9615
9616   return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
9617                            mips_insn_has_flexible_gp_ref_p);
9618 }
9619
9620 /* Return the register that should be used as the global pointer
9621    within this function.  Return INVALID_REGNUM if the function
9622    doesn't need a global pointer.  */
9623
9624 static unsigned int
9625 mips_global_pointer (void)
9626 {
9627   unsigned int regno;
9628
9629   /* $gp is always available unless we're using a GOT.  */
9630   if (!TARGET_USE_GOT)
9631     return GLOBAL_POINTER_REGNUM;
9632
9633   /* If there are inflexible references to $gp, we must use the
9634      standard register.  */
9635   if (mips_cfun_has_inflexible_gp_ref_p ())
9636     return GLOBAL_POINTER_REGNUM;
9637
9638   /* If there are no current references to $gp, then the only uses
9639      we can introduce later are those involved in long branches.  */
9640   if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
9641     return INVALID_REGNUM;
9642
9643   /* If the global pointer is call-saved, try to use a call-clobbered
9644      alternative.  */
9645   if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
9646     for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9647       if (!df_regs_ever_live_p (regno)
9648           && call_really_used_regs[regno]
9649           && !fixed_regs[regno]
9650           && regno != PIC_FUNCTION_ADDR_REGNUM)
9651         return regno;
9652
9653   return GLOBAL_POINTER_REGNUM;
9654 }
9655
9656 /* Return true if the current function's prologue must load the global
9657    pointer value into pic_offset_table_rtx and store the same value in
9658    the function's cprestore slot (if any).
9659
9660    One problem we have to deal with is that, when emitting GOT-based
9661    position independent code, long-branch sequences will need to load
9662    the address of the branch target from the GOT.  We don't know until
9663    the very end of compilation whether (and where) the function needs
9664    long branches, so we must ensure that _any_ branch can access the
9665    global pointer in some form.  However, we do not want to pessimize
9666    the usual case in which all branches are short.
9667
9668    We handle this as follows:
9669
9670    (1) During reload, we set cfun->machine->global_pointer to
9671        INVALID_REGNUM if we _know_ that the current function
9672        doesn't need a global pointer.  This is only valid if
9673        long branches don't need the GOT.
9674
9675        Otherwise, we assume that we might need a global pointer
9676        and pick an appropriate register.
9677
9678    (2) If cfun->machine->global_pointer != INVALID_REGNUM,
9679        we ensure that the global pointer is available at every
9680        block boundary bar entry and exit.  We do this in one of two ways:
9681
9682        - If the function has a cprestore slot, we ensure that this
9683          slot is valid at every branch.  However, as explained in
9684          point (6) below, there is no guarantee that pic_offset_table_rtx
9685          itself is valid if new uses of the global pointer are introduced
9686          after the first post-epilogue split.
9687
9688          We guarantee that the cprestore slot is valid by loading it
9689          into a fake register, CPRESTORE_SLOT_REGNUM.  We then make
9690          this register live at every block boundary bar function entry
9691          and exit.  It is then invalid to move the load (and thus the
9692          preceding store) across a block boundary.
9693
9694        - If the function has no cprestore slot, we guarantee that
9695          pic_offset_table_rtx itself is valid at every branch.
9696
9697        See mips_eh_uses for the handling of the register liveness.
9698
9699    (3) During prologue and epilogue generation, we emit "ghost"
9700        placeholder instructions to manipulate the global pointer.
9701
9702    (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
9703        and cfun->machine->must_restore_gp_when_clobbered_p if we already know
9704        that the function needs a global pointer.  (There is no need to set
9705        them earlier than this, and doing it as late as possible leads to
9706        fewer false positives.)
9707
9708    (5) If cfun->machine->must_initialize_gp_p is true during a
9709        split_insns pass, we split the ghost instructions into real
9710        instructions.  These split instructions can then be optimized in
9711        the usual way.  Otherwise, we keep the ghost instructions intact,
9712        and optimize for the case where they aren't needed.  We still
9713        have the option of splitting them later, if we need to introduce
9714        new uses of the global pointer.
9715
9716        For example, the scheduler ignores a ghost instruction that
9717        stores $28 to the stack, but it handles the split form of
9718        the ghost instruction as an ordinary store.
9719
9720    (6) [OldABI only.]  If cfun->machine->must_restore_gp_when_clobbered_p
9721        is true during the first post-epilogue split_insns pass, we split
9722        calls and restore_gp patterns into instructions that explicitly
9723        load pic_offset_table_rtx from the cprestore slot.  Otherwise,
9724        we split these patterns into instructions that _don't_ load from
9725        the cprestore slot.
9726
9727        If cfun->machine->must_restore_gp_when_clobbered_p is true at the
9728        time of the split, then any instructions that exist at that time
9729        can make free use of pic_offset_table_rtx.  However, if we want
9730        to introduce new uses of the global pointer after the split,
9731        we must explicitly load the value from the cprestore slot, since
9732        pic_offset_table_rtx itself might not be valid at a given point
9733        in the function.
9734
9735        The idea is that we want to be able to delete redundant
9736        loads from the cprestore slot in the usual case where no
9737        long branches are needed.
9738
9739    (7) If cfun->machine->must_initialize_gp_p is still false at the end
9740        of md_reorg, we decide whether the global pointer is needed for
9741        long branches.  If so, we set cfun->machine->must_initialize_gp_p
9742        to true and split the ghost instructions into real instructions
9743        at that stage.
9744
9745    Note that the ghost instructions must have a zero length for three reasons:
9746
9747    - Giving the length of the underlying $gp sequence might cause
9748      us to use long branches in cases where they aren't really needed.
9749
9750    - They would perturb things like alignment calculations.
9751
9752    - More importantly, the hazard detection in md_reorg relies on
9753      empty instructions having a zero length.
9754
9755    If we find a long branch and split the ghost instructions at the
9756    end of md_reorg, the split could introduce more long branches.
9757    That isn't a problem though, because we still do the split before
9758    the final shorten_branches pass.
9759
9760    This is extremely ugly, but it seems like the best compromise between
9761    correctness and efficiency.  */
9762
9763 bool
9764 mips_must_initialize_gp_p (void)
9765 {
9766   return cfun->machine->must_initialize_gp_p;
9767 }
9768
9769 /* Return true if REGNO is a register that is ordinarily call-clobbered
9770    but must nevertheless be preserved by an interrupt handler.  */
9771
9772 static bool
9773 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
9774 {
9775   if (MD_REG_P (regno))
9776     return true;
9777
9778   if (TARGET_DSP && DSP_ACC_REG_P (regno))
9779     return true;
9780
9781   if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
9782     {
9783       /* $0 is hard-wired.  */
9784       if (regno == GP_REG_FIRST)
9785         return false;
9786
9787       /* The interrupt handler can treat kernel registers as
9788          scratch registers.  */
9789       if (KERNEL_REG_P (regno))
9790         return false;
9791
9792       /* The function will return the stack pointer to its original value
9793          anyway.  */
9794       if (regno == STACK_POINTER_REGNUM)
9795         return false;
9796
9797       /* Otherwise, return true for registers that aren't ordinarily
9798          call-clobbered.  */
9799       return call_really_used_regs[regno];
9800     }
9801
9802   return false;
9803 }
9804
9805 /* Return true if the current function should treat register REGNO
9806    as call-saved.  */
9807
9808 static bool
9809 mips_cfun_call_saved_reg_p (unsigned int regno)
9810 {
9811   /* If the user makes an ordinarily-call-saved register global,
9812      that register is no longer call-saved.  */
9813   if (global_regs[regno])
9814     return false;
9815
9816   /* Interrupt handlers need to save extra registers.  */
9817   if (cfun->machine->interrupt_handler_p
9818       && mips_interrupt_extra_call_saved_reg_p (regno))
9819     return true;
9820
9821   /* call_insns preserve $28 unless they explicitly say otherwise,
9822      so call_really_used_regs[] treats $28 as call-saved.  However,
9823      we want the ABI property rather than the default call_insn
9824      property here.  */
9825   return (regno == GLOBAL_POINTER_REGNUM
9826           ? TARGET_CALL_SAVED_GP
9827           : !call_really_used_regs[regno]);
9828 }
9829
9830 /* Return true if the function body might clobber register REGNO.
9831    We know that REGNO is call-saved.  */
9832
9833 static bool
9834 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
9835 {
9836   /* Some functions should be treated as clobbering all call-saved
9837      registers.  */
9838   if (crtl->saves_all_registers)
9839     return true;
9840
9841   /* DF handles cases where a register is explicitly referenced in
9842      the rtl.  Incoming values are passed in call-clobbered registers,
9843      so we can assume that any live call-saved register is set within
9844      the function.  */
9845   if (df_regs_ever_live_p (regno))
9846     return true;
9847
9848   /* Check for registers that are clobbered by FUNCTION_PROFILER.
9849      These clobbers are not explicit in the rtl.  */
9850   if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
9851     return true;
9852
9853   /* If we're using a call-saved global pointer, the function's
9854      prologue will need to set it up.  */
9855   if (cfun->machine->global_pointer == regno)
9856     return true;
9857
9858   /* The function's prologue will need to set the frame pointer if
9859      frame_pointer_needed.  */
9860   if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
9861     return true;
9862
9863   /* If a MIPS16 function returns a value in FPRs, its epilogue
9864      will need to call an external libgcc routine.  This yet-to-be
9865      generated call_insn will clobber $31.  */
9866   if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
9867     return true;
9868
9869   /* If REGNO is ordinarily call-clobbered, we must assume that any
9870      called function could modify it.  */
9871   if (cfun->machine->interrupt_handler_p
9872       && !crtl->is_leaf
9873       && mips_interrupt_extra_call_saved_reg_p (regno))
9874     return true;
9875
9876   return false;
9877 }
9878
9879 /* Return true if the current function must save register REGNO.  */
9880
9881 static bool
9882 mips_save_reg_p (unsigned int regno)
9883 {
9884   if (mips_cfun_call_saved_reg_p (regno))
9885     {
9886       if (mips_cfun_might_clobber_call_saved_reg_p (regno))
9887         return true;
9888
9889       /* Save both registers in an FPR pair if either one is used.  This is
9890          needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
9891          register to be used without the even register.  */
9892       if (FP_REG_P (regno)
9893           && MAX_FPRS_PER_FMT == 2
9894           && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
9895         return true;
9896     }
9897
9898   /* We need to save the incoming return address if __builtin_eh_return
9899      is being used to set a different return address.  */
9900   if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
9901     return true;
9902
9903   return false;
9904 }
9905
9906 /* Populate the current function's mips_frame_info structure.
9907
9908    MIPS stack frames look like:
9909
9910         +-------------------------------+
9911         |                               |
9912         |  incoming stack arguments     |
9913         |                               |
9914         +-------------------------------+
9915         |                               |
9916         |  caller-allocated save area   |
9917       A |  for register arguments       |
9918         |                               |
9919         +-------------------------------+ <-- incoming stack pointer
9920         |                               |
9921         |  callee-allocated save area   |
9922       B |  for arguments that are       |
9923         |  split between registers and  |
9924         |  the stack                    |
9925         |                               |
9926         +-------------------------------+ <-- arg_pointer_rtx
9927         |                               |
9928       C |  callee-allocated save area   |
9929         |  for register varargs         |
9930         |                               |
9931         +-------------------------------+ <-- frame_pointer_rtx
9932         |                               |       + cop0_sp_offset
9933         |  COP0 reg save area           |       + UNITS_PER_WORD
9934         |                               |
9935         +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
9936         |                               |       + UNITS_PER_WORD
9937         |  accumulator save area        |
9938         |                               |
9939         +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
9940         |                               |       + UNITS_PER_HWFPVALUE
9941         |  FPR save area                |
9942         |                               |
9943         +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
9944         |                               |       + UNITS_PER_WORD
9945         |  GPR save area                |
9946         |                               |
9947         +-------------------------------+ <-- frame_pointer_rtx with
9948         |                               | \     -fstack-protector
9949         |  local variables              |  | var_size
9950         |                               | /
9951         +-------------------------------+
9952         |                               | \
9953         |  $gp save area                |  | cprestore_size
9954         |                               | /
9955       P +-------------------------------+ <-- hard_frame_pointer_rtx for
9956         |                               | \     MIPS16 code
9957         |  outgoing stack arguments     |  |
9958         |                               |  |
9959         +-------------------------------+  | args_size
9960         |                               |  |
9961         |  caller-allocated save area   |  |
9962         |  for register arguments       |  |
9963         |                               | /
9964         +-------------------------------+ <-- stack_pointer_rtx
9965                                               frame_pointer_rtx without
9966                                                 -fstack-protector
9967                                               hard_frame_pointer_rtx for
9968                                                 non-MIPS16 code.
9969
9970    At least two of A, B and C will be empty.
9971
9972    Dynamic stack allocations such as alloca insert data at point P.
9973    They decrease stack_pointer_rtx but leave frame_pointer_rtx and
9974    hard_frame_pointer_rtx unchanged.  */
9975
9976 static void
9977 mips_compute_frame_info (void)
9978 {
9979   struct mips_frame_info *frame;
9980   HOST_WIDE_INT offset, size;
9981   unsigned int regno, i;
9982
9983   /* Set this function's interrupt properties.  */
9984   if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
9985     {
9986       if (mips_isa_rev < 2)
9987         error ("the %<interrupt%> attribute requires a MIPS32r2 processor or greater");
9988       else if (TARGET_HARD_FLOAT)
9989         error ("the %<interrupt%> attribute requires %<-msoft-float%>");
9990       else if (TARGET_MIPS16)
9991         error ("interrupt handlers cannot be MIPS16 functions");
9992       else
9993         {
9994           cfun->machine->interrupt_handler_p = true;
9995           cfun->machine->use_shadow_register_set_p =
9996             mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
9997           cfun->machine->keep_interrupts_masked_p =
9998             mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
9999           cfun->machine->use_debug_exception_return_p =
10000             mips_use_debug_exception_return_p (TREE_TYPE
10001                                                (current_function_decl));
10002         }
10003     }
10004
10005   frame = &cfun->machine->frame;
10006   memset (frame, 0, sizeof (*frame));
10007   size = get_frame_size ();
10008
10009   cfun->machine->global_pointer = mips_global_pointer ();
10010
10011   /* The first two blocks contain the outgoing argument area and the $gp save
10012      slot.  This area isn't needed in leaf functions, but if the
10013      target-independent frame size is nonzero, we have already committed to
10014      allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD.  */
10015   if ((size == 0 || FRAME_GROWS_DOWNWARD) && crtl->is_leaf)
10016     {
10017       /* The MIPS 3.0 linker does not like functions that dynamically
10018          allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
10019          looks like we are trying to create a second frame pointer to the
10020          function, so allocate some stack space to make it happy.  */
10021       if (cfun->calls_alloca)
10022         frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
10023       else
10024         frame->args_size = 0;
10025       frame->cprestore_size = 0;
10026     }
10027   else
10028     {
10029       frame->args_size = crtl->outgoing_args_size;
10030       frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
10031     }
10032   offset = frame->args_size + frame->cprestore_size;
10033
10034   /* Move above the local variables.  */
10035   frame->var_size = MIPS_STACK_ALIGN (size);
10036   offset += frame->var_size;
10037
10038   /* Find out which GPRs we need to save.  */
10039   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
10040     if (mips_save_reg_p (regno))
10041       {
10042         frame->num_gp++;
10043         frame->mask |= 1 << (regno - GP_REG_FIRST);
10044       }
10045
10046   /* If this function calls eh_return, we must also save and restore the
10047      EH data registers.  */
10048   if (crtl->calls_eh_return)
10049     for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
10050       {
10051         frame->num_gp++;
10052         frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
10053       }
10054
10055   /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
10056      $a3-$a0 and $s2-$s8.  If we save one register in the range, we must
10057      save all later registers too.  */
10058   if (GENERATE_MIPS16E_SAVE_RESTORE)
10059     {
10060       mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
10061                               ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
10062       mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
10063                               ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
10064     }
10065
10066   /* Move above the GPR save area.  */
10067   if (frame->num_gp > 0)
10068     {
10069       offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
10070       frame->gp_sp_offset = offset - UNITS_PER_WORD;
10071     }
10072
10073   /* Find out which FPRs we need to save.  This loop must iterate over
10074      the same space as its companion in mips_for_each_saved_gpr_and_fpr.  */
10075   if (TARGET_HARD_FLOAT)
10076     for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
10077       if (mips_save_reg_p (regno))
10078         {
10079           frame->num_fp += MAX_FPRS_PER_FMT;
10080           frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
10081         }
10082
10083   /* Move above the FPR save area.  */
10084   if (frame->num_fp > 0)
10085     {
10086       offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
10087       frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
10088     }
10089
10090   /* Add in space for the interrupt context information.  */
10091   if (cfun->machine->interrupt_handler_p)
10092     {
10093       /* Check HI/LO.  */
10094       if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
10095         {
10096           frame->num_acc++;
10097           frame->acc_mask |= (1 << 0);
10098         }
10099
10100       /* Check accumulators 1, 2, 3.  */
10101       for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
10102         if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
10103           {
10104             frame->num_acc++;
10105             frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
10106           }
10107
10108       /* All interrupt context functions need space to preserve STATUS.  */
10109       frame->num_cop0_regs++;
10110
10111       /* If we don't keep interrupts masked, we need to save EPC.  */
10112       if (!cfun->machine->keep_interrupts_masked_p)
10113         frame->num_cop0_regs++;
10114     }
10115
10116   /* Move above the accumulator save area.  */
10117   if (frame->num_acc > 0)
10118     {
10119       /* Each accumulator needs 2 words.  */
10120       offset += frame->num_acc * 2 * UNITS_PER_WORD;
10121       frame->acc_sp_offset = offset - UNITS_PER_WORD;
10122     }
10123
10124   /* Move above the COP0 register save area.  */
10125   if (frame->num_cop0_regs > 0)
10126     {
10127       offset += frame->num_cop0_regs * UNITS_PER_WORD;
10128       frame->cop0_sp_offset = offset - UNITS_PER_WORD;
10129     }
10130
10131   /* Move above the callee-allocated varargs save area.  */
10132   offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
10133   frame->arg_pointer_offset = offset;
10134
10135   /* Move above the callee-allocated area for pretend stack arguments.  */
10136   offset += crtl->args.pretend_args_size;
10137   frame->total_size = offset;
10138
10139   /* Work out the offsets of the save areas from the top of the frame.  */
10140   if (frame->gp_sp_offset > 0)
10141     frame->gp_save_offset = frame->gp_sp_offset - offset;
10142   if (frame->fp_sp_offset > 0)
10143     frame->fp_save_offset = frame->fp_sp_offset - offset;
10144   if (frame->acc_sp_offset > 0)
10145     frame->acc_save_offset = frame->acc_sp_offset - offset;
10146   if (frame->num_cop0_regs > 0)
10147     frame->cop0_save_offset = frame->cop0_sp_offset - offset;
10148
10149   /* MIPS16 code offsets the frame pointer by the size of the outgoing
10150      arguments.  This tends to increase the chances of using unextended
10151      instructions for local variables and incoming arguments.  */
10152   if (TARGET_MIPS16)
10153     frame->hard_frame_pointer_offset = frame->args_size;
10154 }
10155
10156 /* Return the style of GP load sequence that is being used for the
10157    current function.  */
10158
10159 enum mips_loadgp_style
10160 mips_current_loadgp_style (void)
10161 {
10162   if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
10163     return LOADGP_NONE;
10164
10165   if (TARGET_RTP_PIC)
10166     return LOADGP_RTP;
10167
10168   if (TARGET_ABSOLUTE_ABICALLS)
10169     return LOADGP_ABSOLUTE;
10170
10171   return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
10172 }
10173
10174 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
10175
10176 static bool
10177 mips_frame_pointer_required (void)
10178 {
10179   /* If the function contains dynamic stack allocations, we need to
10180      use the frame pointer to access the static parts of the frame.  */
10181   if (cfun->calls_alloca)
10182     return true;
10183
10184   /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
10185      reload may be unable to compute the address of a local variable,
10186      since there is no way to add a large constant to the stack pointer
10187      without using a second temporary register.  */
10188   if (TARGET_MIPS16)
10189     {
10190       mips_compute_frame_info ();
10191       if (!SMALL_OPERAND (cfun->machine->frame.total_size))
10192         return true;
10193     }
10194
10195   return false;
10196 }
10197
10198 /* Make sure that we're not trying to eliminate to the wrong hard frame
10199    pointer.  */
10200
10201 static bool
10202 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
10203 {
10204   return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
10205 }
10206
10207 /* Implement INITIAL_ELIMINATION_OFFSET.  FROM is either the frame pointer
10208    or argument pointer.  TO is either the stack pointer or hard frame
10209    pointer.  */
10210
10211 HOST_WIDE_INT
10212 mips_initial_elimination_offset (int from, int to)
10213 {
10214   HOST_WIDE_INT offset;
10215
10216   mips_compute_frame_info ();
10217
10218   /* Set OFFSET to the offset from the end-of-prologue stack pointer.  */
10219   switch (from)
10220     {
10221     case FRAME_POINTER_REGNUM:
10222       if (FRAME_GROWS_DOWNWARD)
10223         offset = (cfun->machine->frame.args_size
10224                   + cfun->machine->frame.cprestore_size
10225                   + cfun->machine->frame.var_size);
10226       else
10227         offset = 0;
10228       break;
10229
10230     case ARG_POINTER_REGNUM:
10231       offset = cfun->machine->frame.arg_pointer_offset;
10232       break;
10233
10234     default:
10235       gcc_unreachable ();
10236     }
10237
10238   if (to == HARD_FRAME_POINTER_REGNUM)
10239     offset -= cfun->machine->frame.hard_frame_pointer_offset;
10240
10241   return offset;
10242 }
10243 \f
10244 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY.  */
10245
10246 static void
10247 mips_extra_live_on_entry (bitmap regs)
10248 {
10249   if (TARGET_USE_GOT)
10250     {
10251       /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
10252          the global pointer.   */
10253       if (!TARGET_ABSOLUTE_ABICALLS)
10254         bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
10255
10256       /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
10257          the global pointer.  */
10258       if (TARGET_MIPS16)
10259         bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
10260
10261       /* See the comment above load_call<mode> for details.  */
10262       bitmap_set_bit (regs, GOT_VERSION_REGNUM);
10263     }
10264 }
10265
10266 /* Implement RETURN_ADDR_RTX.  We do not support moving back to a
10267    previous frame.  */
10268
10269 rtx
10270 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
10271 {
10272   if (count != 0)
10273     return const0_rtx;
10274
10275   return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
10276 }
10277
10278 /* Emit code to change the current function's return address to
10279    ADDRESS.  SCRATCH is available as a scratch register, if needed.
10280    ADDRESS and SCRATCH are both word-mode GPRs.  */
10281
10282 void
10283 mips_set_return_address (rtx address, rtx scratch)
10284 {
10285   rtx slot_address;
10286
10287   gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
10288   slot_address = mips_add_offset (scratch, stack_pointer_rtx,
10289                                   cfun->machine->frame.gp_sp_offset);
10290   mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
10291 }
10292
10293 /* Return true if the current function has a cprestore slot.  */
10294
10295 bool
10296 mips_cfun_has_cprestore_slot_p (void)
10297 {
10298   return (cfun->machine->global_pointer != INVALID_REGNUM
10299           && cfun->machine->frame.cprestore_size > 0);
10300 }
10301
10302 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
10303    cprestore slot.  LOAD_P is true if the caller wants to load from
10304    the cprestore slot; it is false if the caller wants to store to
10305    the slot.  */
10306
10307 static void
10308 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
10309                                     bool load_p)
10310 {
10311   const struct mips_frame_info *frame;
10312
10313   frame = &cfun->machine->frame;
10314   /* .cprestore always uses the stack pointer instead of the frame pointer.
10315      We have a free choice for direct stores for non-MIPS16 functions,
10316      and for MIPS16 functions whose cprestore slot is in range of the
10317      stack pointer.  Using the stack pointer would sometimes give more
10318      (early) scheduling freedom, but using the frame pointer would
10319      sometimes give more (late) scheduling freedom.  It's hard to
10320      predict which applies to a given function, so let's keep things
10321      simple.
10322
10323      Loads must always use the frame pointer in functions that call
10324      alloca, and there's little benefit to using the stack pointer
10325      otherwise.  */
10326   if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
10327     {
10328       *base = hard_frame_pointer_rtx;
10329       *offset = frame->args_size - frame->hard_frame_pointer_offset;
10330     }
10331   else
10332     {
10333       *base = stack_pointer_rtx;
10334       *offset = frame->args_size;
10335     }
10336 }
10337
10338 /* Return true if X is the load or store address of the cprestore slot;
10339    LOAD_P says which.  */
10340
10341 bool
10342 mips_cprestore_address_p (rtx x, bool load_p)
10343 {
10344   rtx given_base, required_base;
10345   HOST_WIDE_INT given_offset, required_offset;
10346
10347   mips_split_plus (x, &given_base, &given_offset);
10348   mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
10349   return given_base == required_base && given_offset == required_offset;
10350 }
10351
10352 /* Return a MEM rtx for the cprestore slot.  LOAD_P is true if we are
10353    going to load from it, false if we are going to store to it.
10354    Use TEMP as a temporary register if need be.  */
10355
10356 static rtx
10357 mips_cprestore_slot (rtx temp, bool load_p)
10358 {
10359   rtx base;
10360   HOST_WIDE_INT offset;
10361
10362   mips_get_cprestore_base_and_offset (&base, &offset, load_p);
10363   return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
10364 }
10365
10366 /* Emit instructions to save global pointer value GP into cprestore
10367    slot MEM.  OFFSET is the offset that MEM applies to the base register.
10368
10369    MEM may not be a legitimate address.  If it isn't, TEMP is a
10370    temporary register that can be used, otherwise it is a SCRATCH.  */
10371
10372 void
10373 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
10374 {
10375   if (TARGET_CPRESTORE_DIRECTIVE)
10376     {
10377       gcc_assert (gp == pic_offset_table_rtx);
10378       emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
10379     }
10380   else
10381     mips_emit_move (mips_cprestore_slot (temp, false), gp);
10382 }
10383
10384 /* Restore $gp from its save slot, using TEMP as a temporary base register
10385    if need be.  This function is for o32 and o64 abicalls only.
10386
10387    See mips_must_initialize_gp_p for details about how we manage the
10388    global pointer.  */
10389
10390 void
10391 mips_restore_gp_from_cprestore_slot (rtx temp)
10392 {
10393   gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
10394
10395   if (!cfun->machine->must_restore_gp_when_clobbered_p)
10396     {
10397       emit_note (NOTE_INSN_DELETED);
10398       return;
10399     }
10400
10401   if (TARGET_MIPS16)
10402     {
10403       mips_emit_move (temp, mips_cprestore_slot (temp, true));
10404       mips_emit_move (pic_offset_table_rtx, temp);
10405     }
10406   else
10407     mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
10408   if (!TARGET_EXPLICIT_RELOCS)
10409     emit_insn (gen_blockage ());
10410 }
10411 \f
10412 /* A function to save or store a register.  The first argument is the
10413    register and the second is the stack slot.  */
10414 typedef void (*mips_save_restore_fn) (rtx, rtx);
10415
10416 /* Use FN to save or restore register REGNO.  MODE is the register's
10417    mode and OFFSET is the offset of its save slot from the current
10418    stack pointer.  */
10419
10420 static void
10421 mips_save_restore_reg (enum machine_mode mode, int regno,
10422                        HOST_WIDE_INT offset, mips_save_restore_fn fn)
10423 {
10424   rtx mem;
10425
10426   mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
10427                                             offset));
10428   fn (gen_rtx_REG (mode, regno), mem);
10429 }
10430
10431 /* Call FN for each accumlator that is saved by the current function.
10432    SP_OFFSET is the offset of the current stack pointer from the start
10433    of the frame.  */
10434
10435 static void
10436 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
10437 {
10438   HOST_WIDE_INT offset;
10439   int regno;
10440
10441   offset = cfun->machine->frame.acc_sp_offset - sp_offset;
10442   if (BITSET_P (cfun->machine->frame.acc_mask, 0))
10443     {
10444       mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
10445       offset -= UNITS_PER_WORD;
10446       mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
10447       offset -= UNITS_PER_WORD;
10448     }
10449
10450   for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
10451     if (BITSET_P (cfun->machine->frame.acc_mask,
10452                   ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
10453       {
10454         mips_save_restore_reg (word_mode, regno, offset, fn);
10455         offset -= UNITS_PER_WORD;
10456       }
10457 }
10458
10459 /* Save register REG to MEM.  Make the instruction frame-related.  */
10460
10461 static void
10462 mips_save_reg (rtx reg, rtx mem)
10463 {
10464   if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
10465     {
10466       rtx x1, x2;
10467
10468       mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
10469
10470       x1 = mips_frame_set (mips_subword (mem, false),
10471                            mips_subword (reg, false));
10472       x2 = mips_frame_set (mips_subword (mem, true),
10473                            mips_subword (reg, true));
10474       mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
10475     }
10476   else
10477     mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
10478 }
10479
10480 /* Capture the register combinations that are allowed in a SWM or LWM
10481    instruction.  The entries are ordered by number of registers set in
10482    the mask.  We also ignore the single register encodings because a
10483    normal SW/LW is preferred.  */
10484
10485 static const unsigned int umips_swm_mask[17] = {
10486   0xc0ff0000, 0x80ff0000, 0x40ff0000, 0x807f0000,
10487   0x00ff0000, 0x803f0000, 0x007f0000, 0x801f0000,
10488   0x003f0000, 0x800f0000, 0x001f0000, 0x80070000,
10489   0x000f0000, 0x80030000, 0x00070000, 0x80010000,
10490   0x00030000
10491 };
10492
10493 static const unsigned int umips_swm_encoding[17] = {
10494   25, 24, 9, 23, 8, 22, 7, 21, 6, 20, 5, 19, 4, 18, 3, 17, 2
10495 };
10496
10497 /* Try to use a microMIPS LWM or SWM instruction to save or restore
10498    as many GPRs in *MASK as possible.  *OFFSET is the offset from the
10499    stack pointer of the topmost save slot.
10500
10501    Remove from *MASK all registers that were handled using LWM and SWM.
10502    Update *OFFSET so that it points to the first unused save slot.  */
10503
10504 static bool
10505 umips_build_save_restore (mips_save_restore_fn fn,
10506                           unsigned *mask, HOST_WIDE_INT *offset)
10507 {
10508   int nregs;
10509   unsigned int i, j;
10510   rtx pattern, set, reg, mem;
10511   HOST_WIDE_INT this_offset;
10512   rtx this_base;
10513
10514   /* Try matching $16 to $31 (s0 to ra).  */
10515   for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
10516     if ((*mask & 0xffff0000) == umips_swm_mask[i])
10517       break;
10518
10519   if (i == ARRAY_SIZE (umips_swm_mask))
10520     return false;
10521
10522   /* Get the offset of the lowest save slot.  */
10523   nregs = (umips_swm_encoding[i] & 0xf) + (umips_swm_encoding[i] >> 4);
10524   this_offset = *offset - UNITS_PER_WORD * (nregs - 1);
10525
10526   /* LWM/SWM can only support offsets from -2048 to 2047.  */
10527   if (!UMIPS_12BIT_OFFSET_P (this_offset))
10528     return false;
10529
10530   /* Create the final PARALLEL.  */
10531   pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
10532   this_base = stack_pointer_rtx;
10533
10534   /* For registers $16-$23 and $30.  */
10535   for (j = 0; j < (umips_swm_encoding[i] & 0xf); j++)
10536     {
10537       HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10538       mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10539       unsigned int regno = (j != 8) ? 16 + j : 30;
10540       *mask &= ~(1 << regno);
10541       reg = gen_rtx_REG (SImode, regno);
10542       if (fn == mips_save_reg)
10543         set = mips_frame_set (mem, reg);
10544       else
10545         {
10546           set = gen_rtx_SET (VOIDmode, reg, mem);
10547           mips_add_cfa_restore (reg);
10548         }
10549       XVECEXP (pattern, 0, j) = set;
10550     }
10551
10552   /* For register $31.  */
10553   if (umips_swm_encoding[i] >> 4)
10554     {
10555       HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10556       *mask &= ~(1 << 31);
10557       mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10558       reg = gen_rtx_REG (SImode, 31);
10559       if (fn == mips_save_reg)
10560         set = mips_frame_set (mem, reg);
10561       else
10562         {
10563           set = gen_rtx_SET (VOIDmode, reg, mem);
10564           mips_add_cfa_restore (reg);
10565         }
10566       XVECEXP (pattern, 0, j) = set;
10567     }
10568
10569   pattern = emit_insn (pattern);
10570   if (fn == mips_save_reg)
10571     RTX_FRAME_RELATED_P (pattern) = 1;
10572
10573   /* Adjust the last offset.  */
10574   *offset -= UNITS_PER_WORD * nregs;
10575
10576   return true;
10577 }
10578
10579 /* Call FN for each register that is saved by the current function.
10580    SP_OFFSET is the offset of the current stack pointer from the start
10581    of the frame.  */
10582
10583 static void
10584 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
10585                                  mips_save_restore_fn fn)
10586 {
10587   enum machine_mode fpr_mode;
10588   int regno;
10589   const struct mips_frame_info *frame = &cfun->machine->frame;
10590   HOST_WIDE_INT offset;
10591   unsigned int mask;
10592
10593   /* Save registers starting from high to low.  The debuggers prefer at least
10594      the return register be stored at func+4, and also it allows us not to
10595      need a nop in the epilogue if at least one register is reloaded in
10596      addition to return address.  */
10597   offset = frame->gp_sp_offset - sp_offset;
10598   mask = frame->mask;
10599
10600   if (TARGET_MICROMIPS)
10601     umips_build_save_restore (fn, &mask, &offset);
10602
10603   for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
10604     if (BITSET_P (mask, regno - GP_REG_FIRST))
10605       {
10606         /* Record the ra offset for use by mips_function_profiler.  */
10607         if (regno == RETURN_ADDR_REGNUM)
10608           cfun->machine->frame.ra_fp_offset = offset + sp_offset;
10609         mips_save_restore_reg (word_mode, regno, offset, fn);
10610         offset -= UNITS_PER_WORD;
10611       }
10612
10613   /* This loop must iterate over the same space as its companion in
10614      mips_compute_frame_info.  */
10615   offset = cfun->machine->frame.fp_sp_offset - sp_offset;
10616   fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
10617   for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
10618        regno >= FP_REG_FIRST;
10619        regno -= MAX_FPRS_PER_FMT)
10620     if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
10621       {
10622         mips_save_restore_reg (fpr_mode, regno, offset, fn);
10623         offset -= GET_MODE_SIZE (fpr_mode);
10624       }
10625 }
10626
10627 /* Return true if a move between register REGNO and its save slot (MEM)
10628    can be done in a single move.  LOAD_P is true if we are loading
10629    from the slot, false if we are storing to it.  */
10630
10631 static bool
10632 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
10633 {
10634   /* There is a specific MIPS16 instruction for saving $31 to the stack.  */
10635   if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
10636     return false;
10637
10638   return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
10639                                       GET_MODE (mem), mem, load_p) == NO_REGS;
10640 }
10641
10642 /* Emit a move from SRC to DEST, given that one of them is a register
10643    save slot and that the other is a register.  TEMP is a temporary
10644    GPR of the same mode that is available if need be.  */
10645
10646 void
10647 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
10648 {
10649   unsigned int regno;
10650   rtx mem;
10651
10652   if (REG_P (src))
10653     {
10654       regno = REGNO (src);
10655       mem = dest;
10656     }
10657   else
10658     {
10659       regno = REGNO (dest);
10660       mem = src;
10661     }
10662
10663   if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
10664     {
10665       /* We don't yet know whether we'll need this instruction or not.
10666          Postpone the decision by emitting a ghost move.  This move
10667          is specifically not frame-related; only the split version is.  */
10668       if (TARGET_64BIT)
10669         emit_insn (gen_move_gpdi (dest, src));
10670       else
10671         emit_insn (gen_move_gpsi (dest, src));
10672       return;
10673     }
10674
10675   if (regno == HI_REGNUM)
10676     {
10677       if (REG_P (dest))
10678         {
10679           mips_emit_move (temp, src);
10680           if (TARGET_64BIT)
10681             emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
10682                                       temp, gen_rtx_REG (DImode, LO_REGNUM)));
10683           else
10684             emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
10685                                       temp, gen_rtx_REG (SImode, LO_REGNUM)));
10686         }
10687       else
10688         {
10689           if (TARGET_64BIT)
10690             emit_insn (gen_mfhidi_ti (temp,
10691                                       gen_rtx_REG (TImode, MD_REG_FIRST)));
10692           else
10693             emit_insn (gen_mfhisi_di (temp,
10694                                       gen_rtx_REG (DImode, MD_REG_FIRST)));
10695           mips_emit_move (dest, temp);
10696         }
10697     }
10698   else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
10699     mips_emit_move (dest, src);
10700   else
10701     {
10702       gcc_assert (!reg_overlap_mentioned_p (dest, temp));
10703       mips_emit_move (temp, src);
10704       mips_emit_move (dest, temp);
10705     }
10706   if (MEM_P (dest))
10707     mips_set_frame_expr (mips_frame_set (dest, src));
10708 }
10709 \f
10710 /* If we're generating n32 or n64 abicalls, and the current function
10711    does not use $28 as its global pointer, emit a cplocal directive.
10712    Use pic_offset_table_rtx as the argument to the directive.  */
10713
10714 static void
10715 mips_output_cplocal (void)
10716 {
10717   if (!TARGET_EXPLICIT_RELOCS
10718       && mips_must_initialize_gp_p ()
10719       && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
10720     output_asm_insn (".cplocal %+", 0);
10721 }
10722
10723 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE.  */
10724
10725 static void
10726 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10727 {
10728   const char *fnname;
10729
10730   /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
10731      floating-point arguments.  */
10732   if (TARGET_MIPS16
10733       && TARGET_HARD_FLOAT_ABI
10734       && crtl->args.info.fp_code != 0)
10735     mips16_build_function_stub ();
10736
10737   /* Get the function name the same way that toplev.c does before calling
10738      assemble_start_function.  This is needed so that the name used here
10739      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
10740   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10741   mips_start_function_definition (fnname, TARGET_MIPS16);
10742
10743   /* Output MIPS-specific frame information.  */
10744   if (!flag_inhibit_size_directive)
10745     {
10746       const struct mips_frame_info *frame;
10747
10748       frame = &cfun->machine->frame;
10749
10750       /* .frame FRAMEREG, FRAMESIZE, RETREG.  */
10751       fprintf (file,
10752                "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
10753                "# vars= " HOST_WIDE_INT_PRINT_DEC
10754                ", regs= %d/%d"
10755                ", args= " HOST_WIDE_INT_PRINT_DEC
10756                ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
10757                reg_names[frame_pointer_needed
10758                          ? HARD_FRAME_POINTER_REGNUM
10759                          : STACK_POINTER_REGNUM],
10760                (frame_pointer_needed
10761                 ? frame->total_size - frame->hard_frame_pointer_offset
10762                 : frame->total_size),
10763                reg_names[RETURN_ADDR_REGNUM],
10764                frame->var_size,
10765                frame->num_gp, frame->num_fp,
10766                frame->args_size,
10767                frame->cprestore_size);
10768
10769       /* .mask MASK, OFFSET.  */
10770       fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10771                frame->mask, frame->gp_save_offset);
10772
10773       /* .fmask MASK, OFFSET.  */
10774       fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10775                frame->fmask, frame->fp_save_offset);
10776     }
10777
10778   /* Handle the initialization of $gp for SVR4 PIC, if applicable.
10779      Also emit the ".set noreorder; .set nomacro" sequence for functions
10780      that need it.  */
10781   if (mips_must_initialize_gp_p ()
10782       && mips_current_loadgp_style () == LOADGP_OLDABI)
10783     {
10784       if (TARGET_MIPS16)
10785         {
10786           /* This is a fixed-form sequence.  The position of the
10787              first two instructions is important because of the
10788              way _gp_disp is defined.  */
10789           output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
10790           output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
10791           output_asm_insn ("sll\t$2,16", 0);
10792           output_asm_insn ("addu\t$2,$3", 0);
10793         }
10794       else
10795         {
10796           /* .cpload must be in a .set noreorder but not a
10797              .set nomacro block.  */
10798           mips_push_asm_switch (&mips_noreorder);
10799           output_asm_insn (".cpload\t%^", 0);
10800           if (!cfun->machine->all_noreorder_p)
10801             mips_pop_asm_switch (&mips_noreorder);
10802           else
10803             mips_push_asm_switch (&mips_nomacro);
10804         }
10805     }
10806   else if (cfun->machine->all_noreorder_p)
10807     {
10808       mips_push_asm_switch (&mips_noreorder);
10809       mips_push_asm_switch (&mips_nomacro);
10810     }
10811
10812   /* Tell the assembler which register we're using as the global
10813      pointer.  This is needed for thunks, since they can use either
10814      explicit relocs or assembler macros.  */
10815   mips_output_cplocal ();
10816 }
10817
10818 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE.  */
10819
10820 static void
10821 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10822                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10823 {
10824   const char *fnname;
10825
10826   /* Reinstate the normal $gp.  */
10827   SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
10828   mips_output_cplocal ();
10829
10830   if (cfun->machine->all_noreorder_p)
10831     {
10832       mips_pop_asm_switch (&mips_nomacro);
10833       mips_pop_asm_switch (&mips_noreorder);
10834     }
10835
10836   /* Get the function name the same way that toplev.c does before calling
10837      assemble_start_function.  This is needed so that the name used here
10838      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
10839   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10840   mips_end_function_definition (fnname);
10841 }
10842 \f
10843 /* Emit an optimisation barrier for accesses to the current frame.  */
10844
10845 static void
10846 mips_frame_barrier (void)
10847 {
10848   emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
10849 }
10850
10851
10852 /* The __gnu_local_gp symbol.  */
10853
10854 static GTY(()) rtx mips_gnu_local_gp;
10855
10856 /* If we're generating n32 or n64 abicalls, emit instructions
10857    to set up the global pointer.  */
10858
10859 static void
10860 mips_emit_loadgp (void)
10861 {
10862   rtx addr, offset, incoming_address, base, index, pic_reg;
10863
10864   pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10865   switch (mips_current_loadgp_style ())
10866     {
10867     case LOADGP_ABSOLUTE:
10868       if (mips_gnu_local_gp == NULL)
10869         {
10870           mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
10871           SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
10872         }
10873       emit_insn (PMODE_INSN (gen_loadgp_absolute,
10874                              (pic_reg, mips_gnu_local_gp)));
10875       break;
10876
10877     case LOADGP_OLDABI:
10878       /* Added by mips_output_function_prologue.  */
10879       break;
10880
10881     case LOADGP_NEWABI:
10882       addr = XEXP (DECL_RTL (current_function_decl), 0);
10883       offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
10884       incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
10885       emit_insn (PMODE_INSN (gen_loadgp_newabi,
10886                              (pic_reg, offset, incoming_address)));
10887       break;
10888
10889     case LOADGP_RTP:
10890       base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
10891       index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
10892       emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
10893       break;
10894
10895     default:
10896       return;
10897     }
10898
10899   if (TARGET_MIPS16)
10900     emit_insn (PMODE_INSN (gen_copygp_mips16,
10901                            (pic_offset_table_rtx, pic_reg)));
10902
10903   /* Emit a blockage if there are implicit uses of the GP register.
10904      This includes profiled functions, because FUNCTION_PROFILE uses
10905      a jal macro.  */
10906   if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
10907     emit_insn (gen_loadgp_blockage ());
10908 }
10909
10910 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
10911
10912 #if PROBE_INTERVAL > 32768
10913 #error Cannot use indexed addressing mode for stack probing
10914 #endif
10915
10916 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
10917    inclusive.  These are offsets from the current stack pointer.  */
10918
10919 static void
10920 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
10921 {
10922   if (TARGET_MIPS16)
10923     sorry ("-fstack-check=specific not implemented for MIPS16");
10924
10925   /* See if we have a constant small number of probes to generate.  If so,
10926      that's the easy case.  */
10927   if (first + size <= 32768)
10928     {
10929       HOST_WIDE_INT i;
10930
10931       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
10932          it exceeds SIZE.  If only one probe is needed, this will not
10933          generate any code.  Then probe at FIRST + SIZE.  */
10934       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
10935         emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10936                                          -(first + i)));
10937
10938       emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10939                                        -(first + size)));
10940     }
10941
10942   /* Otherwise, do the same as above, but in a loop.  Note that we must be
10943      extra careful with variables wrapping around because we might be at
10944      the very top (or the very bottom) of the address space and we have
10945      to be able to handle this case properly; in particular, we use an
10946      equality test for the loop condition.  */
10947   else
10948     {
10949       HOST_WIDE_INT rounded_size;
10950       rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
10951       rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
10952
10953       /* Sanity check for the addressing mode we're going to use.  */
10954       gcc_assert (first <= 32768);
10955
10956
10957       /* Step 1: round SIZE to the previous multiple of the interval.  */
10958
10959       rounded_size = size & -PROBE_INTERVAL;
10960
10961
10962       /* Step 2: compute initial and final value of the loop counter.  */
10963
10964       /* TEST_ADDR = SP + FIRST.  */
10965       emit_insn (gen_rtx_SET (VOIDmode, r3,
10966                               plus_constant (Pmode, stack_pointer_rtx,
10967                                              -first)));
10968
10969       /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE.  */
10970       if (rounded_size > 32768)
10971         {
10972           emit_move_insn (r12, GEN_INT (rounded_size));
10973           emit_insn (gen_rtx_SET (VOIDmode, r12,
10974                                   gen_rtx_MINUS (Pmode, r3, r12)));
10975         }
10976       else
10977         emit_insn (gen_rtx_SET (VOIDmode, r12,
10978                                 plus_constant (Pmode, r3, -rounded_size)));
10979
10980
10981       /* Step 3: the loop
10982
10983         while (TEST_ADDR != LAST_ADDR)
10984           {
10985             TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
10986             probe at TEST_ADDR
10987           }
10988
10989         probes at FIRST + N * PROBE_INTERVAL for values of N from 1
10990         until it is equal to ROUNDED_SIZE.  */
10991
10992       emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
10993
10994
10995       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
10996          that SIZE is equal to ROUNDED_SIZE.  */
10997
10998       if (size != rounded_size)
10999         emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
11000     }
11001
11002   /* Make sure nothing is scheduled before we are done.  */
11003   emit_insn (gen_blockage ());
11004 }
11005
11006 /* Probe a range of stack addresses from REG1 to REG2 inclusive.  These are
11007    absolute addresses.  */
11008
11009 const char *
11010 mips_output_probe_stack_range (rtx reg1, rtx reg2)
11011 {
11012   static int labelno = 0;
11013   char loop_lab[32], end_lab[32], tmp[64];
11014   rtx xops[2];
11015
11016   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
11017   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
11018
11019   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
11020
11021   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
11022   xops[0] = reg1;
11023   xops[1] = reg2;
11024   strcpy (tmp, "%(%<beq\t%0,%1,");
11025   output_asm_insn (strcat (tmp, &end_lab[1]), xops);
11026  
11027   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
11028   xops[1] = GEN_INT (-PROBE_INTERVAL);
11029   if (TARGET_64BIT && TARGET_LONG64)
11030     output_asm_insn ("daddiu\t%0,%0,%1", xops);
11031   else
11032     output_asm_insn ("addiu\t%0,%0,%1", xops);
11033
11034   /* Probe at TEST_ADDR and branch.  */
11035   fprintf (asm_out_file, "\tb\t");
11036   assemble_name_raw (asm_out_file, loop_lab);
11037   fputc ('\n', asm_out_file);
11038   if (TARGET_64BIT)
11039     output_asm_insn ("sd\t$0,0(%0)%)", xops);
11040   else
11041     output_asm_insn ("sw\t$0,0(%0)%)", xops);
11042
11043   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
11044
11045   return "";
11046 }
11047
11048 /* Return true if X contains a kernel register.  */
11049
11050 static bool
11051 mips_refers_to_kernel_reg_p (const_rtx x)
11052 {
11053   subrtx_iterator::array_type array;
11054   FOR_EACH_SUBRTX (iter, array, x, NONCONST)
11055     if (REG_P (*iter) && KERNEL_REG_P (REGNO (*iter)))
11056       return true;
11057   return false;
11058 }
11059
11060 /* Expand the "prologue" pattern.  */
11061
11062 void
11063 mips_expand_prologue (void)
11064 {
11065   const struct mips_frame_info *frame;
11066   HOST_WIDE_INT size;
11067   unsigned int nargs;
11068
11069   if (cfun->machine->global_pointer != INVALID_REGNUM)
11070     {
11071       /* Check whether an insn uses pic_offset_table_rtx, either explicitly
11072          or implicitly.  If so, we can commit to using a global pointer
11073          straight away, otherwise we need to defer the decision.  */
11074       if (mips_cfun_has_inflexible_gp_ref_p ()
11075           || mips_cfun_has_flexible_gp_ref_p ())
11076         {
11077           cfun->machine->must_initialize_gp_p = true;
11078           cfun->machine->must_restore_gp_when_clobbered_p = true;
11079         }
11080
11081       SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
11082     }
11083
11084   frame = &cfun->machine->frame;
11085   size = frame->total_size;
11086
11087   if (flag_stack_usage_info)
11088     current_function_static_stack_size = size;
11089
11090   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
11091     {
11092       if (crtl->is_leaf && !cfun->calls_alloca)
11093         {
11094           if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
11095             mips_emit_probe_stack_range (STACK_CHECK_PROTECT,
11096                                          size - STACK_CHECK_PROTECT);
11097         }
11098       else if (size > 0)
11099         mips_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
11100     }
11101
11102   /* Save the registers.  Allocate up to MIPS_MAX_FIRST_STACK_STEP
11103      bytes beforehand; this is enough to cover the register save area
11104      without going out of range.  */
11105   if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
11106       || frame->num_cop0_regs > 0)
11107     {
11108       HOST_WIDE_INT step1;
11109
11110       step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
11111       if (GENERATE_MIPS16E_SAVE_RESTORE)
11112         {
11113           HOST_WIDE_INT offset;
11114           unsigned int mask, regno;
11115
11116           /* Try to merge argument stores into the save instruction.  */
11117           nargs = mips16e_collect_argument_saves ();
11118
11119           /* Build the save instruction.  */
11120           mask = frame->mask;
11121           rtx insn = mips16e_build_save_restore (false, &mask, &offset,
11122                                                  nargs, step1);
11123           RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11124           mips_frame_barrier ();
11125           size -= step1;
11126
11127           /* Check if we need to save other registers.  */
11128           for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11129             if (BITSET_P (mask, regno - GP_REG_FIRST))
11130               {
11131                 offset -= UNITS_PER_WORD;
11132                 mips_save_restore_reg (word_mode, regno,
11133                                        offset, mips_save_reg);
11134               }
11135         }
11136       else
11137         {
11138           if (cfun->machine->interrupt_handler_p)
11139             {
11140               HOST_WIDE_INT offset;
11141               rtx mem;
11142
11143               /* If this interrupt is using a shadow register set, we need to
11144                  get the stack pointer from the previous register set.  */
11145               if (cfun->machine->use_shadow_register_set_p)
11146                 emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
11147                                             stack_pointer_rtx));
11148
11149               if (!cfun->machine->keep_interrupts_masked_p)
11150                 {
11151                   /* Move from COP0 Cause to K0.  */
11152                   emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
11153                                             gen_rtx_REG (SImode,
11154                                                          COP0_CAUSE_REG_NUM)));
11155                   /* Move from COP0 EPC to K1.  */
11156                   emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11157                                             gen_rtx_REG (SImode,
11158                                                          COP0_EPC_REG_NUM)));
11159                 }
11160
11161               /* Allocate the first part of the frame.  */
11162               rtx insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
11163                                         GEN_INT (-step1));
11164               RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11165               mips_frame_barrier ();
11166               size -= step1;
11167
11168               /* Start at the uppermost location for saving.  */
11169               offset = frame->cop0_sp_offset - size;
11170               if (!cfun->machine->keep_interrupts_masked_p)
11171                 {
11172                   /* Push EPC into its stack slot.  */
11173                   mem = gen_frame_mem (word_mode,
11174                                        plus_constant (Pmode, stack_pointer_rtx,
11175                                                       offset));
11176                   mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11177                   offset -= UNITS_PER_WORD;
11178                 }
11179
11180               /* Move from COP0 Status to K1.  */
11181               emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11182                                         gen_rtx_REG (SImode,
11183                                                      COP0_STATUS_REG_NUM)));
11184
11185               /* Right justify the RIPL in k0.  */
11186               if (!cfun->machine->keep_interrupts_masked_p)
11187                 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
11188                                         gen_rtx_REG (SImode, K0_REG_NUM),
11189                                         GEN_INT (CAUSE_IPL)));
11190
11191               /* Push Status into its stack slot.  */
11192               mem = gen_frame_mem (word_mode,
11193                                    plus_constant (Pmode, stack_pointer_rtx,
11194                                                   offset));
11195               mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11196               offset -= UNITS_PER_WORD;
11197
11198               /* Insert the RIPL into our copy of SR (k1) as the new IPL.  */
11199               if (!cfun->machine->keep_interrupts_masked_p)
11200                 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11201                                        GEN_INT (6),
11202                                        GEN_INT (SR_IPL),
11203                                        gen_rtx_REG (SImode, K0_REG_NUM)));
11204
11205               if (!cfun->machine->keep_interrupts_masked_p)
11206                 /* Enable interrupts by clearing the KSU ERL and EXL bits.
11207                    IE is already the correct value, so we don't have to do
11208                    anything explicit.  */
11209                 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11210                                        GEN_INT (4),
11211                                        GEN_INT (SR_EXL),
11212                                        gen_rtx_REG (SImode, GP_REG_FIRST)));
11213               else
11214                 /* Disable interrupts by clearing the KSU, ERL, EXL,
11215                    and IE bits.  */
11216                 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11217                                        GEN_INT (5),
11218                                        GEN_INT (SR_IE),
11219                                        gen_rtx_REG (SImode, GP_REG_FIRST)));
11220             }
11221           else
11222             {
11223               rtx insn = gen_add3_insn (stack_pointer_rtx,
11224                                         stack_pointer_rtx,
11225                                         GEN_INT (-step1));
11226               RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11227               mips_frame_barrier ();
11228               size -= step1;
11229             }
11230           mips_for_each_saved_acc (size, mips_save_reg);
11231           mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
11232         }
11233     }
11234
11235   /* Allocate the rest of the frame.  */
11236   if (size > 0)
11237     {
11238       if (SMALL_OPERAND (-size))
11239         RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
11240                                                        stack_pointer_rtx,
11241                                                        GEN_INT (-size)))) = 1;
11242       else
11243         {
11244           mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
11245           if (TARGET_MIPS16)
11246             {
11247               /* There are no instructions to add or subtract registers
11248                  from the stack pointer, so use the frame pointer as a
11249                  temporary.  We should always be using a frame pointer
11250                  in this case anyway.  */
11251               gcc_assert (frame_pointer_needed);
11252               mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11253               emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
11254                                         hard_frame_pointer_rtx,
11255                                         MIPS_PROLOGUE_TEMP (Pmode)));
11256               mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
11257             }
11258           else
11259             emit_insn (gen_sub3_insn (stack_pointer_rtx,
11260                                       stack_pointer_rtx,
11261                                       MIPS_PROLOGUE_TEMP (Pmode)));
11262
11263           /* Describe the combined effect of the previous instructions.  */
11264           mips_set_frame_expr
11265             (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11266                           plus_constant (Pmode, stack_pointer_rtx, -size)));
11267         }
11268       mips_frame_barrier ();
11269     }
11270
11271   /* Set up the frame pointer, if we're using one.  */
11272   if (frame_pointer_needed)
11273     {
11274       HOST_WIDE_INT offset;
11275
11276       offset = frame->hard_frame_pointer_offset;
11277       if (offset == 0)
11278         {
11279           rtx insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11280           RTX_FRAME_RELATED_P (insn) = 1;
11281         }
11282       else if (SMALL_OPERAND (offset))
11283         {
11284           rtx insn = gen_add3_insn (hard_frame_pointer_rtx,
11285                                     stack_pointer_rtx, GEN_INT (offset));
11286           RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11287         }
11288       else
11289         {
11290           mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
11291           mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11292           emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
11293                                     hard_frame_pointer_rtx,
11294                                     MIPS_PROLOGUE_TEMP (Pmode)));
11295           mips_set_frame_expr
11296             (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11297                           plus_constant (Pmode, stack_pointer_rtx, offset)));
11298         }
11299     }
11300
11301   mips_emit_loadgp ();
11302
11303   /* Initialize the $gp save slot.  */
11304   if (mips_cfun_has_cprestore_slot_p ())
11305     {
11306       rtx base, mem, gp, temp;
11307       HOST_WIDE_INT offset;
11308
11309       mips_get_cprestore_base_and_offset (&base, &offset, false);
11310       mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11311       gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
11312       temp = (SMALL_OPERAND (offset)
11313               ? gen_rtx_SCRATCH (Pmode)
11314               : MIPS_PROLOGUE_TEMP (Pmode));
11315       emit_insn (PMODE_INSN (gen_potential_cprestore,
11316                              (mem, GEN_INT (offset), gp, temp)));
11317
11318       mips_get_cprestore_base_and_offset (&base, &offset, true);
11319       mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11320       emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
11321     }
11322
11323   /* We need to search back to the last use of K0 or K1.  */
11324   if (cfun->machine->interrupt_handler_p)
11325     {
11326       rtx_insn *insn;
11327       for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
11328         if (INSN_P (insn)
11329             && mips_refers_to_kernel_reg_p (PATTERN (insn)))
11330           break;
11331       /* Emit a move from K1 to COP0 Status after insn.  */
11332       gcc_assert (insn != NULL_RTX);
11333       emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11334                                       gen_rtx_REG (SImode, K1_REG_NUM)),
11335                        insn);
11336     }
11337
11338   /* If we are profiling, make sure no instructions are scheduled before
11339      the call to mcount.  */
11340   if (crtl->profile)
11341     emit_insn (gen_blockage ());
11342 }
11343 \f
11344 /* Attach all pending register saves to the previous instruction.
11345    Return that instruction.  */
11346
11347 static rtx_insn *
11348 mips_epilogue_emit_cfa_restores (void)
11349 {
11350   rtx_insn *insn;
11351
11352   insn = get_last_insn ();
11353   gcc_assert (insn && !REG_NOTES (insn));
11354   if (mips_epilogue.cfa_restores)
11355     {
11356       RTX_FRAME_RELATED_P (insn) = 1;
11357       REG_NOTES (insn) = mips_epilogue.cfa_restores;
11358       mips_epilogue.cfa_restores = 0;
11359     }
11360   return insn;
11361 }
11362
11363 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
11364    now at REG + OFFSET.  */
11365
11366 static void
11367 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
11368 {
11369   rtx_insn *insn;
11370
11371   insn = mips_epilogue_emit_cfa_restores ();
11372   if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
11373     {
11374       RTX_FRAME_RELATED_P (insn) = 1;
11375       REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
11376                                          plus_constant (Pmode, reg, offset),
11377                                          REG_NOTES (insn));
11378       mips_epilogue.cfa_reg = reg;
11379       mips_epilogue.cfa_offset = offset;
11380     }
11381 }
11382
11383 /* Emit instructions to restore register REG from slot MEM.  Also update
11384    the cfa_restores list.  */
11385
11386 static void
11387 mips_restore_reg (rtx reg, rtx mem)
11388 {
11389   /* There's no MIPS16 instruction to load $31 directly.  Load into
11390      $7 instead and adjust the return insn appropriately.  */
11391   if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
11392     reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
11393   else if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
11394     {
11395       mips_add_cfa_restore (mips_subword (reg, true));
11396       mips_add_cfa_restore (mips_subword (reg, false));
11397     }
11398   else
11399     mips_add_cfa_restore (reg);
11400
11401   mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
11402   if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
11403     /* The CFA is currently defined in terms of the register whose
11404        value we have just restored.  Redefine the CFA in terms of
11405        the stack pointer.  */
11406     mips_epilogue_set_cfa (stack_pointer_rtx,
11407                            mips_epilogue.cfa_restore_sp_offset);
11408 }
11409
11410 /* Emit code to set the stack pointer to BASE + OFFSET, given that
11411    BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
11412    BASE, if not the stack pointer, is available as a temporary.  */
11413
11414 static void
11415 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
11416 {
11417   if (base == stack_pointer_rtx && offset == const0_rtx)
11418     return;
11419
11420   mips_frame_barrier ();
11421   if (offset == const0_rtx)
11422     {
11423       emit_move_insn (stack_pointer_rtx, base);
11424       mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11425     }
11426   else if (TARGET_MIPS16 && base != stack_pointer_rtx)
11427     {
11428       emit_insn (gen_add3_insn (base, base, offset));
11429       mips_epilogue_set_cfa (base, new_frame_size);
11430       emit_move_insn (stack_pointer_rtx, base);
11431     }
11432   else
11433     {
11434       emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
11435       mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11436     }
11437 }
11438
11439 /* Emit any instructions needed before a return.  */
11440
11441 void
11442 mips_expand_before_return (void)
11443 {
11444   /* When using a call-clobbered gp, we start out with unified call
11445      insns that include instructions to restore the gp.  We then split
11446      these unified calls after reload.  These split calls explicitly
11447      clobber gp, so there is no need to define
11448      PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
11449
11450      For consistency, we should also insert an explicit clobber of $28
11451      before return insns, so that the post-reload optimizers know that
11452      the register is not live on exit.  */
11453   if (TARGET_CALL_CLOBBERED_GP)
11454     emit_clobber (pic_offset_table_rtx);
11455 }
11456
11457 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
11458    says which.  */
11459
11460 void
11461 mips_expand_epilogue (bool sibcall_p)
11462 {
11463   const struct mips_frame_info *frame;
11464   HOST_WIDE_INT step1, step2;
11465   rtx base, adjust;
11466   rtx_insn *insn;
11467   bool use_jraddiusp_p = false;
11468
11469   if (!sibcall_p && mips_can_use_return_insn ())
11470     {
11471       emit_jump_insn (gen_return ());
11472       return;
11473     }
11474
11475   /* In MIPS16 mode, if the return value should go into a floating-point
11476      register, we need to call a helper routine to copy it over.  */
11477   if (mips16_cfun_returns_in_fpr_p ())
11478     mips16_copy_fpr_return_value ();
11479
11480   /* Split the frame into two.  STEP1 is the amount of stack we should
11481      deallocate before restoring the registers.  STEP2 is the amount we
11482      should deallocate afterwards.
11483
11484      Start off by assuming that no registers need to be restored.  */
11485   frame = &cfun->machine->frame;
11486   step1 = frame->total_size;
11487   step2 = 0;
11488
11489   /* Work out which register holds the frame address.  */
11490   if (!frame_pointer_needed)
11491     base = stack_pointer_rtx;
11492   else
11493     {
11494       base = hard_frame_pointer_rtx;
11495       step1 -= frame->hard_frame_pointer_offset;
11496     }
11497   mips_epilogue.cfa_reg = base;
11498   mips_epilogue.cfa_offset = step1;
11499   mips_epilogue.cfa_restores = NULL_RTX;
11500
11501   /* If we need to restore registers, deallocate as much stack as
11502      possible in the second step without going out of range.  */
11503   if ((frame->mask | frame->fmask | frame->acc_mask) != 0
11504       || frame->num_cop0_regs > 0)
11505     {
11506       step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
11507       step1 -= step2;
11508     }
11509
11510   /* Get an rtx for STEP1 that we can add to BASE.  */
11511   adjust = GEN_INT (step1);
11512   if (!SMALL_OPERAND (step1))
11513     {
11514       mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
11515       adjust = MIPS_EPILOGUE_TEMP (Pmode);
11516     }
11517   mips_deallocate_stack (base, adjust, step2);
11518
11519   /* If we're using addressing macros, $gp is implicitly used by all
11520      SYMBOL_REFs.  We must emit a blockage insn before restoring $gp
11521      from the stack.  */
11522   if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
11523     emit_insn (gen_blockage ());
11524
11525   mips_epilogue.cfa_restore_sp_offset = step2;
11526   if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
11527     {
11528       unsigned int regno, mask;
11529       HOST_WIDE_INT offset;
11530       rtx restore;
11531
11532       /* Generate the restore instruction.  */
11533       mask = frame->mask;
11534       restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
11535
11536       /* Restore any other registers manually.  */
11537       for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11538         if (BITSET_P (mask, regno - GP_REG_FIRST))
11539           {
11540             offset -= UNITS_PER_WORD;
11541             mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
11542           }
11543
11544       /* Restore the remaining registers and deallocate the final bit
11545          of the frame.  */
11546       mips_frame_barrier ();
11547       emit_insn (restore);
11548       mips_epilogue_set_cfa (stack_pointer_rtx, 0);
11549     }
11550   else
11551     {
11552       /* Restore the registers.  */
11553       mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
11554       mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
11555                                        mips_restore_reg);
11556
11557       if (cfun->machine->interrupt_handler_p)
11558         {
11559           HOST_WIDE_INT offset;
11560           rtx mem;
11561
11562           offset = frame->cop0_sp_offset - (frame->total_size - step2);
11563           if (!cfun->machine->keep_interrupts_masked_p)
11564             {
11565               /* Restore the original EPC.  */
11566               mem = gen_frame_mem (word_mode,
11567                                    plus_constant (Pmode, stack_pointer_rtx,
11568                                                   offset));
11569               mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11570               offset -= UNITS_PER_WORD;
11571
11572               /* Move to COP0 EPC.  */
11573               emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
11574                                         gen_rtx_REG (SImode, K0_REG_NUM)));
11575             }
11576
11577           /* Restore the original Status.  */
11578           mem = gen_frame_mem (word_mode,
11579                                plus_constant (Pmode, stack_pointer_rtx,
11580                                               offset));
11581           mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11582           offset -= UNITS_PER_WORD;
11583
11584           /* If we don't use shadow register set, we need to update SP.  */
11585           if (!cfun->machine->use_shadow_register_set_p)
11586             mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11587           else
11588             /* The choice of position is somewhat arbitrary in this case.  */
11589             mips_epilogue_emit_cfa_restores ();
11590
11591           /* Move to COP0 Status.  */
11592           emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11593                                     gen_rtx_REG (SImode, K0_REG_NUM)));
11594         }
11595       else if (TARGET_MICROMIPS
11596                && !crtl->calls_eh_return
11597                && !sibcall_p
11598                && step2 > 0
11599                && mips_unsigned_immediate_p (step2, 5, 2))
11600         use_jraddiusp_p = true;
11601       else
11602         /* Deallocate the final bit of the frame.  */
11603         mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11604     }
11605
11606   if (!use_jraddiusp_p)
11607     gcc_assert (!mips_epilogue.cfa_restores);
11608
11609   /* Add in the __builtin_eh_return stack adjustment.  We need to
11610      use a temporary in MIPS16 code.  */
11611   if (crtl->calls_eh_return)
11612     {
11613       if (TARGET_MIPS16)
11614         {
11615           mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
11616           emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
11617                                     MIPS_EPILOGUE_TEMP (Pmode),
11618                                     EH_RETURN_STACKADJ_RTX));
11619           mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
11620         }
11621       else
11622         emit_insn (gen_add3_insn (stack_pointer_rtx,
11623                                   stack_pointer_rtx,
11624                                   EH_RETURN_STACKADJ_RTX));
11625     }
11626
11627   if (!sibcall_p)
11628     {
11629       mips_expand_before_return ();
11630       if (cfun->machine->interrupt_handler_p)
11631         {
11632           /* Interrupt handlers generate eret or deret.  */
11633           if (cfun->machine->use_debug_exception_return_p)
11634             emit_jump_insn (gen_mips_deret ());
11635           else
11636             emit_jump_insn (gen_mips_eret ());
11637         }
11638       else
11639         {
11640           rtx pat;
11641
11642           /* When generating MIPS16 code, the normal
11643              mips_for_each_saved_gpr_and_fpr path will restore the return
11644              address into $7 rather than $31.  */
11645           if (TARGET_MIPS16
11646               && !GENERATE_MIPS16E_SAVE_RESTORE
11647               && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
11648             {
11649               /* simple_returns cannot rely on values that are only available
11650                  on paths through the epilogue (because return paths that do
11651                  not pass through the epilogue may nevertheless reuse a
11652                  simple_return that occurs at the end of the epilogue).
11653                  Use a normal return here instead.  */
11654               rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
11655               pat = gen_return_internal (reg);
11656             }
11657           else if (use_jraddiusp_p)
11658             pat = gen_jraddiusp (GEN_INT (step2));
11659           else
11660             {
11661               rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
11662               pat = gen_simple_return_internal (reg);
11663             }
11664           emit_jump_insn (pat);
11665           if (use_jraddiusp_p)
11666             mips_epilogue_set_cfa (stack_pointer_rtx, step2);
11667         }
11668     }
11669
11670   /* Search from the beginning to the first use of K0 or K1.  */
11671   if (cfun->machine->interrupt_handler_p
11672       && !cfun->machine->keep_interrupts_masked_p)
11673     {
11674       for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
11675         if (INSN_P (insn)
11676             && mips_refers_to_kernel_reg_p (PATTERN (insn)))
11677           break;
11678       gcc_assert (insn != NULL_RTX);
11679       /* Insert disable interrupts before the first use of K0 or K1.  */
11680       emit_insn_before (gen_mips_di (), insn);
11681       emit_insn_before (gen_mips_ehb (), insn);
11682     }
11683 }
11684 \f
11685 /* Return nonzero if this function is known to have a null epilogue.
11686    This allows the optimizer to omit jumps to jumps if no stack
11687    was created.  */
11688
11689 bool
11690 mips_can_use_return_insn (void)
11691 {
11692   /* Interrupt handlers need to go through the epilogue.  */
11693   if (cfun->machine->interrupt_handler_p)
11694     return false;
11695
11696   if (!reload_completed)
11697     return false;
11698
11699   if (crtl->profile)
11700     return false;
11701
11702   /* In MIPS16 mode, a function that returns a floating-point value
11703      needs to arrange to copy the return value into the floating-point
11704      registers.  */
11705   if (mips16_cfun_returns_in_fpr_p ())
11706     return false;
11707
11708   return cfun->machine->frame.total_size == 0;
11709 }
11710 \f
11711 /* Return true if register REGNO can store a value of mode MODE.
11712    The result of this function is cached in mips_hard_regno_mode_ok.  */
11713
11714 static bool
11715 mips_hard_regno_mode_ok_p (unsigned int regno, enum machine_mode mode)
11716 {
11717   unsigned int size;
11718   enum mode_class mclass;
11719
11720   if (mode == CCV2mode)
11721     return (ISA_HAS_8CC
11722             && ST_REG_P (regno)
11723             && (regno - ST_REG_FIRST) % 2 == 0);
11724
11725   if (mode == CCV4mode)
11726     return (ISA_HAS_8CC
11727             && ST_REG_P (regno)
11728             && (regno - ST_REG_FIRST) % 4 == 0);
11729
11730   if (mode == CCmode)
11731     return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
11732
11733   size = GET_MODE_SIZE (mode);
11734   mclass = GET_MODE_CLASS (mode);
11735
11736   if (GP_REG_P (regno))
11737     return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
11738
11739   if (FP_REG_P (regno)
11740       && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
11741           || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
11742     {
11743       /* Allow 64-bit vector modes for Loongson-2E/2F.  */
11744       if (TARGET_LOONGSON_VECTORS
11745           && (mode == V2SImode
11746               || mode == V4HImode
11747               || mode == V8QImode
11748               || mode == DImode))
11749         return true;
11750
11751       if (mclass == MODE_FLOAT
11752           || mclass == MODE_COMPLEX_FLOAT
11753           || mclass == MODE_VECTOR_FLOAT)
11754         return size <= UNITS_PER_FPVALUE;
11755
11756       /* Allow integer modes that fit into a single register.  We need
11757          to put integers into FPRs when using instructions like CVT
11758          and TRUNC.  There's no point allowing sizes smaller than a word,
11759          because the FPU has no appropriate load/store instructions.  */
11760       if (mclass == MODE_INT)
11761         return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
11762     }
11763
11764   if (ACC_REG_P (regno)
11765       && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
11766     {
11767       if (MD_REG_P (regno))
11768         {
11769           /* After a multiplication or division, clobbering HI makes
11770              the value of LO unpredictable, and vice versa.  This means
11771              that, for all interesting cases, HI and LO are effectively
11772              a single register.
11773
11774              We model this by requiring that any value that uses HI
11775              also uses LO.  */
11776           if (size <= UNITS_PER_WORD * 2)
11777             return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
11778         }
11779       else
11780         {
11781           /* DSP accumulators do not have the same restrictions as
11782              HI and LO, so we can treat them as normal doubleword
11783              registers.  */
11784           if (size <= UNITS_PER_WORD)
11785             return true;
11786
11787           if (size <= UNITS_PER_WORD * 2
11788               && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
11789             return true;
11790         }
11791     }
11792
11793   if (ALL_COP_REG_P (regno))
11794     return mclass == MODE_INT && size <= UNITS_PER_WORD;
11795
11796   if (regno == GOT_VERSION_REGNUM)
11797     return mode == SImode;
11798
11799   return false;
11800 }
11801
11802 /* Implement HARD_REGNO_NREGS.  */
11803
11804 unsigned int
11805 mips_hard_regno_nregs (int regno, enum machine_mode mode)
11806 {
11807   if (ST_REG_P (regno))
11808     /* The size of FP status registers is always 4, because they only hold
11809        CCmode values, and CCmode is always considered to be 4 bytes wide.  */
11810     return (GET_MODE_SIZE (mode) + 3) / 4;
11811
11812   if (FP_REG_P (regno))
11813     return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
11814
11815   /* All other registers are word-sized.  */
11816   return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
11817 }
11818
11819 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
11820    in mips_hard_regno_nregs.  */
11821
11822 int
11823 mips_class_max_nregs (enum reg_class rclass, enum machine_mode mode)
11824 {
11825   int size;
11826   HARD_REG_SET left;
11827
11828   size = 0x8000;
11829   COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
11830   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
11831     {
11832       if (HARD_REGNO_MODE_OK (ST_REG_FIRST, mode))
11833         size = MIN (size, 4);
11834       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
11835     }
11836   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
11837     {
11838       if (HARD_REGNO_MODE_OK (FP_REG_FIRST, mode))
11839         size = MIN (size, UNITS_PER_FPREG);
11840       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
11841     }
11842   if (!hard_reg_set_empty_p (left))
11843     size = MIN (size, UNITS_PER_WORD);
11844   return (GET_MODE_SIZE (mode) + size - 1) / size;
11845 }
11846
11847 /* Implement CANNOT_CHANGE_MODE_CLASS.  */
11848
11849 bool
11850 mips_cannot_change_mode_class (enum machine_mode from,
11851                                enum machine_mode to,
11852                                enum reg_class rclass)
11853 {
11854   /* Allow conversions between different Loongson integer vectors,
11855      and between those vectors and DImode.  */
11856   if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
11857       && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
11858     return false;
11859
11860   /* Otherwise, there are several problems with changing the modes of
11861      values in floating-point registers:
11862
11863      - When a multi-word value is stored in paired floating-point
11864        registers, the first register always holds the low word.  We
11865        therefore can't allow FPRs to change between single-word and
11866        multi-word modes on big-endian targets.
11867
11868      - GCC assumes that each word of a multiword register can be
11869        accessed individually using SUBREGs.  This is not true for
11870        floating-point registers if they are bigger than a word.
11871
11872      - Loading a 32-bit value into a 64-bit floating-point register
11873        will not sign-extend the value, despite what LOAD_EXTEND_OP
11874        says.  We can't allow FPRs to change from SImode to a wider
11875        mode on 64-bit targets.
11876
11877      - If the FPU has already interpreted a value in one format, we
11878        must not ask it to treat the value as having a different
11879        format.
11880
11881      We therefore disallow all mode changes involving FPRs.  */
11882
11883   return reg_classes_intersect_p (FP_REGS, rclass);
11884 }
11885
11886 /* Implement target hook small_register_classes_for_mode_p.  */
11887
11888 static bool
11889 mips_small_register_classes_for_mode_p (enum machine_mode mode
11890                                         ATTRIBUTE_UNUSED)
11891 {
11892   return TARGET_MIPS16;
11893 }
11894
11895 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction.  */
11896
11897 static bool
11898 mips_mode_ok_for_mov_fmt_p (enum machine_mode mode)
11899 {
11900   switch (mode)
11901     {
11902     case SFmode:
11903       return TARGET_HARD_FLOAT;
11904
11905     case DFmode:
11906       return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
11907
11908     case V2SFmode:
11909       return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
11910
11911     default:
11912       return false;
11913     }
11914 }
11915
11916 /* Implement MODES_TIEABLE_P.  */
11917
11918 bool
11919 mips_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
11920 {
11921   /* FPRs allow no mode punning, so it's not worth tying modes if we'd
11922      prefer to put one of them in FPRs.  */
11923   return (mode1 == mode2
11924           || (!mips_mode_ok_for_mov_fmt_p (mode1)
11925               && !mips_mode_ok_for_mov_fmt_p (mode2)));
11926 }
11927
11928 /* Implement TARGET_PREFERRED_RELOAD_CLASS.  */
11929
11930 static reg_class_t
11931 mips_preferred_reload_class (rtx x, reg_class_t rclass)
11932 {
11933   if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
11934     return LEA_REGS;
11935
11936   if (reg_class_subset_p (FP_REGS, rclass)
11937       && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
11938     return FP_REGS;
11939
11940   if (reg_class_subset_p (GR_REGS, rclass))
11941     rclass = GR_REGS;
11942
11943   if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
11944     rclass = M16_REGS;
11945
11946   return rclass;
11947 }
11948
11949 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
11950    Return a "canonical" class to represent it in later calculations.  */
11951
11952 static reg_class_t
11953 mips_canonicalize_move_class (reg_class_t rclass)
11954 {
11955   /* All moves involving accumulator registers have the same cost.  */
11956   if (reg_class_subset_p (rclass, ACC_REGS))
11957     rclass = ACC_REGS;
11958
11959   /* Likewise promote subclasses of general registers to the most
11960      interesting containing class.  */
11961   if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
11962     rclass = M16_REGS;
11963   else if (reg_class_subset_p (rclass, GENERAL_REGS))
11964     rclass = GENERAL_REGS;
11965
11966   return rclass;
11967 }
11968
11969 /* Return the cost of moving a value from a register of class FROM to a GPR.
11970    Return 0 for classes that are unions of other classes handled by this
11971    function.  */
11972
11973 static int
11974 mips_move_to_gpr_cost (reg_class_t from)
11975 {
11976   switch (from)
11977     {
11978     case M16_REGS:
11979     case GENERAL_REGS:
11980       /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro.  */
11981       return 2;
11982
11983     case ACC_REGS:
11984       /* MFLO and MFHI.  */
11985       return 6;
11986
11987     case FP_REGS:
11988       /* MFC1, etc.  */
11989       return 4;
11990
11991     case COP0_REGS:
11992     case COP2_REGS:
11993     case COP3_REGS:
11994       /* This choice of value is historical.  */
11995       return 5;
11996
11997     default:
11998       return 0;
11999     }
12000 }
12001
12002 /* Return the cost of moving a value from a GPR to a register of class TO.
12003    Return 0 for classes that are unions of other classes handled by this
12004    function.  */
12005
12006 static int
12007 mips_move_from_gpr_cost (reg_class_t to)
12008 {
12009   switch (to)
12010     {
12011     case M16_REGS:
12012     case GENERAL_REGS:
12013       /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro.  */
12014       return 2;
12015
12016     case ACC_REGS:
12017       /* MTLO and MTHI.  */
12018       return 6;
12019
12020     case FP_REGS:
12021       /* MTC1, etc.  */
12022       return 4;
12023
12024     case COP0_REGS:
12025     case COP2_REGS:
12026     case COP3_REGS:
12027       /* This choice of value is historical.  */
12028       return 5;
12029
12030     default:
12031       return 0;
12032     }
12033 }
12034
12035 /* Implement TARGET_REGISTER_MOVE_COST.  Return 0 for classes that are the
12036    maximum of the move costs for subclasses; regclass will work out
12037    the maximum for us.  */
12038
12039 static int
12040 mips_register_move_cost (enum machine_mode mode,
12041                          reg_class_t from, reg_class_t to)
12042 {
12043   reg_class_t dregs;
12044   int cost1, cost2;
12045
12046   from = mips_canonicalize_move_class (from);
12047   to = mips_canonicalize_move_class (to);
12048
12049   /* Handle moves that can be done without using general-purpose registers.  */
12050   if (from == FP_REGS)
12051     {
12052       if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
12053         /* MOV.FMT.  */
12054         return 4;
12055     }
12056
12057   /* Handle cases in which only one class deviates from the ideal.  */
12058   dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
12059   if (from == dregs)
12060     return mips_move_from_gpr_cost (to);
12061   if (to == dregs)
12062     return mips_move_to_gpr_cost (from);
12063
12064   /* Handles cases that require a GPR temporary.  */
12065   cost1 = mips_move_to_gpr_cost (from);
12066   if (cost1 != 0)
12067     {
12068       cost2 = mips_move_from_gpr_cost (to);
12069       if (cost2 != 0)
12070         return cost1 + cost2;
12071     }
12072
12073   return 0;
12074 }
12075
12076 /* Implement TARGET_REGISTER_PRIORITY.  */
12077
12078 static int
12079 mips_register_priority (int hard_regno)
12080 {
12081   /* Treat MIPS16 registers with higher priority than other regs.  */
12082   if (TARGET_MIPS16
12083       && TEST_HARD_REG_BIT (reg_class_contents[M16_REGS], hard_regno))
12084     return 1;
12085   return 0;
12086 }
12087
12088 /* Implement TARGET_MEMORY_MOVE_COST.  */
12089
12090 static int
12091 mips_memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
12092 {
12093   return (mips_cost->memory_latency
12094           + memory_move_secondary_cost (mode, rclass, in));
12095
12096
12097 /* Return the register class required for a secondary register when
12098    copying between one of the registers in RCLASS and value X, which
12099    has mode MODE.  X is the source of the move if IN_P, otherwise it
12100    is the destination.  Return NO_REGS if no secondary register is
12101    needed.  */
12102
12103 enum reg_class
12104 mips_secondary_reload_class (enum reg_class rclass,
12105                              enum machine_mode mode, rtx x, bool)
12106 {
12107   int regno;
12108
12109   /* If X is a constant that cannot be loaded into $25, it must be loaded
12110      into some other GPR.  No other register class allows a direct move.  */
12111   if (mips_dangerous_for_la25_p (x))
12112     return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
12113
12114   regno = true_regnum (x);
12115   if (TARGET_MIPS16)
12116     {
12117       /* In MIPS16 mode, every move must involve a member of M16_REGS.  */
12118       if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
12119         return M16_REGS;
12120
12121       return NO_REGS;
12122     }
12123
12124   /* Copying from accumulator registers to anywhere other than a general
12125      register requires a temporary general register.  */
12126   if (reg_class_subset_p (rclass, ACC_REGS))
12127     return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12128   if (ACC_REG_P (regno))
12129     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12130
12131   if (reg_class_subset_p (rclass, FP_REGS))
12132     {
12133       if (regno < 0
12134           || (MEM_P (x)
12135               && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)))
12136         /* In this case we can use lwc1, swc1, ldc1 or sdc1.  We'll use
12137            pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported.  */
12138         return NO_REGS;
12139
12140       if (GP_REG_P (regno) || x == CONST0_RTX (mode))
12141         /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1.  */
12142         return NO_REGS;
12143
12144       if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
12145         /* We can force the constant to memory and use lwc1
12146            and ldc1.  As above, we will use pairs of lwc1s if
12147            ldc1 is not supported.  */
12148         return NO_REGS;
12149
12150       if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
12151         /* In this case we can use mov.fmt.  */
12152         return NO_REGS;
12153
12154       /* Otherwise, we need to reload through an integer register.  */
12155       return GR_REGS;
12156     }
12157   if (FP_REG_P (regno))
12158     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12159
12160   return NO_REGS;
12161 }
12162
12163 /* Implement TARGET_MODE_REP_EXTENDED.  */
12164
12165 static int
12166 mips_mode_rep_extended (enum machine_mode mode, enum machine_mode mode_rep)
12167 {
12168   /* On 64-bit targets, SImode register values are sign-extended to DImode.  */
12169   if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
12170     return SIGN_EXTEND;
12171
12172   return UNKNOWN;
12173 }
12174 \f
12175 /* Implement TARGET_VALID_POINTER_MODE.  */
12176
12177 static bool
12178 mips_valid_pointer_mode (enum machine_mode mode)
12179 {
12180   return mode == SImode || (TARGET_64BIT && mode == DImode);
12181 }
12182
12183 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P.  */
12184
12185 static bool
12186 mips_vector_mode_supported_p (enum machine_mode mode)
12187 {
12188   switch (mode)
12189     {
12190     case V2SFmode:
12191       return TARGET_PAIRED_SINGLE_FLOAT;
12192
12193     case V2HImode:
12194     case V4QImode:
12195     case V2HQmode:
12196     case V2UHQmode:
12197     case V2HAmode:
12198     case V2UHAmode:
12199     case V4QQmode:
12200     case V4UQQmode:
12201       return TARGET_DSP;
12202
12203     case V2SImode:
12204     case V4HImode:
12205     case V8QImode:
12206       return TARGET_LOONGSON_VECTORS;
12207
12208     default:
12209       return false;
12210     }
12211 }
12212
12213 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.  */
12214
12215 static bool
12216 mips_scalar_mode_supported_p (enum machine_mode mode)
12217 {
12218   if (ALL_FIXED_POINT_MODE_P (mode)
12219       && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
12220     return true;
12221
12222   return default_scalar_mode_supported_p (mode);
12223 }
12224 \f
12225 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE.  */
12226
12227 static enum machine_mode
12228 mips_preferred_simd_mode (enum machine_mode mode ATTRIBUTE_UNUSED)
12229 {
12230   if (TARGET_PAIRED_SINGLE_FLOAT
12231       && mode == SFmode)
12232     return V2SFmode;
12233   return word_mode;
12234 }
12235
12236 /* Implement TARGET_INIT_LIBFUNCS.  */
12237
12238 static void
12239 mips_init_libfuncs (void)
12240 {
12241   if (TARGET_FIX_VR4120)
12242     {
12243       /* Register the special divsi3 and modsi3 functions needed to work
12244          around VR4120 division errata.  */
12245       set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
12246       set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
12247     }
12248
12249   if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
12250     {
12251       /* Register the MIPS16 -mhard-float stubs.  */
12252       set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
12253       set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
12254       set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
12255       set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
12256
12257       set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
12258       set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
12259       set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
12260       set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
12261       set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
12262       set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
12263       set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
12264
12265       set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
12266       set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
12267       set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
12268
12269       if (TARGET_DOUBLE_FLOAT)
12270         {
12271           set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
12272           set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
12273           set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
12274           set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
12275
12276           set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
12277           set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
12278           set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
12279           set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
12280           set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
12281           set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
12282           set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
12283
12284           set_conv_libfunc (sext_optab, DFmode, SFmode,
12285                             "__mips16_extendsfdf2");
12286           set_conv_libfunc (trunc_optab, SFmode, DFmode,
12287                             "__mips16_truncdfsf2");
12288           set_conv_libfunc (sfix_optab, SImode, DFmode,
12289                             "__mips16_fix_truncdfsi");
12290           set_conv_libfunc (sfloat_optab, DFmode, SImode,
12291                             "__mips16_floatsidf");
12292           set_conv_libfunc (ufloat_optab, DFmode, SImode,
12293                             "__mips16_floatunsidf");
12294         }
12295     }
12296
12297   /* The MIPS16 ISA does not have an encoding for "sync", so we rely
12298      on an external non-MIPS16 routine to implement __sync_synchronize.
12299      Similarly for the rest of the ll/sc libfuncs.  */
12300   if (TARGET_MIPS16)
12301     {
12302       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
12303       init_sync_libfuncs (UNITS_PER_WORD);
12304     }
12305 }
12306
12307 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
12308
12309 static void
12310 mips_process_load_label (rtx target)
12311 {
12312   rtx base, gp, intop;
12313   HOST_WIDE_INT offset;
12314
12315   mips_multi_start ();
12316   switch (mips_abi)
12317     {
12318     case ABI_N32:
12319       mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
12320       mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
12321       break;
12322
12323     case ABI_64:
12324       mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
12325       mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
12326       break;
12327
12328     default:
12329       gp = pic_offset_table_rtx;
12330       if (mips_cfun_has_cprestore_slot_p ())
12331         {
12332           gp = gen_rtx_REG (Pmode, AT_REGNUM);
12333           mips_get_cprestore_base_and_offset (&base, &offset, true);
12334           if (!SMALL_OPERAND (offset))
12335             {
12336               intop = GEN_INT (CONST_HIGH_PART (offset));
12337               mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
12338               mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
12339
12340               base = gp;
12341               offset = CONST_LOW_PART (offset);
12342             }
12343           intop = GEN_INT (offset);
12344           if (ISA_HAS_LOAD_DELAY)
12345             mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
12346           else
12347             mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
12348         }
12349       if (ISA_HAS_LOAD_DELAY)
12350         mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
12351       else
12352         mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
12353       mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
12354       break;
12355     }
12356 }
12357
12358 /* Return the number of instructions needed to load a label into $AT.  */
12359
12360 static unsigned int
12361 mips_load_label_num_insns (void)
12362 {
12363   if (cfun->machine->load_label_num_insns == 0)
12364     {
12365       mips_process_load_label (pc_rtx);
12366       cfun->machine->load_label_num_insns = mips_multi_num_insns;
12367     }
12368   return cfun->machine->load_label_num_insns;
12369 }
12370
12371 /* Emit an asm sequence to start a noat block and load the address
12372    of a label into $1.  */
12373
12374 void
12375 mips_output_load_label (rtx target)
12376 {
12377   mips_push_asm_switch (&mips_noat);
12378   if (TARGET_EXPLICIT_RELOCS)
12379     {
12380       mips_process_load_label (target);
12381       mips_multi_write ();
12382     }
12383   else
12384     {
12385       if (Pmode == DImode)
12386         output_asm_insn ("dla\t%@,%0", &target);
12387       else
12388         output_asm_insn ("la\t%@,%0", &target);
12389     }
12390 }
12391
12392 /* Return the length of INSN.  LENGTH is the initial length computed by
12393    attributes in the machine-description file.  */
12394
12395 int
12396 mips_adjust_insn_length (rtx_insn *insn, int length)
12397 {
12398   /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
12399      of a PIC long-branch sequence.  Substitute the correct value.  */
12400   if (length == MAX_PIC_BRANCH_LENGTH
12401       && JUMP_P (insn)
12402       && INSN_CODE (insn) >= 0
12403       && get_attr_type (insn) == TYPE_BRANCH)
12404     {
12405       /* Add the branch-over instruction and its delay slot, if this
12406          is a conditional branch.  */
12407       length = simplejump_p (insn) ? 0 : 8;
12408
12409       /* Add the size of a load into $AT.  */
12410       length += BASE_INSN_LENGTH * mips_load_label_num_insns ();
12411
12412       /* Add the length of an indirect jump, ignoring the delay slot.  */
12413       length += TARGET_COMPRESSION ? 2 : 4;
12414     }
12415
12416   /* A unconditional jump has an unfilled delay slot if it is not part
12417      of a sequence.  A conditional jump normally has a delay slot, but
12418      does not on MIPS16.  */
12419   if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
12420     length += TARGET_MIPS16 ? 2 : 4;
12421
12422   /* See how many nops might be needed to avoid hardware hazards.  */
12423   if (!cfun->machine->ignore_hazard_length_p
12424       && INSN_P (insn)
12425       && INSN_CODE (insn) >= 0)
12426     switch (get_attr_hazard (insn))
12427       {
12428       case HAZARD_NONE:
12429         break;
12430
12431       case HAZARD_DELAY:
12432         length += NOP_INSN_LENGTH;
12433         break;
12434
12435       case HAZARD_HILO:
12436         length += NOP_INSN_LENGTH * 2;
12437         break;
12438       }
12439
12440   return length;
12441 }
12442
12443 /* Return the assembly code for INSN, which has the operands given by
12444    OPERANDS, and which branches to OPERANDS[0] if some condition is true.
12445    BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
12446    is in range of a direct branch.  BRANCH_IF_FALSE is an inverted
12447    version of BRANCH_IF_TRUE.  */
12448
12449 const char *
12450 mips_output_conditional_branch (rtx_insn *insn, rtx *operands,
12451                                 const char *branch_if_true,
12452                                 const char *branch_if_false)
12453 {
12454   unsigned int length;
12455   rtx taken;
12456
12457   gcc_assert (LABEL_P (operands[0]));
12458
12459   length = get_attr_length (insn);
12460   if (length <= 8)
12461     {
12462       /* Just a simple conditional branch.  */
12463       mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
12464       return branch_if_true;
12465     }
12466
12467   /* Generate a reversed branch around a direct jump.  This fallback does
12468      not use branch-likely instructions.  */
12469   mips_branch_likely = false;
12470   rtx_code_label *not_taken = gen_label_rtx ();
12471   taken = operands[0];
12472
12473   /* Generate the reversed branch to NOT_TAKEN.  */
12474   operands[0] = not_taken;
12475   output_asm_insn (branch_if_false, operands);
12476
12477   /* If INSN has a delay slot, we must provide delay slots for both the
12478      branch to NOT_TAKEN and the conditional jump.  We must also ensure
12479      that INSN's delay slot is executed in the appropriate cases.  */
12480   if (final_sequence)
12481     {
12482       /* This first delay slot will always be executed, so use INSN's
12483          delay slot if is not annulled.  */
12484       if (!INSN_ANNULLED_BRANCH_P (insn))
12485         {
12486           final_scan_insn (final_sequence->insn (1),
12487                            asm_out_file, optimize, 1, NULL);
12488           final_sequence->insn (1)->set_deleted ();
12489         }
12490       else
12491         output_asm_insn ("nop", 0);
12492       fprintf (asm_out_file, "\n");
12493     }
12494
12495   /* Output the unconditional branch to TAKEN.  */
12496   if (TARGET_ABSOLUTE_JUMPS)
12497     output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
12498   else
12499     {
12500       mips_output_load_label (taken);
12501       output_asm_insn ("jr\t%@%]%/", 0);
12502     }
12503
12504   /* Now deal with its delay slot; see above.  */
12505   if (final_sequence)
12506     {
12507       /* This delay slot will only be executed if the branch is taken.
12508          Use INSN's delay slot if is annulled.  */
12509       if (INSN_ANNULLED_BRANCH_P (insn))
12510         {
12511           final_scan_insn (final_sequence->insn (1),
12512                            asm_out_file, optimize, 1, NULL);
12513           final_sequence->insn (1)->set_deleted ();
12514         }
12515       else
12516         output_asm_insn ("nop", 0);
12517       fprintf (asm_out_file, "\n");
12518     }
12519
12520   /* Output NOT_TAKEN.  */
12521   targetm.asm_out.internal_label (asm_out_file, "L",
12522                                   CODE_LABEL_NUMBER (not_taken));
12523   return "";
12524 }
12525
12526 /* Return the assembly code for INSN, which branches to OPERANDS[0]
12527    if some ordering condition is true.  The condition is given by
12528    OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
12529    OPERANDS[1].  OPERANDS[2] is the comparison's first operand;
12530    its second is always zero.  */
12531
12532 const char *
12533 mips_output_order_conditional_branch (rtx_insn *insn, rtx *operands, bool inverted_p)
12534 {
12535   const char *branch[2];
12536
12537   /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
12538      Make BRANCH[0] branch on the inverse condition.  */
12539   switch (GET_CODE (operands[1]))
12540     {
12541       /* These cases are equivalent to comparisons against zero.  */
12542     case LEU:
12543       inverted_p = !inverted_p;
12544       /* Fall through.  */
12545     case GTU:
12546       branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
12547       branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
12548       break;
12549
12550       /* These cases are always true or always false.  */
12551     case LTU:
12552       inverted_p = !inverted_p;
12553       /* Fall through.  */
12554     case GEU:
12555       branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
12556       branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
12557       break;
12558
12559     default:
12560       branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
12561       branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
12562       break;
12563     }
12564   return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
12565 }
12566 \f
12567 /* Start a block of code that needs access to the LL, SC and SYNC
12568    instructions.  */
12569
12570 static void
12571 mips_start_ll_sc_sync_block (void)
12572 {
12573   if (!ISA_HAS_LL_SC)
12574     {
12575       output_asm_insn (".set\tpush", 0);
12576       if (TARGET_64BIT)
12577         output_asm_insn (".set\tmips3", 0);
12578       else
12579         output_asm_insn (".set\tmips2", 0);
12580     }
12581 }
12582
12583 /* End a block started by mips_start_ll_sc_sync_block.  */
12584
12585 static void
12586 mips_end_ll_sc_sync_block (void)
12587 {
12588   if (!ISA_HAS_LL_SC)
12589     output_asm_insn (".set\tpop", 0);
12590 }
12591
12592 /* Output and/or return the asm template for a sync instruction.  */
12593
12594 const char *
12595 mips_output_sync (void)
12596 {
12597   mips_start_ll_sc_sync_block ();
12598   output_asm_insn ("sync", 0);
12599   mips_end_ll_sc_sync_block ();
12600   return "";
12601 }
12602
12603 /* Return the asm template associated with sync_insn1 value TYPE.
12604    IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation.  */
12605
12606 static const char *
12607 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
12608 {
12609   switch (type)
12610     {
12611     case SYNC_INSN1_MOVE:
12612       return "move\t%0,%z2";
12613     case SYNC_INSN1_LI:
12614       return "li\t%0,%2";
12615     case SYNC_INSN1_ADDU:
12616       return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
12617     case SYNC_INSN1_ADDIU:
12618       return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
12619     case SYNC_INSN1_SUBU:
12620       return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
12621     case SYNC_INSN1_AND:
12622       return "and\t%0,%1,%z2";
12623     case SYNC_INSN1_ANDI:
12624       return "andi\t%0,%1,%2";
12625     case SYNC_INSN1_OR:
12626       return "or\t%0,%1,%z2";
12627     case SYNC_INSN1_ORI:
12628       return "ori\t%0,%1,%2";
12629     case SYNC_INSN1_XOR:
12630       return "xor\t%0,%1,%z2";
12631     case SYNC_INSN1_XORI:
12632       return "xori\t%0,%1,%2";
12633     }
12634   gcc_unreachable ();
12635 }
12636
12637 /* Return the asm template associated with sync_insn2 value TYPE.  */
12638
12639 static const char *
12640 mips_sync_insn2_template (enum attr_sync_insn2 type)
12641 {
12642   switch (type)
12643     {
12644     case SYNC_INSN2_NOP:
12645       gcc_unreachable ();
12646     case SYNC_INSN2_AND:
12647       return "and\t%0,%1,%z2";
12648     case SYNC_INSN2_XOR:
12649       return "xor\t%0,%1,%z2";
12650     case SYNC_INSN2_NOT:
12651       return "nor\t%0,%1,%.";
12652     }
12653   gcc_unreachable ();
12654 }
12655
12656 /* OPERANDS are the operands to a sync loop instruction and INDEX is
12657    the value of the one of the sync_* attributes.  Return the operand
12658    referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
12659    have the associated attribute.  */
12660
12661 static rtx
12662 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
12663 {
12664   if (index > 0)
12665     default_value = operands[index - 1];
12666   return default_value;
12667 }
12668
12669 /* INSN is a sync loop with operands OPERANDS.  Build up a multi-insn
12670    sequence for it.  */
12671
12672 static void
12673 mips_process_sync_loop (rtx_insn *insn, rtx *operands)
12674 {
12675   rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
12676   rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
12677   unsigned int tmp3_insn;
12678   enum attr_sync_insn1 insn1;
12679   enum attr_sync_insn2 insn2;
12680   bool is_64bit_p;
12681   int memmodel_attr;
12682   enum memmodel model;
12683
12684   /* Read an operand from the sync_WHAT attribute and store it in
12685      variable WHAT.  DEFAULT is the default value if no attribute
12686      is specified.  */
12687 #define READ_OPERAND(WHAT, DEFAULT) \
12688   WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
12689                                 DEFAULT)
12690
12691   /* Read the memory.  */
12692   READ_OPERAND (mem, 0);
12693   gcc_assert (mem);
12694   is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
12695
12696   /* Read the other attributes.  */
12697   at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
12698   READ_OPERAND (oldval, at);
12699   READ_OPERAND (cmp, 0);
12700   READ_OPERAND (newval, at);
12701   READ_OPERAND (inclusive_mask, 0);
12702   READ_OPERAND (exclusive_mask, 0);
12703   READ_OPERAND (required_oldval, 0);
12704   READ_OPERAND (insn1_op2, 0);
12705   insn1 = get_attr_sync_insn1 (insn);
12706   insn2 = get_attr_sync_insn2 (insn);
12707
12708   /* Don't bother setting CMP result that is never used.  */
12709   if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
12710     cmp = 0;
12711
12712   memmodel_attr = get_attr_sync_memmodel (insn);
12713   switch (memmodel_attr)
12714     {
12715     case 10:
12716       model = MEMMODEL_ACQ_REL;
12717       break;
12718     case 11:
12719       model = MEMMODEL_ACQUIRE;
12720       break;
12721     default:
12722       model = (enum memmodel) INTVAL (operands[memmodel_attr]);
12723     }
12724
12725   mips_multi_start ();
12726
12727   /* Output the release side of the memory barrier.  */
12728   if (need_atomic_barrier_p (model, true))
12729     {
12730       if (required_oldval == 0 && TARGET_OCTEON)
12731         {
12732           /* Octeon doesn't reorder reads, so a full barrier can be
12733              created by using SYNCW to order writes combined with the
12734              write from the following SC.  When the SC successfully
12735              completes, we know that all preceding writes are also
12736              committed to the coherent memory system.  It is possible
12737              for a single SYNCW to fail, but a pair of them will never
12738              fail, so we use two.  */
12739           mips_multi_add_insn ("syncw", NULL);
12740           mips_multi_add_insn ("syncw", NULL);
12741         }
12742       else
12743         mips_multi_add_insn ("sync", NULL);
12744     }
12745
12746   /* Output the branch-back label.  */
12747   mips_multi_add_label ("1:");
12748
12749   /* OLDVAL = *MEM.  */
12750   mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
12751                        oldval, mem, NULL);
12752
12753   /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2.  */
12754   if (required_oldval)
12755     {
12756       if (inclusive_mask == 0)
12757         tmp1 = oldval;
12758       else
12759         {
12760           gcc_assert (oldval != at);
12761           mips_multi_add_insn ("and\t%0,%1,%2",
12762                                at, oldval, inclusive_mask, NULL);
12763           tmp1 = at;
12764         }
12765       mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
12766
12767       /* CMP = 0 [delay slot].  */
12768       if (cmp)
12769         mips_multi_add_insn ("li\t%0,0", cmp, NULL);
12770     }
12771
12772   /* $TMP1 = OLDVAL & EXCLUSIVE_MASK.  */
12773   if (exclusive_mask == 0)
12774     tmp1 = const0_rtx;
12775   else
12776     {
12777       gcc_assert (oldval != at);
12778       mips_multi_add_insn ("and\t%0,%1,%z2",
12779                            at, oldval, exclusive_mask, NULL);
12780       tmp1 = at;
12781     }
12782
12783   /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
12784
12785      We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
12786      at least one instruction in that case.  */
12787   if (insn1 == SYNC_INSN1_MOVE
12788       && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
12789     tmp2 = insn1_op2;
12790   else
12791     {
12792       mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
12793                            newval, oldval, insn1_op2, NULL);
12794       tmp2 = newval;
12795     }
12796
12797   /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK).  */
12798   if (insn2 == SYNC_INSN2_NOP)
12799     tmp3 = tmp2;
12800   else
12801     {
12802       mips_multi_add_insn (mips_sync_insn2_template (insn2),
12803                            newval, tmp2, inclusive_mask, NULL);
12804       tmp3 = newval;
12805     }
12806   tmp3_insn = mips_multi_last_index ();
12807
12808   /* $AT = $TMP1 | $TMP3.  */
12809   if (tmp1 == const0_rtx || tmp3 == const0_rtx)
12810     {
12811       mips_multi_set_operand (tmp3_insn, 0, at);
12812       tmp3 = at;
12813     }
12814   else
12815     {
12816       gcc_assert (tmp1 != tmp3);
12817       mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
12818     }
12819
12820   /* if (!commit (*MEM = $AT)) goto 1.
12821
12822      This will sometimes be a delayed branch; see the write code below
12823      for details.  */
12824   mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
12825   mips_multi_add_insn ("beq%?\t%0,%.,1b", at, NULL);
12826
12827   /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot].  */
12828   if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
12829     {
12830       mips_multi_copy_insn (tmp3_insn);
12831       mips_multi_set_operand (mips_multi_last_index (), 0, newval);
12832     }
12833   else if (!(required_oldval && cmp))
12834     mips_multi_add_insn ("nop", NULL);
12835
12836   /* CMP = 1 -- either standalone or in a delay slot.  */
12837   if (required_oldval && cmp)
12838     mips_multi_add_insn ("li\t%0,1", cmp, NULL);
12839
12840   /* Output the acquire side of the memory barrier.  */
12841   if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
12842     mips_multi_add_insn ("sync", NULL);
12843
12844   /* Output the exit label, if needed.  */
12845   if (required_oldval)
12846     mips_multi_add_label ("2:");
12847
12848 #undef READ_OPERAND
12849 }
12850
12851 /* Output and/or return the asm template for sync loop INSN, which has
12852    the operands given by OPERANDS.  */
12853
12854 const char *
12855 mips_output_sync_loop (rtx_insn *insn, rtx *operands)
12856 {
12857   mips_process_sync_loop (insn, operands);
12858
12859   /* Use branch-likely instructions to work around the LL/SC R10000
12860      errata.  */
12861   mips_branch_likely = TARGET_FIX_R10000;
12862
12863   mips_push_asm_switch (&mips_noreorder);
12864   mips_push_asm_switch (&mips_nomacro);
12865   mips_push_asm_switch (&mips_noat);
12866   mips_start_ll_sc_sync_block ();
12867
12868   mips_multi_write ();
12869
12870   mips_end_ll_sc_sync_block ();
12871   mips_pop_asm_switch (&mips_noat);
12872   mips_pop_asm_switch (&mips_nomacro);
12873   mips_pop_asm_switch (&mips_noreorder);
12874
12875   return "";
12876 }
12877
12878 /* Return the number of individual instructions in sync loop INSN,
12879    which has the operands given by OPERANDS.  */
12880
12881 unsigned int
12882 mips_sync_loop_insns (rtx_insn *insn, rtx *operands)
12883 {
12884   mips_process_sync_loop (insn, operands);
12885   return mips_multi_num_insns;
12886 }
12887 \f
12888 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
12889    the operands given by OPERANDS.  Add in a divide-by-zero check if needed.
12890
12891    When working around R4000 and R4400 errata, we need to make sure that
12892    the division is not immediately followed by a shift[1][2].  We also
12893    need to stop the division from being put into a branch delay slot[3].
12894    The easiest way to avoid both problems is to add a nop after the
12895    division.  When a divide-by-zero check is needed, this nop can be
12896    used to fill the branch delay slot.
12897
12898    [1] If a double-word or a variable shift executes immediately
12899        after starting an integer division, the shift may give an
12900        incorrect result.  See quotations of errata #16 and #28 from
12901        "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12902        in mips.md for details.
12903
12904    [2] A similar bug to [1] exists for all revisions of the
12905        R4000 and the R4400 when run in an MC configuration.
12906        From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
12907
12908        "19. In this following sequence:
12909
12910                     ddiv                (or ddivu or div or divu)
12911                     dsll32              (or dsrl32, dsra32)
12912
12913             if an MPT stall occurs, while the divide is slipping the cpu
12914             pipeline, then the following double shift would end up with an
12915             incorrect result.
12916
12917             Workaround: The compiler needs to avoid generating any
12918             sequence with divide followed by extended double shift."
12919
12920        This erratum is also present in "MIPS R4400MC Errata, Processor
12921        Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
12922        & 3.0" as errata #10 and #4, respectively.
12923
12924    [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12925        (also valid for MIPS R4000MC processors):
12926
12927        "52. R4000SC: This bug does not apply for the R4000PC.
12928
12929             There are two flavors of this bug:
12930
12931             1) If the instruction just after divide takes an RF exception
12932                (tlb-refill, tlb-invalid) and gets an instruction cache
12933                miss (both primary and secondary) and the line which is
12934                currently in secondary cache at this index had the first
12935                data word, where the bits 5..2 are set, then R4000 would
12936                get a wrong result for the div.
12937
12938             ##1
12939                     nop
12940                     div r8, r9
12941                     -------------------         # end-of page. -tlb-refill
12942                     nop
12943             ##2
12944                     nop
12945                     div r8, r9
12946                     -------------------         # end-of page. -tlb-invalid
12947                     nop
12948
12949             2) If the divide is in the taken branch delay slot, where the
12950                target takes RF exception and gets an I-cache miss for the
12951                exception vector or where I-cache miss occurs for the
12952                target address, under the above mentioned scenarios, the
12953                div would get wrong results.
12954
12955             ##1
12956                     j   r2              # to next page mapped or unmapped
12957                     div r8,r9           # this bug would be there as long
12958                                         # as there is an ICache miss and
12959                     nop                 # the "data pattern" is present
12960
12961             ##2
12962                     beq r0, r0, NextPage        # to Next page
12963                     div r8,r9
12964                     nop
12965
12966             This bug is present for div, divu, ddiv, and ddivu
12967             instructions.
12968
12969             Workaround: For item 1), OS could make sure that the next page
12970             after the divide instruction is also mapped.  For item 2), the
12971             compiler could make sure that the divide instruction is not in
12972             the branch delay slot."
12973
12974        These processors have PRId values of 0x00004220 and 0x00004300 for
12975        the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400.  */
12976
12977 const char *
12978 mips_output_division (const char *division, rtx *operands)
12979 {
12980   const char *s;
12981
12982   s = division;
12983   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
12984     {
12985       output_asm_insn (s, operands);
12986       s = "nop";
12987     }
12988   if (TARGET_CHECK_ZERO_DIV)
12989     {
12990       if (TARGET_MIPS16)
12991         {
12992           output_asm_insn (s, operands);
12993           s = "bnez\t%2,1f\n\tbreak\t7\n1:";
12994         }
12995       else if (GENERATE_DIVIDE_TRAPS)
12996         {
12997           /* Avoid long replay penalty on load miss by putting the trap before
12998              the divide.  */
12999           if (TUNE_74K)
13000             output_asm_insn ("teq\t%2,%.,7", operands);
13001           else
13002             {
13003               output_asm_insn (s, operands);
13004               s = "teq\t%2,%.,7";
13005             }
13006         }
13007       else
13008         {
13009           output_asm_insn ("%(bne\t%2,%.,1f", operands);
13010           output_asm_insn (s, operands);
13011           s = "break\t7%)\n1:";
13012         }
13013     }
13014   return s;
13015 }
13016 \f
13017 /* Return true if destination of IN_INSN is used as add source in
13018    OUT_INSN. Both IN_INSN and OUT_INSN are of type fmadd. Example:
13019    madd.s dst, x, y, z
13020    madd.s a, dst, b, c  */
13021
13022 bool
13023 mips_fmadd_bypass (rtx_insn *out_insn, rtx_insn *in_insn)
13024 {
13025   int dst_reg, src_reg;
13026   
13027   gcc_assert (get_attr_type (in_insn) == TYPE_FMADD);
13028   gcc_assert (get_attr_type (out_insn) == TYPE_FMADD);
13029
13030   extract_insn (in_insn);
13031   dst_reg = REG_P (recog_data.operand[0]);
13032
13033   extract_insn (out_insn);
13034   src_reg = REG_P (recog_data.operand[1]);
13035
13036   if (dst_reg == src_reg)
13037     return true;
13038
13039   return false;
13040 }
13041
13042 /* Return true if IN_INSN is a multiply-add or multiply-subtract
13043    instruction and if OUT_INSN assigns to the accumulator operand.  */
13044
13045 bool
13046 mips_linked_madd_p (rtx_insn *out_insn, rtx_insn *in_insn)
13047 {
13048   enum attr_accum_in accum_in;
13049   int accum_in_opnum;
13050   rtx accum_in_op;
13051
13052   if (recog_memoized (in_insn) < 0)
13053     return false;
13054
13055   accum_in = get_attr_accum_in (in_insn);
13056   if (accum_in == ACCUM_IN_NONE)
13057     return false;
13058
13059   accum_in_opnum = accum_in - ACCUM_IN_0;
13060
13061   extract_insn (in_insn);
13062   gcc_assert (accum_in_opnum < recog_data.n_operands);
13063   accum_in_op = recog_data.operand[accum_in_opnum];
13064
13065   return reg_set_p (accum_in_op, out_insn);
13066 }
13067
13068 /* True if the dependency between OUT_INSN and IN_INSN is on the store
13069    data rather than the address.  We need this because the cprestore
13070    pattern is type "store", but is defined using an UNSPEC_VOLATILE,
13071    which causes the default routine to abort.  We just return false
13072    for that case.  */
13073
13074 bool
13075 mips_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
13076 {
13077   if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
13078     return false;
13079
13080   return !store_data_bypass_p (out_insn, in_insn);
13081 }
13082 \f
13083
13084 /* Variables and flags used in scheduler hooks when tuning for
13085    Loongson 2E/2F.  */
13086 static struct
13087 {
13088   /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
13089      strategy.  */
13090
13091   /* If true, then next ALU1/2 instruction will go to ALU1.  */
13092   bool alu1_turn_p;
13093
13094   /* If true, then next FALU1/2 unstruction will go to FALU1.  */
13095   bool falu1_turn_p;
13096
13097   /* Codes to query if [f]alu{1,2}_core units are subscribed or not.  */
13098   int alu1_core_unit_code;
13099   int alu2_core_unit_code;
13100   int falu1_core_unit_code;
13101   int falu2_core_unit_code;
13102
13103   /* True if current cycle has a multi instruction.
13104      This flag is used in mips_ls2_dfa_post_advance_cycle.  */
13105   bool cycle_has_multi_p;
13106
13107   /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
13108      These are used in mips_ls2_dfa_post_advance_cycle to initialize
13109      DFA state.
13110      E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
13111      instruction to go ALU1.  */
13112   rtx_insn *alu1_turn_enabled_insn;
13113   rtx_insn *alu2_turn_enabled_insn;
13114   rtx_insn *falu1_turn_enabled_insn;
13115   rtx_insn *falu2_turn_enabled_insn;
13116 } mips_ls2;
13117
13118 /* Implement TARGET_SCHED_ADJUST_COST.  We assume that anti and output
13119    dependencies have no cost, except on the 20Kc where output-dependence
13120    is treated like input-dependence.  */
13121
13122 static int
13123 mips_adjust_cost (rtx_insn *insn ATTRIBUTE_UNUSED, rtx link,
13124                   rtx_insn *dep ATTRIBUTE_UNUSED, int cost)
13125 {
13126   if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
13127       && TUNE_20KC)
13128     return cost;
13129   if (REG_NOTE_KIND (link) != 0)
13130     return 0;
13131   return cost;
13132 }
13133
13134 /* Return the number of instructions that can be issued per cycle.  */
13135
13136 static int
13137 mips_issue_rate (void)
13138 {
13139   switch (mips_tune)
13140     {
13141     case PROCESSOR_74KC:
13142     case PROCESSOR_74KF2_1:
13143     case PROCESSOR_74KF1_1:
13144     case PROCESSOR_74KF3_2:
13145       /* The 74k is not strictly quad-issue cpu, but can be seen as one
13146          by the scheduler.  It can issue 1 ALU, 1 AGEN and 2 FPU insns,
13147          but in reality only a maximum of 3 insns can be issued as
13148          floating-point loads and stores also require a slot in the
13149          AGEN pipe.  */
13150     case PROCESSOR_R10000:
13151       /* All R10K Processors are quad-issue (being the first MIPS
13152          processors to support this feature). */
13153       return 4;
13154
13155     case PROCESSOR_20KC:
13156     case PROCESSOR_R4130:
13157     case PROCESSOR_R5400:
13158     case PROCESSOR_R5500:
13159     case PROCESSOR_R5900:
13160     case PROCESSOR_R7000:
13161     case PROCESSOR_R9000:
13162     case PROCESSOR_OCTEON:
13163     case PROCESSOR_OCTEON2:
13164       return 2;
13165
13166     case PROCESSOR_SB1:
13167     case PROCESSOR_SB1A:
13168       /* This is actually 4, but we get better performance if we claim 3.
13169          This is partly because of unwanted speculative code motion with the
13170          larger number, and partly because in most common cases we can't
13171          reach the theoretical max of 4.  */
13172       return 3;
13173
13174     case PROCESSOR_LOONGSON_2E:
13175     case PROCESSOR_LOONGSON_2F:
13176     case PROCESSOR_LOONGSON_3A:
13177     case PROCESSOR_P5600:
13178       return 4;
13179
13180     case PROCESSOR_XLP:
13181       return (reload_completed ? 4 : 3);
13182
13183     default:
13184       return 1;
13185     }
13186 }
13187
13188 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2.  */
13189
13190 static void
13191 mips_ls2_init_dfa_post_cycle_insn (void)
13192 {
13193   start_sequence ();
13194   emit_insn (gen_ls2_alu1_turn_enabled_insn ());
13195   mips_ls2.alu1_turn_enabled_insn = get_insns ();
13196   end_sequence ();
13197
13198   start_sequence ();
13199   emit_insn (gen_ls2_alu2_turn_enabled_insn ());
13200   mips_ls2.alu2_turn_enabled_insn = get_insns ();
13201   end_sequence ();
13202
13203   start_sequence ();
13204   emit_insn (gen_ls2_falu1_turn_enabled_insn ());
13205   mips_ls2.falu1_turn_enabled_insn = get_insns ();
13206   end_sequence ();
13207
13208   start_sequence ();
13209   emit_insn (gen_ls2_falu2_turn_enabled_insn ());
13210   mips_ls2.falu2_turn_enabled_insn = get_insns ();
13211   end_sequence ();
13212
13213   mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
13214   mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
13215   mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
13216   mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
13217 }
13218
13219 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
13220    Init data used in mips_dfa_post_advance_cycle.  */
13221
13222 static void
13223 mips_init_dfa_post_cycle_insn (void)
13224 {
13225   if (TUNE_LOONGSON_2EF)
13226     mips_ls2_init_dfa_post_cycle_insn ();
13227 }
13228
13229 /* Initialize STATE when scheduling for Loongson 2E/2F.
13230    Support round-robin dispatch scheme by enabling only one of
13231    ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
13232    respectively.  */
13233
13234 static void
13235 mips_ls2_dfa_post_advance_cycle (state_t state)
13236 {
13237   if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
13238     {
13239       /* Though there are no non-pipelined ALU1 insns,
13240          we can get an instruction of type 'multi' before reload.  */
13241       gcc_assert (mips_ls2.cycle_has_multi_p);
13242       mips_ls2.alu1_turn_p = false;
13243     }
13244
13245   mips_ls2.cycle_has_multi_p = false;
13246
13247   if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
13248     /* We have a non-pipelined alu instruction in the core,
13249        adjust round-robin counter.  */
13250     mips_ls2.alu1_turn_p = true;
13251
13252   if (mips_ls2.alu1_turn_p)
13253     {
13254       if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
13255         gcc_unreachable ();
13256     }
13257   else
13258     {
13259       if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
13260         gcc_unreachable ();
13261     }
13262
13263   if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
13264     {
13265       /* There are no non-pipelined FALU1 insns.  */
13266       gcc_unreachable ();
13267       mips_ls2.falu1_turn_p = false;
13268     }
13269
13270   if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
13271     /* We have a non-pipelined falu instruction in the core,
13272        adjust round-robin counter.  */
13273     mips_ls2.falu1_turn_p = true;
13274
13275   if (mips_ls2.falu1_turn_p)
13276     {
13277       if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
13278         gcc_unreachable ();
13279     }
13280   else
13281     {
13282       if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
13283         gcc_unreachable ();
13284     }
13285 }
13286
13287 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
13288    This hook is being called at the start of each cycle.  */
13289
13290 static void
13291 mips_dfa_post_advance_cycle (void)
13292 {
13293   if (TUNE_LOONGSON_2EF)
13294     mips_ls2_dfa_post_advance_cycle (curr_state);
13295 }
13296
13297 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD.  This should
13298    be as wide as the scheduling freedom in the DFA.  */
13299
13300 static int
13301 mips_multipass_dfa_lookahead (void)
13302 {
13303   /* Can schedule up to 4 of the 6 function units in any one cycle.  */
13304   if (TUNE_SB1)
13305     return 4;
13306
13307   if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
13308     return 4;
13309
13310   if (TUNE_OCTEON)
13311     return 2;
13312
13313   if (TUNE_P5600)
13314     return 4;
13315
13316   return 0;
13317 }
13318 \f
13319 /* Remove the instruction at index LOWER from ready queue READY and
13320    reinsert it in front of the instruction at index HIGHER.  LOWER must
13321    be <= HIGHER.  */
13322
13323 static void
13324 mips_promote_ready (rtx_insn **ready, int lower, int higher)
13325 {
13326   rtx_insn *new_head;
13327   int i;
13328
13329   new_head = ready[lower];
13330   for (i = lower; i < higher; i++)
13331     ready[i] = ready[i + 1];
13332   ready[i] = new_head;
13333 }
13334
13335 /* If the priority of the instruction at POS2 in the ready queue READY
13336    is within LIMIT units of that of the instruction at POS1, swap the
13337    instructions if POS2 is not already less than POS1.  */
13338
13339 static void
13340 mips_maybe_swap_ready (rtx_insn **ready, int pos1, int pos2, int limit)
13341 {
13342   if (pos1 < pos2
13343       && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
13344     {
13345       rtx_insn *temp;
13346
13347       temp = ready[pos1];
13348       ready[pos1] = ready[pos2];
13349       ready[pos2] = temp;
13350     }
13351 }
13352 \f
13353 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
13354    that may clobber hi or lo.  */
13355 static rtx_insn *mips_macc_chains_last_hilo;
13356
13357 /* A TUNE_MACC_CHAINS helper function.  Record that instruction INSN has
13358    been scheduled, updating mips_macc_chains_last_hilo appropriately.  */
13359
13360 static void
13361 mips_macc_chains_record (rtx_insn *insn)
13362 {
13363   if (get_attr_may_clobber_hilo (insn))
13364     mips_macc_chains_last_hilo = insn;
13365 }
13366
13367 /* A TUNE_MACC_CHAINS helper function.  Search ready queue READY, which
13368    has NREADY elements, looking for a multiply-add or multiply-subtract
13369    instruction that is cumulative with mips_macc_chains_last_hilo.
13370    If there is one, promote it ahead of anything else that might
13371    clobber hi or lo.  */
13372
13373 static void
13374 mips_macc_chains_reorder (rtx_insn **ready, int nready)
13375 {
13376   int i, j;
13377
13378   if (mips_macc_chains_last_hilo != 0)
13379     for (i = nready - 1; i >= 0; i--)
13380       if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
13381         {
13382           for (j = nready - 1; j > i; j--)
13383             if (recog_memoized (ready[j]) >= 0
13384                 && get_attr_may_clobber_hilo (ready[j]))
13385               {
13386                 mips_promote_ready (ready, i, j);
13387                 break;
13388               }
13389           break;
13390         }
13391 }
13392 \f
13393 /* The last instruction to be scheduled.  */
13394 static rtx_insn *vr4130_last_insn;
13395
13396 /* A note_stores callback used by vr4130_true_reg_dependence_p.  DATA
13397    points to an rtx that is initially an instruction.  Nullify the rtx
13398    if the instruction uses the value of register X.  */
13399
13400 static void
13401 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
13402                                 void *data)
13403 {
13404   rtx *insn_ptr;
13405
13406   insn_ptr = (rtx *) data;
13407   if (REG_P (x)
13408       && *insn_ptr != 0
13409       && reg_referenced_p (x, PATTERN (*insn_ptr)))
13410     *insn_ptr = 0;
13411 }
13412
13413 /* Return true if there is true register dependence between vr4130_last_insn
13414    and INSN.  */
13415
13416 static bool
13417 vr4130_true_reg_dependence_p (rtx insn)
13418 {
13419   note_stores (PATTERN (vr4130_last_insn),
13420                vr4130_true_reg_dependence_p_1, &insn);
13421   return insn == 0;
13422 }
13423
13424 /* A TUNE_MIPS4130 helper function.  Given that INSN1 is at the head of
13425    the ready queue and that INSN2 is the instruction after it, return
13426    true if it is worth promoting INSN2 ahead of INSN1.  Look for cases
13427    in which INSN1 and INSN2 can probably issue in parallel, but for
13428    which (INSN2, INSN1) should be less sensitive to instruction
13429    alignment than (INSN1, INSN2).  See 4130.md for more details.  */
13430
13431 static bool
13432 vr4130_swap_insns_p (rtx_insn *insn1, rtx_insn *insn2)
13433 {
13434   sd_iterator_def sd_it;
13435   dep_t dep;
13436
13437   /* Check for the following case:
13438
13439      1) there is some other instruction X with an anti dependence on INSN1;
13440      2) X has a higher priority than INSN2; and
13441      3) X is an arithmetic instruction (and thus has no unit restrictions).
13442
13443      If INSN1 is the last instruction blocking X, it would better to
13444      choose (INSN1, X) over (INSN2, INSN1).  */
13445   FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
13446     if (DEP_TYPE (dep) == REG_DEP_ANTI
13447         && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
13448         && recog_memoized (DEP_CON (dep)) >= 0
13449         && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
13450       return false;
13451
13452   if (vr4130_last_insn != 0
13453       && recog_memoized (insn1) >= 0
13454       && recog_memoized (insn2) >= 0)
13455     {
13456       /* See whether INSN1 and INSN2 use different execution units,
13457          or if they are both ALU-type instructions.  If so, they can
13458          probably execute in parallel.  */
13459       enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
13460       enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
13461       if (class1 != class2 || class1 == VR4130_CLASS_ALU)
13462         {
13463           /* If only one of the instructions has a dependence on
13464              vr4130_last_insn, prefer to schedule the other one first.  */
13465           bool dep1_p = vr4130_true_reg_dependence_p (insn1);
13466           bool dep2_p = vr4130_true_reg_dependence_p (insn2);
13467           if (dep1_p != dep2_p)
13468             return dep1_p;
13469
13470           /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
13471              is not an ALU-type instruction and if INSN1 uses the same
13472              execution unit.  (Note that if this condition holds, we already
13473              know that INSN2 uses a different execution unit.)  */
13474           if (class1 != VR4130_CLASS_ALU
13475               && recog_memoized (vr4130_last_insn) >= 0
13476               && class1 == get_attr_vr4130_class (vr4130_last_insn))
13477             return true;
13478         }
13479     }
13480   return false;
13481 }
13482
13483 /* A TUNE_MIPS4130 helper function.  (READY, NREADY) describes a ready
13484    queue with at least two instructions.  Swap the first two if
13485    vr4130_swap_insns_p says that it could be worthwhile.  */
13486
13487 static void
13488 vr4130_reorder (rtx_insn **ready, int nready)
13489 {
13490   if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
13491     mips_promote_ready (ready, nready - 2, nready - 1);
13492 }
13493 \f
13494 /* Record whether last 74k AGEN instruction was a load or store.  */
13495 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
13496
13497 /* Initialize mips_last_74k_agen_insn from INSN.  A null argument
13498    resets to TYPE_UNKNOWN state.  */
13499
13500 static void
13501 mips_74k_agen_init (rtx_insn *insn)
13502 {
13503   if (!insn || CALL_P (insn) || JUMP_P (insn))
13504     mips_last_74k_agen_insn = TYPE_UNKNOWN;
13505   else
13506     {
13507       enum attr_type type = get_attr_type (insn);
13508       if (type == TYPE_LOAD || type == TYPE_STORE)
13509         mips_last_74k_agen_insn = type;
13510     }
13511 }
13512
13513 /* A TUNE_74K helper function.  The 74K AGEN pipeline likes multiple
13514    loads to be grouped together, and multiple stores to be grouped
13515    together.  Swap things around in the ready queue to make this happen.  */
13516
13517 static void
13518 mips_74k_agen_reorder (rtx_insn **ready, int nready)
13519 {
13520   int i;
13521   int store_pos, load_pos;
13522
13523   store_pos = -1;
13524   load_pos = -1;
13525
13526   for (i = nready - 1; i >= 0; i--)
13527     {
13528       rtx_insn *insn = ready[i];
13529       if (USEFUL_INSN_P (insn))
13530         switch (get_attr_type (insn))
13531           {
13532           case TYPE_STORE:
13533             if (store_pos == -1)
13534               store_pos = i;
13535             break;
13536
13537           case TYPE_LOAD:
13538             if (load_pos == -1)
13539               load_pos = i;
13540             break;
13541
13542           default:
13543             break;
13544           }
13545     }
13546
13547   if (load_pos == -1 || store_pos == -1)
13548     return;
13549
13550   switch (mips_last_74k_agen_insn)
13551     {
13552     case TYPE_UNKNOWN:
13553       /* Prefer to schedule loads since they have a higher latency.  */
13554     case TYPE_LOAD:
13555       /* Swap loads to the front of the queue.  */
13556       mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
13557       break;
13558     case TYPE_STORE:
13559       /* Swap stores to the front of the queue.  */
13560       mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
13561       break;
13562     default:
13563       break;
13564     }
13565 }
13566 \f
13567 /* Implement TARGET_SCHED_INIT.  */
13568
13569 static void
13570 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13571                  int max_ready ATTRIBUTE_UNUSED)
13572 {
13573   mips_macc_chains_last_hilo = 0;
13574   vr4130_last_insn = 0;
13575   mips_74k_agen_init (NULL);
13576
13577   /* When scheduling for Loongson2, branch instructions go to ALU1,
13578      therefore basic block is most likely to start with round-robin counter
13579      pointed to ALU2.  */
13580   mips_ls2.alu1_turn_p = false;
13581   mips_ls2.falu1_turn_p = true;
13582 }
13583
13584 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2.  */
13585
13586 static void
13587 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13588                       rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13589 {
13590   if (!reload_completed
13591       && TUNE_MACC_CHAINS
13592       && *nreadyp > 0)
13593     mips_macc_chains_reorder (ready, *nreadyp);
13594
13595   if (reload_completed
13596       && TUNE_MIPS4130
13597       && !TARGET_VR4130_ALIGN
13598       && *nreadyp > 1)
13599     vr4130_reorder (ready, *nreadyp);
13600
13601   if (TUNE_74K)
13602     mips_74k_agen_reorder (ready, *nreadyp);
13603 }
13604
13605 /* Implement TARGET_SCHED_REORDER.  */
13606
13607 static int
13608 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13609                     rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13610 {
13611   mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13612   return mips_issue_rate ();
13613 }
13614
13615 /* Implement TARGET_SCHED_REORDER2.  */
13616
13617 static int
13618 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13619                      rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13620 {
13621   mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13622   return cached_can_issue_more;
13623 }
13624
13625 /* Update round-robin counters for ALU1/2 and FALU1/2.  */
13626
13627 static void
13628 mips_ls2_variable_issue (rtx_insn *insn)
13629 {
13630   if (mips_ls2.alu1_turn_p)
13631     {
13632       if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
13633         mips_ls2.alu1_turn_p = false;
13634     }
13635   else
13636     {
13637       if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
13638         mips_ls2.alu1_turn_p = true;
13639     }
13640
13641   if (mips_ls2.falu1_turn_p)
13642     {
13643       if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
13644         mips_ls2.falu1_turn_p = false;
13645     }
13646   else
13647     {
13648       if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
13649         mips_ls2.falu1_turn_p = true;
13650     }
13651
13652   if (recog_memoized (insn) >= 0)
13653     mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
13654 }
13655
13656 /* Implement TARGET_SCHED_VARIABLE_ISSUE.  */
13657
13658 static int
13659 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13660                      rtx_insn *insn, int more)
13661 {
13662   /* Ignore USEs and CLOBBERs; don't count them against the issue rate.  */
13663   if (USEFUL_INSN_P (insn))
13664     {
13665       if (get_attr_type (insn) != TYPE_GHOST)
13666         more--;
13667       if (!reload_completed && TUNE_MACC_CHAINS)
13668         mips_macc_chains_record (insn);
13669       vr4130_last_insn = insn;
13670       if (TUNE_74K)
13671         mips_74k_agen_init (insn);
13672       else if (TUNE_LOONGSON_2EF)
13673         mips_ls2_variable_issue (insn);
13674     }
13675
13676   /* Instructions of type 'multi' should all be split before
13677      the second scheduling pass.  */
13678   gcc_assert (!reload_completed
13679               || recog_memoized (insn) < 0
13680               || get_attr_type (insn) != TYPE_MULTI);
13681
13682   cached_can_issue_more = more;
13683   return more;
13684 }
13685 \f
13686 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
13687    return the first operand of the associated PREF or PREFX insn.  */
13688
13689 rtx
13690 mips_prefetch_cookie (rtx write, rtx locality)
13691 {
13692   /* store_streamed / load_streamed.  */
13693   if (INTVAL (locality) <= 0)
13694     return GEN_INT (INTVAL (write) + 4);
13695
13696   /* store / load.  */
13697   if (INTVAL (locality) <= 2)
13698     return write;
13699
13700   /* store_retained / load_retained.  */
13701   return GEN_INT (INTVAL (write) + 6);
13702 }
13703 \f
13704 /* Flags that indicate when a built-in function is available.
13705
13706    BUILTIN_AVAIL_NON_MIPS16
13707         The function is available on the current target if !TARGET_MIPS16.
13708
13709    BUILTIN_AVAIL_MIPS16
13710         The function is available on the current target if TARGET_MIPS16.  */
13711 #define BUILTIN_AVAIL_NON_MIPS16 1
13712 #define BUILTIN_AVAIL_MIPS16 2
13713
13714 /* Declare an availability predicate for built-in functions that
13715    require non-MIPS16 mode and also require COND to be true.
13716    NAME is the main part of the predicate's name.  */
13717 #define AVAIL_NON_MIPS16(NAME, COND)                                    \
13718  static unsigned int                                                    \
13719  mips_builtin_avail_##NAME (void)                                       \
13720  {                                                                      \
13721    return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0;                        \
13722  }
13723
13724 /* Declare an availability predicate for built-in functions that
13725    support both MIPS16 and non-MIPS16 code and also require COND
13726    to be true.  NAME is the main part of the predicate's name.  */
13727 #define AVAIL_ALL(NAME, COND)                                           \
13728  static unsigned int                                                    \
13729  mips_builtin_avail_##NAME (void)                                       \
13730  {                                                                      \
13731    return (COND) ? BUILTIN_AVAIL_NON_MIPS16 | BUILTIN_AVAIL_MIPS16 : 0; \
13732  }
13733
13734 /* This structure describes a single built-in function.  */
13735 struct mips_builtin_description {
13736   /* The code of the main .md file instruction.  See mips_builtin_type
13737      for more information.  */
13738   enum insn_code icode;
13739
13740   /* The floating-point comparison code to use with ICODE, if any.  */
13741   enum mips_fp_condition cond;
13742
13743   /* The name of the built-in function.  */
13744   const char *name;
13745
13746   /* Specifies how the function should be expanded.  */
13747   enum mips_builtin_type builtin_type;
13748
13749   /* The function's prototype.  */
13750   enum mips_function_type function_type;
13751
13752   /* Whether the function is available.  */
13753   unsigned int (*avail) (void);
13754 };
13755
13756 AVAIL_ALL (hard_float, TARGET_HARD_FLOAT_ABI)
13757 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
13758 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
13759 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
13760 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
13761 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
13762 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
13763 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
13764 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
13765 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
13766 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
13767
13768 /* Construct a mips_builtin_description from the given arguments.
13769
13770    INSN is the name of the associated instruction pattern, without the
13771    leading CODE_FOR_mips_.
13772
13773    CODE is the floating-point condition code associated with the
13774    function.  It can be 'f' if the field is not applicable.
13775
13776    NAME is the name of the function itself, without the leading
13777    "__builtin_mips_".
13778
13779    BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
13780
13781    AVAIL is the name of the availability predicate, without the leading
13782    mips_builtin_avail_.  */
13783 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE,                    \
13784                      FUNCTION_TYPE, AVAIL)                              \
13785   { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND,                      \
13786     "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE,                \
13787     mips_builtin_avail_ ## AVAIL }
13788
13789 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
13790    mapped to instruction CODE_FOR_mips_<INSN>,  FUNCTION_TYPE and AVAIL
13791    are as for MIPS_BUILTIN.  */
13792 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)                      \
13793   MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
13794
13795 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
13796    are subject to mips_builtin_avail_<AVAIL>.  */
13797 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL)                          \
13798   MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s",            \
13799                 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL),  \
13800   MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d",            \
13801                 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
13802
13803 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
13804    The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
13805    while the any and all forms are subject to mips_builtin_avail_mips3d.  */
13806 #define CMP_PS_BUILTINS(INSN, COND, AVAIL)                              \
13807   MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps",   \
13808                 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF,         \
13809                 mips3d),                                                \
13810   MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps",   \
13811                 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF,         \
13812                 mips3d),                                                \
13813   MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
13814                 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF,       \
13815                 AVAIL),                                                 \
13816   MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
13817                 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF,       \
13818                 AVAIL)
13819
13820 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s.  The functions
13821    are subject to mips_builtin_avail_mips3d.  */
13822 #define CMP_4S_BUILTINS(INSN, COND)                                     \
13823   MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s",   \
13824                 MIPS_BUILTIN_CMP_ANY,                                   \
13825                 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d),            \
13826   MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s",   \
13827                 MIPS_BUILTIN_CMP_ALL,                                   \
13828                 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
13829
13830 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps.  The comparison
13831    instruction requires mips_builtin_avail_<AVAIL>.  */
13832 #define MOVTF_BUILTINS(INSN, COND, AVAIL)                               \
13833   MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps",  \
13834                 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13835                 AVAIL),                                                 \
13836   MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps",  \
13837                 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13838                 AVAIL)
13839
13840 /* Define all the built-in functions related to C.cond.fmt condition COND.  */
13841 #define CMP_BUILTINS(COND)                                              \
13842   MOVTF_BUILTINS (c, COND, paired_single),                              \
13843   MOVTF_BUILTINS (cabs, COND, mips3d),                                  \
13844   CMP_SCALAR_BUILTINS (cabs, COND, mips3d),                             \
13845   CMP_PS_BUILTINS (c, COND, paired_single),                             \
13846   CMP_PS_BUILTINS (cabs, COND, mips3d),                                 \
13847   CMP_4S_BUILTINS (c, COND),                                            \
13848   CMP_4S_BUILTINS (cabs, COND)
13849
13850 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
13851    function mapped to instruction CODE_FOR_mips_<INSN>,  FUNCTION_TYPE
13852    and AVAIL are as for MIPS_BUILTIN.  */
13853 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)            \
13854   MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET,          \
13855                 FUNCTION_TYPE, AVAIL)
13856
13857 /* Define __builtin_mips_bposge<VALUE>.  <VALUE> is 32 for the MIPS32 DSP
13858    branch instruction.  AVAIL is as for MIPS_BUILTIN.  */
13859 #define BPOSGE_BUILTIN(VALUE, AVAIL)                                    \
13860   MIPS_BUILTIN (bposge, f, "bposge" #VALUE,                             \
13861                 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
13862
13863 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
13864    for instruction CODE_FOR_loongson_<INSN>.  FUNCTION_TYPE is a
13865    builtin_description field.  */
13866 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE)            \
13867   { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f,                         \
13868     "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT,                \
13869     FUNCTION_TYPE, mips_builtin_avail_loongson }
13870
13871 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
13872    for instruction CODE_FOR_loongson_<INSN>.  FUNCTION_TYPE is a
13873    builtin_description field.  */
13874 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE)                           \
13875   LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
13876
13877 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
13878    We use functions of this form when the same insn can be usefully applied
13879    to more than one datatype.  */
13880 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE)            \
13881   LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
13882
13883 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
13884 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
13885 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
13886 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
13887 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
13888 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
13889 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
13890 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
13891
13892 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
13893 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
13894 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
13895 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
13896 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
13897 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
13898 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
13899 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
13900 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
13901 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
13902 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
13903 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
13904 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
13905 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
13906 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
13907 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
13908 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
13909 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
13910 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
13911 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
13912 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
13913 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
13914 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
13915 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
13916 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
13917 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
13918 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
13919 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
13920 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
13921 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
13922
13923 static const struct mips_builtin_description mips_builtins[] = {
13924 #define MIPS_GET_FCSR 0
13925   DIRECT_BUILTIN (get_fcsr, MIPS_USI_FTYPE_VOID, hard_float),
13926 #define MIPS_SET_FCSR 1
13927   DIRECT_NO_TARGET_BUILTIN (set_fcsr, MIPS_VOID_FTYPE_USI, hard_float),
13928
13929   DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13930   DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13931   DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13932   DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13933   DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
13934   DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
13935   DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
13936   DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
13937
13938   DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
13939   DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13940   DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13941   DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13942   DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
13943
13944   DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
13945   DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
13946   DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13947   DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13948   DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13949   DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13950
13951   DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
13952   DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
13953   DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13954   DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13955   DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13956   DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13957
13958   MIPS_FP_CONDITIONS (CMP_BUILTINS),
13959
13960   /* Built-in functions for the SB-1 processor.  */
13961   DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
13962
13963   /* Built-in functions for the DSP ASE (32-bit and 64-bit).  */
13964   DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13965   DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13966   DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13967   DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13968   DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13969   DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13970   DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13971   DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13972   DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13973   DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13974   DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
13975   DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
13976   DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
13977   DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
13978   DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
13979   DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
13980   DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13981   DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13982   DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13983   DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13984   DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
13985   DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
13986   DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13987   DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13988   DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13989   DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13990   DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13991   DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13992   DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13993   DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13994   DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13995   DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13996   DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13997   DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13998   DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13999   DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14000   DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14001   DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
14002   DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
14003   DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
14004   DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14005   DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
14006   DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
14007   DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
14008   DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
14009   DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
14010   DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
14011   DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14012   DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14013   DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14014   DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14015   DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14016   DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14017   DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14018   DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14019   DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14020   DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
14021   DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14022   DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14023   DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
14024   DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
14025   DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
14026   DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
14027   DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
14028   BPOSGE_BUILTIN (32, dsp),
14029
14030   /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit).  */
14031   DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
14032   DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14033   DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14034   DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14035   DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14036   DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14037   DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14038   DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14039   DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14040   DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14041   DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14042   DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14043   DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14044   DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14045   DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14046   DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
14047   DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
14048   DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
14049   DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14050   DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
14051   DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
14052   DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
14053   DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14054   DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14055   DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14056   DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14057   DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14058   DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14059   DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14060   DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14061   DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14062   DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14063   DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14064   DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14065
14066   /* Built-in functions for the DSP ASE (32-bit only).  */
14067   DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14068   DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14069   DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14070   DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14071   DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14072   DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14073   DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14074   DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14075   DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14076   DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14077   DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14078   DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14079   DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14080   DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14081   DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14082   DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14083   DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
14084   DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
14085   DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
14086   DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
14087   DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
14088   DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14089   DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
14090   DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14091   DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
14092   DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
14093   DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
14094
14095   /* Built-in functions for the DSP ASE (64-bit only).  */
14096   DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
14097
14098   /* The following are for the MIPS DSP ASE REV 2 (32-bit only).  */
14099   DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14100   DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14101   DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14102   DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14103   DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14104   DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14105   DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14106   DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14107   DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14108
14109   /* Builtin functions for ST Microelectronics Loongson-2E/2F cores.  */
14110   LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
14111   LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
14112   LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
14113   LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14114   LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14115   LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14116   LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14117   LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14118   LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14119   LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
14120   LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
14121   LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14122   LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14123   LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14124   LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14125   LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
14126   LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14127   LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14128   LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14129   LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
14130   LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
14131   LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14132   LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14133   LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14134   LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14135   LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14136   LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14137   LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14138   LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14139   LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14140   LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14141   LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14142   LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14143   LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14144   LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14145   LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14146   LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14147   LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
14148   LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
14149   LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14150   LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14151   LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14152   LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14153   LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14154   LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14155   LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14156   LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14157   LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
14158   LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14159   LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14160   LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14161   LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14162   LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
14163   LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
14164   LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14165   LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14166   LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14167   LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
14168   LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14169   LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
14170   LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
14171   LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14172   LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14173   LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14174   LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14175   LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14176   LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14177   LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14178   LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14179   LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14180   LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14181   LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14182   LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14183   LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14184   LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14185   LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14186   LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14187   LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14188   LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14189   LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14190   LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14191   LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
14192   LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
14193   LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14194   LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14195   LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14196   LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14197   LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14198   LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14199   LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14200   LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14201   LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14202   LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14203   LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14204   LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14205   LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14206   LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14207   LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14208   LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14209
14210   /* Sundry other built-in functions.  */
14211   DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
14212 };
14213
14214 /* Index I is the function declaration for mips_builtins[I], or null if the
14215    function isn't defined on this target.  */
14216 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
14217
14218 /* MODE is a vector mode whose elements have type TYPE.  Return the type
14219    of the vector itself.  */
14220
14221 static tree
14222 mips_builtin_vector_type (tree type, enum machine_mode mode)
14223 {
14224   static tree types[2 * (int) MAX_MACHINE_MODE];
14225   int mode_index;
14226
14227   mode_index = (int) mode;
14228
14229   if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
14230     mode_index += MAX_MACHINE_MODE;
14231
14232   if (types[mode_index] == NULL_TREE)
14233     types[mode_index] = build_vector_type_for_mode (type, mode);
14234   return types[mode_index];
14235 }
14236
14237 /* Return a type for 'const volatile void *'.  */
14238
14239 static tree
14240 mips_build_cvpointer_type (void)
14241 {
14242   static tree cache;
14243
14244   if (cache == NULL_TREE)
14245     cache = build_pointer_type (build_qualified_type
14246                                 (void_type_node,
14247                                  TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
14248   return cache;
14249 }
14250
14251 /* Source-level argument types.  */
14252 #define MIPS_ATYPE_VOID void_type_node
14253 #define MIPS_ATYPE_INT integer_type_node
14254 #define MIPS_ATYPE_POINTER ptr_type_node
14255 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
14256
14257 /* Standard mode-based argument types.  */
14258 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
14259 #define MIPS_ATYPE_SI intSI_type_node
14260 #define MIPS_ATYPE_USI unsigned_intSI_type_node
14261 #define MIPS_ATYPE_DI intDI_type_node
14262 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
14263 #define MIPS_ATYPE_SF float_type_node
14264 #define MIPS_ATYPE_DF double_type_node
14265
14266 /* Vector argument types.  */
14267 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
14268 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
14269 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
14270 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
14271 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
14272 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
14273 #define MIPS_ATYPE_UV2SI                                        \
14274   mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
14275 #define MIPS_ATYPE_UV4HI                                        \
14276   mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
14277 #define MIPS_ATYPE_UV8QI                                        \
14278   mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
14279
14280 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
14281    their associated MIPS_ATYPEs.  */
14282 #define MIPS_FTYPE_ATYPES1(A, B) \
14283   MIPS_ATYPE_##A, MIPS_ATYPE_##B
14284
14285 #define MIPS_FTYPE_ATYPES2(A, B, C) \
14286   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
14287
14288 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
14289   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
14290
14291 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
14292   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
14293   MIPS_ATYPE_##E
14294
14295 /* Return the function type associated with function prototype TYPE.  */
14296
14297 static tree
14298 mips_build_function_type (enum mips_function_type type)
14299 {
14300   static tree types[(int) MIPS_MAX_FTYPE_MAX];
14301
14302   if (types[(int) type] == NULL_TREE)
14303     switch (type)
14304       {
14305 #define DEF_MIPS_FTYPE(NUM, ARGS)                                       \
14306   case MIPS_FTYPE_NAME##NUM ARGS:                                       \
14307     types[(int) type]                                                   \
14308       = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS,          \
14309                                   NULL_TREE);                           \
14310     break;
14311 #include "config/mips/mips-ftypes.def"
14312 #undef DEF_MIPS_FTYPE
14313       default:
14314         gcc_unreachable ();
14315       }
14316
14317   return types[(int) type];
14318 }
14319
14320 /* Implement TARGET_INIT_BUILTINS.  */
14321
14322 static void
14323 mips_init_builtins (void)
14324 {
14325   const struct mips_builtin_description *d;
14326   unsigned int i;
14327
14328   /* Iterate through all of the bdesc arrays, initializing all of the
14329      builtin functions.  */
14330   for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
14331     {
14332       d = &mips_builtins[i];
14333       if (d->avail ())
14334         mips_builtin_decls[i]
14335           = add_builtin_function (d->name,
14336                                   mips_build_function_type (d->function_type),
14337                                   i, BUILT_IN_MD, NULL, NULL);
14338     }
14339 }
14340
14341 /* Implement TARGET_BUILTIN_DECL.  */
14342
14343 static tree
14344 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
14345 {
14346   if (code >= ARRAY_SIZE (mips_builtins))
14347     return error_mark_node;
14348   return mips_builtin_decls[code];
14349 }
14350
14351 /* Take argument ARGNO from EXP's argument list and convert it into
14352    an expand operand.  Store the operand in *OP.  */
14353
14354 static void
14355 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
14356                           unsigned int argno)
14357 {
14358   tree arg;
14359   rtx value;
14360
14361   arg = CALL_EXPR_ARG (exp, argno);
14362   value = expand_normal (arg);
14363   create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
14364 }
14365
14366 /* Expand instruction ICODE as part of a built-in function sequence.
14367    Use the first NOPS elements of OPS as the instruction's operands.
14368    HAS_TARGET_P is true if operand 0 is a target; it is false if the
14369    instruction has no target.
14370
14371    Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx.  */
14372
14373 static rtx
14374 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
14375                           struct expand_operand *ops, bool has_target_p)
14376 {
14377   if (!maybe_expand_insn (icode, nops, ops))
14378     {
14379       error ("invalid argument to built-in function");
14380       return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
14381     }
14382   return has_target_p ? ops[0].value : const0_rtx;
14383 }
14384
14385 /* Expand a floating-point comparison for built-in function call EXP.
14386    The first NARGS arguments are the values to be compared.  ICODE is
14387    the .md pattern that does the comparison and COND is the condition
14388    that is being tested.  Return an rtx for the result.  */
14389
14390 static rtx
14391 mips_expand_builtin_compare_1 (enum insn_code icode,
14392                                enum mips_fp_condition cond,
14393                                tree exp, int nargs)
14394 {
14395   struct expand_operand ops[MAX_RECOG_OPERANDS];
14396   rtx output;
14397   int opno, argno;
14398
14399   /* The instruction should have a target operand, an operand for each
14400      argument, and an operand for COND.  */
14401   gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
14402
14403   output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
14404   opno = 0;
14405   create_fixed_operand (&ops[opno++], output);
14406   for (argno = 0; argno < nargs; argno++)
14407     mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14408   create_integer_operand (&ops[opno++], (int) cond);
14409   return mips_expand_builtin_insn (icode, opno, ops, true);
14410 }
14411
14412 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
14413    HAS_TARGET_P says which.  EXP is the CALL_EXPR that calls the function
14414    and ICODE is the code of the associated .md pattern.  TARGET, if nonnull,
14415    suggests a good place to put the result.  */
14416
14417 static rtx
14418 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
14419                             bool has_target_p)
14420 {
14421   struct expand_operand ops[MAX_RECOG_OPERANDS];
14422   int opno, argno;
14423
14424   /* Map any target to operand 0.  */
14425   opno = 0;
14426   if (has_target_p)
14427     create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
14428
14429   /* Map the arguments to the other operands.  */
14430   gcc_assert (opno + call_expr_nargs (exp)
14431               == insn_data[icode].n_generator_args);
14432   for (argno = 0; argno < call_expr_nargs (exp); argno++)
14433     mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14434
14435   return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
14436 }
14437
14438 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
14439    function; TYPE says which.  EXP is the CALL_EXPR that calls the
14440    function, ICODE is the instruction that should be used to compare
14441    the first two arguments, and COND is the condition it should test.
14442    TARGET, if nonnull, suggests a good place to put the result.  */
14443
14444 static rtx
14445 mips_expand_builtin_movtf (enum mips_builtin_type type,
14446                            enum insn_code icode, enum mips_fp_condition cond,
14447                            rtx target, tree exp)
14448 {
14449   struct expand_operand ops[4];
14450   rtx cmp_result;
14451
14452   cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
14453   create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
14454   if (type == MIPS_BUILTIN_MOVT)
14455     {
14456       mips_prepare_builtin_arg (&ops[2], exp, 2);
14457       mips_prepare_builtin_arg (&ops[1], exp, 3);
14458     }
14459   else
14460     {
14461       mips_prepare_builtin_arg (&ops[1], exp, 2);
14462       mips_prepare_builtin_arg (&ops[2], exp, 3);
14463     }
14464   create_fixed_operand (&ops[3], cmp_result);
14465   return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
14466                                    4, ops, true);
14467 }
14468
14469 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
14470    into TARGET otherwise.  Return TARGET.  */
14471
14472 static rtx
14473 mips_builtin_branch_and_move (rtx condition, rtx target,
14474                               rtx value_if_true, rtx value_if_false)
14475 {
14476   rtx_code_label *true_label, *done_label;
14477
14478   true_label = gen_label_rtx ();
14479   done_label = gen_label_rtx ();
14480
14481   /* First assume that CONDITION is false.  */
14482   mips_emit_move (target, value_if_false);
14483
14484   /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise.  */
14485   emit_jump_insn (gen_condjump (condition, true_label));
14486   emit_jump_insn (gen_jump (done_label));
14487   emit_barrier ();
14488
14489   /* Fix TARGET if CONDITION is true.  */
14490   emit_label (true_label);
14491   mips_emit_move (target, value_if_true);
14492
14493   emit_label (done_label);
14494   return target;
14495 }
14496
14497 /* Expand a comparison built-in function of type BUILTIN_TYPE.  EXP is
14498    the CALL_EXPR that calls the function, ICODE is the code of the
14499    comparison instruction, and COND is the condition it should test.
14500    TARGET, if nonnull, suggests a good place to put the boolean result.  */
14501
14502 static rtx
14503 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
14504                              enum insn_code icode, enum mips_fp_condition cond,
14505                              rtx target, tree exp)
14506 {
14507   rtx offset, condition, cmp_result;
14508
14509   if (target == 0 || GET_MODE (target) != SImode)
14510     target = gen_reg_rtx (SImode);
14511   cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
14512                                               call_expr_nargs (exp));
14513
14514   /* If the comparison sets more than one register, we define the result
14515      to be 0 if all registers are false and -1 if all registers are true.
14516      The value of the complete result is indeterminate otherwise.  */
14517   switch (builtin_type)
14518     {
14519     case MIPS_BUILTIN_CMP_ALL:
14520       condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
14521       return mips_builtin_branch_and_move (condition, target,
14522                                            const0_rtx, const1_rtx);
14523
14524     case MIPS_BUILTIN_CMP_UPPER:
14525     case MIPS_BUILTIN_CMP_LOWER:
14526       offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
14527       condition = gen_single_cc (cmp_result, offset);
14528       return mips_builtin_branch_and_move (condition, target,
14529                                            const1_rtx, const0_rtx);
14530
14531     default:
14532       condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
14533       return mips_builtin_branch_and_move (condition, target,
14534                                            const1_rtx, const0_rtx);
14535     }
14536 }
14537
14538 /* Expand a bposge built-in function of type BUILTIN_TYPE.  TARGET,
14539    if nonnull, suggests a good place to put the boolean result.  */
14540
14541 static rtx
14542 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
14543 {
14544   rtx condition, cmp_result;
14545   int cmp_value;
14546
14547   if (target == 0 || GET_MODE (target) != SImode)
14548     target = gen_reg_rtx (SImode);
14549
14550   cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
14551
14552   if (builtin_type == MIPS_BUILTIN_BPOSGE32)
14553     cmp_value = 32;
14554   else
14555     gcc_assert (0);
14556
14557   condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
14558   return mips_builtin_branch_and_move (condition, target,
14559                                        const1_rtx, const0_rtx);
14560 }
14561
14562 /* Implement TARGET_EXPAND_BUILTIN.  */
14563
14564 static rtx
14565 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
14566                      enum machine_mode mode, int ignore)
14567 {
14568   tree fndecl;
14569   unsigned int fcode, avail;
14570   const struct mips_builtin_description *d;
14571
14572   fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14573   fcode = DECL_FUNCTION_CODE (fndecl);
14574   gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
14575   d = &mips_builtins[fcode];
14576   avail = d->avail ();
14577   gcc_assert (avail != 0);
14578   if (TARGET_MIPS16 && !(avail & BUILTIN_AVAIL_MIPS16))
14579     {
14580       error ("built-in function %qE not supported for MIPS16",
14581              DECL_NAME (fndecl));
14582       return ignore ? const0_rtx : CONST0_RTX (mode);
14583     }
14584   switch (d->builtin_type)
14585     {
14586     case MIPS_BUILTIN_DIRECT:
14587       return mips_expand_builtin_direct (d->icode, target, exp, true);
14588
14589     case MIPS_BUILTIN_DIRECT_NO_TARGET:
14590       return mips_expand_builtin_direct (d->icode, target, exp, false);
14591
14592     case MIPS_BUILTIN_MOVT:
14593     case MIPS_BUILTIN_MOVF:
14594       return mips_expand_builtin_movtf (d->builtin_type, d->icode,
14595                                         d->cond, target, exp);
14596
14597     case MIPS_BUILTIN_CMP_ANY:
14598     case MIPS_BUILTIN_CMP_ALL:
14599     case MIPS_BUILTIN_CMP_UPPER:
14600     case MIPS_BUILTIN_CMP_LOWER:
14601     case MIPS_BUILTIN_CMP_SINGLE:
14602       return mips_expand_builtin_compare (d->builtin_type, d->icode,
14603                                           d->cond, target, exp);
14604
14605     case MIPS_BUILTIN_BPOSGE32:
14606       return mips_expand_builtin_bposge (d->builtin_type, target);
14607     }
14608   gcc_unreachable ();
14609 }
14610 \f
14611 /* An entry in the MIPS16 constant pool.  VALUE is the pool constant,
14612    MODE is its mode, and LABEL is the CODE_LABEL associated with it.  */
14613 struct mips16_constant {
14614   struct mips16_constant *next;
14615   rtx value;
14616   rtx_code_label *label;
14617   enum machine_mode mode;
14618 };
14619
14620 /* Information about an incomplete MIPS16 constant pool.  FIRST is the
14621    first constant, HIGHEST_ADDRESS is the highest address that the first
14622    byte of the pool can have, and INSN_ADDRESS is the current instruction
14623    address.  */
14624 struct mips16_constant_pool {
14625   struct mips16_constant *first;
14626   int highest_address;
14627   int insn_address;
14628 };
14629
14630 /* Add constant VALUE to POOL and return its label.  MODE is the
14631    value's mode (used for CONST_INTs, etc.).  */
14632
14633 static rtx_code_label *
14634 mips16_add_constant (struct mips16_constant_pool *pool,
14635                      rtx value, enum machine_mode mode)
14636 {
14637   struct mips16_constant **p, *c;
14638   bool first_of_size_p;
14639
14640   /* See whether the constant is already in the pool.  If so, return the
14641      existing label, otherwise leave P pointing to the place where the
14642      constant should be added.
14643
14644      Keep the pool sorted in increasing order of mode size so that we can
14645      reduce the number of alignments needed.  */
14646   first_of_size_p = true;
14647   for (p = &pool->first; *p != 0; p = &(*p)->next)
14648     {
14649       if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
14650         return (*p)->label;
14651       if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
14652         break;
14653       if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
14654         first_of_size_p = false;
14655     }
14656
14657   /* In the worst case, the constant needed by the earliest instruction
14658      will end up at the end of the pool.  The entire pool must then be
14659      accessible from that instruction.
14660
14661      When adding the first constant, set the pool's highest address to
14662      the address of the first out-of-range byte.  Adjust this address
14663      downwards each time a new constant is added.  */
14664   if (pool->first == 0)
14665     /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
14666        of the instruction with the lowest two bits clear.  The base PC
14667        value for LDPC has the lowest three bits clear.  Assume the worst
14668        case here; namely that the PC-relative instruction occupies the
14669        last 2 bytes in an aligned word.  */
14670     pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
14671   pool->highest_address -= GET_MODE_SIZE (mode);
14672   if (first_of_size_p)
14673     /* Take into account the worst possible padding due to alignment.  */
14674     pool->highest_address -= GET_MODE_SIZE (mode) - 1;
14675
14676   /* Create a new entry.  */
14677   c = XNEW (struct mips16_constant);
14678   c->value = value;
14679   c->mode = mode;
14680   c->label = gen_label_rtx ();
14681   c->next = *p;
14682   *p = c;
14683
14684   return c->label;
14685 }
14686
14687 /* Output constant VALUE after instruction INSN and return the last
14688    instruction emitted.  MODE is the mode of the constant.  */
14689
14690 static rtx_insn *
14691 mips16_emit_constants_1 (enum machine_mode mode, rtx value, rtx_insn *insn)
14692 {
14693   if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
14694     {
14695       rtx size = GEN_INT (GET_MODE_SIZE (mode));
14696       return emit_insn_after (gen_consttable_int (value, size), insn);
14697     }
14698
14699   if (SCALAR_FLOAT_MODE_P (mode))
14700     return emit_insn_after (gen_consttable_float (value), insn);
14701
14702   if (VECTOR_MODE_P (mode))
14703     {
14704       int i;
14705
14706       for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
14707         insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
14708                                         CONST_VECTOR_ELT (value, i), insn);
14709       return insn;
14710     }
14711
14712   gcc_unreachable ();
14713 }
14714
14715 /* Dump out the constants in CONSTANTS after INSN.  */
14716
14717 static void
14718 mips16_emit_constants (struct mips16_constant *constants, rtx_insn *insn)
14719 {
14720   struct mips16_constant *c, *next;
14721   int align;
14722
14723   align = 0;
14724   for (c = constants; c != NULL; c = next)
14725     {
14726       /* If necessary, increase the alignment of PC.  */
14727       if (align < GET_MODE_SIZE (c->mode))
14728         {
14729           int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
14730           insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
14731         }
14732       align = GET_MODE_SIZE (c->mode);
14733
14734       insn = emit_label_after (c->label, insn);
14735       insn = mips16_emit_constants_1 (c->mode, c->value, insn);
14736
14737       next = c->next;
14738       free (c);
14739     }
14740
14741   emit_barrier_after (insn);
14742 }
14743
14744 /* Return the length of instruction INSN.  */
14745
14746 static int
14747 mips16_insn_length (rtx_insn *insn)
14748 {
14749   if (JUMP_TABLE_DATA_P (insn))
14750     {
14751       rtx body = PATTERN (insn);
14752       if (GET_CODE (body) == ADDR_VEC)
14753         return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
14754       else if (GET_CODE (body) == ADDR_DIFF_VEC)
14755         return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
14756       else
14757         gcc_unreachable ();
14758     }
14759   return get_attr_length (insn);
14760 }
14761
14762 /* If *X is a symbolic constant that refers to the constant pool, add
14763    the constant to POOL and rewrite *X to use the constant's label.  */
14764
14765 static void
14766 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
14767 {
14768   rtx base, offset;
14769   rtx_code_label *label;
14770
14771   split_const (*x, &base, &offset);
14772   if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
14773     {
14774       label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
14775                                    get_pool_mode (base));
14776       base = gen_rtx_LABEL_REF (Pmode, label);
14777       *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
14778     }
14779 }
14780
14781 /* Rewrite INSN so that constant pool references refer to the constant's
14782    label instead.  */
14783
14784 static void
14785 mips16_rewrite_pool_refs (rtx_insn *insn, struct mips16_constant_pool *pool)
14786 {
14787   subrtx_ptr_iterator::array_type array;
14788   FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), ALL)
14789     {
14790       rtx *loc = *iter;
14791
14792       if (force_to_mem_operand (*loc, Pmode))
14793         {
14794           rtx mem = force_const_mem (GET_MODE (*loc), *loc);
14795           validate_change (insn, loc, mem, false);
14796         }
14797
14798       if (MEM_P (*loc))
14799         {
14800           mips16_rewrite_pool_constant (pool, &XEXP (*loc, 0));
14801           iter.skip_subrtxes ();
14802         }
14803       else
14804         {
14805           if (TARGET_MIPS16_TEXT_LOADS)
14806             mips16_rewrite_pool_constant (pool, loc);
14807           if (GET_CODE (*loc) == CONST
14808               /* Don't rewrite the __mips16_rdwr symbol.  */
14809               || (GET_CODE (*loc) == UNSPEC
14810                   && XINT (*loc, 1) == UNSPEC_TLS_GET_TP))
14811             iter.skip_subrtxes ();
14812         }
14813     }
14814 }
14815
14816 /* Return whether CFG is used in mips_reorg.  */
14817
14818 static bool
14819 mips_cfg_in_reorg (void)
14820 {
14821   return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
14822           || TARGET_RELAX_PIC_CALLS);
14823 }
14824
14825 /* Build MIPS16 constant pools.  Split the instructions if SPLIT_P,
14826    otherwise assume that they are already split.  */
14827
14828 static void
14829 mips16_lay_out_constants (bool split_p)
14830 {
14831   struct mips16_constant_pool pool;
14832   rtx_insn *insn, *barrier;
14833
14834   if (!TARGET_MIPS16_PCREL_LOADS)
14835     return;
14836
14837   if (split_p)
14838     {
14839       if (mips_cfg_in_reorg ())
14840         split_all_insns ();
14841       else
14842         split_all_insns_noflow ();
14843     }
14844   barrier = 0;
14845   memset (&pool, 0, sizeof (pool));
14846   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14847     {
14848       /* Rewrite constant pool references in INSN.  */
14849       if (USEFUL_INSN_P (insn))
14850         mips16_rewrite_pool_refs (insn, &pool);
14851
14852       pool.insn_address += mips16_insn_length (insn);
14853
14854       if (pool.first != NULL)
14855         {
14856           /* If there are no natural barriers between the first user of
14857              the pool and the highest acceptable address, we'll need to
14858              create a new instruction to jump around the constant pool.
14859              In the worst case, this instruction will be 4 bytes long.
14860
14861              If it's too late to do this transformation after INSN,
14862              do it immediately before INSN.  */
14863           if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
14864             {
14865               rtx_code_label *label;
14866               rtx_insn *jump;
14867
14868               label = gen_label_rtx ();
14869
14870               jump = emit_jump_insn_before (gen_jump (label), insn);
14871               JUMP_LABEL (jump) = label;
14872               LABEL_NUSES (label) = 1;
14873               barrier = emit_barrier_after (jump);
14874
14875               emit_label_after (label, barrier);
14876               pool.insn_address += 4;
14877             }
14878
14879           /* See whether the constant pool is now out of range of the first
14880              user.  If so, output the constants after the previous barrier.
14881              Note that any instructions between BARRIER and INSN (inclusive)
14882              will use negative offsets to refer to the pool.  */
14883           if (pool.insn_address > pool.highest_address)
14884             {
14885               mips16_emit_constants (pool.first, barrier);
14886               pool.first = NULL;
14887               barrier = 0;
14888             }
14889           else if (BARRIER_P (insn))
14890             barrier = insn;
14891         }
14892     }
14893   mips16_emit_constants (pool.first, get_last_insn ());
14894 }
14895 \f
14896 /* Return true if it is worth r10k_simplify_address's while replacing
14897    an address with X.  We are looking for constants, and for addresses
14898    at a known offset from the incoming stack pointer.  */
14899
14900 static bool
14901 r10k_simplified_address_p (rtx x)
14902 {
14903   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
14904     x = XEXP (x, 0);
14905   return x == virtual_incoming_args_rtx || CONSTANT_P (x);
14906 }
14907
14908 /* X is an expression that appears in INSN.  Try to use the UD chains
14909    to simplify it, returning the simplified form on success and the
14910    original form otherwise.  Replace the incoming value of $sp with
14911    virtual_incoming_args_rtx (which should never occur in X otherwise).  */
14912
14913 static rtx
14914 r10k_simplify_address (rtx x, rtx_insn *insn)
14915 {
14916   rtx newx, op0, op1, set, note;
14917   rtx_insn *def_insn;
14918   df_ref use, def;
14919   struct df_link *defs;
14920
14921   newx = NULL_RTX;
14922   if (UNARY_P (x))
14923     {
14924       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14925       if (op0 != XEXP (x, 0))
14926         newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
14927                                    op0, GET_MODE (XEXP (x, 0)));
14928     }
14929   else if (BINARY_P (x))
14930     {
14931       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14932       op1 = r10k_simplify_address (XEXP (x, 1), insn);
14933       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
14934         newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
14935     }
14936   else if (GET_CODE (x) == LO_SUM)
14937     {
14938       /* LO_SUMs can be offset from HIGHs, if we know they won't
14939          overflow.  See mips_classify_address for the rationale behind
14940          the lax check.  */
14941       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14942       if (GET_CODE (op0) == HIGH)
14943         newx = XEXP (x, 1);
14944     }
14945   else if (REG_P (x))
14946     {
14947       /* Uses are recorded by regno_reg_rtx, not X itself.  */
14948       use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
14949       gcc_assert (use);
14950       defs = DF_REF_CHAIN (use);
14951
14952       /* Require a single definition.  */
14953       if (defs && defs->next == NULL)
14954         {
14955           def = defs->ref;
14956           if (DF_REF_IS_ARTIFICIAL (def))
14957             {
14958               /* Replace the incoming value of $sp with
14959                  virtual_incoming_args_rtx.  */
14960               if (x == stack_pointer_rtx
14961                   && DF_REF_BB (def) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
14962                 newx = virtual_incoming_args_rtx;
14963             }
14964           else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
14965                                    DF_REF_BB (def)))
14966             {
14967               /* Make sure that DEF_INSN is a single set of REG.  */
14968               def_insn = DF_REF_INSN (def);
14969               if (NONJUMP_INSN_P (def_insn))
14970                 {
14971                   set = single_set (def_insn);
14972                   if (set && rtx_equal_p (SET_DEST (set), x))
14973                     {
14974                       /* Prefer to use notes, since the def-use chains
14975                          are often shorter.  */
14976                       note = find_reg_equal_equiv_note (def_insn);
14977                       if (note)
14978                         newx = XEXP (note, 0);
14979                       else
14980                         newx = SET_SRC (set);
14981                       newx = r10k_simplify_address (newx, def_insn);
14982                     }
14983                 }
14984             }
14985         }
14986     }
14987   if (newx && r10k_simplified_address_p (newx))
14988     return newx;
14989   return x;
14990 }
14991
14992 /* Return true if ADDRESS is known to be an uncached address
14993    on R10K systems.  */
14994
14995 static bool
14996 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
14997 {
14998   unsigned HOST_WIDE_INT upper;
14999
15000   /* Check for KSEG1.  */
15001   if (address + 0x60000000 < 0x20000000)
15002     return true;
15003
15004   /* Check for uncached XKPHYS addresses.  */
15005   if (Pmode == DImode)
15006     {
15007       upper = (address >> 40) & 0xf9ffff;
15008       if (upper == 0x900000 || upper == 0xb80000)
15009         return true;
15010     }
15011   return false;
15012 }
15013
15014 /* Return true if we can prove that an access to address X in instruction
15015    INSN would be safe from R10K speculation.  This X is a general
15016    expression; it might not be a legitimate address.  */
15017
15018 static bool
15019 r10k_safe_address_p (rtx x, rtx_insn *insn)
15020 {
15021   rtx base, offset;
15022   HOST_WIDE_INT offset_val;
15023
15024   x = r10k_simplify_address (x, insn);
15025
15026   /* Check for references to the stack frame.  It doesn't really matter
15027      how much of the frame has been allocated at INSN; -mr10k-cache-barrier
15028      allows us to assume that accesses to any part of the eventual frame
15029      is safe from speculation at any point in the function.  */
15030   mips_split_plus (x, &base, &offset_val);
15031   if (base == virtual_incoming_args_rtx
15032       && offset_val >= -cfun->machine->frame.total_size
15033       && offset_val < cfun->machine->frame.args_size)
15034     return true;
15035
15036   /* Check for uncached addresses.  */
15037   if (CONST_INT_P (x))
15038     return r10k_uncached_address_p (INTVAL (x));
15039
15040   /* Check for accesses to a static object.  */
15041   split_const (x, &base, &offset);
15042   return offset_within_block_p (base, INTVAL (offset));
15043 }
15044
15045 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
15046    an in-range access to an automatic variable, or to an object with
15047    a link-time-constant address.  */
15048
15049 static bool
15050 r10k_safe_mem_expr_p (tree expr, unsigned HOST_WIDE_INT offset)
15051 {
15052   HOST_WIDE_INT bitoffset, bitsize;
15053   tree inner, var_offset;
15054   enum machine_mode mode;
15055   int unsigned_p, volatile_p;
15056
15057   inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
15058                                &unsigned_p, &volatile_p, false);
15059   if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
15060     return false;
15061
15062   offset += bitoffset / BITS_PER_UNIT;
15063   return offset < tree_to_uhwi (DECL_SIZE_UNIT (inner));
15064 }
15065
15066 /* A for_each_rtx callback for which DATA points to the instruction
15067    containing *X.  Stop the search if we find a MEM that is not safe
15068    from R10K speculation.  */
15069
15070 static int
15071 r10k_needs_protection_p_1 (rtx *loc, void *data)
15072 {
15073   rtx mem;
15074
15075   mem = *loc;
15076   if (!MEM_P (mem))
15077     return 0;
15078
15079   if (MEM_EXPR (mem)
15080       && MEM_OFFSET_KNOWN_P (mem)
15081       && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
15082     return -1;
15083
15084   if (r10k_safe_address_p (XEXP (mem, 0), (rtx_insn *) data))
15085     return -1;
15086
15087   return 1;
15088 }
15089
15090 /* A note_stores callback for which DATA points to an instruction pointer.
15091    If *DATA is nonnull, make it null if it X contains a MEM that is not
15092    safe from R10K speculation.  */
15093
15094 static void
15095 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
15096                                void *data)
15097 {
15098   rtx_insn **insn_ptr;
15099
15100   insn_ptr = (rtx_insn **) data;
15101   if (*insn_ptr && for_each_rtx (&x, r10k_needs_protection_p_1, *insn_ptr))
15102     *insn_ptr = NULL;
15103 }
15104
15105 /* A for_each_rtx callback that iterates over the pattern of a CALL_INSN.
15106    Return nonzero if the call is not to a declared function.  */
15107
15108 static int
15109 r10k_needs_protection_p_call (rtx *loc, void *data ATTRIBUTE_UNUSED)
15110 {
15111   rtx x;
15112
15113   x = *loc;
15114   if (!MEM_P (x))
15115     return 0;
15116
15117   x = XEXP (x, 0);
15118   if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DECL (x))
15119     return -1;
15120
15121   return 1;
15122 }
15123
15124 /* Return true if instruction INSN needs to be protected by an R10K
15125    cache barrier.  */
15126
15127 static bool
15128 r10k_needs_protection_p (rtx_insn *insn)
15129 {
15130   if (CALL_P (insn))
15131     return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_call, NULL);
15132
15133   if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
15134     {
15135       note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
15136       return insn == NULL_RTX;
15137     }
15138
15139   return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_1, insn);
15140 }
15141
15142 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
15143    edge is unconditional.  */
15144
15145 static bool
15146 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
15147 {
15148   edge_iterator ei;
15149   edge e;
15150
15151   FOR_EACH_EDGE (e, ei, bb->preds)
15152     if (!single_succ_p (e->src)
15153         || !bitmap_bit_p (protected_bbs, e->src->index)
15154         || (e->flags & EDGE_COMPLEX) != 0)
15155       return false;
15156   return true;
15157 }
15158
15159 /* Implement -mr10k-cache-barrier= for the current function.  */
15160
15161 static void
15162 r10k_insert_cache_barriers (void)
15163 {
15164   int *rev_post_order;
15165   unsigned int i, n;
15166   basic_block bb;
15167   sbitmap protected_bbs;
15168   rtx_insn *insn, *end;
15169   rtx unprotected_region;
15170
15171   if (TARGET_MIPS16)
15172     {
15173       sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
15174       return;
15175     }
15176
15177   /* Calculate dominators.  */
15178   calculate_dominance_info (CDI_DOMINATORS);
15179
15180   /* Bit X of PROTECTED_BBS is set if the last operation in basic block
15181      X is protected by a cache barrier.  */
15182   protected_bbs = sbitmap_alloc (last_basic_block_for_fn (cfun));
15183   bitmap_clear (protected_bbs);
15184
15185   /* Iterate over the basic blocks in reverse post-order.  */
15186   rev_post_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
15187   n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
15188   for (i = 0; i < n; i++)
15189     {
15190       bb = BASIC_BLOCK_FOR_FN (cfun, rev_post_order[i]);
15191
15192       /* If this block is only reached by unconditional edges, and if the
15193          source of every edge is protected, the beginning of the block is
15194          also protected.  */
15195       if (r10k_protected_bb_p (bb, protected_bbs))
15196         unprotected_region = NULL_RTX;
15197       else
15198         unprotected_region = pc_rtx;
15199       end = NEXT_INSN (BB_END (bb));
15200
15201       /* UNPROTECTED_REGION is:
15202
15203          - null if we are processing a protected region,
15204          - pc_rtx if we are processing an unprotected region but have
15205            not yet found the first instruction in it
15206          - the first instruction in an unprotected region otherwise.  */
15207       for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
15208         {
15209           if (unprotected_region && USEFUL_INSN_P (insn))
15210             {
15211               if (recog_memoized (insn) == CODE_FOR_mips_cache)
15212                 /* This CACHE instruction protects the following code.  */
15213                 unprotected_region = NULL_RTX;
15214               else
15215                 {
15216                   /* See if INSN is the first instruction in this
15217                      unprotected region.  */
15218                   if (unprotected_region == pc_rtx)
15219                     unprotected_region = insn;
15220
15221                   /* See if INSN needs to be protected.  If so,
15222                      we must insert a cache barrier somewhere between
15223                      PREV_INSN (UNPROTECTED_REGION) and INSN.  It isn't
15224                      clear which position is better performance-wise,
15225                      but as a tie-breaker, we assume that it is better
15226                      to allow delay slots to be back-filled where
15227                      possible, and that it is better not to insert
15228                      barriers in the middle of already-scheduled code.
15229                      We therefore insert the barrier at the beginning
15230                      of the region.  */
15231                   if (r10k_needs_protection_p (insn))
15232                     {
15233                       emit_insn_before (gen_r10k_cache_barrier (),
15234                                         unprotected_region);
15235                       unprotected_region = NULL_RTX;
15236                     }
15237                 }
15238             }
15239
15240           if (CALL_P (insn))
15241             /* The called function is not required to protect the exit path.
15242                The code that follows a call is therefore unprotected.  */
15243             unprotected_region = pc_rtx;
15244         }
15245
15246       /* Record whether the end of this block is protected.  */
15247       if (unprotected_region == NULL_RTX)
15248         bitmap_set_bit (protected_bbs, bb->index);
15249     }
15250   XDELETEVEC (rev_post_order);
15251
15252   sbitmap_free (protected_bbs);
15253
15254   free_dominance_info (CDI_DOMINATORS);
15255 }
15256 \f
15257 /* If INSN is a call, return the underlying CALL expr.  Return NULL_RTX
15258    otherwise.  If INSN has two call rtx, then store the second one in
15259    SECOND_CALL.  */
15260
15261 static rtx
15262 mips_call_expr_from_insn (rtx_insn *insn, rtx *second_call)
15263 {
15264   rtx x;
15265   rtx x2;
15266
15267   if (!CALL_P (insn))
15268     return NULL_RTX;
15269
15270   x = PATTERN (insn);
15271   if (GET_CODE (x) == PARALLEL)
15272     {
15273       /* Calls returning complex values have two CALL rtx.  Look for the second
15274          one here, and return it via the SECOND_CALL arg.  */
15275       x2 = XVECEXP (x, 0, 1);
15276       if (GET_CODE (x2) == SET)
15277         x2 = XEXP (x2, 1);
15278       if (GET_CODE (x2) == CALL)
15279         *second_call = x2;
15280
15281       x = XVECEXP (x, 0, 0);
15282     }
15283   if (GET_CODE (x) == SET)
15284     x = XEXP (x, 1);
15285   gcc_assert (GET_CODE (x) == CALL);
15286
15287   return x;
15288 }
15289
15290 /* REG is set in DEF.  See if the definition is one of the ways we load a
15291    register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
15292    If it is, return the symbol reference of the function, otherwise return
15293    NULL_RTX.
15294
15295    If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
15296    the values of source registers, otherwise treat such registers as
15297    having an unknown value.  */
15298
15299 static rtx
15300 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
15301 {
15302   rtx_insn *def_insn;
15303   rtx set;
15304
15305   if (DF_REF_IS_ARTIFICIAL (def))
15306     return NULL_RTX;
15307
15308   def_insn = DF_REF_INSN (def);
15309   set = single_set (def_insn);
15310   if (set && rtx_equal_p (SET_DEST (set), reg))
15311     {
15312       rtx note, src, symbol;
15313
15314       /* First see whether the source is a plain symbol.  This is used
15315          when calling symbols that are not lazily bound.  */
15316       src = SET_SRC (set);
15317       if (GET_CODE (src) == SYMBOL_REF)
15318         return src;
15319
15320       /* Handle %call16 references.  */
15321       symbol = mips_strip_unspec_call (src);
15322       if (symbol)
15323         {
15324           gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15325           return symbol;
15326         }
15327
15328       /* If we have something more complicated, look for a
15329          REG_EQUAL or REG_EQUIV note.  */
15330       note = find_reg_equal_equiv_note (def_insn);
15331       if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
15332         return XEXP (note, 0);
15333
15334       /* Follow at most one simple register copy.  Such copies are
15335          interesting in cases like:
15336
15337              for (...)
15338                {
15339                  locally_binding_fn (...);
15340                }
15341
15342          and:
15343
15344              locally_binding_fn (...);
15345              ...
15346              locally_binding_fn (...);
15347
15348          where the load of locally_binding_fn can legitimately be
15349          hoisted or shared.  However, we do not expect to see complex
15350          chains of copies, so a full worklist solution to the problem
15351          would probably be overkill.  */
15352       if (recurse_p && REG_P (src))
15353         return mips_find_pic_call_symbol (def_insn, src, false);
15354     }
15355
15356   return NULL_RTX;
15357 }
15358
15359 /* Find the definition of the use of REG in INSN.  See if the definition
15360    is one of the ways we load a register with a symbol address for a
15361    mips_use_pic_fn_addr_reg_p call.  If it is return the symbol reference
15362    of the function, otherwise return NULL_RTX.  RECURSE_P is as for
15363    mips_pic_call_symbol_from_set.  */
15364
15365 static rtx
15366 mips_find_pic_call_symbol (rtx_insn *insn, rtx reg, bool recurse_p)
15367 {
15368   df_ref use;
15369   struct df_link *defs;
15370   rtx symbol;
15371
15372   use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
15373   if (!use)
15374     return NULL_RTX;
15375   defs = DF_REF_CHAIN (use);
15376   if (!defs)
15377     return NULL_RTX;
15378   symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15379   if (!symbol)
15380     return NULL_RTX;
15381
15382   /* If we have more than one definition, they need to be identical.  */
15383   for (defs = defs->next; defs; defs = defs->next)
15384     {
15385       rtx other;
15386
15387       other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15388       if (!rtx_equal_p (symbol, other))
15389         return NULL_RTX;
15390     }
15391
15392   return symbol;
15393 }
15394
15395 /* Replace the args_size operand of the call expression CALL with the
15396    call-attribute UNSPEC and fill in SYMBOL as the function symbol.  */
15397
15398 static void
15399 mips_annotate_pic_call_expr (rtx call, rtx symbol)
15400 {
15401   rtx args_size;
15402
15403   args_size = XEXP (call, 1);
15404   XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
15405                                    gen_rtvec (2, args_size, symbol),
15406                                    UNSPEC_CALL_ATTR);
15407 }
15408
15409 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression.  See
15410    if instead of the arg_size argument it contains the call attributes.  If
15411    yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
15412    symbol from the call attributes.  Also return false if ARGS_SIZE_OPNO is
15413    -1.  */
15414
15415 bool
15416 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
15417 {
15418   rtx args_size, symbol;
15419
15420   if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
15421     return false;
15422
15423   args_size = operands[args_size_opno];
15424   if (GET_CODE (args_size) != UNSPEC)
15425     return false;
15426   gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
15427
15428   symbol = XVECEXP (args_size, 0, 1);
15429   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15430
15431   operands[args_size_opno] = symbol;
15432   return true;
15433 }
15434
15435 /* Use DF to annotate PIC indirect calls with the function symbol they
15436    dispatch to.  */
15437
15438 static void
15439 mips_annotate_pic_calls (void)
15440 {
15441   basic_block bb;
15442   rtx_insn *insn;
15443
15444   FOR_EACH_BB_FN (bb, cfun)
15445     FOR_BB_INSNS (bb, insn)
15446     {
15447       rtx call, reg, symbol, second_call;
15448
15449       second_call = 0;
15450       call = mips_call_expr_from_insn (insn, &second_call);
15451       if (!call)
15452         continue;
15453       gcc_assert (MEM_P (XEXP (call, 0)));
15454       reg = XEXP (XEXP (call, 0), 0);
15455       if (!REG_P (reg))
15456         continue;
15457
15458       symbol = mips_find_pic_call_symbol (insn, reg, true);
15459       if (symbol)
15460         {
15461           mips_annotate_pic_call_expr (call, symbol);
15462           if (second_call)
15463             mips_annotate_pic_call_expr (second_call, symbol);
15464         }
15465     }
15466 }
15467 \f
15468 /* A temporary variable used by for_each_rtx callbacks, etc.  */
15469 static rtx_insn *mips_sim_insn;
15470
15471 /* A structure representing the state of the processor pipeline.
15472    Used by the mips_sim_* family of functions.  */
15473 struct mips_sim {
15474   /* The maximum number of instructions that can be issued in a cycle.
15475      (Caches mips_issue_rate.)  */
15476   unsigned int issue_rate;
15477
15478   /* The current simulation time.  */
15479   unsigned int time;
15480
15481   /* How many more instructions can be issued in the current cycle.  */
15482   unsigned int insns_left;
15483
15484   /* LAST_SET[X].INSN is the last instruction to set register X.
15485      LAST_SET[X].TIME is the time at which that instruction was issued.
15486      INSN is null if no instruction has yet set register X.  */
15487   struct {
15488     rtx_insn *insn;
15489     unsigned int time;
15490   } last_set[FIRST_PSEUDO_REGISTER];
15491
15492   /* The pipeline's current DFA state.  */
15493   state_t dfa_state;
15494 };
15495
15496 /* Reset STATE to the initial simulation state.  */
15497
15498 static void
15499 mips_sim_reset (struct mips_sim *state)
15500 {
15501   curr_state = state->dfa_state;
15502
15503   state->time = 0;
15504   state->insns_left = state->issue_rate;
15505   memset (&state->last_set, 0, sizeof (state->last_set));
15506   state_reset (curr_state);
15507
15508   targetm.sched.init (0, false, 0);
15509   advance_state (curr_state);
15510 }
15511
15512 /* Initialize STATE before its first use.  DFA_STATE points to an
15513    allocated but uninitialized DFA state.  */
15514
15515 static void
15516 mips_sim_init (struct mips_sim *state, state_t dfa_state)
15517 {
15518   if (targetm.sched.init_dfa_pre_cycle_insn)
15519     targetm.sched.init_dfa_pre_cycle_insn ();
15520
15521   if (targetm.sched.init_dfa_post_cycle_insn)
15522     targetm.sched.init_dfa_post_cycle_insn ();
15523
15524   state->issue_rate = mips_issue_rate ();
15525   state->dfa_state = dfa_state;
15526   mips_sim_reset (state);
15527 }
15528
15529 /* Advance STATE by one clock cycle.  */
15530
15531 static void
15532 mips_sim_next_cycle (struct mips_sim *state)
15533 {
15534   curr_state = state->dfa_state;
15535
15536   state->time++;
15537   state->insns_left = state->issue_rate;
15538   advance_state (curr_state);
15539 }
15540
15541 /* Advance simulation state STATE until instruction INSN can read
15542    register REG.  */
15543
15544 static void
15545 mips_sim_wait_reg (struct mips_sim *state, rtx_insn *insn, rtx reg)
15546 {
15547   unsigned int regno, end_regno;
15548
15549   end_regno = END_REGNO (reg);
15550   for (regno = REGNO (reg); regno < end_regno; regno++)
15551     if (state->last_set[regno].insn != 0)
15552       {
15553         unsigned int t;
15554
15555         t = (state->last_set[regno].time
15556              + insn_latency (state->last_set[regno].insn, insn));
15557         while (state->time < t)
15558           mips_sim_next_cycle (state);
15559     }
15560 }
15561
15562 /* A for_each_rtx callback.  If *X is a register, advance simulation state
15563    DATA until mips_sim_insn can read the register's value.  */
15564
15565 static int
15566 mips_sim_wait_regs_2 (rtx *x, void *data)
15567 {
15568   if (REG_P (*x))
15569     mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *x);
15570   return 0;
15571 }
15572
15573 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X.  */
15574
15575 static void
15576 mips_sim_wait_regs_1 (rtx *x, void *data)
15577 {
15578   for_each_rtx (x, mips_sim_wait_regs_2, data);
15579 }
15580
15581 /* Advance simulation state STATE until all of INSN's register
15582    dependencies are satisfied.  */
15583
15584 static void
15585 mips_sim_wait_regs (struct mips_sim *state, rtx_insn *insn)
15586 {
15587   mips_sim_insn = insn;
15588   note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
15589 }
15590
15591 /* Advance simulation state STATE until the units required by
15592    instruction INSN are available.  */
15593
15594 static void
15595 mips_sim_wait_units (struct mips_sim *state, rtx_insn *insn)
15596 {
15597   state_t tmp_state;
15598
15599   tmp_state = alloca (state_size ());
15600   while (state->insns_left == 0
15601          || (memcpy (tmp_state, state->dfa_state, state_size ()),
15602              state_transition (tmp_state, insn) >= 0))
15603     mips_sim_next_cycle (state);
15604 }
15605
15606 /* Advance simulation state STATE until INSN is ready to issue.  */
15607
15608 static void
15609 mips_sim_wait_insn (struct mips_sim *state, rtx_insn *insn)
15610 {
15611   mips_sim_wait_regs (state, insn);
15612   mips_sim_wait_units (state, insn);
15613 }
15614
15615 /* mips_sim_insn has just set X.  Update the LAST_SET array
15616    in simulation state DATA.  */
15617
15618 static void
15619 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
15620 {
15621   struct mips_sim *state;
15622
15623   state = (struct mips_sim *) data;
15624   if (REG_P (x))
15625     {
15626       unsigned int regno, end_regno;
15627
15628       end_regno = END_REGNO (x);
15629       for (regno = REGNO (x); regno < end_regno; regno++)
15630         {
15631           state->last_set[regno].insn = mips_sim_insn;
15632           state->last_set[regno].time = state->time;
15633         }
15634     }
15635 }
15636
15637 /* Issue instruction INSN in scheduler state STATE.  Assume that INSN
15638    can issue immediately (i.e., that mips_sim_wait_insn has already
15639    been called).  */
15640
15641 static void
15642 mips_sim_issue_insn (struct mips_sim *state, rtx_insn *insn)
15643 {
15644   curr_state = state->dfa_state;
15645
15646   state_transition (curr_state, insn);
15647   state->insns_left = targetm.sched.variable_issue (0, false, insn,
15648                                                     state->insns_left);
15649
15650   mips_sim_insn = insn;
15651   note_stores (PATTERN (insn), mips_sim_record_set, state);
15652 }
15653
15654 /* Simulate issuing a NOP in state STATE.  */
15655
15656 static void
15657 mips_sim_issue_nop (struct mips_sim *state)
15658 {
15659   if (state->insns_left == 0)
15660     mips_sim_next_cycle (state);
15661   state->insns_left--;
15662 }
15663
15664 /* Update simulation state STATE so that it's ready to accept the instruction
15665    after INSN.  INSN should be part of the main rtl chain, not a member of a
15666    SEQUENCE.  */
15667
15668 static void
15669 mips_sim_finish_insn (struct mips_sim *state, rtx_insn *insn)
15670 {
15671   /* If INSN is a jump with an implicit delay slot, simulate a nop.  */
15672   if (JUMP_P (insn))
15673     mips_sim_issue_nop (state);
15674
15675   switch (GET_CODE (SEQ_BEGIN (insn)))
15676     {
15677     case CODE_LABEL:
15678     case CALL_INSN:
15679       /* We can't predict the processor state after a call or label.  */
15680       mips_sim_reset (state);
15681       break;
15682
15683     case JUMP_INSN:
15684       /* The delay slots of branch likely instructions are only executed
15685          when the branch is taken.  Therefore, if the caller has simulated
15686          the delay slot instruction, STATE does not really reflect the state
15687          of the pipeline for the instruction after the delay slot.  Also,
15688          branch likely instructions tend to incur a penalty when not taken,
15689          so there will probably be an extra delay between the branch and
15690          the instruction after the delay slot.  */
15691       if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
15692         mips_sim_reset (state);
15693       break;
15694
15695     default:
15696       break;
15697     }
15698 }
15699
15700 /* Use simulator state STATE to calculate the execution time of
15701    instruction sequence SEQ.  */
15702
15703 static unsigned int
15704 mips_seq_time (struct mips_sim *state, rtx_insn *seq)
15705 {
15706   mips_sim_reset (state);
15707   for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
15708     {
15709       mips_sim_wait_insn (state, insn);
15710       mips_sim_issue_insn (state, insn);
15711     }
15712   return state->time;
15713 }
15714 \f
15715 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
15716    setting SETTING, using STATE to simulate instruction sequences.  */
15717
15718 static unsigned int
15719 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
15720 {
15721   mips_tuning_info.fast_mult_zero_zero_p = setting;
15722   start_sequence ();
15723
15724   enum machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
15725   rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
15726   mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
15727
15728   /* If the target provides mulsidi3_32bit then that's the most likely
15729      consumer of the result.  Test for bypasses.  */
15730   if (dword_mode == DImode && HAVE_maddsidi4)
15731     {
15732       rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
15733       emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
15734     }
15735
15736   unsigned int time = mips_seq_time (state, get_insns ());
15737   end_sequence ();
15738   return time;
15739 }
15740
15741 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
15742    and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
15743    Prefer MULT -- which is shorter -- in the event of a tie.  */
15744
15745 static void
15746 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
15747 {
15748   if (TARGET_MIPS16)
15749     /* No MTLO or MTHI available.  */
15750     mips_tuning_info.fast_mult_zero_zero_p = true;
15751   else
15752     {
15753       unsigned int true_time = mips_mult_zero_zero_cost (state, true);
15754       unsigned int false_time = mips_mult_zero_zero_cost (state, false);
15755       mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
15756     }
15757 }
15758
15759 /* Set up costs based on the current architecture and tuning settings.  */
15760
15761 static void
15762 mips_set_tuning_info (void)
15763 {
15764   if (mips_tuning_info.initialized_p
15765       && mips_tuning_info.arch == mips_arch
15766       && mips_tuning_info.tune == mips_tune
15767       && mips_tuning_info.mips16_p == TARGET_MIPS16)
15768     return;
15769
15770   mips_tuning_info.arch = mips_arch;
15771   mips_tuning_info.tune = mips_tune;
15772   mips_tuning_info.mips16_p = TARGET_MIPS16;
15773   mips_tuning_info.initialized_p = true;
15774
15775   dfa_start ();
15776
15777   struct mips_sim state;
15778   mips_sim_init (&state, alloca (state_size ()));
15779
15780   mips_set_fast_mult_zero_zero_p (&state);
15781
15782   dfa_finish ();
15783 }
15784
15785 /* Implement TARGET_EXPAND_TO_RTL_HOOK.  */
15786
15787 static void
15788 mips_expand_to_rtl_hook (void)
15789 {
15790   /* We need to call this at a point where we can safely create sequences
15791      of instructions, so TARGET_OVERRIDE_OPTIONS is too early.  We also
15792      need to call it at a point where the DFA infrastructure is not
15793      already in use, so we can't just call it lazily on demand.
15794
15795      At present, mips_tuning_info is only needed during post-expand
15796      RTL passes such as split_insns, so this hook should be early enough.
15797      We may need to move the call elsewhere if mips_tuning_info starts
15798      to be used for other things (such as rtx_costs, or expanders that
15799      could be called during gimple optimization).  */
15800   mips_set_tuning_info ();
15801 }
15802 \f
15803 /* The VR4130 pipeline issues aligned pairs of instructions together,
15804    but it stalls the second instruction if it depends on the first.
15805    In order to cut down the amount of logic required, this dependence
15806    check is not based on a full instruction decode.  Instead, any non-SPECIAL
15807    instruction is assumed to modify the register specified by bits 20-16
15808    (which is usually the "rt" field).
15809
15810    In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
15811    input, so we can end up with a false dependence between the branch
15812    and its delay slot.  If this situation occurs in instruction INSN,
15813    try to avoid it by swapping rs and rt.  */
15814
15815 static void
15816 vr4130_avoid_branch_rt_conflict (rtx_insn *insn)
15817 {
15818   rtx_insn *first, *second;
15819
15820   first = SEQ_BEGIN (insn);
15821   second = SEQ_END (insn);
15822   if (JUMP_P (first)
15823       && NONJUMP_INSN_P (second)
15824       && GET_CODE (PATTERN (first)) == SET
15825       && GET_CODE (SET_DEST (PATTERN (first))) == PC
15826       && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
15827     {
15828       /* Check for the right kind of condition.  */
15829       rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
15830       if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
15831           && REG_P (XEXP (cond, 0))
15832           && REG_P (XEXP (cond, 1))
15833           && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
15834           && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
15835         {
15836           /* SECOND mentions the rt register but not the rs register.  */
15837           rtx tmp = XEXP (cond, 0);
15838           XEXP (cond, 0) = XEXP (cond, 1);
15839           XEXP (cond, 1) = tmp;
15840         }
15841     }
15842 }
15843
15844 /* Implement -mvr4130-align.  Go through each basic block and simulate the
15845    processor pipeline.  If we find that a pair of instructions could execute
15846    in parallel, and the first of those instructions is not 8-byte aligned,
15847    insert a nop to make it aligned.  */
15848
15849 static void
15850 vr4130_align_insns (void)
15851 {
15852   struct mips_sim state;
15853   rtx_insn *insn, *subinsn, *last, *last2, *next;
15854   bool aligned_p;
15855
15856   dfa_start ();
15857
15858   /* LAST is the last instruction before INSN to have a nonzero length.
15859      LAST2 is the last such instruction before LAST.  */
15860   last = 0;
15861   last2 = 0;
15862
15863   /* ALIGNED_P is true if INSN is known to be at an aligned address.  */
15864   aligned_p = true;
15865
15866   mips_sim_init (&state, alloca (state_size ()));
15867   for (insn = get_insns (); insn != 0; insn = next)
15868     {
15869       unsigned int length;
15870
15871       next = NEXT_INSN (insn);
15872
15873       /* See the comment above vr4130_avoid_branch_rt_conflict for details.
15874          This isn't really related to the alignment pass, but we do it on
15875          the fly to avoid a separate instruction walk.  */
15876       vr4130_avoid_branch_rt_conflict (insn);
15877
15878       length = get_attr_length (insn);
15879       if (length > 0 && USEFUL_INSN_P (insn))
15880         FOR_EACH_SUBINSN (subinsn, insn)
15881           {
15882             mips_sim_wait_insn (&state, subinsn);
15883
15884             /* If we want this instruction to issue in parallel with the
15885                previous one, make sure that the previous instruction is
15886                aligned.  There are several reasons why this isn't worthwhile
15887                when the second instruction is a call:
15888
15889                   - Calls are less likely to be performance critical,
15890                   - There's a good chance that the delay slot can execute
15891                     in parallel with the call.
15892                   - The return address would then be unaligned.
15893
15894                In general, if we're going to insert a nop between instructions
15895                X and Y, it's better to insert it immediately after X.  That
15896                way, if the nop makes Y aligned, it will also align any labels
15897                between X and Y.  */
15898             if (state.insns_left != state.issue_rate
15899                 && !CALL_P (subinsn))
15900               {
15901                 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
15902                   {
15903                     /* SUBINSN is the first instruction in INSN and INSN is
15904                        aligned.  We want to align the previous instruction
15905                        instead, so insert a nop between LAST2 and LAST.
15906
15907                        Note that LAST could be either a single instruction
15908                        or a branch with a delay slot.  In the latter case,
15909                        LAST, like INSN, is already aligned, but the delay
15910                        slot must have some extra delay that stops it from
15911                        issuing at the same time as the branch.  We therefore
15912                        insert a nop before the branch in order to align its
15913                        delay slot.  */
15914                     gcc_assert (last2);
15915                     emit_insn_after (gen_nop (), last2);
15916                     aligned_p = false;
15917                   }
15918                 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
15919                   {
15920                     /* SUBINSN is the delay slot of INSN, but INSN is
15921                        currently unaligned.  Insert a nop between
15922                        LAST and INSN to align it.  */
15923                     gcc_assert (last);
15924                     emit_insn_after (gen_nop (), last);
15925                     aligned_p = true;
15926                   }
15927               }
15928             mips_sim_issue_insn (&state, subinsn);
15929           }
15930       mips_sim_finish_insn (&state, insn);
15931
15932       /* Update LAST, LAST2 and ALIGNED_P for the next instruction.  */
15933       length = get_attr_length (insn);
15934       if (length > 0)
15935         {
15936           /* If the instruction is an asm statement or multi-instruction
15937              mips.md patern, the length is only an estimate.  Insert an
15938              8 byte alignment after it so that the following instructions
15939              can be handled correctly.  */
15940           if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
15941               && (recog_memoized (insn) < 0 || length >= 8))
15942             {
15943               next = emit_insn_after (gen_align (GEN_INT (3)), insn);
15944               next = NEXT_INSN (next);
15945               mips_sim_next_cycle (&state);
15946               aligned_p = true;
15947             }
15948           else if (length & 4)
15949             aligned_p = !aligned_p;
15950           last2 = last;
15951           last = insn;
15952         }
15953
15954       /* See whether INSN is an aligned label.  */
15955       if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
15956         aligned_p = true;
15957     }
15958   dfa_finish ();
15959 }
15960 \f
15961 /* This structure records that the current function has a LO_SUM
15962    involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
15963    the largest offset applied to BASE by all such LO_SUMs.  */
15964 struct mips_lo_sum_offset {
15965   rtx base;
15966   HOST_WIDE_INT offset;
15967 };
15968
15969 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE.  */
15970
15971 static hashval_t
15972 mips_hash_base (rtx base)
15973 {
15974   int do_not_record_p;
15975
15976   return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
15977 }
15978
15979 /* Hashtable helpers.  */
15980
15981 struct mips_lo_sum_offset_hasher : typed_free_remove <mips_lo_sum_offset>
15982 {
15983   typedef mips_lo_sum_offset value_type;
15984   typedef rtx_def compare_type;
15985   static inline hashval_t hash (const value_type *);
15986   static inline bool equal (const value_type *, const compare_type *);
15987 };
15988
15989 /* Hash-table callbacks for mips_lo_sum_offsets.  */
15990
15991 inline hashval_t
15992 mips_lo_sum_offset_hasher::hash (const value_type *entry)
15993 {
15994   return mips_hash_base (entry->base);
15995 }
15996
15997 inline bool
15998 mips_lo_sum_offset_hasher::equal (const value_type *entry,
15999                                   const compare_type *value)
16000 {
16001   return rtx_equal_p (entry->base, value);
16002 }
16003
16004 typedef hash_table<mips_lo_sum_offset_hasher> mips_offset_table;
16005
16006 /* Look up symbolic constant X in HTAB, which is a hash table of
16007    mips_lo_sum_offsets.  If OPTION is NO_INSERT, return true if X can be
16008    paired with a recorded LO_SUM, otherwise record X in the table.  */
16009
16010 static bool
16011 mips_lo_sum_offset_lookup (mips_offset_table *htab, rtx x,
16012                            enum insert_option option)
16013 {
16014   rtx base, offset;
16015   mips_lo_sum_offset **slot;
16016   struct mips_lo_sum_offset *entry;
16017
16018   /* Split X into a base and offset.  */
16019   split_const (x, &base, &offset);
16020   if (UNSPEC_ADDRESS_P (base))
16021     base = UNSPEC_ADDRESS (base);
16022
16023   /* Look up the base in the hash table.  */
16024   slot = htab->find_slot_with_hash (base, mips_hash_base (base), option);
16025   if (slot == NULL)
16026     return false;
16027
16028   entry = (struct mips_lo_sum_offset *) *slot;
16029   if (option == INSERT)
16030     {
16031       if (entry == NULL)
16032         {
16033           entry = XNEW (struct mips_lo_sum_offset);
16034           entry->base = base;
16035           entry->offset = INTVAL (offset);
16036           *slot = entry;
16037         }
16038       else
16039         {
16040           if (INTVAL (offset) > entry->offset)
16041             entry->offset = INTVAL (offset);
16042         }
16043     }
16044   return INTVAL (offset) <= entry->offset;
16045 }
16046
16047 /* A for_each_rtx callback for which DATA is a mips_lo_sum_offset hash table.
16048    Record every LO_SUM in *LOC.  */
16049
16050 static int
16051 mips_record_lo_sum (rtx *loc, void *data)
16052 {
16053   if (GET_CODE (*loc) == LO_SUM)
16054     mips_lo_sum_offset_lookup ((mips_offset_table*) data,
16055                                XEXP (*loc, 1), INSERT);
16056   return 0;
16057 }
16058
16059 /* Return true if INSN is a SET of an orphaned high-part relocation.
16060    HTAB is a hash table of mips_lo_sum_offsets that describes all the
16061    LO_SUMs in the current function.  */
16062
16063 static bool
16064 mips_orphaned_high_part_p (mips_offset_table *htab, rtx_insn *insn)
16065 {
16066   enum mips_symbol_type type;
16067   rtx x, set;
16068
16069   set = single_set (insn);
16070   if (set)
16071     {
16072       /* Check for %his.  */
16073       x = SET_SRC (set);
16074       if (GET_CODE (x) == HIGH
16075           && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
16076         return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
16077
16078       /* Check for local %gots (and %got_pages, which is redundant but OK).  */
16079       if (GET_CODE (x) == UNSPEC
16080           && XINT (x, 1) == UNSPEC_LOAD_GOT
16081           && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
16082                                        SYMBOL_CONTEXT_LEA, &type)
16083           && type == SYMBOL_GOTOFF_PAGE)
16084         return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
16085     }
16086   return false;
16087 }
16088
16089 /* Subroutine of mips_reorg_process_insns.  If there is a hazard between
16090    INSN and a previous instruction, avoid it by inserting nops after
16091    instruction AFTER.
16092
16093    *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
16094    this point.  If *DELAYED_REG is non-null, INSN must wait a cycle
16095    before using the value of that register.  *HILO_DELAY counts the
16096    number of instructions since the last hilo hazard (that is,
16097    the number of instructions since the last MFLO or MFHI).
16098
16099    After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
16100    for the next instruction.
16101
16102    LO_REG is an rtx for the LO register, used in dependence checking.  */
16103
16104 static void
16105 mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
16106                    rtx *delayed_reg, rtx lo_reg)
16107 {
16108   rtx pattern, set;
16109   int nops, ninsns;
16110
16111   pattern = PATTERN (insn);
16112
16113   /* Do not put the whole function in .set noreorder if it contains
16114      an asm statement.  We don't know whether there will be hazards
16115      between the asm statement and the gcc-generated code.  */
16116   if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
16117     cfun->machine->all_noreorder_p = false;
16118
16119   /* Ignore zero-length instructions (barriers and the like).  */
16120   ninsns = get_attr_length (insn) / 4;
16121   if (ninsns == 0)
16122     return;
16123
16124   /* Work out how many nops are needed.  Note that we only care about
16125      registers that are explicitly mentioned in the instruction's pattern.
16126      It doesn't matter that calls use the argument registers or that they
16127      clobber hi and lo.  */
16128   if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
16129     nops = 2 - *hilo_delay;
16130   else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
16131     nops = 1;
16132   else
16133     nops = 0;
16134
16135   /* Insert the nops between this instruction and the previous one.
16136      Each new nop takes us further from the last hilo hazard.  */
16137   *hilo_delay += nops;
16138   while (nops-- > 0)
16139     emit_insn_after (gen_hazard_nop (), after);
16140
16141   /* Set up the state for the next instruction.  */
16142   *hilo_delay += ninsns;
16143   *delayed_reg = 0;
16144   if (INSN_CODE (insn) >= 0)
16145     switch (get_attr_hazard (insn))
16146       {
16147       case HAZARD_NONE:
16148         break;
16149
16150       case HAZARD_HILO:
16151         *hilo_delay = 0;
16152         break;
16153
16154       case HAZARD_DELAY:
16155         set = single_set (insn);
16156         gcc_assert (set);
16157         *delayed_reg = SET_DEST (set);
16158         break;
16159       }
16160 }
16161
16162 /* Go through the instruction stream and insert nops where necessary.
16163    Also delete any high-part relocations whose partnering low parts
16164    are now all dead.  See if the whole function can then be put into
16165    .set noreorder and .set nomacro.  */
16166
16167 static void
16168 mips_reorg_process_insns (void)
16169 {
16170   rtx_insn *insn, *last_insn, *subinsn, *next_insn;
16171   rtx lo_reg, delayed_reg;
16172   int hilo_delay;
16173
16174   /* Force all instructions to be split into their final form.  */
16175   split_all_insns_noflow ();
16176
16177   /* Recalculate instruction lengths without taking nops into account.  */
16178   cfun->machine->ignore_hazard_length_p = true;
16179   shorten_branches (get_insns ());
16180
16181   cfun->machine->all_noreorder_p = true;
16182
16183   /* We don't track MIPS16 PC-relative offsets closely enough to make
16184      a good job of "set .noreorder" code in MIPS16 mode.  */
16185   if (TARGET_MIPS16)
16186     cfun->machine->all_noreorder_p = false;
16187
16188   /* Code that doesn't use explicit relocs can't be ".set nomacro".  */
16189   if (!TARGET_EXPLICIT_RELOCS)
16190     cfun->machine->all_noreorder_p = false;
16191
16192   /* Profiled functions can't be all noreorder because the profiler
16193      support uses assembler macros.  */
16194   if (crtl->profile)
16195     cfun->machine->all_noreorder_p = false;
16196
16197   /* Code compiled with -mfix-vr4120, -mfix-rm7000 or -mfix-24k can't be
16198      all noreorder because we rely on the assembler to work around some
16199      errata.  The R5900 too has several bugs.  */
16200   if (TARGET_FIX_VR4120
16201       || TARGET_FIX_RM7000
16202       || TARGET_FIX_24K
16203       || TARGET_MIPS5900)
16204     cfun->machine->all_noreorder_p = false;
16205
16206   /* The same is true for -mfix-vr4130 if we might generate MFLO or
16207      MFHI instructions.  Note that we avoid using MFLO and MFHI if
16208      the VR4130 MACC and DMACC instructions are available instead;
16209      see the *mfhilo_{si,di}_macc patterns.  */
16210   if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
16211     cfun->machine->all_noreorder_p = false;
16212
16213   mips_offset_table htab (37);
16214
16215   /* Make a first pass over the instructions, recording all the LO_SUMs.  */
16216   for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
16217     FOR_EACH_SUBINSN (subinsn, insn)
16218       if (USEFUL_INSN_P (subinsn))
16219         {
16220           rtx body = PATTERN (insn);
16221           int noperands = asm_noperands (body);
16222           if (noperands >= 0)
16223             {
16224               rtx *ops = XALLOCAVEC (rtx, noperands);
16225               bool *used = XALLOCAVEC (bool, noperands);
16226               const char *string = decode_asm_operands (body, ops, NULL, NULL,
16227                                                         NULL, NULL);
16228               get_referenced_operands (string, used, noperands);
16229               for (int i = 0; i < noperands; ++i)
16230                 if (used[i])
16231                   for_each_rtx (&ops[i], mips_record_lo_sum, &htab);
16232             }
16233           else
16234             for_each_rtx (&PATTERN (subinsn), mips_record_lo_sum, &htab);
16235         }
16236
16237   last_insn = 0;
16238   hilo_delay = 2;
16239   delayed_reg = 0;
16240   lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
16241
16242   /* Make a second pass over the instructions.  Delete orphaned
16243      high-part relocations or turn them into NOPs.  Avoid hazards
16244      by inserting NOPs.  */
16245   for (insn = get_insns (); insn != 0; insn = next_insn)
16246     {
16247       next_insn = NEXT_INSN (insn);
16248       if (USEFUL_INSN_P (insn))
16249         {
16250           if (GET_CODE (PATTERN (insn)) == SEQUENCE)
16251             {
16252               /* If we find an orphaned high-part relocation in a delay
16253                  slot, it's easier to turn that instruction into a NOP than
16254                  to delete it.  The delay slot will be a NOP either way.  */
16255               FOR_EACH_SUBINSN (subinsn, insn)
16256                 if (INSN_P (subinsn))
16257                   {
16258                     if (mips_orphaned_high_part_p (&htab, subinsn))
16259                       {
16260                         PATTERN (subinsn) = gen_nop ();
16261                         INSN_CODE (subinsn) = CODE_FOR_nop;
16262                       }
16263                     mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
16264                                        &delayed_reg, lo_reg);
16265                   }
16266               last_insn = insn;
16267             }
16268           else
16269             {
16270               /* INSN is a single instruction.  Delete it if it's an
16271                  orphaned high-part relocation.  */
16272               if (mips_orphaned_high_part_p (&htab, insn))
16273                 delete_insn (insn);
16274               /* Also delete cache barriers if the last instruction
16275                  was an annulled branch.  INSN will not be speculatively
16276                  executed.  */
16277               else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
16278                        && last_insn
16279                        && JUMP_P (SEQ_BEGIN (last_insn))
16280                        && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
16281                 delete_insn (insn);
16282               else
16283                 {
16284                   mips_avoid_hazard (last_insn, insn, &hilo_delay,
16285                                      &delayed_reg, lo_reg);
16286                   last_insn = insn;
16287                 }
16288             }
16289         }
16290     }
16291 }
16292
16293 /* Return true if the function has a long branch instruction.  */
16294
16295 static bool
16296 mips_has_long_branch_p (void)
16297 {
16298   rtx_insn *insn, *subinsn;
16299   int normal_length;
16300
16301   /* We need up-to-date instruction lengths.  */
16302   shorten_branches (get_insns ());
16303
16304   /* Look for a branch that is longer than normal.  The normal length for
16305      non-MIPS16 branches is 8, because the length includes the delay slot.
16306      It is 4 for MIPS16, because MIPS16 branches are extended instructions,
16307      but they have no delay slot.  */
16308   normal_length = (TARGET_MIPS16 ? 4 : 8);
16309   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16310     FOR_EACH_SUBINSN (subinsn, insn)
16311       if (JUMP_P (subinsn)
16312           && get_attr_length (subinsn) > normal_length
16313           && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
16314         return true;
16315
16316   return false;
16317 }
16318
16319 /* If we are using a GOT, but have not decided to use a global pointer yet,
16320    see whether we need one to implement long branches.  Convert the ghost
16321    global-pointer instructions into real ones if so.  */
16322
16323 static bool
16324 mips_expand_ghost_gp_insns (void)
16325 {
16326   /* Quick exit if we already know that we will or won't need a
16327      global pointer.  */
16328   if (!TARGET_USE_GOT
16329       || cfun->machine->global_pointer == INVALID_REGNUM
16330       || mips_must_initialize_gp_p ())
16331     return false;
16332
16333   /* Run a full check for long branches.  */
16334   if (!mips_has_long_branch_p ())
16335     return false;
16336
16337   /* We've now established that we need $gp.  */
16338   cfun->machine->must_initialize_gp_p = true;
16339   split_all_insns_noflow ();
16340
16341   return true;
16342 }
16343
16344 /* Subroutine of mips_reorg to manage passes that require DF.  */
16345
16346 static void
16347 mips_df_reorg (void)
16348 {
16349   /* Create def-use chains.  */
16350   df_set_flags (DF_EQ_NOTES);
16351   df_chain_add_problem (DF_UD_CHAIN);
16352   df_analyze ();
16353
16354   if (TARGET_RELAX_PIC_CALLS)
16355     mips_annotate_pic_calls ();
16356
16357   if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
16358     r10k_insert_cache_barriers ();
16359
16360   df_finish_pass (false);
16361 }
16362
16363 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST.  This is
16364    called very late in mips_reorg, but the caller is required to run
16365    mips16_lay_out_constants on the result.  */
16366
16367 static void
16368 mips16_load_branch_target (rtx dest, rtx src)
16369 {
16370   if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
16371     {
16372       rtx page, low;
16373
16374       if (mips_cfun_has_cprestore_slot_p ())
16375         mips_emit_move (dest, mips_cprestore_slot (dest, true));
16376       else
16377         mips_emit_move (dest, pic_offset_table_rtx);
16378       page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
16379       low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
16380       emit_insn (gen_rtx_SET (VOIDmode, dest,
16381                               PMODE_INSN (gen_unspec_got, (dest, page))));
16382       emit_insn (gen_rtx_SET (VOIDmode, dest,
16383                               gen_rtx_LO_SUM (Pmode, dest, low)));
16384     }
16385   else
16386     {
16387       src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
16388       mips_emit_move (dest, src);
16389     }
16390 }
16391
16392 /* If we're compiling a MIPS16 function, look for and split any long branches.
16393    This must be called after all other instruction modifications in
16394    mips_reorg.  */
16395
16396 static void
16397 mips16_split_long_branches (void)
16398 {
16399   bool something_changed;
16400
16401   if (!TARGET_MIPS16)
16402     return;
16403
16404   /* Loop until the alignments for all targets are sufficient.  */
16405   do
16406     {
16407       rtx_insn *insn;
16408
16409       shorten_branches (get_insns ());
16410       something_changed = false;
16411       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16412         if (JUMP_P (insn)
16413             && get_attr_length (insn) > 4
16414             && (any_condjump_p (insn) || any_uncondjump_p (insn)))
16415           {
16416             rtx old_label, temp, saved_temp;
16417             rtx_code_label *new_label;
16418             rtx target;
16419             rtx_insn *jump, *jump_sequence;
16420
16421             start_sequence ();
16422
16423             /* Free up a MIPS16 register by saving it in $1.  */
16424             saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
16425             temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
16426             emit_move_insn (saved_temp, temp);
16427
16428             /* Load the branch target into TEMP.  */
16429             old_label = JUMP_LABEL (insn);
16430             target = gen_rtx_LABEL_REF (Pmode, old_label);
16431             mips16_load_branch_target (temp, target);
16432
16433             /* Jump to the target and restore the register's
16434                original value.  */
16435             jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
16436                                                (temp, temp, saved_temp)));
16437             JUMP_LABEL (jump) = old_label;
16438             LABEL_NUSES (old_label)++;
16439
16440             /* Rewrite any symbolic references that are supposed to use
16441                a PC-relative constant pool.  */
16442             mips16_lay_out_constants (false);
16443
16444             if (simplejump_p (insn))
16445               /* We're going to replace INSN with a longer form.  */
16446               new_label = NULL;
16447             else
16448               {
16449                 /* Create a branch-around label for the original
16450                    instruction.  */
16451                 new_label = gen_label_rtx ();
16452                 emit_label (new_label);
16453               }
16454
16455             jump_sequence = get_insns ();
16456             end_sequence ();
16457
16458             emit_insn_after (jump_sequence, insn);
16459             if (new_label)
16460               invert_jump (insn, new_label, false);
16461             else
16462               delete_insn (insn);
16463             something_changed = true;
16464           }
16465     }
16466   while (something_changed);
16467 }
16468
16469 /* Implement TARGET_MACHINE_DEPENDENT_REORG.  */
16470
16471 static void
16472 mips_reorg (void)
16473 {
16474   /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF.  Also during
16475      insn splitting in mips16_lay_out_constants, DF insn info is only kept up
16476      to date if the CFG is available.  */
16477   if (mips_cfg_in_reorg ())
16478     compute_bb_for_insn ();
16479   mips16_lay_out_constants (true);
16480   if (mips_cfg_in_reorg ())
16481     {
16482       mips_df_reorg ();
16483       free_bb_for_insn ();
16484     }
16485 }
16486
16487 /* We use a machine specific pass to do a second machine dependent reorg
16488    pass after delay branch scheduling.  */
16489
16490 static unsigned int
16491 mips_machine_reorg2 (void)
16492 {
16493   mips_reorg_process_insns ();
16494   if (!TARGET_MIPS16
16495       && TARGET_EXPLICIT_RELOCS
16496       && TUNE_MIPS4130
16497       && TARGET_VR4130_ALIGN)
16498     vr4130_align_insns ();
16499   if (mips_expand_ghost_gp_insns ())
16500     /* The expansion could invalidate some of the VR4130 alignment
16501        optimizations, but this should be an extremely rare case anyhow.  */
16502     mips_reorg_process_insns ();
16503   mips16_split_long_branches ();
16504   return 0;
16505 }
16506
16507 namespace {
16508
16509 const pass_data pass_data_mips_machine_reorg2 =
16510 {
16511   RTL_PASS, /* type */
16512   "mach2", /* name */
16513   OPTGROUP_NONE, /* optinfo_flags */
16514   TV_MACH_DEP, /* tv_id */
16515   0, /* properties_required */
16516   0, /* properties_provided */
16517   0, /* properties_destroyed */
16518   0, /* todo_flags_start */
16519   0, /* todo_flags_finish */
16520 };
16521
16522 class pass_mips_machine_reorg2 : public rtl_opt_pass
16523 {
16524 public:
16525   pass_mips_machine_reorg2(gcc::context *ctxt)
16526     : rtl_opt_pass(pass_data_mips_machine_reorg2, ctxt)
16527   {}
16528
16529   /* opt_pass methods: */
16530   virtual unsigned int execute (function *) { return mips_machine_reorg2 (); }
16531
16532 }; // class pass_mips_machine_reorg2
16533
16534 } // anon namespace
16535
16536 rtl_opt_pass *
16537 make_pass_mips_machine_reorg2 (gcc::context *ctxt)
16538 {
16539   return new pass_mips_machine_reorg2 (ctxt);
16540 }
16541
16542 \f
16543 /* Implement TARGET_ASM_OUTPUT_MI_THUNK.  Generate rtl rather than asm text
16544    in order to avoid duplicating too much logic from elsewhere.  */
16545
16546 static void
16547 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
16548                       HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
16549                       tree function)
16550 {
16551   rtx this_rtx, temp1, temp2, fnaddr;
16552   rtx_insn *insn;
16553   bool use_sibcall_p;
16554
16555   /* Pretend to be a post-reload pass while generating rtl.  */
16556   reload_completed = 1;
16557
16558   /* Mark the end of the (empty) prologue.  */
16559   emit_note (NOTE_INSN_PROLOGUE_END);
16560
16561   /* Determine if we can use a sibcall to call FUNCTION directly.  */
16562   fnaddr = XEXP (DECL_RTL (function), 0);
16563   use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
16564                    && const_call_insn_operand (fnaddr, Pmode));
16565
16566   /* Determine if we need to load FNADDR from the GOT.  */
16567   if (!use_sibcall_p
16568       && (mips_got_symbol_type_p
16569           (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
16570     {
16571       /* Pick a global pointer.  Use a call-clobbered register if
16572          TARGET_CALL_SAVED_GP.  */
16573       cfun->machine->global_pointer
16574         = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
16575       cfun->machine->must_initialize_gp_p = true;
16576       SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
16577
16578       /* Set up the global pointer for n32 or n64 abicalls.  */
16579       mips_emit_loadgp ();
16580     }
16581
16582   /* We need two temporary registers in some cases.  */
16583   temp1 = gen_rtx_REG (Pmode, 2);
16584   temp2 = gen_rtx_REG (Pmode, 3);
16585
16586   /* Find out which register contains the "this" pointer.  */
16587   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
16588     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
16589   else
16590     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
16591
16592   /* Add DELTA to THIS_RTX.  */
16593   if (delta != 0)
16594     {
16595       rtx offset = GEN_INT (delta);
16596       if (!SMALL_OPERAND (delta))
16597         {
16598           mips_emit_move (temp1, offset);
16599           offset = temp1;
16600         }
16601       emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
16602     }
16603
16604   /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX.  */
16605   if (vcall_offset != 0)
16606     {
16607       rtx addr;
16608
16609       /* Set TEMP1 to *THIS_RTX.  */
16610       mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
16611
16612       /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET.  */
16613       addr = mips_add_offset (temp2, temp1, vcall_offset);
16614
16615       /* Load the offset and add it to THIS_RTX.  */
16616       mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
16617       emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
16618     }
16619
16620   /* Jump to the target function.  Use a sibcall if direct jumps are
16621      allowed, otherwise load the address into a register first.  */
16622   if (use_sibcall_p)
16623     {
16624       insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
16625       SIBLING_CALL_P (insn) = 1;
16626     }
16627   else
16628     {
16629       /* This is messy.  GAS treats "la $25,foo" as part of a call
16630          sequence and may allow a global "foo" to be lazily bound.
16631          The general move patterns therefore reject this combination.
16632
16633          In this context, lazy binding would actually be OK
16634          for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
16635          TARGET_CALL_SAVED_GP; see mips_load_call_address.
16636          We must therefore load the address via a temporary
16637          register if mips_dangerous_for_la25_p.
16638
16639          If we jump to the temporary register rather than $25,
16640          the assembler can use the move insn to fill the jump's
16641          delay slot.
16642
16643          We can use the same technique for MIPS16 code, where $25
16644          is not a valid JR register.  */
16645       if (TARGET_USE_PIC_FN_ADDR_REG
16646           && !TARGET_MIPS16
16647           && !mips_dangerous_for_la25_p (fnaddr))
16648         temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
16649       mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
16650
16651       if (TARGET_USE_PIC_FN_ADDR_REG
16652           && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
16653         mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
16654       emit_jump_insn (gen_indirect_jump (temp1));
16655     }
16656
16657   /* Run just enough of rest_of_compilation.  This sequence was
16658      "borrowed" from alpha.c.  */
16659   insn = get_insns ();
16660   split_all_insns_noflow ();
16661   mips16_lay_out_constants (true);
16662   shorten_branches (insn);
16663   final_start_function (insn, file, 1);
16664   final (insn, file, 1);
16665   final_end_function ();
16666
16667   /* Clean up the vars set above.  Note that final_end_function resets
16668      the global pointer for us.  */
16669   reload_completed = 0;
16670 }
16671 \f
16672
16673 /* The last argument passed to mips_set_compression_mode,
16674    or negative if the function hasn't been called yet.  */
16675 static unsigned int old_compression_mode = -1;
16676
16677 /* Set up the target-dependent global state for ISA mode COMPRESSION_MODE,
16678    which is either MASK_MIPS16 or MASK_MICROMIPS.  */
16679
16680 static void
16681 mips_set_compression_mode (unsigned int compression_mode)
16682 {
16683
16684   if (compression_mode == old_compression_mode)
16685     return;
16686
16687   /* Restore base settings of various flags.  */
16688   target_flags = mips_base_target_flags;
16689   flag_schedule_insns = mips_base_schedule_insns;
16690   flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
16691   flag_move_loop_invariants = mips_base_move_loop_invariants;
16692   align_loops = mips_base_align_loops;
16693   align_jumps = mips_base_align_jumps;
16694   align_functions = mips_base_align_functions;
16695   target_flags &= ~(MASK_MIPS16 | MASK_MICROMIPS);
16696   target_flags |= compression_mode;
16697
16698   if (compression_mode & MASK_MIPS16)
16699     {
16700       /* Switch to MIPS16 mode.  */
16701       target_flags |= MASK_MIPS16;
16702
16703       /* Turn off SYNCI if it was on, MIPS16 doesn't support it.  */
16704       target_flags &= ~MASK_SYNCI;
16705
16706       /* Don't run the scheduler before reload, since it tends to
16707          increase register pressure.  */
16708       flag_schedule_insns = 0;
16709
16710       /* Don't do hot/cold partitioning.  mips16_lay_out_constants expects
16711          the whole function to be in a single section.  */
16712       flag_reorder_blocks_and_partition = 0;
16713
16714       /* Don't move loop invariants, because it tends to increase
16715          register pressure.  It also introduces an extra move in cases
16716          where the constant is the first operand in a two-operand binary
16717          instruction, or when it forms a register argument to a functon
16718          call.  */
16719       flag_move_loop_invariants = 0;
16720
16721       target_flags |= MASK_EXPLICIT_RELOCS;
16722
16723       /* Experiments suggest we get the best overall section-anchor
16724          results from using the range of an unextended LW or SW.  Code
16725          that makes heavy use of byte or short accesses can do better
16726          with ranges of 0...31 and 0...63 respectively, but most code is
16727          sensitive to the range of LW and SW instead.  */
16728       targetm.min_anchor_offset = 0;
16729       targetm.max_anchor_offset = 127;
16730
16731       targetm.const_anchor = 0;
16732
16733       /* MIPS16 has no BAL instruction.  */
16734       target_flags &= ~MASK_RELAX_PIC_CALLS;
16735
16736       /* The R4000 errata don't apply to any known MIPS16 cores.
16737          It's simpler to make the R4000 fixes and MIPS16 mode
16738          mutually exclusive.  */
16739       target_flags &= ~MASK_FIX_R4000;
16740
16741       if (flag_pic && !TARGET_OLDABI)
16742         sorry ("MIPS16 PIC for ABIs other than o32 and o64");
16743
16744       if (TARGET_XGOT)
16745         sorry ("MIPS16 -mxgot code");
16746
16747       if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
16748         sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
16749     }
16750   else
16751     {
16752       /* Switch to microMIPS or the standard encoding.  */
16753
16754       if (TARGET_MICROMIPS)
16755         /* Avoid branch likely.  */
16756         target_flags &= ~MASK_BRANCHLIKELY;
16757
16758       /* Provide default values for align_* for 64-bit targets.  */
16759       if (TARGET_64BIT)
16760         {
16761           if (align_loops == 0)
16762             align_loops = 8;
16763           if (align_jumps == 0)
16764             align_jumps = 8;
16765           if (align_functions == 0)
16766             align_functions = 8;
16767         }
16768
16769       targetm.min_anchor_offset = -32768;
16770       targetm.max_anchor_offset = 32767;
16771
16772       targetm.const_anchor = 0x8000;
16773     }
16774
16775   /* (Re)initialize MIPS target internals for new ISA.  */
16776   mips_init_relocs ();
16777
16778   if (compression_mode & MASK_MIPS16)
16779     {
16780       if (!mips16_globals)
16781         mips16_globals = save_target_globals_default_opts ();
16782       else
16783         restore_target_globals (mips16_globals);
16784     }
16785   else
16786     restore_target_globals (&default_target_globals);
16787
16788   old_compression_mode = compression_mode;
16789 }
16790
16791 /* Implement TARGET_SET_CURRENT_FUNCTION.  Decide whether the current
16792    function should use the MIPS16 or microMIPS ISA and switch modes
16793    accordingly.  */
16794
16795 static void
16796 mips_set_current_function (tree fndecl)
16797 {
16798   mips_set_compression_mode (mips_get_compress_mode (fndecl));
16799 }
16800 \f
16801 /* Allocate a chunk of memory for per-function machine-dependent data.  */
16802
16803 static struct machine_function *
16804 mips_init_machine_status (void)
16805 {
16806   return ggc_cleared_alloc<machine_function> ();
16807 }
16808
16809 /* Return the processor associated with the given ISA level, or null
16810    if the ISA isn't valid.  */
16811
16812 static const struct mips_cpu_info *
16813 mips_cpu_info_from_isa (int isa)
16814 {
16815   unsigned int i;
16816
16817   for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16818     if (mips_cpu_info_table[i].isa == isa)
16819       return mips_cpu_info_table + i;
16820
16821   return NULL;
16822 }
16823
16824 /* Return a mips_cpu_info entry determined by an option valued
16825    OPT.  */
16826
16827 static const struct mips_cpu_info *
16828 mips_cpu_info_from_opt (int opt)
16829 {
16830   switch (opt)
16831     {
16832     case MIPS_ARCH_OPTION_FROM_ABI:
16833       /* 'from-abi' selects the most compatible architecture for the
16834          given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
16835          ABIs.  For the EABIs, we have to decide whether we're using
16836          the 32-bit or 64-bit version.  */
16837       return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
16838                                      : ABI_NEEDS_64BIT_REGS ? 3
16839                                      : (TARGET_64BIT ? 3 : 1));
16840
16841     case MIPS_ARCH_OPTION_NATIVE:
16842       gcc_unreachable ();
16843
16844     default:
16845       return &mips_cpu_info_table[opt];
16846     }
16847 }
16848
16849 /* Return a default mips_cpu_info entry, given that no -march= option
16850    was explicitly specified.  */
16851
16852 static const struct mips_cpu_info *
16853 mips_default_arch (void)
16854 {
16855 #if defined (MIPS_CPU_STRING_DEFAULT)
16856   unsigned int i;
16857   for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16858     if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
16859       return mips_cpu_info_table + i;
16860   gcc_unreachable ();
16861 #elif defined (MIPS_ISA_DEFAULT)
16862   return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
16863 #else
16864   /* 'from-abi' makes a good default: you get whatever the ABI
16865      requires.  */
16866   return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
16867 #endif
16868 }
16869
16870 /* Set up globals to generate code for the ISA or processor
16871    described by INFO.  */
16872
16873 static void
16874 mips_set_architecture (const struct mips_cpu_info *info)
16875 {
16876   if (info != 0)
16877     {
16878       mips_arch_info = info;
16879       mips_arch = info->cpu;
16880       mips_isa = info->isa;
16881       if (mips_isa < 32)
16882         mips_isa_rev = 0;
16883       else
16884         mips_isa_rev = (mips_isa & 31) + 1;
16885     }
16886 }
16887
16888 /* Likewise for tuning.  */
16889
16890 static void
16891 mips_set_tune (const struct mips_cpu_info *info)
16892 {
16893   if (info != 0)
16894     {
16895       mips_tune_info = info;
16896       mips_tune = info->cpu;
16897     }
16898 }
16899
16900 /* Implement TARGET_OPTION_OVERRIDE.  */
16901
16902 static void
16903 mips_option_override (void)
16904 {
16905   int i, start, regno, mode;
16906
16907   if (global_options_set.x_mips_isa_option)
16908     mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
16909
16910 #ifdef SUBTARGET_OVERRIDE_OPTIONS
16911   SUBTARGET_OVERRIDE_OPTIONS;
16912 #endif
16913
16914   /* MIPS16 and microMIPS cannot coexist.  */
16915   if (TARGET_MICROMIPS && TARGET_MIPS16)
16916     error ("unsupported combination: %s", "-mips16 -mmicromips");
16917
16918   /* Save the base compression state and process flags as though we
16919      were generating uncompressed code.  */
16920   mips_base_compression_flags = TARGET_COMPRESSION;
16921   target_flags &= ~TARGET_COMPRESSION;
16922
16923   /* -mno-float overrides -mhard-float and -msoft-float.  */
16924   if (TARGET_NO_FLOAT)
16925     {
16926       target_flags |= MASK_SOFT_FLOAT_ABI;
16927       target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
16928     }
16929
16930   if (TARGET_FLIP_MIPS16)
16931     TARGET_INTERLINK_COMPRESSED = 1;
16932
16933   /* Set the small data limit.  */
16934   mips_small_data_threshold = (global_options_set.x_g_switch_value
16935                                ? g_switch_value
16936                                : MIPS_DEFAULT_GVALUE);
16937
16938   /* The following code determines the architecture and register size.
16939      Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
16940      The GAS and GCC code should be kept in sync as much as possible.  */
16941
16942   if (global_options_set.x_mips_arch_option)
16943     mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
16944
16945   if (mips_isa_option_info != 0)
16946     {
16947       if (mips_arch_info == 0)
16948         mips_set_architecture (mips_isa_option_info);
16949       else if (mips_arch_info->isa != mips_isa_option_info->isa)
16950         error ("%<-%s%> conflicts with the other architecture options, "
16951                "which specify a %s processor",
16952                mips_isa_option_info->name,
16953                mips_cpu_info_from_isa (mips_arch_info->isa)->name);
16954     }
16955
16956   if (mips_arch_info == 0)
16957     mips_set_architecture (mips_default_arch ());
16958
16959   if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
16960     error ("%<-march=%s%> is not compatible with the selected ABI",
16961            mips_arch_info->name);
16962
16963   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
16964   if (global_options_set.x_mips_tune_option)
16965     mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
16966
16967   if (mips_tune_info == 0)
16968     mips_set_tune (mips_arch_info);
16969
16970   if ((target_flags_explicit & MASK_64BIT) != 0)
16971     {
16972       /* The user specified the size of the integer registers.  Make sure
16973          it agrees with the ABI and ISA.  */
16974       if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
16975         error ("%<-mgp64%> used with a 32-bit processor");
16976       else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
16977         error ("%<-mgp32%> used with a 64-bit ABI");
16978       else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
16979         error ("%<-mgp64%> used with a 32-bit ABI");
16980     }
16981   else
16982     {
16983       /* Infer the integer register size from the ABI and processor.
16984          Restrict ourselves to 32-bit registers if that's all the
16985          processor has, or if the ABI cannot handle 64-bit registers.  */
16986       if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
16987         target_flags &= ~MASK_64BIT;
16988       else
16989         target_flags |= MASK_64BIT;
16990     }
16991
16992   if ((target_flags_explicit & MASK_FLOAT64) != 0)
16993     {
16994       if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
16995         error ("unsupported combination: %s", "-mfp64 -msingle-float");
16996       else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
16997         error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
16998       else if (!TARGET_64BIT && TARGET_FLOAT64)
16999         {
17000           if (!ISA_HAS_MXHC1)
17001             error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
17002                    " the target supports the mfhc1 and mthc1 instructions");
17003           else if (mips_abi != ABI_32)
17004             error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
17005                    " the o32 ABI");
17006         }
17007     }
17008   else
17009     {
17010       /* -msingle-float selects 32-bit float registers.  Otherwise the
17011          float registers should be the same size as the integer ones.  */
17012       if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
17013         target_flags |= MASK_FLOAT64;
17014       else
17015         target_flags &= ~MASK_FLOAT64;
17016     }
17017
17018   /* End of code shared with GAS.  */
17019
17020   /* The R5900 FPU only supports single precision.  */
17021   if (TARGET_MIPS5900 && TARGET_HARD_FLOAT_ABI && TARGET_DOUBLE_FLOAT)
17022     error ("unsupported combination: %s",
17023            "-march=r5900 -mhard-float -mdouble-float");
17024
17025   /* If a -mlong* option was given, check that it matches the ABI,
17026      otherwise infer the -mlong* setting from the other options.  */
17027   if ((target_flags_explicit & MASK_LONG64) != 0)
17028     {
17029       if (TARGET_LONG64)
17030         {
17031           if (mips_abi == ABI_N32)
17032             error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
17033           else if (mips_abi == ABI_32)
17034             error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
17035           else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
17036             /* We have traditionally allowed non-abicalls code to use
17037                an LP64 form of o64.  However, it would take a bit more
17038                effort to support the combination of 32-bit GOT entries
17039                and 64-bit pointers, so we treat the abicalls case as
17040                an error.  */
17041             error ("the combination of %qs and %qs is incompatible with %qs",
17042                    "-mabi=o64", "-mabicalls", "-mlong64");
17043         }
17044       else
17045         {
17046           if (mips_abi == ABI_64)
17047             error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
17048         }
17049     }
17050   else
17051     {
17052       if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
17053         target_flags |= MASK_LONG64;
17054       else
17055         target_flags &= ~MASK_LONG64;
17056     }
17057
17058   if (!TARGET_OLDABI)
17059     flag_pcc_struct_return = 0;
17060
17061   /* Decide which rtx_costs structure to use.  */
17062   if (optimize_size)
17063     mips_cost = &mips_rtx_cost_optimize_size;
17064   else
17065     mips_cost = &mips_rtx_cost_data[mips_tune];
17066
17067   /* If the user hasn't specified a branch cost, use the processor's
17068      default.  */
17069   if (mips_branch_cost == 0)
17070     mips_branch_cost = mips_cost->branch_cost;
17071
17072   /* If neither -mbranch-likely nor -mno-branch-likely was given
17073      on the command line, set MASK_BRANCHLIKELY based on the target
17074      architecture and tuning flags.  Annulled delay slots are a
17075      size win, so we only consider the processor-specific tuning
17076      for !optimize_size.  */
17077   if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
17078     {
17079       if (ISA_HAS_BRANCHLIKELY
17080           && (optimize_size
17081               || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
17082         target_flags |= MASK_BRANCHLIKELY;
17083       else
17084         target_flags &= ~MASK_BRANCHLIKELY;
17085     }
17086   else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
17087     warning (0, "the %qs architecture does not support branch-likely"
17088              " instructions", mips_arch_info->name);
17089
17090   /* If the user hasn't specified -mimadd or -mno-imadd set
17091      MASK_IMADD based on the target architecture and tuning
17092      flags.  */
17093   if ((target_flags_explicit & MASK_IMADD) == 0)
17094     {
17095       if (ISA_HAS_MADD_MSUB &&
17096           (mips_tune_info->tune_flags & PTF_AVOID_IMADD) == 0)
17097         target_flags |= MASK_IMADD;
17098       else
17099         target_flags &= ~MASK_IMADD;
17100     }
17101   else if (TARGET_IMADD && !ISA_HAS_MADD_MSUB)
17102     warning (0, "the %qs architecture does not support madd or msub"
17103              " instructions", mips_arch_info->name);
17104
17105   /* The effect of -mabicalls isn't defined for the EABI.  */
17106   if (mips_abi == ABI_EABI && TARGET_ABICALLS)
17107     {
17108       error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
17109       target_flags &= ~MASK_ABICALLS;
17110     }
17111
17112   /* PIC requires -mabicalls.  */
17113   if (flag_pic)
17114     {
17115       if (mips_abi == ABI_EABI)
17116         error ("cannot generate position-independent code for %qs",
17117                "-mabi=eabi");
17118       else if (!TARGET_ABICALLS)
17119         error ("position-independent code requires %qs", "-mabicalls");
17120     }
17121
17122   if (TARGET_ABICALLS_PIC2)
17123     /* We need to set flag_pic for executables as well as DSOs
17124        because we may reference symbols that are not defined in
17125        the final executable.  (MIPS does not use things like
17126        copy relocs, for example.)
17127
17128        There is a body of code that uses __PIC__ to distinguish
17129        between -mabicalls and -mno-abicalls code.  The non-__PIC__
17130        variant is usually appropriate for TARGET_ABICALLS_PIC0, as
17131        long as any indirect jumps use $25.  */
17132     flag_pic = 1;
17133
17134   /* -mvr4130-align is a "speed over size" optimization: it usually produces
17135      faster code, but at the expense of more nops.  Enable it at -O3 and
17136      above.  */
17137   if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
17138     target_flags |= MASK_VR4130_ALIGN;
17139
17140   /* Prefer a call to memcpy over inline code when optimizing for size,
17141      though see MOVE_RATIO in mips.h.  */
17142   if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
17143     target_flags |= MASK_MEMCPY;
17144
17145   /* If we have a nonzero small-data limit, check that the -mgpopt
17146      setting is consistent with the other target flags.  */
17147   if (mips_small_data_threshold > 0)
17148     {
17149       if (!TARGET_GPOPT)
17150         {
17151           if (!TARGET_EXPLICIT_RELOCS)
17152             error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
17153
17154           TARGET_LOCAL_SDATA = false;
17155           TARGET_EXTERN_SDATA = false;
17156         }
17157       else
17158         {
17159           if (TARGET_VXWORKS_RTP)
17160             warning (0, "cannot use small-data accesses for %qs", "-mrtp");
17161
17162           if (TARGET_ABICALLS)
17163             warning (0, "cannot use small-data accesses for %qs",
17164                      "-mabicalls");
17165         }
17166     }
17167
17168   /* Pre-IEEE 754-2008 MIPS hardware has a quirky almost-IEEE format
17169      for all its floating point.  */
17170   if (mips_nan != MIPS_IEEE_754_2008)
17171     {
17172       REAL_MODE_FORMAT (SFmode) = &mips_single_format;
17173       REAL_MODE_FORMAT (DFmode) = &mips_double_format;
17174       REAL_MODE_FORMAT (TFmode) = &mips_quad_format;
17175     }
17176
17177   /* Make sure that the user didn't turn off paired single support when
17178      MIPS-3D support is requested.  */
17179   if (TARGET_MIPS3D
17180       && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
17181       && !TARGET_PAIRED_SINGLE_FLOAT)
17182     error ("%<-mips3d%> requires %<-mpaired-single%>");
17183
17184   /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT.  */
17185   if (TARGET_MIPS3D)
17186     target_flags |= MASK_PAIRED_SINGLE_FLOAT;
17187
17188   /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
17189      and TARGET_HARD_FLOAT_ABI are both true.  */
17190   if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
17191     {
17192       error ("%qs must be used with %qs",
17193              TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
17194              TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
17195       target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
17196       TARGET_MIPS3D = 0;
17197     }
17198
17199   /* Make sure that -mpaired-single is only used on ISAs that support it.
17200      We must disable it otherwise since it relies on other ISA properties
17201      like ISA_HAS_8CC having their normal values.  */
17202   if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
17203     {
17204       error ("the %qs architecture does not support paired-single"
17205              " instructions", mips_arch_info->name);
17206       target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
17207       TARGET_MIPS3D = 0;
17208     }
17209
17210   if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
17211       && !TARGET_CACHE_BUILTIN)
17212     {
17213       error ("%qs requires a target that provides the %qs instruction",
17214              "-mr10k-cache-barrier", "cache");
17215       mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
17216     }
17217
17218   /* If TARGET_DSPR2, enable TARGET_DSP.  */
17219   if (TARGET_DSPR2)
17220     TARGET_DSP = true;
17221
17222   /* .eh_frame addresses should be the same width as a C pointer.
17223      Most MIPS ABIs support only one pointer size, so the assembler
17224      will usually know exactly how big an .eh_frame address is.
17225
17226      Unfortunately, this is not true of the 64-bit EABI.  The ABI was
17227      originally defined to use 64-bit pointers (i.e. it is LP64), and
17228      this is still the default mode.  However, we also support an n32-like
17229      ILP32 mode, which is selected by -mlong32.  The problem is that the
17230      assembler has traditionally not had an -mlong option, so it has
17231      traditionally not known whether we're using the ILP32 or LP64 form.
17232
17233      As it happens, gas versions up to and including 2.19 use _32-bit_
17234      addresses for EABI64 .cfi_* directives.  This is wrong for the
17235      default LP64 mode, so we can't use the directives by default.
17236      Moreover, since gas's current behavior is at odds with gcc's
17237      default behavior, it seems unwise to rely on future versions
17238      of gas behaving the same way.  We therefore avoid using .cfi
17239      directives for -mlong32 as well.  */
17240   if (mips_abi == ABI_EABI && TARGET_64BIT)
17241     flag_dwarf2_cfi_asm = 0;
17242
17243   /* .cfi_* directives generate a read-only section, so fall back on
17244      manual .eh_frame creation if we need the section to be writable.  */
17245   if (TARGET_WRITABLE_EH_FRAME)
17246     flag_dwarf2_cfi_asm = 0;
17247
17248   mips_init_print_operand_punct ();
17249
17250   /* Set up array to map GCC register number to debug register number.
17251      Ignore the special purpose register numbers.  */
17252
17253   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17254     {
17255       mips_dbx_regno[i] = IGNORED_DWARF_REGNUM;
17256       if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
17257         mips_dwarf_regno[i] = i;
17258       else
17259         mips_dwarf_regno[i] = INVALID_REGNUM;
17260     }
17261
17262   start = GP_DBX_FIRST - GP_REG_FIRST;
17263   for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
17264     mips_dbx_regno[i] = i + start;
17265
17266   start = FP_DBX_FIRST - FP_REG_FIRST;
17267   for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
17268     mips_dbx_regno[i] = i + start;
17269
17270   /* Accumulator debug registers use big-endian ordering.  */
17271   mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
17272   mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
17273   mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
17274   mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
17275   for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
17276     {
17277       mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
17278       mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
17279     }
17280
17281   /* Set up mips_hard_regno_mode_ok.  */
17282   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
17283     for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
17284       mips_hard_regno_mode_ok[mode][regno]
17285         = mips_hard_regno_mode_ok_p (regno, (enum machine_mode) mode);
17286
17287   /* Function to allocate machine-dependent function status.  */
17288   init_machine_status = &mips_init_machine_status;
17289
17290   /* Default to working around R4000 errata only if the processor
17291      was selected explicitly.  */
17292   if ((target_flags_explicit & MASK_FIX_R4000) == 0
17293       && strcmp (mips_arch_info->name, "r4000") == 0)
17294     target_flags |= MASK_FIX_R4000;
17295
17296   /* Default to working around R4400 errata only if the processor
17297      was selected explicitly.  */
17298   if ((target_flags_explicit & MASK_FIX_R4400) == 0
17299       && strcmp (mips_arch_info->name, "r4400") == 0)
17300     target_flags |= MASK_FIX_R4400;
17301
17302   /* Default to working around R10000 errata only if the processor
17303      was selected explicitly.  */
17304   if ((target_flags_explicit & MASK_FIX_R10000) == 0
17305       && strcmp (mips_arch_info->name, "r10000") == 0)
17306     target_flags |= MASK_FIX_R10000;
17307
17308   /* Make sure that branch-likely instructions available when using
17309      -mfix-r10000.  The instructions are not available if either:
17310
17311         1. -mno-branch-likely was passed.
17312         2. The selected ISA does not support branch-likely and
17313            the command line does not include -mbranch-likely.  */
17314   if (TARGET_FIX_R10000
17315       && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
17316           ? !ISA_HAS_BRANCHLIKELY
17317           : !TARGET_BRANCHLIKELY))
17318     sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
17319
17320   if (TARGET_SYNCI && !ISA_HAS_SYNCI)
17321     {
17322       warning (0, "the %qs architecture does not support the synci "
17323                "instruction", mips_arch_info->name);
17324       target_flags &= ~MASK_SYNCI;
17325     }
17326
17327   /* Only optimize PIC indirect calls if they are actually required.  */
17328   if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
17329     target_flags &= ~MASK_RELAX_PIC_CALLS;
17330
17331   /* Save base state of options.  */
17332   mips_base_target_flags = target_flags;
17333   mips_base_schedule_insns = flag_schedule_insns;
17334   mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
17335   mips_base_move_loop_invariants = flag_move_loop_invariants;
17336   mips_base_align_loops = align_loops;
17337   mips_base_align_jumps = align_jumps;
17338   mips_base_align_functions = align_functions;
17339
17340   /* Now select the ISA mode.
17341
17342      Do all CPP-sensitive stuff in uncompressed mode; we'll switch modes
17343      later if required.  */
17344   mips_set_compression_mode (0);
17345
17346   /* We register a second machine specific reorg pass after delay slot
17347      filling.  Registering the pass must be done at start up.  It's
17348      convenient to do it here.  */
17349   opt_pass *new_pass = make_pass_mips_machine_reorg2 (g);
17350   struct register_pass_info insert_pass_mips_machine_reorg2 =
17351     {
17352       new_pass,         /* pass */
17353       "dbr",                    /* reference_pass_name */
17354       1,                        /* ref_pass_instance_number */
17355       PASS_POS_INSERT_AFTER     /* po_op */
17356     };
17357   register_pass (&insert_pass_mips_machine_reorg2);
17358
17359   if (TARGET_HARD_FLOAT_ABI && TARGET_MIPS5900)
17360     REAL_MODE_FORMAT (SFmode) = &spu_single_format;
17361 }
17362
17363 /* Swap the register information for registers I and I + 1, which
17364    currently have the wrong endianness.  Note that the registers'
17365    fixedness and call-clobberedness might have been set on the
17366    command line.  */
17367
17368 static void
17369 mips_swap_registers (unsigned int i)
17370 {
17371   int tmpi;
17372   const char *tmps;
17373
17374 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
17375 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
17376
17377   SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
17378   SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
17379   SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
17380   SWAP_STRING (reg_names[i], reg_names[i + 1]);
17381
17382 #undef SWAP_STRING
17383 #undef SWAP_INT
17384 }
17385
17386 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE.  */
17387
17388 static void
17389 mips_conditional_register_usage (void)
17390 {
17391
17392   if (ISA_HAS_DSP)
17393     {
17394       /* These DSP control register fields are global.  */
17395       global_regs[CCDSP_PO_REGNUM] = 1;
17396       global_regs[CCDSP_SC_REGNUM] = 1;
17397     }
17398   else
17399     AND_COMPL_HARD_REG_SET (accessible_reg_set,
17400                             reg_class_contents[(int) DSP_ACC_REGS]);
17401
17402   if (!TARGET_HARD_FLOAT)
17403     {
17404       AND_COMPL_HARD_REG_SET (accessible_reg_set,
17405                               reg_class_contents[(int) FP_REGS]);
17406       AND_COMPL_HARD_REG_SET (accessible_reg_set,
17407                               reg_class_contents[(int) ST_REGS]);
17408     }
17409   else if (!ISA_HAS_8CC)
17410     {
17411       /* We only have a single condition-code register.  We implement
17412          this by fixing all the condition-code registers and generating
17413          RTL that refers directly to ST_REG_FIRST.  */
17414       AND_COMPL_HARD_REG_SET (accessible_reg_set,
17415                               reg_class_contents[(int) ST_REGS]);
17416       SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
17417       fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
17418     }
17419   if (TARGET_MIPS16)
17420     {
17421       /* In MIPS16 mode, we prohibit the unused $s registers, since they
17422          are call-saved, and saving them via a MIPS16 register would
17423          probably waste more time than just reloading the value.
17424
17425          We permit the $t temporary registers when optimizing for speed
17426          but not when optimizing for space because using them results in
17427          code that is larger (but faster) then not using them.  We do
17428          allow $24 (t8) because it is used in CMP and CMPI instructions
17429          and $25 (t9) because it is used as the function call address in
17430          SVR4 PIC code.  */
17431
17432       fixed_regs[18] = call_used_regs[18] = 1;
17433       fixed_regs[19] = call_used_regs[19] = 1;
17434       fixed_regs[20] = call_used_regs[20] = 1;
17435       fixed_regs[21] = call_used_regs[21] = 1;
17436       fixed_regs[22] = call_used_regs[22] = 1;
17437       fixed_regs[23] = call_used_regs[23] = 1;
17438       fixed_regs[26] = call_used_regs[26] = 1;
17439       fixed_regs[27] = call_used_regs[27] = 1;
17440       fixed_regs[30] = call_used_regs[30] = 1;
17441       if (optimize_size)
17442         {
17443           fixed_regs[8] = call_used_regs[8] = 1;
17444           fixed_regs[9] = call_used_regs[9] = 1;
17445           fixed_regs[10] = call_used_regs[10] = 1;
17446           fixed_regs[11] = call_used_regs[11] = 1;
17447           fixed_regs[12] = call_used_regs[12] = 1;
17448           fixed_regs[13] = call_used_regs[13] = 1;
17449           fixed_regs[14] = call_used_regs[14] = 1;
17450           fixed_regs[15] = call_used_regs[15] = 1;
17451         }
17452
17453       /* Do not allow HI and LO to be treated as register operands.
17454          There are no MTHI or MTLO instructions (or any real need
17455          for them) and one-way registers cannot easily be reloaded.  */
17456       AND_COMPL_HARD_REG_SET (operand_reg_set,
17457                               reg_class_contents[(int) MD_REGS]);
17458     }
17459   /* $f20-$f23 are call-clobbered for n64.  */
17460   if (mips_abi == ABI_64)
17461     {
17462       int regno;
17463       for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
17464         call_really_used_regs[regno] = call_used_regs[regno] = 1;
17465     }
17466   /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
17467      for n32.  */
17468   if (mips_abi == ABI_N32)
17469     {
17470       int regno;
17471       for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
17472         call_really_used_regs[regno] = call_used_regs[regno] = 1;
17473     }
17474   /* Make sure that double-register accumulator values are correctly
17475      ordered for the current endianness.  */
17476   if (TARGET_LITTLE_ENDIAN)
17477     {
17478       unsigned int regno;
17479
17480       mips_swap_registers (MD_REG_FIRST);
17481       for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
17482         mips_swap_registers (regno);
17483     }
17484 }
17485
17486 /* Implement EH_USES.  */
17487
17488 bool
17489 mips_eh_uses (unsigned int regno)
17490 {
17491   if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
17492     {
17493       /* We need to force certain registers to be live in order to handle
17494          PIC long branches correctly.  See mips_must_initialize_gp_p for
17495          details.  */
17496       if (mips_cfun_has_cprestore_slot_p ())
17497         {
17498           if (regno == CPRESTORE_SLOT_REGNUM)
17499             return true;
17500         }
17501       else
17502         {
17503           if (cfun->machine->global_pointer == regno)
17504             return true;
17505         }
17506     }
17507
17508   return false;
17509 }
17510
17511 /* Implement EPILOGUE_USES.  */
17512
17513 bool
17514 mips_epilogue_uses (unsigned int regno)
17515 {
17516   /* Say that the epilogue uses the return address register.  Note that
17517      in the case of sibcalls, the values "used by the epilogue" are
17518      considered live at the start of the called function.  */
17519   if (regno == RETURN_ADDR_REGNUM)
17520     return true;
17521
17522   /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
17523      See the comment above load_call<mode> for details.  */
17524   if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
17525     return true;
17526
17527   /* An interrupt handler must preserve some registers that are
17528      ordinarily call-clobbered.  */
17529   if (cfun->machine->interrupt_handler_p
17530       && mips_interrupt_extra_call_saved_reg_p (regno))
17531     return true;
17532
17533   return false;
17534 }
17535
17536 /* A for_each_rtx callback.  Stop the search if *X is an AT register.  */
17537
17538 static int
17539 mips_at_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
17540 {
17541   return REG_P (*x) && REGNO (*x) == AT_REGNUM;
17542 }
17543
17544 /* Return true if INSN needs to be wrapped in ".set noat".
17545    INSN has NOPERANDS operands, stored in OPVEC.  */
17546
17547 static bool
17548 mips_need_noat_wrapper_p (rtx_insn *insn, rtx *opvec, int noperands)
17549 {
17550   int i;
17551
17552   if (recog_memoized (insn) >= 0)
17553     for (i = 0; i < noperands; i++)
17554       if (for_each_rtx (&opvec[i], mips_at_reg_p, NULL))
17555         return true;
17556   return false;
17557 }
17558
17559 /* Implement FINAL_PRESCAN_INSN.  */
17560
17561 void
17562 mips_final_prescan_insn (rtx_insn *insn, rtx *opvec, int noperands)
17563 {
17564   if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17565     mips_push_asm_switch (&mips_noat);
17566 }
17567
17568 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN.  */
17569
17570 static void
17571 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx_insn *insn,
17572                           rtx *opvec, int noperands)
17573 {
17574   if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17575     mips_pop_asm_switch (&mips_noat);
17576 }
17577
17578 /* Return the function that is used to expand the <u>mulsidi3 pattern.
17579    EXT_CODE is the code of the extension used.  Return NULL if widening
17580    multiplication shouldn't be used.  */
17581
17582 mulsidi3_gen_fn
17583 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
17584 {
17585   bool signed_p;
17586
17587   signed_p = ext_code == SIGN_EXTEND;
17588   if (TARGET_64BIT)
17589     {
17590       /* Don't use widening multiplication with MULT when we have DMUL.  Even
17591          with the extension of its input operands DMUL is faster.  Note that
17592          the extension is not needed for signed multiplication.  In order to
17593          ensure that we always remove the redundant sign-extension in this
17594          case we still expand mulsidi3 for DMUL.  */
17595       if (ISA_HAS_DMUL3)
17596         return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
17597       if (TARGET_MIPS16)
17598         return (signed_p
17599                 ? gen_mulsidi3_64bit_mips16
17600                 : gen_umulsidi3_64bit_mips16);
17601       if (TARGET_FIX_R4000)
17602         return NULL;
17603       return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
17604     }
17605   else
17606     {
17607       if (TARGET_MIPS16)
17608         return (signed_p
17609                 ? gen_mulsidi3_32bit_mips16
17610                 : gen_umulsidi3_32bit_mips16);
17611       if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
17612         return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
17613       return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
17614     }
17615 }
17616
17617 /* Return true if PATTERN matches the kind of instruction generated by
17618    umips_build_save_restore.  SAVE_P is true for store.  */
17619
17620 bool
17621 umips_save_restore_pattern_p (bool save_p, rtx pattern)
17622 {
17623   int n;
17624   unsigned int i;
17625   HOST_WIDE_INT first_offset = 0;
17626   rtx first_base = 0;
17627   unsigned int regmask = 0;
17628
17629   for (n = 0; n < XVECLEN (pattern, 0); n++)
17630     {
17631       rtx set, reg, mem, this_base;
17632       HOST_WIDE_INT this_offset;
17633
17634       /* Check that we have a SET.  */
17635       set = XVECEXP (pattern, 0, n);
17636       if (GET_CODE (set) != SET)
17637         return false;
17638
17639       /* Check that the SET is a load (if restoring) or a store
17640          (if saving).  */
17641       mem = save_p ? SET_DEST (set) : SET_SRC (set);
17642       if (!MEM_P (mem) || MEM_VOLATILE_P (mem))
17643         return false;
17644
17645       /* Check that the address is the sum of base and a possibly-zero
17646          constant offset.  Determine if the offset is in range.  */
17647       mips_split_plus (XEXP (mem, 0), &this_base, &this_offset);
17648       if (!REG_P (this_base))
17649         return false;
17650
17651       if (n == 0)
17652         {
17653           if (!UMIPS_12BIT_OFFSET_P (this_offset))
17654             return false;
17655           first_base = this_base;
17656           first_offset = this_offset;
17657         }
17658       else
17659         {
17660           /* Check that the save slots are consecutive.  */
17661           if (REGNO (this_base) != REGNO (first_base)
17662               || this_offset != first_offset + UNITS_PER_WORD * n)
17663             return false;
17664         }
17665
17666       /* Check that SET's other operand is a register.  */
17667       reg = save_p ? SET_SRC (set) : SET_DEST (set);
17668       if (!REG_P (reg))
17669         return false;
17670
17671       regmask |= 1 << REGNO (reg);
17672     }
17673
17674   for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
17675     if (regmask == umips_swm_mask[i])
17676       return true;
17677
17678   return false;
17679 }
17680
17681 /* Return the assembly instruction for microMIPS LWM or SWM.
17682    SAVE_P and PATTERN are as for umips_save_restore_pattern_p.  */
17683
17684 const char *
17685 umips_output_save_restore (bool save_p, rtx pattern)
17686 {
17687   static char buffer[300];
17688   char *s;
17689   int n;
17690   HOST_WIDE_INT offset;
17691   rtx base, mem, set, last_set, last_reg;
17692
17693   /* Parse the pattern.  */
17694   gcc_assert (umips_save_restore_pattern_p (save_p, pattern));
17695
17696   s = strcpy (buffer, save_p ? "swm\t" : "lwm\t");
17697   s += strlen (s);
17698   n = XVECLEN (pattern, 0);
17699
17700   set = XVECEXP (pattern, 0, 0);
17701   mem = save_p ? SET_DEST (set) : SET_SRC (set);
17702   mips_split_plus (XEXP (mem, 0), &base, &offset);
17703
17704   last_set = XVECEXP (pattern, 0, n - 1);
17705   last_reg = save_p ? SET_SRC (last_set) : SET_DEST (last_set);
17706
17707   if (REGNO (last_reg) == 31)
17708     n--;
17709
17710   gcc_assert (n <= 9);
17711   if (n == 0)
17712     ;
17713   else if (n == 1)
17714     s += sprintf (s, "%s,", reg_names[16]);
17715   else if (n < 9)
17716     s += sprintf (s, "%s-%s,", reg_names[16], reg_names[15 + n]);
17717   else if (n == 9)
17718     s += sprintf (s, "%s-%s,%s,", reg_names[16], reg_names[23],
17719                   reg_names[30]);
17720
17721   if (REGNO (last_reg) == 31)
17722     s += sprintf (s, "%s,", reg_names[31]);
17723
17724   s += sprintf (s, "%d(%s)", (int)offset, reg_names[REGNO (base)]);
17725   return buffer;
17726 }
17727
17728 /* Return true if MEM1 and MEM2 use the same base register, and the
17729    offset of MEM2 equals the offset of MEM1 plus 4.  FIRST_REG is the
17730    register into (from) which the contents of MEM1 will be loaded
17731    (stored), depending on the value of LOAD_P.
17732    SWAP_P is true when the 1st and 2nd instructions are swapped.  */
17733
17734 static bool
17735 umips_load_store_pair_p_1 (bool load_p, bool swap_p,
17736                            rtx first_reg, rtx mem1, rtx mem2)
17737 {
17738   rtx base1, base2;
17739   HOST_WIDE_INT offset1, offset2;
17740
17741   if (!MEM_P (mem1) || !MEM_P (mem2))
17742     return false;
17743
17744   mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
17745   mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
17746
17747   if (!REG_P (base1) || !rtx_equal_p (base1, base2))
17748     return false;
17749
17750   /* Avoid invalid load pair instructions.  */
17751   if (load_p && REGNO (first_reg) == REGNO (base1))
17752     return false;
17753
17754   /* We must avoid this case for anti-dependence.
17755      Ex:  lw $3, 4($3)
17756           lw $2, 0($3)
17757      first_reg is $2, but the base is $3.  */
17758   if (load_p
17759       && swap_p
17760       && REGNO (first_reg) + 1 == REGNO (base1))
17761     return false;
17762
17763   if (offset2 != offset1 + 4)
17764     return false;
17765
17766   if (!UMIPS_12BIT_OFFSET_P (offset1))
17767     return false;
17768
17769   return true;
17770 }
17771
17772 /* OPERANDS describes the operands to a pair of SETs, in the order
17773    dest1, src1, dest2, src2.  Return true if the operands can be used
17774    in an LWP or SWP instruction; LOAD_P says which.  */
17775
17776 bool
17777 umips_load_store_pair_p (bool load_p, rtx *operands)
17778 {
17779   rtx reg1, reg2, mem1, mem2;
17780
17781   if (load_p)
17782     {
17783       reg1 = operands[0];
17784       reg2 = operands[2];
17785       mem1 = operands[1];
17786       mem2 = operands[3];
17787     }
17788   else
17789     {
17790       reg1 = operands[1];
17791       reg2 = operands[3];
17792       mem1 = operands[0];
17793       mem2 = operands[2];
17794     }
17795
17796   if (REGNO (reg2) == REGNO (reg1) + 1)
17797     return umips_load_store_pair_p_1 (load_p, false, reg1, mem1, mem2);
17798
17799   if (REGNO (reg1) == REGNO (reg2) + 1)
17800     return umips_load_store_pair_p_1 (load_p, true, reg2, mem2, mem1);
17801
17802   return false;
17803 }
17804
17805 /* Return the assembly instruction for a microMIPS LWP or SWP in which
17806    the first register is REG and the first memory slot is MEM.
17807    LOAD_P is true for LWP.  */
17808
17809 static void
17810 umips_output_load_store_pair_1 (bool load_p, rtx reg, rtx mem)
17811 {
17812   rtx ops[] = {reg, mem};
17813
17814   if (load_p)
17815     output_asm_insn ("lwp\t%0,%1", ops);
17816   else
17817     output_asm_insn ("swp\t%0,%1", ops);
17818 }
17819
17820 /* Output the assembly instruction for a microMIPS LWP or SWP instruction.
17821    LOAD_P and OPERANDS are as for umips_load_store_pair_p.  */
17822
17823 void
17824 umips_output_load_store_pair (bool load_p, rtx *operands)
17825 {
17826   rtx reg1, reg2, mem1, mem2;
17827   if (load_p)
17828     {
17829       reg1 = operands[0];
17830       reg2 = operands[2];
17831       mem1 = operands[1];
17832       mem2 = operands[3];
17833     }
17834   else
17835     {
17836       reg1 = operands[1];
17837       reg2 = operands[3];
17838       mem1 = operands[0];
17839       mem2 = operands[2];
17840     }
17841
17842   if (REGNO (reg2) == REGNO (reg1) + 1)
17843     {
17844       umips_output_load_store_pair_1 (load_p, reg1, mem1);
17845       return;
17846     }
17847
17848   gcc_assert (REGNO (reg1) == REGNO (reg2) + 1);
17849   umips_output_load_store_pair_1 (load_p, reg2, mem2);
17850 }
17851
17852 /* Return true if REG1 and REG2 match the criteria for a movep insn.  */
17853
17854 bool
17855 umips_movep_target_p (rtx reg1, rtx reg2)
17856 {
17857   int regno1, regno2, pair;
17858   unsigned int i;
17859   static const int match[8] = {
17860     0x00000060, /* 5, 6 */
17861     0x000000a0, /* 5, 7 */
17862     0x000000c0, /* 6, 7 */
17863     0x00200010, /* 4, 21 */
17864     0x00400010, /* 4, 22 */
17865     0x00000030, /* 4, 5 */
17866     0x00000050, /* 4, 6 */
17867     0x00000090  /* 4, 7 */
17868   };
17869
17870   if (!REG_P (reg1) || !REG_P (reg2))
17871     return false;
17872
17873   regno1 = REGNO (reg1);
17874   regno2 = REGNO (reg2);
17875
17876   if (!GP_REG_P (regno1) || !GP_REG_P (regno2))
17877     return false;
17878
17879   pair = (1 << regno1) | (1 << regno2);
17880
17881   for (i = 0; i < ARRAY_SIZE (match); i++)
17882     if (pair == match[i])
17883       return true;
17884
17885   return false;
17886 }
17887 \f
17888 /* Return the size in bytes of the trampoline code, padded to
17889    TRAMPOLINE_ALIGNMENT bits.  The static chain pointer and target
17890    function address immediately follow.  */
17891
17892 int
17893 mips_trampoline_code_size (void)
17894 {
17895   if (TARGET_USE_PIC_FN_ADDR_REG)
17896     return 4 * 4;
17897   else if (ptr_mode == DImode)
17898     return 8 * 4;
17899   else if (ISA_HAS_LOAD_DELAY)
17900     return 6 * 4;
17901   else
17902     return 4 * 4;
17903 }
17904
17905 /* Implement TARGET_TRAMPOLINE_INIT.  */
17906
17907 static void
17908 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
17909 {
17910   rtx addr, end_addr, high, low, opcode, mem;
17911   rtx trampoline[8];
17912   unsigned int i, j;
17913   HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
17914
17915   /* Work out the offsets of the pointers from the start of the
17916      trampoline code.  */
17917   end_addr_offset = mips_trampoline_code_size ();
17918   static_chain_offset = end_addr_offset;
17919   target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
17920
17921   /* Get pointers to the beginning and end of the code block.  */
17922   addr = force_reg (Pmode, XEXP (m_tramp, 0));
17923   end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
17924
17925 #define OP(X) gen_int_mode (X, SImode)
17926
17927   /* Build up the code in TRAMPOLINE.  */
17928   i = 0;
17929   if (TARGET_USE_PIC_FN_ADDR_REG)
17930     {
17931       /* $25 contains the address of the trampoline.  Emit code of the form:
17932
17933              l[wd]    $1, target_function_offset($25)
17934              l[wd]    $static_chain, static_chain_offset($25)
17935              jr       $1
17936              move     $25,$1.  */
17937       trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
17938                                            target_function_offset,
17939                                            PIC_FUNCTION_ADDR_REGNUM));
17940       trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17941                                            static_chain_offset,
17942                                            PIC_FUNCTION_ADDR_REGNUM));
17943       trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
17944       trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
17945     }
17946   else if (ptr_mode == DImode)
17947     {
17948       /* It's too cumbersome to create the full 64-bit address, so let's
17949          instead use:
17950
17951              move    $1, $31
17952              bal     1f
17953              nop
17954          1:  l[wd]   $25, target_function_offset - 12($31)
17955              l[wd]   $static_chain, static_chain_offset - 12($31)
17956              jr      $25
17957              move    $31, $1
17958
17959         where 12 is the offset of "1:" from the start of the code block.  */
17960       trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
17961       trampoline[i++] = OP (MIPS_BAL (1));
17962       trampoline[i++] = OP (MIPS_NOP);
17963       trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
17964                                            target_function_offset - 12,
17965                                            RETURN_ADDR_REGNUM));
17966       trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17967                                            static_chain_offset - 12,
17968                                            RETURN_ADDR_REGNUM));
17969       trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17970       trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
17971     }
17972   else
17973     {
17974       /* If the target has load delays, emit:
17975
17976              lui     $1, %hi(end_addr)
17977              lw      $25, %lo(end_addr + ...)($1)
17978              lw      $static_chain, %lo(end_addr + ...)($1)
17979              jr      $25
17980              nop
17981
17982          Otherwise emit:
17983
17984              lui     $1, %hi(end_addr)
17985              lw      $25, %lo(end_addr + ...)($1)
17986              jr      $25
17987              lw      $static_chain, %lo(end_addr + ...)($1).  */
17988
17989       /* Split END_ADDR into %hi and %lo values.  Trampolines are aligned
17990          to 64 bits, so the %lo value will have the bottom 3 bits clear.  */
17991       high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
17992                                   NULL, false, OPTAB_WIDEN);
17993       high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
17994                                   NULL, false, OPTAB_WIDEN);
17995       low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
17996
17997       /* Emit the LUI.  */
17998       opcode = OP (MIPS_LUI (AT_REGNUM, 0));
17999       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
18000                                              NULL, false, OPTAB_WIDEN);
18001
18002       /* Emit the load of the target function.  */
18003       opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
18004                                   target_function_offset - end_addr_offset,
18005                                   AT_REGNUM));
18006       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
18007                                              NULL, false, OPTAB_WIDEN);
18008
18009       /* Emit the JR here, if we can.  */
18010       if (!ISA_HAS_LOAD_DELAY)
18011         trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18012
18013       /* Emit the load of the static chain register.  */
18014       opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
18015                                   static_chain_offset - end_addr_offset,
18016                                   AT_REGNUM));
18017       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
18018                                              NULL, false, OPTAB_WIDEN);
18019
18020       /* Emit the JR, if we couldn't above.  */
18021       if (ISA_HAS_LOAD_DELAY)
18022         {
18023           trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18024           trampoline[i++] = OP (MIPS_NOP);
18025         }
18026     }
18027
18028 #undef OP
18029
18030   /* Copy the trampoline code.  Leave any padding uninitialized.  */
18031   for (j = 0; j < i; j++)
18032     {
18033       mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
18034       mips_emit_move (mem, trampoline[j]);
18035     }
18036
18037   /* Set up the static chain pointer field.  */
18038   mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
18039   mips_emit_move (mem, chain_value);
18040
18041   /* Set up the target function field.  */
18042   mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
18043   mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
18044
18045   /* Flush the code part of the trampoline.  */
18046   emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
18047   emit_insn (gen_clear_cache (addr, end_addr));
18048 }
18049
18050 /* Implement FUNCTION_PROFILER.  */
18051
18052 void mips_function_profiler (FILE *file)
18053 {
18054   if (TARGET_MIPS16)
18055     sorry ("mips16 function profiling");
18056   if (TARGET_LONG_CALLS)
18057     {
18058       /* For TARGET_LONG_CALLS use $3 for the address of _mcount.  */
18059       if (Pmode == DImode)
18060         fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
18061       else
18062         fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
18063     }
18064   mips_push_asm_switch (&mips_noat);
18065   fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
18066            reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
18067   /* _mcount treats $2 as the static chain register.  */
18068   if (cfun->static_chain_decl != NULL)
18069     fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
18070              reg_names[STATIC_CHAIN_REGNUM]);
18071   if (TARGET_MCOUNT_RA_ADDRESS)
18072     {
18073       /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
18074          ra save location.  */
18075       if (cfun->machine->frame.ra_fp_offset == 0)
18076         /* ra not saved, pass zero.  */
18077         fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
18078       else
18079         fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
18080                  Pmode == DImode ? "dla" : "la", reg_names[12],
18081                  cfun->machine->frame.ra_fp_offset,
18082                  reg_names[STACK_POINTER_REGNUM]);
18083     }
18084   if (!TARGET_NEWABI)
18085     fprintf (file,
18086              "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from  stack\n",
18087              TARGET_64BIT ? "dsubu" : "subu",
18088              reg_names[STACK_POINTER_REGNUM],
18089              reg_names[STACK_POINTER_REGNUM],
18090              Pmode == DImode ? 16 : 8);
18091
18092   if (TARGET_LONG_CALLS)
18093     fprintf (file, "\tjalr\t%s\n", reg_names[3]);
18094   else
18095     fprintf (file, "\tjal\t_mcount\n");
18096   mips_pop_asm_switch (&mips_noat);
18097   /* _mcount treats $2 as the static chain register.  */
18098   if (cfun->static_chain_decl != NULL)
18099     fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
18100              reg_names[2]);
18101 }
18102
18103 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  We want to keep the default
18104    behaviour of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
18105    when TARGET_LOONGSON_VECTORS is true.  */
18106
18107 static unsigned HOST_WIDE_INT
18108 mips_shift_truncation_mask (enum machine_mode mode)
18109 {
18110   if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
18111     return 0;
18112
18113   return GET_MODE_BITSIZE (mode) - 1;
18114 }
18115
18116 /* Implement TARGET_PREPARE_PCH_SAVE.  */
18117
18118 static void
18119 mips_prepare_pch_save (void)
18120 {
18121   /* We are called in a context where the current MIPS16 vs. non-MIPS16
18122      setting should be irrelevant.  The question then is: which setting
18123      makes most sense at load time?
18124
18125      The PCH is loaded before the first token is read.  We should never
18126      have switched into MIPS16 mode by that point, and thus should not
18127      have populated mips16_globals.  Nor can we load the entire contents
18128      of mips16_globals from the PCH file, because mips16_globals contains
18129      a combination of GGC and non-GGC data.
18130
18131      There is therefore no point in trying save the GGC part of
18132      mips16_globals to the PCH file, or to preserve MIPS16ness across
18133      the PCH save and load.  The loading compiler would not have access
18134      to the non-GGC parts of mips16_globals (either from the PCH file,
18135      or from a copy that the loading compiler generated itself) and would
18136      have to call target_reinit anyway.
18137
18138      It therefore seems best to switch back to non-MIPS16 mode at
18139      save time, and to ensure that mips16_globals remains null after
18140      a PCH load.  */
18141   mips_set_compression_mode (0);
18142   mips16_globals = 0;
18143 }
18144 \f
18145 /* Generate or test for an insn that supports a constant permutation.  */
18146
18147 #define MAX_VECT_LEN 8
18148
18149 struct expand_vec_perm_d
18150 {
18151   rtx target, op0, op1;
18152   unsigned char perm[MAX_VECT_LEN];
18153   enum machine_mode vmode;
18154   unsigned char nelt;
18155   bool one_vector_p;
18156   bool testing_p;
18157 };
18158
18159 /* Construct (set target (vec_select op0 (parallel perm))) and
18160    return true if that's a valid instruction in the active ISA.  */
18161
18162 static bool
18163 mips_expand_vselect (rtx target, rtx op0,
18164                      const unsigned char *perm, unsigned nelt)
18165 {
18166   rtx rperm[MAX_VECT_LEN], x;
18167   rtx_insn *insn;
18168   unsigned i;
18169
18170   for (i = 0; i < nelt; ++i)
18171     rperm[i] = GEN_INT (perm[i]);
18172
18173   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
18174   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
18175   x = gen_rtx_SET (VOIDmode, target, x);
18176
18177   insn = emit_insn (x);
18178   if (recog_memoized (insn) < 0)
18179     {
18180       remove_insn (insn);
18181       return false;
18182     }
18183   return true;
18184 }
18185
18186 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
18187
18188 static bool
18189 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
18190                              const unsigned char *perm, unsigned nelt)
18191 {
18192   enum machine_mode v2mode;
18193   rtx x;
18194
18195   v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
18196   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
18197   return mips_expand_vselect (target, x, perm, nelt);
18198 }
18199
18200 /* Recognize patterns for even-odd extraction.  */
18201
18202 static bool
18203 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
18204 {
18205   unsigned i, odd, nelt = d->nelt;
18206   rtx t0, t1, t2, t3;
18207
18208   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18209     return false;
18210   /* Even-odd for V2SI/V2SFmode is matched by interleave directly.  */
18211   if (nelt < 4)
18212     return false;
18213
18214   odd = d->perm[0];
18215   if (odd > 1)
18216     return false;
18217   for (i = 1; i < nelt; ++i)
18218     if (d->perm[i] != i * 2 + odd)
18219       return false;
18220
18221   if (d->testing_p)
18222     return true;
18223
18224   /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
18225   t0 = gen_reg_rtx (d->vmode);
18226   t1 = gen_reg_rtx (d->vmode);
18227   switch (d->vmode)
18228     {
18229     case V4HImode:
18230       emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
18231       emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
18232       if (odd)
18233         emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
18234       else
18235         emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
18236       break;
18237
18238     case V8QImode:
18239       t2 = gen_reg_rtx (d->vmode);
18240       t3 = gen_reg_rtx (d->vmode);
18241       emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
18242       emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
18243       emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
18244       emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
18245       if (odd)
18246         emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
18247       else
18248         emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
18249       break;
18250
18251     default:
18252       gcc_unreachable ();
18253     }
18254   return true;
18255 }
18256
18257 /* Recognize patterns for the Loongson PSHUFH instruction.  */
18258
18259 static bool
18260 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
18261 {
18262   unsigned i, mask;
18263   rtx rmask;
18264
18265   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18266     return false;
18267   if (d->vmode != V4HImode)
18268     return false;
18269   if (d->testing_p)
18270     return true;
18271
18272   /* Convert the selector into the packed 8-bit form for pshufh.  */
18273   /* Recall that loongson is little-endian only.  No big-endian
18274      adjustment required.  */
18275   for (i = mask = 0; i < 4; i++)
18276     mask |= (d->perm[i] & 3) << (i * 2);
18277   rmask = force_reg (SImode, GEN_INT (mask));
18278
18279   if (d->one_vector_p)
18280     emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
18281   else
18282     {
18283       rtx t0, t1, x, merge, rmerge[4];
18284
18285       t0 = gen_reg_rtx (V4HImode);
18286       t1 = gen_reg_rtx (V4HImode);
18287       emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
18288       emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
18289
18290       for (i = 0; i < 4; ++i)
18291         rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
18292       merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
18293       merge = force_reg (V4HImode, merge);
18294
18295       x = gen_rtx_AND (V4HImode, merge, t1);
18296       emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18297
18298       x = gen_rtx_NOT (V4HImode, merge);
18299       x = gen_rtx_AND (V4HImode, x, t0);
18300       emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18301
18302       x = gen_rtx_IOR (V4HImode, t0, t1);
18303       emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
18304     }
18305
18306   return true;
18307 }
18308
18309 /* Recognize broadcast patterns for the Loongson.  */
18310
18311 static bool
18312 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
18313 {
18314   unsigned i, elt;
18315   rtx t0, t1;
18316
18317   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18318     return false;
18319   /* Note that we've already matched V2SI via punpck and V4HI via pshufh.  */
18320   if (d->vmode != V8QImode)
18321     return false;
18322   if (!d->one_vector_p)
18323     return false;
18324
18325   elt = d->perm[0];
18326   for (i = 1; i < 8; ++i)
18327     if (d->perm[i] != elt)
18328       return false;
18329
18330   if (d->testing_p)
18331     return true;
18332
18333   /* With one interleave we put two of the desired element adjacent.  */
18334   t0 = gen_reg_rtx (V8QImode);
18335   if (elt < 4)
18336     emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
18337   else
18338     emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
18339
18340   /* Shuffle that one HImode element into all locations.  */
18341   elt &= 3;
18342   elt *= 0x55;
18343   t1 = gen_reg_rtx (V4HImode);
18344   emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
18345                                   force_reg (SImode, GEN_INT (elt))));
18346
18347   emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
18348   return true;
18349 }
18350
18351 static bool
18352 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
18353 {
18354   unsigned int i, nelt = d->nelt;
18355   unsigned char perm2[MAX_VECT_LEN];
18356
18357   if (d->one_vector_p)
18358     {
18359       /* Try interleave with alternating operands.  */
18360       memcpy (perm2, d->perm, sizeof(perm2));
18361       for (i = 1; i < nelt; i += 2)
18362         perm2[i] += nelt;
18363       if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
18364         return true;
18365     }
18366   else
18367     {
18368       if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
18369                                        d->perm, nelt))
18370         return true;
18371
18372       /* Try again with swapped operands.  */
18373       for (i = 0; i < nelt; ++i)
18374         perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
18375       if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
18376         return true;
18377     }
18378
18379   if (mips_expand_vpc_loongson_even_odd (d))
18380     return true;
18381   if (mips_expand_vpc_loongson_pshufh (d))
18382     return true;
18383   if (mips_expand_vpc_loongson_bcast (d))
18384     return true;
18385   return false;
18386 }
18387
18388 /* Expand a vec_perm_const pattern.  */
18389
18390 bool
18391 mips_expand_vec_perm_const (rtx operands[4])
18392 {
18393   struct expand_vec_perm_d d;
18394   int i, nelt, which;
18395   unsigned char orig_perm[MAX_VECT_LEN];
18396   rtx sel;
18397   bool ok;
18398
18399   d.target = operands[0];
18400   d.op0 = operands[1];
18401   d.op1 = operands[2];
18402   sel = operands[3];
18403
18404   d.vmode = GET_MODE (d.target);
18405   gcc_assert (VECTOR_MODE_P (d.vmode));
18406   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18407   d.testing_p = false;
18408
18409   for (i = which = 0; i < nelt; ++i)
18410     {
18411       rtx e = XVECEXP (sel, 0, i);
18412       int ei = INTVAL (e) & (2 * nelt - 1);
18413       which |= (ei < nelt ? 1 : 2);
18414       orig_perm[i] = ei;
18415     }
18416   memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18417
18418   switch (which)
18419     {
18420     default:
18421       gcc_unreachable();
18422
18423     case 3:
18424       d.one_vector_p = false;
18425       if (!rtx_equal_p (d.op0, d.op1))
18426         break;
18427       /* FALLTHRU */
18428
18429     case 2:
18430       for (i = 0; i < nelt; ++i)
18431         d.perm[i] &= nelt - 1;
18432       d.op0 = d.op1;
18433       d.one_vector_p = true;
18434       break;
18435
18436     case 1:
18437       d.op1 = d.op0;
18438       d.one_vector_p = true;
18439       break;
18440     }
18441
18442   ok = mips_expand_vec_perm_const_1 (&d);
18443
18444   /* If we were given a two-vector permutation which just happened to
18445      have both input vectors equal, we folded this into a one-vector
18446      permutation.  There are several loongson patterns that are matched
18447      via direct vec_select+vec_concat expansion, but we do not have
18448      support in mips_expand_vec_perm_const_1 to guess the adjustment
18449      that should be made for a single operand.  Just try again with
18450      the original permutation.  */
18451   if (!ok && which == 3)
18452     {
18453       d.op0 = operands[1];
18454       d.op1 = operands[2];
18455       d.one_vector_p = false;
18456       memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18457       ok = mips_expand_vec_perm_const_1 (&d);
18458     }
18459
18460   return ok;
18461 }
18462
18463 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK.  */
18464
18465 static bool
18466 mips_vectorize_vec_perm_const_ok (enum machine_mode vmode,
18467                                   const unsigned char *sel)
18468 {
18469   struct expand_vec_perm_d d;
18470   unsigned int i, nelt, which;
18471   bool ret;
18472
18473   d.vmode = vmode;
18474   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18475   d.testing_p = true;
18476   memcpy (d.perm, sel, nelt);
18477
18478   /* Categorize the set of elements in the selector.  */
18479   for (i = which = 0; i < nelt; ++i)
18480     {
18481       unsigned char e = d.perm[i];
18482       gcc_assert (e < 2 * nelt);
18483       which |= (e < nelt ? 1 : 2);
18484     }
18485
18486   /* For all elements from second vector, fold the elements to first.  */
18487   if (which == 2)
18488     for (i = 0; i < nelt; ++i)
18489       d.perm[i] -= nelt;
18490
18491   /* Check whether the mask can be applied to the vector type.  */
18492   d.one_vector_p = (which != 3);
18493
18494   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
18495   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
18496   if (!d.one_vector_p)
18497     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
18498
18499   start_sequence ();
18500   ret = mips_expand_vec_perm_const_1 (&d);
18501   end_sequence ();
18502
18503   return ret;
18504 }
18505
18506 /* Expand an integral vector unpack operation.  */
18507
18508 void
18509 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18510 {
18511   enum machine_mode imode = GET_MODE (operands[1]);
18512   rtx (*unpack) (rtx, rtx, rtx);
18513   rtx (*cmpgt) (rtx, rtx, rtx);
18514   rtx tmp, dest, zero;
18515
18516   switch (imode)
18517     {
18518     case V8QImode:
18519       if (high_p)
18520         unpack = gen_loongson_punpckhbh;
18521       else
18522         unpack = gen_loongson_punpcklbh;
18523       cmpgt = gen_loongson_pcmpgtb;
18524       break;
18525     case V4HImode:
18526       if (high_p)
18527         unpack = gen_loongson_punpckhhw;
18528       else
18529         unpack = gen_loongson_punpcklhw;
18530       cmpgt = gen_loongson_pcmpgth;
18531       break;
18532     default:
18533       gcc_unreachable ();
18534     }
18535
18536   zero = force_reg (imode, CONST0_RTX (imode));
18537   if (unsigned_p)
18538     tmp = zero;
18539   else
18540     {
18541       tmp = gen_reg_rtx (imode);
18542       emit_insn (cmpgt (tmp, zero, operands[1]));
18543     }
18544
18545   dest = gen_reg_rtx (imode);
18546   emit_insn (unpack (dest, operands[1], tmp));
18547
18548   emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
18549 }
18550
18551 /* A subroutine of mips_expand_vec_init, match constant vector elements.  */
18552
18553 static inline bool
18554 mips_constant_elt_p (rtx x)
18555 {
18556   return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
18557 }
18558
18559 /* A subroutine of mips_expand_vec_init, expand via broadcast.  */
18560
18561 static void
18562 mips_expand_vi_broadcast (enum machine_mode vmode, rtx target, rtx elt)
18563 {
18564   struct expand_vec_perm_d d;
18565   rtx t1;
18566   bool ok;
18567
18568   if (elt != const0_rtx)
18569     elt = force_reg (GET_MODE_INNER (vmode), elt);
18570   if (REG_P (elt))
18571     elt = gen_lowpart (DImode, elt);
18572
18573   t1 = gen_reg_rtx (vmode);
18574   switch (vmode)
18575     {
18576     case V8QImode:
18577       emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
18578       break;
18579     case V4HImode:
18580       emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
18581       break;
18582     default:
18583       gcc_unreachable ();
18584     }
18585
18586   memset (&d, 0, sizeof (d));
18587   d.target = target;
18588   d.op0 = t1;
18589   d.op1 = t1;
18590   d.vmode = vmode;
18591   d.nelt = GET_MODE_NUNITS (vmode);
18592   d.one_vector_p = true;
18593
18594   ok = mips_expand_vec_perm_const_1 (&d);
18595   gcc_assert (ok);
18596 }
18597
18598 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
18599    elements of VALS with zeros, copy the constant vector to TARGET.  */
18600
18601 static void
18602 mips_expand_vi_constant (enum machine_mode vmode, unsigned nelt,
18603                          rtx target, rtx vals)
18604 {
18605   rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
18606   unsigned i;
18607
18608   for (i = 0; i < nelt; ++i)
18609     {
18610       if (!mips_constant_elt_p (RTVEC_ELT (vec, i)))
18611         RTVEC_ELT (vec, i) = const0_rtx;
18612     }
18613
18614   emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
18615 }
18616
18617
18618 /* A subroutine of mips_expand_vec_init, expand via pinsrh.  */
18619
18620 static void
18621 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
18622 {
18623   mips_expand_vi_constant (V4HImode, 4, target, vals);
18624
18625   emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
18626                               GEN_INT (one_var)));
18627 }
18628
18629 /* A subroutine of mips_expand_vec_init, expand anything via memory.  */
18630
18631 static void
18632 mips_expand_vi_general (enum machine_mode vmode, enum machine_mode imode,
18633                         unsigned nelt, unsigned nvar, rtx target, rtx vals)
18634 {
18635   rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
18636   unsigned int i, isize = GET_MODE_SIZE (imode);
18637
18638   if (nvar < nelt)
18639     mips_expand_vi_constant (vmode, nelt, mem, vals);
18640
18641   for (i = 0; i < nelt; ++i)
18642     {
18643       rtx x = XVECEXP (vals, 0, i);
18644       if (!mips_constant_elt_p (x))
18645         emit_move_insn (adjust_address (mem, imode, i * isize), x);
18646     }
18647
18648   emit_move_insn (target, mem);
18649 }
18650
18651 /* Expand a vector initialization.  */
18652
18653 void
18654 mips_expand_vector_init (rtx target, rtx vals)
18655 {
18656   enum machine_mode vmode = GET_MODE (target);
18657   enum machine_mode imode = GET_MODE_INNER (vmode);
18658   unsigned i, nelt = GET_MODE_NUNITS (vmode);
18659   unsigned nvar = 0, one_var = -1u;
18660   bool all_same = true;
18661   rtx x;
18662
18663   for (i = 0; i < nelt; ++i)
18664     {
18665       x = XVECEXP (vals, 0, i);
18666       if (!mips_constant_elt_p (x))
18667         nvar++, one_var = i;
18668       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
18669         all_same = false;
18670     }
18671
18672   /* Load constants from the pool, or whatever's handy.  */
18673   if (nvar == 0)
18674     {
18675       emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
18676       return;
18677     }
18678
18679   /* For two-part initialization, always use CONCAT.  */
18680   if (nelt == 2)
18681     {
18682       rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
18683       rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
18684       x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
18685       emit_insn (gen_rtx_SET (VOIDmode, target, x));
18686       return;
18687     }
18688
18689   /* Loongson is the only cpu with vectors with more elements.  */
18690   gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
18691
18692   /* If all values are identical, broadcast the value.  */
18693   if (all_same)
18694     {
18695       mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
18696       return;
18697     }
18698
18699   /* If we've only got one non-variable V4HImode, use PINSRH.  */
18700   if (nvar == 1 && vmode == V4HImode)
18701     {
18702       mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
18703       return;
18704     }
18705
18706   mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
18707 }
18708
18709 /* Expand a vector reduction.  */
18710
18711 void
18712 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
18713 {
18714   enum machine_mode vmode = GET_MODE (in);
18715   unsigned char perm2[2];
18716   rtx last, next, fold, x;
18717   bool ok;
18718
18719   last = in;
18720   fold = gen_reg_rtx (vmode);
18721   switch (vmode)
18722     {
18723     case V2SFmode:
18724       /* Use PUL/PLU to produce { L, H } op { H, L }.
18725          By reversing the pair order, rather than a pure interleave high,
18726          we avoid erroneous exceptional conditions that we might otherwise
18727          produce from the computation of H op H.  */
18728       perm2[0] = 1;
18729       perm2[1] = 2;
18730       ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
18731       gcc_assert (ok);
18732       break;
18733
18734     case V2SImode:
18735       /* Use interleave to produce { H, L } op { H, H }.  */
18736       emit_insn (gen_loongson_punpckhwd (fold, last, last));
18737       break;
18738
18739     case V4HImode:
18740       /* Perform the first reduction with interleave,
18741          and subsequent reductions with shifts.  */
18742       emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
18743
18744       next = gen_reg_rtx (vmode);
18745       emit_insn (gen (next, last, fold));
18746       last = next;
18747
18748       fold = gen_reg_rtx (vmode);
18749       x = force_reg (SImode, GEN_INT (16));
18750       emit_insn (gen_vec_shr_v4hi (fold, last, x));
18751       break;
18752
18753     case V8QImode:
18754       emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
18755
18756       next = gen_reg_rtx (vmode);
18757       emit_insn (gen (next, last, fold));
18758       last = next;
18759
18760       fold = gen_reg_rtx (vmode);
18761       x = force_reg (SImode, GEN_INT (16));
18762       emit_insn (gen_vec_shr_v8qi (fold, last, x));
18763
18764       next = gen_reg_rtx (vmode);
18765       emit_insn (gen (next, last, fold));
18766       last = next;
18767
18768       fold = gen_reg_rtx (vmode);
18769       x = force_reg (SImode, GEN_INT (8));
18770       emit_insn (gen_vec_shr_v8qi (fold, last, x));
18771       break;
18772
18773     default:
18774       gcc_unreachable ();
18775     }
18776
18777   emit_insn (gen (target, last, fold));
18778 }
18779
18780 /* Expand a vector minimum/maximum.  */
18781
18782 void
18783 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
18784                         rtx (*cmp) (rtx, rtx, rtx), bool min_p)
18785 {
18786   enum machine_mode vmode = GET_MODE (target);
18787   rtx tc, t0, t1, x;
18788
18789   tc = gen_reg_rtx (vmode);
18790   t0 = gen_reg_rtx (vmode);
18791   t1 = gen_reg_rtx (vmode);
18792
18793   /* op0 > op1 */
18794   emit_insn (cmp (tc, op0, op1));
18795
18796   x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
18797   emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18798
18799   x = gen_rtx_NOT (vmode, tc);
18800   x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
18801   emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18802
18803   x = gen_rtx_IOR (vmode, t0, t1);
18804   emit_insn (gen_rtx_SET (VOIDmode, target, x));
18805 }
18806
18807 /* Implement TARGET_CASE_VALUES_THRESHOLD.  */
18808
18809 unsigned int
18810 mips_case_values_threshold (void)
18811 {
18812   /* In MIPS16 mode using a larger case threshold generates smaller code.  */
18813   if (TARGET_MIPS16 && optimize_size)
18814     return 10;
18815   else
18816     return default_case_values_threshold ();
18817 }
18818
18819 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV.  */
18820
18821 static void
18822 mips_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
18823 {
18824   if (!TARGET_HARD_FLOAT_ABI)
18825     return;
18826   tree exceptions_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18827   tree fcsr_orig_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18828   tree fcsr_mod_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18829   tree get_fcsr = mips_builtin_decls[MIPS_GET_FCSR];
18830   tree set_fcsr = mips_builtin_decls[MIPS_SET_FCSR];
18831   tree get_fcsr_hold_call = build_call_expr (get_fcsr, 0);
18832   tree hold_assign_orig = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18833                                   fcsr_orig_var, get_fcsr_hold_call);
18834   tree hold_mod_val = build2 (BIT_AND_EXPR, MIPS_ATYPE_USI, fcsr_orig_var,
18835                               build_int_cst (MIPS_ATYPE_USI, 0xfffff003));
18836   tree hold_assign_mod = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18837                                  fcsr_mod_var, hold_mod_val);
18838   tree set_fcsr_hold_call = build_call_expr (set_fcsr, 1, fcsr_mod_var);
18839   tree hold_all = build2 (COMPOUND_EXPR, MIPS_ATYPE_USI,
18840                           hold_assign_orig, hold_assign_mod);
18841   *hold = build2 (COMPOUND_EXPR, void_type_node, hold_all,
18842                   set_fcsr_hold_call);
18843
18844   *clear = build_call_expr (set_fcsr, 1, fcsr_mod_var);
18845
18846   tree get_fcsr_update_call = build_call_expr (get_fcsr, 0);
18847   *update = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18848                     exceptions_var, get_fcsr_update_call);
18849   tree set_fcsr_update_call = build_call_expr (set_fcsr, 1, fcsr_orig_var);
18850   *update = build2 (COMPOUND_EXPR, void_type_node, *update,
18851                     set_fcsr_update_call);
18852   tree atomic_feraiseexcept
18853     = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
18854   tree int_exceptions_var = fold_convert (integer_type_node,
18855                                           exceptions_var);
18856   tree atomic_feraiseexcept_call = build_call_expr (atomic_feraiseexcept,
18857                                                     1, int_exceptions_var);
18858   *update = build2 (COMPOUND_EXPR, void_type_node, *update,
18859                     atomic_feraiseexcept_call);
18860 }
18861
18862 /* Implement TARGET_SPILL_CLASS.  */
18863
18864 static reg_class_t
18865 mips_spill_class (reg_class_t rclass ATTRIBUTE_UNUSED,
18866                   enum machine_mode mode ATTRIBUTE_UNUSED)
18867 {
18868   if (TARGET_MIPS16)
18869     return SPILL_REGS;
18870   return NO_REGS;
18871 }
18872
18873 /* Implement TARGET_LRA_P.  */
18874
18875 static bool
18876 mips_lra_p (void)
18877 {
18878   return mips_lra_flag;
18879 }
18880 \f
18881 /* Initialize the GCC target structure.  */
18882 #undef TARGET_ASM_ALIGNED_HI_OP
18883 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
18884 #undef TARGET_ASM_ALIGNED_SI_OP
18885 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
18886 #undef TARGET_ASM_ALIGNED_DI_OP
18887 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
18888
18889 #undef TARGET_OPTION_OVERRIDE
18890 #define TARGET_OPTION_OVERRIDE mips_option_override
18891
18892 #undef TARGET_LEGITIMIZE_ADDRESS
18893 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
18894
18895 #undef TARGET_ASM_FUNCTION_PROLOGUE
18896 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
18897 #undef TARGET_ASM_FUNCTION_EPILOGUE
18898 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
18899 #undef TARGET_ASM_SELECT_RTX_SECTION
18900 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
18901 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
18902 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
18903
18904 #undef TARGET_SCHED_INIT
18905 #define TARGET_SCHED_INIT mips_sched_init
18906 #undef TARGET_SCHED_REORDER
18907 #define TARGET_SCHED_REORDER mips_sched_reorder
18908 #undef TARGET_SCHED_REORDER2
18909 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
18910 #undef TARGET_SCHED_VARIABLE_ISSUE
18911 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
18912 #undef TARGET_SCHED_ADJUST_COST
18913 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
18914 #undef TARGET_SCHED_ISSUE_RATE
18915 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
18916 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
18917 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
18918 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
18919 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
18920 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
18921 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
18922   mips_multipass_dfa_lookahead
18923 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
18924 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
18925   mips_small_register_classes_for_mode_p
18926
18927 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
18928 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
18929
18930 #undef TARGET_INSERT_ATTRIBUTES
18931 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
18932 #undef TARGET_MERGE_DECL_ATTRIBUTES
18933 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
18934 #undef TARGET_CAN_INLINE_P
18935 #define TARGET_CAN_INLINE_P mips_can_inline_p
18936 #undef TARGET_SET_CURRENT_FUNCTION
18937 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
18938
18939 #undef TARGET_VALID_POINTER_MODE
18940 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
18941 #undef TARGET_REGISTER_MOVE_COST
18942 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
18943 #undef TARGET_REGISTER_PRIORITY
18944 #define TARGET_REGISTER_PRIORITY mips_register_priority
18945 #undef TARGET_MEMORY_MOVE_COST
18946 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
18947 #undef TARGET_RTX_COSTS
18948 #define TARGET_RTX_COSTS mips_rtx_costs
18949 #undef TARGET_ADDRESS_COST
18950 #define TARGET_ADDRESS_COST mips_address_cost
18951
18952 #undef TARGET_IN_SMALL_DATA_P
18953 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
18954
18955 #undef TARGET_MACHINE_DEPENDENT_REORG
18956 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
18957
18958 #undef  TARGET_PREFERRED_RELOAD_CLASS
18959 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
18960
18961 #undef TARGET_EXPAND_TO_RTL_HOOK
18962 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
18963 #undef TARGET_ASM_FILE_START
18964 #define TARGET_ASM_FILE_START mips_file_start
18965 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
18966 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
18967 #undef TARGET_ASM_CODE_END
18968 #define TARGET_ASM_CODE_END mips_code_end
18969
18970 #undef TARGET_INIT_LIBFUNCS
18971 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
18972
18973 #undef TARGET_BUILD_BUILTIN_VA_LIST
18974 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
18975 #undef TARGET_EXPAND_BUILTIN_VA_START
18976 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
18977 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
18978 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
18979
18980 #undef  TARGET_PROMOTE_FUNCTION_MODE
18981 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
18982 #undef TARGET_PROMOTE_PROTOTYPES
18983 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
18984
18985 #undef TARGET_FUNCTION_VALUE
18986 #define TARGET_FUNCTION_VALUE mips_function_value
18987 #undef TARGET_LIBCALL_VALUE
18988 #define TARGET_LIBCALL_VALUE mips_libcall_value
18989 #undef TARGET_FUNCTION_VALUE_REGNO_P
18990 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
18991 #undef TARGET_RETURN_IN_MEMORY
18992 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
18993 #undef TARGET_RETURN_IN_MSB
18994 #define TARGET_RETURN_IN_MSB mips_return_in_msb
18995
18996 #undef TARGET_ASM_OUTPUT_MI_THUNK
18997 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
18998 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
18999 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
19000
19001 #undef TARGET_PRINT_OPERAND
19002 #define TARGET_PRINT_OPERAND mips_print_operand
19003 #undef TARGET_PRINT_OPERAND_ADDRESS
19004 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
19005 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
19006 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
19007
19008 #undef TARGET_SETUP_INCOMING_VARARGS
19009 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
19010 #undef TARGET_STRICT_ARGUMENT_NAMING
19011 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
19012 #undef TARGET_MUST_PASS_IN_STACK
19013 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
19014 #undef TARGET_PASS_BY_REFERENCE
19015 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
19016 #undef TARGET_CALLEE_COPIES
19017 #define TARGET_CALLEE_COPIES mips_callee_copies
19018 #undef TARGET_ARG_PARTIAL_BYTES
19019 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
19020 #undef TARGET_FUNCTION_ARG
19021 #define TARGET_FUNCTION_ARG mips_function_arg
19022 #undef TARGET_FUNCTION_ARG_ADVANCE
19023 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
19024 #undef TARGET_FUNCTION_ARG_BOUNDARY
19025 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
19026
19027 #undef TARGET_MODE_REP_EXTENDED
19028 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
19029
19030 #undef TARGET_VECTOR_MODE_SUPPORTED_P
19031 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
19032
19033 #undef TARGET_SCALAR_MODE_SUPPORTED_P
19034 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
19035
19036 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
19037 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
19038
19039 #undef TARGET_INIT_BUILTINS
19040 #define TARGET_INIT_BUILTINS mips_init_builtins
19041 #undef TARGET_BUILTIN_DECL
19042 #define TARGET_BUILTIN_DECL mips_builtin_decl
19043 #undef TARGET_EXPAND_BUILTIN
19044 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
19045
19046 #undef TARGET_HAVE_TLS
19047 #define TARGET_HAVE_TLS HAVE_AS_TLS
19048
19049 #undef TARGET_CANNOT_FORCE_CONST_MEM
19050 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
19051
19052 #undef TARGET_LEGITIMATE_CONSTANT_P
19053 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
19054
19055 #undef TARGET_ENCODE_SECTION_INFO
19056 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
19057
19058 #undef TARGET_ATTRIBUTE_TABLE
19059 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
19060 /* All our function attributes are related to how out-of-line copies should
19061    be compiled or called.  They don't in themselves prevent inlining.  */
19062 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
19063 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
19064
19065 #undef TARGET_EXTRA_LIVE_ON_ENTRY
19066 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
19067
19068 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
19069 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
19070 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
19071 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
19072
19073 #undef  TARGET_COMP_TYPE_ATTRIBUTES
19074 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
19075
19076 #ifdef HAVE_AS_DTPRELWORD
19077 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
19078 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
19079 #endif
19080 #undef TARGET_DWARF_REGISTER_SPAN
19081 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
19082
19083 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
19084 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
19085
19086 #undef TARGET_LEGITIMATE_ADDRESS_P
19087 #define TARGET_LEGITIMATE_ADDRESS_P     mips_legitimate_address_p
19088
19089 #undef TARGET_FRAME_POINTER_REQUIRED
19090 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
19091
19092 #undef TARGET_CAN_ELIMINATE
19093 #define TARGET_CAN_ELIMINATE mips_can_eliminate
19094
19095 #undef TARGET_CONDITIONAL_REGISTER_USAGE
19096 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
19097
19098 #undef TARGET_TRAMPOLINE_INIT
19099 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
19100
19101 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
19102 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
19103
19104 #undef TARGET_SHIFT_TRUNCATION_MASK
19105 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
19106
19107 #undef TARGET_PREPARE_PCH_SAVE
19108 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
19109
19110 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
19111 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK mips_vectorize_vec_perm_const_ok
19112
19113 #undef TARGET_CASE_VALUES_THRESHOLD
19114 #define TARGET_CASE_VALUES_THRESHOLD mips_case_values_threshold
19115
19116 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
19117 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV mips_atomic_assign_expand_fenv
19118
19119 #undef TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS
19120 #define TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS true
19121
19122 #undef TARGET_SPILL_CLASS
19123 #define TARGET_SPILL_CLASS mips_spill_class
19124 #undef TARGET_LRA_P
19125 #define TARGET_LRA_P mips_lra_p
19126
19127 struct gcc_target targetm = TARGET_INITIALIZER;
19128 \f
19129 #include "gt-mips.h"