xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/config/mips/mips.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /* Subroutines used for MIPS code generation.
2    Copyright (C) 1989-2013 Free Software Foundation, Inc.
3    Contributed by A. Lichnewsky, lich@inria.inria.fr.
4    Changes by Michael Meissner, meissner@osf.org.
5    64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
6    Brendan Eich, brendan@microunity.com.
7 
8 This file is part of GCC.
9 
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14 
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23 
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-attr.h"
34 #include "recog.h"
35 #include "output.h"
36 #include "tree.h"
37 #include "function.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "libfuncs.h"
41 #include "flags.h"
42 #include "reload.h"
43 #include "tm_p.h"
44 #include "ggc.h"
45 #include "gstab.h"
46 #include "hashtab.h"
47 #include "debug.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "common/common-target.h"
51 #include "langhooks.h"
52 #include "sched-int.h"
53 #include "gimple.h"
54 #include "bitmap.h"
55 #include "diagnostic.h"
56 #include "target-globals.h"
57 #include "opts.h"
58 
59 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF.  */
60 #define UNSPEC_ADDRESS_P(X)					\
61   (GET_CODE (X) == UNSPEC					\
62    && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST			\
63    && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
64 
65 /* Extract the symbol or label from UNSPEC wrapper X.  */
66 #define UNSPEC_ADDRESS(X) \
67   XVECEXP (X, 0, 0)
68 
69 /* Extract the symbol type from UNSPEC wrapper X.  */
70 #define UNSPEC_ADDRESS_TYPE(X) \
71   ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
72 
73 /* The maximum distance between the top of the stack frame and the
74    value $sp has when we save and restore registers.
75 
76    The value for normal-mode code must be a SMALL_OPERAND and must
77    preserve the maximum stack alignment.  We therefore use a value
78    of 0x7ff0 in this case.
79 
80    MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
81    up to 0x7f8 bytes and can usually save or restore all the registers
82    that we need to save or restore.  (Note that we can only use these
83    instructions for o32, for which the stack alignment is 8 bytes.)
84 
85    We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
86    RESTORE are not available.  We can then use unextended instructions
87    to save and restore registers, and to allocate and deallocate the top
88    part of the frame.  */
89 #define MIPS_MAX_FIRST_STACK_STEP					\
90   (!TARGET_MIPS16 ? 0x7ff0						\
91    : GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8				\
92    : TARGET_64BIT ? 0x100 : 0x400)
93 
94 /* True if INSN is a mips.md pattern or asm statement.  */
95 #define USEFUL_INSN_P(INSN)						\
96   (NONDEBUG_INSN_P (INSN)						\
97    && GET_CODE (PATTERN (INSN)) != USE					\
98    && GET_CODE (PATTERN (INSN)) != CLOBBER				\
99    && GET_CODE (PATTERN (INSN)) != ADDR_VEC				\
100    && GET_CODE (PATTERN (INSN)) != ADDR_DIFF_VEC)
101 
102 /* If INSN is a delayed branch sequence, return the first instruction
103    in the sequence, otherwise return INSN itself.  */
104 #define SEQ_BEGIN(INSN)							\
105   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE		\
106    ? XVECEXP (PATTERN (INSN), 0, 0)					\
107    : (INSN))
108 
109 /* Likewise for the last instruction in a delayed branch sequence.  */
110 #define SEQ_END(INSN)							\
111   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE		\
112    ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1)	\
113    : (INSN))
114 
115 /* Execute the following loop body with SUBINSN set to each instruction
116    between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive.  */
117 #define FOR_EACH_SUBINSN(SUBINSN, INSN)					\
118   for ((SUBINSN) = SEQ_BEGIN (INSN);					\
119        (SUBINSN) != NEXT_INSN (SEQ_END (INSN));				\
120        (SUBINSN) = NEXT_INSN (SUBINSN))
121 
122 /* True if bit BIT is set in VALUE.  */
123 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
124 
125 /* Return the opcode for a ptr_mode load of the form:
126 
127        l[wd]    DEST, OFFSET(BASE).  */
128 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE)	\
129   (((ptr_mode == DImode ? 0x37 : 0x23) << 26)	\
130    | ((BASE) << 21)				\
131    | ((DEST) << 16)				\
132    | (OFFSET))
133 
134 /* Return the opcode to move register SRC into register DEST.  */
135 #define MIPS_MOVE(DEST, SRC)		\
136   ((TARGET_64BIT ? 0x2d : 0x21)		\
137    | ((DEST) << 11)			\
138    | ((SRC) << 21))
139 
140 /* Return the opcode for:
141 
142        lui      DEST, VALUE.  */
143 #define MIPS_LUI(DEST, VALUE) \
144   ((0xf << 26) | ((DEST) << 16) | (VALUE))
145 
146 /* Return the opcode to jump to register DEST.  */
147 #define MIPS_JR(DEST) \
148   (((DEST) << 21) | 0x8)
149 
150 /* Return the opcode for:
151 
152        bal     . + (1 + OFFSET) * 4.  */
153 #define MIPS_BAL(OFFSET) \
154   ((0x1 << 26) | (0x11 << 16) | (OFFSET))
155 
156 /* Return the usual opcode for a nop.  */
157 #define MIPS_NOP 0
158 
159 /* Classifies an address.
160 
161    ADDRESS_REG
162        A natural register + offset address.  The register satisfies
163        mips_valid_base_register_p and the offset is a const_arith_operand.
164 
165    ADDRESS_LO_SUM
166        A LO_SUM rtx.  The first operand is a valid base register and
167        the second operand is a symbolic address.
168 
169    ADDRESS_CONST_INT
170        A signed 16-bit constant address.
171 
172    ADDRESS_SYMBOLIC:
173        A constant symbolic address.  */
174 enum mips_address_type {
175   ADDRESS_REG,
176   ADDRESS_LO_SUM,
177   ADDRESS_CONST_INT,
178   ADDRESS_SYMBOLIC
179 };
180 
181 /* Macros to create an enumeration identifier for a function prototype.  */
182 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
183 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
184 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
185 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
186 
187 /* Classifies the prototype of a built-in function.  */
188 enum mips_function_type {
189 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
190 #include "config/mips/mips-ftypes.def"
191 #undef DEF_MIPS_FTYPE
192   MIPS_MAX_FTYPE_MAX
193 };
194 
195 /* Specifies how a built-in function should be converted into rtl.  */
196 enum mips_builtin_type {
197   /* The function corresponds directly to an .md pattern.  The return
198      value is mapped to operand 0 and the arguments are mapped to
199      operands 1 and above.  */
200   MIPS_BUILTIN_DIRECT,
201 
202   /* The function corresponds directly to an .md pattern.  There is no return
203      value and the arguments are mapped to operands 0 and above.  */
204   MIPS_BUILTIN_DIRECT_NO_TARGET,
205 
206   /* The function corresponds to a comparison instruction followed by
207      a mips_cond_move_tf_ps pattern.  The first two arguments are the
208      values to compare and the second two arguments are the vector
209      operands for the movt.ps or movf.ps instruction (in assembly order).  */
210   MIPS_BUILTIN_MOVF,
211   MIPS_BUILTIN_MOVT,
212 
213   /* The function corresponds to a V2SF comparison instruction.  Operand 0
214      of this instruction is the result of the comparison, which has mode
215      CCV2 or CCV4.  The function arguments are mapped to operands 1 and
216      above.  The function's return value is an SImode boolean that is
217      true under the following conditions:
218 
219      MIPS_BUILTIN_CMP_ANY: one of the registers is true
220      MIPS_BUILTIN_CMP_ALL: all of the registers are true
221      MIPS_BUILTIN_CMP_LOWER: the first register is true
222      MIPS_BUILTIN_CMP_UPPER: the second register is true.  */
223   MIPS_BUILTIN_CMP_ANY,
224   MIPS_BUILTIN_CMP_ALL,
225   MIPS_BUILTIN_CMP_UPPER,
226   MIPS_BUILTIN_CMP_LOWER,
227 
228   /* As above, but the instruction only sets a single $fcc register.  */
229   MIPS_BUILTIN_CMP_SINGLE,
230 
231   /* For generating bposge32 branch instructions in MIPS32 DSP ASE.  */
232   MIPS_BUILTIN_BPOSGE32
233 };
234 
235 /* Invoke MACRO (COND) for each C.cond.fmt condition.  */
236 #define MIPS_FP_CONDITIONS(MACRO) \
237   MACRO (f),	\
238   MACRO (un),	\
239   MACRO (eq),	\
240   MACRO (ueq),	\
241   MACRO (olt),	\
242   MACRO (ult),	\
243   MACRO (ole),	\
244   MACRO (ule),	\
245   MACRO (sf),	\
246   MACRO (ngle),	\
247   MACRO (seq),	\
248   MACRO (ngl),	\
249   MACRO (lt),	\
250   MACRO (nge),	\
251   MACRO (le),	\
252   MACRO (ngt)
253 
254 /* Enumerates the codes above as MIPS_FP_COND_<X>.  */
255 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
256 enum mips_fp_condition {
257   MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
258 };
259 
260 /* Index X provides the string representation of MIPS_FP_COND_<X>.  */
261 #define STRINGIFY(X) #X
262 static const char *const mips_fp_conditions[] = {
263   MIPS_FP_CONDITIONS (STRINGIFY)
264 };
265 
266 /* Tuning information that is automatically derived from other sources
267    (such as the scheduler).  */
268 static struct {
269   /* The architecture and tuning settings that this structure describes.  */
270   enum processor arch;
271   enum processor tune;
272 
273   /* True if this structure describes MIPS16 settings.  */
274   bool mips16_p;
275 
276   /* True if the structure has been initialized.  */
277   bool initialized_p;
278 
279   /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
280      when optimizing for speed.  */
281   bool fast_mult_zero_zero_p;
282 } mips_tuning_info;
283 
284 /* Information about a function's frame layout.  */
285 struct GTY(())  mips_frame_info {
286   /* The size of the frame in bytes.  */
287   HOST_WIDE_INT total_size;
288 
289   /* The number of bytes allocated to variables.  */
290   HOST_WIDE_INT var_size;
291 
292   /* The number of bytes allocated to outgoing function arguments.  */
293   HOST_WIDE_INT args_size;
294 
295   /* The number of bytes allocated to the .cprestore slot, or 0 if there
296      is no such slot.  */
297   HOST_WIDE_INT cprestore_size;
298 
299   /* Bit X is set if the function saves or restores GPR X.  */
300   unsigned int mask;
301 
302   /* Likewise FPR X.  */
303   unsigned int fmask;
304 
305   /* Likewise doubleword accumulator X ($acX).  */
306   unsigned int acc_mask;
307 
308   /* The number of GPRs, FPRs, doubleword accumulators and COP0
309      registers saved.  */
310   unsigned int num_gp;
311   unsigned int num_fp;
312   unsigned int num_acc;
313   unsigned int num_cop0_regs;
314 
315   /* The offset of the topmost GPR, FPR, accumulator and COP0-register
316      save slots from the top of the frame, or zero if no such slots are
317      needed.  */
318   HOST_WIDE_INT gp_save_offset;
319   HOST_WIDE_INT fp_save_offset;
320   HOST_WIDE_INT acc_save_offset;
321   HOST_WIDE_INT cop0_save_offset;
322 
323   /* Likewise, but giving offsets from the bottom of the frame.  */
324   HOST_WIDE_INT gp_sp_offset;
325   HOST_WIDE_INT fp_sp_offset;
326   HOST_WIDE_INT acc_sp_offset;
327   HOST_WIDE_INT cop0_sp_offset;
328 
329   /* Similar, but the value passed to _mcount.  */
330   HOST_WIDE_INT ra_fp_offset;
331 
332   /* The offset of arg_pointer_rtx from the bottom of the frame.  */
333   HOST_WIDE_INT arg_pointer_offset;
334 
335   /* The offset of hard_frame_pointer_rtx from the bottom of the frame.  */
336   HOST_WIDE_INT hard_frame_pointer_offset;
337 };
338 
339 struct GTY(())  machine_function {
340   /* The next floating-point condition-code register to allocate
341      for ISA_HAS_8CC targets, relative to ST_REG_FIRST.  */
342   unsigned int next_fcc;
343 
344   /* The register returned by mips16_gp_pseudo_reg; see there for details.  */
345   rtx mips16_gp_pseudo_rtx;
346 
347   /* The number of extra stack bytes taken up by register varargs.
348      This area is allocated by the callee at the very top of the frame.  */
349   int varargs_size;
350 
351   /* The current frame information, calculated by mips_compute_frame_info.  */
352   struct mips_frame_info frame;
353 
354   /* The register to use as the function's global pointer, or INVALID_REGNUM
355      if the function doesn't need one.  */
356   unsigned int global_pointer;
357 
358   /* How many instructions it takes to load a label into $AT, or 0 if
359      this property hasn't yet been calculated.  */
360   unsigned int load_label_num_insns;
361 
362   /* True if mips_adjust_insn_length should ignore an instruction's
363      hazard attribute.  */
364   bool ignore_hazard_length_p;
365 
366   /* True if the whole function is suitable for .set noreorder and
367      .set nomacro.  */
368   bool all_noreorder_p;
369 
370   /* True if the function has "inflexible" and "flexible" references
371      to the global pointer.  See mips_cfun_has_inflexible_gp_ref_p
372      and mips_cfun_has_flexible_gp_ref_p for details.  */
373   bool has_inflexible_gp_insn_p;
374   bool has_flexible_gp_insn_p;
375 
376   /* True if the function's prologue must load the global pointer
377      value into pic_offset_table_rtx and store the same value in
378      the function's cprestore slot (if any).  Even if this value
379      is currently false, we may decide to set it to true later;
380      see mips_must_initialize_gp_p () for details.  */
381   bool must_initialize_gp_p;
382 
383   /* True if the current function must restore $gp after any potential
384      clobber.  This value is only meaningful during the first post-epilogue
385      split_insns pass; see mips_must_initialize_gp_p () for details.  */
386   bool must_restore_gp_when_clobbered_p;
387 
388   /* True if this is an interrupt handler.  */
389   bool interrupt_handler_p;
390 
391   /* True if this is an interrupt handler that uses shadow registers.  */
392   bool use_shadow_register_set_p;
393 
394   /* True if this is an interrupt handler that should keep interrupts
395      masked.  */
396   bool keep_interrupts_masked_p;
397 
398   /* True if this is an interrupt handler that should use DERET
399      instead of ERET.  */
400   bool use_debug_exception_return_p;
401 };
402 
403 /* Information about a single argument.  */
404 struct mips_arg_info {
405   /* True if the argument is passed in a floating-point register, or
406      would have been if we hadn't run out of registers.  */
407   bool fpr_p;
408 
409   /* The number of words passed in registers, rounded up.  */
410   unsigned int reg_words;
411 
412   /* For EABI, the offset of the first register from GP_ARG_FIRST or
413      FP_ARG_FIRST.  For other ABIs, the offset of the first register from
414      the start of the ABI's argument structure (see the CUMULATIVE_ARGS
415      comment for details).
416 
417      The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
418      on the stack.  */
419   unsigned int reg_offset;
420 
421   /* The number of words that must be passed on the stack, rounded up.  */
422   unsigned int stack_words;
423 
424   /* The offset from the start of the stack overflow area of the argument's
425      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
426   unsigned int stack_offset;
427 };
428 
429 /* Information about an address described by mips_address_type.
430 
431    ADDRESS_CONST_INT
432        No fields are used.
433 
434    ADDRESS_REG
435        REG is the base register and OFFSET is the constant offset.
436 
437    ADDRESS_LO_SUM
438        REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
439        is the type of symbol it references.
440 
441    ADDRESS_SYMBOLIC
442        SYMBOL_TYPE is the type of symbol that the address references.  */
443 struct mips_address_info {
444   enum mips_address_type type;
445   rtx reg;
446   rtx offset;
447   enum mips_symbol_type symbol_type;
448 };
449 
450 /* One stage in a constant building sequence.  These sequences have
451    the form:
452 
453 	A = VALUE[0]
454 	A = A CODE[1] VALUE[1]
455 	A = A CODE[2] VALUE[2]
456 	...
457 
458    where A is an accumulator, each CODE[i] is a binary rtl operation
459    and each VALUE[i] is a constant integer.  CODE[0] is undefined.  */
460 struct mips_integer_op {
461   enum rtx_code code;
462   unsigned HOST_WIDE_INT value;
463 };
464 
465 /* The largest number of operations needed to load an integer constant.
466    The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
467    When the lowest bit is clear, we can try, but reject a sequence with
468    an extra SLL at the end.  */
469 #define MIPS_MAX_INTEGER_OPS 7
470 
471 /* Information about a MIPS16e SAVE or RESTORE instruction.  */
472 struct mips16e_save_restore_info {
473   /* The number of argument registers saved by a SAVE instruction.
474      0 for RESTORE instructions.  */
475   unsigned int nargs;
476 
477   /* Bit X is set if the instruction saves or restores GPR X.  */
478   unsigned int mask;
479 
480   /* The total number of bytes to allocate.  */
481   HOST_WIDE_INT size;
482 };
483 
484 /* Costs of various operations on the different architectures.  */
485 
486 struct mips_rtx_cost_data
487 {
488   unsigned short fp_add;
489   unsigned short fp_mult_sf;
490   unsigned short fp_mult_df;
491   unsigned short fp_div_sf;
492   unsigned short fp_div_df;
493   unsigned short int_mult_si;
494   unsigned short int_mult_di;
495   unsigned short int_div_si;
496   unsigned short int_div_di;
497   unsigned short branch_cost;
498   unsigned short memory_latency;
499 };
500 
501 /* Global variables for machine-dependent things.  */
502 
503 /* The -G setting, or the configuration's default small-data limit if
504    no -G option is given.  */
505 static unsigned int mips_small_data_threshold;
506 
507 /* The number of file directives written by mips_output_filename.  */
508 int num_source_filenames;
509 
510 /* The name that appeared in the last .file directive written by
511    mips_output_filename, or "" if mips_output_filename hasn't
512    written anything yet.  */
513 const char *current_function_file = "";
514 
515 /* Arrays that map GCC register numbers to debugger register numbers.  */
516 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
517 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
518 
519 /* Information about the current function's epilogue, used only while
520    expanding it.  */
521 static struct {
522   /* A list of queued REG_CFA_RESTORE notes.  */
523   rtx cfa_restores;
524 
525   /* The CFA is currently defined as CFA_REG + CFA_OFFSET.  */
526   rtx cfa_reg;
527   HOST_WIDE_INT cfa_offset;
528 
529   /* The offset of the CFA from the stack pointer while restoring
530      registers.  */
531   HOST_WIDE_INT cfa_restore_sp_offset;
532 } mips_epilogue;
533 
534 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs.  */
535 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
536 struct mips_asm_switch mips_nomacro = { "macro", 0 };
537 struct mips_asm_switch mips_noat = { "at", 0 };
538 
539 /* True if we're writing out a branch-likely instruction rather than a
540    normal branch.  */
541 static bool mips_branch_likely;
542 
543 /* The current instruction-set architecture.  */
544 enum processor mips_arch;
545 const struct mips_cpu_info *mips_arch_info;
546 
547 /* The processor that we should tune the code for.  */
548 enum processor mips_tune;
549 const struct mips_cpu_info *mips_tune_info;
550 
551 /* The ISA level associated with mips_arch.  */
552 int mips_isa;
553 
554 /* The architecture selected by -mipsN, or null if -mipsN wasn't used.  */
555 static const struct mips_cpu_info *mips_isa_option_info;
556 
557 /* Which cost information to use.  */
558 static const struct mips_rtx_cost_data *mips_cost;
559 
560 /* The ambient target flags, excluding MASK_MIPS16.  */
561 static int mips_base_target_flags;
562 
563 /* True if MIPS16 is the default mode.  */
564 bool mips_base_mips16;
565 
566 /* The ambient values of other global variables.  */
567 static int mips_base_schedule_insns; /* flag_schedule_insns */
568 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
569 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
570 static int mips_base_align_loops; /* align_loops */
571 static int mips_base_align_jumps; /* align_jumps */
572 static int mips_base_align_functions; /* align_functions */
573 
574 /* Index [M][R] is true if register R is allowed to hold a value of mode M.  */
575 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
576 
577 /* Index C is true if character C is a valid PRINT_OPERAND punctation
578    character.  */
579 static bool mips_print_operand_punct[256];
580 
581 static GTY (()) int mips_output_filename_first_time = 1;
582 
583 /* mips_split_p[X] is true if symbols of type X can be split by
584    mips_split_symbol.  */
585 bool mips_split_p[NUM_SYMBOL_TYPES];
586 
587 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
588    can be split by mips_split_symbol.  */
589 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
590 
591 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
592    forced into a PC-relative constant pool.  */
593 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
594 
595 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
596    appears in a LO_SUM.  It can be null if such LO_SUMs aren't valid or
597    if they are matched by a special .md file pattern.  */
598 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
599 
600 /* Likewise for HIGHs.  */
601 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
602 
603 /* Target state for MIPS16.  */
604 struct target_globals *mips16_globals;
605 
606 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
607    and returned from mips_sched_reorder2.  */
608 static int cached_can_issue_more;
609 
610 /* True if the output uses __mips16_rdhwr.  */
611 static bool mips_need_mips16_rdhwr_p;
612 
613 /* Index R is the smallest register class that contains register R.  */
614 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
615   LEA_REGS,	LEA_REGS,	M16_REGS,	V1_REG,
616   M16_REGS,	M16_REGS,	M16_REGS,	M16_REGS,
617   LEA_REGS,	LEA_REGS,	LEA_REGS,	LEA_REGS,
618   LEA_REGS,	LEA_REGS,	LEA_REGS,	LEA_REGS,
619   M16_REGS,	M16_REGS,	LEA_REGS,	LEA_REGS,
620   LEA_REGS,	LEA_REGS,	LEA_REGS,	LEA_REGS,
621   T_REG,	PIC_FN_ADDR_REG, LEA_REGS,	LEA_REGS,
622   LEA_REGS,	LEA_REGS,	LEA_REGS,	LEA_REGS,
623   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
624   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
625   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
626   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
627   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
628   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
629   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
630   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
631   MD0_REG,	MD1_REG,	NO_REGS,	ST_REGS,
632   ST_REGS,	ST_REGS,	ST_REGS,	ST_REGS,
633   ST_REGS,	ST_REGS,	ST_REGS,	NO_REGS,
634   NO_REGS,	FRAME_REGS,	FRAME_REGS,	NO_REGS,
635   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
636   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
637   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
638   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
639   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
640   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
641   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
642   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
643   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
644   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
645   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
646   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
647   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
648   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
649   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
650   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
651   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
652   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
653   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
654   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
655   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
656   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
657   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
658   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
659   DSP_ACC_REGS,	DSP_ACC_REGS,	DSP_ACC_REGS,	DSP_ACC_REGS,
660   DSP_ACC_REGS,	DSP_ACC_REGS,	ALL_REGS,	ALL_REGS,
661   ALL_REGS,	ALL_REGS,	ALL_REGS,	ALL_REGS
662 };
663 
664 /* The value of TARGET_ATTRIBUTE_TABLE.  */
665 static const struct attribute_spec mips_attribute_table[] = {
666   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
667        om_diagnostic } */
668   { "long_call",   0, 0, false, true,  true,  NULL, false },
669   { "far",     	   0, 0, false, true,  true,  NULL, false },
670   { "near",        0, 0, false, true,  true,  NULL, false },
671   /* We would really like to treat "mips16" and "nomips16" as type
672      attributes, but GCC doesn't provide the hooks we need to support
673      the right conversion rules.  As declaration attributes, they affect
674      code generation but don't carry other semantics.  */
675   { "mips16", 	   0, 0, true,  false, false, NULL, false },
676   { "nomips16",    0, 0, true,  false, false, NULL, false },
677   /* Allow functions to be specified as interrupt handlers */
678   { "interrupt",   0, 0, false, true,  true, NULL, false },
679   { "use_shadow_register_set",	0, 0, false, true,  true, NULL, false },
680   { "keep_interrupts_masked",	0, 0, false, true,  true, NULL, false },
681   { "use_debug_exception_return", 0, 0, false, true,  true, NULL, false },
682   { NULL,	   0, 0, false, false, false, NULL, false }
683 };
684 
685 /* A table describing all the processors GCC knows about; see
686    mips-cpus.def for details.  */
687 static const struct mips_cpu_info mips_cpu_info_table[] = {
688 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
689   { NAME, CPU, ISA, FLAGS },
690 #include "mips-cpus.def"
691 #undef MIPS_CPU
692 };
693 
694 /* Default costs.  If these are used for a processor we should look
695    up the actual costs.  */
696 #define DEFAULT_COSTS COSTS_N_INSNS (6),  /* fp_add */       \
697                       COSTS_N_INSNS (7),  /* fp_mult_sf */   \
698                       COSTS_N_INSNS (8),  /* fp_mult_df */   \
699                       COSTS_N_INSNS (23), /* fp_div_sf */    \
700                       COSTS_N_INSNS (36), /* fp_div_df */    \
701                       COSTS_N_INSNS (10), /* int_mult_si */  \
702                       COSTS_N_INSNS (10), /* int_mult_di */  \
703                       COSTS_N_INSNS (69), /* int_div_si */   \
704                       COSTS_N_INSNS (69), /* int_div_di */   \
705                                        2, /* branch_cost */  \
706                                        4  /* memory_latency */
707 
708 /* Floating-point costs for processors without an FPU.  Just assume that
709    all floating-point libcalls are very expensive.  */
710 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */       \
711                       COSTS_N_INSNS (256), /* fp_mult_sf */   \
712                       COSTS_N_INSNS (256), /* fp_mult_df */   \
713                       COSTS_N_INSNS (256), /* fp_div_sf */    \
714                       COSTS_N_INSNS (256)  /* fp_div_df */
715 
716 /* Costs to use when optimizing for size.  */
717 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
718   COSTS_N_INSNS (1),            /* fp_add */
719   COSTS_N_INSNS (1),            /* fp_mult_sf */
720   COSTS_N_INSNS (1),            /* fp_mult_df */
721   COSTS_N_INSNS (1),            /* fp_div_sf */
722   COSTS_N_INSNS (1),            /* fp_div_df */
723   COSTS_N_INSNS (1),            /* int_mult_si */
724   COSTS_N_INSNS (1),            /* int_mult_di */
725   COSTS_N_INSNS (1),            /* int_div_si */
726   COSTS_N_INSNS (1),            /* int_div_di */
727 		   2,           /* branch_cost */
728 		   4            /* memory_latency */
729 };
730 
731 /* Costs to use when optimizing for speed, indexed by processor.  */
732 static const struct mips_rtx_cost_data
733   mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
734   { /* R3000 */
735     COSTS_N_INSNS (2),            /* fp_add */
736     COSTS_N_INSNS (4),            /* fp_mult_sf */
737     COSTS_N_INSNS (5),            /* fp_mult_df */
738     COSTS_N_INSNS (12),           /* fp_div_sf */
739     COSTS_N_INSNS (19),           /* fp_div_df */
740     COSTS_N_INSNS (12),           /* int_mult_si */
741     COSTS_N_INSNS (12),           /* int_mult_di */
742     COSTS_N_INSNS (35),           /* int_div_si */
743     COSTS_N_INSNS (35),           /* int_div_di */
744 		     1,           /* branch_cost */
745 		     4            /* memory_latency */
746   },
747   { /* 4KC */
748     SOFT_FP_COSTS,
749     COSTS_N_INSNS (6),            /* int_mult_si */
750     COSTS_N_INSNS (6),            /* int_mult_di */
751     COSTS_N_INSNS (36),           /* int_div_si */
752     COSTS_N_INSNS (36),           /* int_div_di */
753 		     1,           /* branch_cost */
754 		     4            /* memory_latency */
755   },
756   { /* 4KP */
757     SOFT_FP_COSTS,
758     COSTS_N_INSNS (36),           /* int_mult_si */
759     COSTS_N_INSNS (36),           /* int_mult_di */
760     COSTS_N_INSNS (37),           /* int_div_si */
761     COSTS_N_INSNS (37),           /* int_div_di */
762 		     1,           /* branch_cost */
763 		     4            /* memory_latency */
764   },
765   { /* 5KC */
766     SOFT_FP_COSTS,
767     COSTS_N_INSNS (4),            /* int_mult_si */
768     COSTS_N_INSNS (11),           /* int_mult_di */
769     COSTS_N_INSNS (36),           /* int_div_si */
770     COSTS_N_INSNS (68),           /* int_div_di */
771 		     1,           /* branch_cost */
772 		     4            /* memory_latency */
773   },
774   { /* 5KF */
775     COSTS_N_INSNS (4),            /* fp_add */
776     COSTS_N_INSNS (4),            /* fp_mult_sf */
777     COSTS_N_INSNS (5),            /* fp_mult_df */
778     COSTS_N_INSNS (17),           /* fp_div_sf */
779     COSTS_N_INSNS (32),           /* fp_div_df */
780     COSTS_N_INSNS (4),            /* int_mult_si */
781     COSTS_N_INSNS (11),           /* int_mult_di */
782     COSTS_N_INSNS (36),           /* int_div_si */
783     COSTS_N_INSNS (68),           /* int_div_di */
784 		     1,           /* branch_cost */
785 		     4            /* memory_latency */
786   },
787   { /* 20KC */
788     COSTS_N_INSNS (4),            /* fp_add */
789     COSTS_N_INSNS (4),            /* fp_mult_sf */
790     COSTS_N_INSNS (5),            /* fp_mult_df */
791     COSTS_N_INSNS (17),           /* fp_div_sf */
792     COSTS_N_INSNS (32),           /* fp_div_df */
793     COSTS_N_INSNS (4),            /* int_mult_si */
794     COSTS_N_INSNS (7),            /* int_mult_di */
795     COSTS_N_INSNS (42),           /* int_div_si */
796     COSTS_N_INSNS (72),           /* int_div_di */
797 		     1,           /* branch_cost */
798 		     4            /* memory_latency */
799   },
800   { /* 24KC */
801     SOFT_FP_COSTS,
802     COSTS_N_INSNS (5),            /* int_mult_si */
803     COSTS_N_INSNS (5),            /* int_mult_di */
804     COSTS_N_INSNS (41),           /* int_div_si */
805     COSTS_N_INSNS (41),           /* int_div_di */
806 		     1,           /* branch_cost */
807 		     4            /* memory_latency */
808   },
809   { /* 24KF2_1 */
810     COSTS_N_INSNS (8),            /* fp_add */
811     COSTS_N_INSNS (8),            /* fp_mult_sf */
812     COSTS_N_INSNS (10),           /* fp_mult_df */
813     COSTS_N_INSNS (34),           /* fp_div_sf */
814     COSTS_N_INSNS (64),           /* fp_div_df */
815     COSTS_N_INSNS (5),            /* int_mult_si */
816     COSTS_N_INSNS (5),            /* int_mult_di */
817     COSTS_N_INSNS (41),           /* int_div_si */
818     COSTS_N_INSNS (41),           /* int_div_di */
819 		     1,           /* branch_cost */
820 		     4            /* memory_latency */
821   },
822   { /* 24KF1_1 */
823     COSTS_N_INSNS (4),            /* fp_add */
824     COSTS_N_INSNS (4),            /* fp_mult_sf */
825     COSTS_N_INSNS (5),            /* fp_mult_df */
826     COSTS_N_INSNS (17),           /* fp_div_sf */
827     COSTS_N_INSNS (32),           /* fp_div_df */
828     COSTS_N_INSNS (5),            /* int_mult_si */
829     COSTS_N_INSNS (5),            /* int_mult_di */
830     COSTS_N_INSNS (41),           /* int_div_si */
831     COSTS_N_INSNS (41),           /* int_div_di */
832 		     1,           /* branch_cost */
833 		     4            /* memory_latency */
834   },
835   { /* 74KC */
836     SOFT_FP_COSTS,
837     COSTS_N_INSNS (5),            /* int_mult_si */
838     COSTS_N_INSNS (5),            /* int_mult_di */
839     COSTS_N_INSNS (41),           /* int_div_si */
840     COSTS_N_INSNS (41),           /* int_div_di */
841 		     1,           /* branch_cost */
842 		     4            /* memory_latency */
843   },
844   { /* 74KF2_1 */
845     COSTS_N_INSNS (8),            /* fp_add */
846     COSTS_N_INSNS (8),            /* fp_mult_sf */
847     COSTS_N_INSNS (10),           /* fp_mult_df */
848     COSTS_N_INSNS (34),           /* fp_div_sf */
849     COSTS_N_INSNS (64),           /* fp_div_df */
850     COSTS_N_INSNS (5),            /* int_mult_si */
851     COSTS_N_INSNS (5),            /* int_mult_di */
852     COSTS_N_INSNS (41),           /* int_div_si */
853     COSTS_N_INSNS (41),           /* int_div_di */
854 		     1,           /* branch_cost */
855 		     4            /* memory_latency */
856   },
857   { /* 74KF1_1 */
858     COSTS_N_INSNS (4),            /* fp_add */
859     COSTS_N_INSNS (4),            /* fp_mult_sf */
860     COSTS_N_INSNS (5),            /* fp_mult_df */
861     COSTS_N_INSNS (17),           /* fp_div_sf */
862     COSTS_N_INSNS (32),           /* fp_div_df */
863     COSTS_N_INSNS (5),            /* int_mult_si */
864     COSTS_N_INSNS (5),            /* int_mult_di */
865     COSTS_N_INSNS (41),           /* int_div_si */
866     COSTS_N_INSNS (41),           /* int_div_di */
867 		     1,           /* branch_cost */
868 		     4            /* memory_latency */
869   },
870   { /* 74KF3_2 */
871     COSTS_N_INSNS (6),            /* fp_add */
872     COSTS_N_INSNS (6),            /* fp_mult_sf */
873     COSTS_N_INSNS (7),            /* fp_mult_df */
874     COSTS_N_INSNS (25),           /* fp_div_sf */
875     COSTS_N_INSNS (48),           /* fp_div_df */
876     COSTS_N_INSNS (5),            /* int_mult_si */
877     COSTS_N_INSNS (5),            /* int_mult_di */
878     COSTS_N_INSNS (41),           /* int_div_si */
879     COSTS_N_INSNS (41),           /* int_div_di */
880 		     1,           /* branch_cost */
881 		     4            /* memory_latency */
882   },
883   { /* Loongson-2E */
884     DEFAULT_COSTS
885   },
886   { /* Loongson-2F */
887     DEFAULT_COSTS
888   },
889   { /* Loongson-3A */
890     DEFAULT_COSTS
891   },
892   { /* M4k */
893     DEFAULT_COSTS
894   },
895     /* Octeon */
896   {
897     SOFT_FP_COSTS,
898     COSTS_N_INSNS (5),            /* int_mult_si */
899     COSTS_N_INSNS (5),            /* int_mult_di */
900     COSTS_N_INSNS (72),           /* int_div_si */
901     COSTS_N_INSNS (72),           /* int_div_di */
902                      1,		  /* branch_cost */
903                      4		  /* memory_latency */
904   },
905     /* Octeon II */
906   {
907     SOFT_FP_COSTS,
908     COSTS_N_INSNS (6),            /* int_mult_si */
909     COSTS_N_INSNS (6),            /* int_mult_di */
910     COSTS_N_INSNS (18),           /* int_div_si */
911     COSTS_N_INSNS (35),           /* int_div_di */
912                      4,		  /* branch_cost */
913                      4		  /* memory_latency */
914   },
915   { /* R3900 */
916     COSTS_N_INSNS (2),            /* fp_add */
917     COSTS_N_INSNS (4),            /* fp_mult_sf */
918     COSTS_N_INSNS (5),            /* fp_mult_df */
919     COSTS_N_INSNS (12),           /* fp_div_sf */
920     COSTS_N_INSNS (19),           /* fp_div_df */
921     COSTS_N_INSNS (2),            /* int_mult_si */
922     COSTS_N_INSNS (2),            /* int_mult_di */
923     COSTS_N_INSNS (35),           /* int_div_si */
924     COSTS_N_INSNS (35),           /* int_div_di */
925 		     1,           /* branch_cost */
926 		     4            /* memory_latency */
927   },
928   { /* R6000 */
929     COSTS_N_INSNS (3),            /* fp_add */
930     COSTS_N_INSNS (5),            /* fp_mult_sf */
931     COSTS_N_INSNS (6),            /* fp_mult_df */
932     COSTS_N_INSNS (15),           /* fp_div_sf */
933     COSTS_N_INSNS (16),           /* fp_div_df */
934     COSTS_N_INSNS (17),           /* int_mult_si */
935     COSTS_N_INSNS (17),           /* int_mult_di */
936     COSTS_N_INSNS (38),           /* int_div_si */
937     COSTS_N_INSNS (38),           /* int_div_di */
938 		     2,           /* branch_cost */
939 		     6            /* memory_latency */
940   },
941   { /* R4000 */
942      COSTS_N_INSNS (6),           /* fp_add */
943      COSTS_N_INSNS (7),           /* fp_mult_sf */
944      COSTS_N_INSNS (8),           /* fp_mult_df */
945      COSTS_N_INSNS (23),          /* fp_div_sf */
946      COSTS_N_INSNS (36),          /* fp_div_df */
947      COSTS_N_INSNS (10),          /* int_mult_si */
948      COSTS_N_INSNS (10),          /* int_mult_di */
949      COSTS_N_INSNS (69),          /* int_div_si */
950      COSTS_N_INSNS (69),          /* int_div_di */
951 		      2,          /* branch_cost */
952 		      6           /* memory_latency */
953   },
954   { /* R4100 */
955     DEFAULT_COSTS
956   },
957   { /* R4111 */
958     DEFAULT_COSTS
959   },
960   { /* R4120 */
961     DEFAULT_COSTS
962   },
963   { /* R4130 */
964     /* The only costs that appear to be updated here are
965        integer multiplication.  */
966     SOFT_FP_COSTS,
967     COSTS_N_INSNS (4),            /* int_mult_si */
968     COSTS_N_INSNS (6),            /* int_mult_di */
969     COSTS_N_INSNS (69),           /* int_div_si */
970     COSTS_N_INSNS (69),           /* int_div_di */
971 		     1,           /* branch_cost */
972 		     4            /* memory_latency */
973   },
974   { /* R4300 */
975     DEFAULT_COSTS
976   },
977   { /* R4600 */
978     DEFAULT_COSTS
979   },
980   { /* R4650 */
981     DEFAULT_COSTS
982   },
983   { /* R4700 */
984     DEFAULT_COSTS
985   },
986   { /* R5000 */
987     COSTS_N_INSNS (6),            /* fp_add */
988     COSTS_N_INSNS (4),            /* fp_mult_sf */
989     COSTS_N_INSNS (5),            /* fp_mult_df */
990     COSTS_N_INSNS (23),           /* fp_div_sf */
991     COSTS_N_INSNS (36),           /* fp_div_df */
992     COSTS_N_INSNS (5),            /* int_mult_si */
993     COSTS_N_INSNS (5),            /* int_mult_di */
994     COSTS_N_INSNS (36),           /* int_div_si */
995     COSTS_N_INSNS (36),           /* int_div_di */
996 		     1,           /* branch_cost */
997 		     4            /* memory_latency */
998   },
999   { /* R5400 */
1000     COSTS_N_INSNS (6),            /* fp_add */
1001     COSTS_N_INSNS (5),            /* fp_mult_sf */
1002     COSTS_N_INSNS (6),            /* fp_mult_df */
1003     COSTS_N_INSNS (30),           /* fp_div_sf */
1004     COSTS_N_INSNS (59),           /* fp_div_df */
1005     COSTS_N_INSNS (3),            /* int_mult_si */
1006     COSTS_N_INSNS (4),            /* int_mult_di */
1007     COSTS_N_INSNS (42),           /* int_div_si */
1008     COSTS_N_INSNS (74),           /* int_div_di */
1009 		     1,           /* branch_cost */
1010 		     4            /* memory_latency */
1011   },
1012   { /* R5500 */
1013     COSTS_N_INSNS (6),            /* fp_add */
1014     COSTS_N_INSNS (5),            /* fp_mult_sf */
1015     COSTS_N_INSNS (6),            /* fp_mult_df */
1016     COSTS_N_INSNS (30),           /* fp_div_sf */
1017     COSTS_N_INSNS (59),           /* fp_div_df */
1018     COSTS_N_INSNS (5),            /* int_mult_si */
1019     COSTS_N_INSNS (9),            /* int_mult_di */
1020     COSTS_N_INSNS (42),           /* int_div_si */
1021     COSTS_N_INSNS (74),           /* int_div_di */
1022 		     1,           /* branch_cost */
1023 		     4            /* memory_latency */
1024   },
1025   { /* R7000 */
1026     /* The only costs that are changed here are
1027        integer multiplication.  */
1028     COSTS_N_INSNS (6),            /* fp_add */
1029     COSTS_N_INSNS (7),            /* fp_mult_sf */
1030     COSTS_N_INSNS (8),            /* fp_mult_df */
1031     COSTS_N_INSNS (23),           /* fp_div_sf */
1032     COSTS_N_INSNS (36),           /* fp_div_df */
1033     COSTS_N_INSNS (5),            /* int_mult_si */
1034     COSTS_N_INSNS (9),            /* int_mult_di */
1035     COSTS_N_INSNS (69),           /* int_div_si */
1036     COSTS_N_INSNS (69),           /* int_div_di */
1037 		     1,           /* branch_cost */
1038 		     4            /* memory_latency */
1039   },
1040   { /* R8000 */
1041     DEFAULT_COSTS
1042   },
1043   { /* R9000 */
1044     /* The only costs that are changed here are
1045        integer multiplication.  */
1046     COSTS_N_INSNS (6),            /* fp_add */
1047     COSTS_N_INSNS (7),            /* fp_mult_sf */
1048     COSTS_N_INSNS (8),            /* fp_mult_df */
1049     COSTS_N_INSNS (23),           /* fp_div_sf */
1050     COSTS_N_INSNS (36),           /* fp_div_df */
1051     COSTS_N_INSNS (3),            /* int_mult_si */
1052     COSTS_N_INSNS (8),            /* int_mult_di */
1053     COSTS_N_INSNS (69),           /* int_div_si */
1054     COSTS_N_INSNS (69),           /* int_div_di */
1055 		     1,           /* branch_cost */
1056 		     4            /* memory_latency */
1057   },
1058   { /* R1x000 */
1059     COSTS_N_INSNS (2),            /* fp_add */
1060     COSTS_N_INSNS (2),            /* fp_mult_sf */
1061     COSTS_N_INSNS (2),            /* fp_mult_df */
1062     COSTS_N_INSNS (12),           /* fp_div_sf */
1063     COSTS_N_INSNS (19),           /* fp_div_df */
1064     COSTS_N_INSNS (5),            /* int_mult_si */
1065     COSTS_N_INSNS (9),            /* int_mult_di */
1066     COSTS_N_INSNS (34),           /* int_div_si */
1067     COSTS_N_INSNS (66),           /* int_div_di */
1068 		     1,           /* branch_cost */
1069 		     4            /* memory_latency */
1070   },
1071   { /* SB1 */
1072     /* These costs are the same as the SB-1A below.  */
1073     COSTS_N_INSNS (4),            /* fp_add */
1074     COSTS_N_INSNS (4),            /* fp_mult_sf */
1075     COSTS_N_INSNS (4),            /* fp_mult_df */
1076     COSTS_N_INSNS (24),           /* fp_div_sf */
1077     COSTS_N_INSNS (32),           /* fp_div_df */
1078     COSTS_N_INSNS (3),            /* int_mult_si */
1079     COSTS_N_INSNS (4),            /* int_mult_di */
1080     COSTS_N_INSNS (36),           /* int_div_si */
1081     COSTS_N_INSNS (68),           /* int_div_di */
1082 		     1,           /* branch_cost */
1083 		     4            /* memory_latency */
1084   },
1085   { /* SB1-A */
1086     /* These costs are the same as the SB-1 above.  */
1087     COSTS_N_INSNS (4),            /* fp_add */
1088     COSTS_N_INSNS (4),            /* fp_mult_sf */
1089     COSTS_N_INSNS (4),            /* fp_mult_df */
1090     COSTS_N_INSNS (24),           /* fp_div_sf */
1091     COSTS_N_INSNS (32),           /* fp_div_df */
1092     COSTS_N_INSNS (3),            /* int_mult_si */
1093     COSTS_N_INSNS (4),            /* int_mult_di */
1094     COSTS_N_INSNS (36),           /* int_div_si */
1095     COSTS_N_INSNS (68),           /* int_div_di */
1096 		     1,           /* branch_cost */
1097 		     4            /* memory_latency */
1098   },
1099   { /* SR71000 */
1100     DEFAULT_COSTS
1101   },
1102   { /* XLR */
1103     SOFT_FP_COSTS,
1104     COSTS_N_INSNS (8),            /* int_mult_si */
1105     COSTS_N_INSNS (8),            /* int_mult_di */
1106     COSTS_N_INSNS (72),           /* int_div_si */
1107     COSTS_N_INSNS (72),           /* int_div_di */
1108 		     1,           /* branch_cost */
1109 		     4            /* memory_latency */
1110   },
1111   { /* XLP */
1112     /* These costs are the same as 5KF above.  */
1113     COSTS_N_INSNS (4),            /* fp_add */
1114     COSTS_N_INSNS (4),            /* fp_mult_sf */
1115     COSTS_N_INSNS (5),            /* fp_mult_df */
1116     COSTS_N_INSNS (17),           /* fp_div_sf */
1117     COSTS_N_INSNS (32),           /* fp_div_df */
1118     COSTS_N_INSNS (4),            /* int_mult_si */
1119     COSTS_N_INSNS (11),           /* int_mult_di */
1120     COSTS_N_INSNS (36),           /* int_div_si */
1121     COSTS_N_INSNS (68),           /* int_div_di */
1122 		     1,           /* branch_cost */
1123 		     4            /* memory_latency */
1124   }
1125 };
1126 
1127 static rtx mips_find_pic_call_symbol (rtx, rtx, bool);
1128 static int mips_register_move_cost (enum machine_mode, reg_class_t,
1129 				    reg_class_t);
1130 static unsigned int mips_function_arg_boundary (enum machine_mode, const_tree);
1131 
1132 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1133    for -mflip_mips16.  It maps decl names onto a boolean mode setting.  */
1134 struct GTY (())  mflip_mips16_entry {
1135   const char *name;
1136   bool mips16_p;
1137 };
1138 static GTY ((param_is (struct mflip_mips16_entry))) htab_t mflip_mips16_htab;
1139 
1140 /* Hash table callbacks for mflip_mips16_htab.  */
1141 
1142 static hashval_t
1143 mflip_mips16_htab_hash (const void *entry)
1144 {
1145   return htab_hash_string (((const struct mflip_mips16_entry *) entry)->name);
1146 }
1147 
1148 static int
1149 mflip_mips16_htab_eq (const void *entry, const void *name)
1150 {
1151   return strcmp (((const struct mflip_mips16_entry *) entry)->name,
1152 		 (const char *) name) == 0;
1153 }
1154 
1155 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1156    mode, false if it should next add an attribute for the opposite mode.  */
1157 static GTY(()) bool mips16_flipper;
1158 
1159 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1160    for -mflip-mips16.  Return true if it should use "mips16" and false if
1161    it should use "nomips16".  */
1162 
1163 static bool
1164 mflip_mips16_use_mips16_p (tree decl)
1165 {
1166   struct mflip_mips16_entry *entry;
1167   const char *name;
1168   hashval_t hash;
1169   void **slot;
1170 
1171   /* Use the opposite of the command-line setting for anonymous decls.  */
1172   if (!DECL_NAME (decl))
1173     return !mips_base_mips16;
1174 
1175   if (!mflip_mips16_htab)
1176     mflip_mips16_htab = htab_create_ggc (37, mflip_mips16_htab_hash,
1177 					 mflip_mips16_htab_eq, NULL);
1178 
1179   name = IDENTIFIER_POINTER (DECL_NAME (decl));
1180   hash = htab_hash_string (name);
1181   slot = htab_find_slot_with_hash (mflip_mips16_htab, name, hash, INSERT);
1182   entry = (struct mflip_mips16_entry *) *slot;
1183   if (!entry)
1184     {
1185       mips16_flipper = !mips16_flipper;
1186       entry = ggc_alloc_mflip_mips16_entry ();
1187       entry->name = name;
1188       entry->mips16_p = mips16_flipper ? !mips_base_mips16 : mips_base_mips16;
1189       *slot = entry;
1190     }
1191   return entry->mips16_p;
1192 }
1193 
1194 /* Predicates to test for presence of "near" and "far"/"long_call"
1195    attributes on the given TYPE.  */
1196 
1197 static bool
1198 mips_near_type_p (const_tree type)
1199 {
1200   return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1201 }
1202 
1203 static bool
1204 mips_far_type_p (const_tree type)
1205 {
1206   return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1207 	  || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1208 }
1209 
1210 /* Similar predicates for "mips16"/"nomips16" function attributes.  */
1211 
1212 static bool
1213 mips_mips16_decl_p (const_tree decl)
1214 {
1215   return lookup_attribute ("mips16", DECL_ATTRIBUTES (decl)) != NULL;
1216 }
1217 
1218 static bool
1219 mips_nomips16_decl_p (const_tree decl)
1220 {
1221   return lookup_attribute ("nomips16", DECL_ATTRIBUTES (decl)) != NULL;
1222 }
1223 
1224 /* Check if the interrupt attribute is set for a function.  */
1225 
1226 static bool
1227 mips_interrupt_type_p (tree type)
1228 {
1229   return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1230 }
1231 
1232 /* Check if the attribute to use shadow register set is set for a function.  */
1233 
1234 static bool
1235 mips_use_shadow_register_set_p (tree type)
1236 {
1237   return lookup_attribute ("use_shadow_register_set",
1238 			   TYPE_ATTRIBUTES (type)) != NULL;
1239 }
1240 
1241 /* Check if the attribute to keep interrupts masked is set for a function.  */
1242 
1243 static bool
1244 mips_keep_interrupts_masked_p (tree type)
1245 {
1246   return lookup_attribute ("keep_interrupts_masked",
1247 			   TYPE_ATTRIBUTES (type)) != NULL;
1248 }
1249 
1250 /* Check if the attribute to use debug exception return is set for
1251    a function.  */
1252 
1253 static bool
1254 mips_use_debug_exception_return_p (tree type)
1255 {
1256   return lookup_attribute ("use_debug_exception_return",
1257 			   TYPE_ATTRIBUTES (type)) != NULL;
1258 }
1259 
1260 /* Return true if function DECL is a MIPS16 function.  Return the ambient
1261    setting if DECL is null.  */
1262 
1263 static bool
1264 mips_use_mips16_mode_p (tree decl)
1265 {
1266   if (decl)
1267     {
1268       /* Nested functions must use the same frame pointer as their
1269 	 parent and must therefore use the same ISA mode.  */
1270       tree parent = decl_function_context (decl);
1271       if (parent)
1272 	decl = parent;
1273       if (mips_mips16_decl_p (decl))
1274 	return true;
1275       if (mips_nomips16_decl_p (decl))
1276 	return false;
1277     }
1278   return mips_base_mips16;
1279 }
1280 
1281 /* Implement TARGET_COMP_TYPE_ATTRIBUTES.  */
1282 
1283 static int
1284 mips_comp_type_attributes (const_tree type1, const_tree type2)
1285 {
1286   /* Disallow mixed near/far attributes.  */
1287   if (mips_far_type_p (type1) && mips_near_type_p (type2))
1288     return 0;
1289   if (mips_near_type_p (type1) && mips_far_type_p (type2))
1290     return 0;
1291   return 1;
1292 }
1293 
1294 /* Implement TARGET_INSERT_ATTRIBUTES.  */
1295 
1296 static void
1297 mips_insert_attributes (tree decl, tree *attributes)
1298 {
1299   const char *name;
1300   bool mips16_p, nomips16_p;
1301 
1302   /* Check for "mips16" and "nomips16" attributes.  */
1303   mips16_p = lookup_attribute ("mips16", *attributes) != NULL;
1304   nomips16_p = lookup_attribute ("nomips16", *attributes) != NULL;
1305   if (TREE_CODE (decl) != FUNCTION_DECL)
1306     {
1307       if (mips16_p)
1308 	error ("%qs attribute only applies to functions", "mips16");
1309       if (nomips16_p)
1310 	error ("%qs attribute only applies to functions", "nomips16");
1311     }
1312   else
1313     {
1314       mips16_p |= mips_mips16_decl_p (decl);
1315       nomips16_p |= mips_nomips16_decl_p (decl);
1316       if (mips16_p || nomips16_p)
1317 	{
1318 	  /* DECL cannot be simultaneously "mips16" and "nomips16".  */
1319 	  if (mips16_p && nomips16_p)
1320 	    error ("%qE cannot have both %<mips16%> and "
1321 		   "%<nomips16%> attributes",
1322 		   DECL_NAME (decl));
1323 	}
1324       else if (TARGET_FLIP_MIPS16 && !DECL_ARTIFICIAL (decl))
1325 	{
1326 	  /* Implement -mflip-mips16.  If DECL has neither a "nomips16" nor a
1327 	     "mips16" attribute, arbitrarily pick one.  We must pick the same
1328 	     setting for duplicate declarations of a function.  */
1329 	  name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1330 	  *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1331 	}
1332     }
1333 }
1334 
1335 /* Implement TARGET_MERGE_DECL_ATTRIBUTES.  */
1336 
1337 static tree
1338 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1339 {
1340   /* The decls' "mips16" and "nomips16" attributes must match exactly.  */
1341   if (mips_mips16_decl_p (olddecl) != mips_mips16_decl_p (newdecl))
1342     error ("%qE redeclared with conflicting %qs attributes",
1343 	   DECL_NAME (newdecl), "mips16");
1344   if (mips_nomips16_decl_p (olddecl) != mips_nomips16_decl_p (newdecl))
1345     error ("%qE redeclared with conflicting %qs attributes",
1346 	   DECL_NAME (newdecl), "nomips16");
1347 
1348   return merge_attributes (DECL_ATTRIBUTES (olddecl),
1349 			   DECL_ATTRIBUTES (newdecl));
1350 }
1351 
1352 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1353    and *OFFSET_PTR.  Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise.  */
1354 
1355 static void
1356 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1357 {
1358   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1359     {
1360       *base_ptr = XEXP (x, 0);
1361       *offset_ptr = INTVAL (XEXP (x, 1));
1362     }
1363   else
1364     {
1365       *base_ptr = x;
1366       *offset_ptr = 0;
1367     }
1368 }
1369 
1370 static unsigned int mips_build_integer (struct mips_integer_op *,
1371 					unsigned HOST_WIDE_INT);
1372 
1373 /* A subroutine of mips_build_integer, with the same interface.
1374    Assume that the final action in the sequence should be a left shift.  */
1375 
1376 static unsigned int
1377 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1378 {
1379   unsigned int i, shift;
1380 
1381   /* Shift VALUE right until its lowest bit is set.  Shift arithmetically
1382      since signed numbers are easier to load than unsigned ones.  */
1383   shift = 0;
1384   while ((value & 1) == 0)
1385     value /= 2, shift++;
1386 
1387   i = mips_build_integer (codes, value);
1388   codes[i].code = ASHIFT;
1389   codes[i].value = shift;
1390   return i + 1;
1391 }
1392 
1393 /* As for mips_build_shift, but assume that the final action will be
1394    an IOR or PLUS operation.  */
1395 
1396 static unsigned int
1397 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1398 {
1399   unsigned HOST_WIDE_INT high;
1400   unsigned int i;
1401 
1402   high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1403   if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1404     {
1405       /* The constant is too complex to load with a simple LUI/ORI pair,
1406 	 so we want to give the recursive call as many trailing zeros as
1407 	 possible.  In this case, we know bit 16 is set and that the
1408 	 low 16 bits form a negative number.  If we subtract that number
1409 	 from VALUE, we will clear at least the lowest 17 bits, maybe more.  */
1410       i = mips_build_integer (codes, CONST_HIGH_PART (value));
1411       codes[i].code = PLUS;
1412       codes[i].value = CONST_LOW_PART (value);
1413     }
1414   else
1415     {
1416       /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1417 	 bits gives a value with at least 17 trailing zeros.  */
1418       i = mips_build_integer (codes, high);
1419       codes[i].code = IOR;
1420       codes[i].value = value & 0xffff;
1421     }
1422   return i + 1;
1423 }
1424 
1425 /* Fill CODES with a sequence of rtl operations to load VALUE.
1426    Return the number of operations needed.  */
1427 
1428 static unsigned int
1429 mips_build_integer (struct mips_integer_op *codes,
1430 		    unsigned HOST_WIDE_INT value)
1431 {
1432   if (SMALL_OPERAND (value)
1433       || SMALL_OPERAND_UNSIGNED (value)
1434       || LUI_OPERAND (value))
1435     {
1436       /* The value can be loaded with a single instruction.  */
1437       codes[0].code = UNKNOWN;
1438       codes[0].value = value;
1439       return 1;
1440     }
1441   else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1442     {
1443       /* Either the constant is a simple LUI/ORI combination or its
1444 	 lowest bit is set.  We don't want to shift in this case.  */
1445       return mips_build_lower (codes, value);
1446     }
1447   else if ((value & 0xffff) == 0)
1448     {
1449       /* The constant will need at least three actions.  The lowest
1450 	 16 bits are clear, so the final action will be a shift.  */
1451       return mips_build_shift (codes, value);
1452     }
1453   else
1454     {
1455       /* The final action could be a shift, add or inclusive OR.
1456 	 Rather than use a complex condition to select the best
1457 	 approach, try both mips_build_shift and mips_build_lower
1458 	 and pick the one that gives the shortest sequence.
1459 	 Note that this case is only used once per constant.  */
1460       struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1461       unsigned int cost, alt_cost;
1462 
1463       cost = mips_build_shift (codes, value);
1464       alt_cost = mips_build_lower (alt_codes, value);
1465       if (alt_cost < cost)
1466 	{
1467 	  memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1468 	  cost = alt_cost;
1469 	}
1470       return cost;
1471     }
1472 }
1473 
1474 /* Implement TARGET_LEGITIMATE_CONSTANT_P.  */
1475 
1476 static bool
1477 mips_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1478 {
1479   return mips_const_insns (x) > 0;
1480 }
1481 
1482 /* Return a SYMBOL_REF for a MIPS16 function called NAME.  */
1483 
1484 static rtx
1485 mips16_stub_function (const char *name)
1486 {
1487   rtx x;
1488 
1489   x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1490   SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1491   return x;
1492 }
1493 
1494 /* Return true if symbols of type TYPE require a GOT access.  */
1495 
1496 static bool
1497 mips_got_symbol_type_p (enum mips_symbol_type type)
1498 {
1499   switch (type)
1500     {
1501     case SYMBOL_GOT_PAGE_OFST:
1502     case SYMBOL_GOT_DISP:
1503       return true;
1504 
1505     default:
1506       return false;
1507     }
1508 }
1509 
1510 /* Return true if X is a thread-local symbol.  */
1511 
1512 static bool
1513 mips_tls_symbol_p (rtx x)
1514 {
1515   return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1516 }
1517 
1518 /* Return true if SYMBOL_REF X is associated with a global symbol
1519    (in the STB_GLOBAL sense).  */
1520 
1521 static bool
1522 mips_global_symbol_p (const_rtx x)
1523 {
1524   const_tree decl = SYMBOL_REF_DECL (x);
1525 
1526   if (!decl)
1527     return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1528 
1529   /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1530      or weak symbols.  Relocations in the object file will be against
1531      the target symbol, so it's that symbol's binding that matters here.  */
1532   return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1533 }
1534 
1535 /* Return true if function X is a libgcc MIPS16 stub function.  */
1536 
1537 static bool
1538 mips16_stub_function_p (const_rtx x)
1539 {
1540   return (GET_CODE (x) == SYMBOL_REF
1541 	  && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1542 }
1543 
1544 /* Return true if function X is a locally-defined and locally-binding
1545    MIPS16 function.  */
1546 
1547 static bool
1548 mips16_local_function_p (const_rtx x)
1549 {
1550   return (GET_CODE (x) == SYMBOL_REF
1551 	  && SYMBOL_REF_LOCAL_P (x)
1552 	  && !SYMBOL_REF_EXTERNAL_P (x)
1553 	  && mips_use_mips16_mode_p (SYMBOL_REF_DECL (x)));
1554 }
1555 
1556 /* Return true if SYMBOL_REF X binds locally.  */
1557 
1558 static bool
1559 mips_symbol_binds_local_p (const_rtx x)
1560 {
1561   return (SYMBOL_REF_DECL (x)
1562 	  ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1563 	  : SYMBOL_REF_LOCAL_P (x));
1564 }
1565 
1566 /* Return true if rtx constants of mode MODE should be put into a small
1567    data section.  */
1568 
1569 static bool
1570 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1571 {
1572   return (!TARGET_EMBEDDED_DATA
1573 	  && TARGET_LOCAL_SDATA
1574 	  && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1575 }
1576 
1577 /* Return true if X should not be moved directly into register $25.
1578    We need this because many versions of GAS will treat "la $25,foo" as
1579    part of a call sequence and so allow a global "foo" to be lazily bound.  */
1580 
1581 bool
1582 mips_dangerous_for_la25_p (rtx x)
1583 {
1584   return (!TARGET_EXPLICIT_RELOCS
1585 	  && TARGET_USE_GOT
1586 	  && GET_CODE (x) == SYMBOL_REF
1587 	  && mips_global_symbol_p (x));
1588 }
1589 
1590 /* Return true if calls to X might need $25 to be valid on entry.  */
1591 
1592 bool
1593 mips_use_pic_fn_addr_reg_p (const_rtx x)
1594 {
1595   if (!TARGET_USE_PIC_FN_ADDR_REG)
1596     return false;
1597 
1598   /* MIPS16 stub functions are guaranteed not to use $25.  */
1599   if (mips16_stub_function_p (x))
1600     return false;
1601 
1602   if (GET_CODE (x) == SYMBOL_REF)
1603     {
1604       /* If PLTs and copy relocations are available, the static linker
1605 	 will make sure that $25 is valid on entry to the target function.  */
1606       if (TARGET_ABICALLS_PIC0)
1607 	return false;
1608 
1609       /* Locally-defined functions use absolute accesses to set up
1610 	 the global pointer.  */
1611       if (TARGET_ABSOLUTE_ABICALLS
1612 	  && mips_symbol_binds_local_p (x)
1613 	  && !SYMBOL_REF_EXTERNAL_P (x))
1614 	return false;
1615     }
1616 
1617   return true;
1618 }
1619 
1620 /* Return the method that should be used to access SYMBOL_REF or
1621    LABEL_REF X in context CONTEXT.  */
1622 
1623 static enum mips_symbol_type
1624 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1625 {
1626   if (TARGET_RTP_PIC)
1627     return SYMBOL_GOT_DISP;
1628 
1629   if (GET_CODE (x) == LABEL_REF)
1630     {
1631       /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
1632 	 code and if we know that the label is in the current function's
1633 	 text section.  LABEL_REFs are used for jump tables as well as
1634 	 text labels, so we must check whether jump tables live in the
1635 	 text section.  */
1636       if (TARGET_MIPS16_SHORT_JUMP_TABLES
1637 	  && !LABEL_REF_NONLOCAL_P (x))
1638 	return SYMBOL_PC_RELATIVE;
1639 
1640       if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1641 	return SYMBOL_GOT_PAGE_OFST;
1642 
1643       return SYMBOL_ABSOLUTE;
1644     }
1645 
1646   gcc_assert (GET_CODE (x) == SYMBOL_REF);
1647 
1648   if (SYMBOL_REF_TLS_MODEL (x))
1649     return SYMBOL_TLS;
1650 
1651   if (CONSTANT_POOL_ADDRESS_P (x))
1652     {
1653       if (TARGET_MIPS16_TEXT_LOADS)
1654 	return SYMBOL_PC_RELATIVE;
1655 
1656       if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1657 	return SYMBOL_PC_RELATIVE;
1658 
1659       if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1660 	return SYMBOL_GP_RELATIVE;
1661     }
1662 
1663   /* Do not use small-data accesses for weak symbols; they may end up
1664      being zero.  */
1665   if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1666     return SYMBOL_GP_RELATIVE;
1667 
1668   /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1669      is in effect.  */
1670   if (TARGET_ABICALLS_PIC2
1671       && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1672     {
1673       /* There are three cases to consider:
1674 
1675 	    - o32 PIC (either with or without explicit relocs)
1676 	    - n32/n64 PIC without explicit relocs
1677 	    - n32/n64 PIC with explicit relocs
1678 
1679 	 In the first case, both local and global accesses will use an
1680 	 R_MIPS_GOT16 relocation.  We must correctly predict which of
1681 	 the two semantics (local or global) the assembler and linker
1682 	 will apply.  The choice depends on the symbol's binding rather
1683 	 than its visibility.
1684 
1685 	 In the second case, the assembler will not use R_MIPS_GOT16
1686 	 relocations, but it chooses between local and global accesses
1687 	 in the same way as for o32 PIC.
1688 
1689 	 In the third case we have more freedom since both forms of
1690 	 access will work for any kind of symbol.  However, there seems
1691 	 little point in doing things differently.  */
1692       if (mips_global_symbol_p (x))
1693 	return SYMBOL_GOT_DISP;
1694 
1695       return SYMBOL_GOT_PAGE_OFST;
1696     }
1697 
1698   return SYMBOL_ABSOLUTE;
1699 }
1700 
1701 /* Classify the base of symbolic expression X, given that X appears in
1702    context CONTEXT.  */
1703 
1704 static enum mips_symbol_type
1705 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1706 {
1707   rtx offset;
1708 
1709   split_const (x, &x, &offset);
1710   if (UNSPEC_ADDRESS_P (x))
1711     return UNSPEC_ADDRESS_TYPE (x);
1712 
1713   return mips_classify_symbol (x, context);
1714 }
1715 
1716 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1717    is the alignment in bytes of SYMBOL_REF X.  */
1718 
1719 static bool
1720 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1721 {
1722   HOST_WIDE_INT align;
1723 
1724   align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1725   return IN_RANGE (offset, 0, align - 1);
1726 }
1727 
1728 /* Return true if X is a symbolic constant that can be used in context
1729    CONTEXT.  If it is, store the type of the symbol in *SYMBOL_TYPE.  */
1730 
1731 bool
1732 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1733 			  enum mips_symbol_type *symbol_type)
1734 {
1735   rtx offset;
1736 
1737   split_const (x, &x, &offset);
1738   if (UNSPEC_ADDRESS_P (x))
1739     {
1740       *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1741       x = UNSPEC_ADDRESS (x);
1742     }
1743   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1744     {
1745       *symbol_type = mips_classify_symbol (x, context);
1746       if (*symbol_type == SYMBOL_TLS)
1747 	return false;
1748     }
1749   else
1750     return false;
1751 
1752   if (offset == const0_rtx)
1753     return true;
1754 
1755   /* Check whether a nonzero offset is valid for the underlying
1756      relocations.  */
1757   switch (*symbol_type)
1758     {
1759     case SYMBOL_ABSOLUTE:
1760     case SYMBOL_64_HIGH:
1761     case SYMBOL_64_MID:
1762     case SYMBOL_64_LOW:
1763       /* If the target has 64-bit pointers and the object file only
1764 	 supports 32-bit symbols, the values of those symbols will be
1765 	 sign-extended.  In this case we can't allow an arbitrary offset
1766 	 in case the 32-bit value X + OFFSET has a different sign from X.  */
1767       if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1768 	return offset_within_block_p (x, INTVAL (offset));
1769 
1770       /* In other cases the relocations can handle any offset.  */
1771       return true;
1772 
1773     case SYMBOL_PC_RELATIVE:
1774       /* Allow constant pool references to be converted to LABEL+CONSTANT.
1775 	 In this case, we no longer have access to the underlying constant,
1776 	 but the original symbol-based access was known to be valid.  */
1777       if (GET_CODE (x) == LABEL_REF)
1778 	return true;
1779 
1780       /* Fall through.  */
1781 
1782     case SYMBOL_GP_RELATIVE:
1783       /* Make sure that the offset refers to something within the
1784 	 same object block.  This should guarantee that the final
1785 	 PC- or GP-relative offset is within the 16-bit limit.  */
1786       return offset_within_block_p (x, INTVAL (offset));
1787 
1788     case SYMBOL_GOT_PAGE_OFST:
1789     case SYMBOL_GOTOFF_PAGE:
1790       /* If the symbol is global, the GOT entry will contain the symbol's
1791 	 address, and we will apply a 16-bit offset after loading it.
1792 	 If the symbol is local, the linker should provide enough local
1793 	 GOT entries for a 16-bit offset, but larger offsets may lead
1794 	 to GOT overflow.  */
1795       return SMALL_INT (offset);
1796 
1797     case SYMBOL_TPREL:
1798     case SYMBOL_DTPREL:
1799       /* There is no carry between the HI and LO REL relocations, so the
1800 	 offset is only valid if we know it won't lead to such a carry.  */
1801       return mips_offset_within_alignment_p (x, INTVAL (offset));
1802 
1803     case SYMBOL_GOT_DISP:
1804     case SYMBOL_GOTOFF_DISP:
1805     case SYMBOL_GOTOFF_CALL:
1806     case SYMBOL_GOTOFF_LOADGP:
1807     case SYMBOL_TLSGD:
1808     case SYMBOL_TLSLDM:
1809     case SYMBOL_GOTTPREL:
1810     case SYMBOL_TLS:
1811     case SYMBOL_HALF:
1812       return false;
1813     }
1814   gcc_unreachable ();
1815 }
1816 
1817 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
1818    single instruction.  We rely on the fact that, in the worst case,
1819    all instructions involved in a MIPS16 address calculation are usually
1820    extended ones.  */
1821 
1822 static int
1823 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
1824 {
1825   if (mips_use_pcrel_pool_p[(int) type])
1826     {
1827       if (mode == MAX_MACHINE_MODE)
1828 	/* LEAs will be converted into constant-pool references by
1829 	   mips_reorg.  */
1830 	type = SYMBOL_PC_RELATIVE;
1831       else
1832 	/* The constant must be loaded and then dereferenced.  */
1833 	return 0;
1834     }
1835 
1836   switch (type)
1837     {
1838     case SYMBOL_ABSOLUTE:
1839       /* When using 64-bit symbols, we need 5 preparatory instructions,
1840 	 such as:
1841 
1842 	     lui     $at,%highest(symbol)
1843 	     daddiu  $at,$at,%higher(symbol)
1844 	     dsll    $at,$at,16
1845 	     daddiu  $at,$at,%hi(symbol)
1846 	     dsll    $at,$at,16
1847 
1848 	 The final address is then $at + %lo(symbol).  With 32-bit
1849 	 symbols we just need a preparatory LUI for normal mode and
1850 	 a preparatory LI and SLL for MIPS16.  */
1851       return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
1852 
1853     case SYMBOL_GP_RELATIVE:
1854       /* Treat GP-relative accesses as taking a single instruction on
1855 	 MIPS16 too; the copy of $gp can often be shared.  */
1856       return 1;
1857 
1858     case SYMBOL_PC_RELATIVE:
1859       /* PC-relative constants can be only be used with ADDIUPC,
1860 	 DADDIUPC, LWPC and LDPC.  */
1861       if (mode == MAX_MACHINE_MODE
1862 	  || GET_MODE_SIZE (mode) == 4
1863 	  || GET_MODE_SIZE (mode) == 8)
1864 	return 1;
1865 
1866       /* The constant must be loaded using ADDIUPC or DADDIUPC first.  */
1867       return 0;
1868 
1869     case SYMBOL_GOT_DISP:
1870       /* The constant will have to be loaded from the GOT before it
1871 	 is used in an address.  */
1872       if (mode != MAX_MACHINE_MODE)
1873 	return 0;
1874 
1875       /* Fall through.  */
1876 
1877     case SYMBOL_GOT_PAGE_OFST:
1878       /* Unless -funit-at-a-time is in effect, we can't be sure whether the
1879 	 local/global classification is accurate.  The worst cases are:
1880 
1881 	 (1) For local symbols when generating o32 or o64 code.  The assembler
1882 	     will use:
1883 
1884 		 lw	      $at,%got(symbol)
1885 		 nop
1886 
1887 	     ...and the final address will be $at + %lo(symbol).
1888 
1889 	 (2) For global symbols when -mxgot.  The assembler will use:
1890 
1891 	         lui     $at,%got_hi(symbol)
1892 	         (d)addu $at,$at,$gp
1893 
1894 	     ...and the final address will be $at + %got_lo(symbol).  */
1895       return 3;
1896 
1897     case SYMBOL_GOTOFF_PAGE:
1898     case SYMBOL_GOTOFF_DISP:
1899     case SYMBOL_GOTOFF_CALL:
1900     case SYMBOL_GOTOFF_LOADGP:
1901     case SYMBOL_64_HIGH:
1902     case SYMBOL_64_MID:
1903     case SYMBOL_64_LOW:
1904     case SYMBOL_TLSGD:
1905     case SYMBOL_TLSLDM:
1906     case SYMBOL_DTPREL:
1907     case SYMBOL_GOTTPREL:
1908     case SYMBOL_TPREL:
1909     case SYMBOL_HALF:
1910       /* A 16-bit constant formed by a single relocation, or a 32-bit
1911 	 constant formed from a high 16-bit relocation and a low 16-bit
1912 	 relocation.  Use mips_split_p to determine which.  32-bit
1913 	 constants need an "lui; addiu" sequence for normal mode and
1914 	 an "li; sll; addiu" sequence for MIPS16 mode.  */
1915       return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
1916 
1917     case SYMBOL_TLS:
1918       /* We don't treat a bare TLS symbol as a constant.  */
1919       return 0;
1920     }
1921   gcc_unreachable ();
1922 }
1923 
1924 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
1925    to load symbols of type TYPE into a register.  Return 0 if the given
1926    type of symbol cannot be used as an immediate operand.
1927 
1928    Otherwise, return the number of instructions needed to load or store
1929    values of mode MODE to or from addresses of type TYPE.  Return 0 if
1930    the given type of symbol is not valid in addresses.
1931 
1932    In both cases, treat extended MIPS16 instructions as two instructions.  */
1933 
1934 static int
1935 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
1936 {
1937   return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
1938 }
1939 
1940 /* A for_each_rtx callback.  Stop the search if *X references a
1941    thread-local symbol.  */
1942 
1943 static int
1944 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1945 {
1946   return mips_tls_symbol_p (*x);
1947 }
1948 
1949 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
1950 
1951 static bool
1952 mips_cannot_force_const_mem (enum machine_mode mode, rtx x)
1953 {
1954   enum mips_symbol_type type;
1955   rtx base, offset;
1956 
1957   /* There is no assembler syntax for expressing an address-sized
1958      high part.  */
1959   if (GET_CODE (x) == HIGH)
1960     return true;
1961 
1962   /* As an optimization, reject constants that mips_legitimize_move
1963      can expand inline.
1964 
1965      Suppose we have a multi-instruction sequence that loads constant C
1966      into register R.  If R does not get allocated a hard register, and
1967      R is used in an operand that allows both registers and memory
1968      references, reload will consider forcing C into memory and using
1969      one of the instruction's memory alternatives.  Returning false
1970      here will force it to use an input reload instead.  */
1971   if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
1972     return true;
1973 
1974   split_const (x, &base, &offset);
1975   if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
1976     {
1977       /* See whether we explicitly want these symbols in the pool.  */
1978       if (mips_use_pcrel_pool_p[(int) type])
1979 	return false;
1980 
1981       /* The same optimization as for CONST_INT.  */
1982       if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
1983 	return true;
1984 
1985       /* If MIPS16 constant pools live in the text section, they should
1986 	 not refer to anything that might need run-time relocation.  */
1987       if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
1988 	return true;
1989     }
1990 
1991   /* TLS symbols must be computed by mips_legitimize_move.  */
1992   if (for_each_rtx (&x, &mips_tls_symbol_ref_1, NULL))
1993     return true;
1994 
1995   return false;
1996 }
1997 
1998 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  We can't use blocks for
1999    constants when we're using a per-function constant pool.  */
2000 
2001 static bool
2002 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2003 				const_rtx x ATTRIBUTE_UNUSED)
2004 {
2005   return !TARGET_MIPS16_PCREL_LOADS;
2006 }
2007 
2008 /* Return true if register REGNO is a valid base register for mode MODE.
2009    STRICT_P is true if REG_OK_STRICT is in effect.  */
2010 
2011 int
2012 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
2013 			       bool strict_p)
2014 {
2015   if (!HARD_REGISTER_NUM_P (regno))
2016     {
2017       if (!strict_p)
2018 	return true;
2019       regno = reg_renumber[regno];
2020     }
2021 
2022   /* These fake registers will be eliminated to either the stack or
2023      hard frame pointer, both of which are usually valid base registers.
2024      Reload deals with the cases where the eliminated form isn't valid.  */
2025   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2026     return true;
2027 
2028   /* In MIPS16 mode, the stack pointer can only address word and doubleword
2029      values, nothing smaller.  There are two problems here:
2030 
2031        (a) Instantiating virtual registers can introduce new uses of the
2032 	   stack pointer.  If these virtual registers are valid addresses,
2033 	   the stack pointer should be too.
2034 
2035        (b) Most uses of the stack pointer are not made explicit until
2036 	   FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
2037 	   We don't know until that stage whether we'll be eliminating to the
2038 	   stack pointer (which needs the restriction) or the hard frame
2039 	   pointer (which doesn't).
2040 
2041      All in all, it seems more consistent to only enforce this restriction
2042      during and after reload.  */
2043   if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2044     return !strict_p || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2045 
2046   return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2047 }
2048 
2049 /* Return true if X is a valid base register for mode MODE.
2050    STRICT_P is true if REG_OK_STRICT is in effect.  */
2051 
2052 static bool
2053 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
2054 {
2055   if (!strict_p && GET_CODE (x) == SUBREG)
2056     x = SUBREG_REG (x);
2057 
2058   return (REG_P (x)
2059 	  && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2060 }
2061 
2062 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2063    can address a value of mode MODE.  */
2064 
2065 static bool
2066 mips_valid_offset_p (rtx x, enum machine_mode mode)
2067 {
2068   /* Check that X is a signed 16-bit number.  */
2069   if (!const_arith_operand (x, Pmode))
2070     return false;
2071 
2072   /* We may need to split multiword moves, so make sure that every word
2073      is accessible.  */
2074   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2075       && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2076     return false;
2077 
2078   return true;
2079 }
2080 
2081 /* Return true if a LO_SUM can address a value of mode MODE when the
2082    LO_SUM symbol has type SYMBOL_TYPE.  */
2083 
2084 static bool
2085 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, enum machine_mode mode)
2086 {
2087   /* Check that symbols of type SYMBOL_TYPE can be used to access values
2088      of mode MODE.  */
2089   if (mips_symbol_insns (symbol_type, mode) == 0)
2090     return false;
2091 
2092   /* Check that there is a known low-part relocation.  */
2093   if (mips_lo_relocs[symbol_type] == NULL)
2094     return false;
2095 
2096   /* We may need to split multiword moves, so make sure that each word
2097      can be accessed without inducing a carry.  This is mainly needed
2098      for o64, which has historically only guaranteed 64-bit alignment
2099      for 128-bit types.  */
2100   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2101       && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2102     return false;
2103 
2104   return true;
2105 }
2106 
2107 /* Return true if X is a valid address for machine mode MODE.  If it is,
2108    fill in INFO appropriately.  STRICT_P is true if REG_OK_STRICT is in
2109    effect.  */
2110 
2111 static bool
2112 mips_classify_address (struct mips_address_info *info, rtx x,
2113 		       enum machine_mode mode, bool strict_p)
2114 {
2115   switch (GET_CODE (x))
2116     {
2117     case REG:
2118     case SUBREG:
2119       info->type = ADDRESS_REG;
2120       info->reg = x;
2121       info->offset = const0_rtx;
2122       return mips_valid_base_register_p (info->reg, mode, strict_p);
2123 
2124     case PLUS:
2125       info->type = ADDRESS_REG;
2126       info->reg = XEXP (x, 0);
2127       info->offset = XEXP (x, 1);
2128       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2129 	      && mips_valid_offset_p (info->offset, mode));
2130 
2131     case LO_SUM:
2132       info->type = ADDRESS_LO_SUM;
2133       info->reg = XEXP (x, 0);
2134       info->offset = XEXP (x, 1);
2135       /* We have to trust the creator of the LO_SUM to do something vaguely
2136 	 sane.  Target-independent code that creates a LO_SUM should also
2137 	 create and verify the matching HIGH.  Target-independent code that
2138 	 adds an offset to a LO_SUM must prove that the offset will not
2139 	 induce a carry.  Failure to do either of these things would be
2140 	 a bug, and we are not required to check for it here.  The MIPS
2141 	 backend itself should only create LO_SUMs for valid symbolic
2142 	 constants, with the high part being either a HIGH or a copy
2143 	 of _gp. */
2144       info->symbol_type
2145 	= mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2146       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2147 	      && mips_valid_lo_sum_p (info->symbol_type, mode));
2148 
2149     case CONST_INT:
2150       /* Small-integer addresses don't occur very often, but they
2151 	 are legitimate if $0 is a valid base register.  */
2152       info->type = ADDRESS_CONST_INT;
2153       return !TARGET_MIPS16 && SMALL_INT (x);
2154 
2155     case CONST:
2156     case LABEL_REF:
2157     case SYMBOL_REF:
2158       info->type = ADDRESS_SYMBOLIC;
2159       return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2160 					&info->symbol_type)
2161 	      && mips_symbol_insns (info->symbol_type, mode) > 0
2162 	      && !mips_split_p[info->symbol_type]);
2163 
2164     default:
2165       return false;
2166     }
2167 }
2168 
2169 /* Implement TARGET_LEGITIMATE_ADDRESS_P.  */
2170 
2171 static bool
2172 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2173 {
2174   struct mips_address_info addr;
2175 
2176   return mips_classify_address (&addr, x, mode, strict_p);
2177 }
2178 
2179 /* Return true if X is a legitimate $sp-based address for mode MDOE.  */
2180 
2181 bool
2182 mips_stack_address_p (rtx x, enum machine_mode mode)
2183 {
2184   struct mips_address_info addr;
2185 
2186   return (mips_classify_address (&addr, x, mode, false)
2187 	  && addr.type == ADDRESS_REG
2188 	  && addr.reg == stack_pointer_rtx);
2189 }
2190 
2191 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2192    address instruction.  Note that such addresses are not considered
2193    legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2194    is so restricted.  */
2195 
2196 static bool
2197 mips_lwxs_address_p (rtx addr)
2198 {
2199   if (ISA_HAS_LWXS
2200       && GET_CODE (addr) == PLUS
2201       && REG_P (XEXP (addr, 1)))
2202     {
2203       rtx offset = XEXP (addr, 0);
2204       if (GET_CODE (offset) == MULT
2205 	  && REG_P (XEXP (offset, 0))
2206 	  && CONST_INT_P (XEXP (offset, 1))
2207 	  && INTVAL (XEXP (offset, 1)) == 4)
2208 	return true;
2209     }
2210   return false;
2211 }
2212 
2213 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load
2214    indexed address instruction.  Note that such addresses are
2215    not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2216    sense, because their use is so restricted.  */
2217 
2218 static bool
2219 mips_lx_address_p (rtx addr, enum machine_mode mode)
2220 {
2221   if (GET_CODE (addr) != PLUS
2222       || !REG_P (XEXP (addr, 0))
2223       || !REG_P (XEXP (addr, 1)))
2224     return false;
2225   if (ISA_HAS_LBX && mode == QImode)
2226     return true;
2227   if (ISA_HAS_LHX && mode == HImode)
2228     return true;
2229   if (ISA_HAS_LWX && mode == SImode)
2230     return true;
2231   if (ISA_HAS_LDX && mode == DImode)
2232     return true;
2233   return false;
2234 }
2235 
2236 /* Return true if a value at OFFSET bytes from base register BASE can be
2237    accessed using an unextended MIPS16 instruction.  MODE is the mode of
2238    the value.
2239 
2240    Usually the offset in an unextended instruction is a 5-bit field.
2241    The offset is unsigned and shifted left once for LH and SH, twice
2242    for LW and SW, and so on.  An exception is LWSP and SWSP, which have
2243    an 8-bit immediate field that's shifted left twice.  */
2244 
2245 static bool
2246 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
2247 			       unsigned HOST_WIDE_INT offset)
2248 {
2249   if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2250     {
2251       if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2252 	return offset < 256U * GET_MODE_SIZE (mode);
2253       return offset < 32U * GET_MODE_SIZE (mode);
2254     }
2255   return false;
2256 }
2257 
2258 /* Return the number of instructions needed to load or store a value
2259    of mode MODE at address X.  Return 0 if X isn't valid for MODE.
2260    Assume that multiword moves may need to be split into word moves
2261    if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2262    enough.
2263 
2264    For MIPS16 code, count extended instructions as two instructions.  */
2265 
2266 int
2267 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
2268 {
2269   struct mips_address_info addr;
2270   int factor;
2271 
2272   /* BLKmode is used for single unaligned loads and stores and should
2273      not count as a multiword mode.  (GET_MODE_SIZE (BLKmode) is pretty
2274      meaningless, so we have to single it out as a special case one way
2275      or the other.)  */
2276   if (mode != BLKmode && might_split_p)
2277     factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2278   else
2279     factor = 1;
2280 
2281   if (mips_classify_address (&addr, x, mode, false))
2282     switch (addr.type)
2283       {
2284       case ADDRESS_REG:
2285 	if (TARGET_MIPS16
2286 	    && !mips16_unextended_reference_p (mode, addr.reg,
2287 					       UINTVAL (addr.offset)))
2288 	  return factor * 2;
2289 	return factor;
2290 
2291       case ADDRESS_LO_SUM:
2292 	return TARGET_MIPS16 ? factor * 2 : factor;
2293 
2294       case ADDRESS_CONST_INT:
2295 	return factor;
2296 
2297       case ADDRESS_SYMBOLIC:
2298 	return factor * mips_symbol_insns (addr.symbol_type, mode);
2299       }
2300   return 0;
2301 }
2302 
2303 /* Return the number of instructions needed to load constant X.
2304    Return 0 if X isn't a valid constant.  */
2305 
2306 int
2307 mips_const_insns (rtx x)
2308 {
2309   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2310   enum mips_symbol_type symbol_type;
2311   rtx offset;
2312 
2313   switch (GET_CODE (x))
2314     {
2315     case HIGH:
2316       if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2317 				     &symbol_type)
2318 	  || !mips_split_p[symbol_type])
2319 	return 0;
2320 
2321       /* This is simply an LUI for normal mode.  It is an extended
2322 	 LI followed by an extended SLL for MIPS16.  */
2323       return TARGET_MIPS16 ? 4 : 1;
2324 
2325     case CONST_INT:
2326       if (TARGET_MIPS16)
2327 	/* Unsigned 8-bit constants can be loaded using an unextended
2328 	   LI instruction.  Unsigned 16-bit constants can be loaded
2329 	   using an extended LI.  Negative constants must be loaded
2330 	   using LI and then negated.  */
2331 	return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2332 		: SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2333 		: IN_RANGE (-INTVAL (x), 0, 255) ? 2
2334 		: SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2335 		: 0);
2336 
2337       return mips_build_integer (codes, INTVAL (x));
2338 
2339     case CONST_DOUBLE:
2340     case CONST_VECTOR:
2341       /* Allow zeros for normal mode, where we can use $0.  */
2342       return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2343 
2344     case CONST:
2345       if (CONST_GP_P (x))
2346 	return 1;
2347 
2348       /* See if we can refer to X directly.  */
2349       if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2350 	return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2351 
2352       /* Otherwise try splitting the constant into a base and offset.
2353 	 If the offset is a 16-bit value, we can load the base address
2354 	 into a register and then use (D)ADDIU to add in the offset.
2355 	 If the offset is larger, we can load the base and offset
2356 	 into separate registers and add them together with (D)ADDU.
2357 	 However, the latter is only possible before reload; during
2358 	 and after reload, we must have the option of forcing the
2359 	 constant into the pool instead.  */
2360       split_const (x, &x, &offset);
2361       if (offset != 0)
2362 	{
2363 	  int n = mips_const_insns (x);
2364 	  if (n != 0)
2365 	    {
2366 	      if (SMALL_INT (offset))
2367 		return n + 1;
2368 	      else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2369 		return n + 1 + mips_build_integer (codes, INTVAL (offset));
2370 	    }
2371 	}
2372       return 0;
2373 
2374     case SYMBOL_REF:
2375     case LABEL_REF:
2376       return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2377 				MAX_MACHINE_MODE);
2378 
2379     default:
2380       return 0;
2381     }
2382 }
2383 
2384 /* X is a doubleword constant that can be handled by splitting it into
2385    two words and loading each word separately.  Return the number of
2386    instructions required to do this.  */
2387 
2388 int
2389 mips_split_const_insns (rtx x)
2390 {
2391   unsigned int low, high;
2392 
2393   low = mips_const_insns (mips_subword (x, false));
2394   high = mips_const_insns (mips_subword (x, true));
2395   gcc_assert (low > 0 && high > 0);
2396   return low + high;
2397 }
2398 
2399 /* Return the number of instructions needed to implement INSN,
2400    given that it loads from or stores to MEM.  Count extended
2401    MIPS16 instructions as two instructions.  */
2402 
2403 int
2404 mips_load_store_insns (rtx mem, rtx insn)
2405 {
2406   enum machine_mode mode;
2407   bool might_split_p;
2408   rtx set;
2409 
2410   gcc_assert (MEM_P (mem));
2411   mode = GET_MODE (mem);
2412 
2413   /* Try to prove that INSN does not need to be split.  */
2414   might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2415   if (might_split_p)
2416     {
2417       set = single_set (insn);
2418       if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
2419 	might_split_p = false;
2420     }
2421 
2422   return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2423 }
2424 
2425 /* Return the number of instructions needed for an integer division.  */
2426 
2427 int
2428 mips_idiv_insns (void)
2429 {
2430   int count;
2431 
2432   count = 1;
2433   if (TARGET_CHECK_ZERO_DIV)
2434     {
2435       if (GENERATE_DIVIDE_TRAPS)
2436         count++;
2437       else
2438         count += 2;
2439     }
2440 
2441   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2442     count++;
2443   return count;
2444 }
2445 
2446 /* Emit a move from SRC to DEST.  Assume that the move expanders can
2447    handle all moves if !can_create_pseudo_p ().  The distinction is
2448    important because, unlike emit_move_insn, the move expanders know
2449    how to force Pmode objects into the constant pool even when the
2450    constant pool address is not itself legitimate.  */
2451 
2452 rtx
2453 mips_emit_move (rtx dest, rtx src)
2454 {
2455   return (can_create_pseudo_p ()
2456 	  ? emit_move_insn (dest, src)
2457 	  : emit_move_insn_1 (dest, src));
2458 }
2459 
2460 /* Emit a move from SRC to DEST, splitting compound moves into individual
2461    instructions.  SPLIT_TYPE is the type of split to perform.  */
2462 
2463 static void
2464 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
2465 {
2466   if (mips_split_move_p (dest, src, split_type))
2467     mips_split_move (dest, src, split_type);
2468   else
2469     mips_emit_move (dest, src);
2470 }
2471 
2472 /* Emit an instruction of the form (set TARGET (CODE OP0)).  */
2473 
2474 static void
2475 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2476 {
2477   emit_insn (gen_rtx_SET (VOIDmode, target,
2478 			  gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2479 }
2480 
2481 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2482    Return that new register.  */
2483 
2484 static rtx
2485 mips_force_unary (enum machine_mode mode, enum rtx_code code, rtx op0)
2486 {
2487   rtx reg;
2488 
2489   reg = gen_reg_rtx (mode);
2490   mips_emit_unary (code, reg, op0);
2491   return reg;
2492 }
2493 
2494 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)).  */
2495 
2496 void
2497 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2498 {
2499   emit_insn (gen_rtx_SET (VOIDmode, target,
2500 			  gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2501 }
2502 
2503 /* Compute (CODE OP0 OP1) and store the result in a new register
2504    of mode MODE.  Return that new register.  */
2505 
2506 static rtx
2507 mips_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2508 {
2509   rtx reg;
2510 
2511   reg = gen_reg_rtx (mode);
2512   mips_emit_binary (code, reg, op0, op1);
2513   return reg;
2514 }
2515 
2516 /* Copy VALUE to a register and return that register.  If new pseudos
2517    are allowed, copy it into a new register, otherwise use DEST.  */
2518 
2519 static rtx
2520 mips_force_temporary (rtx dest, rtx value)
2521 {
2522   if (can_create_pseudo_p ())
2523     return force_reg (Pmode, value);
2524   else
2525     {
2526       mips_emit_move (dest, value);
2527       return dest;
2528     }
2529 }
2530 
2531 /* Emit a call sequence with call pattern PATTERN and return the call
2532    instruction itself (which is not necessarily the last instruction
2533    emitted).  ORIG_ADDR is the original, unlegitimized address,
2534    ADDR is the legitimized form, and LAZY_P is true if the call
2535    address is lazily-bound.  */
2536 
2537 static rtx
2538 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2539 {
2540   rtx insn, reg;
2541 
2542   insn = emit_call_insn (pattern);
2543 
2544   if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2545     {
2546       /* MIPS16 JALRs only take MIPS16 registers.  If the target
2547 	 function requires $25 to be valid on entry, we must copy it
2548 	 there separately.  The move instruction can be put in the
2549 	 call's delay slot.  */
2550       reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2551       emit_insn_before (gen_move_insn (reg, addr), insn);
2552       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2553     }
2554 
2555   if (lazy_p)
2556     /* Lazy-binding stubs require $gp to be valid on entry.  */
2557     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2558 
2559   if (TARGET_USE_GOT)
2560     {
2561       /* See the comment above load_call<mode> for details.  */
2562       use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2563 	       gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2564       emit_insn (gen_update_got_version ());
2565     }
2566   return insn;
2567 }
2568 
2569 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2570    then add CONST_INT OFFSET to the result.  */
2571 
2572 static rtx
2573 mips_unspec_address_offset (rtx base, rtx offset,
2574 			    enum mips_symbol_type symbol_type)
2575 {
2576   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2577 			 UNSPEC_ADDRESS_FIRST + symbol_type);
2578   if (offset != const0_rtx)
2579     base = gen_rtx_PLUS (Pmode, base, offset);
2580   return gen_rtx_CONST (Pmode, base);
2581 }
2582 
2583 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2584    type SYMBOL_TYPE.  */
2585 
2586 rtx
2587 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2588 {
2589   rtx base, offset;
2590 
2591   split_const (address, &base, &offset);
2592   return mips_unspec_address_offset (base, offset, symbol_type);
2593 }
2594 
2595 /* If OP is an UNSPEC address, return the address to which it refers,
2596    otherwise return OP itself.  */
2597 
2598 rtx
2599 mips_strip_unspec_address (rtx op)
2600 {
2601   rtx base, offset;
2602 
2603   split_const (op, &base, &offset);
2604   if (UNSPEC_ADDRESS_P (base))
2605     op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2606   return op;
2607 }
2608 
2609 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2610    high part to BASE and return the result.  Just return BASE otherwise.
2611    TEMP is as for mips_force_temporary.
2612 
2613    The returned expression can be used as the first operand to a LO_SUM.  */
2614 
2615 static rtx
2616 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2617 			 enum mips_symbol_type symbol_type)
2618 {
2619   if (mips_split_p[symbol_type])
2620     {
2621       addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2622       addr = mips_force_temporary (temp, addr);
2623       base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2624     }
2625   return base;
2626 }
2627 
2628 /* Return an instruction that copies $gp into register REG.  We want
2629    GCC to treat the register's value as constant, so that its value
2630    can be rematerialized on demand.  */
2631 
2632 static rtx
2633 gen_load_const_gp (rtx reg)
2634 {
2635   return PMODE_INSN (gen_load_const_gp, (reg));
2636 }
2637 
2638 /* Return a pseudo register that contains the value of $gp throughout
2639    the current function.  Such registers are needed by MIPS16 functions,
2640    for which $gp itself is not a valid base register or addition operand.  */
2641 
2642 static rtx
2643 mips16_gp_pseudo_reg (void)
2644 {
2645   if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2646     {
2647       rtx insn, scan;
2648 
2649       cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2650 
2651       push_topmost_sequence ();
2652 
2653       scan = get_insns ();
2654       while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2655 	scan = NEXT_INSN (scan);
2656 
2657       insn = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2658       insn = emit_insn_after (insn, scan);
2659       INSN_LOCATION (insn) = 0;
2660 
2661       pop_topmost_sequence ();
2662     }
2663 
2664   return cfun->machine->mips16_gp_pseudo_rtx;
2665 }
2666 
2667 /* Return a base register that holds pic_offset_table_rtx.
2668    TEMP, if nonnull, is a scratch Pmode base register.  */
2669 
2670 rtx
2671 mips_pic_base_register (rtx temp)
2672 {
2673   if (!TARGET_MIPS16)
2674     return pic_offset_table_rtx;
2675 
2676   if (currently_expanding_to_rtl)
2677     return mips16_gp_pseudo_reg ();
2678 
2679   if (can_create_pseudo_p ())
2680     temp = gen_reg_rtx (Pmode);
2681 
2682   if (TARGET_USE_GOT)
2683     /* The first post-reload split exposes all references to $gp
2684        (both uses and definitions).  All references must remain
2685        explicit after that point.
2686 
2687        It is safe to introduce uses of $gp at any time, so for
2688        simplicity, we do that before the split too.  */
2689     mips_emit_move (temp, pic_offset_table_rtx);
2690   else
2691     emit_insn (gen_load_const_gp (temp));
2692   return temp;
2693 }
2694 
2695 /* Return the RHS of a load_call<mode> insn.  */
2696 
2697 static rtx
2698 mips_unspec_call (rtx reg, rtx symbol)
2699 {
2700   rtvec vec;
2701 
2702   vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2703   return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2704 }
2705 
2706 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2707    reference.  Return NULL_RTX otherwise.  */
2708 
2709 static rtx
2710 mips_strip_unspec_call (rtx src)
2711 {
2712   if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
2713     return mips_strip_unspec_address (XVECEXP (src, 0, 1));
2714   return NULL_RTX;
2715 }
2716 
2717 /* Create and return a GOT reference of type TYPE for address ADDR.
2718    TEMP, if nonnull, is a scratch Pmode base register.  */
2719 
2720 rtx
2721 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
2722 {
2723   rtx base, high, lo_sum_symbol;
2724 
2725   base = mips_pic_base_register (temp);
2726 
2727   /* If we used the temporary register to load $gp, we can't use
2728      it for the high part as well.  */
2729   if (temp != NULL && reg_overlap_mentioned_p (base, temp))
2730     temp = NULL;
2731 
2732   high = mips_unspec_offset_high (temp, base, addr, type);
2733   lo_sum_symbol = mips_unspec_address (addr, type);
2734 
2735   if (type == SYMBOL_GOTOFF_CALL)
2736     return mips_unspec_call (high, lo_sum_symbol);
2737   else
2738     return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
2739 }
2740 
2741 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
2742    it appears in a MEM of that mode.  Return true if ADDR is a legitimate
2743    constant in that context and can be split into high and low parts.
2744    If so, and if LOW_OUT is nonnull, emit the high part and store the
2745    low part in *LOW_OUT.  Leave *LOW_OUT unchanged otherwise.
2746 
2747    TEMP is as for mips_force_temporary and is used to load the high
2748    part into a register.
2749 
2750    When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
2751    a legitimize SET_SRC for an .md pattern, otherwise the low part
2752    is guaranteed to be a legitimate address for mode MODE.  */
2753 
2754 bool
2755 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
2756 {
2757   enum mips_symbol_context context;
2758   enum mips_symbol_type symbol_type;
2759   rtx high;
2760 
2761   context = (mode == MAX_MACHINE_MODE
2762 	     ? SYMBOL_CONTEXT_LEA
2763 	     : SYMBOL_CONTEXT_MEM);
2764   if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
2765     {
2766       addr = XEXP (addr, 0);
2767       if (mips_symbolic_constant_p (addr, context, &symbol_type)
2768 	  && mips_symbol_insns (symbol_type, mode) > 0
2769 	  && mips_split_hi_p[symbol_type])
2770 	{
2771 	  if (low_out)
2772 	    switch (symbol_type)
2773 	      {
2774 	      case SYMBOL_GOT_PAGE_OFST:
2775 		/* The high part of a page/ofst pair is loaded from the GOT.  */
2776 		*low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
2777 		break;
2778 
2779 	      default:
2780 		gcc_unreachable ();
2781 	      }
2782 	  return true;
2783 	}
2784     }
2785   else
2786     {
2787       if (mips_symbolic_constant_p (addr, context, &symbol_type)
2788 	  && mips_symbol_insns (symbol_type, mode) > 0
2789 	  && mips_split_p[symbol_type])
2790 	{
2791 	  if (low_out)
2792 	    switch (symbol_type)
2793 	      {
2794 	      case SYMBOL_GOT_DISP:
2795 		/* SYMBOL_GOT_DISP symbols are loaded from the GOT.  */
2796 		*low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
2797 		break;
2798 
2799 	      case SYMBOL_GP_RELATIVE:
2800 		high = mips_pic_base_register (temp);
2801 		*low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2802 		break;
2803 
2804 	      default:
2805 		high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
2806 		high = mips_force_temporary (temp, high);
2807 		*low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2808 		break;
2809 	      }
2810 	  return true;
2811 	}
2812     }
2813   return false;
2814 }
2815 
2816 /* Return a legitimate address for REG + OFFSET.  TEMP is as for
2817    mips_force_temporary; it is only needed when OFFSET is not a
2818    SMALL_OPERAND.  */
2819 
2820 static rtx
2821 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2822 {
2823   if (!SMALL_OPERAND (offset))
2824     {
2825       rtx high;
2826 
2827       if (TARGET_MIPS16)
2828 	{
2829 	  /* Load the full offset into a register so that we can use
2830 	     an unextended instruction for the address itself.  */
2831 	  high = GEN_INT (offset);
2832 	  offset = 0;
2833 	}
2834       else
2835 	{
2836 	  /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
2837 	     The addition inside the macro CONST_HIGH_PART may cause an
2838 	     overflow, so we need to force a sign-extension check.  */
2839 	  high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
2840 	  offset = CONST_LOW_PART (offset);
2841 	}
2842       high = mips_force_temporary (temp, high);
2843       reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
2844     }
2845   return plus_constant (Pmode, reg, offset);
2846 }
2847 
2848 /* The __tls_get_attr symbol.  */
2849 static GTY(()) rtx mips_tls_symbol;
2850 
2851 /* Return an instruction sequence that calls __tls_get_addr.  SYM is
2852    the TLS symbol we are referencing and TYPE is the symbol type to use
2853    (either global dynamic or local dynamic).  V0 is an RTX for the
2854    return value location.  */
2855 
2856 static rtx
2857 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
2858 {
2859   rtx insn, loc, a0;
2860 
2861   a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
2862 
2863   if (!mips_tls_symbol)
2864     mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
2865 
2866   loc = mips_unspec_address (sym, type);
2867 
2868   start_sequence ();
2869 
2870   emit_insn (gen_rtx_SET (Pmode, a0,
2871 			  gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
2872   insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
2873 			   const0_rtx, NULL_RTX, false);
2874   RTL_CONST_CALL_P (insn) = 1;
2875   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
2876   insn = get_insns ();
2877 
2878   end_sequence ();
2879 
2880   return insn;
2881 }
2882 
2883 /* Return a pseudo register that contains the current thread pointer.  */
2884 
2885 rtx
2886 mips_expand_thread_pointer (rtx tp)
2887 {
2888   rtx fn;
2889 
2890   if (TARGET_MIPS16)
2891     {
2892       mips_need_mips16_rdhwr_p = true;
2893       fn = mips16_stub_function ("__mips16_rdhwr");
2894       SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
2895       if (!call_insn_operand (fn, VOIDmode))
2896 	fn = force_reg (Pmode, fn);
2897       emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
2898     }
2899   else
2900     emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
2901   return tp;
2902 }
2903 
2904 static rtx
2905 mips_get_tp (void)
2906 {
2907   return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
2908 }
2909 
2910 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
2911    its address.  The return value will be both a valid address and a valid
2912    SET_SRC (either a REG or a LO_SUM).  */
2913 
2914 static rtx
2915 mips_legitimize_tls_address (rtx loc)
2916 {
2917   rtx dest, insn, v0, tp, tmp1, tmp2, eqv, offset;
2918   enum tls_model model;
2919 
2920   model = SYMBOL_REF_TLS_MODEL (loc);
2921   /* Only TARGET_ABICALLS code can have more than one module; other
2922      code must be be static and should not use a GOT.  All TLS models
2923      reduce to local exec in this situation.  */
2924   if (!TARGET_ABICALLS)
2925     model = TLS_MODEL_LOCAL_EXEC;
2926 
2927   switch (model)
2928     {
2929     case TLS_MODEL_GLOBAL_DYNAMIC:
2930       v0 = gen_rtx_REG (Pmode, GP_RETURN);
2931       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
2932       dest = gen_reg_rtx (Pmode);
2933       emit_libcall_block (insn, dest, v0, loc);
2934       break;
2935 
2936     case TLS_MODEL_LOCAL_DYNAMIC:
2937       v0 = gen_rtx_REG (Pmode, GP_RETURN);
2938       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
2939       tmp1 = gen_reg_rtx (Pmode);
2940 
2941       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2942 	 share the LDM result with other LD model accesses.  */
2943       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2944 			    UNSPEC_TLS_LDM);
2945       emit_libcall_block (insn, tmp1, v0, eqv);
2946 
2947       offset = mips_unspec_address (loc, SYMBOL_DTPREL);
2948       if (mips_split_p[SYMBOL_DTPREL])
2949 	{
2950 	  tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
2951 	  dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
2952 	}
2953       else
2954 	dest = expand_binop (Pmode, add_optab, tmp1, offset,
2955 			     0, 0, OPTAB_DIRECT);
2956       break;
2957 
2958     case TLS_MODEL_INITIAL_EXEC:
2959       tp = mips_get_tp ();
2960       tmp1 = gen_reg_rtx (Pmode);
2961       tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
2962       if (Pmode == DImode)
2963 	emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
2964       else
2965 	emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
2966       dest = gen_reg_rtx (Pmode);
2967       emit_insn (gen_add3_insn (dest, tmp1, tp));
2968       break;
2969 
2970     case TLS_MODEL_LOCAL_EXEC:
2971       tmp1 = mips_get_tp ();
2972       offset = mips_unspec_address (loc, SYMBOL_TPREL);
2973       if (mips_split_p[SYMBOL_TPREL])
2974 	{
2975 	  tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
2976 	  dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
2977 	}
2978       else
2979 	dest = expand_binop (Pmode, add_optab, tmp1, offset,
2980 			     0, 0, OPTAB_DIRECT);
2981       break;
2982 
2983     default:
2984       gcc_unreachable ();
2985     }
2986   return dest;
2987 }
2988 
2989 /* If X is not a valid address for mode MODE, force it into a register.  */
2990 
2991 static rtx
2992 mips_force_address (rtx x, enum machine_mode mode)
2993 {
2994   if (!mips_legitimate_address_p (mode, x, false))
2995     x = force_reg (Pmode, x);
2996   return x;
2997 }
2998 
2999 /* This function is used to implement LEGITIMIZE_ADDRESS.  If X can
3000    be legitimized in a way that the generic machinery might not expect,
3001    return a new address, otherwise return NULL.  MODE is the mode of
3002    the memory being accessed.  */
3003 
3004 static rtx
3005 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3006 			 enum machine_mode mode)
3007 {
3008   rtx base, addr;
3009   HOST_WIDE_INT offset;
3010 
3011   if (mips_tls_symbol_p (x))
3012     return mips_legitimize_tls_address (x);
3013 
3014   /* See if the address can split into a high part and a LO_SUM.  */
3015   if (mips_split_symbol (NULL, x, mode, &addr))
3016     return mips_force_address (addr, mode);
3017 
3018   /* Handle BASE + OFFSET using mips_add_offset.  */
3019   mips_split_plus (x, &base, &offset);
3020   if (offset != 0)
3021     {
3022       if (!mips_valid_base_register_p (base, mode, false))
3023 	base = copy_to_mode_reg (Pmode, base);
3024       addr = mips_add_offset (NULL, base, offset);
3025       return mips_force_address (addr, mode);
3026     }
3027 
3028   return x;
3029 }
3030 
3031 /* Load VALUE into DEST.  TEMP is as for mips_force_temporary.  */
3032 
3033 void
3034 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3035 {
3036   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3037   enum machine_mode mode;
3038   unsigned int i, num_ops;
3039   rtx x;
3040 
3041   mode = GET_MODE (dest);
3042   num_ops = mips_build_integer (codes, value);
3043 
3044   /* Apply each binary operation to X.  Invariant: X is a legitimate
3045      source operand for a SET pattern.  */
3046   x = GEN_INT (codes[0].value);
3047   for (i = 1; i < num_ops; i++)
3048     {
3049       if (!can_create_pseudo_p ())
3050 	{
3051 	  emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3052 	  x = temp;
3053 	}
3054       else
3055 	x = force_reg (mode, x);
3056       x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3057     }
3058 
3059   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3060 }
3061 
3062 /* Subroutine of mips_legitimize_move.  Move constant SRC into register
3063    DEST given that SRC satisfies immediate_operand but doesn't satisfy
3064    move_operand.  */
3065 
3066 static void
3067 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
3068 {
3069   rtx base, offset;
3070 
3071   /* Split moves of big integers into smaller pieces.  */
3072   if (splittable_const_int_operand (src, mode))
3073     {
3074       mips_move_integer (dest, dest, INTVAL (src));
3075       return;
3076     }
3077 
3078   /* Split moves of symbolic constants into high/low pairs.  */
3079   if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3080     {
3081       emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3082       return;
3083     }
3084 
3085   /* Generate the appropriate access sequences for TLS symbols.  */
3086   if (mips_tls_symbol_p (src))
3087     {
3088       mips_emit_move (dest, mips_legitimize_tls_address (src));
3089       return;
3090     }
3091 
3092   /* If we have (const (plus symbol offset)), and that expression cannot
3093      be forced into memory, load the symbol first and add in the offset.
3094      In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3095      forced into memory, as it usually produces better code.  */
3096   split_const (src, &base, &offset);
3097   if (offset != const0_rtx
3098       && (targetm.cannot_force_const_mem (mode, src)
3099 	  || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3100     {
3101       base = mips_force_temporary (dest, base);
3102       mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3103       return;
3104     }
3105 
3106   src = force_const_mem (mode, src);
3107 
3108   /* When using explicit relocs, constant pool references are sometimes
3109      not legitimate addresses.  */
3110   mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3111   mips_emit_move (dest, src);
3112 }
3113 
3114 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3115    sequence that is valid.  */
3116 
3117 bool
3118 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
3119 {
3120   if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3121     {
3122       mips_emit_move (dest, force_reg (mode, src));
3123       return true;
3124     }
3125 
3126   /* We need to deal with constants that would be legitimate
3127      immediate_operands but aren't legitimate move_operands.  */
3128   if (CONSTANT_P (src) && !move_operand (src, mode))
3129     {
3130       mips_legitimize_const_move (mode, dest, src);
3131       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3132       return true;
3133     }
3134   return false;
3135 }
3136 
3137 /* Return true if value X in context CONTEXT is a small-data address
3138    that can be rewritten as a LO_SUM.  */
3139 
3140 static bool
3141 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3142 {
3143   enum mips_symbol_type symbol_type;
3144 
3145   return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3146 	  && !mips_split_p[SYMBOL_GP_RELATIVE]
3147 	  && mips_symbolic_constant_p (x, context, &symbol_type)
3148 	  && symbol_type == SYMBOL_GP_RELATIVE);
3149 }
3150 
3151 /* A for_each_rtx callback for mips_small_data_pattern_p.  DATA is the
3152    containing MEM, or null if none.  */
3153 
3154 static int
3155 mips_small_data_pattern_1 (rtx *loc, void *data)
3156 {
3157   enum mips_symbol_context context;
3158 
3159   /* Ignore things like "g" constraints in asms.  We make no particular
3160      guarantee about which symbolic constants are acceptable as asm operands
3161      versus which must be forced into a GPR.  */
3162   if (GET_CODE (*loc) == LO_SUM || GET_CODE (*loc) == ASM_OPERANDS)
3163     return -1;
3164 
3165   if (MEM_P (*loc))
3166     {
3167       if (for_each_rtx (&XEXP (*loc, 0), mips_small_data_pattern_1, *loc))
3168 	return 1;
3169       return -1;
3170     }
3171 
3172   context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3173   return mips_rewrite_small_data_p (*loc, context);
3174 }
3175 
3176 /* Return true if OP refers to small data symbols directly, not through
3177    a LO_SUM.  */
3178 
3179 bool
3180 mips_small_data_pattern_p (rtx op)
3181 {
3182   return for_each_rtx (&op, mips_small_data_pattern_1, NULL);
3183 }
3184 
3185 /* A for_each_rtx callback, used by mips_rewrite_small_data.
3186    DATA is the containing MEM, or null if none.  */
3187 
3188 static int
3189 mips_rewrite_small_data_1 (rtx *loc, void *data)
3190 {
3191   enum mips_symbol_context context;
3192 
3193   if (MEM_P (*loc))
3194     {
3195       for_each_rtx (&XEXP (*loc, 0), mips_rewrite_small_data_1, *loc);
3196       return -1;
3197     }
3198 
3199   context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3200   if (mips_rewrite_small_data_p (*loc, context))
3201     *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3202 
3203   if (GET_CODE (*loc) == LO_SUM)
3204     return -1;
3205 
3206   return 0;
3207 }
3208 
3209 /* Rewrite instruction pattern PATTERN so that it refers to small data
3210    using explicit relocations.  */
3211 
3212 rtx
3213 mips_rewrite_small_data (rtx pattern)
3214 {
3215   pattern = copy_insn (pattern);
3216   for_each_rtx (&pattern, mips_rewrite_small_data_1, NULL);
3217   return pattern;
3218 }
3219 
3220 /* We need a lot of little routines to check the range of MIPS16 immediate
3221    operands.  */
3222 
3223 static int
3224 m16_check_op (rtx op, int low, int high, int mask)
3225 {
3226   return (CONST_INT_P (op)
3227 	  && IN_RANGE (INTVAL (op), low, high)
3228 	  && (INTVAL (op) & mask) == 0);
3229 }
3230 
3231 int
3232 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3233 {
3234   return m16_check_op (op, 0x1, 0x8, 0);
3235 }
3236 
3237 int
3238 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3239 {
3240   return m16_check_op (op, -0x8, 0x7, 0);
3241 }
3242 
3243 int
3244 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3245 {
3246   return m16_check_op (op, -0x7, 0x8, 0);
3247 }
3248 
3249 int
3250 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3251 {
3252   return m16_check_op (op, -0x10, 0xf, 0);
3253 }
3254 
3255 int
3256 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3257 {
3258   return m16_check_op (op, -0xf, 0x10, 0);
3259 }
3260 
3261 int
3262 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3263 {
3264   return m16_check_op (op, -0x10 << 2, 0xf << 2, 3);
3265 }
3266 
3267 int
3268 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3269 {
3270   return m16_check_op (op, -0xf << 2, 0x10 << 2, 3);
3271 }
3272 
3273 int
3274 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3275 {
3276   return m16_check_op (op, -0x80, 0x7f, 0);
3277 }
3278 
3279 int
3280 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3281 {
3282   return m16_check_op (op, -0x7f, 0x80, 0);
3283 }
3284 
3285 int
3286 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3287 {
3288   return m16_check_op (op, 0x0, 0xff, 0);
3289 }
3290 
3291 int
3292 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3293 {
3294   return m16_check_op (op, -0xff, 0x0, 0);
3295 }
3296 
3297 int
3298 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3299 {
3300   return m16_check_op (op, -0x1, 0xfe, 0);
3301 }
3302 
3303 int
3304 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3305 {
3306   return m16_check_op (op, 0x0, 0xff << 2, 3);
3307 }
3308 
3309 int
3310 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3311 {
3312   return m16_check_op (op, -0xff << 2, 0x0, 3);
3313 }
3314 
3315 int
3316 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3317 {
3318   return m16_check_op (op, -0x80 << 3, 0x7f << 3, 7);
3319 }
3320 
3321 int
3322 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3323 {
3324   return m16_check_op (op, -0x7f << 3, 0x80 << 3, 7);
3325 }
3326 
3327 /* The cost of loading values from the constant pool.  It should be
3328    larger than the cost of any constant we want to synthesize inline.  */
3329 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3330 
3331 /* Return the cost of X when used as an operand to the MIPS16 instruction
3332    that implements CODE.  Return -1 if there is no such instruction, or if
3333    X is not a valid immediate operand for it.  */
3334 
3335 static int
3336 mips16_constant_cost (int code, HOST_WIDE_INT x)
3337 {
3338   switch (code)
3339     {
3340     case ASHIFT:
3341     case ASHIFTRT:
3342     case LSHIFTRT:
3343       /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3344 	 other shifts are extended.  The shift patterns truncate the shift
3345 	 count to the right size, so there are no out-of-range values.  */
3346       if (IN_RANGE (x, 1, 8))
3347 	return 0;
3348       return COSTS_N_INSNS (1);
3349 
3350     case PLUS:
3351       if (IN_RANGE (x, -128, 127))
3352 	return 0;
3353       if (SMALL_OPERAND (x))
3354 	return COSTS_N_INSNS (1);
3355       return -1;
3356 
3357     case LEU:
3358       /* Like LE, but reject the always-true case.  */
3359       if (x == -1)
3360 	return -1;
3361     case LE:
3362       /* We add 1 to the immediate and use SLT.  */
3363       x += 1;
3364     case XOR:
3365       /* We can use CMPI for an xor with an unsigned 16-bit X.  */
3366     case LT:
3367     case LTU:
3368       if (IN_RANGE (x, 0, 255))
3369 	return 0;
3370       if (SMALL_OPERAND_UNSIGNED (x))
3371 	return COSTS_N_INSNS (1);
3372       return -1;
3373 
3374     case EQ:
3375     case NE:
3376       /* Equality comparisons with 0 are cheap.  */
3377       if (x == 0)
3378 	return 0;
3379       return -1;
3380 
3381     default:
3382       return -1;
3383     }
3384 }
3385 
3386 /* Return true if there is a non-MIPS16 instruction that implements CODE
3387    and if that instruction accepts X as an immediate operand.  */
3388 
3389 static int
3390 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3391 {
3392   switch (code)
3393     {
3394     case ASHIFT:
3395     case ASHIFTRT:
3396     case LSHIFTRT:
3397       /* All shift counts are truncated to a valid constant.  */
3398       return true;
3399 
3400     case ROTATE:
3401     case ROTATERT:
3402       /* Likewise rotates, if the target supports rotates at all.  */
3403       return ISA_HAS_ROR;
3404 
3405     case AND:
3406     case IOR:
3407     case XOR:
3408       /* These instructions take 16-bit unsigned immediates.  */
3409       return SMALL_OPERAND_UNSIGNED (x);
3410 
3411     case PLUS:
3412     case LT:
3413     case LTU:
3414       /* These instructions take 16-bit signed immediates.  */
3415       return SMALL_OPERAND (x);
3416 
3417     case EQ:
3418     case NE:
3419     case GT:
3420     case GTU:
3421       /* The "immediate" forms of these instructions are really
3422 	 implemented as comparisons with register 0.  */
3423       return x == 0;
3424 
3425     case GE:
3426     case GEU:
3427       /* Likewise, meaning that the only valid immediate operand is 1.  */
3428       return x == 1;
3429 
3430     case LE:
3431       /* We add 1 to the immediate and use SLT.  */
3432       return SMALL_OPERAND (x + 1);
3433 
3434     case LEU:
3435       /* Likewise SLTU, but reject the always-true case.  */
3436       return SMALL_OPERAND (x + 1) && x + 1 != 0;
3437 
3438     case SIGN_EXTRACT:
3439     case ZERO_EXTRACT:
3440       /* The bit position and size are immediate operands.  */
3441       return ISA_HAS_EXT_INS;
3442 
3443     default:
3444       /* By default assume that $0 can be used for 0.  */
3445       return x == 0;
3446     }
3447 }
3448 
3449 /* Return the cost of binary operation X, given that the instruction
3450    sequence for a word-sized or smaller operation has cost SINGLE_COST
3451    and that the sequence of a double-word operation has cost DOUBLE_COST.
3452    If SPEED is true, optimize for speed otherwise optimize for size.  */
3453 
3454 static int
3455 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3456 {
3457   int cost;
3458 
3459   if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3460     cost = double_cost;
3461   else
3462     cost = single_cost;
3463   return (cost
3464 	  + set_src_cost (XEXP (x, 0), speed)
3465 	  + rtx_cost (XEXP (x, 1), GET_CODE (x), 1, speed));
3466 }
3467 
3468 /* Return the cost of floating-point multiplications of mode MODE.  */
3469 
3470 static int
3471 mips_fp_mult_cost (enum machine_mode mode)
3472 {
3473   return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3474 }
3475 
3476 /* Return the cost of floating-point divisions of mode MODE.  */
3477 
3478 static int
3479 mips_fp_div_cost (enum machine_mode mode)
3480 {
3481   return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3482 }
3483 
3484 /* Return the cost of sign-extending OP to mode MODE, not including the
3485    cost of OP itself.  */
3486 
3487 static int
3488 mips_sign_extend_cost (enum machine_mode mode, rtx op)
3489 {
3490   if (MEM_P (op))
3491     /* Extended loads are as cheap as unextended ones.  */
3492     return 0;
3493 
3494   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3495     /* A sign extension from SImode to DImode in 64-bit mode is free.  */
3496     return 0;
3497 
3498   if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3499     /* We can use SEB or SEH.  */
3500     return COSTS_N_INSNS (1);
3501 
3502   /* We need to use a shift left and a shift right.  */
3503   return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3504 }
3505 
3506 /* Return the cost of zero-extending OP to mode MODE, not including the
3507    cost of OP itself.  */
3508 
3509 static int
3510 mips_zero_extend_cost (enum machine_mode mode, rtx op)
3511 {
3512   if (MEM_P (op))
3513     /* Extended loads are as cheap as unextended ones.  */
3514     return 0;
3515 
3516   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3517     /* We need a shift left by 32 bits and a shift right by 32 bits.  */
3518     return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3519 
3520   if (GENERATE_MIPS16E)
3521     /* We can use ZEB or ZEH.  */
3522     return COSTS_N_INSNS (1);
3523 
3524   if (TARGET_MIPS16)
3525     /* We need to load 0xff or 0xffff into a register and use AND.  */
3526     return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3527 
3528   /* We can use ANDI.  */
3529   return COSTS_N_INSNS (1);
3530 }
3531 
3532 /* Return the cost of moving between two registers of mode MODE,
3533    assuming that the move will be in pieces of at most UNITS bytes.  */
3534 
3535 static int
3536 mips_set_reg_reg_piece_cost (enum machine_mode mode, unsigned int units)
3537 {
3538   return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3539 }
3540 
3541 /* Return the cost of moving between two registers of mode MODE.  */
3542 
3543 static int
3544 mips_set_reg_reg_cost (enum machine_mode mode)
3545 {
3546   switch (GET_MODE_CLASS (mode))
3547     {
3548     case MODE_CC:
3549       return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3550 
3551     case MODE_FLOAT:
3552     case MODE_COMPLEX_FLOAT:
3553     case MODE_VECTOR_FLOAT:
3554       if (TARGET_HARD_FLOAT)
3555 	return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3556       /* Fall through */
3557 
3558     default:
3559       return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3560     }
3561 }
3562 
3563 /* Implement TARGET_RTX_COSTS.  */
3564 
3565 static bool
3566 mips_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3567 		int *total, bool speed)
3568 {
3569   enum machine_mode mode = GET_MODE (x);
3570   bool float_mode_p = FLOAT_MODE_P (mode);
3571   int cost;
3572   rtx addr;
3573 
3574   /* The cost of a COMPARE is hard to define for MIPS.  COMPAREs don't
3575      appear in the instruction stream, and the cost of a comparison is
3576      really the cost of the branch or scc condition.  At the time of
3577      writing, GCC only uses an explicit outer COMPARE code when optabs
3578      is testing whether a constant is expensive enough to force into a
3579      register.  We want optabs to pass such constants through the MIPS
3580      expanders instead, so make all constants very cheap here.  */
3581   if (outer_code == COMPARE)
3582     {
3583       gcc_assert (CONSTANT_P (x));
3584       *total = 0;
3585       return true;
3586     }
3587 
3588   switch (code)
3589     {
3590     case CONST_INT:
3591       /* Treat *clear_upper32-style ANDs as having zero cost in the
3592 	 second operand.  The cost is entirely in the first operand.
3593 
3594 	 ??? This is needed because we would otherwise try to CSE
3595 	 the constant operand.  Although that's the right thing for
3596 	 instructions that continue to be a register operation throughout
3597 	 compilation, it is disastrous for instructions that could
3598 	 later be converted into a memory operation.  */
3599       if (TARGET_64BIT
3600 	  && outer_code == AND
3601 	  && UINTVAL (x) == 0xffffffff)
3602 	{
3603 	  *total = 0;
3604 	  return true;
3605 	}
3606 
3607       if (TARGET_MIPS16)
3608 	{
3609 	  cost = mips16_constant_cost (outer_code, INTVAL (x));
3610 	  if (cost >= 0)
3611 	    {
3612 	      *total = cost;
3613 	      return true;
3614 	    }
3615 	}
3616       else
3617 	{
3618 	  /* When not optimizing for size, we care more about the cost
3619 	     of hot code, and hot code is often in a loop.  If a constant
3620 	     operand needs to be forced into a register, we will often be
3621 	     able to hoist the constant load out of the loop, so the load
3622 	     should not contribute to the cost.  */
3623 	  if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3624 	    {
3625 	      *total = 0;
3626 	      return true;
3627 	    }
3628 	}
3629       /* Fall through.  */
3630 
3631     case CONST:
3632     case SYMBOL_REF:
3633     case LABEL_REF:
3634     case CONST_DOUBLE:
3635       if (force_to_mem_operand (x, VOIDmode))
3636 	{
3637 	  *total = COSTS_N_INSNS (1);
3638 	  return true;
3639 	}
3640       cost = mips_const_insns (x);
3641       if (cost > 0)
3642 	{
3643 	  /* If the constant is likely to be stored in a GPR, SETs of
3644 	     single-insn constants are as cheap as register sets; we
3645 	     never want to CSE them.
3646 
3647 	     Don't reduce the cost of storing a floating-point zero in
3648 	     FPRs.  If we have a zero in an FPR for other reasons, we
3649 	     can get better cfg-cleanup and delayed-branch results by
3650 	     using it consistently, rather than using $0 sometimes and
3651 	     an FPR at other times.  Also, moves between floating-point
3652 	     registers are sometimes cheaper than (D)MTC1 $0.  */
3653 	  if (cost == 1
3654 	      && outer_code == SET
3655 	      && !(float_mode_p && TARGET_HARD_FLOAT))
3656 	    cost = 0;
3657 	  /* When non-MIPS16 code loads a constant N>1 times, we rarely
3658 	     want to CSE the constant itself.  It is usually better to
3659 	     have N copies of the last operation in the sequence and one
3660 	     shared copy of the other operations.  (Note that this is
3661 	     not true for MIPS16 code, where the final operation in the
3662 	     sequence is often an extended instruction.)
3663 
3664 	     Also, if we have a CONST_INT, we don't know whether it is
3665 	     for a word or doubleword operation, so we cannot rely on
3666 	     the result of mips_build_integer.  */
3667 	  else if (!TARGET_MIPS16
3668 		   && (outer_code == SET || mode == VOIDmode))
3669 	    cost = 1;
3670 	  *total = COSTS_N_INSNS (cost);
3671 	  return true;
3672 	}
3673       /* The value will need to be fetched from the constant pool.  */
3674       *total = CONSTANT_POOL_COST;
3675       return true;
3676 
3677     case MEM:
3678       /* If the address is legitimate, return the number of
3679 	 instructions it needs.  */
3680       addr = XEXP (x, 0);
3681       cost = mips_address_insns (addr, mode, true);
3682       if (cost > 0)
3683 	{
3684 	  *total = COSTS_N_INSNS (cost + 1);
3685 	  return true;
3686 	}
3687       /* Check for a scaled indexed address.  */
3688       if (mips_lwxs_address_p (addr)
3689 	  || mips_lx_address_p (addr, mode))
3690 	{
3691 	  *total = COSTS_N_INSNS (2);
3692 	  return true;
3693 	}
3694       /* Otherwise use the default handling.  */
3695       return false;
3696 
3697     case FFS:
3698       *total = COSTS_N_INSNS (6);
3699       return false;
3700 
3701     case NOT:
3702       *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3703       return false;
3704 
3705     case AND:
3706       /* Check for a *clear_upper32 pattern and treat it like a zero
3707 	 extension.  See the pattern's comment for details.  */
3708       if (TARGET_64BIT
3709 	  && mode == DImode
3710 	  && CONST_INT_P (XEXP (x, 1))
3711 	  && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3712 	{
3713 	  *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3714 		    + set_src_cost (XEXP (x, 0), speed));
3715 	  return true;
3716 	}
3717       if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
3718 	{
3719 	  rtx op = XEXP (x, 0);
3720 	  if (GET_CODE (op) == ASHIFT
3721 	      && CONST_INT_P (XEXP (op, 1))
3722 	      && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
3723 	    {
3724 	      *total = COSTS_N_INSNS (1) + set_src_cost (XEXP (op, 0), speed);
3725 	      return true;
3726 	    }
3727 	}
3728 
3729       /* Fall through.  */
3730 
3731     case IOR:
3732     case XOR:
3733       /* Double-word operations use two single-word operations.  */
3734       *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3735 				 speed);
3736       return true;
3737 
3738     case ASHIFT:
3739     case ASHIFTRT:
3740     case LSHIFTRT:
3741     case ROTATE:
3742     case ROTATERT:
3743       if (CONSTANT_P (XEXP (x, 1)))
3744 	*total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3745 				   speed);
3746       else
3747 	*total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
3748 				   speed);
3749       return true;
3750 
3751     case ABS:
3752       if (float_mode_p)
3753         *total = mips_cost->fp_add;
3754       else
3755         *total = COSTS_N_INSNS (4);
3756       return false;
3757 
3758     case LO_SUM:
3759       /* Low-part immediates need an extended MIPS16 instruction.  */
3760       *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3761 		+ set_src_cost (XEXP (x, 0), speed));
3762       return true;
3763 
3764     case LT:
3765     case LTU:
3766     case LE:
3767     case LEU:
3768     case GT:
3769     case GTU:
3770     case GE:
3771     case GEU:
3772     case EQ:
3773     case NE:
3774     case UNORDERED:
3775     case LTGT:
3776       /* Branch comparisons have VOIDmode, so use the first operand's
3777 	 mode instead.  */
3778       mode = GET_MODE (XEXP (x, 0));
3779       if (FLOAT_MODE_P (mode))
3780 	{
3781 	  *total = mips_cost->fp_add;
3782 	  return false;
3783 	}
3784       *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3785 				 speed);
3786       return true;
3787 
3788     case MINUS:
3789       if (float_mode_p
3790 	  && (ISA_HAS_NMADD4_NMSUB4 (mode) || ISA_HAS_NMADD3_NMSUB3 (mode))
3791 	  && TARGET_FUSED_MADD
3792 	  && !HONOR_NANS (mode)
3793 	  && !HONOR_SIGNED_ZEROS (mode))
3794 	{
3795 	  /* See if we can use NMADD or NMSUB.  See mips.md for the
3796 	     associated patterns.  */
3797 	  rtx op0 = XEXP (x, 0);
3798 	  rtx op1 = XEXP (x, 1);
3799 	  if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
3800 	    {
3801 	      *total = (mips_fp_mult_cost (mode)
3802 			+ set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
3803 			+ set_src_cost (XEXP (op0, 1), speed)
3804 			+ set_src_cost (op1, speed));
3805 	      return true;
3806 	    }
3807 	  if (GET_CODE (op1) == MULT)
3808 	    {
3809 	      *total = (mips_fp_mult_cost (mode)
3810 			+ set_src_cost (op0, speed)
3811 			+ set_src_cost (XEXP (op1, 0), speed)
3812 			+ set_src_cost (XEXP (op1, 1), speed));
3813 	      return true;
3814 	    }
3815 	}
3816       /* Fall through.  */
3817 
3818     case PLUS:
3819       if (float_mode_p)
3820 	{
3821 	  /* If this is part of a MADD or MSUB, treat the PLUS as
3822 	     being free.  */
3823 	  if (ISA_HAS_FP4
3824 	      && TARGET_FUSED_MADD
3825 	      && GET_CODE (XEXP (x, 0)) == MULT)
3826 	    *total = 0;
3827 	  else
3828 	    *total = mips_cost->fp_add;
3829 	  return false;
3830 	}
3831 
3832       /* Double-word operations require three single-word operations and
3833 	 an SLTU.  The MIPS16 version then needs to move the result of
3834 	 the SLTU from $24 to a MIPS16 register.  */
3835       *total = mips_binary_cost (x, COSTS_N_INSNS (1),
3836 				 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
3837 				 speed);
3838       return true;
3839 
3840     case NEG:
3841       if (float_mode_p
3842 	  && (ISA_HAS_NMADD4_NMSUB4 (mode) || ISA_HAS_NMADD3_NMSUB3 (mode))
3843 	  && TARGET_FUSED_MADD
3844 	  && !HONOR_NANS (mode)
3845 	  && HONOR_SIGNED_ZEROS (mode))
3846 	{
3847 	  /* See if we can use NMADD or NMSUB.  See mips.md for the
3848 	     associated patterns.  */
3849 	  rtx op = XEXP (x, 0);
3850 	  if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3851 	      && GET_CODE (XEXP (op, 0)) == MULT)
3852 	    {
3853 	      *total = (mips_fp_mult_cost (mode)
3854 			+ set_src_cost (XEXP (XEXP (op, 0), 0), speed)
3855 			+ set_src_cost (XEXP (XEXP (op, 0), 1), speed)
3856 			+ set_src_cost (XEXP (op, 1), speed));
3857 	      return true;
3858 	    }
3859 	}
3860 
3861       if (float_mode_p)
3862 	*total = mips_cost->fp_add;
3863       else
3864 	*total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
3865       return false;
3866 
3867     case MULT:
3868       if (float_mode_p)
3869 	*total = mips_fp_mult_cost (mode);
3870       else if (mode == DImode && !TARGET_64BIT)
3871 	/* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
3872 	   where the mulsidi3 always includes an MFHI and an MFLO.  */
3873 	*total = (speed
3874 		  ? mips_cost->int_mult_si * 3 + 6
3875 		  : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
3876       else if (!speed)
3877 	*total = COSTS_N_INSNS (ISA_HAS_MUL3 ? 1 : 2);
3878       else if (mode == DImode)
3879 	*total = mips_cost->int_mult_di;
3880       else
3881 	*total = mips_cost->int_mult_si;
3882       return false;
3883 
3884     case DIV:
3885       /* Check for a reciprocal.  */
3886       if (float_mode_p
3887 	  && ISA_HAS_FP4
3888 	  && flag_unsafe_math_optimizations
3889 	  && XEXP (x, 0) == CONST1_RTX (mode))
3890 	{
3891 	  if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
3892 	    /* An rsqrt<mode>a or rsqrt<mode>b pattern.  Count the
3893 	       division as being free.  */
3894 	    *total = set_src_cost (XEXP (x, 1), speed);
3895 	  else
3896 	    *total = (mips_fp_div_cost (mode)
3897 		      + set_src_cost (XEXP (x, 1), speed));
3898 	  return true;
3899 	}
3900       /* Fall through.  */
3901 
3902     case SQRT:
3903     case MOD:
3904       if (float_mode_p)
3905 	{
3906 	  *total = mips_fp_div_cost (mode);
3907 	  return false;
3908 	}
3909       /* Fall through.  */
3910 
3911     case UDIV:
3912     case UMOD:
3913       if (!speed)
3914 	{
3915 	  /* It is our responsibility to make division by a power of 2
3916 	     as cheap as 2 register additions if we want the division
3917 	     expanders to be used for such operations; see the setting
3918 	     of sdiv_pow2_cheap in optabs.c.  Using (D)DIV for MIPS16
3919 	     should always produce shorter code than using
3920 	     expand_sdiv2_pow2.  */
3921 	  if (TARGET_MIPS16
3922 	      && CONST_INT_P (XEXP (x, 1))
3923 	      && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
3924 	    {
3925 	      *total = COSTS_N_INSNS (2) + set_src_cost (XEXP (x, 0), speed);
3926 	      return true;
3927 	    }
3928 	  *total = COSTS_N_INSNS (mips_idiv_insns ());
3929 	}
3930       else if (mode == DImode)
3931         *total = mips_cost->int_div_di;
3932       else
3933 	*total = mips_cost->int_div_si;
3934       return false;
3935 
3936     case SIGN_EXTEND:
3937       *total = mips_sign_extend_cost (mode, XEXP (x, 0));
3938       return false;
3939 
3940     case ZERO_EXTEND:
3941       if (outer_code == SET
3942 	  && ISA_HAS_BADDU
3943 	  && (GET_CODE (XEXP (x, 0)) == TRUNCATE
3944 	      || GET_CODE (XEXP (x, 0)) == SUBREG)
3945 	  && GET_MODE (XEXP (x, 0)) == QImode
3946 	  && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
3947 	{
3948 	  *total = set_src_cost (XEXP (XEXP (x, 0), 0), speed);
3949 	  return true;
3950 	}
3951       *total = mips_zero_extend_cost (mode, XEXP (x, 0));
3952       return false;
3953 
3954     case FLOAT:
3955     case UNSIGNED_FLOAT:
3956     case FIX:
3957     case FLOAT_EXTEND:
3958     case FLOAT_TRUNCATE:
3959       *total = mips_cost->fp_add;
3960       return false;
3961 
3962     case SET:
3963       if (register_operand (SET_DEST (x), VOIDmode)
3964 	  && reg_or_0_operand (SET_SRC (x), VOIDmode))
3965 	{
3966 	  *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
3967 	  return true;
3968 	}
3969       return false;
3970 
3971     default:
3972       return false;
3973     }
3974 }
3975 
3976 /* Implement TARGET_ADDRESS_COST.  */
3977 
3978 static int
3979 mips_address_cost (rtx addr, enum machine_mode mode,
3980 		   addr_space_t as ATTRIBUTE_UNUSED,
3981 		   bool speed ATTRIBUTE_UNUSED)
3982 {
3983   return mips_address_insns (addr, mode, false);
3984 }
3985 
3986 /* Information about a single instruction in a multi-instruction
3987    asm sequence.  */
3988 struct mips_multi_member {
3989   /* True if this is a label, false if it is code.  */
3990   bool is_label_p;
3991 
3992   /* The output_asm_insn format of the instruction.  */
3993   const char *format;
3994 
3995   /* The operands to the instruction.  */
3996   rtx operands[MAX_RECOG_OPERANDS];
3997 };
3998 typedef struct mips_multi_member mips_multi_member;
3999 
4000 /* The instructions that make up the current multi-insn sequence.  */
4001 static vec<mips_multi_member> mips_multi_members;
4002 
4003 /* How many instructions (as opposed to labels) are in the current
4004    multi-insn sequence.  */
4005 static unsigned int mips_multi_num_insns;
4006 
4007 /* Start a new multi-insn sequence.  */
4008 
4009 static void
4010 mips_multi_start (void)
4011 {
4012   mips_multi_members.truncate (0);
4013   mips_multi_num_insns = 0;
4014 }
4015 
4016 /* Add a new, uninitialized member to the current multi-insn sequence.  */
4017 
4018 static struct mips_multi_member *
4019 mips_multi_add (void)
4020 {
4021   mips_multi_member empty;
4022   return mips_multi_members.safe_push (empty);
4023 }
4024 
4025 /* Add a normal insn with the given asm format to the current multi-insn
4026    sequence.  The other arguments are a null-terminated list of operands.  */
4027 
4028 static void
4029 mips_multi_add_insn (const char *format, ...)
4030 {
4031   struct mips_multi_member *member;
4032   va_list ap;
4033   unsigned int i;
4034   rtx op;
4035 
4036   member = mips_multi_add ();
4037   member->is_label_p = false;
4038   member->format = format;
4039   va_start (ap, format);
4040   i = 0;
4041   while ((op = va_arg (ap, rtx)))
4042     member->operands[i++] = op;
4043   va_end (ap);
4044   mips_multi_num_insns++;
4045 }
4046 
4047 /* Add the given label definition to the current multi-insn sequence.
4048    The definition should include the colon.  */
4049 
4050 static void
4051 mips_multi_add_label (const char *label)
4052 {
4053   struct mips_multi_member *member;
4054 
4055   member = mips_multi_add ();
4056   member->is_label_p = true;
4057   member->format = label;
4058 }
4059 
4060 /* Return the index of the last member of the current multi-insn sequence.  */
4061 
4062 static unsigned int
4063 mips_multi_last_index (void)
4064 {
4065   return mips_multi_members.length () - 1;
4066 }
4067 
4068 /* Add a copy of an existing instruction to the current multi-insn
4069    sequence.  I is the index of the instruction that should be copied.  */
4070 
4071 static void
4072 mips_multi_copy_insn (unsigned int i)
4073 {
4074   struct mips_multi_member *member;
4075 
4076   member = mips_multi_add ();
4077   memcpy (member, &mips_multi_members[i], sizeof (*member));
4078   gcc_assert (!member->is_label_p);
4079 }
4080 
4081 /* Change the operand of an existing instruction in the current
4082    multi-insn sequence.  I is the index of the instruction,
4083    OP is the index of the operand, and X is the new value.  */
4084 
4085 static void
4086 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4087 {
4088   mips_multi_members[i].operands[op] = x;
4089 }
4090 
4091 /* Write out the asm code for the current multi-insn sequence.  */
4092 
4093 static void
4094 mips_multi_write (void)
4095 {
4096   struct mips_multi_member *member;
4097   unsigned int i;
4098 
4099   FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4100     if (member->is_label_p)
4101       fprintf (asm_out_file, "%s\n", member->format);
4102     else
4103       output_asm_insn (member->format, member->operands);
4104 }
4105 
4106 /* Return one word of double-word value OP, taking into account the fixed
4107    endianness of certain registers.  HIGH_P is true to select the high part,
4108    false to select the low part.  */
4109 
4110 rtx
4111 mips_subword (rtx op, bool high_p)
4112 {
4113   unsigned int byte, offset;
4114   enum machine_mode mode;
4115 
4116   mode = GET_MODE (op);
4117   if (mode == VOIDmode)
4118     mode = TARGET_64BIT ? TImode : DImode;
4119 
4120   if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4121     byte = UNITS_PER_WORD;
4122   else
4123     byte = 0;
4124 
4125   if (FP_REG_RTX_P (op))
4126     {
4127       /* Paired FPRs are always ordered little-endian.  */
4128       offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4129       return gen_rtx_REG (word_mode, REGNO (op) + offset);
4130     }
4131 
4132   if (MEM_P (op))
4133     return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4134 
4135   return simplify_gen_subreg (word_mode, op, mode, byte);
4136 }
4137 
4138 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4139    SPLIT_TYPE is the condition under which moves should be split.  */
4140 
4141 static bool
4142 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4143 {
4144   return ((split_type != SPLIT_FOR_SPEED
4145 	   || mips_tuning_info.fast_mult_zero_zero_p)
4146 	  && src == const0_rtx
4147 	  && REG_P (dest)
4148 	  && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4149 	  && (ISA_HAS_DSP_MULT
4150 	      ? ACC_REG_P (REGNO (dest))
4151 	      : MD_REG_P (REGNO (dest))));
4152 }
4153 
4154 /* Return true if a move from SRC to DEST should be split into two.
4155    SPLIT_TYPE describes the split condition.  */
4156 
4157 bool
4158 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4159 {
4160   /* Check whether the move can be done using some variant of MULT $0,$0.  */
4161   if (mips_mult_move_p (dest, src, split_type))
4162     return false;
4163 
4164   /* FPR-to-FPR moves can be done in a single instruction, if they're
4165      allowed at all.  */
4166   unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4167   if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4168     return false;
4169 
4170   /* Check for floating-point loads and stores.  */
4171   if (size == 8 && ISA_HAS_LDC1_SDC1)
4172     {
4173       if (FP_REG_RTX_P (dest) && MEM_P (src))
4174 	return false;
4175       if (FP_REG_RTX_P (src) && MEM_P (dest))
4176 	return false;
4177     }
4178 
4179   /* Otherwise split all multiword moves.  */
4180   return size > UNITS_PER_WORD;
4181 }
4182 
4183 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
4184    SPLIT_TYPE describes the split condition.  */
4185 
4186 void
4187 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type)
4188 {
4189   rtx low_dest;
4190 
4191   gcc_checking_assert (mips_split_move_p (dest, src, split_type));
4192   if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4193     {
4194       if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4195 	emit_insn (gen_move_doubleword_fprdi (dest, src));
4196       else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4197 	emit_insn (gen_move_doubleword_fprdf (dest, src));
4198       else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4199 	emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4200       else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4201 	emit_insn (gen_move_doubleword_fprv2si (dest, src));
4202       else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4203 	emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4204       else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4205 	emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4206       else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4207 	emit_insn (gen_move_doubleword_fprtf (dest, src));
4208       else
4209 	gcc_unreachable ();
4210     }
4211   else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4212     {
4213       low_dest = mips_subword (dest, false);
4214       mips_emit_move (low_dest, mips_subword (src, false));
4215       if (TARGET_64BIT)
4216 	emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4217       else
4218 	emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4219     }
4220   else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4221     {
4222       mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4223       if (TARGET_64BIT)
4224 	emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4225       else
4226 	emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4227     }
4228   else
4229     {
4230       /* The operation can be split into two normal moves.  Decide in
4231 	 which order to do them.  */
4232       low_dest = mips_subword (dest, false);
4233       if (REG_P (low_dest)
4234 	  && reg_overlap_mentioned_p (low_dest, src))
4235 	{
4236 	  mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4237 	  mips_emit_move (low_dest, mips_subword (src, false));
4238 	}
4239       else
4240 	{
4241 	  mips_emit_move (low_dest, mips_subword (src, false));
4242 	  mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4243 	}
4244     }
4245 }
4246 
4247 /* Return the split type for instruction INSN.  */
4248 
4249 static enum mips_split_type
4250 mips_insn_split_type (rtx insn)
4251 {
4252   basic_block bb = BLOCK_FOR_INSN (insn);
4253   if (bb)
4254     {
4255       if (optimize_bb_for_speed_p (bb))
4256 	return SPLIT_FOR_SPEED;
4257       else
4258 	return SPLIT_FOR_SIZE;
4259     }
4260   /* Once CFG information has been removed, we should trust the optimization
4261      decisions made by previous passes and only split where necessary.  */
4262   return SPLIT_IF_NECESSARY;
4263 }
4264 
4265 /* Return true if a move from SRC to DEST in INSN should be split.  */
4266 
4267 bool
4268 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
4269 {
4270   return mips_split_move_p (dest, src, mips_insn_split_type (insn));
4271 }
4272 
4273 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
4274    holds.  */
4275 
4276 void
4277 mips_split_move_insn (rtx dest, rtx src, rtx insn)
4278 {
4279   mips_split_move (dest, src, mips_insn_split_type (insn));
4280 }
4281 
4282 /* Return the appropriate instructions to move SRC into DEST.  Assume
4283    that SRC is operand 1 and DEST is operand 0.  */
4284 
4285 const char *
4286 mips_output_move (rtx dest, rtx src)
4287 {
4288   enum rtx_code dest_code, src_code;
4289   enum machine_mode mode;
4290   enum mips_symbol_type symbol_type;
4291   bool dbl_p;
4292 
4293   dest_code = GET_CODE (dest);
4294   src_code = GET_CODE (src);
4295   mode = GET_MODE (dest);
4296   dbl_p = (GET_MODE_SIZE (mode) == 8);
4297 
4298   if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
4299     return "#";
4300 
4301   if ((src_code == REG && GP_REG_P (REGNO (src)))
4302       || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4303     {
4304       if (dest_code == REG)
4305 	{
4306 	  if (GP_REG_P (REGNO (dest)))
4307 	    return "move\t%0,%z1";
4308 
4309 	  if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
4310 	    {
4311 	      if (ISA_HAS_DSP_MULT)
4312 		return "mult\t%q0,%.,%.";
4313 	      else
4314 		return "mult\t%.,%.";
4315 	    }
4316 
4317 	  /* Moves to HI are handled by special .md insns.  */
4318 	  if (REGNO (dest) == LO_REGNUM)
4319 	    return "mtlo\t%z1";
4320 
4321 	  if (DSP_ACC_REG_P (REGNO (dest)))
4322 	    {
4323 	      static char retval[] = "mt__\t%z1,%q0";
4324 
4325 	      retval[2] = reg_names[REGNO (dest)][4];
4326 	      retval[3] = reg_names[REGNO (dest)][5];
4327 	      return retval;
4328 	    }
4329 
4330 	  if (FP_REG_P (REGNO (dest)))
4331 	    return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4332 
4333 	  if (ALL_COP_REG_P (REGNO (dest)))
4334 	    {
4335 	      static char retval[] = "dmtc_\t%z1,%0";
4336 
4337 	      retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4338 	      return dbl_p ? retval : retval + 1;
4339 	    }
4340 	}
4341       if (dest_code == MEM)
4342 	switch (GET_MODE_SIZE (mode))
4343 	  {
4344 	  case 1: return "sb\t%z1,%0";
4345 	  case 2: return "sh\t%z1,%0";
4346 	  case 4: return "sw\t%z1,%0";
4347 	  case 8: return "sd\t%z1,%0";
4348 	  }
4349     }
4350   if (dest_code == REG && GP_REG_P (REGNO (dest)))
4351     {
4352       if (src_code == REG)
4353 	{
4354 	  /* Moves from HI are handled by special .md insns.  */
4355 	  if (REGNO (src) == LO_REGNUM)
4356 	    {
4357 	      /* When generating VR4120 or VR4130 code, we use MACC and
4358 		 DMACC instead of MFLO.  This avoids both the normal
4359 		 MIPS III HI/LO hazards and the errata related to
4360 		 -mfix-vr4130.  */
4361 	      if (ISA_HAS_MACCHI)
4362 		return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4363 	      return "mflo\t%0";
4364 	    }
4365 
4366 	  if (DSP_ACC_REG_P (REGNO (src)))
4367 	    {
4368 	      static char retval[] = "mf__\t%0,%q1";
4369 
4370 	      retval[2] = reg_names[REGNO (src)][4];
4371 	      retval[3] = reg_names[REGNO (src)][5];
4372 	      return retval;
4373 	    }
4374 
4375 	  if (FP_REG_P (REGNO (src)))
4376 	    return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4377 
4378 	  if (ALL_COP_REG_P (REGNO (src)))
4379 	    {
4380 	      static char retval[] = "dmfc_\t%0,%1";
4381 
4382 	      retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4383 	      return dbl_p ? retval : retval + 1;
4384 	    }
4385 	}
4386 
4387       if (src_code == MEM)
4388 	switch (GET_MODE_SIZE (mode))
4389 	  {
4390 	  case 1: return "lbu\t%0,%1";
4391 	  case 2: return "lhu\t%0,%1";
4392 	  case 4: return "lw\t%0,%1";
4393 	  case 8: return "ld\t%0,%1";
4394 	  }
4395 
4396       if (src_code == CONST_INT)
4397 	{
4398 	  /* Don't use the X format for the operand itself, because that
4399 	     will give out-of-range numbers for 64-bit hosts and 32-bit
4400 	     targets.  */
4401 	  if (!TARGET_MIPS16)
4402 	    return "li\t%0,%1\t\t\t# %X1";
4403 
4404 	  if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4405 	    return "li\t%0,%1";
4406 
4407 	  if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4408 	    return "#";
4409 	}
4410 
4411       if (src_code == HIGH)
4412 	return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4413 
4414       if (CONST_GP_P (src))
4415 	return "move\t%0,%1";
4416 
4417       if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4418 	  && mips_lo_relocs[symbol_type] != 0)
4419 	{
4420 	  /* A signed 16-bit constant formed by applying a relocation
4421 	     operator to a symbolic address.  */
4422 	  gcc_assert (!mips_split_p[symbol_type]);
4423 	  return "li\t%0,%R1";
4424 	}
4425 
4426       if (symbolic_operand (src, VOIDmode))
4427 	{
4428 	  gcc_assert (TARGET_MIPS16
4429 		      ? TARGET_MIPS16_TEXT_LOADS
4430 		      : !TARGET_EXPLICIT_RELOCS);
4431 	  return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4432 	}
4433     }
4434   if (src_code == REG && FP_REG_P (REGNO (src)))
4435     {
4436       if (dest_code == REG && FP_REG_P (REGNO (dest)))
4437 	{
4438 	  if (GET_MODE (dest) == V2SFmode)
4439 	    return "mov.ps\t%0,%1";
4440 	  else
4441 	    return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4442 	}
4443 
4444       if (dest_code == MEM)
4445 	return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4446     }
4447   if (dest_code == REG && FP_REG_P (REGNO (dest)))
4448     {
4449       if (src_code == MEM)
4450 	return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4451     }
4452   if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4453     {
4454       static char retval[] = "l_c_\t%0,%1";
4455 
4456       retval[1] = (dbl_p ? 'd' : 'w');
4457       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4458       return retval;
4459     }
4460   if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4461     {
4462       static char retval[] = "s_c_\t%1,%0";
4463 
4464       retval[1] = (dbl_p ? 'd' : 'w');
4465       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4466       return retval;
4467     }
4468   gcc_unreachable ();
4469 }
4470 
4471 /* Return true if CMP1 is a suitable second operand for integer ordering
4472    test CODE.  See also the *sCC patterns in mips.md.  */
4473 
4474 static bool
4475 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4476 {
4477   switch (code)
4478     {
4479     case GT:
4480     case GTU:
4481       return reg_or_0_operand (cmp1, VOIDmode);
4482 
4483     case GE:
4484     case GEU:
4485       return !TARGET_MIPS16 && cmp1 == const1_rtx;
4486 
4487     case LT:
4488     case LTU:
4489       return arith_operand (cmp1, VOIDmode);
4490 
4491     case LE:
4492       return sle_operand (cmp1, VOIDmode);
4493 
4494     case LEU:
4495       return sleu_operand (cmp1, VOIDmode);
4496 
4497     default:
4498       gcc_unreachable ();
4499     }
4500 }
4501 
4502 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4503    integer ordering test *CODE, or if an equivalent combination can
4504    be formed by adjusting *CODE and *CMP1.  When returning true, update
4505    *CODE and *CMP1 with the chosen code and operand, otherwise leave
4506    them alone.  */
4507 
4508 static bool
4509 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4510 				  enum machine_mode mode)
4511 {
4512   HOST_WIDE_INT plus_one;
4513 
4514   if (mips_int_order_operand_ok_p (*code, *cmp1))
4515     return true;
4516 
4517   if (CONST_INT_P (*cmp1))
4518     switch (*code)
4519       {
4520       case LE:
4521 	plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4522 	if (INTVAL (*cmp1) < plus_one)
4523 	  {
4524 	    *code = LT;
4525 	    *cmp1 = force_reg (mode, GEN_INT (plus_one));
4526 	    return true;
4527 	  }
4528 	break;
4529 
4530       case LEU:
4531 	plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4532 	if (plus_one != 0)
4533 	  {
4534 	    *code = LTU;
4535 	    *cmp1 = force_reg (mode, GEN_INT (plus_one));
4536 	    return true;
4537 	  }
4538 	break;
4539 
4540       default:
4541 	break;
4542       }
4543   return false;
4544 }
4545 
4546 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4547    in TARGET.  CMP0 and TARGET are register_operands.  If INVERT_PTR
4548    is nonnull, it's OK to set TARGET to the inverse of the result and
4549    flip *INVERT_PTR instead.  */
4550 
4551 static void
4552 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4553 			  rtx target, rtx cmp0, rtx cmp1)
4554 {
4555   enum machine_mode mode;
4556 
4557   /* First see if there is a MIPS instruction that can do this operation.
4558      If not, try doing the same for the inverse operation.  If that also
4559      fails, force CMP1 into a register and try again.  */
4560   mode = GET_MODE (cmp0);
4561   if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4562     mips_emit_binary (code, target, cmp0, cmp1);
4563   else
4564     {
4565       enum rtx_code inv_code = reverse_condition (code);
4566       if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4567 	{
4568 	  cmp1 = force_reg (mode, cmp1);
4569 	  mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4570 	}
4571       else if (invert_ptr == 0)
4572 	{
4573 	  rtx inv_target;
4574 
4575 	  inv_target = mips_force_binary (GET_MODE (target),
4576 					  inv_code, cmp0, cmp1);
4577 	  mips_emit_binary (XOR, target, inv_target, const1_rtx);
4578 	}
4579       else
4580 	{
4581 	  *invert_ptr = !*invert_ptr;
4582 	  mips_emit_binary (inv_code, target, cmp0, cmp1);
4583 	}
4584     }
4585 }
4586 
4587 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4588    The register will have the same mode as CMP0.  */
4589 
4590 static rtx
4591 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4592 {
4593   if (cmp1 == const0_rtx)
4594     return cmp0;
4595 
4596   if (uns_arith_operand (cmp1, VOIDmode))
4597     return expand_binop (GET_MODE (cmp0), xor_optab,
4598 			 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4599 
4600   return expand_binop (GET_MODE (cmp0), sub_optab,
4601 		       cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4602 }
4603 
4604 /* Convert *CODE into a code that can be used in a floating-point
4605    scc instruction (C.cond.fmt).  Return true if the values of
4606    the condition code registers will be inverted, with 0 indicating
4607    that the condition holds.  */
4608 
4609 static bool
4610 mips_reversed_fp_cond (enum rtx_code *code)
4611 {
4612   switch (*code)
4613     {
4614     case NE:
4615     case LTGT:
4616     case ORDERED:
4617       *code = reverse_condition_maybe_unordered (*code);
4618       return true;
4619 
4620     default:
4621       return false;
4622     }
4623 }
4624 
4625 /* Allocate a floating-point condition-code register of mode MODE.
4626 
4627    These condition code registers are used for certain kinds
4628    of compound operation, such as compare and branches, vconds,
4629    and built-in functions.  At expand time, their use is entirely
4630    controlled by MIPS-specific code and is entirely internal
4631    to these compound operations.
4632 
4633    We could (and did in the past) expose condition-code values
4634    as pseudo registers and leave the register allocator to pick
4635    appropriate registers.  The problem is that it is not practically
4636    possible for the rtl optimizers to guarantee that no spills will
4637    be needed, even when AVOID_CCMODE_COPIES is defined.  We would
4638    therefore need spill and reload sequences to handle the worst case.
4639 
4640    Although such sequences do exist, they are very expensive and are
4641    not something we'd want to use.  This is especially true of CCV2 and
4642    CCV4, where all the shuffling would greatly outweigh whatever benefit
4643    the vectorization itself provides.
4644 
4645    The main benefit of having more than one condition-code register
4646    is to allow the pipelining of operations, especially those involving
4647    comparisons and conditional moves.  We don't really expect the
4648    registers to be live for long periods, and certainly never want
4649    them to be live across calls.
4650 
4651    Also, there should be no penalty attached to using all the available
4652    registers.  They are simply bits in the same underlying FPU control
4653    register.
4654 
4655    We therefore expose the hardware registers from the outset and use
4656    a simple round-robin allocation scheme.  */
4657 
4658 static rtx
4659 mips_allocate_fcc (enum machine_mode mode)
4660 {
4661   unsigned int regno, count;
4662 
4663   gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
4664 
4665   if (mode == CCmode)
4666     count = 1;
4667   else if (mode == CCV2mode)
4668     count = 2;
4669   else if (mode == CCV4mode)
4670     count = 4;
4671   else
4672     gcc_unreachable ();
4673 
4674   cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
4675   if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
4676     cfun->machine->next_fcc = 0;
4677   regno = ST_REG_FIRST + cfun->machine->next_fcc;
4678   cfun->machine->next_fcc += count;
4679   return gen_rtx_REG (mode, regno);
4680 }
4681 
4682 /* Convert a comparison into something that can be used in a branch or
4683    conditional move.  On entry, *OP0 and *OP1 are the values being
4684    compared and *CODE is the code used to compare them.
4685 
4686    Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
4687    If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
4688    otherwise any standard branch condition can be used.  The standard branch
4689    conditions are:
4690 
4691       - EQ or NE between two registers.
4692       - any comparison between a register and zero.  */
4693 
4694 static void
4695 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
4696 {
4697   rtx cmp_op0 = *op0;
4698   rtx cmp_op1 = *op1;
4699 
4700   if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
4701     {
4702       if (!need_eq_ne_p && *op1 == const0_rtx)
4703 	;
4704       else if (*code == EQ || *code == NE)
4705 	{
4706 	  if (need_eq_ne_p)
4707 	    {
4708 	      *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
4709 	      *op1 = const0_rtx;
4710 	    }
4711 	  else
4712 	    *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
4713 	}
4714       else
4715 	{
4716 	  /* The comparison needs a separate scc instruction.  Store the
4717 	     result of the scc in *OP0 and compare it against zero.  */
4718 	  bool invert = false;
4719 	  *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
4720 	  mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
4721 	  *code = (invert ? EQ : NE);
4722 	  *op1 = const0_rtx;
4723 	}
4724     }
4725   else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
4726     {
4727       *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
4728       mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
4729       *code = NE;
4730       *op1 = const0_rtx;
4731     }
4732   else
4733     {
4734       enum rtx_code cmp_code;
4735 
4736       /* Floating-point tests use a separate C.cond.fmt comparison to
4737 	 set a condition code register.  The branch or conditional move
4738 	 will then compare that register against zero.
4739 
4740 	 Set CMP_CODE to the code of the comparison instruction and
4741 	 *CODE to the code that the branch or move should use.  */
4742       cmp_code = *code;
4743       *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
4744       *op0 = (ISA_HAS_8CC
4745 	      ? mips_allocate_fcc (CCmode)
4746 	      : gen_rtx_REG (CCmode, FPSW_REGNUM));
4747       *op1 = const0_rtx;
4748       mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
4749     }
4750 }
4751 
4752 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
4753    and OPERAND[3].  Store the result in OPERANDS[0].
4754 
4755    On 64-bit targets, the mode of the comparison and target will always be
4756    SImode, thus possibly narrower than that of the comparison's operands.  */
4757 
4758 void
4759 mips_expand_scc (rtx operands[])
4760 {
4761   rtx target = operands[0];
4762   enum rtx_code code = GET_CODE (operands[1]);
4763   rtx op0 = operands[2];
4764   rtx op1 = operands[3];
4765 
4766   gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
4767 
4768   if (code == EQ || code == NE)
4769     {
4770       if (ISA_HAS_SEQ_SNE
4771 	  && reg_imm10_operand (op1, GET_MODE (op1)))
4772 	mips_emit_binary (code, target, op0, op1);
4773       else
4774 	{
4775 	  rtx zie = mips_zero_if_equal (op0, op1);
4776 	  mips_emit_binary (code, target, zie, const0_rtx);
4777 	}
4778     }
4779   else
4780     mips_emit_int_order_test (code, 0, target, op0, op1);
4781 }
4782 
4783 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
4784    CODE and jump to OPERANDS[3] if the condition holds.  */
4785 
4786 void
4787 mips_expand_conditional_branch (rtx *operands)
4788 {
4789   enum rtx_code code = GET_CODE (operands[0]);
4790   rtx op0 = operands[1];
4791   rtx op1 = operands[2];
4792   rtx condition;
4793 
4794   mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
4795   condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
4796   emit_jump_insn (gen_condjump (condition, operands[3]));
4797 }
4798 
4799 /* Implement:
4800 
4801    (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
4802    (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS))  */
4803 
4804 void
4805 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
4806 		       enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
4807 {
4808   rtx cmp_result;
4809   bool reversed_p;
4810 
4811   reversed_p = mips_reversed_fp_cond (&cond);
4812   cmp_result = mips_allocate_fcc (CCV2mode);
4813   emit_insn (gen_scc_ps (cmp_result,
4814 			 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
4815   if (reversed_p)
4816     emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
4817 					 cmp_result));
4818   else
4819     emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
4820 					 cmp_result));
4821 }
4822 
4823 /* Perform the comparison in OPERANDS[1].  Move OPERANDS[2] into OPERANDS[0]
4824    if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0].  */
4825 
4826 void
4827 mips_expand_conditional_move (rtx *operands)
4828 {
4829   rtx cond;
4830   enum rtx_code code = GET_CODE (operands[1]);
4831   rtx op0 = XEXP (operands[1], 0);
4832   rtx op1 = XEXP (operands[1], 1);
4833 
4834   mips_emit_compare (&code, &op0, &op1, true);
4835   cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
4836   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
4837 			  gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
4838 						operands[2], operands[3])));
4839 }
4840 
4841 /* Perform the comparison in COMPARISON, then trap if the condition holds.  */
4842 
4843 void
4844 mips_expand_conditional_trap (rtx comparison)
4845 {
4846   rtx op0, op1;
4847   enum machine_mode mode;
4848   enum rtx_code code;
4849 
4850   /* MIPS conditional trap instructions don't have GT or LE flavors,
4851      so we must swap the operands and convert to LT and GE respectively.  */
4852   code = GET_CODE (comparison);
4853   switch (code)
4854     {
4855     case GT:
4856     case LE:
4857     case GTU:
4858     case LEU:
4859       code = swap_condition (code);
4860       op0 = XEXP (comparison, 1);
4861       op1 = XEXP (comparison, 0);
4862       break;
4863 
4864     default:
4865       op0 = XEXP (comparison, 0);
4866       op1 = XEXP (comparison, 1);
4867       break;
4868     }
4869 
4870   mode = GET_MODE (XEXP (comparison, 0));
4871   op0 = force_reg (mode, op0);
4872   if (!arith_operand (op1, mode))
4873     op1 = force_reg (mode, op1);
4874 
4875   emit_insn (gen_rtx_TRAP_IF (VOIDmode,
4876 			      gen_rtx_fmt_ee (code, mode, op0, op1),
4877 			      const0_rtx));
4878 }
4879 
4880 /* Initialize *CUM for a call to a function of type FNTYPE.  */
4881 
4882 void
4883 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
4884 {
4885   memset (cum, 0, sizeof (*cum));
4886   cum->prototype = (fntype && prototype_p (fntype));
4887   cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
4888 }
4889 
4890 /* Fill INFO with information about a single argument.  CUM is the
4891    cumulative state for earlier arguments.  MODE is the mode of this
4892    argument and TYPE is its type (if known).  NAMED is true if this
4893    is a named (fixed) argument rather than a variable one.  */
4894 
4895 static void
4896 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
4897 		   enum machine_mode mode, const_tree type, bool named)
4898 {
4899   bool doubleword_aligned_p;
4900   unsigned int num_bytes, num_words, max_regs;
4901 
4902   /* Work out the size of the argument.  */
4903   num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
4904   num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4905 
4906   /* Decide whether it should go in a floating-point register, assuming
4907      one is free.  Later code checks for availability.
4908 
4909      The checks against UNITS_PER_FPVALUE handle the soft-float and
4910      single-float cases.  */
4911   switch (mips_abi)
4912     {
4913     case ABI_EABI:
4914       /* The EABI conventions have traditionally been defined in terms
4915 	 of TYPE_MODE, regardless of the actual type.  */
4916       info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
4917 		      || mode == V2SFmode)
4918 		     && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
4919       break;
4920 
4921     case ABI_32:
4922     case ABI_O64:
4923       /* Only leading floating-point scalars are passed in
4924 	 floating-point registers.  We also handle vector floats the same
4925 	 say, which is OK because they are not covered by the standard ABI.  */
4926       info->fpr_p = (!cum->gp_reg_found
4927 		     && cum->arg_number < 2
4928 		     && (type == 0
4929 			 || SCALAR_FLOAT_TYPE_P (type)
4930 			 || VECTOR_FLOAT_TYPE_P (type))
4931 		     && (GET_MODE_CLASS (mode) == MODE_FLOAT
4932 			 || mode == V2SFmode)
4933 		     && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
4934       break;
4935 
4936     case ABI_N32:
4937     case ABI_64:
4938       /* Scalar, complex and vector floating-point types are passed in
4939 	 floating-point registers, as long as this is a named rather
4940 	 than a variable argument.  */
4941       info->fpr_p = (named
4942 		     && (type == 0 || FLOAT_TYPE_P (type))
4943 		     && (GET_MODE_CLASS (mode) == MODE_FLOAT
4944 			 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4945 			 || mode == V2SFmode)
4946 		     && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
4947 
4948       /* ??? According to the ABI documentation, the real and imaginary
4949 	 parts of complex floats should be passed in individual registers.
4950 	 The real and imaginary parts of stack arguments are supposed
4951 	 to be contiguous and there should be an extra word of padding
4952 	 at the end.
4953 
4954 	 This has two problems.  First, it makes it impossible to use a
4955 	 single "void *" va_list type, since register and stack arguments
4956 	 are passed differently.  (At the time of writing, MIPSpro cannot
4957 	 handle complex float varargs correctly.)  Second, it's unclear
4958 	 what should happen when there is only one register free.
4959 
4960 	 For now, we assume that named complex floats should go into FPRs
4961 	 if there are two FPRs free, otherwise they should be passed in the
4962 	 same way as a struct containing two floats.  */
4963       if (info->fpr_p
4964 	  && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4965 	  && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
4966 	{
4967 	  if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
4968 	    info->fpr_p = false;
4969 	  else
4970 	    num_words = 2;
4971 	}
4972       break;
4973 
4974     default:
4975       gcc_unreachable ();
4976     }
4977 
4978   /* See whether the argument has doubleword alignment.  */
4979   doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
4980 			  > BITS_PER_WORD);
4981 
4982   /* Set REG_OFFSET to the register count we're interested in.
4983      The EABI allocates the floating-point registers separately,
4984      but the other ABIs allocate them like integer registers.  */
4985   info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
4986 		      ? cum->num_fprs
4987 		      : cum->num_gprs);
4988 
4989   /* Advance to an even register if the argument is doubleword-aligned.  */
4990   if (doubleword_aligned_p)
4991     info->reg_offset += info->reg_offset & 1;
4992 
4993   /* Work out the offset of a stack argument.  */
4994   info->stack_offset = cum->stack_words;
4995   if (doubleword_aligned_p)
4996     info->stack_offset += info->stack_offset & 1;
4997 
4998   max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
4999 
5000   /* Partition the argument between registers and stack.  */
5001   info->reg_words = MIN (num_words, max_regs);
5002   info->stack_words = num_words - info->reg_words;
5003 }
5004 
5005 /* INFO describes a register argument that has the normal format for the
5006    argument's mode.  Return the register it uses, assuming that FPRs are
5007    available if HARD_FLOAT_P.  */
5008 
5009 static unsigned int
5010 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
5011 {
5012   if (!info->fpr_p || !hard_float_p)
5013     return GP_ARG_FIRST + info->reg_offset;
5014   else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
5015     /* In o32, the second argument is always passed in $f14
5016        for TARGET_DOUBLE_FLOAT, regardless of whether the
5017        first argument was a word or doubleword.  */
5018     return FP_ARG_FIRST + 2;
5019   else
5020     return FP_ARG_FIRST + info->reg_offset;
5021 }
5022 
5023 /* Implement TARGET_STRICT_ARGUMENT_NAMING.  */
5024 
5025 static bool
5026 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
5027 {
5028   return !TARGET_OLDABI;
5029 }
5030 
5031 /* Implement TARGET_FUNCTION_ARG.  */
5032 
5033 static rtx
5034 mips_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
5035 		   const_tree type, bool named)
5036 {
5037   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5038   struct mips_arg_info info;
5039 
5040   /* We will be called with a mode of VOIDmode after the last argument
5041      has been seen.  Whatever we return will be passed to the call expander.
5042      If we need a MIPS16 fp_code, return a REG with the code stored as
5043      the mode.  */
5044   if (mode == VOIDmode)
5045     {
5046       if (TARGET_MIPS16 && cum->fp_code != 0)
5047 	return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
5048       else
5049 	return NULL;
5050     }
5051 
5052   mips_get_arg_info (&info, cum, mode, type, named);
5053 
5054   /* Return straight away if the whole argument is passed on the stack.  */
5055   if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
5056     return NULL;
5057 
5058   /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
5059      contains a double in its entirety, then that 64-bit chunk is passed
5060      in a floating-point register.  */
5061   if (TARGET_NEWABI
5062       && TARGET_HARD_FLOAT
5063       && named
5064       && type != 0
5065       && TREE_CODE (type) == RECORD_TYPE
5066       && TYPE_SIZE_UNIT (type)
5067       && host_integerp (TYPE_SIZE_UNIT (type), 1))
5068     {
5069       tree field;
5070 
5071       /* First check to see if there is any such field.  */
5072       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5073 	if (TREE_CODE (field) == FIELD_DECL
5074 	    && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5075 	    && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
5076 	    && host_integerp (bit_position (field), 0)
5077 	    && int_bit_position (field) % BITS_PER_WORD == 0)
5078 	  break;
5079 
5080       if (field != 0)
5081 	{
5082 	  /* Now handle the special case by returning a PARALLEL
5083 	     indicating where each 64-bit chunk goes.  INFO.REG_WORDS
5084 	     chunks are passed in registers.  */
5085 	  unsigned int i;
5086 	  HOST_WIDE_INT bitpos;
5087 	  rtx ret;
5088 
5089 	  /* assign_parms checks the mode of ENTRY_PARM, so we must
5090 	     use the actual mode here.  */
5091 	  ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
5092 
5093 	  bitpos = 0;
5094 	  field = TYPE_FIELDS (type);
5095 	  for (i = 0; i < info.reg_words; i++)
5096 	    {
5097 	      rtx reg;
5098 
5099 	      for (; field; field = DECL_CHAIN (field))
5100 		if (TREE_CODE (field) == FIELD_DECL
5101 		    && int_bit_position (field) >= bitpos)
5102 		  break;
5103 
5104 	      if (field
5105 		  && int_bit_position (field) == bitpos
5106 		  && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5107 		  && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
5108 		reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
5109 	      else
5110 		reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
5111 
5112 	      XVECEXP (ret, 0, i)
5113 		= gen_rtx_EXPR_LIST (VOIDmode, reg,
5114 				     GEN_INT (bitpos / BITS_PER_UNIT));
5115 
5116 	      bitpos += BITS_PER_WORD;
5117 	    }
5118 	  return ret;
5119 	}
5120     }
5121 
5122   /* Handle the n32/n64 conventions for passing complex floating-point
5123      arguments in FPR pairs.  The real part goes in the lower register
5124      and the imaginary part goes in the upper register.  */
5125   if (TARGET_NEWABI
5126       && info.fpr_p
5127       && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5128     {
5129       rtx real, imag;
5130       enum machine_mode inner;
5131       unsigned int regno;
5132 
5133       inner = GET_MODE_INNER (mode);
5134       regno = FP_ARG_FIRST + info.reg_offset;
5135       if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
5136 	{
5137 	  /* Real part in registers, imaginary part on stack.  */
5138 	  gcc_assert (info.stack_words == info.reg_words);
5139 	  return gen_rtx_REG (inner, regno);
5140 	}
5141       else
5142 	{
5143 	  gcc_assert (info.stack_words == 0);
5144 	  real = gen_rtx_EXPR_LIST (VOIDmode,
5145 				    gen_rtx_REG (inner, regno),
5146 				    const0_rtx);
5147 	  imag = gen_rtx_EXPR_LIST (VOIDmode,
5148 				    gen_rtx_REG (inner,
5149 						 regno + info.reg_words / 2),
5150 				    GEN_INT (GET_MODE_SIZE (inner)));
5151 	  return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
5152 	}
5153     }
5154 
5155   return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
5156 }
5157 
5158 /* Implement TARGET_FUNCTION_ARG_ADVANCE.  */
5159 
5160 static void
5161 mips_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
5162 			   const_tree type, bool named)
5163 {
5164   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5165   struct mips_arg_info info;
5166 
5167   mips_get_arg_info (&info, cum, mode, type, named);
5168 
5169   if (!info.fpr_p)
5170     cum->gp_reg_found = true;
5171 
5172   /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
5173      an explanation of what this code does.  It assumes that we're using
5174      either the o32 or the o64 ABI, both of which pass at most 2 arguments
5175      in FPRs.  */
5176   if (cum->arg_number < 2 && info.fpr_p)
5177     cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
5178 
5179   /* Advance the register count.  This has the effect of setting
5180      num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
5181      argument required us to skip the final GPR and pass the whole
5182      argument on the stack.  */
5183   if (mips_abi != ABI_EABI || !info.fpr_p)
5184     cum->num_gprs = info.reg_offset + info.reg_words;
5185   else if (info.reg_words > 0)
5186     cum->num_fprs += MAX_FPRS_PER_FMT;
5187 
5188   /* Advance the stack word count.  */
5189   if (info.stack_words > 0)
5190     cum->stack_words = info.stack_offset + info.stack_words;
5191 
5192   cum->arg_number++;
5193 }
5194 
5195 /* Implement TARGET_ARG_PARTIAL_BYTES.  */
5196 
5197 static int
5198 mips_arg_partial_bytes (cumulative_args_t cum,
5199 			enum machine_mode mode, tree type, bool named)
5200 {
5201   struct mips_arg_info info;
5202 
5203   mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
5204   return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
5205 }
5206 
5207 /* Implement TARGET_FUNCTION_ARG_BOUNDARY.  Every parameter gets at
5208    least PARM_BOUNDARY bits of alignment, but will be given anything up
5209    to STACK_BOUNDARY bits if the type requires it.  */
5210 
5211 static unsigned int
5212 mips_function_arg_boundary (enum machine_mode mode, const_tree type)
5213 {
5214   unsigned int alignment;
5215 
5216   alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5217   if (alignment < PARM_BOUNDARY)
5218     alignment = PARM_BOUNDARY;
5219   if (alignment > STACK_BOUNDARY)
5220     alignment = STACK_BOUNDARY;
5221   return alignment;
5222 }
5223 
5224 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5225    upward rather than downward.  In other words, return true if the
5226    first byte of the stack slot has useful data, false if the last
5227    byte does.  */
5228 
5229 bool
5230 mips_pad_arg_upward (enum machine_mode mode, const_tree type)
5231 {
5232   /* On little-endian targets, the first byte of every stack argument
5233      is passed in the first byte of the stack slot.  */
5234   if (!BYTES_BIG_ENDIAN)
5235     return true;
5236 
5237   /* Otherwise, integral types are padded downward: the last byte of a
5238      stack argument is passed in the last byte of the stack slot.  */
5239   if (type != 0
5240       ? (INTEGRAL_TYPE_P (type)
5241 	 || POINTER_TYPE_P (type)
5242 	 || FIXED_POINT_TYPE_P (type))
5243       : (SCALAR_INT_MODE_P (mode)
5244 	 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5245     return false;
5246 
5247   /* Big-endian o64 pads floating-point arguments downward.  */
5248   if (mips_abi == ABI_O64)
5249     if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5250       return false;
5251 
5252   /* Other types are padded upward for o32, o64, n32 and n64.  */
5253   if (mips_abi != ABI_EABI)
5254     return true;
5255 
5256   /* Arguments smaller than a stack slot are padded downward.  */
5257   if (mode != BLKmode)
5258     return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5259   else
5260     return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5261 }
5262 
5263 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...).  Return !BYTES_BIG_ENDIAN
5264    if the least significant byte of the register has useful data.  Return
5265    the opposite if the most significant byte does.  */
5266 
5267 bool
5268 mips_pad_reg_upward (enum machine_mode mode, tree type)
5269 {
5270   /* No shifting is required for floating-point arguments.  */
5271   if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5272     return !BYTES_BIG_ENDIAN;
5273 
5274   /* Otherwise, apply the same padding to register arguments as we do
5275      to stack arguments.  */
5276   return mips_pad_arg_upward (mode, type);
5277 }
5278 
5279 /* Return nonzero when an argument must be passed by reference.  */
5280 
5281 static bool
5282 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5283 			enum machine_mode mode, const_tree type,
5284 			bool named ATTRIBUTE_UNUSED)
5285 {
5286   if (mips_abi == ABI_EABI)
5287     {
5288       int size;
5289 
5290       /* ??? How should SCmode be handled?  */
5291       if (mode == DImode || mode == DFmode
5292 	  || mode == DQmode || mode == UDQmode
5293 	  || mode == DAmode || mode == UDAmode)
5294 	return 0;
5295 
5296       size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5297       return size == -1 || size > UNITS_PER_WORD;
5298     }
5299   else
5300     {
5301       /* If we have a variable-sized parameter, we have no choice.  */
5302       return targetm.calls.must_pass_in_stack (mode, type);
5303     }
5304 }
5305 
5306 /* Implement TARGET_CALLEE_COPIES.  */
5307 
5308 static bool
5309 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
5310 		    enum machine_mode mode ATTRIBUTE_UNUSED,
5311 		    const_tree type ATTRIBUTE_UNUSED, bool named)
5312 {
5313   return mips_abi == ABI_EABI && named;
5314 }
5315 
5316 /* See whether VALTYPE is a record whose fields should be returned in
5317    floating-point registers.  If so, return the number of fields and
5318    list them in FIELDS (which should have two elements).  Return 0
5319    otherwise.
5320 
5321    For n32 & n64, a structure with one or two fields is returned in
5322    floating-point registers as long as every field has a floating-point
5323    type.  */
5324 
5325 static int
5326 mips_fpr_return_fields (const_tree valtype, tree *fields)
5327 {
5328   tree field;
5329   int i;
5330 
5331   if (!TARGET_NEWABI)
5332     return 0;
5333 
5334   if (TREE_CODE (valtype) != RECORD_TYPE)
5335     return 0;
5336 
5337   i = 0;
5338   for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5339     {
5340       if (TREE_CODE (field) != FIELD_DECL)
5341 	continue;
5342 
5343       if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5344 	return 0;
5345 
5346       if (i == 2)
5347 	return 0;
5348 
5349       fields[i++] = field;
5350     }
5351   return i;
5352 }
5353 
5354 /* Implement TARGET_RETURN_IN_MSB.  For n32 & n64, we should return
5355    a value in the most significant part of $2/$3 if:
5356 
5357       - the target is big-endian;
5358 
5359       - the value has a structure or union type (we generalize this to
5360 	cover aggregates from other languages too); and
5361 
5362       - the structure is not returned in floating-point registers.  */
5363 
5364 static bool
5365 mips_return_in_msb (const_tree valtype)
5366 {
5367   tree fields[2];
5368 
5369   return (TARGET_NEWABI
5370 	  && TARGET_BIG_ENDIAN
5371 	  && AGGREGATE_TYPE_P (valtype)
5372 	  && mips_fpr_return_fields (valtype, fields) == 0);
5373 }
5374 
5375 /* Return true if the function return value MODE will get returned in a
5376    floating-point register.  */
5377 
5378 static bool
5379 mips_return_mode_in_fpr_p (enum machine_mode mode)
5380 {
5381   return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5382 	   || mode == V2SFmode
5383 	   || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5384 	  && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5385 }
5386 
5387 /* Return the representation of an FPR return register when the
5388    value being returned in FP_RETURN has mode VALUE_MODE and the
5389    return type itself has mode TYPE_MODE.  On NewABI targets,
5390    the two modes may be different for structures like:
5391 
5392        struct __attribute__((packed)) foo { float f; }
5393 
5394    where we return the SFmode value of "f" in FP_RETURN, but where
5395    the structure itself has mode BLKmode.  */
5396 
5397 static rtx
5398 mips_return_fpr_single (enum machine_mode type_mode,
5399 			enum machine_mode value_mode)
5400 {
5401   rtx x;
5402 
5403   x = gen_rtx_REG (value_mode, FP_RETURN);
5404   if (type_mode != value_mode)
5405     {
5406       x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5407       x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5408     }
5409   return x;
5410 }
5411 
5412 /* Return a composite value in a pair of floating-point registers.
5413    MODE1 and OFFSET1 are the mode and byte offset for the first value,
5414    likewise MODE2 and OFFSET2 for the second.  MODE is the mode of the
5415    complete value.
5416 
5417    For n32 & n64, $f0 always holds the first value and $f2 the second.
5418    Otherwise the values are packed together as closely as possible.  */
5419 
5420 static rtx
5421 mips_return_fpr_pair (enum machine_mode mode,
5422 		      enum machine_mode mode1, HOST_WIDE_INT offset1,
5423 		      enum machine_mode mode2, HOST_WIDE_INT offset2)
5424 {
5425   int inc;
5426 
5427   inc = (TARGET_NEWABI ? 2 : MAX_FPRS_PER_FMT);
5428   return gen_rtx_PARALLEL
5429     (mode,
5430      gen_rtvec (2,
5431 		gen_rtx_EXPR_LIST (VOIDmode,
5432 				   gen_rtx_REG (mode1, FP_RETURN),
5433 				   GEN_INT (offset1)),
5434 		gen_rtx_EXPR_LIST (VOIDmode,
5435 				   gen_rtx_REG (mode2, FP_RETURN + inc),
5436 				   GEN_INT (offset2))));
5437 
5438 }
5439 
5440 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
5441    For normal calls, VALTYPE is the return type and MODE is VOIDmode.
5442    For libcalls, VALTYPE is null and MODE is the mode of the return value.  */
5443 
5444 static rtx
5445 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
5446 		       enum machine_mode mode)
5447 {
5448   if (valtype)
5449     {
5450       tree fields[2];
5451       int unsigned_p;
5452       const_tree func;
5453 
5454       if (fn_decl_or_type && DECL_P (fn_decl_or_type))
5455 	func = fn_decl_or_type;
5456       else
5457 	func = NULL;
5458 
5459       mode = TYPE_MODE (valtype);
5460       unsigned_p = TYPE_UNSIGNED (valtype);
5461 
5462       /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5463 	 return values, promote the mode here too.  */
5464       mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5465 
5466       /* Handle structures whose fields are returned in $f0/$f2.  */
5467       switch (mips_fpr_return_fields (valtype, fields))
5468 	{
5469 	case 1:
5470 	  return mips_return_fpr_single (mode,
5471 					 TYPE_MODE (TREE_TYPE (fields[0])));
5472 
5473 	case 2:
5474 	  return mips_return_fpr_pair (mode,
5475 				       TYPE_MODE (TREE_TYPE (fields[0])),
5476 				       int_byte_position (fields[0]),
5477 				       TYPE_MODE (TREE_TYPE (fields[1])),
5478 				       int_byte_position (fields[1]));
5479 	}
5480 
5481       /* If a value is passed in the most significant part of a register, see
5482 	 whether we have to round the mode up to a whole number of words.  */
5483       if (mips_return_in_msb (valtype))
5484 	{
5485 	  HOST_WIDE_INT size = int_size_in_bytes (valtype);
5486 	  if (size % UNITS_PER_WORD != 0)
5487 	    {
5488 	      size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5489 	      mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5490 	    }
5491 	}
5492 
5493       /* For EABI, the class of return register depends entirely on MODE.
5494 	 For example, "struct { some_type x; }" and "union { some_type x; }"
5495 	 are returned in the same way as a bare "some_type" would be.
5496 	 Other ABIs only use FPRs for scalar, complex or vector types.  */
5497       if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5498 	return gen_rtx_REG (mode, GP_RETURN);
5499     }
5500 
5501   if (!TARGET_MIPS16)
5502     {
5503       /* Handle long doubles for n32 & n64.  */
5504       if (mode == TFmode)
5505 	return mips_return_fpr_pair (mode,
5506 				     DImode, 0,
5507 				     DImode, GET_MODE_SIZE (mode) / 2);
5508 
5509       if (mips_return_mode_in_fpr_p (mode))
5510 	{
5511 	  if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5512 	    return mips_return_fpr_pair (mode,
5513 					 GET_MODE_INNER (mode), 0,
5514 					 GET_MODE_INNER (mode),
5515 					 GET_MODE_SIZE (mode) / 2);
5516 	  else
5517 	    return gen_rtx_REG (mode, FP_RETURN);
5518 	}
5519     }
5520 
5521   return gen_rtx_REG (mode, GP_RETURN);
5522 }
5523 
5524 /* Implement TARGET_FUNCTION_VALUE.  */
5525 
5526 static rtx
5527 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
5528 		     bool outgoing ATTRIBUTE_UNUSED)
5529 {
5530   return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5531 }
5532 
5533 /* Implement TARGET_LIBCALL_VALUE.  */
5534 
5535 static rtx
5536 mips_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5537 {
5538   return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
5539 }
5540 
5541 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5542 
5543    On the MIPS, R2 R3 and F0 F2 are the only register thus used.
5544    Currently, R2 and F0 are only implemented here (C has no complex type).  */
5545 
5546 static bool
5547 mips_function_value_regno_p (const unsigned int regno)
5548 {
5549   if (regno == GP_RETURN
5550       || regno == FP_RETURN
5551       || (LONG_DOUBLE_TYPE_SIZE == 128
5552 	  && FP_RETURN != GP_RETURN
5553 	  && regno == FP_RETURN + 2))
5554     return true;
5555 
5556   return false;
5557 }
5558 
5559 /* Implement TARGET_RETURN_IN_MEMORY.  Under the o32 and o64 ABIs,
5560    all BLKmode objects are returned in memory.  Under the n32, n64
5561    and embedded ABIs, small structures are returned in a register.
5562    Objects with varying size must still be returned in memory, of
5563    course.  */
5564 
5565 static bool
5566 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5567 {
5568   return (TARGET_OLDABI
5569 	  ? TYPE_MODE (type) == BLKmode
5570 	  : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5571 }
5572 
5573 /* Implement TARGET_SETUP_INCOMING_VARARGS.  */
5574 
5575 static void
5576 mips_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
5577 			     tree type, int *pretend_size ATTRIBUTE_UNUSED,
5578 			     int no_rtl)
5579 {
5580   CUMULATIVE_ARGS local_cum;
5581   int gp_saved, fp_saved;
5582 
5583   /* The caller has advanced CUM up to, but not beyond, the last named
5584      argument.  Advance a local copy of CUM past the last "real" named
5585      argument, to find out how many registers are left over.  */
5586   local_cum = *get_cumulative_args (cum);
5587   mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
5588 			     true);
5589 
5590   /* Found out how many registers we need to save.  */
5591   gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
5592   fp_saved = (EABI_FLOAT_VARARGS_P
5593 	      ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
5594 	      : 0);
5595 
5596   if (!no_rtl)
5597     {
5598       if (gp_saved > 0)
5599 	{
5600 	  rtx ptr, mem;
5601 
5602 	  ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
5603 			       REG_PARM_STACK_SPACE (cfun->decl)
5604 			       - gp_saved * UNITS_PER_WORD);
5605 	  mem = gen_frame_mem (BLKmode, ptr);
5606 	  set_mem_alias_set (mem, get_varargs_alias_set ());
5607 
5608 	  move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
5609 			       mem, gp_saved);
5610 	}
5611       if (fp_saved > 0)
5612 	{
5613 	  /* We can't use move_block_from_reg, because it will use
5614 	     the wrong mode.  */
5615 	  enum machine_mode mode;
5616 	  int off, i;
5617 
5618 	  /* Set OFF to the offset from virtual_incoming_args_rtx of
5619 	     the first float register.  The FP save area lies below
5620 	     the integer one, and is aligned to UNITS_PER_FPVALUE bytes.  */
5621 	  off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
5622 	  off -= fp_saved * UNITS_PER_FPREG;
5623 
5624 	  mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
5625 
5626 	  for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
5627 	       i += MAX_FPRS_PER_FMT)
5628 	    {
5629 	      rtx ptr, mem;
5630 
5631 	      ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
5632 	      mem = gen_frame_mem (mode, ptr);
5633 	      set_mem_alias_set (mem, get_varargs_alias_set ());
5634 	      mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
5635 	      off += UNITS_PER_HWFPVALUE;
5636 	    }
5637 	}
5638     }
5639   if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
5640     cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
5641 				   + fp_saved * UNITS_PER_FPREG);
5642 }
5643 
5644 /* Implement TARGET_BUILTIN_VA_LIST.  */
5645 
5646 static tree
5647 mips_build_builtin_va_list (void)
5648 {
5649   if (EABI_FLOAT_VARARGS_P)
5650     {
5651       /* We keep 3 pointers, and two offsets.
5652 
5653 	 Two pointers are to the overflow area, which starts at the CFA.
5654 	 One of these is constant, for addressing into the GPR save area
5655 	 below it.  The other is advanced up the stack through the
5656 	 overflow region.
5657 
5658 	 The third pointer is to the bottom of the GPR save area.
5659 	 Since the FPR save area is just below it, we can address
5660 	 FPR slots off this pointer.
5661 
5662 	 We also keep two one-byte offsets, which are to be subtracted
5663 	 from the constant pointers to yield addresses in the GPR and
5664 	 FPR save areas.  These are downcounted as float or non-float
5665 	 arguments are used, and when they get to zero, the argument
5666 	 must be obtained from the overflow region.  */
5667       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
5668       tree array, index;
5669 
5670       record = lang_hooks.types.make_type (RECORD_TYPE);
5671 
5672       f_ovfl = build_decl (BUILTINS_LOCATION,
5673 			   FIELD_DECL, get_identifier ("__overflow_argptr"),
5674 			   ptr_type_node);
5675       f_gtop = build_decl (BUILTINS_LOCATION,
5676 			   FIELD_DECL, get_identifier ("__gpr_top"),
5677 			   ptr_type_node);
5678       f_ftop = build_decl (BUILTINS_LOCATION,
5679 			   FIELD_DECL, get_identifier ("__fpr_top"),
5680 			   ptr_type_node);
5681       f_goff = build_decl (BUILTINS_LOCATION,
5682 			   FIELD_DECL, get_identifier ("__gpr_offset"),
5683 			   unsigned_char_type_node);
5684       f_foff = build_decl (BUILTINS_LOCATION,
5685 			   FIELD_DECL, get_identifier ("__fpr_offset"),
5686 			   unsigned_char_type_node);
5687       /* Explicitly pad to the size of a pointer, so that -Wpadded won't
5688 	 warn on every user file.  */
5689       index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
5690       array = build_array_type (unsigned_char_type_node,
5691 			        build_index_type (index));
5692       f_res = build_decl (BUILTINS_LOCATION,
5693 			  FIELD_DECL, get_identifier ("__reserved"), array);
5694 
5695       DECL_FIELD_CONTEXT (f_ovfl) = record;
5696       DECL_FIELD_CONTEXT (f_gtop) = record;
5697       DECL_FIELD_CONTEXT (f_ftop) = record;
5698       DECL_FIELD_CONTEXT (f_goff) = record;
5699       DECL_FIELD_CONTEXT (f_foff) = record;
5700       DECL_FIELD_CONTEXT (f_res) = record;
5701 
5702       TYPE_FIELDS (record) = f_ovfl;
5703       DECL_CHAIN (f_ovfl) = f_gtop;
5704       DECL_CHAIN (f_gtop) = f_ftop;
5705       DECL_CHAIN (f_ftop) = f_goff;
5706       DECL_CHAIN (f_goff) = f_foff;
5707       DECL_CHAIN (f_foff) = f_res;
5708 
5709       layout_type (record);
5710       return record;
5711     }
5712   else
5713     /* Otherwise, we use 'void *'.  */
5714     return ptr_type_node;
5715 }
5716 
5717 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
5718 
5719 static void
5720 mips_va_start (tree valist, rtx nextarg)
5721 {
5722   if (EABI_FLOAT_VARARGS_P)
5723     {
5724       const CUMULATIVE_ARGS *cum;
5725       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5726       tree ovfl, gtop, ftop, goff, foff;
5727       tree t;
5728       int gpr_save_area_size;
5729       int fpr_save_area_size;
5730       int fpr_offset;
5731 
5732       cum = &crtl->args.info;
5733       gpr_save_area_size
5734 	= (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5735       fpr_save_area_size
5736 	= (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5737 
5738       f_ovfl = TYPE_FIELDS (va_list_type_node);
5739       f_gtop = DECL_CHAIN (f_ovfl);
5740       f_ftop = DECL_CHAIN (f_gtop);
5741       f_goff = DECL_CHAIN (f_ftop);
5742       f_foff = DECL_CHAIN (f_goff);
5743 
5744       ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5745 		     NULL_TREE);
5746       gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
5747 		     NULL_TREE);
5748       ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
5749 		     NULL_TREE);
5750       goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
5751 		     NULL_TREE);
5752       foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
5753 		     NULL_TREE);
5754 
5755       /* Emit code to initialize OVFL, which points to the next varargs
5756 	 stack argument.  CUM->STACK_WORDS gives the number of stack
5757 	 words used by named arguments.  */
5758       t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5759       if (cum->stack_words > 0)
5760 	t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
5761       t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5762       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5763 
5764       /* Emit code to initialize GTOP, the top of the GPR save area.  */
5765       t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5766       t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5767       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5768 
5769       /* Emit code to initialize FTOP, the top of the FPR save area.
5770 	 This address is gpr_save_area_bytes below GTOP, rounded
5771 	 down to the next fp-aligned boundary.  */
5772       t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5773       fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5774       fpr_offset &= -UNITS_PER_FPVALUE;
5775       if (fpr_offset)
5776 	t = fold_build_pointer_plus_hwi (t, -fpr_offset);
5777       t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5778       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5779 
5780       /* Emit code to initialize GOFF, the offset from GTOP of the
5781 	 next GPR argument.  */
5782       t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
5783 		  build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
5784       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5785 
5786       /* Likewise emit code to initialize FOFF, the offset from FTOP
5787 	 of the next FPR argument.  */
5788       t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
5789 		  build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
5790       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5791     }
5792   else
5793     {
5794       nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
5795       std_expand_builtin_va_start (valist, nextarg);
5796     }
5797 }
5798 
5799 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
5800    types as well.  */
5801 
5802 static tree
5803 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5804 			       gimple_seq *post_p)
5805 {
5806   tree addr, t, type_size, rounded_size, valist_tmp;
5807   unsigned HOST_WIDE_INT align, boundary;
5808   bool indirect;
5809 
5810   indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
5811   if (indirect)
5812     type = build_pointer_type (type);
5813 
5814   align = PARM_BOUNDARY / BITS_PER_UNIT;
5815   boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
5816 
5817   /* When we align parameter on stack for caller, if the parameter
5818      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
5819      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
5820      here with caller.  */
5821   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
5822     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
5823 
5824   boundary /= BITS_PER_UNIT;
5825 
5826   /* Hoist the valist value into a temporary for the moment.  */
5827   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
5828 
5829   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
5830      requires greater alignment, we must perform dynamic alignment.  */
5831   if (boundary > align)
5832     {
5833       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
5834 		  fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
5835       gimplify_and_add (t, pre_p);
5836 
5837       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
5838 		  fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
5839 			       valist_tmp,
5840 			       build_int_cst (TREE_TYPE (valist), -boundary)));
5841       gimplify_and_add (t, pre_p);
5842     }
5843   else
5844     boundary = align;
5845 
5846   /* If the actual alignment is less than the alignment of the type,
5847      adjust the type accordingly so that we don't assume strict alignment
5848      when dereferencing the pointer.  */
5849   boundary *= BITS_PER_UNIT;
5850   if (boundary < TYPE_ALIGN (type))
5851     {
5852       type = build_variant_type_copy (type);
5853       TYPE_ALIGN (type) = boundary;
5854     }
5855 
5856   /* Compute the rounded size of the type.  */
5857   type_size = size_in_bytes (type);
5858   rounded_size = round_up (type_size, align);
5859 
5860   /* Reduce rounded_size so it's sharable with the postqueue.  */
5861   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
5862 
5863   /* Get AP.  */
5864   addr = valist_tmp;
5865   if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
5866     {
5867       /* Small args are padded downward.  */
5868       t = fold_build2_loc (input_location, GT_EXPR, sizetype,
5869 		       rounded_size, size_int (align));
5870       t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
5871 		       size_binop (MINUS_EXPR, rounded_size, type_size));
5872       addr = fold_build_pointer_plus (addr, t);
5873     }
5874 
5875   /* Compute new value for AP.  */
5876   t = fold_build_pointer_plus (valist_tmp, rounded_size);
5877   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
5878   gimplify_and_add (t, pre_p);
5879 
5880   addr = fold_convert (build_pointer_type (type), addr);
5881 
5882   if (indirect)
5883     addr = build_va_arg_indirect_ref (addr);
5884 
5885   return build_va_arg_indirect_ref (addr);
5886 }
5887 
5888 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
5889 
5890 static tree
5891 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5892 			   gimple_seq *post_p)
5893 {
5894   tree addr;
5895   bool indirect_p;
5896 
5897   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
5898   if (indirect_p)
5899     type = build_pointer_type (type);
5900 
5901   if (!EABI_FLOAT_VARARGS_P)
5902     addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5903   else
5904     {
5905       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5906       tree ovfl, top, off, align;
5907       HOST_WIDE_INT size, rsize, osize;
5908       tree t, u;
5909 
5910       f_ovfl = TYPE_FIELDS (va_list_type_node);
5911       f_gtop = DECL_CHAIN (f_ovfl);
5912       f_ftop = DECL_CHAIN (f_gtop);
5913       f_goff = DECL_CHAIN (f_ftop);
5914       f_foff = DECL_CHAIN (f_goff);
5915 
5916       /* Let:
5917 
5918 	 TOP be the top of the GPR or FPR save area;
5919 	 OFF be the offset from TOP of the next register;
5920 	 ADDR_RTX be the address of the argument;
5921 	 SIZE be the number of bytes in the argument type;
5922 	 RSIZE be the number of bytes used to store the argument
5923 	   when it's in the register save area; and
5924 	 OSIZE be the number of bytes used to store it when it's
5925 	   in the stack overflow area.
5926 
5927 	 The code we want is:
5928 
5929 	 1: off &= -rsize;	  // round down
5930 	 2: if (off != 0)
5931 	 3:   {
5932 	 4:	addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
5933 	 5:	off -= rsize;
5934 	 6:   }
5935 	 7: else
5936 	 8:   {
5937 	 9:	ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
5938 	 10:	addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
5939 	 11:	ovfl += osize;
5940 	 14:  }
5941 
5942 	 [1] and [9] can sometimes be optimized away.  */
5943 
5944       ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5945 		     NULL_TREE);
5946       size = int_size_in_bytes (type);
5947 
5948       if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
5949 	  && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
5950 	{
5951 	  top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
5952 			unshare_expr (valist), f_ftop, NULL_TREE);
5953 	  off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
5954 			unshare_expr (valist), f_foff, NULL_TREE);
5955 
5956 	  /* When va_start saves FPR arguments to the stack, each slot
5957 	     takes up UNITS_PER_HWFPVALUE bytes, regardless of the
5958 	     argument's precision.  */
5959 	  rsize = UNITS_PER_HWFPVALUE;
5960 
5961 	  /* Overflow arguments are padded to UNITS_PER_WORD bytes
5962 	     (= PARM_BOUNDARY bits).  This can be different from RSIZE
5963 	     in two cases:
5964 
5965 	     (1) On 32-bit targets when TYPE is a structure such as:
5966 
5967 	     struct s { float f; };
5968 
5969 	     Such structures are passed in paired FPRs, so RSIZE
5970 	     will be 8 bytes.  However, the structure only takes
5971 	     up 4 bytes of memory, so OSIZE will only be 4.
5972 
5973 	     (2) In combinations such as -mgp64 -msingle-float
5974 	     -fshort-double.  Doubles passed in registers will then take
5975 	     up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
5976 	     stack take up UNITS_PER_WORD bytes.  */
5977 	  osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
5978 	}
5979       else
5980 	{
5981 	  top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
5982 			unshare_expr (valist), f_gtop, NULL_TREE);
5983 	  off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
5984 			unshare_expr (valist), f_goff, NULL_TREE);
5985 	  rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
5986 	  if (rsize > UNITS_PER_WORD)
5987 	    {
5988 	      /* [1] Emit code for: off &= -rsize.	*/
5989 	      t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
5990 			  build_int_cst (TREE_TYPE (off), -rsize));
5991 	      gimplify_assign (unshare_expr (off), t, pre_p);
5992 	    }
5993 	  osize = rsize;
5994 	}
5995 
5996       /* [2] Emit code to branch if off == 0.  */
5997       t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
5998 		  build_int_cst (TREE_TYPE (off), 0));
5999       addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
6000 
6001       /* [5] Emit code for: off -= rsize.  We do this as a form of
6002 	 post-decrement not available to C.  */
6003       t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
6004       t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
6005 
6006       /* [4] Emit code for:
6007 	 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0).  */
6008       t = fold_convert (sizetype, t);
6009       t = fold_build1 (NEGATE_EXPR, sizetype, t);
6010       t = fold_build_pointer_plus (top, t);
6011       if (BYTES_BIG_ENDIAN && rsize > size)
6012 	t = fold_build_pointer_plus_hwi (t, rsize - size);
6013       COND_EXPR_THEN (addr) = t;
6014 
6015       if (osize > UNITS_PER_WORD)
6016 	{
6017 	  /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize.  */
6018 	  t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
6019 	  u = build_int_cst (TREE_TYPE (t), -osize);
6020 	  t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
6021 	  align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
6022 			  unshare_expr (ovfl), t);
6023 	}
6024       else
6025 	align = NULL;
6026 
6027       /* [10, 11] Emit code for:
6028 	 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
6029 	 ovfl += osize.  */
6030       u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
6031       t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
6032       if (BYTES_BIG_ENDIAN && osize > size)
6033 	t = fold_build_pointer_plus_hwi (t, osize - size);
6034 
6035       /* String [9] and [10, 11] together.  */
6036       if (align)
6037 	t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
6038       COND_EXPR_ELSE (addr) = t;
6039 
6040       addr = fold_convert (build_pointer_type (type), addr);
6041       addr = build_va_arg_indirect_ref (addr);
6042     }
6043 
6044   if (indirect_p)
6045     addr = build_va_arg_indirect_ref (addr);
6046 
6047   return addr;
6048 }
6049 
6050 /* Declare a unique, locally-binding function called NAME, then start
6051    its definition.  */
6052 
6053 static void
6054 mips_start_unique_function (const char *name)
6055 {
6056   tree decl;
6057 
6058   decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
6059 		     get_identifier (name),
6060 		     build_function_type_list (void_type_node, NULL_TREE));
6061   DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
6062 				   NULL_TREE, void_type_node);
6063   TREE_PUBLIC (decl) = 1;
6064   TREE_STATIC (decl) = 1;
6065 
6066   DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
6067 
6068   targetm.asm_out.unique_section (decl, 0);
6069   switch_to_section (get_named_section (decl, NULL, 0));
6070 
6071   targetm.asm_out.globalize_label (asm_out_file, name);
6072   fputs ("\t.hidden\t", asm_out_file);
6073   assemble_name (asm_out_file, name);
6074   putc ('\n', asm_out_file);
6075 }
6076 
6077 /* Start a definition of function NAME.  MIPS16_P indicates whether the
6078    function contains MIPS16 code.  */
6079 
6080 static void
6081 mips_start_function_definition (const char *name, bool mips16_p)
6082 {
6083   if (mips16_p)
6084     fprintf (asm_out_file, "\t.set\tmips16\n");
6085   else
6086     fprintf (asm_out_file, "\t.set\tnomips16\n");
6087 
6088   if (!flag_inhibit_size_directive)
6089     {
6090       fputs ("\t.ent\t", asm_out_file);
6091       assemble_name (asm_out_file, name);
6092       fputs ("\n", asm_out_file);
6093     }
6094 
6095   ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
6096 
6097   /* Start the definition proper.  */
6098   assemble_name (asm_out_file, name);
6099   fputs (":\n", asm_out_file);
6100 }
6101 
6102 /* End a function definition started by mips_start_function_definition.  */
6103 
6104 static void
6105 mips_end_function_definition (const char *name)
6106 {
6107   if (!flag_inhibit_size_directive)
6108     {
6109       fputs ("\t.end\t", asm_out_file);
6110       assemble_name (asm_out_file, name);
6111       fputs ("\n", asm_out_file);
6112     }
6113 }
6114 
6115 /* Output a definition of the __mips16_rdhwr function.  */
6116 
6117 static void
6118 mips_output_mips16_rdhwr (void)
6119 {
6120   const char *name;
6121 
6122   name = "__mips16_rdhwr";
6123   mips_start_unique_function (name);
6124   mips_start_function_definition (name, false);
6125   fprintf (asm_out_file,
6126 	   "\t.set\tpush\n"
6127 	   "\t.set\tmips32r2\n"
6128 	   "\t.set\tnoreorder\n"
6129 	   "\trdhwr\t$3,$29\n"
6130 	   "\t.set\tpop\n"
6131 	   "\tj\t$31\n");
6132   mips_end_function_definition (name);
6133 }
6134 
6135 /* Return true if calls to X can use R_MIPS_CALL* relocations.  */
6136 
6137 static bool
6138 mips_ok_for_lazy_binding_p (rtx x)
6139 {
6140   return (TARGET_USE_GOT
6141 	  && GET_CODE (x) == SYMBOL_REF
6142 	  && !SYMBOL_REF_BIND_NOW_P (x)
6143 	  && !mips_symbol_binds_local_p (x));
6144 }
6145 
6146 /* Load function address ADDR into register DEST.  TYPE is as for
6147    mips_expand_call.  Return true if we used an explicit lazy-binding
6148    sequence.  */
6149 
6150 static bool
6151 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
6152 {
6153   /* If we're generating PIC, and this call is to a global function,
6154      try to allow its address to be resolved lazily.  This isn't
6155      possible for sibcalls when $gp is call-saved because the value
6156      of $gp on entry to the stub would be our caller's gp, not ours.  */
6157   if (TARGET_EXPLICIT_RELOCS
6158       && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
6159       && mips_ok_for_lazy_binding_p (addr))
6160     {
6161       addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
6162       emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
6163       return true;
6164     }
6165   else
6166     {
6167       mips_emit_move (dest, addr);
6168       return false;
6169     }
6170 }
6171 
6172 /* Each locally-defined hard-float MIPS16 function has a local symbol
6173    associated with it.  This hash table maps the function symbol (FUNC)
6174    to the local symbol (LOCAL). */
6175 struct GTY(()) mips16_local_alias {
6176   rtx func;
6177   rtx local;
6178 };
6179 static GTY ((param_is (struct mips16_local_alias))) htab_t mips16_local_aliases;
6180 
6181 /* Hash table callbacks for mips16_local_aliases.  */
6182 
6183 static hashval_t
6184 mips16_local_aliases_hash (const void *entry)
6185 {
6186   const struct mips16_local_alias *alias;
6187 
6188   alias = (const struct mips16_local_alias *) entry;
6189   return htab_hash_string (XSTR (alias->func, 0));
6190 }
6191 
6192 static int
6193 mips16_local_aliases_eq (const void *entry1, const void *entry2)
6194 {
6195   const struct mips16_local_alias *alias1, *alias2;
6196 
6197   alias1 = (const struct mips16_local_alias *) entry1;
6198   alias2 = (const struct mips16_local_alias *) entry2;
6199   return rtx_equal_p (alias1->func, alias2->func);
6200 }
6201 
6202 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
6203    Return a local alias for it, creating a new one if necessary.  */
6204 
6205 static rtx
6206 mips16_local_alias (rtx func)
6207 {
6208   struct mips16_local_alias *alias, tmp_alias;
6209   void **slot;
6210 
6211   /* Create the hash table if this is the first call.  */
6212   if (mips16_local_aliases == NULL)
6213     mips16_local_aliases = htab_create_ggc (37, mips16_local_aliases_hash,
6214 					    mips16_local_aliases_eq, NULL);
6215 
6216   /* Look up the function symbol, creating a new entry if need be.  */
6217   tmp_alias.func = func;
6218   slot = htab_find_slot (mips16_local_aliases, &tmp_alias, INSERT);
6219   gcc_assert (slot != NULL);
6220 
6221   alias = (struct mips16_local_alias *) *slot;
6222   if (alias == NULL)
6223     {
6224       const char *func_name, *local_name;
6225       rtx local;
6226 
6227       /* Create a new SYMBOL_REF for the local symbol.  The choice of
6228 	 __fn_local_* is based on the __fn_stub_* names that we've
6229 	 traditionally used for the non-MIPS16 stub.  */
6230       func_name = targetm.strip_name_encoding (XSTR (func, 0));
6231       local_name = ACONCAT (("__fn_local_", func_name, NULL));
6232       local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
6233       SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
6234 
6235       /* Create a new structure to represent the mapping.  */
6236       alias = ggc_alloc_mips16_local_alias ();
6237       alias->func = func;
6238       alias->local = local;
6239       *slot = alias;
6240     }
6241   return alias->local;
6242 }
6243 
6244 /* A chained list of functions for which mips16_build_call_stub has already
6245    generated a stub.  NAME is the name of the function and FP_RET_P is true
6246    if the function returns a value in floating-point registers.  */
6247 struct mips16_stub {
6248   struct mips16_stub *next;
6249   char *name;
6250   bool fp_ret_p;
6251 };
6252 static struct mips16_stub *mips16_stubs;
6253 
6254 /* Return the two-character string that identifies floating-point
6255    return mode MODE in the name of a MIPS16 function stub.  */
6256 
6257 static const char *
6258 mips16_call_stub_mode_suffix (enum machine_mode mode)
6259 {
6260   if (mode == SFmode)
6261     return "sf";
6262   else if (mode == DFmode)
6263     return "df";
6264   else if (mode == SCmode)
6265     return "sc";
6266   else if (mode == DCmode)
6267     return "dc";
6268   else if (mode == V2SFmode)
6269     return "df";
6270   else
6271     gcc_unreachable ();
6272 }
6273 
6274 /* Write instructions to move a 32-bit value between general register
6275    GPREG and floating-point register FPREG.  DIRECTION is 't' to move
6276    from GPREG to FPREG and 'f' to move in the opposite direction.  */
6277 
6278 static void
6279 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6280 {
6281   fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6282 	   reg_names[gpreg], reg_names[fpreg]);
6283 }
6284 
6285 /* Likewise for 64-bit values.  */
6286 
6287 static void
6288 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6289 {
6290   if (TARGET_64BIT)
6291     fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
6292  	     reg_names[gpreg], reg_names[fpreg]);
6293   else if (TARGET_FLOAT64)
6294     {
6295       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6296  	       reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6297       fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
6298  	       reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
6299     }
6300   else
6301     {
6302       /* Move the least-significant word.  */
6303       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6304 	       reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6305       /* ...then the most significant word.  */
6306       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6307 	       reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
6308     }
6309 }
6310 
6311 /* Write out code to move floating-point arguments into or out of
6312    general registers.  FP_CODE is the code describing which arguments
6313    are present (see the comment above the definition of CUMULATIVE_ARGS
6314    in mips.h).  DIRECTION is as for mips_output_32bit_xfer.  */
6315 
6316 static void
6317 mips_output_args_xfer (int fp_code, char direction)
6318 {
6319   unsigned int gparg, fparg, f;
6320   CUMULATIVE_ARGS cum;
6321 
6322   /* This code only works for o32 and o64.  */
6323   gcc_assert (TARGET_OLDABI);
6324 
6325   mips_init_cumulative_args (&cum, NULL);
6326 
6327   for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6328     {
6329       enum machine_mode mode;
6330       struct mips_arg_info info;
6331 
6332       if ((f & 3) == 1)
6333 	mode = SFmode;
6334       else if ((f & 3) == 2)
6335 	mode = DFmode;
6336       else
6337 	gcc_unreachable ();
6338 
6339       mips_get_arg_info (&info, &cum, mode, NULL, true);
6340       gparg = mips_arg_regno (&info, false);
6341       fparg = mips_arg_regno (&info, true);
6342 
6343       if (mode == SFmode)
6344 	mips_output_32bit_xfer (direction, gparg, fparg);
6345       else
6346 	mips_output_64bit_xfer (direction, gparg, fparg);
6347 
6348       mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
6349     }
6350 }
6351 
6352 /* Write a MIPS16 stub for the current function.  This stub is used
6353    for functions which take arguments in the floating-point registers.
6354    It is normal-mode code that moves the floating-point arguments
6355    into the general registers and then jumps to the MIPS16 code.  */
6356 
6357 static void
6358 mips16_build_function_stub (void)
6359 {
6360   const char *fnname, *alias_name, *separator;
6361   char *secname, *stubname;
6362   tree stubdecl;
6363   unsigned int f;
6364   rtx symbol, alias;
6365 
6366   /* Create the name of the stub, and its unique section.  */
6367   symbol = XEXP (DECL_RTL (current_function_decl), 0);
6368   alias = mips16_local_alias (symbol);
6369 
6370   fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6371   alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6372   secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6373   stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6374 
6375   /* Build a decl for the stub.  */
6376   stubdecl = build_decl (BUILTINS_LOCATION,
6377 			 FUNCTION_DECL, get_identifier (stubname),
6378 			 build_function_type_list (void_type_node, NULL_TREE));
6379   DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6380   DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6381 				       RESULT_DECL, NULL_TREE, void_type_node);
6382 
6383   /* Output a comment.  */
6384   fprintf (asm_out_file, "\t# Stub function for %s (",
6385 	   current_function_name ());
6386   separator = "";
6387   for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6388     {
6389       fprintf (asm_out_file, "%s%s", separator,
6390 	       (f & 3) == 1 ? "float" : "double");
6391       separator = ", ";
6392     }
6393   fprintf (asm_out_file, ")\n");
6394 
6395   /* Start the function definition.  */
6396   assemble_start_function (stubdecl, stubname);
6397   mips_start_function_definition (stubname, false);
6398 
6399   /* If generating pic2 code, either set up the global pointer or
6400      switch to pic0.  */
6401   if (TARGET_ABICALLS_PIC2)
6402     {
6403       if (TARGET_ABSOLUTE_ABICALLS)
6404 	fprintf (asm_out_file, "\t.option\tpic0\n");
6405       else
6406 	{
6407 	  output_asm_insn ("%(.cpload\t%^%)", NULL);
6408 	  /* Emit an R_MIPS_NONE relocation to tell the linker what the
6409 	     target function is.  Use a local GOT access when loading the
6410 	     symbol, to cut down on the number of unnecessary GOT entries
6411 	     for stubs that aren't needed.  */
6412 	  output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6413 	  symbol = alias;
6414 	}
6415     }
6416 
6417   /* Load the address of the MIPS16 function into $25.  Do this first so
6418      that targets with coprocessor interlocks can use an MFC1 to fill the
6419      delay slot.  */
6420   output_asm_insn ("la\t%^,%0", &symbol);
6421 
6422   /* Move the arguments from floating-point registers to general registers.  */
6423   mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6424 
6425   /* Jump to the MIPS16 function.  */
6426   output_asm_insn ("jr\t%^", NULL);
6427 
6428   if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6429     fprintf (asm_out_file, "\t.option\tpic2\n");
6430 
6431   mips_end_function_definition (stubname);
6432 
6433   /* If the linker needs to create a dynamic symbol for the target
6434      function, it will associate the symbol with the stub (which,
6435      unlike the target function, follows the proper calling conventions).
6436      It is therefore useful to have a local alias for the target function,
6437      so that it can still be identified as MIPS16 code.  As an optimization,
6438      this symbol can also be used for indirect MIPS16 references from
6439      within this file.  */
6440   ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6441 
6442   switch_to_section (function_section (current_function_decl));
6443 }
6444 
6445 /* The current function is a MIPS16 function that returns a value in an FPR.
6446    Copy the return value from its soft-float to its hard-float location.
6447    libgcc2 has special non-MIPS16 helper functions for each case.  */
6448 
6449 static void
6450 mips16_copy_fpr_return_value (void)
6451 {
6452   rtx fn, insn, retval;
6453   tree return_type;
6454   enum machine_mode return_mode;
6455   const char *name;
6456 
6457   return_type = DECL_RESULT (current_function_decl);
6458   return_mode = DECL_MODE (return_type);
6459 
6460   name = ACONCAT (("__mips16_ret_",
6461 		   mips16_call_stub_mode_suffix (return_mode),
6462 		   NULL));
6463   fn = mips16_stub_function (name);
6464 
6465   /* The function takes arguments in $2 (and possibly $3), so calls
6466      to it cannot be lazily bound.  */
6467   SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6468 
6469   /* Model the call as something that takes the GPR return value as
6470      argument and returns an "updated" value.  */
6471   retval = gen_rtx_REG (return_mode, GP_RETURN);
6472   insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6473 			   const0_rtx, NULL_RTX, false);
6474   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6475 }
6476 
6477 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6478    RETVAL is the location of the return value, or null if this is
6479    a "call" rather than a "call_value".  ARGS_SIZE is the size of the
6480    arguments and FP_CODE is the code built by mips_function_arg;
6481    see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6482 
6483    There are three alternatives:
6484 
6485    - If a stub was needed, emit the call and return the call insn itself.
6486 
6487    - If we can avoid using a stub by redirecting the call, set *FN_PTR
6488      to the new target and return null.
6489 
6490    - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6491      unmodified.
6492 
6493    A stub is needed for calls to functions that, in normal mode,
6494    receive arguments in FPRs or return values in FPRs.  The stub
6495    copies the arguments from their soft-float positions to their
6496    hard-float positions, calls the real function, then copies the
6497    return value from its hard-float position to its soft-float
6498    position.
6499 
6500    We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6501    If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6502    automatically redirects the JAL to the stub, otherwise the JAL
6503    continues to call FN directly.  */
6504 
6505 static rtx
6506 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6507 {
6508   const char *fnname;
6509   bool fp_ret_p;
6510   struct mips16_stub *l;
6511   rtx insn, fn;
6512 
6513   /* We don't need to do anything if we aren't in MIPS16 mode, or if
6514      we were invoked with the -msoft-float option.  */
6515   if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
6516     return NULL_RTX;
6517 
6518   /* Figure out whether the value might come back in a floating-point
6519      register.  */
6520   fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
6521 
6522   /* We don't need to do anything if there were no floating-point
6523      arguments and the value will not be returned in a floating-point
6524      register.  */
6525   if (fp_code == 0 && !fp_ret_p)
6526     return NULL_RTX;
6527 
6528   /* We don't need to do anything if this is a call to a special
6529      MIPS16 support function.  */
6530   fn = *fn_ptr;
6531   if (mips16_stub_function_p (fn))
6532     return NULL_RTX;
6533 
6534   /* If we're calling a locally-defined MIPS16 function, we know that
6535      it will return values in both the "soft-float" and "hard-float"
6536      registers.  There is no need to use a stub to move the latter
6537      to the former.  */
6538   if (fp_code == 0 && mips16_local_function_p (fn))
6539     return NULL_RTX;
6540 
6541   /* This code will only work for o32 and o64 abis.  The other ABI's
6542      require more sophisticated support.  */
6543   gcc_assert (TARGET_OLDABI);
6544 
6545   /* If we're calling via a function pointer, use one of the magic
6546      libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
6547      Each stub expects the function address to arrive in register $2.  */
6548   if (GET_CODE (fn) != SYMBOL_REF
6549       || !call_insn_operand (fn, VOIDmode))
6550     {
6551       char buf[30];
6552       rtx stub_fn, insn, addr;
6553       bool lazy_p;
6554 
6555       /* If this is a locally-defined and locally-binding function,
6556 	 avoid the stub by calling the local alias directly.  */
6557       if (mips16_local_function_p (fn))
6558 	{
6559 	  *fn_ptr = mips16_local_alias (fn);
6560 	  return NULL_RTX;
6561 	}
6562 
6563       /* Create a SYMBOL_REF for the libgcc.a function.  */
6564       if (fp_ret_p)
6565 	sprintf (buf, "__mips16_call_stub_%s_%d",
6566 		 mips16_call_stub_mode_suffix (GET_MODE (retval)),
6567 		 fp_code);
6568       else
6569 	sprintf (buf, "__mips16_call_stub_%d", fp_code);
6570       stub_fn = mips16_stub_function (buf);
6571 
6572       /* The function uses $2 as an argument, so calls to it
6573 	 cannot be lazily bound.  */
6574       SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6575 
6576       /* Load the target function into $2.  */
6577       addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6578       lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
6579 
6580       /* Emit the call.  */
6581       insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
6582 			       args_size, NULL_RTX, lazy_p);
6583 
6584       /* Tell GCC that this call does indeed use the value of $2.  */
6585       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
6586 
6587       /* If we are handling a floating-point return value, we need to
6588          save $18 in the function prologue.  Putting a note on the
6589          call will mean that df_regs_ever_live_p ($18) will be true if the
6590          call is not eliminated, and we can check that in the prologue
6591          code.  */
6592       if (fp_ret_p)
6593 	CALL_INSN_FUNCTION_USAGE (insn) =
6594 	  gen_rtx_EXPR_LIST (VOIDmode,
6595 			     gen_rtx_CLOBBER (VOIDmode,
6596 					      gen_rtx_REG (word_mode, 18)),
6597 			     CALL_INSN_FUNCTION_USAGE (insn));
6598 
6599       return insn;
6600     }
6601 
6602   /* We know the function we are going to call.  If we have already
6603      built a stub, we don't need to do anything further.  */
6604   fnname = targetm.strip_name_encoding (XSTR (fn, 0));
6605   for (l = mips16_stubs; l != NULL; l = l->next)
6606     if (strcmp (l->name, fnname) == 0)
6607       break;
6608 
6609   if (l == NULL)
6610     {
6611       const char *separator;
6612       char *secname, *stubname;
6613       tree stubid, stubdecl;
6614       unsigned int f;
6615 
6616       /* If the function does not return in FPRs, the special stub
6617 	 section is named
6618 	     .mips16.call.FNNAME
6619 
6620 	 If the function does return in FPRs, the stub section is named
6621 	     .mips16.call.fp.FNNAME
6622 
6623 	 Build a decl for the stub.  */
6624       secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
6625 			  fnname, NULL));
6626       stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
6627 			   fnname, NULL));
6628       stubid = get_identifier (stubname);
6629       stubdecl = build_decl (BUILTINS_LOCATION,
6630 			     FUNCTION_DECL, stubid,
6631 			     build_function_type_list (void_type_node,
6632 						       NULL_TREE));
6633       DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6634       DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6635 					   RESULT_DECL, NULL_TREE,
6636 					   void_type_node);
6637 
6638       /* Output a comment.  */
6639       fprintf (asm_out_file, "\t# Stub function to call %s%s (",
6640 	       (fp_ret_p
6641 		? (GET_MODE (retval) == SFmode ? "float " : "double ")
6642 		: ""),
6643 	       fnname);
6644       separator = "";
6645       for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6646 	{
6647 	  fprintf (asm_out_file, "%s%s", separator,
6648 		   (f & 3) == 1 ? "float" : "double");
6649 	  separator = ", ";
6650 	}
6651       fprintf (asm_out_file, ")\n");
6652 
6653       /* Start the function definition.  */
6654       assemble_start_function (stubdecl, stubname);
6655       mips_start_function_definition (stubname, false);
6656 
6657       if (fp_ret_p)
6658 	{
6659 	  fprintf (asm_out_file, "\t.cfi_startproc\n");
6660 
6661 	  /* Create a fake CFA 4 bytes below the stack pointer.
6662 	     This works around unwinders (like libgcc's) that expect
6663 	     the CFA for non-signal frames to be unique.  */
6664 	  fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
6665 
6666 	  /* "Save" $sp in itself so we don't use the fake CFA.
6667 	     This is: DW_CFA_val_expression r29, { DW_OP_reg29 }.  */
6668 	  fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
6669 	}
6670       else
6671 	{
6672 	  /* Load the address of the MIPS16 function into $25.  Do this
6673 	     first so that targets with coprocessor interlocks can use
6674 	     an MFC1 to fill the delay slot.  */
6675 	  if (TARGET_EXPLICIT_RELOCS)
6676 	    {
6677 	      output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
6678 	      output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
6679 	    }
6680 	  else
6681 	    output_asm_insn ("la\t%^,%0", &fn);
6682 	}
6683 
6684       /* Move the arguments from general registers to floating-point
6685 	 registers.  */
6686       mips_output_args_xfer (fp_code, 't');
6687 
6688       if (fp_ret_p)
6689 	{
6690 	  /* Save the return address in $18 and call the non-MIPS16 function.
6691 	     The stub's caller knows that $18 might be clobbered, even though
6692 	     $18 is usually a call-saved register.  */
6693 	  fprintf (asm_out_file, "\tmove\t%s,%s\n",
6694 		   reg_names[GP_REG_FIRST + 18], reg_names[RETURN_ADDR_REGNUM]);
6695 	  output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
6696 	  fprintf (asm_out_file, "\t.cfi_register 31,18\n");
6697 
6698 	  /* Move the result from floating-point registers to
6699 	     general registers.  */
6700 	  switch (GET_MODE (retval))
6701 	    {
6702 	    case SCmode:
6703 	      mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
6704 				      TARGET_BIG_ENDIAN
6705 				      ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6706 				      : FP_REG_FIRST);
6707 	      mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
6708 				      TARGET_LITTLE_ENDIAN
6709 				      ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6710 				      : FP_REG_FIRST);
6711 	      if (GET_MODE (retval) == SCmode && TARGET_64BIT)
6712 		{
6713 		  /* On 64-bit targets, complex floats are returned in
6714 		     a single GPR, such that "sd" on a suitably-aligned
6715 		     target would store the value correctly.  */
6716 		  fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6717 			   reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6718 			   reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6719 		  fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6720 			   reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
6721 			   reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
6722 		  fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
6723 			   reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6724 			   reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6725 		  fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
6726 			   reg_names[GP_RETURN],
6727 			   reg_names[GP_RETURN],
6728 			   reg_names[GP_RETURN + 1]);
6729 		}
6730 	      break;
6731 
6732 	    case SFmode:
6733 	      mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6734 	      break;
6735 
6736 	    case DCmode:
6737 	      mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
6738 				      FP_REG_FIRST + MAX_FPRS_PER_FMT);
6739 	      /* Fall though.  */
6740  	    case DFmode:
6741 	    case V2SFmode:
6742 	      mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6743 	      break;
6744 
6745 	    default:
6746 	      gcc_unreachable ();
6747 	    }
6748 	  fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
6749 	  fprintf (asm_out_file, "\t.cfi_endproc\n");
6750 	}
6751       else
6752 	{
6753 	  /* Jump to the previously-loaded address.  */
6754 	  output_asm_insn ("jr\t%^", NULL);
6755 	}
6756 
6757 #ifdef ASM_DECLARE_FUNCTION_SIZE
6758       ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
6759 #endif
6760 
6761       mips_end_function_definition (stubname);
6762 
6763       /* Record this stub.  */
6764       l = XNEW (struct mips16_stub);
6765       l->name = xstrdup (fnname);
6766       l->fp_ret_p = fp_ret_p;
6767       l->next = mips16_stubs;
6768       mips16_stubs = l;
6769     }
6770 
6771   /* If we expect a floating-point return value, but we've built a
6772      stub which does not expect one, then we're in trouble.  We can't
6773      use the existing stub, because it won't handle the floating-point
6774      value.  We can't build a new stub, because the linker won't know
6775      which stub to use for the various calls in this object file.
6776      Fortunately, this case is illegal, since it means that a function
6777      was declared in two different ways in a single compilation.  */
6778   if (fp_ret_p && !l->fp_ret_p)
6779     error ("cannot handle inconsistent calls to %qs", fnname);
6780 
6781   if (retval == NULL_RTX)
6782     insn = gen_call_internal_direct (fn, args_size);
6783   else
6784     insn = gen_call_value_internal_direct (retval, fn, args_size);
6785   insn = mips_emit_call_insn (insn, fn, fn, false);
6786 
6787   /* If we are calling a stub which handles a floating-point return
6788      value, we need to arrange to save $18 in the prologue.  We do this
6789      by marking the function call as using the register.  The prologue
6790      will later see that it is used, and emit code to save it.  */
6791   if (fp_ret_p)
6792     CALL_INSN_FUNCTION_USAGE (insn) =
6793       gen_rtx_EXPR_LIST (VOIDmode,
6794 			 gen_rtx_CLOBBER (VOIDmode,
6795 					  gen_rtx_REG (word_mode, 18)),
6796 			 CALL_INSN_FUNCTION_USAGE (insn));
6797 
6798   return insn;
6799 }
6800 
6801 /* Expand a call of type TYPE.  RESULT is where the result will go (null
6802    for "call"s and "sibcall"s), ADDR is the address of the function,
6803    ARGS_SIZE is the size of the arguments and AUX is the value passed
6804    to us by mips_function_arg.  LAZY_P is true if this call already
6805    involves a lazily-bound function address (such as when calling
6806    functions through a MIPS16 hard-float stub).
6807 
6808    Return the call itself.  */
6809 
6810 rtx
6811 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
6812 		  rtx args_size, rtx aux, bool lazy_p)
6813 {
6814   rtx orig_addr, pattern, insn;
6815   int fp_code;
6816 
6817   fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
6818   insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
6819   if (insn)
6820     {
6821       gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
6822       return insn;
6823     }
6824 				 ;
6825   orig_addr = addr;
6826   if (!call_insn_operand (addr, VOIDmode))
6827     {
6828       if (type == MIPS_CALL_EPILOGUE)
6829 	addr = MIPS_EPILOGUE_TEMP (Pmode);
6830       else
6831 	addr = gen_reg_rtx (Pmode);
6832       lazy_p |= mips_load_call_address (type, addr, orig_addr);
6833     }
6834 
6835   if (result == 0)
6836     {
6837       rtx (*fn) (rtx, rtx);
6838 
6839       if (type == MIPS_CALL_SIBCALL)
6840 	fn = gen_sibcall_internal;
6841       else
6842 	fn = gen_call_internal;
6843 
6844       pattern = fn (addr, args_size);
6845     }
6846   else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
6847     {
6848       /* Handle return values created by mips_return_fpr_pair.  */
6849       rtx (*fn) (rtx, rtx, rtx, rtx);
6850       rtx reg1, reg2;
6851 
6852       if (type == MIPS_CALL_SIBCALL)
6853 	fn = gen_sibcall_value_multiple_internal;
6854       else
6855 	fn = gen_call_value_multiple_internal;
6856 
6857       reg1 = XEXP (XVECEXP (result, 0, 0), 0);
6858       reg2 = XEXP (XVECEXP (result, 0, 1), 0);
6859       pattern = fn (reg1, addr, args_size, reg2);
6860     }
6861   else
6862     {
6863       rtx (*fn) (rtx, rtx, rtx);
6864 
6865       if (type == MIPS_CALL_SIBCALL)
6866 	fn = gen_sibcall_value_internal;
6867       else
6868 	fn = gen_call_value_internal;
6869 
6870       /* Handle return values created by mips_return_fpr_single.  */
6871       if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
6872 	result = XEXP (XVECEXP (result, 0, 0), 0);
6873       pattern = fn (result, addr, args_size);
6874     }
6875 
6876   return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
6877 }
6878 
6879 /* Split call instruction INSN into a $gp-clobbering call and
6880    (where necessary) an instruction to restore $gp from its save slot.
6881    CALL_PATTERN is the pattern of the new call.  */
6882 
6883 void
6884 mips_split_call (rtx insn, rtx call_pattern)
6885 {
6886   emit_call_insn (call_pattern);
6887   if (!find_reg_note (insn, REG_NORETURN, 0))
6888     /* Pick a temporary register that is suitable for both MIPS16 and
6889        non-MIPS16 code.  $4 and $5 are used for returning complex double
6890        values in soft-float code, so $6 is the first suitable candidate.  */
6891     mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode, GP_ARG_FIRST + 2));
6892 }
6893 
6894 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL.  */
6895 
6896 static bool
6897 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
6898 {
6899   if (!TARGET_SIBCALLS)
6900     return false;
6901 
6902   /* Interrupt handlers need special epilogue code and therefore can't
6903      use sibcalls.  */
6904   if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
6905     return false;
6906 
6907   /* We can't do a sibcall if the called function is a MIPS16 function
6908      because there is no direct "jx" instruction equivalent to "jalx" to
6909      switch the ISA mode.  We only care about cases where the sibling
6910      and normal calls would both be direct.  */
6911   if (decl
6912       && mips_use_mips16_mode_p (decl)
6913       && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode))
6914     return false;
6915 
6916   /* When -minterlink-mips16 is in effect, assume that non-locally-binding
6917      functions could be MIPS16 ones unless an attribute explicitly tells
6918      us otherwise.  */
6919   if (TARGET_INTERLINK_MIPS16
6920       && decl
6921       && (DECL_EXTERNAL (decl) || !targetm.binds_local_p (decl))
6922       && !mips_nomips16_decl_p (decl)
6923       && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode))
6924     return false;
6925 
6926   /* Otherwise OK.  */
6927   return true;
6928 }
6929 
6930 /* Emit code to move general operand SRC into condition-code
6931    register DEST given that SCRATCH is a scratch TFmode FPR.
6932    The sequence is:
6933 
6934 	FP1 = SRC
6935 	FP2 = 0.0f
6936 	DEST = FP2 < FP1
6937 
6938    where FP1 and FP2 are single-precision FPRs taken from SCRATCH.  */
6939 
6940 void
6941 mips_expand_fcc_reload (rtx dest, rtx src, rtx scratch)
6942 {
6943   rtx fp1, fp2;
6944 
6945   /* Change the source to SFmode.  */
6946   if (MEM_P (src))
6947     src = adjust_address (src, SFmode, 0);
6948   else if (REG_P (src) || GET_CODE (src) == SUBREG)
6949     src = gen_rtx_REG (SFmode, true_regnum (src));
6950 
6951   fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
6952   fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + MAX_FPRS_PER_FMT);
6953 
6954   mips_emit_move (copy_rtx (fp1), src);
6955   mips_emit_move (copy_rtx (fp2), CONST0_RTX (SFmode));
6956   emit_insn (gen_slt_sf (dest, fp2, fp1));
6957 }
6958 
6959 /* Implement MOVE_BY_PIECES_P.  */
6960 
6961 bool
6962 mips_move_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
6963 {
6964   if (HAVE_movmemsi)
6965     {
6966       /* movmemsi is meant to generate code that is at least as good as
6967 	 move_by_pieces.  However, movmemsi effectively uses a by-pieces
6968 	 implementation both for moves smaller than a word and for
6969 	 word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
6970 	 bytes.  We should allow the tree-level optimisers to do such
6971 	 moves by pieces, as it often exposes other optimization
6972 	 opportunities.  We might as well continue to use movmemsi at
6973 	 the rtl level though, as it produces better code when
6974 	 scheduling is disabled (such as at -O).  */
6975       if (currently_expanding_to_rtl)
6976 	return false;
6977       if (align < BITS_PER_WORD)
6978 	return size < UNITS_PER_WORD;
6979       return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
6980     }
6981   /* The default value.  If this becomes a target hook, we should
6982      call the default definition instead.  */
6983   return (move_by_pieces_ninsns (size, align, MOVE_MAX_PIECES + 1)
6984 	  < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()));
6985 }
6986 
6987 /* Implement STORE_BY_PIECES_P.  */
6988 
6989 bool
6990 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
6991 {
6992   /* Storing by pieces involves moving constants into registers
6993      of size MIN (ALIGN, BITS_PER_WORD), then storing them.
6994      We need to decide whether it is cheaper to load the address of
6995      constant data into a register and use a block move instead.  */
6996 
6997   /* If the data is only byte aligned, then:
6998 
6999      (a1) A block move of less than 4 bytes would involve three 3 LBs and
7000 	  3 SBs.  We might as well use 3 single-instruction LIs and 3 SBs
7001 	  instead.
7002 
7003      (a2) A block move of 4 bytes from aligned source data can use an
7004 	  LW/SWL/SWR sequence.  This is often better than the 4 LIs and
7005 	  4 SBs that we would generate when storing by pieces.  */
7006   if (align <= BITS_PER_UNIT)
7007     return size < 4;
7008 
7009   /* If the data is 2-byte aligned, then:
7010 
7011      (b1) A block move of less than 4 bytes would use a combination of LBs,
7012 	  LHs, SBs and SHs.  We get better code by using single-instruction
7013 	  LIs, SBs and SHs instead.
7014 
7015      (b2) A block move of 4 bytes from aligned source data would again use
7016 	  an LW/SWL/SWR sequence.  In most cases, loading the address of
7017 	  the source data would require at least one extra instruction.
7018 	  It is often more efficient to use 2 single-instruction LIs and
7019 	  2 SHs instead.
7020 
7021      (b3) A block move of up to 3 additional bytes would be like (b1).
7022 
7023      (b4) A block move of 8 bytes from aligned source data can use two
7024 	  LW/SWL/SWR sequences or a single LD/SDL/SDR sequence.  Both
7025 	  sequences are better than the 4 LIs and 4 SHs that we'd generate
7026 	  when storing by pieces.
7027 
7028      The reasoning for higher alignments is similar:
7029 
7030      (c1) A block move of less than 4 bytes would be the same as (b1).
7031 
7032      (c2) A block move of 4 bytes would use an LW/SW sequence.  Again,
7033 	  loading the address of the source data would typically require
7034 	  at least one extra instruction.  It is generally better to use
7035 	  LUI/ORI/SW instead.
7036 
7037      (c3) A block move of up to 3 additional bytes would be like (b1).
7038 
7039      (c4) A block move of 8 bytes can use two LW/SW sequences or a single
7040 	  LD/SD sequence, and in these cases we've traditionally preferred
7041 	  the memory copy over the more bulky constant moves.  */
7042   return size < 8;
7043 }
7044 
7045 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
7046    Assume that the areas do not overlap.  */
7047 
7048 static void
7049 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
7050 {
7051   HOST_WIDE_INT offset, delta;
7052   unsigned HOST_WIDE_INT bits;
7053   int i;
7054   enum machine_mode mode;
7055   rtx *regs;
7056 
7057   /* Work out how many bits to move at a time.  If both operands have
7058      half-word alignment, it is usually better to move in half words.
7059      For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
7060      and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
7061      Otherwise move word-sized chunks.  */
7062   if (MEM_ALIGN (src) == BITS_PER_WORD / 2
7063       && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
7064     bits = BITS_PER_WORD / 2;
7065   else
7066     bits = BITS_PER_WORD;
7067 
7068   mode = mode_for_size (bits, MODE_INT, 0);
7069   delta = bits / BITS_PER_UNIT;
7070 
7071   /* Allocate a buffer for the temporary registers.  */
7072   regs = XALLOCAVEC (rtx, length / delta);
7073 
7074   /* Load as many BITS-sized chunks as possible.  Use a normal load if
7075      the source has enough alignment, otherwise use left/right pairs.  */
7076   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7077     {
7078       regs[i] = gen_reg_rtx (mode);
7079       if (MEM_ALIGN (src) >= bits)
7080 	mips_emit_move (regs[i], adjust_address (src, mode, offset));
7081       else
7082 	{
7083 	  rtx part = adjust_address (src, BLKmode, offset);
7084 	  set_mem_size (part, delta);
7085 	  if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
7086 	    gcc_unreachable ();
7087 	}
7088     }
7089 
7090   /* Copy the chunks to the destination.  */
7091   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7092     if (MEM_ALIGN (dest) >= bits)
7093       mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
7094     else
7095       {
7096 	rtx part = adjust_address (dest, BLKmode, offset);
7097 	set_mem_size (part, delta);
7098 	if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
7099 	  gcc_unreachable ();
7100       }
7101 
7102   /* Mop up any left-over bytes.  */
7103   if (offset < length)
7104     {
7105       src = adjust_address (src, BLKmode, offset);
7106       dest = adjust_address (dest, BLKmode, offset);
7107       move_by_pieces (dest, src, length - offset,
7108 		      MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
7109     }
7110 }
7111 
7112 /* Helper function for doing a loop-based block operation on memory
7113    reference MEM.  Each iteration of the loop will operate on LENGTH
7114    bytes of MEM.
7115 
7116    Create a new base register for use within the loop and point it to
7117    the start of MEM.  Create a new memory reference that uses this
7118    register.  Store them in *LOOP_REG and *LOOP_MEM respectively.  */
7119 
7120 static void
7121 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
7122 		       rtx *loop_reg, rtx *loop_mem)
7123 {
7124   *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
7125 
7126   /* Although the new mem does not refer to a known location,
7127      it does keep up to LENGTH bytes of alignment.  */
7128   *loop_mem = change_address (mem, BLKmode, *loop_reg);
7129   set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
7130 }
7131 
7132 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
7133    bytes at a time.  LENGTH must be at least BYTES_PER_ITER.  Assume that
7134    the memory regions do not overlap.  */
7135 
7136 static void
7137 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
7138 		      HOST_WIDE_INT bytes_per_iter)
7139 {
7140   rtx label, src_reg, dest_reg, final_src, test;
7141   HOST_WIDE_INT leftover;
7142 
7143   leftover = length % bytes_per_iter;
7144   length -= leftover;
7145 
7146   /* Create registers and memory references for use within the loop.  */
7147   mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
7148   mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
7149 
7150   /* Calculate the value that SRC_REG should have after the last iteration
7151      of the loop.  */
7152   final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
7153 				   0, 0, OPTAB_WIDEN);
7154 
7155   /* Emit the start of the loop.  */
7156   label = gen_label_rtx ();
7157   emit_label (label);
7158 
7159   /* Emit the loop body.  */
7160   mips_block_move_straight (dest, src, bytes_per_iter);
7161 
7162   /* Move on to the next block.  */
7163   mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
7164   mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
7165 
7166   /* Emit the loop condition.  */
7167   test = gen_rtx_NE (VOIDmode, src_reg, final_src);
7168   if (Pmode == DImode)
7169     emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
7170   else
7171     emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
7172 
7173   /* Mop up any left-over bytes.  */
7174   if (leftover)
7175     mips_block_move_straight (dest, src, leftover);
7176 }
7177 
7178 /* Expand a movmemsi instruction, which copies LENGTH bytes from
7179    memory reference SRC to memory reference DEST.  */
7180 
7181 bool
7182 mips_expand_block_move (rtx dest, rtx src, rtx length)
7183 {
7184   if (CONST_INT_P (length))
7185     {
7186       if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
7187 	{
7188 	  mips_block_move_straight (dest, src, INTVAL (length));
7189 	  return true;
7190 	}
7191       else if (optimize)
7192 	{
7193 	  mips_block_move_loop (dest, src, INTVAL (length),
7194 				MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
7195 	  return true;
7196 	}
7197     }
7198   return false;
7199 }
7200 
7201 /* Expand a loop of synci insns for the address range [BEGIN, END).  */
7202 
7203 void
7204 mips_expand_synci_loop (rtx begin, rtx end)
7205 {
7206   rtx inc, label, end_label, cmp_result, mask, length;
7207 
7208   /* Create end_label.  */
7209   end_label = gen_label_rtx ();
7210 
7211   /* Check if begin equals end.  */
7212   cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
7213   emit_jump_insn (gen_condjump (cmp_result, end_label));
7214 
7215   /* Load INC with the cache line size (rdhwr INC,$1).  */
7216   inc = gen_reg_rtx (Pmode);
7217   emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
7218 
7219   /* Check if inc is 0.  */
7220   cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
7221   emit_jump_insn (gen_condjump (cmp_result, end_label));
7222 
7223   /* Calculate mask.  */
7224   mask = mips_force_unary (Pmode, NEG, inc);
7225 
7226   /* Mask out begin by mask.  */
7227   begin = mips_force_binary (Pmode, AND, begin, mask);
7228 
7229   /* Calculate length.  */
7230   length = mips_force_binary (Pmode, MINUS, end, begin);
7231 
7232   /* Loop back to here.  */
7233   label = gen_label_rtx ();
7234   emit_label (label);
7235 
7236   emit_insn (gen_synci (begin));
7237 
7238   /* Update length.  */
7239   mips_emit_binary (MINUS, length, length, inc);
7240 
7241   /* Update begin.  */
7242   mips_emit_binary (PLUS, begin, begin, inc);
7243 
7244   /* Check if length is greater than 0.  */
7245   cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
7246   emit_jump_insn (gen_condjump (cmp_result, label));
7247 
7248   emit_label (end_label);
7249 }
7250 
7251 /* Expand a QI or HI mode atomic memory operation.
7252 
7253    GENERATOR contains a pointer to the gen_* function that generates
7254    the SI mode underlying atomic operation using masks that we
7255    calculate.
7256 
7257    RESULT is the return register for the operation.  Its value is NULL
7258    if unused.
7259 
7260    MEM is the location of the atomic access.
7261 
7262    OLDVAL is the first operand for the operation.
7263 
7264    NEWVAL is the optional second operand for the operation.  Its value
7265    is NULL if unused.  */
7266 
7267 void
7268 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
7269                          rtx result, rtx mem, rtx oldval, rtx newval)
7270 {
7271   rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
7272   rtx unshifted_mask_reg, mask, inverted_mask, si_op;
7273   rtx res = NULL;
7274   enum machine_mode mode;
7275 
7276   mode = GET_MODE (mem);
7277 
7278   /* Compute the address of the containing SImode value.  */
7279   orig_addr = force_reg (Pmode, XEXP (mem, 0));
7280   memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
7281 				  force_reg (Pmode, GEN_INT (-4)));
7282 
7283   /* Create a memory reference for it.  */
7284   memsi = gen_rtx_MEM (SImode, memsi_addr);
7285   set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
7286   MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
7287 
7288   /* Work out the byte offset of the QImode or HImode value,
7289      counting from the least significant byte.  */
7290   shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
7291   if (TARGET_BIG_ENDIAN)
7292     mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
7293 
7294   /* Multiply by eight to convert the shift value from bytes to bits.  */
7295   mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
7296 
7297   /* Make the final shift an SImode value, so that it can be used in
7298      SImode operations.  */
7299   shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
7300 
7301   /* Set MASK to an inclusive mask of the QImode or HImode value.  */
7302   unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
7303   unshifted_mask_reg = force_reg (SImode, unshifted_mask);
7304   mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
7305 
7306   /* Compute the equivalent exclusive mask.  */
7307   inverted_mask = gen_reg_rtx (SImode);
7308   emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
7309 			  gen_rtx_NOT (SImode, mask)));
7310 
7311   /* Shift the old value into place.  */
7312   if (oldval != const0_rtx)
7313     {
7314       oldval = convert_modes (SImode, mode, oldval, true);
7315       oldval = force_reg (SImode, oldval);
7316       oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
7317     }
7318 
7319   /* Do the same for the new value.  */
7320   if (newval && newval != const0_rtx)
7321     {
7322       newval = convert_modes (SImode, mode, newval, true);
7323       newval = force_reg (SImode, newval);
7324       newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
7325     }
7326 
7327   /* Do the SImode atomic access.  */
7328   if (result)
7329     res = gen_reg_rtx (SImode);
7330   if (newval)
7331     si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
7332   else if (result)
7333     si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
7334   else
7335     si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
7336 
7337   emit_insn (si_op);
7338 
7339   if (result)
7340     {
7341       /* Shift and convert the result.  */
7342       mips_emit_binary (AND, res, res, mask);
7343       mips_emit_binary (LSHIFTRT, res, res, shiftsi);
7344       mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
7345     }
7346 }
7347 
7348 /* Return true if it is possible to use left/right accesses for a
7349    bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
7350    When returning true, update *LEFT and *RIGHT as follows:
7351 
7352    *LEFT is a QImode reference to the first byte if big endian or
7353    the last byte if little endian.  This address can be used in the
7354    left-side instructions (LWL, SWL, LDL, SDL).
7355 
7356    *RIGHT is a QImode reference to the opposite end of the field and
7357    can be used in the patterning right-side instruction.  */
7358 
7359 static bool
7360 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
7361 			rtx *left, rtx *right)
7362 {
7363   rtx first, last;
7364 
7365   /* Check that the size is valid.  */
7366   if (width != 32 && (!TARGET_64BIT || width != 64))
7367     return false;
7368 
7369   /* We can only access byte-aligned values.  Since we are always passed
7370      a reference to the first byte of the field, it is not necessary to
7371      do anything with BITPOS after this check.  */
7372   if (bitpos % BITS_PER_UNIT != 0)
7373     return false;
7374 
7375   /* Reject aligned bitfields: we want to use a normal load or store
7376      instead of a left/right pair.  */
7377   if (MEM_ALIGN (op) >= width)
7378     return false;
7379 
7380   /* Get references to both ends of the field.  */
7381   first = adjust_address (op, QImode, 0);
7382   last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
7383 
7384   /* Allocate to LEFT and RIGHT according to endianness.  LEFT should
7385      correspond to the MSB and RIGHT to the LSB.  */
7386   if (TARGET_BIG_ENDIAN)
7387     *left = first, *right = last;
7388   else
7389     *left = last, *right = first;
7390 
7391   return true;
7392 }
7393 
7394 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
7395    DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
7396    the operation is the equivalent of:
7397 
7398       (set DEST (*_extract SRC WIDTH BITPOS))
7399 
7400    Return true on success.  */
7401 
7402 bool
7403 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
7404 				   HOST_WIDE_INT bitpos, bool unsigned_p)
7405 {
7406   rtx left, right, temp;
7407   rtx dest1 = NULL_RTX;
7408 
7409   /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
7410      be a DImode, create a new temp and emit a zero extend at the end.  */
7411   if (GET_MODE (dest) == DImode
7412       && REG_P (dest)
7413       && GET_MODE_BITSIZE (SImode) == width)
7414     {
7415       dest1 = dest;
7416       dest = gen_reg_rtx (SImode);
7417     }
7418 
7419   if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
7420     return false;
7421 
7422   temp = gen_reg_rtx (GET_MODE (dest));
7423   if (GET_MODE (dest) == DImode)
7424     {
7425       emit_insn (gen_mov_ldl (temp, src, left));
7426       emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
7427     }
7428   else
7429     {
7430       emit_insn (gen_mov_lwl (temp, src, left));
7431       emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
7432     }
7433 
7434   /* If we were loading 32bits and the original register was DI then
7435      sign/zero extend into the orignal dest.  */
7436   if (dest1)
7437     {
7438       if (unsigned_p)
7439         emit_insn (gen_zero_extendsidi2 (dest1, dest));
7440       else
7441         emit_insn (gen_extendsidi2 (dest1, dest));
7442     }
7443   return true;
7444 }
7445 
7446 /* Try to use left/right stores to expand an "ins" pattern.  DEST, WIDTH,
7447    BITPOS and SRC are the operands passed to the expander; the operation
7448    is the equivalent of:
7449 
7450        (set (zero_extract DEST WIDTH BITPOS) SRC)
7451 
7452    Return true on success.  */
7453 
7454 bool
7455 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
7456 				    HOST_WIDE_INT bitpos)
7457 {
7458   rtx left, right;
7459   enum machine_mode mode;
7460 
7461   if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
7462     return false;
7463 
7464   mode = mode_for_size (width, MODE_INT, 0);
7465   src = gen_lowpart (mode, src);
7466   if (mode == DImode)
7467     {
7468       emit_insn (gen_mov_sdl (dest, src, left));
7469       emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7470     }
7471   else
7472     {
7473       emit_insn (gen_mov_swl (dest, src, left));
7474       emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7475     }
7476   return true;
7477 }
7478 
7479 /* Return true if X is a MEM with the same size as MODE.  */
7480 
7481 bool
7482 mips_mem_fits_mode_p (enum machine_mode mode, rtx x)
7483 {
7484   return (MEM_P (x)
7485 	  && MEM_SIZE_KNOWN_P (x)
7486 	  && MEM_SIZE (x) == GET_MODE_SIZE (mode));
7487 }
7488 
7489 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7490    source of an "ext" instruction or the destination of an "ins"
7491    instruction.  OP must be a register operand and the following
7492    conditions must hold:
7493 
7494      0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7495      0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7496      0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7497 
7498    Also reject lengths equal to a word as they are better handled
7499    by the move patterns.  */
7500 
7501 bool
7502 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7503 {
7504   if (!ISA_HAS_EXT_INS
7505       || !register_operand (op, VOIDmode)
7506       || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7507     return false;
7508 
7509   if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7510     return false;
7511 
7512   if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7513     return false;
7514 
7515   return true;
7516 }
7517 
7518 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7519    operation if MAXLEN is the maxium length of consecutive bits that
7520    can make up MASK.  MODE is the mode of the operation.  See
7521    mask_low_and_shift_len for the actual definition.  */
7522 
7523 bool
7524 mask_low_and_shift_p (enum machine_mode mode, rtx mask, rtx shift, int maxlen)
7525 {
7526   return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7527 }
7528 
7529 /* Return true iff OP1 and OP2 are valid operands together for the
7530    *and<MODE>3 and *and<MODE>3_mips16 patterns.  For the cases to consider,
7531    see the table in the comment before the pattern.  */
7532 
7533 bool
7534 and_operands_ok (enum machine_mode mode, rtx op1, rtx op2)
7535 {
7536   return (memory_operand (op1, mode)
7537 	  ? and_load_operand (op2, mode)
7538 	  : and_reg_operand (op2, mode));
7539 }
7540 
7541 /* The canonical form of a mask-low-and-shift-left operation is
7542    (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7543    cleared.  Thus we need to shift MASK to the right before checking if it
7544    is a valid mask value.  MODE is the mode of the operation.  If true
7545    return the length of the mask, otherwise return -1.  */
7546 
7547 int
7548 mask_low_and_shift_len (enum machine_mode mode, rtx mask, rtx shift)
7549 {
7550   HOST_WIDE_INT shval;
7551 
7552   shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7553   return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7554 }
7555 
7556 /* Return true if -msplit-addresses is selected and should be honored.
7557 
7558    -msplit-addresses is a half-way house between explicit relocations
7559    and the traditional assembler macros.  It can split absolute 32-bit
7560    symbolic constants into a high/lo_sum pair but uses macros for other
7561    sorts of access.
7562 
7563    Like explicit relocation support for REL targets, it relies
7564    on GNU extensions in the assembler and the linker.
7565 
7566    Although this code should work for -O0, it has traditionally
7567    been treated as an optimization.  */
7568 
7569 static bool
7570 mips_split_addresses_p (void)
7571 {
7572   return (TARGET_SPLIT_ADDRESSES
7573 	  && optimize
7574 	  && !TARGET_MIPS16
7575 	  && !flag_pic
7576 	  && !ABI_HAS_64BIT_SYMBOLS);
7577 }
7578 
7579 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs.  */
7580 
7581 static void
7582 mips_init_relocs (void)
7583 {
7584   memset (mips_split_p, '\0', sizeof (mips_split_p));
7585   memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
7586   memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
7587   memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
7588   memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
7589 
7590   if (TARGET_MIPS16_PCREL_LOADS)
7591     mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
7592   else
7593     {
7594       if (ABI_HAS_64BIT_SYMBOLS)
7595 	{
7596 	  if (TARGET_EXPLICIT_RELOCS)
7597 	    {
7598 	      mips_split_p[SYMBOL_64_HIGH] = true;
7599 	      mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
7600 	      mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
7601 
7602 	      mips_split_p[SYMBOL_64_MID] = true;
7603 	      mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
7604 	      mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
7605 
7606 	      mips_split_p[SYMBOL_64_LOW] = true;
7607 	      mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
7608 	      mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
7609 
7610 	      mips_split_p[SYMBOL_ABSOLUTE] = true;
7611 	      mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7612 	    }
7613 	}
7614       else
7615 	{
7616 	  if (TARGET_EXPLICIT_RELOCS
7617 	      || mips_split_addresses_p ()
7618 	      || TARGET_MIPS16)
7619 	    {
7620 	      mips_split_p[SYMBOL_ABSOLUTE] = true;
7621 	      mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
7622 	      mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7623 	    }
7624 	}
7625     }
7626 
7627   if (TARGET_MIPS16)
7628     {
7629       /* The high part is provided by a pseudo copy of $gp.  */
7630       mips_split_p[SYMBOL_GP_RELATIVE] = true;
7631       mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
7632     }
7633   else if (TARGET_EXPLICIT_RELOCS)
7634     /* Small data constants are kept whole until after reload,
7635        then lowered by mips_rewrite_small_data.  */
7636     mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
7637 
7638   if (TARGET_EXPLICIT_RELOCS)
7639     {
7640       mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
7641       if (TARGET_NEWABI)
7642 	{
7643 	  mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
7644 	  mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
7645 	}
7646       else
7647 	{
7648 	  mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
7649 	  mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
7650 	}
7651       if (TARGET_MIPS16)
7652 	/* Expose the use of $28 as soon as possible.  */
7653 	mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
7654 
7655       if (TARGET_XGOT)
7656 	{
7657 	  /* The HIGH and LO_SUM are matched by special .md patterns.  */
7658 	  mips_split_p[SYMBOL_GOT_DISP] = true;
7659 
7660 	  mips_split_p[SYMBOL_GOTOFF_DISP] = true;
7661 	  mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
7662 	  mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
7663 
7664 	  mips_split_p[SYMBOL_GOTOFF_CALL] = true;
7665 	  mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
7666 	  mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
7667 	}
7668       else
7669 	{
7670 	  if (TARGET_NEWABI)
7671 	    mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
7672 	  else
7673 	    mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
7674 	  mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
7675 	  if (TARGET_MIPS16)
7676 	    /* Expose the use of $28 as soon as possible.  */
7677 	    mips_split_p[SYMBOL_GOT_DISP] = true;
7678 	}
7679     }
7680 
7681   if (TARGET_NEWABI)
7682     {
7683       mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
7684       mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
7685       mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
7686     }
7687 
7688   mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
7689   mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
7690 
7691   if (TARGET_MIPS16_PCREL_LOADS)
7692     {
7693       mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
7694       mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
7695     }
7696   else
7697     {
7698       mips_split_p[SYMBOL_DTPREL] = true;
7699       mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
7700       mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
7701 
7702       mips_split_p[SYMBOL_TPREL] = true;
7703       mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
7704       mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
7705     }
7706 
7707   mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
7708   mips_lo_relocs[SYMBOL_HALF] = "%half(";
7709 }
7710 
7711 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
7712    in context CONTEXT.  RELOCS is the array of relocations to use.  */
7713 
7714 static void
7715 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
7716 			  const char **relocs)
7717 {
7718   enum mips_symbol_type symbol_type;
7719   const char *p;
7720 
7721   symbol_type = mips_classify_symbolic_expression (op, context);
7722   gcc_assert (relocs[symbol_type]);
7723 
7724   fputs (relocs[symbol_type], file);
7725   output_addr_const (file, mips_strip_unspec_address (op));
7726   for (p = relocs[symbol_type]; *p != 0; p++)
7727     if (*p == '(')
7728       fputc (')', file);
7729 }
7730 
7731 /* Start a new block with the given asm switch enabled.  If we need
7732    to print a directive, emit PREFIX before it and SUFFIX after it.  */
7733 
7734 static void
7735 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
7736 			const char *prefix, const char *suffix)
7737 {
7738   if (asm_switch->nesting_level == 0)
7739     fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
7740   asm_switch->nesting_level++;
7741 }
7742 
7743 /* Likewise, but end a block.  */
7744 
7745 static void
7746 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
7747 		       const char *prefix, const char *suffix)
7748 {
7749   gcc_assert (asm_switch->nesting_level);
7750   asm_switch->nesting_level--;
7751   if (asm_switch->nesting_level == 0)
7752     fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
7753 }
7754 
7755 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
7756    that either print a complete line or print nothing.  */
7757 
7758 void
7759 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
7760 {
7761   mips_push_asm_switch_1 (asm_switch, "\t", "\n");
7762 }
7763 
7764 void
7765 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
7766 {
7767   mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
7768 }
7769 
7770 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
7771    The punctuation characters are:
7772 
7773    '('	Start a nested ".set noreorder" block.
7774    ')'	End a nested ".set noreorder" block.
7775    '['	Start a nested ".set noat" block.
7776    ']'	End a nested ".set noat" block.
7777    '<'	Start a nested ".set nomacro" block.
7778    '>'	End a nested ".set nomacro" block.
7779    '*'	Behave like %(%< if generating a delayed-branch sequence.
7780    '#'	Print a nop if in a ".set noreorder" block.
7781    '/'	Like '#', but do nothing within a delayed-branch sequence.
7782    '?'	Print "l" if mips_branch_likely is true
7783    '~'	Print a nop if mips_branch_likely is true
7784    '.'	Print the name of the register with a hard-wired zero (zero or $0).
7785    '@'	Print the name of the assembler temporary register (at or $1).
7786    '^'	Print the name of the pic call-through register (t9 or $25).
7787    '+'	Print the name of the gp register (usually gp or $28).
7788    '$'	Print the name of the stack pointer register (sp or $29).
7789 
7790    See also mips_init_print_operand_pucnt.  */
7791 
7792 static void
7793 mips_print_operand_punctuation (FILE *file, int ch)
7794 {
7795   switch (ch)
7796     {
7797     case '(':
7798       mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
7799       break;
7800 
7801     case ')':
7802       mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
7803       break;
7804 
7805     case '[':
7806       mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
7807       break;
7808 
7809     case ']':
7810       mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
7811       break;
7812 
7813     case '<':
7814       mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
7815       break;
7816 
7817     case '>':
7818       mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
7819       break;
7820 
7821     case '*':
7822       if (final_sequence != 0)
7823 	{
7824 	  mips_print_operand_punctuation (file, '(');
7825 	  mips_print_operand_punctuation (file, '<');
7826 	}
7827       break;
7828 
7829     case '#':
7830       if (mips_noreorder.nesting_level > 0)
7831 	fputs ("\n\tnop", file);
7832       break;
7833 
7834     case '/':
7835       /* Print an extra newline so that the delayed insn is separated
7836 	 from the following ones.  This looks neater and is consistent
7837 	 with non-nop delayed sequences.  */
7838       if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
7839 	fputs ("\n\tnop\n", file);
7840       break;
7841 
7842     case '?':
7843       if (mips_branch_likely)
7844 	putc ('l', file);
7845       break;
7846 
7847     case '~':
7848       if (mips_branch_likely)
7849 	fputs ("\n\tnop", file);
7850       break;
7851 
7852     case '.':
7853       fputs (reg_names[GP_REG_FIRST + 0], file);
7854       break;
7855 
7856     case '@':
7857       fputs (reg_names[AT_REGNUM], file);
7858       break;
7859 
7860     case '^':
7861       fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
7862       break;
7863 
7864     case '+':
7865       fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
7866       break;
7867 
7868     case '$':
7869       fputs (reg_names[STACK_POINTER_REGNUM], file);
7870       break;
7871 
7872     default:
7873       gcc_unreachable ();
7874       break;
7875     }
7876 }
7877 
7878 /* Initialize mips_print_operand_punct.  */
7879 
7880 static void
7881 mips_init_print_operand_punct (void)
7882 {
7883   const char *p;
7884 
7885   for (p = "()[]<>*#/?~.@^+$"; *p; p++)
7886     mips_print_operand_punct[(unsigned char) *p] = true;
7887 }
7888 
7889 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
7890    associated with condition CODE.  Print the condition part of the
7891    opcode to FILE.  */
7892 
7893 static void
7894 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
7895 {
7896   switch (code)
7897     {
7898     case EQ:
7899     case NE:
7900     case GT:
7901     case GE:
7902     case LT:
7903     case LE:
7904     case GTU:
7905     case GEU:
7906     case LTU:
7907     case LEU:
7908       /* Conveniently, the MIPS names for these conditions are the same
7909 	 as their RTL equivalents.  */
7910       fputs (GET_RTX_NAME (code), file);
7911       break;
7912 
7913     default:
7914       output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
7915       break;
7916     }
7917 }
7918 
7919 /* Likewise floating-point branches.  */
7920 
7921 static void
7922 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
7923 {
7924   switch (code)
7925     {
7926     case EQ:
7927       fputs ("c1f", file);
7928       break;
7929 
7930     case NE:
7931       fputs ("c1t", file);
7932       break;
7933 
7934     default:
7935       output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
7936       break;
7937     }
7938 }
7939 
7940 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P.  */
7941 
7942 static bool
7943 mips_print_operand_punct_valid_p (unsigned char code)
7944 {
7945   return mips_print_operand_punct[code];
7946 }
7947 
7948 /* Implement TARGET_PRINT_OPERAND.  The MIPS-specific operand codes are:
7949 
7950    'X'	Print CONST_INT OP in hexadecimal format.
7951    'x'	Print the low 16 bits of CONST_INT OP in hexadecimal format.
7952    'd'	Print CONST_INT OP in decimal.
7953    'm'	Print one less than CONST_INT OP in decimal.
7954    'h'	Print the high-part relocation associated with OP, after stripping
7955 	  any outermost HIGH.
7956    'R'	Print the low-part relocation associated with OP.
7957    'C'	Print the integer branch condition for comparison OP.
7958    'N'	Print the inverse of the integer branch condition for comparison OP.
7959    'F'	Print the FPU branch condition for comparison OP.
7960    'W'	Print the inverse of the FPU branch condition for comparison OP.
7961    'T'	Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
7962 	      'z' for (eq:?I ...), 'n' for (ne:?I ...).
7963    't'	Like 'T', but with the EQ/NE cases reversed
7964    'Y'	Print mips_fp_conditions[INTVAL (OP)]
7965    'Z'	Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
7966    'q'	Print a DSP accumulator register.
7967    'D'	Print the second part of a double-word register or memory operand.
7968    'L'	Print the low-order register in a double-word register operand.
7969    'M'	Print high-order register in a double-word register operand.
7970    'z'	Print $0 if OP is zero, otherwise print OP normally.
7971    'b'	Print the address of a memory operand, without offset.  */
7972 
7973 static void
7974 mips_print_operand (FILE *file, rtx op, int letter)
7975 {
7976   enum rtx_code code;
7977 
7978   if (mips_print_operand_punct_valid_p (letter))
7979     {
7980       mips_print_operand_punctuation (file, letter);
7981       return;
7982     }
7983 
7984   gcc_assert (op);
7985   code = GET_CODE (op);
7986 
7987   switch (letter)
7988     {
7989     case 'X':
7990       if (CONST_INT_P (op))
7991 	fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
7992       else
7993 	output_operand_lossage ("invalid use of '%%%c'", letter);
7994       break;
7995 
7996     case 'x':
7997       if (CONST_INT_P (op))
7998 	fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
7999       else
8000 	output_operand_lossage ("invalid use of '%%%c'", letter);
8001       break;
8002 
8003     case 'd':
8004       if (CONST_INT_P (op))
8005 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
8006       else
8007 	output_operand_lossage ("invalid use of '%%%c'", letter);
8008       break;
8009 
8010     case 'm':
8011       if (CONST_INT_P (op))
8012 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
8013       else
8014 	output_operand_lossage ("invalid use of '%%%c'", letter);
8015       break;
8016 
8017     case 'h':
8018       if (code == HIGH)
8019 	op = XEXP (op, 0);
8020       mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
8021       break;
8022 
8023     case 'R':
8024       mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
8025       break;
8026 
8027     case 'C':
8028       mips_print_int_branch_condition (file, code, letter);
8029       break;
8030 
8031     case 'N':
8032       mips_print_int_branch_condition (file, reverse_condition (code), letter);
8033       break;
8034 
8035     case 'F':
8036       mips_print_float_branch_condition (file, code, letter);
8037       break;
8038 
8039     case 'W':
8040       mips_print_float_branch_condition (file, reverse_condition (code),
8041 					 letter);
8042       break;
8043 
8044     case 'T':
8045     case 't':
8046       {
8047 	int truth = (code == NE) == (letter == 'T');
8048 	fputc ("zfnt"[truth * 2 + ST_REG_P (REGNO (XEXP (op, 0)))], file);
8049       }
8050       break;
8051 
8052     case 'Y':
8053       if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
8054 	fputs (mips_fp_conditions[UINTVAL (op)], file);
8055       else
8056 	output_operand_lossage ("'%%%c' is not a valid operand prefix",
8057 				letter);
8058       break;
8059 
8060     case 'Z':
8061       if (ISA_HAS_8CC)
8062 	{
8063 	  mips_print_operand (file, op, 0);
8064 	  fputc (',', file);
8065 	}
8066       break;
8067 
8068     case 'q':
8069       if (code == REG && MD_REG_P (REGNO (op)))
8070 	fprintf (file, "$ac0");
8071       else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
8072 	fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
8073       else
8074 	output_operand_lossage ("invalid use of '%%%c'", letter);
8075       break;
8076 
8077     default:
8078       switch (code)
8079 	{
8080 	case REG:
8081 	  {
8082 	    unsigned int regno = REGNO (op);
8083 	    if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
8084 		|| (letter == 'L' && TARGET_BIG_ENDIAN)
8085 		|| letter == 'D')
8086 	      regno++;
8087 	    else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
8088 	      output_operand_lossage ("invalid use of '%%%c'", letter);
8089 	    /* We need to print $0 .. $31 for COP0 registers.  */
8090 	    if (COP0_REG_P (regno))
8091 	      fprintf (file, "$%s", &reg_names[regno][4]);
8092 	    else
8093 	      fprintf (file, "%s", reg_names[regno]);
8094 	  }
8095 	  break;
8096 
8097 	case MEM:
8098 	  if (letter == 'D')
8099 	    output_address (plus_constant (Pmode, XEXP (op, 0), 4));
8100 	  else if (letter == 'b')
8101 	    {
8102 	      gcc_assert (REG_P (XEXP (op, 0)));
8103 	      mips_print_operand (file, XEXP (op, 0), 0);
8104 	    }
8105 	  else if (letter && letter != 'z')
8106 	    output_operand_lossage ("invalid use of '%%%c'", letter);
8107 	  else
8108 	    output_address (XEXP (op, 0));
8109 	  break;
8110 
8111 	default:
8112 	  if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
8113 	    fputs (reg_names[GP_REG_FIRST], file);
8114 	  else if (letter && letter != 'z')
8115 	    output_operand_lossage ("invalid use of '%%%c'", letter);
8116 	  else if (CONST_GP_P (op))
8117 	    fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
8118 	  else
8119 	    output_addr_const (file, mips_strip_unspec_address (op));
8120 	  break;
8121 	}
8122     }
8123 }
8124 
8125 /* Implement TARGET_PRINT_OPERAND_ADDRESS.  */
8126 
8127 static void
8128 mips_print_operand_address (FILE *file, rtx x)
8129 {
8130   struct mips_address_info addr;
8131 
8132   if (mips_classify_address (&addr, x, word_mode, true))
8133     switch (addr.type)
8134       {
8135       case ADDRESS_REG:
8136 	mips_print_operand (file, addr.offset, 0);
8137 	fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8138 	return;
8139 
8140       case ADDRESS_LO_SUM:
8141 	mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
8142 				  mips_lo_relocs);
8143 	fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8144 	return;
8145 
8146       case ADDRESS_CONST_INT:
8147 	output_addr_const (file, x);
8148 	fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
8149 	return;
8150 
8151       case ADDRESS_SYMBOLIC:
8152 	output_addr_const (file, mips_strip_unspec_address (x));
8153 	return;
8154       }
8155   gcc_unreachable ();
8156 }
8157 
8158 /* Implement TARGET_ENCODE_SECTION_INFO.  */
8159 
8160 static void
8161 mips_encode_section_info (tree decl, rtx rtl, int first)
8162 {
8163   default_encode_section_info (decl, rtl, first);
8164 
8165   if (TREE_CODE (decl) == FUNCTION_DECL)
8166     {
8167       rtx symbol = XEXP (rtl, 0);
8168       tree type = TREE_TYPE (decl);
8169 
8170       /* Encode whether the symbol is short or long.  */
8171       if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
8172 	  || mips_far_type_p (type))
8173 	SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
8174     }
8175 }
8176 
8177 /* Implement TARGET_SELECT_RTX_SECTION.  */
8178 
8179 static section *
8180 mips_select_rtx_section (enum machine_mode mode, rtx x,
8181 			 unsigned HOST_WIDE_INT align)
8182 {
8183   /* ??? Consider using mergeable small data sections.  */
8184   if (mips_rtx_constant_in_small_data_p (mode))
8185     return get_named_section (NULL, ".sdata", 0);
8186 
8187   return default_elf_select_rtx_section (mode, x, align);
8188 }
8189 
8190 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
8191 
8192    The complication here is that, with the combination TARGET_ABICALLS
8193    && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
8194    absolute addresses, and should therefore not be included in the
8195    read-only part of a DSO.  Handle such cases by selecting a normal
8196    data section instead of a read-only one.  The logic apes that in
8197    default_function_rodata_section.  */
8198 
8199 static section *
8200 mips_function_rodata_section (tree decl)
8201 {
8202   if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
8203     return default_function_rodata_section (decl);
8204 
8205   if (decl && DECL_SECTION_NAME (decl))
8206     {
8207       const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8208       if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
8209 	{
8210 	  char *rname = ASTRDUP (name);
8211 	  rname[14] = 'd';
8212 	  return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
8213 	}
8214       else if (flag_function_sections
8215 	       && flag_data_sections
8216 	       && strncmp (name, ".text.", 6) == 0)
8217 	{
8218 	  char *rname = ASTRDUP (name);
8219 	  memcpy (rname + 1, "data", 4);
8220 	  return get_section (rname, SECTION_WRITE, decl);
8221 	}
8222     }
8223   return data_section;
8224 }
8225 
8226 /* Implement TARGET_IN_SMALL_DATA_P.  */
8227 
8228 static bool
8229 mips_in_small_data_p (const_tree decl)
8230 {
8231   unsigned HOST_WIDE_INT size;
8232 
8233   if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
8234     return false;
8235 
8236   /* We don't yet generate small-data references for -mabicalls
8237      or VxWorks RTP code.  See the related -G handling in
8238      mips_option_override.  */
8239   if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
8240     return false;
8241 
8242   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
8243     {
8244       const char *name;
8245 
8246       /* Reject anything that isn't in a known small-data section.  */
8247       name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8248       if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
8249 	return false;
8250 
8251       /* If a symbol is defined externally, the assembler will use the
8252 	 usual -G rules when deciding how to implement macros.  */
8253       if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
8254 	return true;
8255     }
8256   else if (TARGET_EMBEDDED_DATA)
8257     {
8258       /* Don't put constants into the small data section: we want them
8259 	 to be in ROM rather than RAM.  */
8260       if (TREE_CODE (decl) != VAR_DECL)
8261 	return false;
8262 
8263       if (TREE_READONLY (decl)
8264 	  && !TREE_SIDE_EFFECTS (decl)
8265 	  && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
8266 	return false;
8267     }
8268 
8269   /* Enforce -mlocal-sdata.  */
8270   if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
8271     return false;
8272 
8273   /* Enforce -mextern-sdata.  */
8274   if (!TARGET_EXTERN_SDATA && DECL_P (decl))
8275     {
8276       if (DECL_EXTERNAL (decl))
8277 	return false;
8278       if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
8279 	return false;
8280     }
8281 
8282   /* We have traditionally not treated zero-sized objects as small data,
8283      so this is now effectively part of the ABI.  */
8284   size = int_size_in_bytes (TREE_TYPE (decl));
8285   return size > 0 && size <= mips_small_data_threshold;
8286 }
8287 
8288 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P.  We don't want to use
8289    anchors for small data: the GP register acts as an anchor in that
8290    case.  We also don't want to use them for PC-relative accesses,
8291    where the PC acts as an anchor.  */
8292 
8293 static bool
8294 mips_use_anchors_for_symbol_p (const_rtx symbol)
8295 {
8296   switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
8297     {
8298     case SYMBOL_PC_RELATIVE:
8299     case SYMBOL_GP_RELATIVE:
8300       return false;
8301 
8302     default:
8303       return default_use_anchors_for_symbol_p (symbol);
8304     }
8305 }
8306 
8307 /* The MIPS debug format wants all automatic variables and arguments
8308    to be in terms of the virtual frame pointer (stack pointer before
8309    any adjustment in the function), while the MIPS 3.0 linker wants
8310    the frame pointer to be the stack pointer after the initial
8311    adjustment.  So, we do the adjustment here.  The arg pointer (which
8312    is eliminated) points to the virtual frame pointer, while the frame
8313    pointer (which may be eliminated) points to the stack pointer after
8314    the initial adjustments.  */
8315 
8316 HOST_WIDE_INT
8317 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
8318 {
8319   rtx offset2 = const0_rtx;
8320   rtx reg = eliminate_constant_term (addr, &offset2);
8321 
8322   if (offset == 0)
8323     offset = INTVAL (offset2);
8324 
8325   if (reg == stack_pointer_rtx
8326       || reg == frame_pointer_rtx
8327       || reg == hard_frame_pointer_rtx)
8328     {
8329       offset -= cfun->machine->frame.total_size;
8330       if (reg == hard_frame_pointer_rtx)
8331 	offset += cfun->machine->frame.hard_frame_pointer_offset;
8332     }
8333 
8334   return offset;
8335 }
8336 
8337 /* Implement ASM_OUTPUT_EXTERNAL.  */
8338 
8339 void
8340 mips_output_external (FILE *file, tree decl, const char *name)
8341 {
8342   default_elf_asm_output_external (file, decl, name);
8343 
8344   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8345      set in order to avoid putting out names that are never really
8346      used. */
8347   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
8348     {
8349       if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
8350 	{
8351 	  /* When using assembler macros, emit .extern directives for
8352 	     all small-data externs so that the assembler knows how
8353 	     big they are.
8354 
8355 	     In most cases it would be safe (though pointless) to emit
8356 	     .externs for other symbols too.  One exception is when an
8357 	     object is within the -G limit but declared by the user to
8358 	     be in a section other than .sbss or .sdata.  */
8359 	  fputs ("\t.extern\t", file);
8360 	  assemble_name (file, name);
8361 	  fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
8362 		   int_size_in_bytes (TREE_TYPE (decl)));
8363 	}
8364     }
8365 }
8366 
8367 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME.  */
8368 
8369 static void
8370 mips_output_filename (FILE *stream, const char *name)
8371 {
8372   /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
8373      directives.  */
8374   if (write_symbols == DWARF2_DEBUG)
8375     return;
8376   else if (mips_output_filename_first_time)
8377     {
8378       mips_output_filename_first_time = 0;
8379       num_source_filenames += 1;
8380       current_function_file = name;
8381       fprintf (stream, "\t.file\t%d ", num_source_filenames);
8382       output_quoted_string (stream, name);
8383       putc ('\n', stream);
8384     }
8385   /* If we are emitting stabs, let dbxout.c handle this (except for
8386      the mips_output_filename_first_time case).  */
8387   else if (write_symbols == DBX_DEBUG)
8388     return;
8389   else if (name != current_function_file
8390 	   && strcmp (name, current_function_file) != 0)
8391     {
8392       num_source_filenames += 1;
8393       current_function_file = name;
8394       fprintf (stream, "\t.file\t%d ", num_source_filenames);
8395       output_quoted_string (stream, name);
8396       putc ('\n', stream);
8397     }
8398 }
8399 
8400 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
8401 
8402 static void ATTRIBUTE_UNUSED
8403 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
8404 {
8405   switch (size)
8406     {
8407     case 4:
8408       fputs ("\t.dtprelword\t", file);
8409       break;
8410 
8411     case 8:
8412       fputs ("\t.dtpreldword\t", file);
8413       break;
8414 
8415     default:
8416       gcc_unreachable ();
8417     }
8418   output_addr_const (file, x);
8419   fputs ("+0x8000", file);
8420 }
8421 
8422 /* Implement TARGET_DWARF_REGISTER_SPAN.  */
8423 
8424 static rtx
8425 mips_dwarf_register_span (rtx reg)
8426 {
8427   rtx high, low;
8428   enum machine_mode mode;
8429 
8430   /* By default, GCC maps increasing register numbers to increasing
8431      memory locations, but paired FPRs are always little-endian,
8432      regardless of the prevailing endianness.  */
8433   mode = GET_MODE (reg);
8434   if (FP_REG_P (REGNO (reg))
8435       && TARGET_BIG_ENDIAN
8436       && MAX_FPRS_PER_FMT > 1
8437       && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8438     {
8439       gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
8440       high = mips_subword (reg, true);
8441       low = mips_subword (reg, false);
8442       return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
8443     }
8444 
8445   return NULL_RTX;
8446 }
8447 
8448 /* DSP ALU can bypass data with no delays for the following pairs. */
8449 enum insn_code dspalu_bypass_table[][2] =
8450 {
8451   {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
8452   {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
8453   {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
8454   {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
8455   {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
8456   {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
8457   {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
8458   {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
8459 };
8460 
8461 int
8462 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
8463 {
8464   int i;
8465   int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
8466   enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
8467   enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
8468 
8469   for (i = 0; i < num_bypass; i++)
8470     {
8471       if (out_icode == dspalu_bypass_table[i][0]
8472 	  && in_icode == dspalu_bypass_table[i][1])
8473        return true;
8474     }
8475 
8476   return false;
8477 }
8478 /* Implement ASM_OUTPUT_ASCII.  */
8479 
8480 void
8481 mips_output_ascii (FILE *stream, const char *string, size_t len)
8482 {
8483   size_t i;
8484   int cur_pos;
8485 
8486   cur_pos = 17;
8487   fprintf (stream, "\t.ascii\t\"");
8488   for (i = 0; i < len; i++)
8489     {
8490       int c;
8491 
8492       c = (unsigned char) string[i];
8493       if (ISPRINT (c))
8494 	{
8495 	  if (c == '\\' || c == '\"')
8496 	    {
8497 	      putc ('\\', stream);
8498 	      cur_pos++;
8499 	    }
8500 	  putc (c, stream);
8501 	  cur_pos++;
8502 	}
8503       else
8504 	{
8505 	  fprintf (stream, "\\%03o", c);
8506 	  cur_pos += 4;
8507 	}
8508 
8509       if (cur_pos > 72 && i+1 < len)
8510 	{
8511 	  cur_pos = 17;
8512 	  fprintf (stream, "\"\n\t.ascii\t\"");
8513 	}
8514     }
8515   fprintf (stream, "\"\n");
8516 }
8517 
8518 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
8519    Update *ADDR with the operand that should be printed.  */
8520 
8521 const char *
8522 mips_output_tls_reloc_directive (rtx *addr)
8523 {
8524   enum mips_symbol_type type;
8525 
8526   type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
8527   *addr = mips_strip_unspec_address (*addr);
8528   switch (type)
8529     {
8530     case SYMBOL_DTPREL:
8531       return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
8532 
8533     case SYMBOL_TPREL:
8534       return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
8535 
8536     default:
8537       gcc_unreachable ();
8538     }
8539 }
8540 
8541 /* Emit either a label, .comm, or .lcomm directive.  When using assembler
8542    macros, mark the symbol as written so that mips_asm_output_external
8543    won't emit an .extern for it.  STREAM is the output file, NAME is the
8544    name of the symbol, INIT_STRING is the string that should be written
8545    before the symbol and FINAL_STRING is the string that should be
8546    written after it.  FINAL_STRING is a printf format that consumes the
8547    remaining arguments.  */
8548 
8549 void
8550 mips_declare_object (FILE *stream, const char *name, const char *init_string,
8551 		     const char *final_string, ...)
8552 {
8553   va_list ap;
8554 
8555   fputs (init_string, stream);
8556   assemble_name (stream, name);
8557   va_start (ap, final_string);
8558   vfprintf (stream, final_string, ap);
8559   va_end (ap);
8560 
8561   if (!TARGET_EXPLICIT_RELOCS)
8562     {
8563       tree name_tree = get_identifier (name);
8564       TREE_ASM_WRITTEN (name_tree) = 1;
8565     }
8566 }
8567 
8568 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
8569    NAME is the name of the object and ALIGN is the required alignment
8570    in bytes.  TAKES_ALIGNMENT_P is true if the directive takes a third
8571    alignment argument.  */
8572 
8573 void
8574 mips_declare_common_object (FILE *stream, const char *name,
8575 			    const char *init_string,
8576 			    unsigned HOST_WIDE_INT size,
8577 			    unsigned int align, bool takes_alignment_p)
8578 {
8579   if (!takes_alignment_p)
8580     {
8581       size += (align / BITS_PER_UNIT) - 1;
8582       size -= size % (align / BITS_PER_UNIT);
8583       mips_declare_object (stream, name, init_string,
8584 			   "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
8585     }
8586   else
8587     mips_declare_object (stream, name, init_string,
8588 			 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
8589 			 size, align / BITS_PER_UNIT);
8590 }
8591 
8592 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON.  This is usually the same as the
8593    elfos.h version, but we also need to handle -muninit-const-in-rodata.  */
8594 
8595 void
8596 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
8597 				 unsigned HOST_WIDE_INT size,
8598 				 unsigned int align)
8599 {
8600   /* If the target wants uninitialized const declarations in
8601      .rdata then don't put them in .comm.  */
8602   if (TARGET_EMBEDDED_DATA
8603       && TARGET_UNINIT_CONST_IN_RODATA
8604       && TREE_CODE (decl) == VAR_DECL
8605       && TREE_READONLY (decl)
8606       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
8607     {
8608       if (TREE_PUBLIC (decl) && DECL_NAME (decl))
8609 	targetm.asm_out.globalize_label (stream, name);
8610 
8611       switch_to_section (readonly_data_section);
8612       ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
8613       mips_declare_object (stream, name, "",
8614 			   ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
8615 			   size);
8616     }
8617   else
8618     mips_declare_common_object (stream, name, "\n\t.comm\t",
8619 				size, align, true);
8620 }
8621 
8622 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
8623 extern int size_directive_output;
8624 
8625 /* Implement ASM_DECLARE_OBJECT_NAME.  This is like most of the standard ELF
8626    definitions except that it uses mips_declare_object to emit the label.  */
8627 
8628 void
8629 mips_declare_object_name (FILE *stream, const char *name,
8630 			  tree decl ATTRIBUTE_UNUSED)
8631 {
8632 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
8633   ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
8634 #endif
8635 
8636   size_directive_output = 0;
8637   if (!flag_inhibit_size_directive && DECL_SIZE (decl))
8638     {
8639       HOST_WIDE_INT size;
8640 
8641       size_directive_output = 1;
8642       size = int_size_in_bytes (TREE_TYPE (decl));
8643       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8644     }
8645 
8646   mips_declare_object (stream, name, "", ":\n");
8647 }
8648 
8649 /* Implement ASM_FINISH_DECLARE_OBJECT.  This is generic ELF stuff.  */
8650 
8651 void
8652 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
8653 {
8654   const char *name;
8655 
8656   name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8657   if (!flag_inhibit_size_directive
8658       && DECL_SIZE (decl) != 0
8659       && !at_end
8660       && top_level
8661       && DECL_INITIAL (decl) == error_mark_node
8662       && !size_directive_output)
8663     {
8664       HOST_WIDE_INT size;
8665 
8666       size_directive_output = 1;
8667       size = int_size_in_bytes (TREE_TYPE (decl));
8668       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8669     }
8670 }
8671 #endif
8672 
8673 /* Return the FOO in the name of the ".mdebug.FOO" section associated
8674    with the current ABI.  */
8675 
8676 static const char *
8677 mips_mdebug_abi_name (void)
8678 {
8679   switch (mips_abi)
8680     {
8681     case ABI_32:
8682       return "abi32";
8683     case ABI_O64:
8684       return "abiO64";
8685     case ABI_N32:
8686       return "abiN32";
8687     case ABI_64:
8688       return "abi64";
8689     case ABI_EABI:
8690       return TARGET_64BIT ? "eabi64" : "eabi32";
8691     default:
8692       gcc_unreachable ();
8693     }
8694 }
8695 
8696 /* Implement TARGET_ASM_FILE_START.  */
8697 
8698 static void
8699 mips_file_start (void)
8700 {
8701   default_file_start ();
8702 
8703   /* Generate a special section to describe the ABI switches used to
8704      produce the resultant binary.  */
8705 
8706   /* Record the ABI itself.  Modern versions of binutils encode
8707      this information in the ELF header flags, but GDB needs the
8708      information in order to correctly debug binaries produced by
8709      older binutils.  See the function mips_gdbarch_init in
8710      gdb/mips-tdep.c.  */
8711   fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
8712 	   mips_mdebug_abi_name ());
8713 
8714   /* There is no ELF header flag to distinguish long32 forms of the
8715      EABI from long64 forms.  Emit a special section to help tools
8716      such as GDB.  Do the same for o64, which is sometimes used with
8717      -mlong64.  */
8718   if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
8719     fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
8720 	     "\t.previous\n", TARGET_LONG64 ? 64 : 32);
8721 
8722 #ifdef HAVE_AS_GNU_ATTRIBUTE
8723   {
8724     int attr;
8725 
8726     /* No floating-point operations, -mno-float.  */
8727     if (TARGET_NO_FLOAT)
8728       attr = 0;
8729     /* Soft-float code, -msoft-float.  */
8730     else if (!TARGET_HARD_FLOAT_ABI)
8731       attr = 3;
8732     /* Single-float code, -msingle-float.  */
8733     else if (!TARGET_DOUBLE_FLOAT)
8734       attr = 2;
8735     /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64.  */
8736     else if (!TARGET_64BIT && TARGET_FLOAT64)
8737       attr = 4;
8738     /* Regular FP code, FP regs same size as GP regs, -mdouble-float.  */
8739     else
8740       attr = 1;
8741 
8742     fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
8743   }
8744 #endif
8745 
8746   /* If TARGET_ABICALLS, tell GAS to generate -KPIC code.  */
8747   if (TARGET_ABICALLS)
8748     {
8749       fprintf (asm_out_file, "\t.abicalls\n");
8750       if (TARGET_ABICALLS_PIC0)
8751 	fprintf (asm_out_file, "\t.option\tpic0\n");
8752     }
8753 
8754   if (flag_verbose_asm)
8755     fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
8756 	     ASM_COMMENT_START,
8757 	     mips_small_data_threshold, mips_arch_info->name, mips_isa);
8758 }
8759 
8760 /* Implement TARGET_ASM_CODE_END.  */
8761 
8762 static void
8763 mips_code_end (void)
8764 {
8765   if (mips_need_mips16_rdhwr_p)
8766     mips_output_mips16_rdhwr ();
8767 }
8768 
8769 /* Make the last instruction frame-related and note that it performs
8770    the operation described by FRAME_PATTERN.  */
8771 
8772 static void
8773 mips_set_frame_expr (rtx frame_pattern)
8774 {
8775   rtx insn;
8776 
8777   insn = get_last_insn ();
8778   RTX_FRAME_RELATED_P (insn) = 1;
8779   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8780 				      frame_pattern,
8781 				      REG_NOTES (insn));
8782 }
8783 
8784 /* Return a frame-related rtx that stores REG at MEM.
8785    REG must be a single register.  */
8786 
8787 static rtx
8788 mips_frame_set (rtx mem, rtx reg)
8789 {
8790   rtx set;
8791 
8792   set = gen_rtx_SET (VOIDmode, mem, reg);
8793   RTX_FRAME_RELATED_P (set) = 1;
8794 
8795   return set;
8796 }
8797 
8798 /* Record that the epilogue has restored call-saved register REG.  */
8799 
8800 static void
8801 mips_add_cfa_restore (rtx reg)
8802 {
8803   mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
8804 					       mips_epilogue.cfa_restores);
8805 }
8806 
8807 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
8808    mips16e_s2_s8_regs[X], it must also save the registers in indexes
8809    X + 1 onwards.  Likewise mips16e_a0_a3_regs.  */
8810 static const unsigned char mips16e_s2_s8_regs[] = {
8811   30, 23, 22, 21, 20, 19, 18
8812 };
8813 static const unsigned char mips16e_a0_a3_regs[] = {
8814   4, 5, 6, 7
8815 };
8816 
8817 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
8818    ordered from the uppermost in memory to the lowest in memory.  */
8819 static const unsigned char mips16e_save_restore_regs[] = {
8820   31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
8821 };
8822 
8823 /* Return the index of the lowest X in the range [0, SIZE) for which
8824    bit REGS[X] is set in MASK.  Return SIZE if there is no such X.  */
8825 
8826 static unsigned int
8827 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
8828 			     unsigned int size)
8829 {
8830   unsigned int i;
8831 
8832   for (i = 0; i < size; i++)
8833     if (BITSET_P (mask, regs[i]))
8834       break;
8835 
8836   return i;
8837 }
8838 
8839 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
8840    is the number of set bits.  If *MASK_PTR contains REGS[X] for some X
8841    in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
8842    is true for all indexes (X, SIZE).  */
8843 
8844 static void
8845 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
8846 			unsigned int size, unsigned int *num_regs_ptr)
8847 {
8848   unsigned int i;
8849 
8850   i = mips16e_find_first_register (*mask_ptr, regs, size);
8851   for (i++; i < size; i++)
8852     if (!BITSET_P (*mask_ptr, regs[i]))
8853       {
8854 	*num_regs_ptr += 1;
8855 	*mask_ptr |= 1 << regs[i];
8856       }
8857 }
8858 
8859 /* Return a simplified form of X using the register values in REG_VALUES.
8860    REG_VALUES[R] is the last value assigned to hard register R, or null
8861    if R has not been modified.
8862 
8863    This function is rather limited, but is good enough for our purposes.  */
8864 
8865 static rtx
8866 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
8867 {
8868   x = avoid_constant_pool_reference (x);
8869 
8870   if (UNARY_P (x))
8871     {
8872       rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
8873       return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
8874 				 x0, GET_MODE (XEXP (x, 0)));
8875     }
8876 
8877   if (ARITHMETIC_P (x))
8878     {
8879       rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
8880       rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
8881       return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
8882     }
8883 
8884   if (REG_P (x)
8885       && reg_values[REGNO (x)]
8886       && !rtx_unstable_p (reg_values[REGNO (x)]))
8887     return reg_values[REGNO (x)];
8888 
8889   return x;
8890 }
8891 
8892 /* Return true if (set DEST SRC) stores an argument register into its
8893    caller-allocated save slot, storing the number of that argument
8894    register in *REGNO_PTR if so.  REG_VALUES is as for
8895    mips16e_collect_propagate_value.  */
8896 
8897 static bool
8898 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
8899 				 unsigned int *regno_ptr)
8900 {
8901   unsigned int argno, regno;
8902   HOST_WIDE_INT offset, required_offset;
8903   rtx addr, base;
8904 
8905   /* Check that this is a word-mode store.  */
8906   if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
8907     return false;
8908 
8909   /* Check that the register being saved is an unmodified argument
8910      register.  */
8911   regno = REGNO (src);
8912   if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
8913     return false;
8914   argno = regno - GP_ARG_FIRST;
8915 
8916   /* Check whether the address is an appropriate stack-pointer or
8917      frame-pointer access.  */
8918   addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
8919   mips_split_plus (addr, &base, &offset);
8920   required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
8921   if (base == hard_frame_pointer_rtx)
8922     required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
8923   else if (base != stack_pointer_rtx)
8924     return false;
8925   if (offset != required_offset)
8926     return false;
8927 
8928   *regno_ptr = regno;
8929   return true;
8930 }
8931 
8932 /* A subroutine of mips_expand_prologue, called only when generating
8933    MIPS16e SAVE instructions.  Search the start of the function for any
8934    instructions that save argument registers into their caller-allocated
8935    save slots.  Delete such instructions and return a value N such that
8936    saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
8937    instructions redundant.  */
8938 
8939 static unsigned int
8940 mips16e_collect_argument_saves (void)
8941 {
8942   rtx reg_values[FIRST_PSEUDO_REGISTER];
8943   rtx insn, next, set, dest, src;
8944   unsigned int nargs, regno;
8945 
8946   push_topmost_sequence ();
8947   nargs = 0;
8948   memset (reg_values, 0, sizeof (reg_values));
8949   for (insn = get_insns (); insn; insn = next)
8950     {
8951       next = NEXT_INSN (insn);
8952       if (NOTE_P (insn) || DEBUG_INSN_P (insn))
8953 	continue;
8954 
8955       if (!INSN_P (insn))
8956 	break;
8957 
8958       set = PATTERN (insn);
8959       if (GET_CODE (set) != SET)
8960 	break;
8961 
8962       dest = SET_DEST (set);
8963       src = SET_SRC (set);
8964       if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
8965 	{
8966 	  if (!BITSET_P (cfun->machine->frame.mask, regno))
8967 	    {
8968 	      delete_insn (insn);
8969 	      nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
8970 	    }
8971 	}
8972       else if (REG_P (dest) && GET_MODE (dest) == word_mode)
8973 	reg_values[REGNO (dest)]
8974 	  = mips16e_collect_propagate_value (src, reg_values);
8975       else
8976 	break;
8977     }
8978   pop_topmost_sequence ();
8979 
8980   return nargs;
8981 }
8982 
8983 /* Return a move between register REGNO and memory location SP + OFFSET.
8984    REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
8985    Make the move a load if RESTORE_P, otherwise make it a store.  */
8986 
8987 static rtx
8988 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
8989 			  HOST_WIDE_INT offset, unsigned int regno)
8990 {
8991   rtx reg, mem;
8992 
8993   mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
8994 					      offset));
8995   reg = gen_rtx_REG (SImode, regno);
8996   if (restore_p)
8997     {
8998       mips_add_cfa_restore (reg);
8999       return gen_rtx_SET (VOIDmode, reg, mem);
9000     }
9001   if (reg_parm_p)
9002     return gen_rtx_SET (VOIDmode, mem, reg);
9003   return mips_frame_set (mem, reg);
9004 }
9005 
9006 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
9007    The instruction must:
9008 
9009      - Allocate or deallocate SIZE bytes in total; SIZE is known
9010        to be nonzero.
9011 
9012      - Save or restore as many registers in *MASK_PTR as possible.
9013        The instruction saves the first registers at the top of the
9014        allocated area, with the other registers below it.
9015 
9016      - Save NARGS argument registers above the allocated area.
9017 
9018    (NARGS is always zero if RESTORE_P.)
9019 
9020    The SAVE and RESTORE instructions cannot save and restore all general
9021    registers, so there may be some registers left over for the caller to
9022    handle.  Destructively modify *MASK_PTR so that it contains the registers
9023    that still need to be saved or restored.  The caller can save these
9024    registers in the memory immediately below *OFFSET_PTR, which is a
9025    byte offset from the bottom of the allocated stack area.  */
9026 
9027 static rtx
9028 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
9029 			    HOST_WIDE_INT *offset_ptr, unsigned int nargs,
9030 			    HOST_WIDE_INT size)
9031 {
9032   rtx pattern, set;
9033   HOST_WIDE_INT offset, top_offset;
9034   unsigned int i, regno;
9035   int n;
9036 
9037   gcc_assert (cfun->machine->frame.num_fp == 0);
9038 
9039   /* Calculate the number of elements in the PARALLEL.  We need one element
9040      for the stack adjustment, one for each argument register save, and one
9041      for each additional register move.  */
9042   n = 1 + nargs;
9043   for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9044     if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
9045       n++;
9046 
9047   /* Create the final PARALLEL.  */
9048   pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
9049   n = 0;
9050 
9051   /* Add the stack pointer adjustment.  */
9052   set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9053 		     plus_constant (Pmode, stack_pointer_rtx,
9054 				    restore_p ? size : -size));
9055   RTX_FRAME_RELATED_P (set) = 1;
9056   XVECEXP (pattern, 0, n++) = set;
9057 
9058   /* Stack offsets in the PARALLEL are relative to the old stack pointer.  */
9059   top_offset = restore_p ? size : 0;
9060 
9061   /* Save the arguments.  */
9062   for (i = 0; i < nargs; i++)
9063     {
9064       offset = top_offset + i * UNITS_PER_WORD;
9065       set = mips16e_save_restore_reg (restore_p, true, offset,
9066 				      GP_ARG_FIRST + i);
9067       XVECEXP (pattern, 0, n++) = set;
9068     }
9069 
9070   /* Then fill in the other register moves.  */
9071   offset = top_offset;
9072   for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9073     {
9074       regno = mips16e_save_restore_regs[i];
9075       if (BITSET_P (*mask_ptr, regno))
9076 	{
9077 	  offset -= UNITS_PER_WORD;
9078 	  set = mips16e_save_restore_reg (restore_p, false, offset, regno);
9079 	  XVECEXP (pattern, 0, n++) = set;
9080 	  *mask_ptr &= ~(1 << regno);
9081 	}
9082     }
9083 
9084   /* Tell the caller what offset it should use for the remaining registers.  */
9085   *offset_ptr = size + (offset - top_offset);
9086 
9087   gcc_assert (n == XVECLEN (pattern, 0));
9088 
9089   return pattern;
9090 }
9091 
9092 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
9093    pointer.  Return true if PATTERN matches the kind of instruction
9094    generated by mips16e_build_save_restore.  If INFO is nonnull,
9095    initialize it when returning true.  */
9096 
9097 bool
9098 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
9099 				struct mips16e_save_restore_info *info)
9100 {
9101   unsigned int i, nargs, mask, extra;
9102   HOST_WIDE_INT top_offset, save_offset, offset;
9103   rtx set, reg, mem, base;
9104   int n;
9105 
9106   if (!GENERATE_MIPS16E_SAVE_RESTORE)
9107     return false;
9108 
9109   /* Stack offsets in the PARALLEL are relative to the old stack pointer.  */
9110   top_offset = adjust > 0 ? adjust : 0;
9111 
9112   /* Interpret all other members of the PARALLEL.  */
9113   save_offset = top_offset - UNITS_PER_WORD;
9114   mask = 0;
9115   nargs = 0;
9116   i = 0;
9117   for (n = 1; n < XVECLEN (pattern, 0); n++)
9118     {
9119       /* Check that we have a SET.  */
9120       set = XVECEXP (pattern, 0, n);
9121       if (GET_CODE (set) != SET)
9122 	return false;
9123 
9124       /* Check that the SET is a load (if restoring) or a store
9125 	 (if saving).  */
9126       mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
9127       if (!MEM_P (mem))
9128 	return false;
9129 
9130       /* Check that the address is the sum of the stack pointer and a
9131 	 possibly-zero constant offset.  */
9132       mips_split_plus (XEXP (mem, 0), &base, &offset);
9133       if (base != stack_pointer_rtx)
9134 	return false;
9135 
9136       /* Check that SET's other operand is a register.  */
9137       reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
9138       if (!REG_P (reg))
9139 	return false;
9140 
9141       /* Check for argument saves.  */
9142       if (offset == top_offset + nargs * UNITS_PER_WORD
9143 	  && REGNO (reg) == GP_ARG_FIRST + nargs)
9144 	nargs++;
9145       else if (offset == save_offset)
9146 	{
9147 	  while (mips16e_save_restore_regs[i++] != REGNO (reg))
9148 	    if (i == ARRAY_SIZE (mips16e_save_restore_regs))
9149 	      return false;
9150 
9151 	  mask |= 1 << REGNO (reg);
9152 	  save_offset -= UNITS_PER_WORD;
9153 	}
9154       else
9155 	return false;
9156     }
9157 
9158   /* Check that the restrictions on register ranges are met.  */
9159   extra = 0;
9160   mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
9161 			  ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
9162   mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
9163 			  ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
9164   if (extra != 0)
9165     return false;
9166 
9167   /* Make sure that the topmost argument register is not saved twice.
9168      The checks above ensure that the same is then true for the other
9169      argument registers.  */
9170   if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
9171     return false;
9172 
9173   /* Pass back information, if requested.  */
9174   if (info)
9175     {
9176       info->nargs = nargs;
9177       info->mask = mask;
9178       info->size = (adjust > 0 ? adjust : -adjust);
9179     }
9180 
9181   return true;
9182 }
9183 
9184 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
9185    for the register range [MIN_REG, MAX_REG].  Return a pointer to
9186    the null terminator.  */
9187 
9188 static char *
9189 mips16e_add_register_range (char *s, unsigned int min_reg,
9190 			    unsigned int max_reg)
9191 {
9192   if (min_reg != max_reg)
9193     s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
9194   else
9195     s += sprintf (s, ",%s", reg_names[min_reg]);
9196   return s;
9197 }
9198 
9199 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
9200    PATTERN and ADJUST are as for mips16e_save_restore_pattern_p.  */
9201 
9202 const char *
9203 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
9204 {
9205   static char buffer[300];
9206 
9207   struct mips16e_save_restore_info info;
9208   unsigned int i, end;
9209   char *s;
9210 
9211   /* Parse the pattern.  */
9212   if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
9213     gcc_unreachable ();
9214 
9215   /* Add the mnemonic.  */
9216   s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
9217   s += strlen (s);
9218 
9219   /* Save the arguments.  */
9220   if (info.nargs > 1)
9221     s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
9222 		  reg_names[GP_ARG_FIRST + info.nargs - 1]);
9223   else if (info.nargs == 1)
9224     s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
9225 
9226   /* Emit the amount of stack space to allocate or deallocate.  */
9227   s += sprintf (s, "%d", (int) info.size);
9228 
9229   /* Save or restore $16.  */
9230   if (BITSET_P (info.mask, 16))
9231     s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
9232 
9233   /* Save or restore $17.  */
9234   if (BITSET_P (info.mask, 17))
9235     s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
9236 
9237   /* Save or restore registers in the range $s2...$s8, which
9238      mips16e_s2_s8_regs lists in decreasing order.  Note that this
9239      is a software register range; the hardware registers are not
9240      numbered consecutively.  */
9241   end = ARRAY_SIZE (mips16e_s2_s8_regs);
9242   i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
9243   if (i < end)
9244     s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
9245 				    mips16e_s2_s8_regs[i]);
9246 
9247   /* Save or restore registers in the range $a0...$a3.  */
9248   end = ARRAY_SIZE (mips16e_a0_a3_regs);
9249   i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
9250   if (i < end)
9251     s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
9252 				    mips16e_a0_a3_regs[end - 1]);
9253 
9254   /* Save or restore $31.  */
9255   if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
9256     s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
9257 
9258   return buffer;
9259 }
9260 
9261 /* Return true if the current function returns its value in a floating-point
9262    register in MIPS16 mode.  */
9263 
9264 static bool
9265 mips16_cfun_returns_in_fpr_p (void)
9266 {
9267   tree return_type = DECL_RESULT (current_function_decl);
9268   return (TARGET_MIPS16
9269 	  && TARGET_HARD_FLOAT_ABI
9270 	  && !aggregate_value_p (return_type, current_function_decl)
9271  	  && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
9272 }
9273 
9274 /* Return true if predicate PRED is true for at least one instruction.
9275    Cache the result in *CACHE, and assume that the result is true
9276    if *CACHE is already true.  */
9277 
9278 static bool
9279 mips_find_gp_ref (bool *cache, bool (*pred) (rtx))
9280 {
9281   rtx insn;
9282 
9283   if (!*cache)
9284     {
9285       push_topmost_sequence ();
9286       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9287 	if (USEFUL_INSN_P (insn) && pred (insn))
9288 	  {
9289 	    *cache = true;
9290 	    break;
9291 	  }
9292       pop_topmost_sequence ();
9293     }
9294   return *cache;
9295 }
9296 
9297 /* Return true if INSN refers to the global pointer in an "inflexible" way.
9298    See mips_cfun_has_inflexible_gp_ref_p for details.  */
9299 
9300 static bool
9301 mips_insn_has_inflexible_gp_ref_p (rtx insn)
9302 {
9303   /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
9304      indicate that the target could be a traditional MIPS
9305      lazily-binding stub.  */
9306   return find_reg_fusage (insn, USE, pic_offset_table_rtx);
9307 }
9308 
9309 /* Return true if the current function refers to the global pointer
9310    in a way that forces $28 to be valid.  This means that we can't
9311    change the choice of global pointer, even for NewABI code.
9312 
9313    One example of this (and one which needs several checks) is that
9314    $28 must be valid when calling traditional MIPS lazy-binding stubs.
9315    (This restriction does not apply to PLTs.)  */
9316 
9317 static bool
9318 mips_cfun_has_inflexible_gp_ref_p (void)
9319 {
9320   /* If the function has a nonlocal goto, $28 must hold the correct
9321      global pointer for the target function.  That is, the target
9322      of the goto implicitly uses $28.  */
9323   if (crtl->has_nonlocal_goto)
9324     return true;
9325 
9326   if (TARGET_ABICALLS_PIC2)
9327     {
9328       /* Symbolic accesses implicitly use the global pointer unless
9329 	 -mexplicit-relocs is in effect.  JAL macros to symbolic addresses
9330 	 might go to traditional MIPS lazy-binding stubs.  */
9331       if (!TARGET_EXPLICIT_RELOCS)
9332 	return true;
9333 
9334       /* FUNCTION_PROFILER includes a JAL to _mcount, which again
9335 	 can be lazily-bound.  */
9336       if (crtl->profile)
9337 	return true;
9338 
9339       /* MIPS16 functions that return in FPRs need to call an
9340 	 external libgcc routine.  This call is only made explict
9341 	 during mips_expand_epilogue, and it too might be lazily bound.  */
9342       if (mips16_cfun_returns_in_fpr_p ())
9343 	return true;
9344     }
9345 
9346   return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
9347 			   mips_insn_has_inflexible_gp_ref_p);
9348 }
9349 
9350 /* Return true if INSN refers to the global pointer in a "flexible" way.
9351    See mips_cfun_has_flexible_gp_ref_p for details.  */
9352 
9353 static bool
9354 mips_insn_has_flexible_gp_ref_p (rtx insn)
9355 {
9356   return (get_attr_got (insn) != GOT_UNSET
9357 	  || mips_small_data_pattern_p (PATTERN (insn))
9358 	  || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
9359 }
9360 
9361 /* Return true if the current function references the global pointer,
9362    but if those references do not inherently require the global pointer
9363    to be $28.  Assume !mips_cfun_has_inflexible_gp_ref_p ().  */
9364 
9365 static bool
9366 mips_cfun_has_flexible_gp_ref_p (void)
9367 {
9368   /* Reload can sometimes introduce constant pool references
9369      into a function that otherwise didn't need them.  For example,
9370      suppose we have an instruction like:
9371 
9372 	(set (reg:DF R1) (float:DF (reg:SI R2)))
9373 
9374      If R2 turns out to be a constant such as 1, the instruction may
9375      have a REG_EQUAL note saying that R1 == 1.0.  Reload then has
9376      the option of using this constant if R2 doesn't get allocated
9377      to a register.
9378 
9379      In cases like these, reload will have added the constant to the
9380      pool but no instruction will yet refer to it.  */
9381   if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
9382     return true;
9383 
9384   return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
9385 			   mips_insn_has_flexible_gp_ref_p);
9386 }
9387 
9388 /* Return the register that should be used as the global pointer
9389    within this function.  Return INVALID_REGNUM if the function
9390    doesn't need a global pointer.  */
9391 
9392 static unsigned int
9393 mips_global_pointer (void)
9394 {
9395   unsigned int regno;
9396 
9397   /* $gp is always available unless we're using a GOT.  */
9398   if (!TARGET_USE_GOT)
9399     return GLOBAL_POINTER_REGNUM;
9400 
9401   /* If there are inflexible references to $gp, we must use the
9402      standard register.  */
9403   if (mips_cfun_has_inflexible_gp_ref_p ())
9404     return GLOBAL_POINTER_REGNUM;
9405 
9406   /* If there are no current references to $gp, then the only uses
9407      we can introduce later are those involved in long branches.  */
9408   if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
9409     return INVALID_REGNUM;
9410 
9411   /* If the global pointer is call-saved, try to use a call-clobbered
9412      alternative.  */
9413   if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
9414     for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9415       if (!df_regs_ever_live_p (regno)
9416 	  && call_really_used_regs[regno]
9417 	  && !fixed_regs[regno]
9418 	  && regno != PIC_FUNCTION_ADDR_REGNUM)
9419 	return regno;
9420 
9421   return GLOBAL_POINTER_REGNUM;
9422 }
9423 
9424 /* Return true if the current function's prologue must load the global
9425    pointer value into pic_offset_table_rtx and store the same value in
9426    the function's cprestore slot (if any).
9427 
9428    One problem we have to deal with is that, when emitting GOT-based
9429    position independent code, long-branch sequences will need to load
9430    the address of the branch target from the GOT.  We don't know until
9431    the very end of compilation whether (and where) the function needs
9432    long branches, so we must ensure that _any_ branch can access the
9433    global pointer in some form.  However, we do not want to pessimize
9434    the usual case in which all branches are short.
9435 
9436    We handle this as follows:
9437 
9438    (1) During reload, we set cfun->machine->global_pointer to
9439        INVALID_REGNUM if we _know_ that the current function
9440        doesn't need a global pointer.  This is only valid if
9441        long branches don't need the GOT.
9442 
9443        Otherwise, we assume that we might need a global pointer
9444        and pick an appropriate register.
9445 
9446    (2) If cfun->machine->global_pointer != INVALID_REGNUM,
9447        we ensure that the global pointer is available at every
9448        block boundary bar entry and exit.  We do this in one of two ways:
9449 
9450        - If the function has a cprestore slot, we ensure that this
9451 	 slot is valid at every branch.  However, as explained in
9452 	 point (6) below, there is no guarantee that pic_offset_table_rtx
9453 	 itself is valid if new uses of the global pointer are introduced
9454 	 after the first post-epilogue split.
9455 
9456 	 We guarantee that the cprestore slot is valid by loading it
9457 	 into a fake register, CPRESTORE_SLOT_REGNUM.  We then make
9458 	 this register live at every block boundary bar function entry
9459 	 and exit.  It is then invalid to move the load (and thus the
9460 	 preceding store) across a block boundary.
9461 
9462        - If the function has no cprestore slot, we guarantee that
9463 	 pic_offset_table_rtx itself is valid at every branch.
9464 
9465        See mips_eh_uses for the handling of the register liveness.
9466 
9467    (3) During prologue and epilogue generation, we emit "ghost"
9468        placeholder instructions to manipulate the global pointer.
9469 
9470    (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
9471        and cfun->machine->must_restore_gp_when_clobbered_p if we already know
9472        that the function needs a global pointer.  (There is no need to set
9473        them earlier than this, and doing it as late as possible leads to
9474        fewer false positives.)
9475 
9476    (5) If cfun->machine->must_initialize_gp_p is true during a
9477        split_insns pass, we split the ghost instructions into real
9478        instructions.  These split instructions can then be optimized in
9479        the usual way.  Otherwise, we keep the ghost instructions intact,
9480        and optimize for the case where they aren't needed.  We still
9481        have the option of splitting them later, if we need to introduce
9482        new uses of the global pointer.
9483 
9484        For example, the scheduler ignores a ghost instruction that
9485        stores $28 to the stack, but it handles the split form of
9486        the ghost instruction as an ordinary store.
9487 
9488    (6) [OldABI only.]  If cfun->machine->must_restore_gp_when_clobbered_p
9489        is true during the first post-epilogue split_insns pass, we split
9490        calls and restore_gp patterns into instructions that explicitly
9491        load pic_offset_table_rtx from the cprestore slot.  Otherwise,
9492        we split these patterns into instructions that _don't_ load from
9493        the cprestore slot.
9494 
9495        If cfun->machine->must_restore_gp_when_clobbered_p is true at the
9496        time of the split, then any instructions that exist at that time
9497        can make free use of pic_offset_table_rtx.  However, if we want
9498        to introduce new uses of the global pointer after the split,
9499        we must explicitly load the value from the cprestore slot, since
9500        pic_offset_table_rtx itself might not be valid at a given point
9501        in the function.
9502 
9503        The idea is that we want to be able to delete redundant
9504        loads from the cprestore slot in the usual case where no
9505        long branches are needed.
9506 
9507    (7) If cfun->machine->must_initialize_gp_p is still false at the end
9508        of md_reorg, we decide whether the global pointer is needed for
9509        long branches.  If so, we set cfun->machine->must_initialize_gp_p
9510        to true and split the ghost instructions into real instructions
9511        at that stage.
9512 
9513    Note that the ghost instructions must have a zero length for three reasons:
9514 
9515    - Giving the length of the underlying $gp sequence might cause
9516      us to use long branches in cases where they aren't really needed.
9517 
9518    - They would perturb things like alignment calculations.
9519 
9520    - More importantly, the hazard detection in md_reorg relies on
9521      empty instructions having a zero length.
9522 
9523    If we find a long branch and split the ghost instructions at the
9524    end of md_reorg, the split could introduce more long branches.
9525    That isn't a problem though, because we still do the split before
9526    the final shorten_branches pass.
9527 
9528    This is extremely ugly, but it seems like the best compromise between
9529    correctness and efficiency.  */
9530 
9531 bool
9532 mips_must_initialize_gp_p (void)
9533 {
9534   return cfun->machine->must_initialize_gp_p;
9535 }
9536 
9537 /* Return true if REGNO is a register that is ordinarily call-clobbered
9538    but must nevertheless be preserved by an interrupt handler.  */
9539 
9540 static bool
9541 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
9542 {
9543   if (MD_REG_P (regno))
9544     return true;
9545 
9546   if (TARGET_DSP && DSP_ACC_REG_P (regno))
9547     return true;
9548 
9549   if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
9550     {
9551       /* $0 is hard-wired.  */
9552       if (regno == GP_REG_FIRST)
9553 	return false;
9554 
9555       /* The interrupt handler can treat kernel registers as
9556 	 scratch registers.  */
9557       if (KERNEL_REG_P (regno))
9558 	return false;
9559 
9560       /* The function will return the stack pointer to its original value
9561 	 anyway.  */
9562       if (regno == STACK_POINTER_REGNUM)
9563 	return false;
9564 
9565       /* Otherwise, return true for registers that aren't ordinarily
9566 	 call-clobbered.  */
9567       return call_really_used_regs[regno];
9568     }
9569 
9570   return false;
9571 }
9572 
9573 /* Return true if the current function should treat register REGNO
9574    as call-saved.  */
9575 
9576 static bool
9577 mips_cfun_call_saved_reg_p (unsigned int regno)
9578 {
9579   /* If the user makes an ordinarily-call-saved register global,
9580      that register is no longer call-saved.  */
9581   if (global_regs[regno])
9582     return false;
9583 
9584   /* Interrupt handlers need to save extra registers.  */
9585   if (cfun->machine->interrupt_handler_p
9586       && mips_interrupt_extra_call_saved_reg_p (regno))
9587     return true;
9588 
9589   /* call_insns preserve $28 unless they explicitly say otherwise,
9590      so call_really_used_regs[] treats $28 as call-saved.  However,
9591      we want the ABI property rather than the default call_insn
9592      property here.  */
9593   return (regno == GLOBAL_POINTER_REGNUM
9594 	  ? TARGET_CALL_SAVED_GP
9595 	  : !call_really_used_regs[regno]);
9596 }
9597 
9598 /* Return true if the function body might clobber register REGNO.
9599    We know that REGNO is call-saved.  */
9600 
9601 static bool
9602 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
9603 {
9604   /* Some functions should be treated as clobbering all call-saved
9605      registers.  */
9606   if (crtl->saves_all_registers)
9607     return true;
9608 
9609   /* DF handles cases where a register is explicitly referenced in
9610      the rtl.  Incoming values are passed in call-clobbered registers,
9611      so we can assume that any live call-saved register is set within
9612      the function.  */
9613   if (df_regs_ever_live_p (regno))
9614     return true;
9615 
9616   /* Check for registers that are clobbered by FUNCTION_PROFILER.
9617      These clobbers are not explicit in the rtl.  */
9618   if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
9619     return true;
9620 
9621   /* If we're using a call-saved global pointer, the function's
9622      prologue will need to set it up.  */
9623   if (cfun->machine->global_pointer == regno)
9624     return true;
9625 
9626   /* The function's prologue will need to set the frame pointer if
9627      frame_pointer_needed.  */
9628   if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
9629     return true;
9630 
9631   /* If a MIPS16 function returns a value in FPRs, its epilogue
9632      will need to call an external libgcc routine.  This yet-to-be
9633      generated call_insn will clobber $31.  */
9634   if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
9635     return true;
9636 
9637   /* If REGNO is ordinarily call-clobbered, we must assume that any
9638      called function could modify it.  */
9639   if (cfun->machine->interrupt_handler_p
9640       && !crtl->is_leaf
9641       && mips_interrupt_extra_call_saved_reg_p (regno))
9642     return true;
9643 
9644   return false;
9645 }
9646 
9647 /* Return true if the current function must save register REGNO.  */
9648 
9649 static bool
9650 mips_save_reg_p (unsigned int regno)
9651 {
9652   if (mips_cfun_call_saved_reg_p (regno))
9653     {
9654       if (mips_cfun_might_clobber_call_saved_reg_p (regno))
9655 	return true;
9656 
9657       /* Save both registers in an FPR pair if either one is used.  This is
9658 	 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
9659 	 register to be used without the even register.  */
9660       if (FP_REG_P (regno)
9661 	  && MAX_FPRS_PER_FMT == 2
9662 	  && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
9663 	return true;
9664     }
9665 
9666   /* We need to save the incoming return address if __builtin_eh_return
9667      is being used to set a different return address.  */
9668   if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
9669     return true;
9670 
9671   return false;
9672 }
9673 
9674 /* Populate the current function's mips_frame_info structure.
9675 
9676    MIPS stack frames look like:
9677 
9678 	+-------------------------------+
9679 	|                               |
9680 	|  incoming stack arguments     |
9681 	|                               |
9682 	+-------------------------------+
9683 	|                               |
9684 	|  caller-allocated save area   |
9685       A |  for register arguments       |
9686 	|                               |
9687 	+-------------------------------+ <-- incoming stack pointer
9688 	|                               |
9689 	|  callee-allocated save area   |
9690       B |  for arguments that are       |
9691 	|  split between registers and  |
9692 	|  the stack                    |
9693 	|                               |
9694 	+-------------------------------+ <-- arg_pointer_rtx
9695 	|                               |
9696       C |  callee-allocated save area   |
9697 	|  for register varargs         |
9698 	|                               |
9699 	+-------------------------------+ <-- frame_pointer_rtx
9700 	|                               |       + cop0_sp_offset
9701 	|  COP0 reg save area           |	+ UNITS_PER_WORD
9702 	|                               |
9703 	+-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
9704 	|                               |       + UNITS_PER_WORD
9705 	|  accumulator save area        |
9706 	|                               |
9707 	+-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
9708 	|                               |       + UNITS_PER_HWFPVALUE
9709 	|  FPR save area                |
9710 	|                               |
9711 	+-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
9712 	|                               |       + UNITS_PER_WORD
9713 	|  GPR save area                |
9714 	|                               |
9715 	+-------------------------------+ <-- frame_pointer_rtx with
9716 	|                               | \     -fstack-protector
9717 	|  local variables              |  | var_size
9718 	|                               | /
9719 	+-------------------------------+
9720 	|                               | \
9721 	|  $gp save area                |  | cprestore_size
9722 	|                               | /
9723       P +-------------------------------+ <-- hard_frame_pointer_rtx for
9724 	|                               | \     MIPS16 code
9725 	|  outgoing stack arguments     |  |
9726 	|                               |  |
9727 	+-------------------------------+  | args_size
9728 	|                               |  |
9729 	|  caller-allocated save area   |  |
9730 	|  for register arguments       |  |
9731 	|                               | /
9732 	+-------------------------------+ <-- stack_pointer_rtx
9733 					      frame_pointer_rtx without
9734 					        -fstack-protector
9735 					      hard_frame_pointer_rtx for
9736 						non-MIPS16 code.
9737 
9738    At least two of A, B and C will be empty.
9739 
9740    Dynamic stack allocations such as alloca insert data at point P.
9741    They decrease stack_pointer_rtx but leave frame_pointer_rtx and
9742    hard_frame_pointer_rtx unchanged.  */
9743 
9744 static void
9745 mips_compute_frame_info (void)
9746 {
9747   struct mips_frame_info *frame;
9748   HOST_WIDE_INT offset, size;
9749   unsigned int regno, i;
9750 
9751   /* Set this function's interrupt properties.  */
9752   if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
9753     {
9754       if (!ISA_MIPS32R2)
9755 	error ("the %<interrupt%> attribute requires a MIPS32r2 processor");
9756       else if (TARGET_HARD_FLOAT)
9757 	error ("the %<interrupt%> attribute requires %<-msoft-float%>");
9758       else if (TARGET_MIPS16)
9759 	error ("interrupt handlers cannot be MIPS16 functions");
9760       else
9761 	{
9762 	  cfun->machine->interrupt_handler_p = true;
9763 	  cfun->machine->use_shadow_register_set_p =
9764 	    mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
9765 	  cfun->machine->keep_interrupts_masked_p =
9766 	    mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
9767 	  cfun->machine->use_debug_exception_return_p =
9768 	    mips_use_debug_exception_return_p (TREE_TYPE
9769 					       (current_function_decl));
9770 	}
9771     }
9772 
9773   frame = &cfun->machine->frame;
9774   memset (frame, 0, sizeof (*frame));
9775   size = get_frame_size ();
9776 
9777   cfun->machine->global_pointer = mips_global_pointer ();
9778 
9779   /* The first two blocks contain the outgoing argument area and the $gp save
9780      slot.  This area isn't needed in leaf functions, but if the
9781      target-independent frame size is nonzero, we have already committed to
9782      allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD.  */
9783   if ((size == 0 || FRAME_GROWS_DOWNWARD) && crtl->is_leaf)
9784     {
9785       /* The MIPS 3.0 linker does not like functions that dynamically
9786 	 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
9787 	 looks like we are trying to create a second frame pointer to the
9788 	 function, so allocate some stack space to make it happy.  */
9789       if (cfun->calls_alloca)
9790 	frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
9791       else
9792 	frame->args_size = 0;
9793       frame->cprestore_size = 0;
9794     }
9795   else
9796     {
9797       frame->args_size = crtl->outgoing_args_size;
9798       frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
9799     }
9800   offset = frame->args_size + frame->cprestore_size;
9801 
9802   /* Move above the local variables.  */
9803   frame->var_size = MIPS_STACK_ALIGN (size);
9804   offset += frame->var_size;
9805 
9806   /* Find out which GPRs we need to save.  */
9807   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9808     if (mips_save_reg_p (regno))
9809       {
9810 	frame->num_gp++;
9811 	frame->mask |= 1 << (regno - GP_REG_FIRST);
9812       }
9813 
9814   /* If this function calls eh_return, we must also save and restore the
9815      EH data registers.  */
9816   if (crtl->calls_eh_return)
9817     for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
9818       {
9819 	frame->num_gp++;
9820 	frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
9821       }
9822 
9823   /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
9824      $a3-$a0 and $s2-$s8.  If we save one register in the range, we must
9825      save all later registers too.  */
9826   if (GENERATE_MIPS16E_SAVE_RESTORE)
9827     {
9828       mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
9829  			      ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
9830       mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
9831  			      ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
9832     }
9833 
9834   /* Move above the GPR save area.  */
9835   if (frame->num_gp > 0)
9836     {
9837       offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
9838       frame->gp_sp_offset = offset - UNITS_PER_WORD;
9839     }
9840 
9841   /* Find out which FPRs we need to save.  This loop must iterate over
9842      the same space as its companion in mips_for_each_saved_gpr_and_fpr.  */
9843   if (TARGET_HARD_FLOAT)
9844     for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
9845       if (mips_save_reg_p (regno))
9846 	{
9847 	  frame->num_fp += MAX_FPRS_PER_FMT;
9848 	  frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
9849 	}
9850 
9851   /* Move above the FPR save area.  */
9852   if (frame->num_fp > 0)
9853     {
9854       offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
9855       frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
9856     }
9857 
9858   /* Add in space for the interrupt context information.  */
9859   if (cfun->machine->interrupt_handler_p)
9860     {
9861       /* Check HI/LO.  */
9862       if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
9863 	{
9864 	  frame->num_acc++;
9865 	  frame->acc_mask |= (1 << 0);
9866 	}
9867 
9868       /* Check accumulators 1, 2, 3.  */
9869       for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
9870 	if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
9871 	  {
9872 	    frame->num_acc++;
9873 	    frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
9874 	  }
9875 
9876       /* All interrupt context functions need space to preserve STATUS.  */
9877       frame->num_cop0_regs++;
9878 
9879       /* If we don't keep interrupts masked, we need to save EPC.  */
9880       if (!cfun->machine->keep_interrupts_masked_p)
9881 	frame->num_cop0_regs++;
9882     }
9883 
9884   /* Move above the accumulator save area.  */
9885   if (frame->num_acc > 0)
9886     {
9887       /* Each accumulator needs 2 words.  */
9888       offset += frame->num_acc * 2 * UNITS_PER_WORD;
9889       frame->acc_sp_offset = offset - UNITS_PER_WORD;
9890     }
9891 
9892   /* Move above the COP0 register save area.  */
9893   if (frame->num_cop0_regs > 0)
9894     {
9895       offset += frame->num_cop0_regs * UNITS_PER_WORD;
9896       frame->cop0_sp_offset = offset - UNITS_PER_WORD;
9897     }
9898 
9899   /* Move above the callee-allocated varargs save area.  */
9900   offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
9901   frame->arg_pointer_offset = offset;
9902 
9903   /* Move above the callee-allocated area for pretend stack arguments.  */
9904   offset += crtl->args.pretend_args_size;
9905   frame->total_size = offset;
9906 
9907   /* Work out the offsets of the save areas from the top of the frame.  */
9908   if (frame->gp_sp_offset > 0)
9909     frame->gp_save_offset = frame->gp_sp_offset - offset;
9910   if (frame->fp_sp_offset > 0)
9911     frame->fp_save_offset = frame->fp_sp_offset - offset;
9912   if (frame->acc_sp_offset > 0)
9913     frame->acc_save_offset = frame->acc_sp_offset - offset;
9914   if (frame->num_cop0_regs > 0)
9915     frame->cop0_save_offset = frame->cop0_sp_offset - offset;
9916 
9917   /* MIPS16 code offsets the frame pointer by the size of the outgoing
9918      arguments.  This tends to increase the chances of using unextended
9919      instructions for local variables and incoming arguments.  */
9920   if (TARGET_MIPS16)
9921     frame->hard_frame_pointer_offset = frame->args_size;
9922 }
9923 
9924 /* Return the style of GP load sequence that is being used for the
9925    current function.  */
9926 
9927 enum mips_loadgp_style
9928 mips_current_loadgp_style (void)
9929 {
9930   if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
9931     return LOADGP_NONE;
9932 
9933   if (TARGET_RTP_PIC)
9934     return LOADGP_RTP;
9935 
9936   if (TARGET_ABSOLUTE_ABICALLS)
9937     return LOADGP_ABSOLUTE;
9938 
9939   return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
9940 }
9941 
9942 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
9943 
9944 static bool
9945 mips_frame_pointer_required (void)
9946 {
9947   /* If the function contains dynamic stack allocations, we need to
9948      use the frame pointer to access the static parts of the frame.  */
9949   if (cfun->calls_alloca)
9950     return true;
9951 
9952   /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
9953      reload may be unable to compute the address of a local variable,
9954      since there is no way to add a large constant to the stack pointer
9955      without using a second temporary register.  */
9956   if (TARGET_MIPS16)
9957     {
9958       mips_compute_frame_info ();
9959       if (!SMALL_OPERAND (cfun->machine->frame.total_size))
9960 	return true;
9961     }
9962 
9963   return false;
9964 }
9965 
9966 /* Make sure that we're not trying to eliminate to the wrong hard frame
9967    pointer.  */
9968 
9969 static bool
9970 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
9971 {
9972   return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
9973 }
9974 
9975 /* Implement INITIAL_ELIMINATION_OFFSET.  FROM is either the frame pointer
9976    or argument pointer.  TO is either the stack pointer or hard frame
9977    pointer.  */
9978 
9979 HOST_WIDE_INT
9980 mips_initial_elimination_offset (int from, int to)
9981 {
9982   HOST_WIDE_INT offset;
9983 
9984   mips_compute_frame_info ();
9985 
9986   /* Set OFFSET to the offset from the end-of-prologue stack pointer.  */
9987   switch (from)
9988     {
9989     case FRAME_POINTER_REGNUM:
9990       if (FRAME_GROWS_DOWNWARD)
9991 	offset = (cfun->machine->frame.args_size
9992 		  + cfun->machine->frame.cprestore_size
9993 		  + cfun->machine->frame.var_size);
9994       else
9995 	offset = 0;
9996       break;
9997 
9998     case ARG_POINTER_REGNUM:
9999       offset = cfun->machine->frame.arg_pointer_offset;
10000       break;
10001 
10002     default:
10003       gcc_unreachable ();
10004     }
10005 
10006   if (to == HARD_FRAME_POINTER_REGNUM)
10007     offset -= cfun->machine->frame.hard_frame_pointer_offset;
10008 
10009   return offset;
10010 }
10011 
10012 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY.  */
10013 
10014 static void
10015 mips_extra_live_on_entry (bitmap regs)
10016 {
10017   if (TARGET_USE_GOT)
10018     {
10019       /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
10020 	 the global pointer.   */
10021       if (!TARGET_ABSOLUTE_ABICALLS)
10022 	bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
10023 
10024       /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
10025 	 the global pointer.  */
10026       if (TARGET_MIPS16)
10027 	bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
10028 
10029       /* See the comment above load_call<mode> for details.  */
10030       bitmap_set_bit (regs, GOT_VERSION_REGNUM);
10031     }
10032 }
10033 
10034 /* Implement RETURN_ADDR_RTX.  We do not support moving back to a
10035    previous frame.  */
10036 
10037 rtx
10038 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
10039 {
10040   if (count != 0)
10041     return const0_rtx;
10042 
10043   return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
10044 }
10045 
10046 /* Emit code to change the current function's return address to
10047    ADDRESS.  SCRATCH is available as a scratch register, if needed.
10048    ADDRESS and SCRATCH are both word-mode GPRs.  */
10049 
10050 void
10051 mips_set_return_address (rtx address, rtx scratch)
10052 {
10053   rtx slot_address;
10054 
10055   gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
10056   slot_address = mips_add_offset (scratch, stack_pointer_rtx,
10057 				  cfun->machine->frame.gp_sp_offset);
10058   mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
10059 }
10060 
10061 /* Return true if the current function has a cprestore slot.  */
10062 
10063 bool
10064 mips_cfun_has_cprestore_slot_p (void)
10065 {
10066   return (cfun->machine->global_pointer != INVALID_REGNUM
10067 	  && cfun->machine->frame.cprestore_size > 0);
10068 }
10069 
10070 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
10071    cprestore slot.  LOAD_P is true if the caller wants to load from
10072    the cprestore slot; it is false if the caller wants to store to
10073    the slot.  */
10074 
10075 static void
10076 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
10077 				    bool load_p)
10078 {
10079   const struct mips_frame_info *frame;
10080 
10081   frame = &cfun->machine->frame;
10082   /* .cprestore always uses the stack pointer instead of the frame pointer.
10083      We have a free choice for direct stores for non-MIPS16 functions,
10084      and for MIPS16 functions whose cprestore slot is in range of the
10085      stack pointer.  Using the stack pointer would sometimes give more
10086      (early) scheduling freedom, but using the frame pointer would
10087      sometimes give more (late) scheduling freedom.  It's hard to
10088      predict which applies to a given function, so let's keep things
10089      simple.
10090 
10091      Loads must always use the frame pointer in functions that call
10092      alloca, and there's little benefit to using the stack pointer
10093      otherwise.  */
10094   if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
10095     {
10096       *base = hard_frame_pointer_rtx;
10097       *offset = frame->args_size - frame->hard_frame_pointer_offset;
10098     }
10099   else
10100     {
10101       *base = stack_pointer_rtx;
10102       *offset = frame->args_size;
10103     }
10104 }
10105 
10106 /* Return true if X is the load or store address of the cprestore slot;
10107    LOAD_P says which.  */
10108 
10109 bool
10110 mips_cprestore_address_p (rtx x, bool load_p)
10111 {
10112   rtx given_base, required_base;
10113   HOST_WIDE_INT given_offset, required_offset;
10114 
10115   mips_split_plus (x, &given_base, &given_offset);
10116   mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
10117   return given_base == required_base && given_offset == required_offset;
10118 }
10119 
10120 /* Return a MEM rtx for the cprestore slot.  LOAD_P is true if we are
10121    going to load from it, false if we are going to store to it.
10122    Use TEMP as a temporary register if need be.  */
10123 
10124 static rtx
10125 mips_cprestore_slot (rtx temp, bool load_p)
10126 {
10127   rtx base;
10128   HOST_WIDE_INT offset;
10129 
10130   mips_get_cprestore_base_and_offset (&base, &offset, load_p);
10131   return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
10132 }
10133 
10134 /* Emit instructions to save global pointer value GP into cprestore
10135    slot MEM.  OFFSET is the offset that MEM applies to the base register.
10136 
10137    MEM may not be a legitimate address.  If it isn't, TEMP is a
10138    temporary register that can be used, otherwise it is a SCRATCH.  */
10139 
10140 void
10141 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
10142 {
10143   if (TARGET_CPRESTORE_DIRECTIVE)
10144     {
10145       gcc_assert (gp == pic_offset_table_rtx);
10146       emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
10147     }
10148   else
10149     mips_emit_move (mips_cprestore_slot (temp, false), gp);
10150 }
10151 
10152 /* Restore $gp from its save slot, using TEMP as a temporary base register
10153    if need be.  This function is for o32 and o64 abicalls only.
10154 
10155    See mips_must_initialize_gp_p for details about how we manage the
10156    global pointer.  */
10157 
10158 void
10159 mips_restore_gp_from_cprestore_slot (rtx temp)
10160 {
10161   gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
10162 
10163   if (!cfun->machine->must_restore_gp_when_clobbered_p)
10164     {
10165       emit_note (NOTE_INSN_DELETED);
10166       return;
10167     }
10168 
10169   if (TARGET_MIPS16)
10170     {
10171       mips_emit_move (temp, mips_cprestore_slot (temp, true));
10172       mips_emit_move (pic_offset_table_rtx, temp);
10173     }
10174   else
10175     mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
10176   if (!TARGET_EXPLICIT_RELOCS)
10177     emit_insn (gen_blockage ());
10178 }
10179 
10180 /* A function to save or store a register.  The first argument is the
10181    register and the second is the stack slot.  */
10182 typedef void (*mips_save_restore_fn) (rtx, rtx);
10183 
10184 /* Use FN to save or restore register REGNO.  MODE is the register's
10185    mode and OFFSET is the offset of its save slot from the current
10186    stack pointer.  */
10187 
10188 static void
10189 mips_save_restore_reg (enum machine_mode mode, int regno,
10190 		       HOST_WIDE_INT offset, mips_save_restore_fn fn)
10191 {
10192   rtx mem;
10193 
10194   mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
10195 					    offset));
10196   fn (gen_rtx_REG (mode, regno), mem);
10197 }
10198 
10199 /* Call FN for each accumlator that is saved by the current function.
10200    SP_OFFSET is the offset of the current stack pointer from the start
10201    of the frame.  */
10202 
10203 static void
10204 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
10205 {
10206   HOST_WIDE_INT offset;
10207   int regno;
10208 
10209   offset = cfun->machine->frame.acc_sp_offset - sp_offset;
10210   if (BITSET_P (cfun->machine->frame.acc_mask, 0))
10211     {
10212       mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
10213       offset -= UNITS_PER_WORD;
10214       mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
10215       offset -= UNITS_PER_WORD;
10216     }
10217 
10218   for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
10219     if (BITSET_P (cfun->machine->frame.acc_mask,
10220 		  ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
10221       {
10222 	mips_save_restore_reg (word_mode, regno, offset, fn);
10223 	offset -= UNITS_PER_WORD;
10224       }
10225 }
10226 
10227 /* Call FN for each register that is saved by the current function.
10228    SP_OFFSET is the offset of the current stack pointer from the start
10229    of the frame.  */
10230 
10231 static void
10232 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
10233 				 mips_save_restore_fn fn)
10234 {
10235   enum machine_mode fpr_mode;
10236   HOST_WIDE_INT offset;
10237   int regno;
10238 
10239   /* Save registers starting from high to low.  The debuggers prefer at least
10240      the return register be stored at func+4, and also it allows us not to
10241      need a nop in the epilogue if at least one register is reloaded in
10242      addition to return address.  */
10243   offset = cfun->machine->frame.gp_sp_offset - sp_offset;
10244   for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
10245     if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
10246       {
10247 	/* Record the ra offset for use by mips_function_profiler.  */
10248 	if (regno == RETURN_ADDR_REGNUM)
10249 	  cfun->machine->frame.ra_fp_offset = offset + sp_offset;
10250 	mips_save_restore_reg (word_mode, regno, offset, fn);
10251 	offset -= UNITS_PER_WORD;
10252       }
10253 
10254   /* This loop must iterate over the same space as its companion in
10255      mips_compute_frame_info.  */
10256   offset = cfun->machine->frame.fp_sp_offset - sp_offset;
10257   fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
10258   for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
10259        regno >= FP_REG_FIRST;
10260        regno -= MAX_FPRS_PER_FMT)
10261     if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
10262       {
10263 	mips_save_restore_reg (fpr_mode, regno, offset, fn);
10264 	offset -= GET_MODE_SIZE (fpr_mode);
10265       }
10266 }
10267 
10268 /* Return true if a move between register REGNO and its save slot (MEM)
10269    can be done in a single move.  LOAD_P is true if we are loading
10270    from the slot, false if we are storing to it.  */
10271 
10272 static bool
10273 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
10274 {
10275   /* There is a specific MIPS16 instruction for saving $31 to the stack.  */
10276   if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
10277     return false;
10278 
10279   return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
10280 				      GET_MODE (mem), mem, load_p) == NO_REGS;
10281 }
10282 
10283 /* Emit a move from SRC to DEST, given that one of them is a register
10284    save slot and that the other is a register.  TEMP is a temporary
10285    GPR of the same mode that is available if need be.  */
10286 
10287 void
10288 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
10289 {
10290   unsigned int regno;
10291   rtx mem;
10292 
10293   if (REG_P (src))
10294     {
10295       regno = REGNO (src);
10296       mem = dest;
10297     }
10298   else
10299     {
10300       regno = REGNO (dest);
10301       mem = src;
10302     }
10303 
10304   if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
10305     {
10306       /* We don't yet know whether we'll need this instruction or not.
10307 	 Postpone the decision by emitting a ghost move.  This move
10308 	 is specifically not frame-related; only the split version is.  */
10309       if (TARGET_64BIT)
10310 	emit_insn (gen_move_gpdi (dest, src));
10311       else
10312 	emit_insn (gen_move_gpsi (dest, src));
10313       return;
10314     }
10315 
10316   if (regno == HI_REGNUM)
10317     {
10318       if (REG_P (dest))
10319 	{
10320 	  mips_emit_move (temp, src);
10321 	  if (TARGET_64BIT)
10322 	    emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
10323 				      temp, gen_rtx_REG (DImode, LO_REGNUM)));
10324 	  else
10325 	    emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
10326 				      temp, gen_rtx_REG (SImode, LO_REGNUM)));
10327 	}
10328       else
10329 	{
10330 	  if (TARGET_64BIT)
10331 	    emit_insn (gen_mfhidi_ti (temp,
10332 				      gen_rtx_REG (TImode, MD_REG_FIRST)));
10333 	  else
10334 	    emit_insn (gen_mfhisi_di (temp,
10335 				      gen_rtx_REG (DImode, MD_REG_FIRST)));
10336 	  mips_emit_move (dest, temp);
10337 	}
10338     }
10339   else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
10340     mips_emit_move (dest, src);
10341   else
10342     {
10343       gcc_assert (!reg_overlap_mentioned_p (dest, temp));
10344       mips_emit_move (temp, src);
10345       mips_emit_move (dest, temp);
10346     }
10347   if (MEM_P (dest))
10348     mips_set_frame_expr (mips_frame_set (dest, src));
10349 }
10350 
10351 /* If we're generating n32 or n64 abicalls, and the current function
10352    does not use $28 as its global pointer, emit a cplocal directive.
10353    Use pic_offset_table_rtx as the argument to the directive.  */
10354 
10355 static void
10356 mips_output_cplocal (void)
10357 {
10358   if (!TARGET_EXPLICIT_RELOCS
10359       && mips_must_initialize_gp_p ()
10360       && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
10361     output_asm_insn (".cplocal %+", 0);
10362 }
10363 
10364 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE.  */
10365 
10366 static void
10367 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10368 {
10369   const char *fnname;
10370 
10371   /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
10372      floating-point arguments.  */
10373   if (TARGET_MIPS16
10374       && TARGET_HARD_FLOAT_ABI
10375       && crtl->args.info.fp_code != 0)
10376     mips16_build_function_stub ();
10377 
10378   /* Get the function name the same way that toplev.c does before calling
10379      assemble_start_function.  This is needed so that the name used here
10380      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
10381   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10382   mips_start_function_definition (fnname, TARGET_MIPS16);
10383 
10384   /* Output MIPS-specific frame information.  */
10385   if (!flag_inhibit_size_directive)
10386     {
10387       const struct mips_frame_info *frame;
10388 
10389       frame = &cfun->machine->frame;
10390 
10391       /* .frame FRAMEREG, FRAMESIZE, RETREG.  */
10392       fprintf (file,
10393 	       "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
10394 	       "# vars= " HOST_WIDE_INT_PRINT_DEC
10395 	       ", regs= %d/%d"
10396 	       ", args= " HOST_WIDE_INT_PRINT_DEC
10397 	       ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
10398 	       reg_names[frame_pointer_needed
10399 			 ? HARD_FRAME_POINTER_REGNUM
10400 			 : STACK_POINTER_REGNUM],
10401 	       (frame_pointer_needed
10402 		? frame->total_size - frame->hard_frame_pointer_offset
10403 		: frame->total_size),
10404 	       reg_names[RETURN_ADDR_REGNUM],
10405 	       frame->var_size,
10406 	       frame->num_gp, frame->num_fp,
10407 	       frame->args_size,
10408 	       frame->cprestore_size);
10409 
10410       /* .mask MASK, OFFSET.  */
10411       fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10412 	       frame->mask, frame->gp_save_offset);
10413 
10414       /* .fmask MASK, OFFSET.  */
10415       fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10416 	       frame->fmask, frame->fp_save_offset);
10417     }
10418 
10419   /* Handle the initialization of $gp for SVR4 PIC, if applicable.
10420      Also emit the ".set noreorder; .set nomacro" sequence for functions
10421      that need it.  */
10422   if (mips_must_initialize_gp_p ()
10423       && mips_current_loadgp_style () == LOADGP_OLDABI)
10424     {
10425       if (TARGET_MIPS16)
10426 	{
10427 	  /* This is a fixed-form sequence.  The position of the
10428 	     first two instructions is important because of the
10429 	     way _gp_disp is defined.  */
10430 	  output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
10431 	  output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
10432 	  output_asm_insn ("sll\t$2,16", 0);
10433 	  output_asm_insn ("addu\t$2,$3", 0);
10434 	}
10435       else
10436 	{
10437 	  /* .cpload must be in a .set noreorder but not a
10438 	     .set nomacro block.  */
10439 	  mips_push_asm_switch (&mips_noreorder);
10440 	  output_asm_insn (".cpload\t%^", 0);
10441 	  if (!cfun->machine->all_noreorder_p)
10442 	    mips_pop_asm_switch (&mips_noreorder);
10443 	  else
10444 	    mips_push_asm_switch (&mips_nomacro);
10445 	}
10446     }
10447   else if (cfun->machine->all_noreorder_p)
10448     {
10449       mips_push_asm_switch (&mips_noreorder);
10450       mips_push_asm_switch (&mips_nomacro);
10451     }
10452 
10453   /* Tell the assembler which register we're using as the global
10454      pointer.  This is needed for thunks, since they can use either
10455      explicit relocs or assembler macros.  */
10456   mips_output_cplocal ();
10457 }
10458 
10459 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE.  */
10460 
10461 static void
10462 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10463 			       HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10464 {
10465   const char *fnname;
10466 
10467   /* Reinstate the normal $gp.  */
10468   SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
10469   mips_output_cplocal ();
10470 
10471   if (cfun->machine->all_noreorder_p)
10472     {
10473       mips_pop_asm_switch (&mips_nomacro);
10474       mips_pop_asm_switch (&mips_noreorder);
10475     }
10476 
10477   /* Get the function name the same way that toplev.c does before calling
10478      assemble_start_function.  This is needed so that the name used here
10479      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
10480   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10481   mips_end_function_definition (fnname);
10482 }
10483 
10484 /* Emit an optimisation barrier for accesses to the current frame.  */
10485 
10486 static void
10487 mips_frame_barrier (void)
10488 {
10489   emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
10490 }
10491 
10492 /* Save register REG to MEM.  Make the instruction frame-related.  */
10493 
10494 static void
10495 mips_save_reg (rtx reg, rtx mem)
10496 {
10497   if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
10498     {
10499       rtx x1, x2;
10500 
10501       mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
10502 
10503       x1 = mips_frame_set (mips_subword (mem, false),
10504 			   mips_subword (reg, false));
10505       x2 = mips_frame_set (mips_subword (mem, true),
10506 			   mips_subword (reg, true));
10507       mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
10508     }
10509   else
10510     mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
10511 }
10512 
10513 /* The __gnu_local_gp symbol.  */
10514 
10515 static GTY(()) rtx mips_gnu_local_gp;
10516 
10517 /* If we're generating n32 or n64 abicalls, emit instructions
10518    to set up the global pointer.  */
10519 
10520 static void
10521 mips_emit_loadgp (void)
10522 {
10523   rtx addr, offset, incoming_address, base, index, pic_reg;
10524 
10525   pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10526   switch (mips_current_loadgp_style ())
10527     {
10528     case LOADGP_ABSOLUTE:
10529       if (mips_gnu_local_gp == NULL)
10530 	{
10531 	  mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
10532 	  SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
10533 	}
10534       emit_insn (PMODE_INSN (gen_loadgp_absolute,
10535 			     (pic_reg, mips_gnu_local_gp)));
10536       break;
10537 
10538     case LOADGP_OLDABI:
10539       /* Added by mips_output_function_prologue.  */
10540       break;
10541 
10542     case LOADGP_NEWABI:
10543       addr = XEXP (DECL_RTL (current_function_decl), 0);
10544       offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
10545       incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
10546       emit_insn (PMODE_INSN (gen_loadgp_newabi,
10547 			     (pic_reg, offset, incoming_address)));
10548       break;
10549 
10550     case LOADGP_RTP:
10551       base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
10552       index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
10553       emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
10554       break;
10555 
10556     default:
10557       return;
10558     }
10559 
10560   if (TARGET_MIPS16)
10561     emit_insn (PMODE_INSN (gen_copygp_mips16,
10562 			   (pic_offset_table_rtx, pic_reg)));
10563 
10564   /* Emit a blockage if there are implicit uses of the GP register.
10565      This includes profiled functions, because FUNCTION_PROFILE uses
10566      a jal macro.  */
10567   if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
10568     emit_insn (gen_loadgp_blockage ());
10569 }
10570 
10571 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
10572 
10573 #if PROBE_INTERVAL > 32768
10574 #error Cannot use indexed addressing mode for stack probing
10575 #endif
10576 
10577 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
10578    inclusive.  These are offsets from the current stack pointer.  */
10579 
10580 static void
10581 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
10582 {
10583   if (TARGET_MIPS16)
10584     sorry ("-fstack-check=specific not implemented for MIPS16");
10585 
10586   /* See if we have a constant small number of probes to generate.  If so,
10587      that's the easy case.  */
10588   if (first + size <= 32768)
10589     {
10590       HOST_WIDE_INT i;
10591 
10592       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
10593 	 it exceeds SIZE.  If only one probe is needed, this will not
10594 	 generate any code.  Then probe at FIRST + SIZE.  */
10595       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
10596         emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10597 					 -(first + i)));
10598 
10599       emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10600 				       -(first + size)));
10601     }
10602 
10603   /* Otherwise, do the same as above, but in a loop.  Note that we must be
10604      extra careful with variables wrapping around because we might be at
10605      the very top (or the very bottom) of the address space and we have
10606      to be able to handle this case properly; in particular, we use an
10607      equality test for the loop condition.  */
10608   else
10609     {
10610       HOST_WIDE_INT rounded_size;
10611       rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
10612       rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
10613 
10614       /* Sanity check for the addressing mode we're going to use.  */
10615       gcc_assert (first <= 32768);
10616 
10617 
10618       /* Step 1: round SIZE to the previous multiple of the interval.  */
10619 
10620       rounded_size = size & -PROBE_INTERVAL;
10621 
10622 
10623       /* Step 2: compute initial and final value of the loop counter.  */
10624 
10625       /* TEST_ADDR = SP + FIRST.  */
10626       emit_insn (gen_rtx_SET (VOIDmode, r3,
10627 			      plus_constant (Pmode, stack_pointer_rtx,
10628 					     -first)));
10629 
10630       /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE.  */
10631       if (rounded_size > 32768)
10632 	{
10633           emit_move_insn (r12, GEN_INT (rounded_size));
10634 	  emit_insn (gen_rtx_SET (VOIDmode, r12,
10635 			          gen_rtx_MINUS (Pmode, r3, r12)));
10636 	}
10637       else
10638 	emit_insn (gen_rtx_SET (VOIDmode, r12,
10639 			        plus_constant (Pmode, r3, -rounded_size)));
10640 
10641 
10642       /* Step 3: the loop
10643 
10644 	while (TEST_ADDR != LAST_ADDR)
10645 	  {
10646 	    TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
10647 	    probe at TEST_ADDR
10648 	  }
10649 
10650 	probes at FIRST + N * PROBE_INTERVAL for values of N from 1
10651 	until it is equal to ROUNDED_SIZE.  */
10652 
10653       emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
10654 
10655 
10656       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
10657 	 that SIZE is equal to ROUNDED_SIZE.  */
10658 
10659       if (size != rounded_size)
10660 	emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
10661     }
10662 
10663   /* Make sure nothing is scheduled before we are done.  */
10664   emit_insn (gen_blockage ());
10665 }
10666 
10667 /* Probe a range of stack addresses from REG1 to REG2 inclusive.  These are
10668    absolute addresses.  */
10669 
10670 const char *
10671 mips_output_probe_stack_range (rtx reg1, rtx reg2)
10672 {
10673   static int labelno = 0;
10674   char loop_lab[32], end_lab[32], tmp[64];
10675   rtx xops[2];
10676 
10677   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
10678   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
10679 
10680   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
10681 
10682   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
10683   xops[0] = reg1;
10684   xops[1] = reg2;
10685   strcpy (tmp, "%(%<beq\t%0,%1,");
10686   output_asm_insn (strcat (tmp, &end_lab[1]), xops);
10687 
10688   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
10689   xops[1] = GEN_INT (-PROBE_INTERVAL);
10690   if (TARGET_64BIT && TARGET_LONG64)
10691     output_asm_insn ("daddiu\t%0,%0,%1", xops);
10692   else
10693     output_asm_insn ("addiu\t%0,%0,%1", xops);
10694 
10695   /* Probe at TEST_ADDR and branch.  */
10696   fprintf (asm_out_file, "\tb\t");
10697   assemble_name_raw (asm_out_file, loop_lab);
10698   fputc ('\n', asm_out_file);
10699   if (TARGET_64BIT)
10700     output_asm_insn ("sd\t$0,0(%0)%)", xops);
10701   else
10702     output_asm_insn ("sw\t$0,0(%0)%)", xops);
10703 
10704   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
10705 
10706   return "";
10707 }
10708 
10709 /* A for_each_rtx callback.  Stop the search if *X is a kernel register.  */
10710 
10711 static int
10712 mips_kernel_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
10713 {
10714   return REG_P (*x) && KERNEL_REG_P (REGNO (*x));
10715 }
10716 
10717 /* Expand the "prologue" pattern.  */
10718 
10719 void
10720 mips_expand_prologue (void)
10721 {
10722   const struct mips_frame_info *frame;
10723   HOST_WIDE_INT size;
10724   unsigned int nargs;
10725   rtx insn;
10726 
10727   if (cfun->machine->global_pointer != INVALID_REGNUM)
10728     {
10729       /* Check whether an insn uses pic_offset_table_rtx, either explicitly
10730 	 or implicitly.  If so, we can commit to using a global pointer
10731 	 straight away, otherwise we need to defer the decision.  */
10732       if (mips_cfun_has_inflexible_gp_ref_p ()
10733 	  || mips_cfun_has_flexible_gp_ref_p ())
10734 	{
10735 	  cfun->machine->must_initialize_gp_p = true;
10736 	  cfun->machine->must_restore_gp_when_clobbered_p = true;
10737 	}
10738 
10739       SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
10740     }
10741 
10742   frame = &cfun->machine->frame;
10743   size = frame->total_size;
10744 
10745   if (flag_stack_usage_info)
10746     current_function_static_stack_size = size;
10747 
10748   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && size)
10749     mips_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
10750 
10751   /* Save the registers.  Allocate up to MIPS_MAX_FIRST_STACK_STEP
10752      bytes beforehand; this is enough to cover the register save area
10753      without going out of range.  */
10754   if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
10755       || frame->num_cop0_regs > 0)
10756     {
10757       HOST_WIDE_INT step1;
10758 
10759       step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
10760       if (GENERATE_MIPS16E_SAVE_RESTORE)
10761  	{
10762  	  HOST_WIDE_INT offset;
10763  	  unsigned int mask, regno;
10764 
10765 	  /* Try to merge argument stores into the save instruction.  */
10766 	  nargs = mips16e_collect_argument_saves ();
10767 
10768 	  /* Build the save instruction.  */
10769 	  mask = frame->mask;
10770 	  insn = mips16e_build_save_restore (false, &mask, &offset,
10771 					     nargs, step1);
10772 	  RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10773 	  mips_frame_barrier ();
10774  	  size -= step1;
10775 
10776  	  /* Check if we need to save other registers.  */
10777  	  for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
10778  	    if (BITSET_P (mask, regno - GP_REG_FIRST))
10779  	      {
10780 		offset -= UNITS_PER_WORD;
10781 		mips_save_restore_reg (word_mode, regno,
10782 				       offset, mips_save_reg);
10783  	      }
10784  	}
10785       else
10786  	{
10787 	  if (cfun->machine->interrupt_handler_p)
10788 	    {
10789 	      HOST_WIDE_INT offset;
10790 	      rtx mem;
10791 
10792 	      /* If this interrupt is using a shadow register set, we need to
10793 		 get the stack pointer from the previous register set.  */
10794 	      if (cfun->machine->use_shadow_register_set_p)
10795 		emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
10796 					    stack_pointer_rtx));
10797 
10798 	      if (!cfun->machine->keep_interrupts_masked_p)
10799 		{
10800 		  /* Move from COP0 Cause to K0.  */
10801 		  emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
10802 					    gen_rtx_REG (SImode,
10803 							 COP0_CAUSE_REG_NUM)));
10804 		  /* Move from COP0 EPC to K1.  */
10805 		  emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
10806 					    gen_rtx_REG (SImode,
10807 							 COP0_EPC_REG_NUM)));
10808 		}
10809 
10810 	      /* Allocate the first part of the frame.  */
10811 	      insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
10812 				    GEN_INT (-step1));
10813 	      RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10814 	      mips_frame_barrier ();
10815 	      size -= step1;
10816 
10817 	      /* Start at the uppermost location for saving.  */
10818 	      offset = frame->cop0_sp_offset - size;
10819 	      if (!cfun->machine->keep_interrupts_masked_p)
10820 		{
10821 		  /* Push EPC into its stack slot.  */
10822 		  mem = gen_frame_mem (word_mode,
10823 				       plus_constant (Pmode, stack_pointer_rtx,
10824 						      offset));
10825 		  mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
10826 		  offset -= UNITS_PER_WORD;
10827 		}
10828 
10829 	      /* Move from COP0 Status to K1.  */
10830 	      emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
10831 					gen_rtx_REG (SImode,
10832 						     COP0_STATUS_REG_NUM)));
10833 
10834 	      /* Right justify the RIPL in k0.  */
10835 	      if (!cfun->machine->keep_interrupts_masked_p)
10836 		emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
10837 					gen_rtx_REG (SImode, K0_REG_NUM),
10838 					GEN_INT (CAUSE_IPL)));
10839 
10840 	      /* Push Status into its stack slot.  */
10841 	      mem = gen_frame_mem (word_mode,
10842 				   plus_constant (Pmode, stack_pointer_rtx,
10843 						  offset));
10844 	      mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
10845 	      offset -= UNITS_PER_WORD;
10846 
10847 	      /* Insert the RIPL into our copy of SR (k1) as the new IPL.  */
10848 	      if (!cfun->machine->keep_interrupts_masked_p)
10849 		emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10850 				       GEN_INT (6),
10851 				       GEN_INT (SR_IPL),
10852 				       gen_rtx_REG (SImode, K0_REG_NUM)));
10853 
10854 	      if (!cfun->machine->keep_interrupts_masked_p)
10855 		/* Enable interrupts by clearing the KSU ERL and EXL bits.
10856 		   IE is already the correct value, so we don't have to do
10857 		   anything explicit.  */
10858 		emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10859 				       GEN_INT (4),
10860 				       GEN_INT (SR_EXL),
10861 				       gen_rtx_REG (SImode, GP_REG_FIRST)));
10862 	      else
10863 		/* Disable interrupts by clearing the KSU, ERL, EXL,
10864 		   and IE bits.  */
10865 		emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10866 				       GEN_INT (5),
10867 				       GEN_INT (SR_IE),
10868 				       gen_rtx_REG (SImode, GP_REG_FIRST)));
10869 	    }
10870 	  else
10871 	    {
10872 	      insn = gen_add3_insn (stack_pointer_rtx,
10873 				    stack_pointer_rtx,
10874 				    GEN_INT (-step1));
10875 	      RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10876 	      mips_frame_barrier ();
10877 	      size -= step1;
10878 	    }
10879 	  mips_for_each_saved_acc (size, mips_save_reg);
10880 	  mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
10881 	}
10882     }
10883 
10884   /* Allocate the rest of the frame.  */
10885   if (size > 0)
10886     {
10887       if (SMALL_OPERAND (-size))
10888 	RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
10889 						       stack_pointer_rtx,
10890 						       GEN_INT (-size)))) = 1;
10891       else
10892 	{
10893 	  mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
10894 	  if (TARGET_MIPS16)
10895 	    {
10896 	      /* There are no instructions to add or subtract registers
10897 		 from the stack pointer, so use the frame pointer as a
10898 		 temporary.  We should always be using a frame pointer
10899 		 in this case anyway.  */
10900 	      gcc_assert (frame_pointer_needed);
10901 	      mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10902 	      emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
10903 					hard_frame_pointer_rtx,
10904 					MIPS_PROLOGUE_TEMP (Pmode)));
10905 	      mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
10906 	    }
10907 	  else
10908 	    emit_insn (gen_sub3_insn (stack_pointer_rtx,
10909 				      stack_pointer_rtx,
10910 				      MIPS_PROLOGUE_TEMP (Pmode)));
10911 
10912 	  /* Describe the combined effect of the previous instructions.  */
10913 	  mips_set_frame_expr
10914 	    (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10915 			  plus_constant (Pmode, stack_pointer_rtx, -size)));
10916 	}
10917       mips_frame_barrier ();
10918     }
10919 
10920   /* Set up the frame pointer, if we're using one.  */
10921   if (frame_pointer_needed)
10922     {
10923       HOST_WIDE_INT offset;
10924 
10925       offset = frame->hard_frame_pointer_offset;
10926       if (offset == 0)
10927 	{
10928 	  insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10929 	  RTX_FRAME_RELATED_P (insn) = 1;
10930 	}
10931       else if (SMALL_OPERAND (offset))
10932 	{
10933 	  insn = gen_add3_insn (hard_frame_pointer_rtx,
10934 				stack_pointer_rtx, GEN_INT (offset));
10935 	  RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10936 	}
10937       else
10938 	{
10939 	  mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
10940 	  mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10941 	  emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
10942 				    hard_frame_pointer_rtx,
10943 				    MIPS_PROLOGUE_TEMP (Pmode)));
10944 	  mips_set_frame_expr
10945 	    (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
10946 			  plus_constant (Pmode, stack_pointer_rtx, offset)));
10947 	}
10948     }
10949 
10950   mips_emit_loadgp ();
10951 
10952   /* Initialize the $gp save slot.  */
10953   if (mips_cfun_has_cprestore_slot_p ())
10954     {
10955       rtx base, mem, gp, temp;
10956       HOST_WIDE_INT offset;
10957 
10958       mips_get_cprestore_base_and_offset (&base, &offset, false);
10959       mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
10960       gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10961       temp = (SMALL_OPERAND (offset)
10962 	      ? gen_rtx_SCRATCH (Pmode)
10963 	      : MIPS_PROLOGUE_TEMP (Pmode));
10964       emit_insn (PMODE_INSN (gen_potential_cprestore,
10965 			     (mem, GEN_INT (offset), gp, temp)));
10966 
10967       mips_get_cprestore_base_and_offset (&base, &offset, true);
10968       mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
10969       emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
10970     }
10971 
10972   /* We need to search back to the last use of K0 or K1.  */
10973   if (cfun->machine->interrupt_handler_p)
10974     {
10975       for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
10976 	if (INSN_P (insn)
10977 	    && for_each_rtx (&PATTERN (insn), mips_kernel_reg_p, NULL))
10978 	  break;
10979       /* Emit a move from K1 to COP0 Status after insn.  */
10980       gcc_assert (insn != NULL_RTX);
10981       emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
10982 				      gen_rtx_REG (SImode, K1_REG_NUM)),
10983 		       insn);
10984     }
10985 
10986   /* If we are profiling, make sure no instructions are scheduled before
10987      the call to mcount.  */
10988   if (crtl->profile)
10989     emit_insn (gen_blockage ());
10990 }
10991 
10992 /* Attach all pending register saves to the previous instruction.
10993    Return that instruction.  */
10994 
10995 static rtx
10996 mips_epilogue_emit_cfa_restores (void)
10997 {
10998   rtx insn;
10999 
11000   insn = get_last_insn ();
11001   gcc_assert (insn && !REG_NOTES (insn));
11002   if (mips_epilogue.cfa_restores)
11003     {
11004       RTX_FRAME_RELATED_P (insn) = 1;
11005       REG_NOTES (insn) = mips_epilogue.cfa_restores;
11006       mips_epilogue.cfa_restores = 0;
11007     }
11008   return insn;
11009 }
11010 
11011 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
11012    now at REG + OFFSET.  */
11013 
11014 static void
11015 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
11016 {
11017   rtx insn;
11018 
11019   insn = mips_epilogue_emit_cfa_restores ();
11020   if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
11021     {
11022       RTX_FRAME_RELATED_P (insn) = 1;
11023       REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
11024 					 plus_constant (Pmode, reg, offset),
11025 					 REG_NOTES (insn));
11026       mips_epilogue.cfa_reg = reg;
11027       mips_epilogue.cfa_offset = offset;
11028     }
11029 }
11030 
11031 /* Emit instructions to restore register REG from slot MEM.  Also update
11032    the cfa_restores list.  */
11033 
11034 static void
11035 mips_restore_reg (rtx reg, rtx mem)
11036 {
11037   /* There's no MIPS16 instruction to load $31 directly.  Load into
11038      $7 instead and adjust the return insn appropriately.  */
11039   if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
11040     reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
11041   else if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
11042     {
11043       mips_add_cfa_restore (mips_subword (reg, true));
11044       mips_add_cfa_restore (mips_subword (reg, false));
11045     }
11046   else
11047     mips_add_cfa_restore (reg);
11048 
11049   mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
11050   if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
11051     /* The CFA is currently defined in terms of the register whose
11052        value we have just restored.  Redefine the CFA in terms of
11053        the stack pointer.  */
11054     mips_epilogue_set_cfa (stack_pointer_rtx,
11055 			   mips_epilogue.cfa_restore_sp_offset);
11056 }
11057 
11058 /* Emit code to set the stack pointer to BASE + OFFSET, given that
11059    BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
11060    BASE, if not the stack pointer, is available as a temporary.  */
11061 
11062 static void
11063 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
11064 {
11065   if (base == stack_pointer_rtx && offset == const0_rtx)
11066     return;
11067 
11068   mips_frame_barrier ();
11069   if (offset == const0_rtx)
11070     {
11071       emit_move_insn (stack_pointer_rtx, base);
11072       mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11073     }
11074   else if (TARGET_MIPS16 && base != stack_pointer_rtx)
11075     {
11076       emit_insn (gen_add3_insn (base, base, offset));
11077       mips_epilogue_set_cfa (base, new_frame_size);
11078       emit_move_insn (stack_pointer_rtx, base);
11079     }
11080   else
11081     {
11082       emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
11083       mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11084     }
11085 }
11086 
11087 /* Emit any instructions needed before a return.  */
11088 
11089 void
11090 mips_expand_before_return (void)
11091 {
11092   /* When using a call-clobbered gp, we start out with unified call
11093      insns that include instructions to restore the gp.  We then split
11094      these unified calls after reload.  These split calls explicitly
11095      clobber gp, so there is no need to define
11096      PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
11097 
11098      For consistency, we should also insert an explicit clobber of $28
11099      before return insns, so that the post-reload optimizers know that
11100      the register is not live on exit.  */
11101   if (TARGET_CALL_CLOBBERED_GP)
11102     emit_clobber (pic_offset_table_rtx);
11103 }
11104 
11105 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
11106    says which.  */
11107 
11108 void
11109 mips_expand_epilogue (bool sibcall_p)
11110 {
11111   const struct mips_frame_info *frame;
11112   HOST_WIDE_INT step1, step2;
11113   rtx base, adjust, insn;
11114 
11115   if (!sibcall_p && mips_can_use_return_insn ())
11116     {
11117       emit_jump_insn (gen_return ());
11118       return;
11119     }
11120 
11121   /* In MIPS16 mode, if the return value should go into a floating-point
11122      register, we need to call a helper routine to copy it over.  */
11123   if (mips16_cfun_returns_in_fpr_p ())
11124     mips16_copy_fpr_return_value ();
11125 
11126   /* Split the frame into two.  STEP1 is the amount of stack we should
11127      deallocate before restoring the registers.  STEP2 is the amount we
11128      should deallocate afterwards.
11129 
11130      Start off by assuming that no registers need to be restored.  */
11131   frame = &cfun->machine->frame;
11132   step1 = frame->total_size;
11133   step2 = 0;
11134 
11135   /* Work out which register holds the frame address.  */
11136   if (!frame_pointer_needed)
11137     base = stack_pointer_rtx;
11138   else
11139     {
11140       base = hard_frame_pointer_rtx;
11141       step1 -= frame->hard_frame_pointer_offset;
11142     }
11143   mips_epilogue.cfa_reg = base;
11144   mips_epilogue.cfa_offset = step1;
11145   mips_epilogue.cfa_restores = NULL_RTX;
11146 
11147   /* If we need to restore registers, deallocate as much stack as
11148      possible in the second step without going out of range.  */
11149   if ((frame->mask | frame->fmask | frame->acc_mask) != 0
11150       || frame->num_cop0_regs > 0)
11151     {
11152       step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
11153       step1 -= step2;
11154     }
11155 
11156   /* Get an rtx for STEP1 that we can add to BASE.  */
11157   adjust = GEN_INT (step1);
11158   if (!SMALL_OPERAND (step1))
11159     {
11160       mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
11161       adjust = MIPS_EPILOGUE_TEMP (Pmode);
11162     }
11163   mips_deallocate_stack (base, adjust, step2);
11164 
11165   /* If we're using addressing macros, $gp is implicitly used by all
11166      SYMBOL_REFs.  We must emit a blockage insn before restoring $gp
11167      from the stack.  */
11168   if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
11169     emit_insn (gen_blockage ());
11170 
11171   mips_epilogue.cfa_restore_sp_offset = step2;
11172   if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
11173     {
11174       unsigned int regno, mask;
11175       HOST_WIDE_INT offset;
11176       rtx restore;
11177 
11178       /* Generate the restore instruction.  */
11179       mask = frame->mask;
11180       restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
11181 
11182       /* Restore any other registers manually.  */
11183       for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11184  	if (BITSET_P (mask, regno - GP_REG_FIRST))
11185  	  {
11186  	    offset -= UNITS_PER_WORD;
11187  	    mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
11188  	  }
11189 
11190       /* Restore the remaining registers and deallocate the final bit
11191 	 of the frame.  */
11192       mips_frame_barrier ();
11193       emit_insn (restore);
11194       mips_epilogue_set_cfa (stack_pointer_rtx, 0);
11195     }
11196   else
11197     {
11198       /* Restore the registers.  */
11199       mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
11200       mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
11201 				       mips_restore_reg);
11202 
11203       if (cfun->machine->interrupt_handler_p)
11204 	{
11205 	  HOST_WIDE_INT offset;
11206 	  rtx mem;
11207 
11208 	  offset = frame->cop0_sp_offset - (frame->total_size - step2);
11209 	  if (!cfun->machine->keep_interrupts_masked_p)
11210 	    {
11211 	      /* Restore the original EPC.  */
11212 	      mem = gen_frame_mem (word_mode,
11213 				   plus_constant (Pmode, stack_pointer_rtx,
11214 						  offset));
11215 	      mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11216 	      offset -= UNITS_PER_WORD;
11217 
11218 	      /* Move to COP0 EPC.  */
11219 	      emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
11220 					gen_rtx_REG (SImode, K0_REG_NUM)));
11221 	    }
11222 
11223 	  /* Restore the original Status.  */
11224 	  mem = gen_frame_mem (word_mode,
11225 			       plus_constant (Pmode, stack_pointer_rtx,
11226 					      offset));
11227 	  mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11228 	  offset -= UNITS_PER_WORD;
11229 
11230 	  /* If we don't use shoadow register set, we need to update SP.  */
11231 	  if (!cfun->machine->use_shadow_register_set_p)
11232 	    mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11233 	  else
11234 	    /* The choice of position is somewhat arbitrary in this case.  */
11235 	    mips_epilogue_emit_cfa_restores ();
11236 
11237 	  /* Move to COP0 Status.  */
11238 	  emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11239 				    gen_rtx_REG (SImode, K0_REG_NUM)));
11240 	}
11241       else
11242 	/* Deallocate the final bit of the frame.  */
11243 	mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11244     }
11245   gcc_assert (!mips_epilogue.cfa_restores);
11246 
11247   /* Add in the __builtin_eh_return stack adjustment.  We need to
11248      use a temporary in MIPS16 code.  */
11249   if (crtl->calls_eh_return)
11250     {
11251       if (TARGET_MIPS16)
11252 	{
11253 	  mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
11254 	  emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
11255 				    MIPS_EPILOGUE_TEMP (Pmode),
11256 				    EH_RETURN_STACKADJ_RTX));
11257 	  mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
11258 	}
11259       else
11260 	emit_insn (gen_add3_insn (stack_pointer_rtx,
11261 				  stack_pointer_rtx,
11262 				  EH_RETURN_STACKADJ_RTX));
11263     }
11264 
11265   if (!sibcall_p)
11266     {
11267       mips_expand_before_return ();
11268       if (cfun->machine->interrupt_handler_p)
11269 	{
11270 	  /* Interrupt handlers generate eret or deret.  */
11271 	  if (cfun->machine->use_debug_exception_return_p)
11272 	    emit_jump_insn (gen_mips_deret ());
11273 	  else
11274 	    emit_jump_insn (gen_mips_eret ());
11275 	}
11276       else
11277 	{
11278 	  rtx pat;
11279 
11280 	  /* When generating MIPS16 code, the normal
11281 	     mips_for_each_saved_gpr_and_fpr path will restore the return
11282 	     address into $7 rather than $31.  */
11283 	  if (TARGET_MIPS16
11284 	      && !GENERATE_MIPS16E_SAVE_RESTORE
11285 	      && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
11286 	    {
11287 	      /* simple_returns cannot rely on values that are only available
11288 		 on paths through the epilogue (because return paths that do
11289 		 not pass through the epilogue may nevertheless reuse a
11290 		 simple_return that occurs at the end of the epilogue).
11291 		 Use a normal return here instead.  */
11292 	      rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
11293 	      pat = gen_return_internal (reg);
11294 	    }
11295 	  else
11296 	    {
11297 	      rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
11298 	      pat = gen_simple_return_internal (reg);
11299 	    }
11300 	  emit_jump_insn (pat);
11301 	}
11302     }
11303 
11304   /* Search from the beginning to the first use of K0 or K1.  */
11305   if (cfun->machine->interrupt_handler_p
11306       && !cfun->machine->keep_interrupts_masked_p)
11307     {
11308       for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
11309 	if (INSN_P (insn)
11310 	    && for_each_rtx (&PATTERN(insn), mips_kernel_reg_p, NULL))
11311 	  break;
11312       gcc_assert (insn != NULL_RTX);
11313       /* Insert disable interrupts before the first use of K0 or K1.  */
11314       emit_insn_before (gen_mips_di (), insn);
11315       emit_insn_before (gen_mips_ehb (), insn);
11316     }
11317 }
11318 
11319 /* Return nonzero if this function is known to have a null epilogue.
11320    This allows the optimizer to omit jumps to jumps if no stack
11321    was created.  */
11322 
11323 bool
11324 mips_can_use_return_insn (void)
11325 {
11326   /* Interrupt handlers need to go through the epilogue.  */
11327   if (cfun->machine->interrupt_handler_p)
11328     return false;
11329 
11330   if (!reload_completed)
11331     return false;
11332 
11333   if (crtl->profile)
11334     return false;
11335 
11336   /* In MIPS16 mode, a function that returns a floating-point value
11337      needs to arrange to copy the return value into the floating-point
11338      registers.  */
11339   if (mips16_cfun_returns_in_fpr_p ())
11340     return false;
11341 
11342   return cfun->machine->frame.total_size == 0;
11343 }
11344 
11345 /* Return true if register REGNO can store a value of mode MODE.
11346    The result of this function is cached in mips_hard_regno_mode_ok.  */
11347 
11348 static bool
11349 mips_hard_regno_mode_ok_p (unsigned int regno, enum machine_mode mode)
11350 {
11351   unsigned int size;
11352   enum mode_class mclass;
11353 
11354   if (mode == CCV2mode)
11355     return (ISA_HAS_8CC
11356 	    && ST_REG_P (regno)
11357 	    && (regno - ST_REG_FIRST) % 2 == 0);
11358 
11359   if (mode == CCV4mode)
11360     return (ISA_HAS_8CC
11361 	    && ST_REG_P (regno)
11362 	    && (regno - ST_REG_FIRST) % 4 == 0);
11363 
11364   if (mode == CCmode)
11365     return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
11366 
11367   size = GET_MODE_SIZE (mode);
11368   mclass = GET_MODE_CLASS (mode);
11369 
11370   if (GP_REG_P (regno))
11371     return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
11372 
11373   if (FP_REG_P (regno)
11374       && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
11375 	  || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
11376     {
11377       /* Allow 64-bit vector modes for Loongson-2E/2F.  */
11378       if (TARGET_LOONGSON_VECTORS
11379 	  && (mode == V2SImode
11380 	      || mode == V4HImode
11381 	      || mode == V8QImode
11382 	      || mode == DImode))
11383 	return true;
11384 
11385       if (mclass == MODE_FLOAT
11386 	  || mclass == MODE_COMPLEX_FLOAT
11387 	  || mclass == MODE_VECTOR_FLOAT)
11388 	return size <= UNITS_PER_FPVALUE;
11389 
11390       /* Allow integer modes that fit into a single register.  We need
11391 	 to put integers into FPRs when using instructions like CVT
11392 	 and TRUNC.  There's no point allowing sizes smaller than a word,
11393 	 because the FPU has no appropriate load/store instructions.  */
11394       if (mclass == MODE_INT)
11395 	return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
11396     }
11397 
11398   if (ACC_REG_P (regno)
11399       && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
11400     {
11401       if (MD_REG_P (regno))
11402 	{
11403 	  /* After a multiplication or division, clobbering HI makes
11404 	     the value of LO unpredictable, and vice versa.  This means
11405 	     that, for all interesting cases, HI and LO are effectively
11406 	     a single register.
11407 
11408 	     We model this by requiring that any value that uses HI
11409 	     also uses LO.  */
11410 	  if (size <= UNITS_PER_WORD * 2)
11411 	    return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
11412 	}
11413       else
11414 	{
11415 	  /* DSP accumulators do not have the same restrictions as
11416 	     HI and LO, so we can treat them as normal doubleword
11417 	     registers.  */
11418 	  if (size <= UNITS_PER_WORD)
11419 	    return true;
11420 
11421 	  if (size <= UNITS_PER_WORD * 2
11422 	      && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
11423 	    return true;
11424 	}
11425     }
11426 
11427   if (ALL_COP_REG_P (regno))
11428     return mclass == MODE_INT && size <= UNITS_PER_WORD;
11429 
11430   if (regno == GOT_VERSION_REGNUM)
11431     return mode == SImode;
11432 
11433   return false;
11434 }
11435 
11436 /* Implement HARD_REGNO_NREGS.  */
11437 
11438 unsigned int
11439 mips_hard_regno_nregs (int regno, enum machine_mode mode)
11440 {
11441   if (ST_REG_P (regno))
11442     /* The size of FP status registers is always 4, because they only hold
11443        CCmode values, and CCmode is always considered to be 4 bytes wide.  */
11444     return (GET_MODE_SIZE (mode) + 3) / 4;
11445 
11446   if (FP_REG_P (regno))
11447     return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
11448 
11449   /* All other registers are word-sized.  */
11450   return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
11451 }
11452 
11453 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
11454    in mips_hard_regno_nregs.  */
11455 
11456 int
11457 mips_class_max_nregs (enum reg_class rclass, enum machine_mode mode)
11458 {
11459   int size;
11460   HARD_REG_SET left;
11461 
11462   size = 0x8000;
11463   COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
11464   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
11465     {
11466       if (HARD_REGNO_MODE_OK (ST_REG_FIRST, mode))
11467 	size = MIN (size, 4);
11468       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
11469     }
11470   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
11471     {
11472       if (HARD_REGNO_MODE_OK (FP_REG_FIRST, mode))
11473 	size = MIN (size, UNITS_PER_FPREG);
11474       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
11475     }
11476   if (!hard_reg_set_empty_p (left))
11477     size = MIN (size, UNITS_PER_WORD);
11478   return (GET_MODE_SIZE (mode) + size - 1) / size;
11479 }
11480 
11481 /* Implement CANNOT_CHANGE_MODE_CLASS.  */
11482 
11483 bool
11484 mips_cannot_change_mode_class (enum machine_mode from,
11485 			       enum machine_mode to,
11486 			       enum reg_class rclass)
11487 {
11488   /* Allow conversions between different Loongson integer vectors,
11489      and between those vectors and DImode.  */
11490   if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
11491       && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
11492     return false;
11493 
11494   /* Otherwise, there are several problems with changing the modes of
11495      values in floating-point registers:
11496 
11497      - When a multi-word value is stored in paired floating-point
11498        registers, the first register always holds the low word.  We
11499        therefore can't allow FPRs to change between single-word and
11500        multi-word modes on big-endian targets.
11501 
11502      - GCC assumes that each word of a multiword register can be
11503        accessed individually using SUBREGs.  This is not true for
11504        floating-point registers if they are bigger than a word.
11505 
11506      - Loading a 32-bit value into a 64-bit floating-point register
11507        will not sign-extend the value, despite what LOAD_EXTEND_OP
11508        says.  We can't allow FPRs to change from SImode to a wider
11509        mode on 64-bit targets.
11510 
11511      - If the FPU has already interpreted a value in one format, we
11512        must not ask it to treat the value as having a different
11513        format.
11514 
11515      We therefore disallow all mode changes involving FPRs.  */
11516 
11517   return reg_classes_intersect_p (FP_REGS, rclass);
11518 }
11519 
11520 /* Implement target hook small_register_classes_for_mode_p.  */
11521 
11522 static bool
11523 mips_small_register_classes_for_mode_p (enum machine_mode mode
11524 					ATTRIBUTE_UNUSED)
11525 {
11526   return TARGET_MIPS16;
11527 }
11528 
11529 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction.  */
11530 
11531 static bool
11532 mips_mode_ok_for_mov_fmt_p (enum machine_mode mode)
11533 {
11534   switch (mode)
11535     {
11536     case SFmode:
11537       return TARGET_HARD_FLOAT;
11538 
11539     case DFmode:
11540       return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
11541 
11542     case V2SFmode:
11543       return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
11544 
11545     default:
11546       return false;
11547     }
11548 }
11549 
11550 /* Implement MODES_TIEABLE_P.  */
11551 
11552 bool
11553 mips_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
11554 {
11555   /* FPRs allow no mode punning, so it's not worth tying modes if we'd
11556      prefer to put one of them in FPRs.  */
11557   return (mode1 == mode2
11558 	  || (!mips_mode_ok_for_mov_fmt_p (mode1)
11559 	      && !mips_mode_ok_for_mov_fmt_p (mode2)));
11560 }
11561 
11562 /* Implement TARGET_PREFERRED_RELOAD_CLASS.  */
11563 
11564 static reg_class_t
11565 mips_preferred_reload_class (rtx x, reg_class_t rclass)
11566 {
11567   if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
11568     return LEA_REGS;
11569 
11570   if (reg_class_subset_p (FP_REGS, rclass)
11571       && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
11572     return FP_REGS;
11573 
11574   if (reg_class_subset_p (GR_REGS, rclass))
11575     rclass = GR_REGS;
11576 
11577   if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
11578     rclass = M16_REGS;
11579 
11580   return rclass;
11581 }
11582 
11583 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
11584    Return a "canonical" class to represent it in later calculations.  */
11585 
11586 static reg_class_t
11587 mips_canonicalize_move_class (reg_class_t rclass)
11588 {
11589   /* All moves involving accumulator registers have the same cost.  */
11590   if (reg_class_subset_p (rclass, ACC_REGS))
11591     rclass = ACC_REGS;
11592 
11593   /* Likewise promote subclasses of general registers to the most
11594      interesting containing class.  */
11595   if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
11596     rclass = M16_REGS;
11597   else if (reg_class_subset_p (rclass, GENERAL_REGS))
11598     rclass = GENERAL_REGS;
11599 
11600   return rclass;
11601 }
11602 
11603 /* Return the cost of moving a value of mode MODE from a register of
11604    class FROM to a GPR.  Return 0 for classes that are unions of other
11605    classes handled by this function.  */
11606 
11607 static int
11608 mips_move_to_gpr_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
11609 		       reg_class_t from)
11610 {
11611   switch (from)
11612     {
11613     case GENERAL_REGS:
11614       /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro.  */
11615       return 2;
11616 
11617     case ACC_REGS:
11618       /* MFLO and MFHI.  */
11619       return 6;
11620 
11621     case FP_REGS:
11622       /* MFC1, etc.  */
11623       return 4;
11624 
11625     case ST_REGS:
11626       /* LUI followed by MOVF.  */
11627       return 4;
11628 
11629     case COP0_REGS:
11630     case COP2_REGS:
11631     case COP3_REGS:
11632       /* This choice of value is historical.  */
11633       return 5;
11634 
11635     default:
11636       return 0;
11637     }
11638 }
11639 
11640 /* Return the cost of moving a value of mode MODE from a GPR to a
11641    register of class TO.  Return 0 for classes that are unions of
11642    other classes handled by this function.  */
11643 
11644 static int
11645 mips_move_from_gpr_cost (enum machine_mode mode, reg_class_t to)
11646 {
11647   switch (to)
11648     {
11649     case GENERAL_REGS:
11650       /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro.  */
11651       return 2;
11652 
11653     case ACC_REGS:
11654       /* MTLO and MTHI.  */
11655       return 6;
11656 
11657     case FP_REGS:
11658       /* MTC1, etc.  */
11659       return 4;
11660 
11661     case ST_REGS:
11662       /* A secondary reload through an FPR scratch.  */
11663       return (mips_register_move_cost (mode, GENERAL_REGS, FP_REGS)
11664 	      + mips_register_move_cost (mode, FP_REGS, ST_REGS));
11665 
11666     case COP0_REGS:
11667     case COP2_REGS:
11668     case COP3_REGS:
11669       /* This choice of value is historical.  */
11670       return 5;
11671 
11672     default:
11673       return 0;
11674     }
11675 }
11676 
11677 /* Implement TARGET_REGISTER_MOVE_COST.  Return 0 for classes that are the
11678    maximum of the move costs for subclasses; regclass will work out
11679    the maximum for us.  */
11680 
11681 static int
11682 mips_register_move_cost (enum machine_mode mode,
11683 			 reg_class_t from, reg_class_t to)
11684 {
11685   reg_class_t dregs;
11686   int cost1, cost2;
11687 
11688   from = mips_canonicalize_move_class (from);
11689   to = mips_canonicalize_move_class (to);
11690 
11691   /* Handle moves that can be done without using general-purpose registers.  */
11692   if (from == FP_REGS)
11693     {
11694       if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
11695 	/* MOV.FMT.  */
11696 	return 4;
11697       if (to == ST_REGS)
11698 	/* The sequence generated by mips_expand_fcc_reload.  */
11699 	return 8;
11700     }
11701 
11702   /* Handle cases in which only one class deviates from the ideal.  */
11703   dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
11704   if (from == dregs)
11705     return mips_move_from_gpr_cost (mode, to);
11706   if (to == dregs)
11707     return mips_move_to_gpr_cost (mode, from);
11708 
11709   /* Handles cases that require a GPR temporary.  */
11710   cost1 = mips_move_to_gpr_cost (mode, from);
11711   if (cost1 != 0)
11712     {
11713       cost2 = mips_move_from_gpr_cost (mode, to);
11714       if (cost2 != 0)
11715 	return cost1 + cost2;
11716     }
11717 
11718   return 0;
11719 }
11720 
11721 /* Implement TARGET_MEMORY_MOVE_COST.  */
11722 
11723 static int
11724 mips_memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
11725 {
11726   return (mips_cost->memory_latency
11727 	  + memory_move_secondary_cost (mode, rclass, in));
11728 }
11729 
11730 /* Return the register class required for a secondary register when
11731    copying between one of the registers in RCLASS and value X, which
11732    has mode MODE.  X is the source of the move if IN_P, otherwise it
11733    is the destination.  Return NO_REGS if no secondary register is
11734    needed.  */
11735 
11736 enum reg_class
11737 mips_secondary_reload_class (enum reg_class rclass,
11738 			     enum machine_mode mode, rtx x, bool in_p)
11739 {
11740   int regno;
11741 
11742   /* If X is a constant that cannot be loaded into $25, it must be loaded
11743      into some other GPR.  No other register class allows a direct move.  */
11744   if (mips_dangerous_for_la25_p (x))
11745     return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
11746 
11747   regno = true_regnum (x);
11748   if (TARGET_MIPS16)
11749     {
11750       /* In MIPS16 mode, every move must involve a member of M16_REGS.  */
11751       if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
11752 	return M16_REGS;
11753 
11754       return NO_REGS;
11755     }
11756 
11757   /* Copying from accumulator registers to anywhere other than a general
11758      register requires a temporary general register.  */
11759   if (reg_class_subset_p (rclass, ACC_REGS))
11760     return GP_REG_P (regno) ? NO_REGS : GR_REGS;
11761   if (ACC_REG_P (regno))
11762     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11763 
11764   /* We can only copy a value to a condition code register from a
11765      floating-point register, and even then we require a scratch
11766      floating-point register.  We can only copy a value out of a
11767      condition-code register into a general register.  */
11768   if (reg_class_subset_p (rclass, ST_REGS))
11769     {
11770       if (in_p)
11771 	return FP_REGS;
11772       return GP_REG_P (regno) ? NO_REGS : GR_REGS;
11773     }
11774   if (ST_REG_P (regno))
11775     {
11776       if (!in_p)
11777 	return FP_REGS;
11778       return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11779     }
11780 
11781   if (reg_class_subset_p (rclass, FP_REGS))
11782     {
11783       if (MEM_P (x)
11784 	  && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8))
11785 	/* In this case we can use lwc1, swc1, ldc1 or sdc1.  We'll use
11786 	   pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported.  */
11787 	return NO_REGS;
11788 
11789       if (GP_REG_P (regno) || x == CONST0_RTX (mode))
11790 	/* In this case we can use mtc1, mfc1, dmtc1 or dmfc1.  */
11791 	return NO_REGS;
11792 
11793       if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
11794 	/* We can force the constant to memory and use lwc1
11795 	   and ldc1.  As above, we will use pairs of lwc1s if
11796 	   ldc1 is not supported.  */
11797 	return NO_REGS;
11798 
11799       if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
11800 	/* In this case we can use mov.fmt.  */
11801 	return NO_REGS;
11802 
11803       /* Otherwise, we need to reload through an integer register.  */
11804       return GR_REGS;
11805     }
11806   if (FP_REG_P (regno))
11807     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11808 
11809   return NO_REGS;
11810 }
11811 
11812 /* Implement TARGET_MODE_REP_EXTENDED.  */
11813 
11814 static int
11815 mips_mode_rep_extended (enum machine_mode mode, enum machine_mode mode_rep)
11816 {
11817   /* On 64-bit targets, SImode register values are sign-extended to DImode.  */
11818   if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
11819     return SIGN_EXTEND;
11820 
11821   return UNKNOWN;
11822 }
11823 
11824 /* Implement TARGET_VALID_POINTER_MODE.  */
11825 
11826 static bool
11827 mips_valid_pointer_mode (enum machine_mode mode)
11828 {
11829   return mode == SImode || (TARGET_64BIT && mode == DImode);
11830 }
11831 
11832 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P.  */
11833 
11834 static bool
11835 mips_vector_mode_supported_p (enum machine_mode mode)
11836 {
11837   switch (mode)
11838     {
11839     case V2SFmode:
11840       return TARGET_PAIRED_SINGLE_FLOAT;
11841 
11842     case V2HImode:
11843     case V4QImode:
11844     case V2HQmode:
11845     case V2UHQmode:
11846     case V2HAmode:
11847     case V2UHAmode:
11848     case V4QQmode:
11849     case V4UQQmode:
11850       return TARGET_DSP;
11851 
11852     case V2SImode:
11853     case V4HImode:
11854     case V8QImode:
11855       return TARGET_LOONGSON_VECTORS;
11856 
11857     default:
11858       return false;
11859     }
11860 }
11861 
11862 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.  */
11863 
11864 static bool
11865 mips_scalar_mode_supported_p (enum machine_mode mode)
11866 {
11867   if (ALL_FIXED_POINT_MODE_P (mode)
11868       && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
11869     return true;
11870 
11871   return default_scalar_mode_supported_p (mode);
11872 }
11873 
11874 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE.  */
11875 
11876 static enum machine_mode
11877 mips_preferred_simd_mode (enum machine_mode mode ATTRIBUTE_UNUSED)
11878 {
11879   if (TARGET_PAIRED_SINGLE_FLOAT
11880       && mode == SFmode)
11881     return V2SFmode;
11882   return word_mode;
11883 }
11884 
11885 /* Implement TARGET_INIT_LIBFUNCS.  */
11886 
11887 static void
11888 mips_init_libfuncs (void)
11889 {
11890   if (TARGET_FIX_VR4120)
11891     {
11892       /* Register the special divsi3 and modsi3 functions needed to work
11893 	 around VR4120 division errata.  */
11894       set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
11895       set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
11896     }
11897 
11898   if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
11899     {
11900       /* Register the MIPS16 -mhard-float stubs.  */
11901       set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
11902       set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
11903       set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
11904       set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
11905 
11906       set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
11907       set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
11908       set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
11909       set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
11910       set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
11911       set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
11912       set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
11913 
11914       set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
11915       set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
11916       set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
11917 
11918       if (TARGET_DOUBLE_FLOAT)
11919 	{
11920 	  set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
11921 	  set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
11922 	  set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
11923 	  set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
11924 
11925 	  set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
11926 	  set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
11927 	  set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
11928 	  set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
11929 	  set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
11930 	  set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
11931 	  set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
11932 
11933 	  set_conv_libfunc (sext_optab, DFmode, SFmode,
11934 			    "__mips16_extendsfdf2");
11935 	  set_conv_libfunc (trunc_optab, SFmode, DFmode,
11936 			    "__mips16_truncdfsf2");
11937 	  set_conv_libfunc (sfix_optab, SImode, DFmode,
11938 			    "__mips16_fix_truncdfsi");
11939 	  set_conv_libfunc (sfloat_optab, DFmode, SImode,
11940 			    "__mips16_floatsidf");
11941 	  set_conv_libfunc (ufloat_optab, DFmode, SImode,
11942 			    "__mips16_floatunsidf");
11943 	}
11944     }
11945 
11946   /* The MIPS16 ISA does not have an encoding for "sync", so we rely
11947      on an external non-MIPS16 routine to implement __sync_synchronize.
11948      Similarly for the rest of the ll/sc libfuncs.  */
11949   if (TARGET_MIPS16)
11950     {
11951       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
11952       init_sync_libfuncs (UNITS_PER_WORD);
11953     }
11954 }
11955 
11956 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
11957 
11958 static void
11959 mips_process_load_label (rtx target)
11960 {
11961   rtx base, gp, intop;
11962   HOST_WIDE_INT offset;
11963 
11964   mips_multi_start ();
11965   switch (mips_abi)
11966     {
11967     case ABI_N32:
11968       mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
11969       mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
11970       break;
11971 
11972     case ABI_64:
11973       mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
11974       mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
11975       break;
11976 
11977     default:
11978       gp = pic_offset_table_rtx;
11979       if (mips_cfun_has_cprestore_slot_p ())
11980 	{
11981 	  gp = gen_rtx_REG (Pmode, AT_REGNUM);
11982 	  mips_get_cprestore_base_and_offset (&base, &offset, true);
11983 	  if (!SMALL_OPERAND (offset))
11984 	    {
11985 	      intop = GEN_INT (CONST_HIGH_PART (offset));
11986 	      mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
11987 	      mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
11988 
11989 	      base = gp;
11990 	      offset = CONST_LOW_PART (offset);
11991 	    }
11992 	  intop = GEN_INT (offset);
11993 	  if (ISA_HAS_LOAD_DELAY)
11994 	    mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
11995 	  else
11996 	    mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
11997 	}
11998       if (ISA_HAS_LOAD_DELAY)
11999 	mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
12000       else
12001 	mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
12002       mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
12003       break;
12004     }
12005 }
12006 
12007 /* Return the number of instructions needed to load a label into $AT.  */
12008 
12009 static unsigned int
12010 mips_load_label_num_insns (void)
12011 {
12012   if (cfun->machine->load_label_num_insns == 0)
12013     {
12014       mips_process_load_label (pc_rtx);
12015       cfun->machine->load_label_num_insns = mips_multi_num_insns;
12016     }
12017   return cfun->machine->load_label_num_insns;
12018 }
12019 
12020 /* Emit an asm sequence to start a noat block and load the address
12021    of a label into $1.  */
12022 
12023 void
12024 mips_output_load_label (rtx target)
12025 {
12026   mips_push_asm_switch (&mips_noat);
12027   if (TARGET_EXPLICIT_RELOCS)
12028     {
12029       mips_process_load_label (target);
12030       mips_multi_write ();
12031     }
12032   else
12033     {
12034       if (Pmode == DImode)
12035 	output_asm_insn ("dla\t%@,%0", &target);
12036       else
12037 	output_asm_insn ("la\t%@,%0", &target);
12038     }
12039 }
12040 
12041 /* Return the length of INSN.  LENGTH is the initial length computed by
12042    attributes in the machine-description file.  */
12043 
12044 int
12045 mips_adjust_insn_length (rtx insn, int length)
12046 {
12047   /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
12048      of a PIC long-branch sequence.  Substitute the correct value.  */
12049   if (length == MAX_PIC_BRANCH_LENGTH
12050       && INSN_CODE (insn) >= 0
12051       && get_attr_type (insn) == TYPE_BRANCH)
12052     {
12053       /* Add the branch-over instruction and its delay slot, if this
12054 	 is a conditional branch.  */
12055       length = simplejump_p (insn) ? 0 : 8;
12056 
12057       /* Load the label into $AT and jump to it.  Ignore the delay
12058 	 slot of the jump.  */
12059       length += 4 * mips_load_label_num_insns() + 4;
12060     }
12061 
12062   /* A unconditional jump has an unfilled delay slot if it is not part
12063      of a sequence.  A conditional jump normally has a delay slot, but
12064      does not on MIPS16.  */
12065   if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
12066     length += 4;
12067 
12068   /* See how many nops might be needed to avoid hardware hazards.  */
12069   if (!cfun->machine->ignore_hazard_length_p && INSN_CODE (insn) >= 0)
12070     switch (get_attr_hazard (insn))
12071       {
12072       case HAZARD_NONE:
12073 	break;
12074 
12075       case HAZARD_DELAY:
12076 	length += 4;
12077 	break;
12078 
12079       case HAZARD_HILO:
12080 	length += 8;
12081 	break;
12082       }
12083 
12084   /* In order to make it easier to share MIPS16 and non-MIPS16 patterns,
12085      the .md file length attributes are 4-based for both modes.
12086      Adjust the MIPS16 ones here.  */
12087   if (TARGET_MIPS16)
12088     length /= 2;
12089 
12090   return length;
12091 }
12092 
12093 /* Return the assembly code for INSN, which has the operands given by
12094    OPERANDS, and which branches to OPERANDS[0] if some condition is true.
12095    BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
12096    is in range of a direct branch.  BRANCH_IF_FALSE is an inverted
12097    version of BRANCH_IF_TRUE.  */
12098 
12099 const char *
12100 mips_output_conditional_branch (rtx insn, rtx *operands,
12101 				const char *branch_if_true,
12102 				const char *branch_if_false)
12103 {
12104   unsigned int length;
12105   rtx taken, not_taken;
12106 
12107   gcc_assert (LABEL_P (operands[0]));
12108 
12109   length = get_attr_length (insn);
12110   if (length <= 8)
12111     {
12112       /* Just a simple conditional branch.  */
12113       mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
12114       return branch_if_true;
12115     }
12116 
12117   /* Generate a reversed branch around a direct jump.  This fallback does
12118      not use branch-likely instructions.  */
12119   mips_branch_likely = false;
12120   not_taken = gen_label_rtx ();
12121   taken = operands[0];
12122 
12123   /* Generate the reversed branch to NOT_TAKEN.  */
12124   operands[0] = not_taken;
12125   output_asm_insn (branch_if_false, operands);
12126 
12127   /* If INSN has a delay slot, we must provide delay slots for both the
12128      branch to NOT_TAKEN and the conditional jump.  We must also ensure
12129      that INSN's delay slot is executed in the appropriate cases.  */
12130   if (final_sequence)
12131     {
12132       /* This first delay slot will always be executed, so use INSN's
12133 	 delay slot if is not annulled.  */
12134       if (!INSN_ANNULLED_BRANCH_P (insn))
12135 	{
12136 	  final_scan_insn (XVECEXP (final_sequence, 0, 1),
12137 			   asm_out_file, optimize, 1, NULL);
12138 	  INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
12139 	}
12140       else
12141 	output_asm_insn ("nop", 0);
12142       fprintf (asm_out_file, "\n");
12143     }
12144 
12145   /* Output the unconditional branch to TAKEN.  */
12146   if (TARGET_ABSOLUTE_JUMPS)
12147     output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
12148   else
12149     {
12150       mips_output_load_label (taken);
12151       output_asm_insn ("jr\t%@%]%/", 0);
12152     }
12153 
12154   /* Now deal with its delay slot; see above.  */
12155   if (final_sequence)
12156     {
12157       /* This delay slot will only be executed if the branch is taken.
12158 	 Use INSN's delay slot if is annulled.  */
12159       if (INSN_ANNULLED_BRANCH_P (insn))
12160 	{
12161 	  final_scan_insn (XVECEXP (final_sequence, 0, 1),
12162 			   asm_out_file, optimize, 1, NULL);
12163 	  INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
12164 	}
12165       else
12166 	output_asm_insn ("nop", 0);
12167       fprintf (asm_out_file, "\n");
12168     }
12169 
12170   /* Output NOT_TAKEN.  */
12171   targetm.asm_out.internal_label (asm_out_file, "L",
12172 				  CODE_LABEL_NUMBER (not_taken));
12173   return "";
12174 }
12175 
12176 /* Return the assembly code for INSN, which branches to OPERANDS[0]
12177    if some ordering condition is true.  The condition is given by
12178    OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
12179    OPERANDS[1].  OPERANDS[2] is the comparison's first operand;
12180    its second is always zero.  */
12181 
12182 const char *
12183 mips_output_order_conditional_branch (rtx insn, rtx *operands, bool inverted_p)
12184 {
12185   const char *branch[2];
12186 
12187   /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
12188      Make BRANCH[0] branch on the inverse condition.  */
12189   switch (GET_CODE (operands[1]))
12190     {
12191       /* These cases are equivalent to comparisons against zero.  */
12192     case LEU:
12193       inverted_p = !inverted_p;
12194       /* Fall through.  */
12195     case GTU:
12196       branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
12197       branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
12198       break;
12199 
12200       /* These cases are always true or always false.  */
12201     case LTU:
12202       inverted_p = !inverted_p;
12203       /* Fall through.  */
12204     case GEU:
12205       branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
12206       branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
12207       break;
12208 
12209     default:
12210       branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
12211       branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
12212       break;
12213     }
12214   return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
12215 }
12216 
12217 /* Start a block of code that needs access to the LL, SC and SYNC
12218    instructions.  */
12219 
12220 static void
12221 mips_start_ll_sc_sync_block (void)
12222 {
12223   if (!ISA_HAS_LL_SC)
12224     {
12225       output_asm_insn (".set\tpush", 0);
12226       output_asm_insn (".set\tmips2", 0);
12227     }
12228 }
12229 
12230 /* End a block started by mips_start_ll_sc_sync_block.  */
12231 
12232 static void
12233 mips_end_ll_sc_sync_block (void)
12234 {
12235   if (!ISA_HAS_LL_SC)
12236     output_asm_insn (".set\tpop", 0);
12237 }
12238 
12239 /* Output and/or return the asm template for a sync instruction.  */
12240 
12241 const char *
12242 mips_output_sync (void)
12243 {
12244   mips_start_ll_sc_sync_block ();
12245   output_asm_insn ("sync", 0);
12246   mips_end_ll_sc_sync_block ();
12247   return "";
12248 }
12249 
12250 /* Return the asm template associated with sync_insn1 value TYPE.
12251    IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation.  */
12252 
12253 static const char *
12254 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
12255 {
12256   switch (type)
12257     {
12258     case SYNC_INSN1_MOVE:
12259       return "move\t%0,%z2";
12260     case SYNC_INSN1_LI:
12261       return "li\t%0,%2";
12262     case SYNC_INSN1_ADDU:
12263       return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
12264     case SYNC_INSN1_ADDIU:
12265       return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
12266     case SYNC_INSN1_SUBU:
12267       return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
12268     case SYNC_INSN1_AND:
12269       return "and\t%0,%1,%z2";
12270     case SYNC_INSN1_ANDI:
12271       return "andi\t%0,%1,%2";
12272     case SYNC_INSN1_OR:
12273       return "or\t%0,%1,%z2";
12274     case SYNC_INSN1_ORI:
12275       return "ori\t%0,%1,%2";
12276     case SYNC_INSN1_XOR:
12277       return "xor\t%0,%1,%z2";
12278     case SYNC_INSN1_XORI:
12279       return "xori\t%0,%1,%2";
12280     }
12281   gcc_unreachable ();
12282 }
12283 
12284 /* Return the asm template associated with sync_insn2 value TYPE.  */
12285 
12286 static const char *
12287 mips_sync_insn2_template (enum attr_sync_insn2 type)
12288 {
12289   switch (type)
12290     {
12291     case SYNC_INSN2_NOP:
12292       gcc_unreachable ();
12293     case SYNC_INSN2_AND:
12294       return "and\t%0,%1,%z2";
12295     case SYNC_INSN2_XOR:
12296       return "xor\t%0,%1,%z2";
12297     case SYNC_INSN2_NOT:
12298       return "nor\t%0,%1,%.";
12299     }
12300   gcc_unreachable ();
12301 }
12302 
12303 /* OPERANDS are the operands to a sync loop instruction and INDEX is
12304    the value of the one of the sync_* attributes.  Return the operand
12305    referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
12306    have the associated attribute.  */
12307 
12308 static rtx
12309 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
12310 {
12311   if (index > 0)
12312     default_value = operands[index - 1];
12313   return default_value;
12314 }
12315 
12316 /* INSN is a sync loop with operands OPERANDS.  Build up a multi-insn
12317    sequence for it.  */
12318 
12319 static void
12320 mips_process_sync_loop (rtx insn, rtx *operands)
12321 {
12322   rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
12323   rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
12324   unsigned int tmp3_insn;
12325   enum attr_sync_insn1 insn1;
12326   enum attr_sync_insn2 insn2;
12327   bool is_64bit_p;
12328   int memmodel_attr;
12329   enum memmodel model;
12330 
12331   /* Read an operand from the sync_WHAT attribute and store it in
12332      variable WHAT.  DEFAULT is the default value if no attribute
12333      is specified.  */
12334 #define READ_OPERAND(WHAT, DEFAULT) \
12335   WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
12336   				DEFAULT)
12337 
12338   /* Read the memory.  */
12339   READ_OPERAND (mem, 0);
12340   gcc_assert (mem);
12341   is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
12342 
12343   /* Read the other attributes.  */
12344   at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
12345   READ_OPERAND (oldval, at);
12346   READ_OPERAND (cmp, 0);
12347   READ_OPERAND (newval, at);
12348   READ_OPERAND (inclusive_mask, 0);
12349   READ_OPERAND (exclusive_mask, 0);
12350   READ_OPERAND (required_oldval, 0);
12351   READ_OPERAND (insn1_op2, 0);
12352   insn1 = get_attr_sync_insn1 (insn);
12353   insn2 = get_attr_sync_insn2 (insn);
12354 
12355   /* Don't bother setting CMP result that is never used.  */
12356   if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
12357     cmp = 0;
12358 
12359   memmodel_attr = get_attr_sync_memmodel (insn);
12360   switch (memmodel_attr)
12361     {
12362     case 10:
12363       model = MEMMODEL_ACQ_REL;
12364       break;
12365     case 11:
12366       model = MEMMODEL_ACQUIRE;
12367       break;
12368     default:
12369       model = (enum memmodel) INTVAL (operands[memmodel_attr]);
12370     }
12371 
12372   mips_multi_start ();
12373 
12374   /* Output the release side of the memory barrier.  */
12375   if (need_atomic_barrier_p (model, true))
12376     {
12377       if (required_oldval == 0 && TARGET_OCTEON)
12378 	{
12379 	  /* Octeon doesn't reorder reads, so a full barrier can be
12380 	     created by using SYNCW to order writes combined with the
12381 	     write from the following SC.  When the SC successfully
12382 	     completes, we know that all preceding writes are also
12383 	     committed to the coherent memory system.  It is possible
12384 	     for a single SYNCW to fail, but a pair of them will never
12385 	     fail, so we use two.  */
12386 	  mips_multi_add_insn ("syncw", NULL);
12387 	  mips_multi_add_insn ("syncw", NULL);
12388 	}
12389       else
12390 	mips_multi_add_insn ("sync", NULL);
12391     }
12392 
12393   /* Output the branch-back label.  */
12394   mips_multi_add_label ("1:");
12395 
12396   /* OLDVAL = *MEM.  */
12397   mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
12398 		       oldval, mem, NULL);
12399 
12400   /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2.  */
12401   if (required_oldval)
12402     {
12403       if (inclusive_mask == 0)
12404 	tmp1 = oldval;
12405       else
12406 	{
12407 	  gcc_assert (oldval != at);
12408 	  mips_multi_add_insn ("and\t%0,%1,%2",
12409 			       at, oldval, inclusive_mask, NULL);
12410 	  tmp1 = at;
12411 	}
12412       mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
12413 
12414       /* CMP = 0 [delay slot].  */
12415       if (cmp)
12416         mips_multi_add_insn ("li\t%0,0", cmp, NULL);
12417     }
12418 
12419   /* $TMP1 = OLDVAL & EXCLUSIVE_MASK.  */
12420   if (exclusive_mask == 0)
12421     tmp1 = const0_rtx;
12422   else
12423     {
12424       gcc_assert (oldval != at);
12425       mips_multi_add_insn ("and\t%0,%1,%z2",
12426 			   at, oldval, exclusive_mask, NULL);
12427       tmp1 = at;
12428     }
12429 
12430   /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
12431 
12432      We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
12433      at least one instruction in that case.  */
12434   if (insn1 == SYNC_INSN1_MOVE
12435       && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
12436     tmp2 = insn1_op2;
12437   else
12438     {
12439       mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
12440 			   newval, oldval, insn1_op2, NULL);
12441       tmp2 = newval;
12442     }
12443 
12444   /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK).  */
12445   if (insn2 == SYNC_INSN2_NOP)
12446     tmp3 = tmp2;
12447   else
12448     {
12449       mips_multi_add_insn (mips_sync_insn2_template (insn2),
12450 			   newval, tmp2, inclusive_mask, NULL);
12451       tmp3 = newval;
12452     }
12453   tmp3_insn = mips_multi_last_index ();
12454 
12455   /* $AT = $TMP1 | $TMP3.  */
12456   if (tmp1 == const0_rtx || tmp3 == const0_rtx)
12457     {
12458       mips_multi_set_operand (tmp3_insn, 0, at);
12459       tmp3 = at;
12460     }
12461   else
12462     {
12463       gcc_assert (tmp1 != tmp3);
12464       mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
12465     }
12466 
12467   /* if (!commit (*MEM = $AT)) goto 1.
12468 
12469      This will sometimes be a delayed branch; see the write code below
12470      for details.  */
12471   mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
12472   mips_multi_add_insn ("beq%?\t%0,%.,1b", at, NULL);
12473 
12474   /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot].  */
12475   if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
12476     {
12477       mips_multi_copy_insn (tmp3_insn);
12478       mips_multi_set_operand (mips_multi_last_index (), 0, newval);
12479     }
12480   else if (!(required_oldval && cmp))
12481     mips_multi_add_insn ("nop", NULL);
12482 
12483   /* CMP = 1 -- either standalone or in a delay slot.  */
12484   if (required_oldval && cmp)
12485     mips_multi_add_insn ("li\t%0,1", cmp, NULL);
12486 
12487   /* Output the acquire side of the memory barrier.  */
12488   if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
12489     mips_multi_add_insn ("sync", NULL);
12490 
12491   /* Output the exit label, if needed.  */
12492   if (required_oldval)
12493     mips_multi_add_label ("2:");
12494 
12495 #undef READ_OPERAND
12496 }
12497 
12498 /* Output and/or return the asm template for sync loop INSN, which has
12499    the operands given by OPERANDS.  */
12500 
12501 const char *
12502 mips_output_sync_loop (rtx insn, rtx *operands)
12503 {
12504   mips_process_sync_loop (insn, operands);
12505 
12506   /* Use branch-likely instructions to work around the LL/SC R10000
12507      errata.  */
12508   mips_branch_likely = TARGET_FIX_R10000;
12509 
12510   mips_push_asm_switch (&mips_noreorder);
12511   mips_push_asm_switch (&mips_nomacro);
12512   mips_push_asm_switch (&mips_noat);
12513   mips_start_ll_sc_sync_block ();
12514 
12515   mips_multi_write ();
12516 
12517   mips_end_ll_sc_sync_block ();
12518   mips_pop_asm_switch (&mips_noat);
12519   mips_pop_asm_switch (&mips_nomacro);
12520   mips_pop_asm_switch (&mips_noreorder);
12521 
12522   return "";
12523 }
12524 
12525 /* Return the number of individual instructions in sync loop INSN,
12526    which has the operands given by OPERANDS.  */
12527 
12528 unsigned int
12529 mips_sync_loop_insns (rtx insn, rtx *operands)
12530 {
12531   mips_process_sync_loop (insn, operands);
12532   return mips_multi_num_insns;
12533 }
12534 
12535 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
12536    the operands given by OPERANDS.  Add in a divide-by-zero check if needed.
12537 
12538    When working around R4000 and R4400 errata, we need to make sure that
12539    the division is not immediately followed by a shift[1][2].  We also
12540    need to stop the division from being put into a branch delay slot[3].
12541    The easiest way to avoid both problems is to add a nop after the
12542    division.  When a divide-by-zero check is needed, this nop can be
12543    used to fill the branch delay slot.
12544 
12545    [1] If a double-word or a variable shift executes immediately
12546        after starting an integer division, the shift may give an
12547        incorrect result.  See quotations of errata #16 and #28 from
12548        "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12549        in mips.md for details.
12550 
12551    [2] A similar bug to [1] exists for all revisions of the
12552        R4000 and the R4400 when run in an MC configuration.
12553        From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
12554 
12555        "19. In this following sequence:
12556 
12557 		    ddiv		(or ddivu or div or divu)
12558 		    dsll32		(or dsrl32, dsra32)
12559 
12560 	    if an MPT stall occurs, while the divide is slipping the cpu
12561 	    pipeline, then the following double shift would end up with an
12562 	    incorrect result.
12563 
12564 	    Workaround: The compiler needs to avoid generating any
12565 	    sequence with divide followed by extended double shift."
12566 
12567        This erratum is also present in "MIPS R4400MC Errata, Processor
12568        Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
12569        & 3.0" as errata #10 and #4, respectively.
12570 
12571    [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12572        (also valid for MIPS R4000MC processors):
12573 
12574        "52. R4000SC: This bug does not apply for the R4000PC.
12575 
12576 	    There are two flavors of this bug:
12577 
12578 	    1) If the instruction just after divide takes an RF exception
12579 	       (tlb-refill, tlb-invalid) and gets an instruction cache
12580 	       miss (both primary and secondary) and the line which is
12581 	       currently in secondary cache at this index had the first
12582 	       data word, where the bits 5..2 are set, then R4000 would
12583 	       get a wrong result for the div.
12584 
12585 	    ##1
12586 		    nop
12587 		    div	r8, r9
12588 		    -------------------		# end-of page. -tlb-refill
12589 		    nop
12590 	    ##2
12591 		    nop
12592 		    div	r8, r9
12593 		    -------------------		# end-of page. -tlb-invalid
12594 		    nop
12595 
12596 	    2) If the divide is in the taken branch delay slot, where the
12597 	       target takes RF exception and gets an I-cache miss for the
12598 	       exception vector or where I-cache miss occurs for the
12599 	       target address, under the above mentioned scenarios, the
12600 	       div would get wrong results.
12601 
12602 	    ##1
12603 		    j	r2		# to next page mapped or unmapped
12604 		    div	r8,r9		# this bug would be there as long
12605 					# as there is an ICache miss and
12606 		    nop			# the "data pattern" is present
12607 
12608 	    ##2
12609 		    beq	r0, r0, NextPage	# to Next page
12610 		    div	r8,r9
12611 		    nop
12612 
12613 	    This bug is present for div, divu, ddiv, and ddivu
12614 	    instructions.
12615 
12616 	    Workaround: For item 1), OS could make sure that the next page
12617 	    after the divide instruction is also mapped.  For item 2), the
12618 	    compiler could make sure that the divide instruction is not in
12619 	    the branch delay slot."
12620 
12621        These processors have PRId values of 0x00004220 and 0x00004300 for
12622        the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400.  */
12623 
12624 const char *
12625 mips_output_division (const char *division, rtx *operands)
12626 {
12627   const char *s;
12628 
12629   s = division;
12630   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
12631     {
12632       output_asm_insn (s, operands);
12633       s = "nop";
12634     }
12635   if (TARGET_CHECK_ZERO_DIV)
12636     {
12637       if (TARGET_MIPS16)
12638 	{
12639 	  output_asm_insn (s, operands);
12640 	  s = "bnez\t%2,1f\n\tbreak\t7\n1:";
12641 	}
12642       else if (GENERATE_DIVIDE_TRAPS)
12643 	{
12644 	  /* Avoid long replay penalty on load miss by putting the trap before
12645 	     the divide.  */
12646 	  if (TUNE_74K)
12647 	    output_asm_insn ("teq\t%2,%.,7", operands);
12648 	  else
12649 	    {
12650 	      output_asm_insn (s, operands);
12651 	      s = "teq\t%2,%.,7";
12652 	    }
12653 	}
12654       else
12655 	{
12656 	  output_asm_insn ("%(bne\t%2,%.,1f", operands);
12657 	  output_asm_insn (s, operands);
12658 	  s = "break\t7%)\n1:";
12659 	}
12660     }
12661   return s;
12662 }
12663 
12664 /* Return true if IN_INSN is a multiply-add or multiply-subtract
12665    instruction and if OUT_INSN assigns to the accumulator operand.  */
12666 
12667 bool
12668 mips_linked_madd_p (rtx out_insn, rtx in_insn)
12669 {
12670   enum attr_accum_in accum_in;
12671   int accum_in_opnum;
12672   rtx accum_in_op;
12673 
12674   if (recog_memoized (in_insn) < 0)
12675     return false;
12676 
12677   accum_in = get_attr_accum_in (in_insn);
12678   if (accum_in == ACCUM_IN_NONE)
12679     return false;
12680 
12681   accum_in_opnum = accum_in - ACCUM_IN_0;
12682 
12683   extract_insn (in_insn);
12684   gcc_assert (accum_in_opnum < recog_data.n_operands);
12685   accum_in_op = recog_data.operand[accum_in_opnum];
12686 
12687   return reg_set_p (accum_in_op, out_insn);
12688 }
12689 
12690 /* True if the dependency between OUT_INSN and IN_INSN is on the store
12691    data rather than the address.  We need this because the cprestore
12692    pattern is type "store", but is defined using an UNSPEC_VOLATILE,
12693    which causes the default routine to abort.  We just return false
12694    for that case.  */
12695 
12696 bool
12697 mips_store_data_bypass_p (rtx out_insn, rtx in_insn)
12698 {
12699   if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
12700     return false;
12701 
12702   return !store_data_bypass_p (out_insn, in_insn);
12703 }
12704 
12705 
12706 /* Variables and flags used in scheduler hooks when tuning for
12707    Loongson 2E/2F.  */
12708 static struct
12709 {
12710   /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
12711      strategy.  */
12712 
12713   /* If true, then next ALU1/2 instruction will go to ALU1.  */
12714   bool alu1_turn_p;
12715 
12716   /* If true, then next FALU1/2 unstruction will go to FALU1.  */
12717   bool falu1_turn_p;
12718 
12719   /* Codes to query if [f]alu{1,2}_core units are subscribed or not.  */
12720   int alu1_core_unit_code;
12721   int alu2_core_unit_code;
12722   int falu1_core_unit_code;
12723   int falu2_core_unit_code;
12724 
12725   /* True if current cycle has a multi instruction.
12726      This flag is used in mips_ls2_dfa_post_advance_cycle.  */
12727   bool cycle_has_multi_p;
12728 
12729   /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
12730      These are used in mips_ls2_dfa_post_advance_cycle to initialize
12731      DFA state.
12732      E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
12733      instruction to go ALU1.  */
12734   rtx alu1_turn_enabled_insn;
12735   rtx alu2_turn_enabled_insn;
12736   rtx falu1_turn_enabled_insn;
12737   rtx falu2_turn_enabled_insn;
12738 } mips_ls2;
12739 
12740 /* Implement TARGET_SCHED_ADJUST_COST.  We assume that anti and output
12741    dependencies have no cost, except on the 20Kc where output-dependence
12742    is treated like input-dependence.  */
12743 
12744 static int
12745 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
12746 		  rtx dep ATTRIBUTE_UNUSED, int cost)
12747 {
12748   if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
12749       && TUNE_20KC)
12750     return cost;
12751   if (REG_NOTE_KIND (link) != 0)
12752     return 0;
12753   return cost;
12754 }
12755 
12756 /* Return the number of instructions that can be issued per cycle.  */
12757 
12758 static int
12759 mips_issue_rate (void)
12760 {
12761   switch (mips_tune)
12762     {
12763     case PROCESSOR_74KC:
12764     case PROCESSOR_74KF2_1:
12765     case PROCESSOR_74KF1_1:
12766     case PROCESSOR_74KF3_2:
12767       /* The 74k is not strictly quad-issue cpu, but can be seen as one
12768 	 by the scheduler.  It can issue 1 ALU, 1 AGEN and 2 FPU insns,
12769 	 but in reality only a maximum of 3 insns can be issued as
12770 	 floating-point loads and stores also require a slot in the
12771 	 AGEN pipe.  */
12772     case PROCESSOR_R10000:
12773       /* All R10K Processors are quad-issue (being the first MIPS
12774          processors to support this feature). */
12775       return 4;
12776 
12777     case PROCESSOR_20KC:
12778     case PROCESSOR_R4130:
12779     case PROCESSOR_R5400:
12780     case PROCESSOR_R5500:
12781     case PROCESSOR_R7000:
12782     case PROCESSOR_R9000:
12783     case PROCESSOR_OCTEON:
12784     case PROCESSOR_OCTEON2:
12785       return 2;
12786 
12787     case PROCESSOR_SB1:
12788     case PROCESSOR_SB1A:
12789       /* This is actually 4, but we get better performance if we claim 3.
12790 	 This is partly because of unwanted speculative code motion with the
12791 	 larger number, and partly because in most common cases we can't
12792 	 reach the theoretical max of 4.  */
12793       return 3;
12794 
12795     case PROCESSOR_LOONGSON_2E:
12796     case PROCESSOR_LOONGSON_2F:
12797     case PROCESSOR_LOONGSON_3A:
12798       return 4;
12799 
12800     case PROCESSOR_XLP:
12801       return (reload_completed ? 4 : 3);
12802 
12803     default:
12804       return 1;
12805     }
12806 }
12807 
12808 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2.  */
12809 
12810 static void
12811 mips_ls2_init_dfa_post_cycle_insn (void)
12812 {
12813   start_sequence ();
12814   emit_insn (gen_ls2_alu1_turn_enabled_insn ());
12815   mips_ls2.alu1_turn_enabled_insn = get_insns ();
12816   end_sequence ();
12817 
12818   start_sequence ();
12819   emit_insn (gen_ls2_alu2_turn_enabled_insn ());
12820   mips_ls2.alu2_turn_enabled_insn = get_insns ();
12821   end_sequence ();
12822 
12823   start_sequence ();
12824   emit_insn (gen_ls2_falu1_turn_enabled_insn ());
12825   mips_ls2.falu1_turn_enabled_insn = get_insns ();
12826   end_sequence ();
12827 
12828   start_sequence ();
12829   emit_insn (gen_ls2_falu2_turn_enabled_insn ());
12830   mips_ls2.falu2_turn_enabled_insn = get_insns ();
12831   end_sequence ();
12832 
12833   mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
12834   mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
12835   mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
12836   mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
12837 }
12838 
12839 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
12840    Init data used in mips_dfa_post_advance_cycle.  */
12841 
12842 static void
12843 mips_init_dfa_post_cycle_insn (void)
12844 {
12845   if (TUNE_LOONGSON_2EF)
12846     mips_ls2_init_dfa_post_cycle_insn ();
12847 }
12848 
12849 /* Initialize STATE when scheduling for Loongson 2E/2F.
12850    Support round-robin dispatch scheme by enabling only one of
12851    ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
12852    respectively.  */
12853 
12854 static void
12855 mips_ls2_dfa_post_advance_cycle (state_t state)
12856 {
12857   if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
12858     {
12859       /* Though there are no non-pipelined ALU1 insns,
12860 	 we can get an instruction of type 'multi' before reload.  */
12861       gcc_assert (mips_ls2.cycle_has_multi_p);
12862       mips_ls2.alu1_turn_p = false;
12863     }
12864 
12865   mips_ls2.cycle_has_multi_p = false;
12866 
12867   if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
12868     /* We have a non-pipelined alu instruction in the core,
12869        adjust round-robin counter.  */
12870     mips_ls2.alu1_turn_p = true;
12871 
12872   if (mips_ls2.alu1_turn_p)
12873     {
12874       if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
12875 	gcc_unreachable ();
12876     }
12877   else
12878     {
12879       if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
12880 	gcc_unreachable ();
12881     }
12882 
12883   if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
12884     {
12885       /* There are no non-pipelined FALU1 insns.  */
12886       gcc_unreachable ();
12887       mips_ls2.falu1_turn_p = false;
12888     }
12889 
12890   if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
12891     /* We have a non-pipelined falu instruction in the core,
12892        adjust round-robin counter.  */
12893     mips_ls2.falu1_turn_p = true;
12894 
12895   if (mips_ls2.falu1_turn_p)
12896     {
12897       if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
12898 	gcc_unreachable ();
12899     }
12900   else
12901     {
12902       if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
12903 	gcc_unreachable ();
12904     }
12905 }
12906 
12907 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
12908    This hook is being called at the start of each cycle.  */
12909 
12910 static void
12911 mips_dfa_post_advance_cycle (void)
12912 {
12913   if (TUNE_LOONGSON_2EF)
12914     mips_ls2_dfa_post_advance_cycle (curr_state);
12915 }
12916 
12917 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD.  This should
12918    be as wide as the scheduling freedom in the DFA.  */
12919 
12920 static int
12921 mips_multipass_dfa_lookahead (void)
12922 {
12923   /* Can schedule up to 4 of the 6 function units in any one cycle.  */
12924   if (TUNE_SB1)
12925     return 4;
12926 
12927   if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
12928     return 4;
12929 
12930   if (TUNE_OCTEON)
12931     return 2;
12932 
12933   return 0;
12934 }
12935 
12936 /* Remove the instruction at index LOWER from ready queue READY and
12937    reinsert it in front of the instruction at index HIGHER.  LOWER must
12938    be <= HIGHER.  */
12939 
12940 static void
12941 mips_promote_ready (rtx *ready, int lower, int higher)
12942 {
12943   rtx new_head;
12944   int i;
12945 
12946   new_head = ready[lower];
12947   for (i = lower; i < higher; i++)
12948     ready[i] = ready[i + 1];
12949   ready[i] = new_head;
12950 }
12951 
12952 /* If the priority of the instruction at POS2 in the ready queue READY
12953    is within LIMIT units of that of the instruction at POS1, swap the
12954    instructions if POS2 is not already less than POS1.  */
12955 
12956 static void
12957 mips_maybe_swap_ready (rtx *ready, int pos1, int pos2, int limit)
12958 {
12959   if (pos1 < pos2
12960       && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
12961     {
12962       rtx temp;
12963 
12964       temp = ready[pos1];
12965       ready[pos1] = ready[pos2];
12966       ready[pos2] = temp;
12967     }
12968 }
12969 
12970 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
12971    that may clobber hi or lo.  */
12972 static rtx mips_macc_chains_last_hilo;
12973 
12974 /* A TUNE_MACC_CHAINS helper function.  Record that instruction INSN has
12975    been scheduled, updating mips_macc_chains_last_hilo appropriately.  */
12976 
12977 static void
12978 mips_macc_chains_record (rtx insn)
12979 {
12980   if (get_attr_may_clobber_hilo (insn))
12981     mips_macc_chains_last_hilo = insn;
12982 }
12983 
12984 /* A TUNE_MACC_CHAINS helper function.  Search ready queue READY, which
12985    has NREADY elements, looking for a multiply-add or multiply-subtract
12986    instruction that is cumulative with mips_macc_chains_last_hilo.
12987    If there is one, promote it ahead of anything else that might
12988    clobber hi or lo.  */
12989 
12990 static void
12991 mips_macc_chains_reorder (rtx *ready, int nready)
12992 {
12993   int i, j;
12994 
12995   if (mips_macc_chains_last_hilo != 0)
12996     for (i = nready - 1; i >= 0; i--)
12997       if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
12998 	{
12999 	  for (j = nready - 1; j > i; j--)
13000 	    if (recog_memoized (ready[j]) >= 0
13001 		&& get_attr_may_clobber_hilo (ready[j]))
13002 	      {
13003 		mips_promote_ready (ready, i, j);
13004 		break;
13005 	      }
13006 	  break;
13007 	}
13008 }
13009 
13010 /* The last instruction to be scheduled.  */
13011 static rtx vr4130_last_insn;
13012 
13013 /* A note_stores callback used by vr4130_true_reg_dependence_p.  DATA
13014    points to an rtx that is initially an instruction.  Nullify the rtx
13015    if the instruction uses the value of register X.  */
13016 
13017 static void
13018 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
13019 				void *data)
13020 {
13021   rtx *insn_ptr;
13022 
13023   insn_ptr = (rtx *) data;
13024   if (REG_P (x)
13025       && *insn_ptr != 0
13026       && reg_referenced_p (x, PATTERN (*insn_ptr)))
13027     *insn_ptr = 0;
13028 }
13029 
13030 /* Return true if there is true register dependence between vr4130_last_insn
13031    and INSN.  */
13032 
13033 static bool
13034 vr4130_true_reg_dependence_p (rtx insn)
13035 {
13036   note_stores (PATTERN (vr4130_last_insn),
13037 	       vr4130_true_reg_dependence_p_1, &insn);
13038   return insn == 0;
13039 }
13040 
13041 /* A TUNE_MIPS4130 helper function.  Given that INSN1 is at the head of
13042    the ready queue and that INSN2 is the instruction after it, return
13043    true if it is worth promoting INSN2 ahead of INSN1.  Look for cases
13044    in which INSN1 and INSN2 can probably issue in parallel, but for
13045    which (INSN2, INSN1) should be less sensitive to instruction
13046    alignment than (INSN1, INSN2).  See 4130.md for more details.  */
13047 
13048 static bool
13049 vr4130_swap_insns_p (rtx insn1, rtx insn2)
13050 {
13051   sd_iterator_def sd_it;
13052   dep_t dep;
13053 
13054   /* Check for the following case:
13055 
13056      1) there is some other instruction X with an anti dependence on INSN1;
13057      2) X has a higher priority than INSN2; and
13058      3) X is an arithmetic instruction (and thus has no unit restrictions).
13059 
13060      If INSN1 is the last instruction blocking X, it would better to
13061      choose (INSN1, X) over (INSN2, INSN1).  */
13062   FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
13063     if (DEP_TYPE (dep) == REG_DEP_ANTI
13064 	&& INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
13065 	&& recog_memoized (DEP_CON (dep)) >= 0
13066 	&& get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
13067       return false;
13068 
13069   if (vr4130_last_insn != 0
13070       && recog_memoized (insn1) >= 0
13071       && recog_memoized (insn2) >= 0)
13072     {
13073       /* See whether INSN1 and INSN2 use different execution units,
13074 	 or if they are both ALU-type instructions.  If so, they can
13075 	 probably execute in parallel.  */
13076       enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
13077       enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
13078       if (class1 != class2 || class1 == VR4130_CLASS_ALU)
13079 	{
13080 	  /* If only one of the instructions has a dependence on
13081 	     vr4130_last_insn, prefer to schedule the other one first.  */
13082 	  bool dep1_p = vr4130_true_reg_dependence_p (insn1);
13083 	  bool dep2_p = vr4130_true_reg_dependence_p (insn2);
13084 	  if (dep1_p != dep2_p)
13085 	    return dep1_p;
13086 
13087 	  /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
13088 	     is not an ALU-type instruction and if INSN1 uses the same
13089 	     execution unit.  (Note that if this condition holds, we already
13090 	     know that INSN2 uses a different execution unit.)  */
13091 	  if (class1 != VR4130_CLASS_ALU
13092 	      && recog_memoized (vr4130_last_insn) >= 0
13093 	      && class1 == get_attr_vr4130_class (vr4130_last_insn))
13094 	    return true;
13095 	}
13096     }
13097   return false;
13098 }
13099 
13100 /* A TUNE_MIPS4130 helper function.  (READY, NREADY) describes a ready
13101    queue with at least two instructions.  Swap the first two if
13102    vr4130_swap_insns_p says that it could be worthwhile.  */
13103 
13104 static void
13105 vr4130_reorder (rtx *ready, int nready)
13106 {
13107   if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
13108     mips_promote_ready (ready, nready - 2, nready - 1);
13109 }
13110 
13111 /* Record whether last 74k AGEN instruction was a load or store.  */
13112 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
13113 
13114 /* Initialize mips_last_74k_agen_insn from INSN.  A null argument
13115    resets to TYPE_UNKNOWN state.  */
13116 
13117 static void
13118 mips_74k_agen_init (rtx insn)
13119 {
13120   if (!insn || CALL_P (insn) || JUMP_P (insn))
13121     mips_last_74k_agen_insn = TYPE_UNKNOWN;
13122   else
13123     {
13124       enum attr_type type = get_attr_type (insn);
13125       if (type == TYPE_LOAD || type == TYPE_STORE)
13126 	mips_last_74k_agen_insn = type;
13127     }
13128 }
13129 
13130 /* A TUNE_74K helper function.  The 74K AGEN pipeline likes multiple
13131    loads to be grouped together, and multiple stores to be grouped
13132    together.  Swap things around in the ready queue to make this happen.  */
13133 
13134 static void
13135 mips_74k_agen_reorder (rtx *ready, int nready)
13136 {
13137   int i;
13138   int store_pos, load_pos;
13139 
13140   store_pos = -1;
13141   load_pos = -1;
13142 
13143   for (i = nready - 1; i >= 0; i--)
13144     {
13145       rtx insn = ready[i];
13146       if (USEFUL_INSN_P (insn))
13147 	switch (get_attr_type (insn))
13148 	  {
13149 	  case TYPE_STORE:
13150 	    if (store_pos == -1)
13151 	      store_pos = i;
13152 	    break;
13153 
13154 	  case TYPE_LOAD:
13155 	    if (load_pos == -1)
13156 	      load_pos = i;
13157 	    break;
13158 
13159 	  default:
13160 	    break;
13161 	  }
13162     }
13163 
13164   if (load_pos == -1 || store_pos == -1)
13165     return;
13166 
13167   switch (mips_last_74k_agen_insn)
13168     {
13169     case TYPE_UNKNOWN:
13170       /* Prefer to schedule loads since they have a higher latency.  */
13171     case TYPE_LOAD:
13172       /* Swap loads to the front of the queue.  */
13173       mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
13174       break;
13175     case TYPE_STORE:
13176       /* Swap stores to the front of the queue.  */
13177       mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
13178       break;
13179     default:
13180       break;
13181     }
13182 }
13183 
13184 /* Implement TARGET_SCHED_INIT.  */
13185 
13186 static void
13187 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13188 		 int max_ready ATTRIBUTE_UNUSED)
13189 {
13190   mips_macc_chains_last_hilo = 0;
13191   vr4130_last_insn = 0;
13192   mips_74k_agen_init (NULL_RTX);
13193 
13194   /* When scheduling for Loongson2, branch instructions go to ALU1,
13195      therefore basic block is most likely to start with round-robin counter
13196      pointed to ALU2.  */
13197   mips_ls2.alu1_turn_p = false;
13198   mips_ls2.falu1_turn_p = true;
13199 }
13200 
13201 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2.  */
13202 
13203 static void
13204 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13205 		      rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13206 {
13207   if (!reload_completed
13208       && TUNE_MACC_CHAINS
13209       && *nreadyp > 0)
13210     mips_macc_chains_reorder (ready, *nreadyp);
13211 
13212   if (reload_completed
13213       && TUNE_MIPS4130
13214       && !TARGET_VR4130_ALIGN
13215       && *nreadyp > 1)
13216     vr4130_reorder (ready, *nreadyp);
13217 
13218   if (TUNE_74K)
13219     mips_74k_agen_reorder (ready, *nreadyp);
13220 }
13221 
13222 /* Implement TARGET_SCHED_REORDER.  */
13223 
13224 static int
13225 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13226 		    rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13227 {
13228   mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13229   return mips_issue_rate ();
13230 }
13231 
13232 /* Implement TARGET_SCHED_REORDER2.  */
13233 
13234 static int
13235 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13236 		     rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13237 {
13238   mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13239   return cached_can_issue_more;
13240 }
13241 
13242 /* Update round-robin counters for ALU1/2 and FALU1/2.  */
13243 
13244 static void
13245 mips_ls2_variable_issue (rtx insn)
13246 {
13247   if (mips_ls2.alu1_turn_p)
13248     {
13249       if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
13250 	mips_ls2.alu1_turn_p = false;
13251     }
13252   else
13253     {
13254       if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
13255 	mips_ls2.alu1_turn_p = true;
13256     }
13257 
13258   if (mips_ls2.falu1_turn_p)
13259     {
13260       if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
13261 	mips_ls2.falu1_turn_p = false;
13262     }
13263   else
13264     {
13265       if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
13266 	mips_ls2.falu1_turn_p = true;
13267     }
13268 
13269   if (recog_memoized (insn) >= 0)
13270     mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
13271 }
13272 
13273 /* Implement TARGET_SCHED_VARIABLE_ISSUE.  */
13274 
13275 static int
13276 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13277 		     rtx insn, int more)
13278 {
13279   /* Ignore USEs and CLOBBERs; don't count them against the issue rate.  */
13280   if (USEFUL_INSN_P (insn))
13281     {
13282       if (get_attr_type (insn) != TYPE_GHOST)
13283 	more--;
13284       if (!reload_completed && TUNE_MACC_CHAINS)
13285 	mips_macc_chains_record (insn);
13286       vr4130_last_insn = insn;
13287       if (TUNE_74K)
13288 	mips_74k_agen_init (insn);
13289       else if (TUNE_LOONGSON_2EF)
13290 	mips_ls2_variable_issue (insn);
13291     }
13292 
13293   /* Instructions of type 'multi' should all be split before
13294      the second scheduling pass.  */
13295   gcc_assert (!reload_completed
13296 	      || recog_memoized (insn) < 0
13297 	      || get_attr_type (insn) != TYPE_MULTI);
13298 
13299   cached_can_issue_more = more;
13300   return more;
13301 }
13302 
13303 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
13304    return the first operand of the associated PREF or PREFX insn.  */
13305 
13306 rtx
13307 mips_prefetch_cookie (rtx write, rtx locality)
13308 {
13309   /* store_streamed / load_streamed.  */
13310   if (INTVAL (locality) <= 0)
13311     return GEN_INT (INTVAL (write) + 4);
13312 
13313   /* store / load.  */
13314   if (INTVAL (locality) <= 2)
13315     return write;
13316 
13317   /* store_retained / load_retained.  */
13318   return GEN_INT (INTVAL (write) + 6);
13319 }
13320 
13321 /* Flags that indicate when a built-in function is available.
13322 
13323    BUILTIN_AVAIL_NON_MIPS16
13324 	The function is available on the current target, but only
13325 	in non-MIPS16 mode.  */
13326 #define BUILTIN_AVAIL_NON_MIPS16 1
13327 
13328 /* Declare an availability predicate for built-in functions that
13329    require non-MIPS16 mode and also require COND to be true.
13330    NAME is the main part of the predicate's name.  */
13331 #define AVAIL_NON_MIPS16(NAME, COND)					\
13332  static unsigned int							\
13333  mips_builtin_avail_##NAME (void)					\
13334  {									\
13335    return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0;			\
13336  }
13337 
13338 /* This structure describes a single built-in function.  */
13339 struct mips_builtin_description {
13340   /* The code of the main .md file instruction.  See mips_builtin_type
13341      for more information.  */
13342   enum insn_code icode;
13343 
13344   /* The floating-point comparison code to use with ICODE, if any.  */
13345   enum mips_fp_condition cond;
13346 
13347   /* The name of the built-in function.  */
13348   const char *name;
13349 
13350   /* Specifies how the function should be expanded.  */
13351   enum mips_builtin_type builtin_type;
13352 
13353   /* The function's prototype.  */
13354   enum mips_function_type function_type;
13355 
13356   /* Whether the function is available.  */
13357   unsigned int (*avail) (void);
13358 };
13359 
13360 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
13361 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
13362 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
13363 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
13364 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
13365 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
13366 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
13367 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
13368 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
13369 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
13370 
13371 /* Construct a mips_builtin_description from the given arguments.
13372 
13373    INSN is the name of the associated instruction pattern, without the
13374    leading CODE_FOR_mips_.
13375 
13376    CODE is the floating-point condition code associated with the
13377    function.  It can be 'f' if the field is not applicable.
13378 
13379    NAME is the name of the function itself, without the leading
13380    "__builtin_mips_".
13381 
13382    BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
13383 
13384    AVAIL is the name of the availability predicate, without the leading
13385    mips_builtin_avail_.  */
13386 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE,			\
13387 		     FUNCTION_TYPE, AVAIL)				\
13388   { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND,			\
13389     "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE,		\
13390     mips_builtin_avail_ ## AVAIL }
13391 
13392 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
13393    mapped to instruction CODE_FOR_mips_<INSN>,  FUNCTION_TYPE and AVAIL
13394    are as for MIPS_BUILTIN.  */
13395 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)			\
13396   MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
13397 
13398 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
13399    are subject to mips_builtin_avail_<AVAIL>.  */
13400 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL)				\
13401   MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s",		\
13402 		MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL),	\
13403   MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d",		\
13404 		MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
13405 
13406 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
13407    The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
13408    while the any and all forms are subject to mips_builtin_avail_mips3d.  */
13409 #define CMP_PS_BUILTINS(INSN, COND, AVAIL)				\
13410   MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps",	\
13411 		MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF,		\
13412 		mips3d),						\
13413   MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps",	\
13414 		MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF,		\
13415 		mips3d),						\
13416   MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps",	\
13417 		MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF,	\
13418 		AVAIL),							\
13419   MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps",	\
13420 		MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF,	\
13421 		AVAIL)
13422 
13423 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s.  The functions
13424    are subject to mips_builtin_avail_mips3d.  */
13425 #define CMP_4S_BUILTINS(INSN, COND)					\
13426   MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s",	\
13427 		MIPS_BUILTIN_CMP_ANY,					\
13428 		MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d),		\
13429   MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s",	\
13430 		MIPS_BUILTIN_CMP_ALL,					\
13431 		MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
13432 
13433 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps.  The comparison
13434    instruction requires mips_builtin_avail_<AVAIL>.  */
13435 #define MOVTF_BUILTINS(INSN, COND, AVAIL)				\
13436   MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps",	\
13437 		MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF,	\
13438 		AVAIL),							\
13439   MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps",	\
13440 		MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF,	\
13441 		AVAIL)
13442 
13443 /* Define all the built-in functions related to C.cond.fmt condition COND.  */
13444 #define CMP_BUILTINS(COND)						\
13445   MOVTF_BUILTINS (c, COND, paired_single),				\
13446   MOVTF_BUILTINS (cabs, COND, mips3d),					\
13447   CMP_SCALAR_BUILTINS (cabs, COND, mips3d),				\
13448   CMP_PS_BUILTINS (c, COND, paired_single),				\
13449   CMP_PS_BUILTINS (cabs, COND, mips3d),					\
13450   CMP_4S_BUILTINS (c, COND),						\
13451   CMP_4S_BUILTINS (cabs, COND)
13452 
13453 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
13454    function mapped to instruction CODE_FOR_mips_<INSN>,  FUNCTION_TYPE
13455    and AVAIL are as for MIPS_BUILTIN.  */
13456 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)		\
13457   MIPS_BUILTIN (INSN, f, #INSN,	MIPS_BUILTIN_DIRECT_NO_TARGET,		\
13458 		FUNCTION_TYPE, AVAIL)
13459 
13460 /* Define __builtin_mips_bposge<VALUE>.  <VALUE> is 32 for the MIPS32 DSP
13461    branch instruction.  AVAIL is as for MIPS_BUILTIN.  */
13462 #define BPOSGE_BUILTIN(VALUE, AVAIL)					\
13463   MIPS_BUILTIN (bposge, f, "bposge" #VALUE,				\
13464 		MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
13465 
13466 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
13467    for instruction CODE_FOR_loongson_<INSN>.  FUNCTION_TYPE is a
13468    builtin_description field.  */
13469 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE)		\
13470   { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f,				\
13471     "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT,		\
13472     FUNCTION_TYPE, mips_builtin_avail_loongson }
13473 
13474 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
13475    for instruction CODE_FOR_loongson_<INSN>.  FUNCTION_TYPE is a
13476    builtin_description field.  */
13477 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE)				\
13478   LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
13479 
13480 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
13481    We use functions of this form when the same insn can be usefully applied
13482    to more than one datatype.  */
13483 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE)		\
13484   LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
13485 
13486 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
13487 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
13488 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
13489 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
13490 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
13491 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
13492 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
13493 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
13494 
13495 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
13496 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
13497 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
13498 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
13499 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
13500 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
13501 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
13502 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
13503 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
13504 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
13505 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
13506 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
13507 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
13508 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
13509 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
13510 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
13511 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
13512 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
13513 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
13514 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
13515 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
13516 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
13517 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
13518 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
13519 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
13520 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
13521 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
13522 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
13523 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
13524 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
13525 
13526 static const struct mips_builtin_description mips_builtins[] = {
13527   DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13528   DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13529   DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13530   DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13531   DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
13532   DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
13533   DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
13534   DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
13535 
13536   DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
13537   DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13538   DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13539   DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13540   DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
13541 
13542   DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
13543   DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
13544   DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13545   DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13546   DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13547   DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13548 
13549   DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
13550   DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
13551   DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13552   DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13553   DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13554   DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13555 
13556   MIPS_FP_CONDITIONS (CMP_BUILTINS),
13557 
13558   /* Built-in functions for the SB-1 processor.  */
13559   DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
13560 
13561   /* Built-in functions for the DSP ASE (32-bit and 64-bit).  */
13562   DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13563   DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13564   DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13565   DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13566   DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13567   DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13568   DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13569   DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13570   DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13571   DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13572   DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
13573   DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
13574   DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
13575   DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
13576   DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
13577   DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
13578   DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13579   DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13580   DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13581   DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13582   DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
13583   DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
13584   DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13585   DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13586   DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13587   DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13588   DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13589   DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13590   DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13591   DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13592   DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13593   DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13594   DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13595   DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13596   DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13597   DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13598   DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13599   DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
13600   DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
13601   DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
13602   DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13603   DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
13604   DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
13605   DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
13606   DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
13607   DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
13608   DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
13609   DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13610   DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13611   DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13612   DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13613   DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13614   DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13615   DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13616   DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13617   DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13618   DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13619   DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13620   DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13621   DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
13622   DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
13623   DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
13624   DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
13625   DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
13626   BPOSGE_BUILTIN (32, dsp),
13627 
13628   /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit).  */
13629   DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
13630   DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13631   DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13632   DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13633   DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13634   DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13635   DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13636   DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13637   DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13638   DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13639   DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13640   DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13641   DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13642   DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13643   DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13644   DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
13645   DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
13646   DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
13647   DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13648   DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
13649   DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
13650   DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
13651   DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13652   DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13653   DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13654   DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13655   DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13656   DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13657   DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13658   DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13659   DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13660   DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13661   DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13662   DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13663 
13664   /* Built-in functions for the DSP ASE (32-bit only).  */
13665   DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13666   DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13667   DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13668   DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13669   DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13670   DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13671   DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13672   DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13673   DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13674   DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13675   DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13676   DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13677   DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13678   DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13679   DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13680   DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13681   DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
13682   DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
13683   DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
13684   DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
13685   DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
13686   DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13687   DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
13688   DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13689   DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
13690   DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
13691   DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
13692 
13693   /* Built-in functions for the DSP ASE (64-bit only).  */
13694   DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
13695 
13696   /* The following are for the MIPS DSP ASE REV 2 (32-bit only).  */
13697   DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13698   DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13699   DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13700   DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13701   DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13702   DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13703   DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13704   DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13705   DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13706 
13707   /* Builtin functions for ST Microelectronics Loongson-2E/2F cores.  */
13708   LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
13709   LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
13710   LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
13711   LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13712   LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13713   LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13714   LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13715   LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13716   LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13717   LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
13718   LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
13719   LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13720   LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
13721   LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13722   LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13723   LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
13724   LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13725   LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13726   LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13727   LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
13728   LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
13729   LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13730   LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
13731   LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13732   LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13733   LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13734   LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13735   LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13736   LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13737   LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13738   LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13739   LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13740   LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13741   LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13742   LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13743   LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13744   LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13745   LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
13746   LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
13747   LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13748   LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13749   LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13750   LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13751   LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13752   LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13753   LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13754   LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13755   LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
13756   LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13757   LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13758   LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13759   LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13760   LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
13761   LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
13762   LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13763   LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13764   LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13765   LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
13766   LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13767   LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
13768   LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
13769   LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
13770   LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
13771   LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
13772   LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
13773   LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
13774   LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
13775   LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
13776   LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
13777   LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
13778   LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
13779   LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
13780   LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
13781   LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
13782   LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
13783   LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13784   LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13785   LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13786   LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13787   LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13788   LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13789   LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
13790   LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
13791   LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13792   LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
13793   LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13794   LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13795   LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13796   LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13797   LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13798   LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13799   LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13800   LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13801   LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13802   LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13803   LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13804   LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13805   LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13806   LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13807 
13808   /* Sundry other built-in functions.  */
13809   DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
13810 };
13811 
13812 /* Index I is the function declaration for mips_builtins[I], or null if the
13813    function isn't defined on this target.  */
13814 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
13815 
13816 /* MODE is a vector mode whose elements have type TYPE.  Return the type
13817    of the vector itself.  */
13818 
13819 static tree
13820 mips_builtin_vector_type (tree type, enum machine_mode mode)
13821 {
13822   static tree types[2 * (int) MAX_MACHINE_MODE];
13823   int mode_index;
13824 
13825   mode_index = (int) mode;
13826 
13827   if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
13828     mode_index += MAX_MACHINE_MODE;
13829 
13830   if (types[mode_index] == NULL_TREE)
13831     types[mode_index] = build_vector_type_for_mode (type, mode);
13832   return types[mode_index];
13833 }
13834 
13835 /* Return a type for 'const volatile void *'.  */
13836 
13837 static tree
13838 mips_build_cvpointer_type (void)
13839 {
13840   static tree cache;
13841 
13842   if (cache == NULL_TREE)
13843     cache = build_pointer_type (build_qualified_type
13844 				(void_type_node,
13845 				 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
13846   return cache;
13847 }
13848 
13849 /* Source-level argument types.  */
13850 #define MIPS_ATYPE_VOID void_type_node
13851 #define MIPS_ATYPE_INT integer_type_node
13852 #define MIPS_ATYPE_POINTER ptr_type_node
13853 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
13854 
13855 /* Standard mode-based argument types.  */
13856 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
13857 #define MIPS_ATYPE_SI intSI_type_node
13858 #define MIPS_ATYPE_USI unsigned_intSI_type_node
13859 #define MIPS_ATYPE_DI intDI_type_node
13860 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
13861 #define MIPS_ATYPE_SF float_type_node
13862 #define MIPS_ATYPE_DF double_type_node
13863 
13864 /* Vector argument types.  */
13865 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
13866 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
13867 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
13868 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
13869 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
13870 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
13871 #define MIPS_ATYPE_UV2SI					\
13872   mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
13873 #define MIPS_ATYPE_UV4HI					\
13874   mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
13875 #define MIPS_ATYPE_UV8QI					\
13876   mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
13877 
13878 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
13879    their associated MIPS_ATYPEs.  */
13880 #define MIPS_FTYPE_ATYPES1(A, B) \
13881   MIPS_ATYPE_##A, MIPS_ATYPE_##B
13882 
13883 #define MIPS_FTYPE_ATYPES2(A, B, C) \
13884   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
13885 
13886 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
13887   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
13888 
13889 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
13890   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
13891   MIPS_ATYPE_##E
13892 
13893 /* Return the function type associated with function prototype TYPE.  */
13894 
13895 static tree
13896 mips_build_function_type (enum mips_function_type type)
13897 {
13898   static tree types[(int) MIPS_MAX_FTYPE_MAX];
13899 
13900   if (types[(int) type] == NULL_TREE)
13901     switch (type)
13902       {
13903 #define DEF_MIPS_FTYPE(NUM, ARGS)					\
13904   case MIPS_FTYPE_NAME##NUM ARGS:					\
13905     types[(int) type]							\
13906       = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS,		\
13907 				  NULL_TREE);				\
13908     break;
13909 #include "config/mips/mips-ftypes.def"
13910 #undef DEF_MIPS_FTYPE
13911       default:
13912 	gcc_unreachable ();
13913       }
13914 
13915   return types[(int) type];
13916 }
13917 
13918 /* Implement TARGET_INIT_BUILTINS.  */
13919 
13920 static void
13921 mips_init_builtins (void)
13922 {
13923   const struct mips_builtin_description *d;
13924   unsigned int i;
13925 
13926   /* Iterate through all of the bdesc arrays, initializing all of the
13927      builtin functions.  */
13928   for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
13929     {
13930       d = &mips_builtins[i];
13931       if (d->avail ())
13932 	mips_builtin_decls[i]
13933 	  = add_builtin_function (d->name,
13934 				  mips_build_function_type (d->function_type),
13935 				  i, BUILT_IN_MD, NULL, NULL);
13936     }
13937 }
13938 
13939 /* Implement TARGET_BUILTIN_DECL.  */
13940 
13941 static tree
13942 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
13943 {
13944   if (code >= ARRAY_SIZE (mips_builtins))
13945     return error_mark_node;
13946   return mips_builtin_decls[code];
13947 }
13948 
13949 /* Take argument ARGNO from EXP's argument list and convert it into
13950    an expand operand.  Store the operand in *OP.  */
13951 
13952 static void
13953 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
13954 			  unsigned int argno)
13955 {
13956   tree arg;
13957   rtx value;
13958 
13959   arg = CALL_EXPR_ARG (exp, argno);
13960   value = expand_normal (arg);
13961   create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
13962 }
13963 
13964 /* Expand instruction ICODE as part of a built-in function sequence.
13965    Use the first NOPS elements of OPS as the instruction's operands.
13966    HAS_TARGET_P is true if operand 0 is a target; it is false if the
13967    instruction has no target.
13968 
13969    Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx.  */
13970 
13971 static rtx
13972 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
13973 			  struct expand_operand *ops, bool has_target_p)
13974 {
13975   if (!maybe_expand_insn (icode, nops, ops))
13976     {
13977       error ("invalid argument to built-in function");
13978       return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
13979     }
13980   return has_target_p ? ops[0].value : const0_rtx;
13981 }
13982 
13983 /* Expand a floating-point comparison for built-in function call EXP.
13984    The first NARGS arguments are the values to be compared.  ICODE is
13985    the .md pattern that does the comparison and COND is the condition
13986    that is being tested.  Return an rtx for the result.  */
13987 
13988 static rtx
13989 mips_expand_builtin_compare_1 (enum insn_code icode,
13990 			       enum mips_fp_condition cond,
13991 			       tree exp, int nargs)
13992 {
13993   struct expand_operand ops[MAX_RECOG_OPERANDS];
13994   rtx output;
13995   int opno, argno;
13996 
13997   /* The instruction should have a target operand, an operand for each
13998      argument, and an operand for COND.  */
13999   gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
14000 
14001   output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
14002   opno = 0;
14003   create_fixed_operand (&ops[opno++], output);
14004   for (argno = 0; argno < nargs; argno++)
14005     mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14006   create_integer_operand (&ops[opno++], (int) cond);
14007   return mips_expand_builtin_insn (icode, opno, ops, true);
14008 }
14009 
14010 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
14011    HAS_TARGET_P says which.  EXP is the CALL_EXPR that calls the function
14012    and ICODE is the code of the associated .md pattern.  TARGET, if nonnull,
14013    suggests a good place to put the result.  */
14014 
14015 static rtx
14016 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
14017 			    bool has_target_p)
14018 {
14019   struct expand_operand ops[MAX_RECOG_OPERANDS];
14020   int opno, argno;
14021 
14022   /* Map any target to operand 0.  */
14023   opno = 0;
14024   if (has_target_p)
14025     create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
14026 
14027   /* Map the arguments to the other operands.  */
14028   gcc_assert (opno + call_expr_nargs (exp)
14029 	      == insn_data[icode].n_generator_args);
14030   for (argno = 0; argno < call_expr_nargs (exp); argno++)
14031     mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14032 
14033   return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
14034 }
14035 
14036 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
14037    function; TYPE says which.  EXP is the CALL_EXPR that calls the
14038    function, ICODE is the instruction that should be used to compare
14039    the first two arguments, and COND is the condition it should test.
14040    TARGET, if nonnull, suggests a good place to put the result.  */
14041 
14042 static rtx
14043 mips_expand_builtin_movtf (enum mips_builtin_type type,
14044 			   enum insn_code icode, enum mips_fp_condition cond,
14045 			   rtx target, tree exp)
14046 {
14047   struct expand_operand ops[4];
14048   rtx cmp_result;
14049 
14050   cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
14051   create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
14052   if (type == MIPS_BUILTIN_MOVT)
14053     {
14054       mips_prepare_builtin_arg (&ops[2], exp, 2);
14055       mips_prepare_builtin_arg (&ops[1], exp, 3);
14056     }
14057   else
14058     {
14059       mips_prepare_builtin_arg (&ops[1], exp, 2);
14060       mips_prepare_builtin_arg (&ops[2], exp, 3);
14061     }
14062   create_fixed_operand (&ops[3], cmp_result);
14063   return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
14064 				   4, ops, true);
14065 }
14066 
14067 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
14068    into TARGET otherwise.  Return TARGET.  */
14069 
14070 static rtx
14071 mips_builtin_branch_and_move (rtx condition, rtx target,
14072 			      rtx value_if_true, rtx value_if_false)
14073 {
14074   rtx true_label, done_label;
14075 
14076   true_label = gen_label_rtx ();
14077   done_label = gen_label_rtx ();
14078 
14079   /* First assume that CONDITION is false.  */
14080   mips_emit_move (target, value_if_false);
14081 
14082   /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise.  */
14083   emit_jump_insn (gen_condjump (condition, true_label));
14084   emit_jump_insn (gen_jump (done_label));
14085   emit_barrier ();
14086 
14087   /* Fix TARGET if CONDITION is true.  */
14088   emit_label (true_label);
14089   mips_emit_move (target, value_if_true);
14090 
14091   emit_label (done_label);
14092   return target;
14093 }
14094 
14095 /* Expand a comparison built-in function of type BUILTIN_TYPE.  EXP is
14096    the CALL_EXPR that calls the function, ICODE is the code of the
14097    comparison instruction, and COND is the condition it should test.
14098    TARGET, if nonnull, suggests a good place to put the boolean result.  */
14099 
14100 static rtx
14101 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
14102 			     enum insn_code icode, enum mips_fp_condition cond,
14103 			     rtx target, tree exp)
14104 {
14105   rtx offset, condition, cmp_result;
14106 
14107   if (target == 0 || GET_MODE (target) != SImode)
14108     target = gen_reg_rtx (SImode);
14109   cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
14110 					      call_expr_nargs (exp));
14111 
14112   /* If the comparison sets more than one register, we define the result
14113      to be 0 if all registers are false and -1 if all registers are true.
14114      The value of the complete result is indeterminate otherwise.  */
14115   switch (builtin_type)
14116     {
14117     case MIPS_BUILTIN_CMP_ALL:
14118       condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
14119       return mips_builtin_branch_and_move (condition, target,
14120 					   const0_rtx, const1_rtx);
14121 
14122     case MIPS_BUILTIN_CMP_UPPER:
14123     case MIPS_BUILTIN_CMP_LOWER:
14124       offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
14125       condition = gen_single_cc (cmp_result, offset);
14126       return mips_builtin_branch_and_move (condition, target,
14127 					   const1_rtx, const0_rtx);
14128 
14129     default:
14130       condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
14131       return mips_builtin_branch_and_move (condition, target,
14132 					   const1_rtx, const0_rtx);
14133     }
14134 }
14135 
14136 /* Expand a bposge built-in function of type BUILTIN_TYPE.  TARGET,
14137    if nonnull, suggests a good place to put the boolean result.  */
14138 
14139 static rtx
14140 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
14141 {
14142   rtx condition, cmp_result;
14143   int cmp_value;
14144 
14145   if (target == 0 || GET_MODE (target) != SImode)
14146     target = gen_reg_rtx (SImode);
14147 
14148   cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
14149 
14150   if (builtin_type == MIPS_BUILTIN_BPOSGE32)
14151     cmp_value = 32;
14152   else
14153     gcc_assert (0);
14154 
14155   condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
14156   return mips_builtin_branch_and_move (condition, target,
14157 				       const1_rtx, const0_rtx);
14158 }
14159 
14160 /* Implement TARGET_EXPAND_BUILTIN.  */
14161 
14162 static rtx
14163 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
14164 		     enum machine_mode mode, int ignore)
14165 {
14166   tree fndecl;
14167   unsigned int fcode, avail;
14168   const struct mips_builtin_description *d;
14169 
14170   fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14171   fcode = DECL_FUNCTION_CODE (fndecl);
14172   gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
14173   d = &mips_builtins[fcode];
14174   avail = d->avail ();
14175   gcc_assert (avail != 0);
14176   if (TARGET_MIPS16)
14177     {
14178       error ("built-in function %qE not supported for MIPS16",
14179 	     DECL_NAME (fndecl));
14180       return ignore ? const0_rtx : CONST0_RTX (mode);
14181     }
14182   switch (d->builtin_type)
14183     {
14184     case MIPS_BUILTIN_DIRECT:
14185       return mips_expand_builtin_direct (d->icode, target, exp, true);
14186 
14187     case MIPS_BUILTIN_DIRECT_NO_TARGET:
14188       return mips_expand_builtin_direct (d->icode, target, exp, false);
14189 
14190     case MIPS_BUILTIN_MOVT:
14191     case MIPS_BUILTIN_MOVF:
14192       return mips_expand_builtin_movtf (d->builtin_type, d->icode,
14193 					d->cond, target, exp);
14194 
14195     case MIPS_BUILTIN_CMP_ANY:
14196     case MIPS_BUILTIN_CMP_ALL:
14197     case MIPS_BUILTIN_CMP_UPPER:
14198     case MIPS_BUILTIN_CMP_LOWER:
14199     case MIPS_BUILTIN_CMP_SINGLE:
14200       return mips_expand_builtin_compare (d->builtin_type, d->icode,
14201 					  d->cond, target, exp);
14202 
14203     case MIPS_BUILTIN_BPOSGE32:
14204       return mips_expand_builtin_bposge (d->builtin_type, target);
14205     }
14206   gcc_unreachable ();
14207 }
14208 
14209 /* An entry in the MIPS16 constant pool.  VALUE is the pool constant,
14210    MODE is its mode, and LABEL is the CODE_LABEL associated with it.  */
14211 struct mips16_constant {
14212   struct mips16_constant *next;
14213   rtx value;
14214   rtx label;
14215   enum machine_mode mode;
14216 };
14217 
14218 /* Information about an incomplete MIPS16 constant pool.  FIRST is the
14219    first constant, HIGHEST_ADDRESS is the highest address that the first
14220    byte of the pool can have, and INSN_ADDRESS is the current instruction
14221    address.  */
14222 struct mips16_constant_pool {
14223   struct mips16_constant *first;
14224   int highest_address;
14225   int insn_address;
14226 };
14227 
14228 /* Add constant VALUE to POOL and return its label.  MODE is the
14229    value's mode (used for CONST_INTs, etc.).  */
14230 
14231 static rtx
14232 mips16_add_constant (struct mips16_constant_pool *pool,
14233 		     rtx value, enum machine_mode mode)
14234 {
14235   struct mips16_constant **p, *c;
14236   bool first_of_size_p;
14237 
14238   /* See whether the constant is already in the pool.  If so, return the
14239      existing label, otherwise leave P pointing to the place where the
14240      constant should be added.
14241 
14242      Keep the pool sorted in increasing order of mode size so that we can
14243      reduce the number of alignments needed.  */
14244   first_of_size_p = true;
14245   for (p = &pool->first; *p != 0; p = &(*p)->next)
14246     {
14247       if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
14248 	return (*p)->label;
14249       if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
14250 	break;
14251       if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
14252 	first_of_size_p = false;
14253     }
14254 
14255   /* In the worst case, the constant needed by the earliest instruction
14256      will end up at the end of the pool.  The entire pool must then be
14257      accessible from that instruction.
14258 
14259      When adding the first constant, set the pool's highest address to
14260      the address of the first out-of-range byte.  Adjust this address
14261      downwards each time a new constant is added.  */
14262   if (pool->first == 0)
14263     /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
14264        of the instruction with the lowest two bits clear.  The base PC
14265        value for LDPC has the lowest three bits clear.  Assume the worst
14266        case here; namely that the PC-relative instruction occupies the
14267        last 2 bytes in an aligned word.  */
14268     pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
14269   pool->highest_address -= GET_MODE_SIZE (mode);
14270   if (first_of_size_p)
14271     /* Take into account the worst possible padding due to alignment.  */
14272     pool->highest_address -= GET_MODE_SIZE (mode) - 1;
14273 
14274   /* Create a new entry.  */
14275   c = XNEW (struct mips16_constant);
14276   c->value = value;
14277   c->mode = mode;
14278   c->label = gen_label_rtx ();
14279   c->next = *p;
14280   *p = c;
14281 
14282   return c->label;
14283 }
14284 
14285 /* Output constant VALUE after instruction INSN and return the last
14286    instruction emitted.  MODE is the mode of the constant.  */
14287 
14288 static rtx
14289 mips16_emit_constants_1 (enum machine_mode mode, rtx value, rtx insn)
14290 {
14291   if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
14292     {
14293       rtx size = GEN_INT (GET_MODE_SIZE (mode));
14294       return emit_insn_after (gen_consttable_int (value, size), insn);
14295     }
14296 
14297   if (SCALAR_FLOAT_MODE_P (mode))
14298     return emit_insn_after (gen_consttable_float (value), insn);
14299 
14300   if (VECTOR_MODE_P (mode))
14301     {
14302       int i;
14303 
14304       for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
14305 	insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
14306 					CONST_VECTOR_ELT (value, i), insn);
14307       return insn;
14308     }
14309 
14310   gcc_unreachable ();
14311 }
14312 
14313 /* Dump out the constants in CONSTANTS after INSN.  */
14314 
14315 static void
14316 mips16_emit_constants (struct mips16_constant *constants, rtx insn)
14317 {
14318   struct mips16_constant *c, *next;
14319   int align;
14320 
14321   align = 0;
14322   for (c = constants; c != NULL; c = next)
14323     {
14324       /* If necessary, increase the alignment of PC.  */
14325       if (align < GET_MODE_SIZE (c->mode))
14326 	{
14327 	  int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
14328 	  insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
14329 	}
14330       align = GET_MODE_SIZE (c->mode);
14331 
14332       insn = emit_label_after (c->label, insn);
14333       insn = mips16_emit_constants_1 (c->mode, c->value, insn);
14334 
14335       next = c->next;
14336       free (c);
14337     }
14338 
14339   emit_barrier_after (insn);
14340 }
14341 
14342 /* Return the length of instruction INSN.  */
14343 
14344 static int
14345 mips16_insn_length (rtx insn)
14346 {
14347   if (JUMP_P (insn))
14348     {
14349       rtx body = PATTERN (insn);
14350       if (GET_CODE (body) == ADDR_VEC)
14351 	return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
14352       if (GET_CODE (body) == ADDR_DIFF_VEC)
14353 	return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
14354     }
14355   return get_attr_length (insn);
14356 }
14357 
14358 /* If *X is a symbolic constant that refers to the constant pool, add
14359    the constant to POOL and rewrite *X to use the constant's label.  */
14360 
14361 static void
14362 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
14363 {
14364   rtx base, offset, label;
14365 
14366   split_const (*x, &base, &offset);
14367   if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
14368     {
14369       label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
14370 				   get_pool_mode (base));
14371       base = gen_rtx_LABEL_REF (Pmode, label);
14372       *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
14373     }
14374 }
14375 
14376 /* This structure is used to communicate with mips16_rewrite_pool_refs.
14377    INSN is the instruction we're rewriting and POOL points to the current
14378    constant pool.  */
14379 struct mips16_rewrite_pool_refs_info {
14380   rtx insn;
14381   struct mips16_constant_pool *pool;
14382 };
14383 
14384 /* Rewrite *X so that constant pool references refer to the constant's
14385    label instead.  DATA points to a mips16_rewrite_pool_refs_info
14386    structure.  */
14387 
14388 static int
14389 mips16_rewrite_pool_refs (rtx *x, void *data)
14390 {
14391   struct mips16_rewrite_pool_refs_info *info =
14392     (struct mips16_rewrite_pool_refs_info *) data;
14393 
14394   if (force_to_mem_operand (*x, Pmode))
14395     {
14396       rtx mem = force_const_mem (GET_MODE (*x), *x);
14397       validate_change (info->insn, x, mem, false);
14398     }
14399 
14400   if (MEM_P (*x))
14401     {
14402       mips16_rewrite_pool_constant (info->pool, &XEXP (*x, 0));
14403       return -1;
14404     }
14405 
14406   /* Don't rewrite the __mips16_rdwr symbol.  */
14407   if (GET_CODE (*x) == UNSPEC && XINT (*x, 1) == UNSPEC_TLS_GET_TP)
14408     return -1;
14409 
14410   if (TARGET_MIPS16_TEXT_LOADS)
14411     mips16_rewrite_pool_constant (info->pool, x);
14412 
14413   return GET_CODE (*x) == CONST ? -1 : 0;
14414 }
14415 
14416 /* Return whether CFG is used in mips_reorg.  */
14417 
14418 static bool
14419 mips_cfg_in_reorg (void)
14420 {
14421   return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
14422 	  || TARGET_RELAX_PIC_CALLS);
14423 }
14424 
14425 /* Build MIPS16 constant pools.  Split the instructions if SPLIT_P,
14426    otherwise assume that they are already split.  */
14427 
14428 static void
14429 mips16_lay_out_constants (bool split_p)
14430 {
14431   struct mips16_constant_pool pool;
14432   struct mips16_rewrite_pool_refs_info info;
14433   rtx insn, barrier;
14434 
14435   if (!TARGET_MIPS16_PCREL_LOADS)
14436     return;
14437 
14438   if (split_p)
14439     {
14440       if (mips_cfg_in_reorg ())
14441 	split_all_insns ();
14442       else
14443 	split_all_insns_noflow ();
14444     }
14445   barrier = 0;
14446   memset (&pool, 0, sizeof (pool));
14447   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14448     {
14449       /* Rewrite constant pool references in INSN.  */
14450       if (USEFUL_INSN_P (insn))
14451 	{
14452 	  info.insn = insn;
14453 	  info.pool = &pool;
14454 	  for_each_rtx (&PATTERN (insn), mips16_rewrite_pool_refs, &info);
14455 	}
14456 
14457       pool.insn_address += mips16_insn_length (insn);
14458 
14459       if (pool.first != NULL)
14460 	{
14461 	  /* If there are no natural barriers between the first user of
14462 	     the pool and the highest acceptable address, we'll need to
14463 	     create a new instruction to jump around the constant pool.
14464 	     In the worst case, this instruction will be 4 bytes long.
14465 
14466 	     If it's too late to do this transformation after INSN,
14467 	     do it immediately before INSN.  */
14468 	  if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
14469 	    {
14470 	      rtx label, jump;
14471 
14472 	      label = gen_label_rtx ();
14473 
14474 	      jump = emit_jump_insn_before (gen_jump (label), insn);
14475 	      JUMP_LABEL (jump) = label;
14476 	      LABEL_NUSES (label) = 1;
14477 	      barrier = emit_barrier_after (jump);
14478 
14479 	      emit_label_after (label, barrier);
14480 	      pool.insn_address += 4;
14481 	    }
14482 
14483 	  /* See whether the constant pool is now out of range of the first
14484 	     user.  If so, output the constants after the previous barrier.
14485 	     Note that any instructions between BARRIER and INSN (inclusive)
14486 	     will use negative offsets to refer to the pool.  */
14487 	  if (pool.insn_address > pool.highest_address)
14488 	    {
14489 	      mips16_emit_constants (pool.first, barrier);
14490 	      pool.first = NULL;
14491 	      barrier = 0;
14492 	    }
14493 	  else if (BARRIER_P (insn))
14494 	    barrier = insn;
14495 	}
14496     }
14497   mips16_emit_constants (pool.first, get_last_insn ());
14498 }
14499 
14500 /* Return true if it is worth r10k_simplify_address's while replacing
14501    an address with X.  We are looking for constants, and for addresses
14502    at a known offset from the incoming stack pointer.  */
14503 
14504 static bool
14505 r10k_simplified_address_p (rtx x)
14506 {
14507   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
14508     x = XEXP (x, 0);
14509   return x == virtual_incoming_args_rtx || CONSTANT_P (x);
14510 }
14511 
14512 /* X is an expression that appears in INSN.  Try to use the UD chains
14513    to simplify it, returning the simplified form on success and the
14514    original form otherwise.  Replace the incoming value of $sp with
14515    virtual_incoming_args_rtx (which should never occur in X otherwise).  */
14516 
14517 static rtx
14518 r10k_simplify_address (rtx x, rtx insn)
14519 {
14520   rtx newx, op0, op1, set, def_insn, note;
14521   df_ref use, def;
14522   struct df_link *defs;
14523 
14524   newx = NULL_RTX;
14525   if (UNARY_P (x))
14526     {
14527       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14528       if (op0 != XEXP (x, 0))
14529 	newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
14530 				   op0, GET_MODE (XEXP (x, 0)));
14531     }
14532   else if (BINARY_P (x))
14533     {
14534       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14535       op1 = r10k_simplify_address (XEXP (x, 1), insn);
14536       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
14537 	newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
14538     }
14539   else if (GET_CODE (x) == LO_SUM)
14540     {
14541       /* LO_SUMs can be offset from HIGHs, if we know they won't
14542 	 overflow.  See mips_classify_address for the rationale behind
14543 	 the lax check.  */
14544       op0 = r10k_simplify_address (XEXP (x, 0), insn);
14545       if (GET_CODE (op0) == HIGH)
14546 	newx = XEXP (x, 1);
14547     }
14548   else if (REG_P (x))
14549     {
14550       /* Uses are recorded by regno_reg_rtx, not X itself.  */
14551       use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
14552       gcc_assert (use);
14553       defs = DF_REF_CHAIN (use);
14554 
14555       /* Require a single definition.  */
14556       if (defs && defs->next == NULL)
14557 	{
14558 	  def = defs->ref;
14559 	  if (DF_REF_IS_ARTIFICIAL (def))
14560 	    {
14561 	      /* Replace the incoming value of $sp with
14562 		 virtual_incoming_args_rtx.  */
14563 	      if (x == stack_pointer_rtx
14564 		  && DF_REF_BB (def) == ENTRY_BLOCK_PTR)
14565 		newx = virtual_incoming_args_rtx;
14566 	    }
14567 	  else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
14568 				   DF_REF_BB (def)))
14569 	    {
14570 	      /* Make sure that DEF_INSN is a single set of REG.  */
14571 	      def_insn = DF_REF_INSN (def);
14572 	      if (NONJUMP_INSN_P (def_insn))
14573 		{
14574 		  set = single_set (def_insn);
14575 		  if (set && rtx_equal_p (SET_DEST (set), x))
14576 		    {
14577 		      /* Prefer to use notes, since the def-use chains
14578 			 are often shorter.  */
14579 		      note = find_reg_equal_equiv_note (def_insn);
14580 		      if (note)
14581 			newx = XEXP (note, 0);
14582 		      else
14583 			newx = SET_SRC (set);
14584 		      newx = r10k_simplify_address (newx, def_insn);
14585 		    }
14586 		}
14587 	    }
14588 	}
14589     }
14590   if (newx && r10k_simplified_address_p (newx))
14591     return newx;
14592   return x;
14593 }
14594 
14595 /* Return true if ADDRESS is known to be an uncached address
14596    on R10K systems.  */
14597 
14598 static bool
14599 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
14600 {
14601   unsigned HOST_WIDE_INT upper;
14602 
14603   /* Check for KSEG1.  */
14604   if (address + 0x60000000 < 0x20000000)
14605     return true;
14606 
14607   /* Check for uncached XKPHYS addresses.  */
14608   if (Pmode == DImode)
14609     {
14610       upper = (address >> 40) & 0xf9ffff;
14611       if (upper == 0x900000 || upper == 0xb80000)
14612 	return true;
14613     }
14614   return false;
14615 }
14616 
14617 /* Return true if we can prove that an access to address X in instruction
14618    INSN would be safe from R10K speculation.  This X is a general
14619    expression; it might not be a legitimate address.  */
14620 
14621 static bool
14622 r10k_safe_address_p (rtx x, rtx insn)
14623 {
14624   rtx base, offset;
14625   HOST_WIDE_INT offset_val;
14626 
14627   x = r10k_simplify_address (x, insn);
14628 
14629   /* Check for references to the stack frame.  It doesn't really matter
14630      how much of the frame has been allocated at INSN; -mr10k-cache-barrier
14631      allows us to assume that accesses to any part of the eventual frame
14632      is safe from speculation at any point in the function.  */
14633   mips_split_plus (x, &base, &offset_val);
14634   if (base == virtual_incoming_args_rtx
14635       && offset_val >= -cfun->machine->frame.total_size
14636       && offset_val < cfun->machine->frame.args_size)
14637     return true;
14638 
14639   /* Check for uncached addresses.  */
14640   if (CONST_INT_P (x))
14641     return r10k_uncached_address_p (INTVAL (x));
14642 
14643   /* Check for accesses to a static object.  */
14644   split_const (x, &base, &offset);
14645   return offset_within_block_p (base, INTVAL (offset));
14646 }
14647 
14648 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
14649    an in-range access to an automatic variable, or to an object with
14650    a link-time-constant address.  */
14651 
14652 static bool
14653 r10k_safe_mem_expr_p (tree expr, HOST_WIDE_INT offset)
14654 {
14655   HOST_WIDE_INT bitoffset, bitsize;
14656   tree inner, var_offset;
14657   enum machine_mode mode;
14658   int unsigned_p, volatile_p;
14659 
14660   inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
14661 			       &unsigned_p, &volatile_p, false);
14662   if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
14663     return false;
14664 
14665   offset += bitoffset / BITS_PER_UNIT;
14666   return offset >= 0 && offset < tree_low_cst (DECL_SIZE_UNIT (inner), 1);
14667 }
14668 
14669 /* A for_each_rtx callback for which DATA points to the instruction
14670    containing *X.  Stop the search if we find a MEM that is not safe
14671    from R10K speculation.  */
14672 
14673 static int
14674 r10k_needs_protection_p_1 (rtx *loc, void *data)
14675 {
14676   rtx mem;
14677 
14678   mem = *loc;
14679   if (!MEM_P (mem))
14680     return 0;
14681 
14682   if (MEM_EXPR (mem)
14683       && MEM_OFFSET_KNOWN_P (mem)
14684       && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
14685     return -1;
14686 
14687   if (r10k_safe_address_p (XEXP (mem, 0), (rtx) data))
14688     return -1;
14689 
14690   return 1;
14691 }
14692 
14693 /* A note_stores callback for which DATA points to an instruction pointer.
14694    If *DATA is nonnull, make it null if it X contains a MEM that is not
14695    safe from R10K speculation.  */
14696 
14697 static void
14698 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
14699 			       void *data)
14700 {
14701   rtx *insn_ptr;
14702 
14703   insn_ptr = (rtx *) data;
14704   if (*insn_ptr && for_each_rtx (&x, r10k_needs_protection_p_1, *insn_ptr))
14705     *insn_ptr = NULL_RTX;
14706 }
14707 
14708 /* A for_each_rtx callback that iterates over the pattern of a CALL_INSN.
14709    Return nonzero if the call is not to a declared function.  */
14710 
14711 static int
14712 r10k_needs_protection_p_call (rtx *loc, void *data ATTRIBUTE_UNUSED)
14713 {
14714   rtx x;
14715 
14716   x = *loc;
14717   if (!MEM_P (x))
14718     return 0;
14719 
14720   x = XEXP (x, 0);
14721   if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DECL (x))
14722     return -1;
14723 
14724   return 1;
14725 }
14726 
14727 /* Return true if instruction INSN needs to be protected by an R10K
14728    cache barrier.  */
14729 
14730 static bool
14731 r10k_needs_protection_p (rtx insn)
14732 {
14733   if (CALL_P (insn))
14734     return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_call, NULL);
14735 
14736   if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
14737     {
14738       note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
14739       return insn == NULL_RTX;
14740     }
14741 
14742   return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_1, insn);
14743 }
14744 
14745 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
14746    edge is unconditional.  */
14747 
14748 static bool
14749 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
14750 {
14751   edge_iterator ei;
14752   edge e;
14753 
14754   FOR_EACH_EDGE (e, ei, bb->preds)
14755     if (!single_succ_p (e->src)
14756 	|| !bitmap_bit_p (protected_bbs, e->src->index)
14757 	|| (e->flags & EDGE_COMPLEX) != 0)
14758       return false;
14759   return true;
14760 }
14761 
14762 /* Implement -mr10k-cache-barrier= for the current function.  */
14763 
14764 static void
14765 r10k_insert_cache_barriers (void)
14766 {
14767   int *rev_post_order;
14768   unsigned int i, n;
14769   basic_block bb;
14770   sbitmap protected_bbs;
14771   rtx insn, end, unprotected_region;
14772 
14773   if (TARGET_MIPS16)
14774     {
14775       sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
14776       return;
14777     }
14778 
14779   /* Calculate dominators.  */
14780   calculate_dominance_info (CDI_DOMINATORS);
14781 
14782   /* Bit X of PROTECTED_BBS is set if the last operation in basic block
14783      X is protected by a cache barrier.  */
14784   protected_bbs = sbitmap_alloc (last_basic_block);
14785   bitmap_clear (protected_bbs);
14786 
14787   /* Iterate over the basic blocks in reverse post-order.  */
14788   rev_post_order = XNEWVEC (int, last_basic_block);
14789   n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
14790   for (i = 0; i < n; i++)
14791     {
14792       bb = BASIC_BLOCK (rev_post_order[i]);
14793 
14794       /* If this block is only reached by unconditional edges, and if the
14795 	 source of every edge is protected, the beginning of the block is
14796 	 also protected.  */
14797       if (r10k_protected_bb_p (bb, protected_bbs))
14798 	unprotected_region = NULL_RTX;
14799       else
14800 	unprotected_region = pc_rtx;
14801       end = NEXT_INSN (BB_END (bb));
14802 
14803       /* UNPROTECTED_REGION is:
14804 
14805 	 - null if we are processing a protected region,
14806 	 - pc_rtx if we are processing an unprotected region but have
14807 	   not yet found the first instruction in it
14808 	 - the first instruction in an unprotected region otherwise.  */
14809       for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
14810 	{
14811 	  if (unprotected_region && USEFUL_INSN_P (insn))
14812 	    {
14813 	      if (recog_memoized (insn) == CODE_FOR_mips_cache)
14814 		/* This CACHE instruction protects the following code.  */
14815 		unprotected_region = NULL_RTX;
14816 	      else
14817 		{
14818 		  /* See if INSN is the first instruction in this
14819 		     unprotected region.  */
14820 		  if (unprotected_region == pc_rtx)
14821 		    unprotected_region = insn;
14822 
14823 		  /* See if INSN needs to be protected.  If so,
14824 		     we must insert a cache barrier somewhere between
14825 		     PREV_INSN (UNPROTECTED_REGION) and INSN.  It isn't
14826 		     clear which position is better performance-wise,
14827 		     but as a tie-breaker, we assume that it is better
14828 		     to allow delay slots to be back-filled where
14829 		     possible, and that it is better not to insert
14830 		     barriers in the middle of already-scheduled code.
14831 		     We therefore insert the barrier at the beginning
14832 		     of the region.  */
14833 		  if (r10k_needs_protection_p (insn))
14834 		    {
14835 		      emit_insn_before (gen_r10k_cache_barrier (),
14836 					unprotected_region);
14837 		      unprotected_region = NULL_RTX;
14838 		    }
14839 		}
14840 	    }
14841 
14842 	  if (CALL_P (insn))
14843 	    /* The called function is not required to protect the exit path.
14844 	       The code that follows a call is therefore unprotected.  */
14845 	    unprotected_region = pc_rtx;
14846 	}
14847 
14848       /* Record whether the end of this block is protected.  */
14849       if (unprotected_region == NULL_RTX)
14850 	bitmap_set_bit (protected_bbs, bb->index);
14851     }
14852   XDELETEVEC (rev_post_order);
14853 
14854   sbitmap_free (protected_bbs);
14855 
14856   free_dominance_info (CDI_DOMINATORS);
14857 }
14858 
14859 /* If INSN is a call, return the underlying CALL expr.  Return NULL_RTX
14860    otherwise.  If INSN has two call rtx, then store the second one in
14861    SECOND_CALL.  */
14862 
14863 static rtx
14864 mips_call_expr_from_insn (rtx insn, rtx *second_call)
14865 {
14866   rtx x;
14867   rtx x2;
14868 
14869   if (!CALL_P (insn))
14870     return NULL_RTX;
14871 
14872   x = PATTERN (insn);
14873   if (GET_CODE (x) == PARALLEL)
14874     {
14875       /* Calls returning complex values have two CALL rtx.  Look for the second
14876 	 one here, and return it via the SECOND_CALL arg.  */
14877       x2 = XVECEXP (x, 0, 1);
14878       if (GET_CODE (x2) == SET)
14879 	x2 = XEXP (x2, 1);
14880       if (GET_CODE (x2) == CALL)
14881 	*second_call = x2;
14882 
14883       x = XVECEXP (x, 0, 0);
14884     }
14885   if (GET_CODE (x) == SET)
14886     x = XEXP (x, 1);
14887   gcc_assert (GET_CODE (x) == CALL);
14888 
14889   return x;
14890 }
14891 
14892 /* REG is set in DEF.  See if the definition is one of the ways we load a
14893    register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
14894    If it is, return the symbol reference of the function, otherwise return
14895    NULL_RTX.
14896 
14897    If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
14898    the values of source registers, otherwise treat such registers as
14899    having an unknown value.  */
14900 
14901 static rtx
14902 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
14903 {
14904   rtx def_insn, set;
14905 
14906   if (DF_REF_IS_ARTIFICIAL (def))
14907     return NULL_RTX;
14908 
14909   def_insn = DF_REF_INSN (def);
14910   set = single_set (def_insn);
14911   if (set && rtx_equal_p (SET_DEST (set), reg))
14912     {
14913       rtx note, src, symbol;
14914 
14915       /* First see whether the source is a plain symbol.  This is used
14916 	 when calling symbols that are not lazily bound.  */
14917       src = SET_SRC (set);
14918       if (GET_CODE (src) == SYMBOL_REF)
14919 	return src;
14920 
14921       /* Handle %call16 references.  */
14922       symbol = mips_strip_unspec_call (src);
14923       if (symbol)
14924 	{
14925 	  gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
14926 	  return symbol;
14927 	}
14928 
14929       /* If we have something more complicated, look for a
14930 	 REG_EQUAL or REG_EQUIV note.  */
14931       note = find_reg_equal_equiv_note (def_insn);
14932       if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
14933 	return XEXP (note, 0);
14934 
14935       /* Follow at most one simple register copy.  Such copies are
14936 	 interesting in cases like:
14937 
14938 	     for (...)
14939 	       {
14940 	         locally_binding_fn (...);
14941 	       }
14942 
14943 	 and:
14944 
14945 	     locally_binding_fn (...);
14946 	     ...
14947 	     locally_binding_fn (...);
14948 
14949 	 where the load of locally_binding_fn can legitimately be
14950 	 hoisted or shared.  However, we do not expect to see complex
14951 	 chains of copies, so a full worklist solution to the problem
14952 	 would probably be overkill.  */
14953       if (recurse_p && REG_P (src))
14954 	return mips_find_pic_call_symbol (def_insn, src, false);
14955     }
14956 
14957   return NULL_RTX;
14958 }
14959 
14960 /* Find the definition of the use of REG in INSN.  See if the definition
14961    is one of the ways we load a register with a symbol address for a
14962    mips_use_pic_fn_addr_reg_p call.  If it is return the symbol reference
14963    of the function, otherwise return NULL_RTX.  RECURSE_P is as for
14964    mips_pic_call_symbol_from_set.  */
14965 
14966 static rtx
14967 mips_find_pic_call_symbol (rtx insn, rtx reg, bool recurse_p)
14968 {
14969   df_ref use;
14970   struct df_link *defs;
14971   rtx symbol;
14972 
14973   use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
14974   if (!use)
14975     return NULL_RTX;
14976   defs = DF_REF_CHAIN (use);
14977   if (!defs)
14978     return NULL_RTX;
14979   symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
14980   if (!symbol)
14981     return NULL_RTX;
14982 
14983   /* If we have more than one definition, they need to be identical.  */
14984   for (defs = defs->next; defs; defs = defs->next)
14985     {
14986       rtx other;
14987 
14988       other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
14989       if (!rtx_equal_p (symbol, other))
14990 	return NULL_RTX;
14991     }
14992 
14993   return symbol;
14994 }
14995 
14996 /* Replace the args_size operand of the call expression CALL with the
14997    call-attribute UNSPEC and fill in SYMBOL as the function symbol.  */
14998 
14999 static void
15000 mips_annotate_pic_call_expr (rtx call, rtx symbol)
15001 {
15002   rtx args_size;
15003 
15004   args_size = XEXP (call, 1);
15005   XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
15006 				   gen_rtvec (2, args_size, symbol),
15007 				   UNSPEC_CALL_ATTR);
15008 }
15009 
15010 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression.  See
15011    if instead of the arg_size argument it contains the call attributes.  If
15012    yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
15013    symbol from the call attributes.  Also return false if ARGS_SIZE_OPNO is
15014    -1.  */
15015 
15016 bool
15017 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
15018 {
15019   rtx args_size, symbol;
15020 
15021   if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
15022     return false;
15023 
15024   args_size = operands[args_size_opno];
15025   if (GET_CODE (args_size) != UNSPEC)
15026     return false;
15027   gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
15028 
15029   symbol = XVECEXP (args_size, 0, 1);
15030   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15031 
15032   operands[args_size_opno] = symbol;
15033   return true;
15034 }
15035 
15036 /* Use DF to annotate PIC indirect calls with the function symbol they
15037    dispatch to.  */
15038 
15039 static void
15040 mips_annotate_pic_calls (void)
15041 {
15042   basic_block bb;
15043   rtx insn;
15044 
15045   FOR_EACH_BB (bb)
15046     FOR_BB_INSNS (bb, insn)
15047     {
15048       rtx call, reg, symbol, second_call;
15049 
15050       second_call = 0;
15051       call = mips_call_expr_from_insn (insn, &second_call);
15052       if (!call)
15053 	continue;
15054       gcc_assert (MEM_P (XEXP (call, 0)));
15055       reg = XEXP (XEXP (call, 0), 0);
15056       if (!REG_P (reg))
15057 	continue;
15058 
15059       symbol = mips_find_pic_call_symbol (insn, reg, true);
15060       if (symbol)
15061 	{
15062 	  mips_annotate_pic_call_expr (call, symbol);
15063 	  if (second_call)
15064 	    mips_annotate_pic_call_expr (second_call, symbol);
15065 	}
15066     }
15067 }
15068 
15069 /* A temporary variable used by for_each_rtx callbacks, etc.  */
15070 static rtx mips_sim_insn;
15071 
15072 /* A structure representing the state of the processor pipeline.
15073    Used by the mips_sim_* family of functions.  */
15074 struct mips_sim {
15075   /* The maximum number of instructions that can be issued in a cycle.
15076      (Caches mips_issue_rate.)  */
15077   unsigned int issue_rate;
15078 
15079   /* The current simulation time.  */
15080   unsigned int time;
15081 
15082   /* How many more instructions can be issued in the current cycle.  */
15083   unsigned int insns_left;
15084 
15085   /* LAST_SET[X].INSN is the last instruction to set register X.
15086      LAST_SET[X].TIME is the time at which that instruction was issued.
15087      INSN is null if no instruction has yet set register X.  */
15088   struct {
15089     rtx insn;
15090     unsigned int time;
15091   } last_set[FIRST_PSEUDO_REGISTER];
15092 
15093   /* The pipeline's current DFA state.  */
15094   state_t dfa_state;
15095 };
15096 
15097 /* Reset STATE to the initial simulation state.  */
15098 
15099 static void
15100 mips_sim_reset (struct mips_sim *state)
15101 {
15102   curr_state = state->dfa_state;
15103 
15104   state->time = 0;
15105   state->insns_left = state->issue_rate;
15106   memset (&state->last_set, 0, sizeof (state->last_set));
15107   state_reset (curr_state);
15108 
15109   targetm.sched.init (0, false, 0);
15110   advance_state (curr_state);
15111 }
15112 
15113 /* Initialize STATE before its first use.  DFA_STATE points to an
15114    allocated but uninitialized DFA state.  */
15115 
15116 static void
15117 mips_sim_init (struct mips_sim *state, state_t dfa_state)
15118 {
15119   if (targetm.sched.init_dfa_pre_cycle_insn)
15120     targetm.sched.init_dfa_pre_cycle_insn ();
15121 
15122   if (targetm.sched.init_dfa_post_cycle_insn)
15123     targetm.sched.init_dfa_post_cycle_insn ();
15124 
15125   state->issue_rate = mips_issue_rate ();
15126   state->dfa_state = dfa_state;
15127   mips_sim_reset (state);
15128 }
15129 
15130 /* Advance STATE by one clock cycle.  */
15131 
15132 static void
15133 mips_sim_next_cycle (struct mips_sim *state)
15134 {
15135   curr_state = state->dfa_state;
15136 
15137   state->time++;
15138   state->insns_left = state->issue_rate;
15139   advance_state (curr_state);
15140 }
15141 
15142 /* Advance simulation state STATE until instruction INSN can read
15143    register REG.  */
15144 
15145 static void
15146 mips_sim_wait_reg (struct mips_sim *state, rtx insn, rtx reg)
15147 {
15148   unsigned int regno, end_regno;
15149 
15150   end_regno = END_REGNO (reg);
15151   for (regno = REGNO (reg); regno < end_regno; regno++)
15152     if (state->last_set[regno].insn != 0)
15153       {
15154 	unsigned int t;
15155 
15156 	t = (state->last_set[regno].time
15157 	     + insn_latency (state->last_set[regno].insn, insn));
15158 	while (state->time < t)
15159 	  mips_sim_next_cycle (state);
15160     }
15161 }
15162 
15163 /* A for_each_rtx callback.  If *X is a register, advance simulation state
15164    DATA until mips_sim_insn can read the register's value.  */
15165 
15166 static int
15167 mips_sim_wait_regs_2 (rtx *x, void *data)
15168 {
15169   if (REG_P (*x))
15170     mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *x);
15171   return 0;
15172 }
15173 
15174 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X.  */
15175 
15176 static void
15177 mips_sim_wait_regs_1 (rtx *x, void *data)
15178 {
15179   for_each_rtx (x, mips_sim_wait_regs_2, data);
15180 }
15181 
15182 /* Advance simulation state STATE until all of INSN's register
15183    dependencies are satisfied.  */
15184 
15185 static void
15186 mips_sim_wait_regs (struct mips_sim *state, rtx insn)
15187 {
15188   mips_sim_insn = insn;
15189   note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
15190 }
15191 
15192 /* Advance simulation state STATE until the units required by
15193    instruction INSN are available.  */
15194 
15195 static void
15196 mips_sim_wait_units (struct mips_sim *state, rtx insn)
15197 {
15198   state_t tmp_state;
15199 
15200   tmp_state = alloca (state_size ());
15201   while (state->insns_left == 0
15202 	 || (memcpy (tmp_state, state->dfa_state, state_size ()),
15203 	     state_transition (tmp_state, insn) >= 0))
15204     mips_sim_next_cycle (state);
15205 }
15206 
15207 /* Advance simulation state STATE until INSN is ready to issue.  */
15208 
15209 static void
15210 mips_sim_wait_insn (struct mips_sim *state, rtx insn)
15211 {
15212   mips_sim_wait_regs (state, insn);
15213   mips_sim_wait_units (state, insn);
15214 }
15215 
15216 /* mips_sim_insn has just set X.  Update the LAST_SET array
15217    in simulation state DATA.  */
15218 
15219 static void
15220 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
15221 {
15222   struct mips_sim *state;
15223 
15224   state = (struct mips_sim *) data;
15225   if (REG_P (x))
15226     {
15227       unsigned int regno, end_regno;
15228 
15229       end_regno = END_REGNO (x);
15230       for (regno = REGNO (x); regno < end_regno; regno++)
15231 	{
15232 	  state->last_set[regno].insn = mips_sim_insn;
15233 	  state->last_set[regno].time = state->time;
15234 	}
15235     }
15236 }
15237 
15238 /* Issue instruction INSN in scheduler state STATE.  Assume that INSN
15239    can issue immediately (i.e., that mips_sim_wait_insn has already
15240    been called).  */
15241 
15242 static void
15243 mips_sim_issue_insn (struct mips_sim *state, rtx insn)
15244 {
15245   curr_state = state->dfa_state;
15246 
15247   state_transition (curr_state, insn);
15248   state->insns_left = targetm.sched.variable_issue (0, false, insn,
15249 						    state->insns_left);
15250 
15251   mips_sim_insn = insn;
15252   note_stores (PATTERN (insn), mips_sim_record_set, state);
15253 }
15254 
15255 /* Simulate issuing a NOP in state STATE.  */
15256 
15257 static void
15258 mips_sim_issue_nop (struct mips_sim *state)
15259 {
15260   if (state->insns_left == 0)
15261     mips_sim_next_cycle (state);
15262   state->insns_left--;
15263 }
15264 
15265 /* Update simulation state STATE so that it's ready to accept the instruction
15266    after INSN.  INSN should be part of the main rtl chain, not a member of a
15267    SEQUENCE.  */
15268 
15269 static void
15270 mips_sim_finish_insn (struct mips_sim *state, rtx insn)
15271 {
15272   /* If INSN is a jump with an implicit delay slot, simulate a nop.  */
15273   if (JUMP_P (insn))
15274     mips_sim_issue_nop (state);
15275 
15276   switch (GET_CODE (SEQ_BEGIN (insn)))
15277     {
15278     case CODE_LABEL:
15279     case CALL_INSN:
15280       /* We can't predict the processor state after a call or label.  */
15281       mips_sim_reset (state);
15282       break;
15283 
15284     case JUMP_INSN:
15285       /* The delay slots of branch likely instructions are only executed
15286 	 when the branch is taken.  Therefore, if the caller has simulated
15287 	 the delay slot instruction, STATE does not really reflect the state
15288 	 of the pipeline for the instruction after the delay slot.  Also,
15289 	 branch likely instructions tend to incur a penalty when not taken,
15290 	 so there will probably be an extra delay between the branch and
15291 	 the instruction after the delay slot.  */
15292       if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
15293 	mips_sim_reset (state);
15294       break;
15295 
15296     default:
15297       break;
15298     }
15299 }
15300 
15301 /* Use simulator state STATE to calculate the execution time of
15302    instruction sequence SEQ.  */
15303 
15304 static unsigned int
15305 mips_seq_time (struct mips_sim *state, rtx seq)
15306 {
15307   mips_sim_reset (state);
15308   for (rtx insn = seq; insn; insn = NEXT_INSN (insn))
15309     {
15310       mips_sim_wait_insn (state, insn);
15311       mips_sim_issue_insn (state, insn);
15312     }
15313   return state->time;
15314 }
15315 
15316 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
15317    setting SETTING, using STATE to simulate instruction sequences.  */
15318 
15319 static unsigned int
15320 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
15321 {
15322   mips_tuning_info.fast_mult_zero_zero_p = setting;
15323   start_sequence ();
15324 
15325   enum machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
15326   rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
15327   mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
15328 
15329   /* If the target provides mulsidi3_32bit then that's the most likely
15330      consumer of the result.  Test for bypasses.  */
15331   if (dword_mode == DImode && HAVE_maddsidi4)
15332     {
15333       rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
15334       emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
15335     }
15336 
15337   unsigned int time = mips_seq_time (state, get_insns ());
15338   end_sequence ();
15339   return time;
15340 }
15341 
15342 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
15343    and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
15344    Prefer MULT -- which is shorter -- in the event of a tie.  */
15345 
15346 static void
15347 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
15348 {
15349   if (TARGET_MIPS16)
15350     /* No MTLO or MTHI available.  */
15351     mips_tuning_info.fast_mult_zero_zero_p = true;
15352   else
15353     {
15354       unsigned int true_time = mips_mult_zero_zero_cost (state, true);
15355       unsigned int false_time = mips_mult_zero_zero_cost (state, false);
15356       mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
15357     }
15358 }
15359 
15360 /* Set up costs based on the current architecture and tuning settings.  */
15361 
15362 static void
15363 mips_set_tuning_info (void)
15364 {
15365   if (mips_tuning_info.initialized_p
15366       && mips_tuning_info.arch == mips_arch
15367       && mips_tuning_info.tune == mips_tune
15368       && mips_tuning_info.mips16_p == TARGET_MIPS16)
15369     return;
15370 
15371   mips_tuning_info.arch = mips_arch;
15372   mips_tuning_info.tune = mips_tune;
15373   mips_tuning_info.mips16_p = TARGET_MIPS16;
15374   mips_tuning_info.initialized_p = true;
15375 
15376   dfa_start ();
15377 
15378   struct mips_sim state;
15379   mips_sim_init (&state, alloca (state_size ()));
15380 
15381   mips_set_fast_mult_zero_zero_p (&state);
15382 
15383   dfa_finish ();
15384 }
15385 
15386 /* Implement TARGET_EXPAND_TO_RTL_HOOK.  */
15387 
15388 static void
15389 mips_expand_to_rtl_hook (void)
15390 {
15391   /* We need to call this at a point where we can safely create sequences
15392      of instructions, so TARGET_OVERRIDE_OPTIONS is too early.  We also
15393      need to call it at a point where the DFA infrastructure is not
15394      already in use, so we can't just call it lazily on demand.
15395 
15396      At present, mips_tuning_info is only needed during post-expand
15397      RTL passes such as split_insns, so this hook should be early enough.
15398      We may need to move the call elsewhere if mips_tuning_info starts
15399      to be used for other things (such as rtx_costs, or expanders that
15400      could be called during gimple optimization).  */
15401   mips_set_tuning_info ();
15402 }
15403 
15404 /* The VR4130 pipeline issues aligned pairs of instructions together,
15405    but it stalls the second instruction if it depends on the first.
15406    In order to cut down the amount of logic required, this dependence
15407    check is not based on a full instruction decode.  Instead, any non-SPECIAL
15408    instruction is assumed to modify the register specified by bits 20-16
15409    (which is usually the "rt" field).
15410 
15411    In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
15412    input, so we can end up with a false dependence between the branch
15413    and its delay slot.  If this situation occurs in instruction INSN,
15414    try to avoid it by swapping rs and rt.  */
15415 
15416 static void
15417 vr4130_avoid_branch_rt_conflict (rtx insn)
15418 {
15419   rtx first, second;
15420 
15421   first = SEQ_BEGIN (insn);
15422   second = SEQ_END (insn);
15423   if (JUMP_P (first)
15424       && NONJUMP_INSN_P (second)
15425       && GET_CODE (PATTERN (first)) == SET
15426       && GET_CODE (SET_DEST (PATTERN (first))) == PC
15427       && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
15428     {
15429       /* Check for the right kind of condition.  */
15430       rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
15431       if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
15432 	  && REG_P (XEXP (cond, 0))
15433 	  && REG_P (XEXP (cond, 1))
15434 	  && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
15435 	  && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
15436 	{
15437 	  /* SECOND mentions the rt register but not the rs register.  */
15438 	  rtx tmp = XEXP (cond, 0);
15439 	  XEXP (cond, 0) = XEXP (cond, 1);
15440 	  XEXP (cond, 1) = tmp;
15441 	}
15442     }
15443 }
15444 
15445 /* Implement -mvr4130-align.  Go through each basic block and simulate the
15446    processor pipeline.  If we find that a pair of instructions could execute
15447    in parallel, and the first of those instructions is not 8-byte aligned,
15448    insert a nop to make it aligned.  */
15449 
15450 static void
15451 vr4130_align_insns (void)
15452 {
15453   struct mips_sim state;
15454   rtx insn, subinsn, last, last2, next;
15455   bool aligned_p;
15456 
15457   dfa_start ();
15458 
15459   /* LAST is the last instruction before INSN to have a nonzero length.
15460      LAST2 is the last such instruction before LAST.  */
15461   last = 0;
15462   last2 = 0;
15463 
15464   /* ALIGNED_P is true if INSN is known to be at an aligned address.  */
15465   aligned_p = true;
15466 
15467   mips_sim_init (&state, alloca (state_size ()));
15468   for (insn = get_insns (); insn != 0; insn = next)
15469     {
15470       unsigned int length;
15471 
15472       next = NEXT_INSN (insn);
15473 
15474       /* See the comment above vr4130_avoid_branch_rt_conflict for details.
15475 	 This isn't really related to the alignment pass, but we do it on
15476 	 the fly to avoid a separate instruction walk.  */
15477       vr4130_avoid_branch_rt_conflict (insn);
15478 
15479       length = get_attr_length (insn);
15480       if (length > 0 && USEFUL_INSN_P (insn))
15481 	FOR_EACH_SUBINSN (subinsn, insn)
15482 	  {
15483 	    mips_sim_wait_insn (&state, subinsn);
15484 
15485 	    /* If we want this instruction to issue in parallel with the
15486 	       previous one, make sure that the previous instruction is
15487 	       aligned.  There are several reasons why this isn't worthwhile
15488 	       when the second instruction is a call:
15489 
15490 	          - Calls are less likely to be performance critical,
15491 		  - There's a good chance that the delay slot can execute
15492 		    in parallel with the call.
15493 	          - The return address would then be unaligned.
15494 
15495 	       In general, if we're going to insert a nop between instructions
15496 	       X and Y, it's better to insert it immediately after X.  That
15497 	       way, if the nop makes Y aligned, it will also align any labels
15498 	       between X and Y.  */
15499 	    if (state.insns_left != state.issue_rate
15500 		&& !CALL_P (subinsn))
15501 	      {
15502 		if (subinsn == SEQ_BEGIN (insn) && aligned_p)
15503 		  {
15504 		    /* SUBINSN is the first instruction in INSN and INSN is
15505 		       aligned.  We want to align the previous instruction
15506 		       instead, so insert a nop between LAST2 and LAST.
15507 
15508 		       Note that LAST could be either a single instruction
15509 		       or a branch with a delay slot.  In the latter case,
15510 		       LAST, like INSN, is already aligned, but the delay
15511 		       slot must have some extra delay that stops it from
15512 		       issuing at the same time as the branch.  We therefore
15513 		       insert a nop before the branch in order to align its
15514 		       delay slot.  */
15515 		    gcc_assert (last2);
15516 		    emit_insn_after (gen_nop (), last2);
15517 		    aligned_p = false;
15518 		  }
15519 		else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
15520 		  {
15521 		    /* SUBINSN is the delay slot of INSN, but INSN is
15522 		       currently unaligned.  Insert a nop between
15523 		       LAST and INSN to align it.  */
15524 		    gcc_assert (last);
15525 		    emit_insn_after (gen_nop (), last);
15526 		    aligned_p = true;
15527 		  }
15528 	      }
15529 	    mips_sim_issue_insn (&state, subinsn);
15530 	  }
15531       mips_sim_finish_insn (&state, insn);
15532 
15533       /* Update LAST, LAST2 and ALIGNED_P for the next instruction.  */
15534       length = get_attr_length (insn);
15535       if (length > 0)
15536 	{
15537 	  /* If the instruction is an asm statement or multi-instruction
15538 	     mips.md patern, the length is only an estimate.  Insert an
15539 	     8 byte alignment after it so that the following instructions
15540 	     can be handled correctly.  */
15541 	  if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
15542 	      && (recog_memoized (insn) < 0 || length >= 8))
15543 	    {
15544 	      next = emit_insn_after (gen_align (GEN_INT (3)), insn);
15545 	      next = NEXT_INSN (next);
15546 	      mips_sim_next_cycle (&state);
15547 	      aligned_p = true;
15548 	    }
15549 	  else if (length & 4)
15550 	    aligned_p = !aligned_p;
15551 	  last2 = last;
15552 	  last = insn;
15553 	}
15554 
15555       /* See whether INSN is an aligned label.  */
15556       if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
15557 	aligned_p = true;
15558     }
15559   dfa_finish ();
15560 }
15561 
15562 /* This structure records that the current function has a LO_SUM
15563    involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
15564    the largest offset applied to BASE by all such LO_SUMs.  */
15565 struct mips_lo_sum_offset {
15566   rtx base;
15567   HOST_WIDE_INT offset;
15568 };
15569 
15570 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE.  */
15571 
15572 static hashval_t
15573 mips_hash_base (rtx base)
15574 {
15575   int do_not_record_p;
15576 
15577   return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
15578 }
15579 
15580 /* Hash-table callbacks for mips_lo_sum_offsets.  */
15581 
15582 static hashval_t
15583 mips_lo_sum_offset_hash (const void *entry)
15584 {
15585   return mips_hash_base (((const struct mips_lo_sum_offset *) entry)->base);
15586 }
15587 
15588 static int
15589 mips_lo_sum_offset_eq (const void *entry, const void *value)
15590 {
15591   return rtx_equal_p (((const struct mips_lo_sum_offset *) entry)->base,
15592 		      (const_rtx) value);
15593 }
15594 
15595 /* Look up symbolic constant X in HTAB, which is a hash table of
15596    mips_lo_sum_offsets.  If OPTION is NO_INSERT, return true if X can be
15597    paired with a recorded LO_SUM, otherwise record X in the table.  */
15598 
15599 static bool
15600 mips_lo_sum_offset_lookup (htab_t htab, rtx x, enum insert_option option)
15601 {
15602   rtx base, offset;
15603   void **slot;
15604   struct mips_lo_sum_offset *entry;
15605 
15606   /* Split X into a base and offset.  */
15607   split_const (x, &base, &offset);
15608   if (UNSPEC_ADDRESS_P (base))
15609     base = UNSPEC_ADDRESS (base);
15610 
15611   /* Look up the base in the hash table.  */
15612   slot = htab_find_slot_with_hash (htab, base, mips_hash_base (base), option);
15613   if (slot == NULL)
15614     return false;
15615 
15616   entry = (struct mips_lo_sum_offset *) *slot;
15617   if (option == INSERT)
15618     {
15619       if (entry == NULL)
15620 	{
15621 	  entry = XNEW (struct mips_lo_sum_offset);
15622 	  entry->base = base;
15623 	  entry->offset = INTVAL (offset);
15624 	  *slot = entry;
15625 	}
15626       else
15627 	{
15628 	  if (INTVAL (offset) > entry->offset)
15629 	    entry->offset = INTVAL (offset);
15630 	}
15631     }
15632   return INTVAL (offset) <= entry->offset;
15633 }
15634 
15635 /* A for_each_rtx callback for which DATA is a mips_lo_sum_offset hash table.
15636    Record every LO_SUM in *LOC.  */
15637 
15638 static int
15639 mips_record_lo_sum (rtx *loc, void *data)
15640 {
15641   if (GET_CODE (*loc) == LO_SUM)
15642     mips_lo_sum_offset_lookup ((htab_t) data, XEXP (*loc, 1), INSERT);
15643   return 0;
15644 }
15645 
15646 /* Return true if INSN is a SET of an orphaned high-part relocation.
15647    HTAB is a hash table of mips_lo_sum_offsets that describes all the
15648    LO_SUMs in the current function.  */
15649 
15650 static bool
15651 mips_orphaned_high_part_p (htab_t htab, rtx insn)
15652 {
15653   enum mips_symbol_type type;
15654   rtx x, set;
15655 
15656   set = single_set (insn);
15657   if (set)
15658     {
15659       /* Check for %his.  */
15660       x = SET_SRC (set);
15661       if (GET_CODE (x) == HIGH
15662 	  && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
15663 	return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
15664 
15665       /* Check for local %gots (and %got_pages, which is redundant but OK).  */
15666       if (GET_CODE (x) == UNSPEC
15667 	  && XINT (x, 1) == UNSPEC_LOAD_GOT
15668 	  && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
15669 				       SYMBOL_CONTEXT_LEA, &type)
15670 	  && type == SYMBOL_GOTOFF_PAGE)
15671 	return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
15672     }
15673   return false;
15674 }
15675 
15676 /* Subroutine of mips_reorg_process_insns.  If there is a hazard between
15677    INSN and a previous instruction, avoid it by inserting nops after
15678    instruction AFTER.
15679 
15680    *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
15681    this point.  If *DELAYED_REG is non-null, INSN must wait a cycle
15682    before using the value of that register.  *HILO_DELAY counts the
15683    number of instructions since the last hilo hazard (that is,
15684    the number of instructions since the last MFLO or MFHI).
15685 
15686    After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
15687    for the next instruction.
15688 
15689    LO_REG is an rtx for the LO register, used in dependence checking.  */
15690 
15691 static void
15692 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
15693 		   rtx *delayed_reg, rtx lo_reg)
15694 {
15695   rtx pattern, set;
15696   int nops, ninsns;
15697 
15698   pattern = PATTERN (insn);
15699 
15700   /* Do not put the whole function in .set noreorder if it contains
15701      an asm statement.  We don't know whether there will be hazards
15702      between the asm statement and the gcc-generated code.  */
15703   if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
15704     cfun->machine->all_noreorder_p = false;
15705 
15706   /* Ignore zero-length instructions (barriers and the like).  */
15707   ninsns = get_attr_length (insn) / 4;
15708   if (ninsns == 0)
15709     return;
15710 
15711   /* Work out how many nops are needed.  Note that we only care about
15712      registers that are explicitly mentioned in the instruction's pattern.
15713      It doesn't matter that calls use the argument registers or that they
15714      clobber hi and lo.  */
15715   if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
15716     nops = 2 - *hilo_delay;
15717   else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
15718     nops = 1;
15719   else
15720     nops = 0;
15721 
15722   /* Insert the nops between this instruction and the previous one.
15723      Each new nop takes us further from the last hilo hazard.  */
15724   *hilo_delay += nops;
15725   while (nops-- > 0)
15726     emit_insn_after (gen_hazard_nop (), after);
15727 
15728   /* Set up the state for the next instruction.  */
15729   *hilo_delay += ninsns;
15730   *delayed_reg = 0;
15731   if (INSN_CODE (insn) >= 0)
15732     switch (get_attr_hazard (insn))
15733       {
15734       case HAZARD_NONE:
15735 	break;
15736 
15737       case HAZARD_HILO:
15738 	*hilo_delay = 0;
15739 	break;
15740 
15741       case HAZARD_DELAY:
15742 	set = single_set (insn);
15743 	gcc_assert (set);
15744 	*delayed_reg = SET_DEST (set);
15745 	break;
15746       }
15747 }
15748 
15749 /* Go through the instruction stream and insert nops where necessary.
15750    Also delete any high-part relocations whose partnering low parts
15751    are now all dead.  See if the whole function can then be put into
15752    .set noreorder and .set nomacro.  */
15753 
15754 static void
15755 mips_reorg_process_insns (void)
15756 {
15757   rtx insn, last_insn, subinsn, next_insn, lo_reg, delayed_reg;
15758   int hilo_delay;
15759   htab_t htab;
15760 
15761   /* Force all instructions to be split into their final form.  */
15762   split_all_insns_noflow ();
15763 
15764   /* Recalculate instruction lengths without taking nops into account.  */
15765   cfun->machine->ignore_hazard_length_p = true;
15766   shorten_branches (get_insns ());
15767 
15768   cfun->machine->all_noreorder_p = true;
15769 
15770   /* We don't track MIPS16 PC-relative offsets closely enough to make
15771      a good job of "set .noreorder" code in MIPS16 mode.  */
15772   if (TARGET_MIPS16)
15773     cfun->machine->all_noreorder_p = false;
15774 
15775   /* Code that doesn't use explicit relocs can't be ".set nomacro".  */
15776   if (!TARGET_EXPLICIT_RELOCS)
15777     cfun->machine->all_noreorder_p = false;
15778 
15779   /* Profiled functions can't be all noreorder because the profiler
15780      support uses assembler macros.  */
15781   if (crtl->profile)
15782     cfun->machine->all_noreorder_p = false;
15783 
15784   /* Code compiled with -mfix-vr4120 or -mfix-24k can't be all noreorder
15785      because we rely on the assembler to work around some errata.  */
15786   if (TARGET_FIX_VR4120 || TARGET_FIX_24K)
15787     cfun->machine->all_noreorder_p = false;
15788 
15789   /* The same is true for -mfix-vr4130 if we might generate MFLO or
15790      MFHI instructions.  Note that we avoid using MFLO and MFHI if
15791      the VR4130 MACC and DMACC instructions are available instead;
15792      see the *mfhilo_{si,di}_macc patterns.  */
15793   if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
15794     cfun->machine->all_noreorder_p = false;
15795 
15796   htab = htab_create (37, mips_lo_sum_offset_hash,
15797 		      mips_lo_sum_offset_eq, free);
15798 
15799   /* Make a first pass over the instructions, recording all the LO_SUMs.  */
15800   for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
15801     FOR_EACH_SUBINSN (subinsn, insn)
15802       if (USEFUL_INSN_P (subinsn))
15803 	for_each_rtx (&PATTERN (subinsn), mips_record_lo_sum, htab);
15804 
15805   last_insn = 0;
15806   hilo_delay = 2;
15807   delayed_reg = 0;
15808   lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
15809 
15810   /* Make a second pass over the instructions.  Delete orphaned
15811      high-part relocations or turn them into NOPs.  Avoid hazards
15812      by inserting NOPs.  */
15813   for (insn = get_insns (); insn != 0; insn = next_insn)
15814     {
15815       next_insn = NEXT_INSN (insn);
15816       if (USEFUL_INSN_P (insn))
15817 	{
15818 	  if (GET_CODE (PATTERN (insn)) == SEQUENCE)
15819 	    {
15820 	      /* If we find an orphaned high-part relocation in a delay
15821 		 slot, it's easier to turn that instruction into a NOP than
15822 		 to delete it.  The delay slot will be a NOP either way.  */
15823 	      FOR_EACH_SUBINSN (subinsn, insn)
15824 		if (INSN_P (subinsn))
15825 		  {
15826 		    if (mips_orphaned_high_part_p (htab, subinsn))
15827 		      {
15828 			PATTERN (subinsn) = gen_nop ();
15829 			INSN_CODE (subinsn) = CODE_FOR_nop;
15830 		      }
15831 		    mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
15832 				       &delayed_reg, lo_reg);
15833 		  }
15834 	      last_insn = insn;
15835 	    }
15836 	  else
15837 	    {
15838 	      /* INSN is a single instruction.  Delete it if it's an
15839 		 orphaned high-part relocation.  */
15840 	      if (mips_orphaned_high_part_p (htab, insn))
15841 		delete_insn (insn);
15842 	      /* Also delete cache barriers if the last instruction
15843 		 was an annulled branch.  INSN will not be speculatively
15844 		 executed.  */
15845 	      else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
15846 		       && last_insn
15847 		       && JUMP_P (SEQ_BEGIN (last_insn))
15848 		       && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
15849 		delete_insn (insn);
15850 	      else
15851 		{
15852 		  mips_avoid_hazard (last_insn, insn, &hilo_delay,
15853 				     &delayed_reg, lo_reg);
15854 		  last_insn = insn;
15855 		}
15856 	    }
15857 	}
15858     }
15859 
15860   htab_delete (htab);
15861 }
15862 
15863 /* Return true if the function has a long branch instruction.  */
15864 
15865 static bool
15866 mips_has_long_branch_p (void)
15867 {
15868   rtx insn, subinsn;
15869   int normal_length;
15870 
15871   /* We need up-to-date instruction lengths.  */
15872   shorten_branches (get_insns ());
15873 
15874   /* Look for a branch that is longer than normal.  The normal length for
15875      non-MIPS16 branches is 8, because the length includes the delay slot.
15876      It is 4 for MIPS16, because MIPS16 branches are extended instructions,
15877      but they have no delay slot.  */
15878   normal_length = (TARGET_MIPS16 ? 4 : 8);
15879   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15880     FOR_EACH_SUBINSN (subinsn, insn)
15881       if (JUMP_P (subinsn)
15882 	  && USEFUL_INSN_P (subinsn)
15883 	  && get_attr_length (subinsn) > normal_length
15884 	  && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
15885 	return true;
15886 
15887   return false;
15888 }
15889 
15890 /* If we are using a GOT, but have not decided to use a global pointer yet,
15891    see whether we need one to implement long branches.  Convert the ghost
15892    global-pointer instructions into real ones if so.  */
15893 
15894 static bool
15895 mips_expand_ghost_gp_insns (void)
15896 {
15897   /* Quick exit if we already know that we will or won't need a
15898      global pointer.  */
15899   if (!TARGET_USE_GOT
15900       || cfun->machine->global_pointer == INVALID_REGNUM
15901       || mips_must_initialize_gp_p ())
15902     return false;
15903 
15904   /* Run a full check for long branches.  */
15905   if (!mips_has_long_branch_p ())
15906     return false;
15907 
15908   /* We've now established that we need $gp.  */
15909   cfun->machine->must_initialize_gp_p = true;
15910   split_all_insns_noflow ();
15911 
15912   return true;
15913 }
15914 
15915 /* Subroutine of mips_reorg to manage passes that require DF.  */
15916 
15917 static void
15918 mips_df_reorg (void)
15919 {
15920   /* Create def-use chains.  */
15921   df_set_flags (DF_EQ_NOTES);
15922   df_chain_add_problem (DF_UD_CHAIN);
15923   df_analyze ();
15924 
15925   if (TARGET_RELAX_PIC_CALLS)
15926     mips_annotate_pic_calls ();
15927 
15928   if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
15929     r10k_insert_cache_barriers ();
15930 
15931   df_finish_pass (false);
15932 }
15933 
15934 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST.  This is
15935    called very late in mips_reorg, but the caller is required to run
15936    mips16_lay_out_constants on the result.  */
15937 
15938 static void
15939 mips16_load_branch_target (rtx dest, rtx src)
15940 {
15941   if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
15942     {
15943       rtx page, low;
15944 
15945       if (mips_cfun_has_cprestore_slot_p ())
15946 	mips_emit_move (dest, mips_cprestore_slot (dest, true));
15947       else
15948 	mips_emit_move (dest, pic_offset_table_rtx);
15949       page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
15950       low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
15951       emit_insn (gen_rtx_SET (VOIDmode, dest,
15952 			      PMODE_INSN (gen_unspec_got, (dest, page))));
15953       emit_insn (gen_rtx_SET (VOIDmode, dest,
15954 			      gen_rtx_LO_SUM (Pmode, dest, low)));
15955     }
15956   else
15957     {
15958       src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
15959       mips_emit_move (dest, src);
15960     }
15961 }
15962 
15963 /* If we're compiling a MIPS16 function, look for and split any long branches.
15964    This must be called after all other instruction modifications in
15965    mips_reorg.  */
15966 
15967 static void
15968 mips16_split_long_branches (void)
15969 {
15970   bool something_changed;
15971 
15972   if (!TARGET_MIPS16)
15973     return;
15974 
15975   /* Loop until the alignments for all targets are sufficient.  */
15976   do
15977     {
15978       rtx insn;
15979 
15980       shorten_branches (get_insns ());
15981       something_changed = false;
15982       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15983 	if (JUMP_P (insn)
15984 	    && USEFUL_INSN_P (insn)
15985 	    && get_attr_length (insn) > 8
15986 	    && (any_condjump_p (insn) || any_uncondjump_p (insn)))
15987 	  {
15988 	    rtx old_label, new_label, temp, saved_temp;
15989 	    rtx target, jump, jump_sequence;
15990 
15991 	    start_sequence ();
15992 
15993 	    /* Free up a MIPS16 register by saving it in $1.  */
15994 	    saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
15995 	    temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
15996 	    emit_move_insn (saved_temp, temp);
15997 
15998 	    /* Load the branch target into TEMP.  */
15999 	    old_label = JUMP_LABEL (insn);
16000 	    target = gen_rtx_LABEL_REF (Pmode, old_label);
16001 	    mips16_load_branch_target (temp, target);
16002 
16003 	    /* Jump to the target and restore the register's
16004 	       original value.  */
16005 	    jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
16006 					       (temp, temp, saved_temp)));
16007 	    JUMP_LABEL (jump) = old_label;
16008 	    LABEL_NUSES (old_label)++;
16009 
16010 	    /* Rewrite any symbolic references that are supposed to use
16011 	       a PC-relative constant pool.  */
16012 	    mips16_lay_out_constants (false);
16013 
16014 	    if (simplejump_p (insn))
16015 	      /* We're going to replace INSN with a longer form.  */
16016 	      new_label = NULL_RTX;
16017 	    else
16018 	      {
16019 		/* Create a branch-around label for the original
16020 		   instruction.  */
16021 		new_label = gen_label_rtx ();
16022 		emit_label (new_label);
16023 	      }
16024 
16025 	    jump_sequence = get_insns ();
16026 	    end_sequence ();
16027 
16028 	    emit_insn_after (jump_sequence, insn);
16029 	    if (new_label)
16030 	      invert_jump (insn, new_label, false);
16031 	    else
16032 	      delete_insn (insn);
16033 	    something_changed = true;
16034 	  }
16035     }
16036   while (something_changed);
16037 }
16038 
16039 /* Implement TARGET_MACHINE_DEPENDENT_REORG.  */
16040 
16041 static void
16042 mips_reorg (void)
16043 {
16044   /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF.  Also during
16045      insn splitting in mips16_lay_out_constants, DF insn info is only kept up
16046      to date if the CFG is available.  */
16047   if (mips_cfg_in_reorg ())
16048     compute_bb_for_insn ();
16049   mips16_lay_out_constants (true);
16050   if (mips_cfg_in_reorg ())
16051     {
16052       mips_df_reorg ();
16053       free_bb_for_insn ();
16054     }
16055 
16056   if (optimize > 0 && flag_delayed_branch)
16057     {
16058       cleanup_barriers ();
16059       dbr_schedule (get_insns ());
16060     }
16061   mips_reorg_process_insns ();
16062   if (!TARGET_MIPS16
16063       && TARGET_EXPLICIT_RELOCS
16064       && TUNE_MIPS4130
16065       && TARGET_VR4130_ALIGN)
16066     vr4130_align_insns ();
16067   if (mips_expand_ghost_gp_insns ())
16068     /* The expansion could invalidate some of the VR4130 alignment
16069        optimizations, but this should be an extremely rare case anyhow.  */
16070     mips_reorg_process_insns ();
16071   mips16_split_long_branches ();
16072 }
16073 
16074 /* Implement TARGET_ASM_OUTPUT_MI_THUNK.  Generate rtl rather than asm text
16075    in order to avoid duplicating too much logic from elsewhere.  */
16076 
16077 static void
16078 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
16079 		      HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
16080 		      tree function)
16081 {
16082   rtx this_rtx, temp1, temp2, insn, fnaddr;
16083   bool use_sibcall_p;
16084 
16085   /* Pretend to be a post-reload pass while generating rtl.  */
16086   reload_completed = 1;
16087 
16088   /* Mark the end of the (empty) prologue.  */
16089   emit_note (NOTE_INSN_PROLOGUE_END);
16090 
16091   /* Determine if we can use a sibcall to call FUNCTION directly.  */
16092   fnaddr = XEXP (DECL_RTL (function), 0);
16093   use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
16094 		   && const_call_insn_operand (fnaddr, Pmode));
16095 
16096   /* Determine if we need to load FNADDR from the GOT.  */
16097   if (!use_sibcall_p
16098       && (mips_got_symbol_type_p
16099 	  (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
16100     {
16101       /* Pick a global pointer.  Use a call-clobbered register if
16102 	 TARGET_CALL_SAVED_GP.  */
16103       cfun->machine->global_pointer
16104 	= TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
16105       cfun->machine->must_initialize_gp_p = true;
16106       SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
16107 
16108       /* Set up the global pointer for n32 or n64 abicalls.  */
16109       mips_emit_loadgp ();
16110     }
16111 
16112   /* We need two temporary registers in some cases.  */
16113   temp1 = gen_rtx_REG (Pmode, 2);
16114   temp2 = gen_rtx_REG (Pmode, 3);
16115 
16116   /* Find out which register contains the "this" pointer.  */
16117   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
16118     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
16119   else
16120     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
16121 
16122   /* Add DELTA to THIS_RTX.  */
16123   if (delta != 0)
16124     {
16125       rtx offset = GEN_INT (delta);
16126       if (!SMALL_OPERAND (delta))
16127 	{
16128 	  mips_emit_move (temp1, offset);
16129 	  offset = temp1;
16130 	}
16131       emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
16132     }
16133 
16134   /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX.  */
16135   if (vcall_offset != 0)
16136     {
16137       rtx addr;
16138 
16139       /* Set TEMP1 to *THIS_RTX.  */
16140       mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
16141 
16142       /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET.  */
16143       addr = mips_add_offset (temp2, temp1, vcall_offset);
16144 
16145       /* Load the offset and add it to THIS_RTX.  */
16146       mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
16147       emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
16148     }
16149 
16150   /* Jump to the target function.  Use a sibcall if direct jumps are
16151      allowed, otherwise load the address into a register first.  */
16152   if (use_sibcall_p)
16153     {
16154       insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
16155       SIBLING_CALL_P (insn) = 1;
16156     }
16157   else
16158     {
16159       /* This is messy.  GAS treats "la $25,foo" as part of a call
16160 	 sequence and may allow a global "foo" to be lazily bound.
16161 	 The general move patterns therefore reject this combination.
16162 
16163 	 In this context, lazy binding would actually be OK
16164 	 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
16165 	 TARGET_CALL_SAVED_GP; see mips_load_call_address.
16166 	 We must therefore load the address via a temporary
16167 	 register if mips_dangerous_for_la25_p.
16168 
16169 	 If we jump to the temporary register rather than $25,
16170 	 the assembler can use the move insn to fill the jump's
16171 	 delay slot.
16172 
16173 	 We can use the same technique for MIPS16 code, where $25
16174 	 is not a valid JR register.  */
16175       if (TARGET_USE_PIC_FN_ADDR_REG
16176 	  && !TARGET_MIPS16
16177 	  && !mips_dangerous_for_la25_p (fnaddr))
16178 	temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
16179       mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
16180 
16181       if (TARGET_USE_PIC_FN_ADDR_REG
16182 	  && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
16183 	mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
16184       emit_jump_insn (gen_indirect_jump (temp1));
16185     }
16186 
16187   /* Run just enough of rest_of_compilation.  This sequence was
16188      "borrowed" from alpha.c.  */
16189   insn = get_insns ();
16190   split_all_insns_noflow ();
16191   mips16_lay_out_constants (true);
16192   shorten_branches (insn);
16193   final_start_function (insn, file, 1);
16194   final (insn, file, 1);
16195   final_end_function ();
16196 
16197   /* Clean up the vars set above.  Note that final_end_function resets
16198      the global pointer for us.  */
16199   reload_completed = 0;
16200 }
16201 
16202 /* The last argument passed to mips_set_mips16_mode, or negative if the
16203    function hasn't been called yet.  */
16204 static int was_mips16_p = -1;
16205 
16206 /* Set up the target-dependent global state so that it matches the
16207    current function's ISA mode.  */
16208 
16209 static void
16210 mips_set_mips16_mode (int mips16_p)
16211 {
16212   if (mips16_p == was_mips16_p)
16213     return;
16214 
16215   /* Restore base settings of various flags.  */
16216   target_flags = mips_base_target_flags;
16217   flag_schedule_insns = mips_base_schedule_insns;
16218   flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
16219   flag_move_loop_invariants = mips_base_move_loop_invariants;
16220   align_loops = mips_base_align_loops;
16221   align_jumps = mips_base_align_jumps;
16222   align_functions = mips_base_align_functions;
16223 
16224   if (mips16_p)
16225     {
16226       /* Switch to MIPS16 mode.  */
16227       target_flags |= MASK_MIPS16;
16228 
16229       /* Turn off SYNCI if it was on, MIPS16 doesn't support it.  */
16230       target_flags &= ~MASK_SYNCI;
16231 
16232       /* Don't run the scheduler before reload, since it tends to
16233          increase register pressure.  */
16234       flag_schedule_insns = 0;
16235 
16236       /* Don't do hot/cold partitioning.  mips16_lay_out_constants expects
16237 	 the whole function to be in a single section.  */
16238       flag_reorder_blocks_and_partition = 0;
16239 
16240       /* Don't move loop invariants, because it tends to increase
16241 	 register pressure.  It also introduces an extra move in cases
16242 	 where the constant is the first operand in a two-operand binary
16243 	 instruction, or when it forms a register argument to a functon
16244 	 call.  */
16245       flag_move_loop_invariants = 0;
16246 
16247       target_flags |= MASK_EXPLICIT_RELOCS;
16248 
16249       /* Experiments suggest we get the best overall section-anchor
16250 	 results from using the range of an unextended LW or SW.  Code
16251 	 that makes heavy use of byte or short accesses can do better
16252 	 with ranges of 0...31 and 0...63 respectively, but most code is
16253 	 sensitive to the range of LW and SW instead.  */
16254       targetm.min_anchor_offset = 0;
16255       targetm.max_anchor_offset = 127;
16256 
16257       targetm.const_anchor = 0;
16258 
16259       /* MIPS16 has no BAL instruction.  */
16260       target_flags &= ~MASK_RELAX_PIC_CALLS;
16261 
16262       /* The R4000 errata don't apply to any known MIPS16 cores.
16263 	 It's simpler to make the R4000 fixes and MIPS16 mode
16264 	 mutually exclusive.  */
16265       target_flags &= ~MASK_FIX_R4000;
16266 
16267       if (flag_pic && !TARGET_OLDABI)
16268 	sorry ("MIPS16 PIC for ABIs other than o32 and o64");
16269 
16270       if (TARGET_XGOT)
16271 	sorry ("MIPS16 -mxgot code");
16272 
16273       if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
16274 	sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
16275     }
16276   else
16277     {
16278       /* Switch to normal (non-MIPS16) mode.  */
16279       target_flags &= ~MASK_MIPS16;
16280 
16281       /* Provide default values for align_* for 64-bit targets.  */
16282       if (TARGET_64BIT)
16283 	{
16284 	  if (align_loops == 0)
16285 	    align_loops = 8;
16286 	  if (align_jumps == 0)
16287 	    align_jumps = 8;
16288 	  if (align_functions == 0)
16289 	    align_functions = 8;
16290 	}
16291 
16292       targetm.min_anchor_offset = -32768;
16293       targetm.max_anchor_offset = 32767;
16294 
16295       targetm.const_anchor = 0x8000;
16296     }
16297 
16298   /* (Re)initialize MIPS target internals for new ISA.  */
16299   mips_init_relocs ();
16300 
16301   if (mips16_p)
16302     {
16303       if (!mips16_globals)
16304 	mips16_globals = save_target_globals_default_opts ();
16305       else
16306 	restore_target_globals (mips16_globals);
16307     }
16308   else
16309     restore_target_globals (&default_target_globals);
16310 
16311   was_mips16_p = mips16_p;
16312 }
16313 
16314 /* Implement TARGET_SET_CURRENT_FUNCTION.  Decide whether the current
16315    function should use the MIPS16 ISA and switch modes accordingly.  */
16316 
16317 static void
16318 mips_set_current_function (tree fndecl)
16319 {
16320   mips_set_mips16_mode (mips_use_mips16_mode_p (fndecl));
16321 }
16322 
16323 /* Allocate a chunk of memory for per-function machine-dependent data.  */
16324 
16325 static struct machine_function *
16326 mips_init_machine_status (void)
16327 {
16328   return ggc_alloc_cleared_machine_function ();
16329 }
16330 
16331 /* Return the processor associated with the given ISA level, or null
16332    if the ISA isn't valid.  */
16333 
16334 static const struct mips_cpu_info *
16335 mips_cpu_info_from_isa (int isa)
16336 {
16337   unsigned int i;
16338 
16339   for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16340     if (mips_cpu_info_table[i].isa == isa)
16341       return mips_cpu_info_table + i;
16342 
16343   return NULL;
16344 }
16345 
16346 /* Return a mips_cpu_info entry determined by an option valued
16347    OPT.  */
16348 
16349 static const struct mips_cpu_info *
16350 mips_cpu_info_from_opt (int opt)
16351 {
16352   switch (opt)
16353     {
16354     case MIPS_ARCH_OPTION_FROM_ABI:
16355       /* 'from-abi' selects the most compatible architecture for the
16356 	 given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
16357 	 ABIs.  For the EABIs, we have to decide whether we're using
16358 	 the 32-bit or 64-bit version.  */
16359       return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
16360 				     : ABI_NEEDS_64BIT_REGS ? 3
16361 				     : (TARGET_64BIT ? 3 : 1));
16362 
16363     case MIPS_ARCH_OPTION_NATIVE:
16364       gcc_unreachable ();
16365 
16366     default:
16367       return &mips_cpu_info_table[opt];
16368     }
16369 }
16370 
16371 /* Return a default mips_cpu_info entry, given that no -march= option
16372    was explicitly specified.  */
16373 
16374 static const struct mips_cpu_info *
16375 mips_default_arch (void)
16376 {
16377 #if defined (MIPS_CPU_STRING_DEFAULT)
16378   unsigned int i;
16379   for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16380     if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
16381       return mips_cpu_info_table + i;
16382   gcc_unreachable ();
16383 #elif defined (MIPS_ISA_DEFAULT)
16384   return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
16385 #else
16386   /* 'from-abi' makes a good default: you get whatever the ABI
16387      requires.  */
16388   return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
16389 #endif
16390 }
16391 
16392 /* Set up globals to generate code for the ISA or processor
16393    described by INFO.  */
16394 
16395 static void
16396 mips_set_architecture (const struct mips_cpu_info *info)
16397 {
16398   if (info != 0)
16399     {
16400       mips_arch_info = info;
16401       mips_arch = info->cpu;
16402       mips_isa = info->isa;
16403     }
16404 }
16405 
16406 /* Likewise for tuning.  */
16407 
16408 static void
16409 mips_set_tune (const struct mips_cpu_info *info)
16410 {
16411   if (info != 0)
16412     {
16413       mips_tune_info = info;
16414       mips_tune = info->cpu;
16415     }
16416 }
16417 
16418 /* Implement TARGET_OPTION_OVERRIDE.  */
16419 
16420 static void
16421 mips_option_override (void)
16422 {
16423   int i, start, regno, mode;
16424 
16425   if (global_options_set.x_mips_isa_option)
16426     mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
16427 
16428   /* Process flags as though we were generating non-MIPS16 code.  */
16429   mips_base_mips16 = TARGET_MIPS16;
16430   target_flags &= ~MASK_MIPS16;
16431 
16432 #ifdef SUBTARGET_OVERRIDE_OPTIONS
16433   SUBTARGET_OVERRIDE_OPTIONS;
16434 #endif
16435 
16436   /* -mno-float overrides -mhard-float and -msoft-float.  */
16437   if (TARGET_NO_FLOAT)
16438     {
16439       target_flags |= MASK_SOFT_FLOAT_ABI;
16440       target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
16441     }
16442 
16443   if (TARGET_FLIP_MIPS16)
16444     TARGET_INTERLINK_MIPS16 = 1;
16445 
16446   /* Set the small data limit.  */
16447   mips_small_data_threshold = (global_options_set.x_g_switch_value
16448 			       ? g_switch_value
16449 			       : MIPS_DEFAULT_GVALUE);
16450 
16451   /* The following code determines the architecture and register size.
16452      Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
16453      The GAS and GCC code should be kept in sync as much as possible.  */
16454 
16455   if (global_options_set.x_mips_arch_option)
16456     mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
16457 
16458   if (mips_isa_option_info != 0)
16459     {
16460       if (mips_arch_info == 0)
16461 	mips_set_architecture (mips_isa_option_info);
16462       else if (mips_arch_info->isa != mips_isa_option_info->isa)
16463 	error ("%<-%s%> conflicts with the other architecture options, "
16464 	       "which specify a %s processor",
16465 	       mips_isa_option_info->name,
16466 	       mips_cpu_info_from_isa (mips_arch_info->isa)->name);
16467     }
16468 
16469   if (mips_arch_info == 0)
16470     mips_set_architecture (mips_default_arch ());
16471 
16472   if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
16473     error ("%<-march=%s%> is not compatible with the selected ABI",
16474 	   mips_arch_info->name);
16475 
16476   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
16477   if (global_options_set.x_mips_tune_option)
16478     mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
16479 
16480   if (mips_tune_info == 0)
16481     mips_set_tune (mips_arch_info);
16482 
16483   if ((target_flags_explicit & MASK_64BIT) != 0)
16484     {
16485       /* The user specified the size of the integer registers.  Make sure
16486 	 it agrees with the ABI and ISA.  */
16487       if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
16488 	error ("%<-mgp64%> used with a 32-bit processor");
16489       else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
16490 	error ("%<-mgp32%> used with a 64-bit ABI");
16491       else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
16492 	error ("%<-mgp64%> used with a 32-bit ABI");
16493     }
16494   else
16495     {
16496       /* Infer the integer register size from the ABI and processor.
16497 	 Restrict ourselves to 32-bit registers if that's all the
16498 	 processor has, or if the ABI cannot handle 64-bit registers.  */
16499       if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
16500 	target_flags &= ~MASK_64BIT;
16501       else
16502 	target_flags |= MASK_64BIT;
16503     }
16504 
16505   if ((target_flags_explicit & MASK_FLOAT64) != 0)
16506     {
16507       if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
16508 	error ("unsupported combination: %s", "-mfp64 -msingle-float");
16509       else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
16510 	error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
16511       else if (!TARGET_64BIT && TARGET_FLOAT64)
16512 	{
16513 	  if (!ISA_HAS_MXHC1)
16514 	    error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
16515 		   " the target supports the mfhc1 and mthc1 instructions");
16516 	  else if (mips_abi != ABI_32)
16517 	    error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
16518 		   " the o32 ABI");
16519 	}
16520     }
16521   else
16522     {
16523       /* -msingle-float selects 32-bit float registers.  Otherwise the
16524 	 float registers should be the same size as the integer ones.  */
16525       if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
16526 	target_flags |= MASK_FLOAT64;
16527       else
16528 	target_flags &= ~MASK_FLOAT64;
16529     }
16530 
16531   /* End of code shared with GAS.  */
16532 
16533   /* If a -mlong* option was given, check that it matches the ABI,
16534      otherwise infer the -mlong* setting from the other options.  */
16535   if ((target_flags_explicit & MASK_LONG64) != 0)
16536     {
16537       if (TARGET_LONG64)
16538 	{
16539 	  if (mips_abi == ABI_N32)
16540 	    error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
16541 	  else if (mips_abi == ABI_32)
16542 	    error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
16543 	  else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
16544 	    /* We have traditionally allowed non-abicalls code to use
16545 	       an LP64 form of o64.  However, it would take a bit more
16546 	       effort to support the combination of 32-bit GOT entries
16547 	       and 64-bit pointers, so we treat the abicalls case as
16548 	       an error.  */
16549 	    error ("the combination of %qs and %qs is incompatible with %qs",
16550 		   "-mabi=o64", "-mabicalls", "-mlong64");
16551 	}
16552       else
16553 	{
16554 	  if (mips_abi == ABI_64)
16555 	    error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
16556 	}
16557     }
16558   else
16559     {
16560       if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
16561 	target_flags |= MASK_LONG64;
16562       else
16563 	target_flags &= ~MASK_LONG64;
16564     }
16565 
16566   if (!TARGET_OLDABI)
16567     flag_pcc_struct_return = 0;
16568 
16569   /* Decide which rtx_costs structure to use.  */
16570   if (optimize_size)
16571     mips_cost = &mips_rtx_cost_optimize_size;
16572   else
16573     mips_cost = &mips_rtx_cost_data[mips_tune];
16574 
16575   /* If the user hasn't specified a branch cost, use the processor's
16576      default.  */
16577   if (mips_branch_cost == 0)
16578     mips_branch_cost = mips_cost->branch_cost;
16579 
16580   /* If neither -mbranch-likely nor -mno-branch-likely was given
16581      on the command line, set MASK_BRANCHLIKELY based on the target
16582      architecture and tuning flags.  Annulled delay slots are a
16583      size win, so we only consider the processor-specific tuning
16584      for !optimize_size.  */
16585   if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
16586     {
16587       if (ISA_HAS_BRANCHLIKELY
16588 	  && (optimize_size
16589 	      || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
16590 	target_flags |= MASK_BRANCHLIKELY;
16591       else
16592 	target_flags &= ~MASK_BRANCHLIKELY;
16593     }
16594   else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
16595     warning (0, "the %qs architecture does not support branch-likely"
16596 	     " instructions", mips_arch_info->name);
16597 
16598   /* The effect of -mabicalls isn't defined for the EABI.  */
16599   if (mips_abi == ABI_EABI && TARGET_ABICALLS)
16600     {
16601       error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
16602       target_flags &= ~MASK_ABICALLS;
16603     }
16604 
16605   /* PIC requires -mabicalls.  */
16606   if (flag_pic)
16607     {
16608       if (mips_abi == ABI_EABI)
16609 	error ("cannot generate position-independent code for %qs",
16610 	       "-mabi=eabi");
16611       else if (!TARGET_ABICALLS)
16612 	error ("position-independent code requires %qs", "-mabicalls");
16613     }
16614 
16615   if (TARGET_ABICALLS_PIC2)
16616     /* We need to set flag_pic for executables as well as DSOs
16617        because we may reference symbols that are not defined in
16618        the final executable.  (MIPS does not use things like
16619        copy relocs, for example.)
16620 
16621        There is a body of code that uses __PIC__ to distinguish
16622        between -mabicalls and -mno-abicalls code.  The non-__PIC__
16623        variant is usually appropriate for TARGET_ABICALLS_PIC0, as
16624        long as any indirect jumps use $25.  */
16625     flag_pic = 1;
16626 
16627   /* -mvr4130-align is a "speed over size" optimization: it usually produces
16628      faster code, but at the expense of more nops.  Enable it at -O3 and
16629      above.  */
16630   if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
16631     target_flags |= MASK_VR4130_ALIGN;
16632 
16633   /* Prefer a call to memcpy over inline code when optimizing for size,
16634      though see MOVE_RATIO in mips.h.  */
16635   if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
16636     target_flags |= MASK_MEMCPY;
16637 
16638   /* If we have a nonzero small-data limit, check that the -mgpopt
16639      setting is consistent with the other target flags.  */
16640   if (mips_small_data_threshold > 0)
16641     {
16642       if (!TARGET_GPOPT)
16643 	{
16644 	  if (!TARGET_EXPLICIT_RELOCS)
16645 	    error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
16646 
16647 	  TARGET_LOCAL_SDATA = false;
16648 	  TARGET_EXTERN_SDATA = false;
16649 	}
16650       else
16651 	{
16652 	  if (TARGET_VXWORKS_RTP)
16653 	    warning (0, "cannot use small-data accesses for %qs", "-mrtp");
16654 
16655 	  if (TARGET_ABICALLS)
16656 	    warning (0, "cannot use small-data accesses for %qs",
16657 		     "-mabicalls");
16658 	}
16659     }
16660 
16661   /* Make sure that the user didn't turn off paired single support when
16662      MIPS-3D support is requested.  */
16663   if (TARGET_MIPS3D
16664       && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
16665       && !TARGET_PAIRED_SINGLE_FLOAT)
16666     error ("%<-mips3d%> requires %<-mpaired-single%>");
16667 
16668   /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT.  */
16669   if (TARGET_MIPS3D)
16670     target_flags |= MASK_PAIRED_SINGLE_FLOAT;
16671 
16672   /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
16673      and TARGET_HARD_FLOAT_ABI are both true.  */
16674   if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
16675     error ("%qs must be used with %qs",
16676 	   TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
16677 	   TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
16678 
16679   /* Make sure that the ISA supports TARGET_PAIRED_SINGLE_FLOAT when it is
16680      enabled.  */
16681   if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
16682     warning (0, "the %qs architecture does not support paired-single"
16683 	     " instructions", mips_arch_info->name);
16684 
16685   if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
16686       && !TARGET_CACHE_BUILTIN)
16687     {
16688       error ("%qs requires a target that provides the %qs instruction",
16689 	     "-mr10k-cache-barrier", "cache");
16690       mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
16691     }
16692 
16693   /* If TARGET_DSPR2, enable MASK_DSP.  */
16694   if (TARGET_DSPR2)
16695     target_flags |= MASK_DSP;
16696 
16697   /* .eh_frame addresses should be the same width as a C pointer.
16698      Most MIPS ABIs support only one pointer size, so the assembler
16699      will usually know exactly how big an .eh_frame address is.
16700 
16701      Unfortunately, this is not true of the 64-bit EABI.  The ABI was
16702      originally defined to use 64-bit pointers (i.e. it is LP64), and
16703      this is still the default mode.  However, we also support an n32-like
16704      ILP32 mode, which is selected by -mlong32.  The problem is that the
16705      assembler has traditionally not had an -mlong option, so it has
16706      traditionally not known whether we're using the ILP32 or LP64 form.
16707 
16708      As it happens, gas versions up to and including 2.19 use _32-bit_
16709      addresses for EABI64 .cfi_* directives.  This is wrong for the
16710      default LP64 mode, so we can't use the directives by default.
16711      Moreover, since gas's current behavior is at odds with gcc's
16712      default behavior, it seems unwise to rely on future versions
16713      of gas behaving the same way.  We therefore avoid using .cfi
16714      directives for -mlong32 as well.  */
16715   if (mips_abi == ABI_EABI && TARGET_64BIT)
16716     flag_dwarf2_cfi_asm = 0;
16717 
16718   /* .cfi_* directives generate a read-only section, so fall back on
16719      manual .eh_frame creation if we need the section to be writable.  */
16720   if (TARGET_WRITABLE_EH_FRAME)
16721     flag_dwarf2_cfi_asm = 0;
16722 
16723   mips_init_print_operand_punct ();
16724 
16725   /* Set up array to map GCC register number to debug register number.
16726      Ignore the special purpose register numbers.  */
16727 
16728   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
16729     {
16730       mips_dbx_regno[i] = IGNORED_DWARF_REGNUM;
16731       if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
16732 	mips_dwarf_regno[i] = i;
16733       else
16734 	mips_dwarf_regno[i] = INVALID_REGNUM;
16735     }
16736 
16737   start = GP_DBX_FIRST - GP_REG_FIRST;
16738   for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
16739     mips_dbx_regno[i] = i + start;
16740 
16741   start = FP_DBX_FIRST - FP_REG_FIRST;
16742   for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
16743     mips_dbx_regno[i] = i + start;
16744 
16745   /* Accumulator debug registers use big-endian ordering.  */
16746   mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
16747   mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
16748   mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
16749   mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
16750   for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
16751     {
16752       mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
16753       mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
16754     }
16755 
16756   /* Set up mips_hard_regno_mode_ok.  */
16757   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
16758     for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
16759       mips_hard_regno_mode_ok[mode][regno]
16760 	= mips_hard_regno_mode_ok_p (regno, (enum machine_mode) mode);
16761 
16762   /* Function to allocate machine-dependent function status.  */
16763   init_machine_status = &mips_init_machine_status;
16764 
16765   /* Default to working around R4000 errata only if the processor
16766      was selected explicitly.  */
16767   if ((target_flags_explicit & MASK_FIX_R4000) == 0
16768       && strcmp (mips_arch_info->name, "r4000") == 0)
16769     target_flags |= MASK_FIX_R4000;
16770 
16771   /* Default to working around R4400 errata only if the processor
16772      was selected explicitly.  */
16773   if ((target_flags_explicit & MASK_FIX_R4400) == 0
16774       && strcmp (mips_arch_info->name, "r4400") == 0)
16775     target_flags |= MASK_FIX_R4400;
16776 
16777   /* Default to working around R10000 errata only if the processor
16778      was selected explicitly.  */
16779   if ((target_flags_explicit & MASK_FIX_R10000) == 0
16780       && strcmp (mips_arch_info->name, "r10000") == 0)
16781     target_flags |= MASK_FIX_R10000;
16782 
16783   /* Make sure that branch-likely instructions available when using
16784      -mfix-r10000.  The instructions are not available if either:
16785 
16786 	1. -mno-branch-likely was passed.
16787 	2. The selected ISA does not support branch-likely and
16788 	   the command line does not include -mbranch-likely.  */
16789   if (TARGET_FIX_R10000
16790       && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
16791           ? !ISA_HAS_BRANCHLIKELY
16792           : !TARGET_BRANCHLIKELY))
16793     sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
16794 
16795   if (TARGET_SYNCI && !ISA_HAS_SYNCI)
16796     {
16797       warning (0, "the %qs architecture does not support the synci "
16798 	       "instruction", mips_arch_info->name);
16799       target_flags &= ~MASK_SYNCI;
16800     }
16801 
16802   /* Only optimize PIC indirect calls if they are actually required.  */
16803   if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
16804     target_flags &= ~MASK_RELAX_PIC_CALLS;
16805 
16806   /* Save base state of options.  */
16807   mips_base_target_flags = target_flags;
16808   mips_base_schedule_insns = flag_schedule_insns;
16809   mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
16810   mips_base_move_loop_invariants = flag_move_loop_invariants;
16811   mips_base_align_loops = align_loops;
16812   mips_base_align_jumps = align_jumps;
16813   mips_base_align_functions = align_functions;
16814 
16815   /* Now select the ISA mode.
16816 
16817      Do all CPP-sensitive stuff in non-MIPS16 mode; we'll switch to
16818      MIPS16 mode afterwards if need be.  */
16819   mips_set_mips16_mode (false);
16820 }
16821 
16822 /* Swap the register information for registers I and I + 1, which
16823    currently have the wrong endianness.  Note that the registers'
16824    fixedness and call-clobberedness might have been set on the
16825    command line.  */
16826 
16827 static void
16828 mips_swap_registers (unsigned int i)
16829 {
16830   int tmpi;
16831   const char *tmps;
16832 
16833 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
16834 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
16835 
16836   SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
16837   SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
16838   SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
16839   SWAP_STRING (reg_names[i], reg_names[i + 1]);
16840 
16841 #undef SWAP_STRING
16842 #undef SWAP_INT
16843 }
16844 
16845 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE.  */
16846 
16847 static void
16848 mips_conditional_register_usage (void)
16849 {
16850 
16851   if (ISA_HAS_DSP)
16852     {
16853       /* These DSP control register fields are global.  */
16854       global_regs[CCDSP_PO_REGNUM] = 1;
16855       global_regs[CCDSP_SC_REGNUM] = 1;
16856     }
16857   else
16858     AND_COMPL_HARD_REG_SET (accessible_reg_set,
16859 			    reg_class_contents[(int) DSP_ACC_REGS]);
16860 
16861   if (!TARGET_HARD_FLOAT)
16862     {
16863       AND_COMPL_HARD_REG_SET (accessible_reg_set,
16864 			      reg_class_contents[(int) FP_REGS]);
16865       AND_COMPL_HARD_REG_SET (accessible_reg_set,
16866 			      reg_class_contents[(int) ST_REGS]);
16867     }
16868   else if (!ISA_HAS_8CC)
16869     {
16870       /* We only have a single condition-code register.  We implement
16871 	 this by fixing all the condition-code registers and generating
16872 	 RTL that refers directly to ST_REG_FIRST.  */
16873       AND_COMPL_HARD_REG_SET (accessible_reg_set,
16874 			      reg_class_contents[(int) ST_REGS]);
16875       SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
16876       fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
16877     }
16878   if (TARGET_MIPS16)
16879     {
16880       /* In MIPS16 mode, we permit the $t temporary registers to be used
16881 	 for reload.  We prohibit the unused $s registers, since they
16882 	 are call-saved, and saving them via a MIPS16 register would
16883 	 probably waste more time than just reloading the value.  */
16884       fixed_regs[18] = call_used_regs[18] = 1;
16885       fixed_regs[19] = call_used_regs[19] = 1;
16886       fixed_regs[20] = call_used_regs[20] = 1;
16887       fixed_regs[21] = call_used_regs[21] = 1;
16888       fixed_regs[22] = call_used_regs[22] = 1;
16889       fixed_regs[23] = call_used_regs[23] = 1;
16890       fixed_regs[26] = call_used_regs[26] = 1;
16891       fixed_regs[27] = call_used_regs[27] = 1;
16892       fixed_regs[30] = call_used_regs[30] = 1;
16893 
16894       /* Do not allow HI and LO to be treated as register operands.
16895 	 There are no MTHI or MTLO instructions (or any real need
16896 	 for them) and one-way registers cannot easily be reloaded.  */
16897       AND_COMPL_HARD_REG_SET (operand_reg_set,
16898 			      reg_class_contents[(int) MD_REGS]);
16899     }
16900   /* $f20-$f23 are call-clobbered for n64.  */
16901   if (mips_abi == ABI_64)
16902     {
16903       int regno;
16904       for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
16905 	call_really_used_regs[regno] = call_used_regs[regno] = 1;
16906     }
16907   /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
16908      for n32.  */
16909   if (mips_abi == ABI_N32)
16910     {
16911       int regno;
16912       for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
16913 	call_really_used_regs[regno] = call_used_regs[regno] = 1;
16914     }
16915   /* Make sure that double-register accumulator values are correctly
16916      ordered for the current endianness.  */
16917   if (TARGET_LITTLE_ENDIAN)
16918     {
16919       unsigned int regno;
16920 
16921       mips_swap_registers (MD_REG_FIRST);
16922       for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
16923 	mips_swap_registers (regno);
16924     }
16925 }
16926 
16927 /* When generating MIPS16 code, we want to allocate $24 (T_REG) before
16928    other registers for instructions for which it is possible.  This
16929    encourages the compiler to use CMP in cases where an XOR would
16930    require some register shuffling.  */
16931 
16932 void
16933 mips_order_regs_for_local_alloc (void)
16934 {
16935   int i;
16936 
16937   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
16938     reg_alloc_order[i] = i;
16939 
16940   if (TARGET_MIPS16)
16941     {
16942       /* It really doesn't matter where we put register 0, since it is
16943          a fixed register anyhow.  */
16944       reg_alloc_order[0] = 24;
16945       reg_alloc_order[24] = 0;
16946     }
16947 }
16948 
16949 /* Implement EH_USES.  */
16950 
16951 bool
16952 mips_eh_uses (unsigned int regno)
16953 {
16954   if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
16955     {
16956       /* We need to force certain registers to be live in order to handle
16957 	 PIC long branches correctly.  See mips_must_initialize_gp_p for
16958 	 details.  */
16959       if (mips_cfun_has_cprestore_slot_p ())
16960 	{
16961 	  if (regno == CPRESTORE_SLOT_REGNUM)
16962 	    return true;
16963 	}
16964       else
16965 	{
16966 	  if (cfun->machine->global_pointer == regno)
16967 	    return true;
16968 	}
16969     }
16970 
16971   return false;
16972 }
16973 
16974 /* Implement EPILOGUE_USES.  */
16975 
16976 bool
16977 mips_epilogue_uses (unsigned int regno)
16978 {
16979   /* Say that the epilogue uses the return address register.  Note that
16980      in the case of sibcalls, the values "used by the epilogue" are
16981      considered live at the start of the called function.  */
16982   if (regno == RETURN_ADDR_REGNUM)
16983     return true;
16984 
16985   /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
16986      See the comment above load_call<mode> for details.  */
16987   if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
16988     return true;
16989 
16990   /* An interrupt handler must preserve some registers that are
16991      ordinarily call-clobbered.  */
16992   if (cfun->machine->interrupt_handler_p
16993       && mips_interrupt_extra_call_saved_reg_p (regno))
16994     return true;
16995 
16996   return false;
16997 }
16998 
16999 /* A for_each_rtx callback.  Stop the search if *X is an AT register.  */
17000 
17001 static int
17002 mips_at_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
17003 {
17004   return REG_P (*x) && REGNO (*x) == AT_REGNUM;
17005 }
17006 
17007 /* Return true if INSN needs to be wrapped in ".set noat".
17008    INSN has NOPERANDS operands, stored in OPVEC.  */
17009 
17010 static bool
17011 mips_need_noat_wrapper_p (rtx insn, rtx *opvec, int noperands)
17012 {
17013   int i;
17014 
17015   if (recog_memoized (insn) >= 0)
17016     for (i = 0; i < noperands; i++)
17017       if (for_each_rtx (&opvec[i], mips_at_reg_p, NULL))
17018 	return true;
17019   return false;
17020 }
17021 
17022 /* Implement FINAL_PRESCAN_INSN.  */
17023 
17024 void
17025 mips_final_prescan_insn (rtx insn, rtx *opvec, int noperands)
17026 {
17027   if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17028     mips_push_asm_switch (&mips_noat);
17029 }
17030 
17031 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN.  */
17032 
17033 static void
17034 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx insn,
17035 			  rtx *opvec, int noperands)
17036 {
17037   if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17038     mips_pop_asm_switch (&mips_noat);
17039 }
17040 
17041 /* Return the function that is used to expand the <u>mulsidi3 pattern.
17042    EXT_CODE is the code of the extension used.  Return NULL if widening
17043    multiplication shouldn't be used.  */
17044 
17045 mulsidi3_gen_fn
17046 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
17047 {
17048   bool signed_p;
17049 
17050   signed_p = ext_code == SIGN_EXTEND;
17051   if (TARGET_64BIT)
17052     {
17053       /* Don't use widening multiplication with MULT when we have DMUL.  Even
17054 	 with the extension of its input operands DMUL is faster.  Note that
17055 	 the extension is not needed for signed multiplication.  In order to
17056 	 ensure that we always remove the redundant sign-extension in this
17057 	 case we still expand mulsidi3 for DMUL.  */
17058       if (ISA_HAS_DMUL3)
17059 	return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
17060       if (TARGET_MIPS16)
17061 	return (signed_p
17062 		? gen_mulsidi3_64bit_mips16
17063 		: gen_umulsidi3_64bit_mips16);
17064       if (TARGET_FIX_R4000)
17065 	return NULL;
17066       return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
17067     }
17068   else
17069     {
17070       if (TARGET_MIPS16)
17071 	return (signed_p
17072 		? gen_mulsidi3_32bit_mips16
17073 		: gen_umulsidi3_32bit_mips16);
17074       if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
17075 	return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
17076       return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
17077     }
17078 }
17079 
17080 /* Return the size in bytes of the trampoline code, padded to
17081    TRAMPOLINE_ALIGNMENT bits.  The static chain pointer and target
17082    function address immediately follow.  */
17083 
17084 int
17085 mips_trampoline_code_size (void)
17086 {
17087   if (TARGET_USE_PIC_FN_ADDR_REG)
17088     return 4 * 4;
17089   else if (ptr_mode == DImode)
17090     return 8 * 4;
17091   else if (ISA_HAS_LOAD_DELAY)
17092     return 6 * 4;
17093   else
17094     return 4 * 4;
17095 }
17096 
17097 /* Implement TARGET_TRAMPOLINE_INIT.  */
17098 
17099 static void
17100 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
17101 {
17102   rtx addr, end_addr, high, low, opcode, mem;
17103   rtx trampoline[8];
17104   unsigned int i, j;
17105   HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
17106 
17107   /* Work out the offsets of the pointers from the start of the
17108      trampoline code.  */
17109   end_addr_offset = mips_trampoline_code_size ();
17110   static_chain_offset = end_addr_offset;
17111   target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
17112 
17113   /* Get pointers to the beginning and end of the code block.  */
17114   addr = force_reg (Pmode, XEXP (m_tramp, 0));
17115   end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
17116 
17117 #define OP(X) gen_int_mode (X, SImode)
17118 
17119   /* Build up the code in TRAMPOLINE.  */
17120   i = 0;
17121   if (TARGET_USE_PIC_FN_ADDR_REG)
17122     {
17123       /* $25 contains the address of the trampoline.  Emit code of the form:
17124 
17125 	     l[wd]    $1, target_function_offset($25)
17126 	     l[wd]    $static_chain, static_chain_offset($25)
17127 	     jr       $1
17128 	     move     $25,$1.  */
17129       trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
17130 					   target_function_offset,
17131 					   PIC_FUNCTION_ADDR_REGNUM));
17132       trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17133 					   static_chain_offset,
17134 					   PIC_FUNCTION_ADDR_REGNUM));
17135       trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
17136       trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
17137     }
17138   else if (ptr_mode == DImode)
17139     {
17140       /* It's too cumbersome to create the full 64-bit address, so let's
17141 	 instead use:
17142 
17143 	     move    $1, $31
17144 	     bal     1f
17145 	     nop
17146 	 1:  l[wd]   $25, target_function_offset - 12($31)
17147 	     l[wd]   $static_chain, static_chain_offset - 12($31)
17148 	     jr      $25
17149 	     move    $31, $1
17150 
17151 	where 12 is the offset of "1:" from the start of the code block.  */
17152       trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
17153       trampoline[i++] = OP (MIPS_BAL (1));
17154       trampoline[i++] = OP (MIPS_NOP);
17155       trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
17156 					   target_function_offset - 12,
17157 					   RETURN_ADDR_REGNUM));
17158       trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17159 					   static_chain_offset - 12,
17160 					   RETURN_ADDR_REGNUM));
17161       trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17162       trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
17163     }
17164   else
17165     {
17166       /* If the target has load delays, emit:
17167 
17168 	     lui     $1, %hi(end_addr)
17169 	     lw      $25, %lo(end_addr + ...)($1)
17170 	     lw      $static_chain, %lo(end_addr + ...)($1)
17171 	     jr      $25
17172 	     nop
17173 
17174 	 Otherwise emit:
17175 
17176 	     lui     $1, %hi(end_addr)
17177 	     lw      $25, %lo(end_addr + ...)($1)
17178 	     jr      $25
17179 	     lw      $static_chain, %lo(end_addr + ...)($1).  */
17180 
17181       /* Split END_ADDR into %hi and %lo values.  Trampolines are aligned
17182 	 to 64 bits, so the %lo value will have the bottom 3 bits clear.  */
17183       high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
17184 				  NULL, false, OPTAB_WIDEN);
17185       high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
17186 				  NULL, false, OPTAB_WIDEN);
17187       low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
17188 
17189       /* Emit the LUI.  */
17190       opcode = OP (MIPS_LUI (AT_REGNUM, 0));
17191       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
17192 					     NULL, false, OPTAB_WIDEN);
17193 
17194       /* Emit the load of the target function.  */
17195       opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
17196 				  target_function_offset - end_addr_offset,
17197 				  AT_REGNUM));
17198       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
17199 					     NULL, false, OPTAB_WIDEN);
17200 
17201       /* Emit the JR here, if we can.  */
17202       if (!ISA_HAS_LOAD_DELAY)
17203 	trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17204 
17205       /* Emit the load of the static chain register.  */
17206       opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17207 				  static_chain_offset - end_addr_offset,
17208 				  AT_REGNUM));
17209       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
17210 					     NULL, false, OPTAB_WIDEN);
17211 
17212       /* Emit the JR, if we couldn't above.  */
17213       if (ISA_HAS_LOAD_DELAY)
17214 	{
17215 	  trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17216 	  trampoline[i++] = OP (MIPS_NOP);
17217 	}
17218     }
17219 
17220 #undef OP
17221 
17222   /* Copy the trampoline code.  Leave any padding uninitialized.  */
17223   for (j = 0; j < i; j++)
17224     {
17225       mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
17226       mips_emit_move (mem, trampoline[j]);
17227     }
17228 
17229   /* Set up the static chain pointer field.  */
17230   mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
17231   mips_emit_move (mem, chain_value);
17232 
17233   /* Set up the target function field.  */
17234   mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
17235   mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
17236 
17237   /* Flush the code part of the trampoline.  */
17238   emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
17239   emit_insn (gen_clear_cache (addr, end_addr));
17240 }
17241 
17242 /* Implement FUNCTION_PROFILER.  */
17243 
17244 void mips_function_profiler (FILE *file)
17245 {
17246   if (TARGET_MIPS16)
17247     sorry ("mips16 function profiling");
17248   if (TARGET_LONG_CALLS)
17249     {
17250       /* For TARGET_LONG_CALLS use $3 for the address of _mcount.  */
17251       if (Pmode == DImode)
17252 	fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
17253       else
17254 	fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
17255     }
17256   mips_push_asm_switch (&mips_noat);
17257   fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
17258 	   reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
17259   /* _mcount treats $2 as the static chain register.  */
17260   if (cfun->static_chain_decl != NULL)
17261     fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
17262 	     reg_names[STATIC_CHAIN_REGNUM]);
17263   if (TARGET_MCOUNT_RA_ADDRESS)
17264     {
17265       /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
17266 	 ra save location.  */
17267       if (cfun->machine->frame.ra_fp_offset == 0)
17268 	/* ra not saved, pass zero.  */
17269 	fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
17270       else
17271 	fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
17272 		 Pmode == DImode ? "dla" : "la", reg_names[12],
17273 		 cfun->machine->frame.ra_fp_offset,
17274 		 reg_names[STACK_POINTER_REGNUM]);
17275     }
17276   if (!TARGET_NEWABI)
17277     fprintf (file,
17278 	     "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from  stack\n",
17279 	     TARGET_64BIT ? "dsubu" : "subu",
17280 	     reg_names[STACK_POINTER_REGNUM],
17281 	     reg_names[STACK_POINTER_REGNUM],
17282 	     Pmode == DImode ? 16 : 8);
17283 
17284   if (TARGET_LONG_CALLS)
17285     fprintf (file, "\tjalr\t%s\n", reg_names[3]);
17286   else
17287     fprintf (file, "\tjal\t_mcount\n");
17288   mips_pop_asm_switch (&mips_noat);
17289   /* _mcount treats $2 as the static chain register.  */
17290   if (cfun->static_chain_decl != NULL)
17291     fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
17292 	     reg_names[2]);
17293 }
17294 
17295 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  We want to keep the default
17296    behaviour of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
17297    when TARGET_LOONGSON_VECTORS is true.  */
17298 
17299 static unsigned HOST_WIDE_INT
17300 mips_shift_truncation_mask (enum machine_mode mode)
17301 {
17302   if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
17303     return 0;
17304 
17305   return GET_MODE_BITSIZE (mode) - 1;
17306 }
17307 
17308 /* Implement TARGET_PREPARE_PCH_SAVE.  */
17309 
17310 static void
17311 mips_prepare_pch_save (void)
17312 {
17313   /* We are called in a context where the current MIPS16 vs. non-MIPS16
17314      setting should be irrelevant.  The question then is: which setting
17315      makes most sense at load time?
17316 
17317      The PCH is loaded before the first token is read.  We should never
17318      have switched into MIPS16 mode by that point, and thus should not
17319      have populated mips16_globals.  Nor can we load the entire contents
17320      of mips16_globals from the PCH file, because mips16_globals contains
17321      a combination of GGC and non-GGC data.
17322 
17323      There is therefore no point in trying save the GGC part of
17324      mips16_globals to the PCH file, or to preserve MIPS16ness across
17325      the PCH save and load.  The loading compiler would not have access
17326      to the non-GGC parts of mips16_globals (either from the PCH file,
17327      or from a copy that the loading compiler generated itself) and would
17328      have to call target_reinit anyway.
17329 
17330      It therefore seems best to switch back to non-MIPS16 mode at
17331      save time, and to ensure that mips16_globals remains null after
17332      a PCH load.  */
17333   mips_set_mips16_mode (false);
17334   mips16_globals = 0;
17335 }
17336 
17337 /* Generate or test for an insn that supports a constant permutation.  */
17338 
17339 #define MAX_VECT_LEN 8
17340 
17341 struct expand_vec_perm_d
17342 {
17343   rtx target, op0, op1;
17344   unsigned char perm[MAX_VECT_LEN];
17345   enum machine_mode vmode;
17346   unsigned char nelt;
17347   bool one_vector_p;
17348   bool testing_p;
17349 };
17350 
17351 /* Construct (set target (vec_select op0 (parallel perm))) and
17352    return true if that's a valid instruction in the active ISA.  */
17353 
17354 static bool
17355 mips_expand_vselect (rtx target, rtx op0,
17356 		     const unsigned char *perm, unsigned nelt)
17357 {
17358   rtx rperm[MAX_VECT_LEN], x;
17359   unsigned i;
17360 
17361   for (i = 0; i < nelt; ++i)
17362     rperm[i] = GEN_INT (perm[i]);
17363 
17364   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
17365   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
17366   x = gen_rtx_SET (VOIDmode, target, x);
17367 
17368   x = emit_insn (x);
17369   if (recog_memoized (x) < 0)
17370     {
17371       remove_insn (x);
17372       return false;
17373     }
17374   return true;
17375 }
17376 
17377 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
17378 
17379 static bool
17380 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
17381 			     const unsigned char *perm, unsigned nelt)
17382 {
17383   enum machine_mode v2mode;
17384   rtx x;
17385 
17386   v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
17387   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
17388   return mips_expand_vselect (target, x, perm, nelt);
17389 }
17390 
17391 /* Recognize patterns for even-odd extraction.  */
17392 
17393 static bool
17394 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
17395 {
17396   unsigned i, odd, nelt = d->nelt;
17397   rtx t0, t1, t2, t3;
17398 
17399   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
17400     return false;
17401   /* Even-odd for V2SI/V2SFmode is matched by interleave directly.  */
17402   if (nelt < 4)
17403     return false;
17404 
17405   odd = d->perm[0];
17406   if (odd > 1)
17407     return false;
17408   for (i = 1; i < nelt; ++i)
17409     if (d->perm[i] != i * 2 + odd)
17410       return false;
17411 
17412   if (d->testing_p)
17413     return true;
17414 
17415   /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
17416   t0 = gen_reg_rtx (d->vmode);
17417   t1 = gen_reg_rtx (d->vmode);
17418   switch (d->vmode)
17419     {
17420     case V4HImode:
17421       emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
17422       emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
17423       if (odd)
17424 	emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
17425       else
17426 	emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
17427       break;
17428 
17429     case V8QImode:
17430       t2 = gen_reg_rtx (d->vmode);
17431       t3 = gen_reg_rtx (d->vmode);
17432       emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
17433       emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
17434       emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
17435       emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
17436       if (odd)
17437 	emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
17438       else
17439 	emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
17440       break;
17441 
17442     default:
17443       gcc_unreachable ();
17444     }
17445   return true;
17446 }
17447 
17448 /* Recognize patterns for the Loongson PSHUFH instruction.  */
17449 
17450 static bool
17451 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
17452 {
17453   unsigned i, mask;
17454   rtx rmask;
17455 
17456   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
17457     return false;
17458   if (d->vmode != V4HImode)
17459     return false;
17460   if (d->testing_p)
17461     return true;
17462 
17463   /* Convert the selector into the packed 8-bit form for pshufh.  */
17464   /* Recall that loongson is little-endian only.  No big-endian
17465      adjustment required.  */
17466   for (i = mask = 0; i < 4; i++)
17467     mask |= (d->perm[i] & 3) << (i * 2);
17468   rmask = force_reg (SImode, GEN_INT (mask));
17469 
17470   if (d->one_vector_p)
17471     emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
17472   else
17473     {
17474       rtx t0, t1, x, merge, rmerge[4];
17475 
17476       t0 = gen_reg_rtx (V4HImode);
17477       t1 = gen_reg_rtx (V4HImode);
17478       emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
17479       emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
17480 
17481       for (i = 0; i < 4; ++i)
17482 	rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
17483       merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
17484       merge = force_reg (V4HImode, merge);
17485 
17486       x = gen_rtx_AND (V4HImode, merge, t1);
17487       emit_insn (gen_rtx_SET (VOIDmode, t1, x));
17488 
17489       x = gen_rtx_NOT (V4HImode, merge);
17490       x = gen_rtx_AND (V4HImode, x, t0);
17491       emit_insn (gen_rtx_SET (VOIDmode, t0, x));
17492 
17493       x = gen_rtx_IOR (V4HImode, t0, t1);
17494       emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
17495     }
17496 
17497   return true;
17498 }
17499 
17500 /* Recognize broadcast patterns for the Loongson.  */
17501 
17502 static bool
17503 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
17504 {
17505   unsigned i, elt;
17506   rtx t0, t1;
17507 
17508   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
17509     return false;
17510   /* Note that we've already matched V2SI via punpck and V4HI via pshufh.  */
17511   if (d->vmode != V8QImode)
17512     return false;
17513   if (!d->one_vector_p)
17514     return false;
17515 
17516   elt = d->perm[0];
17517   for (i = 1; i < 8; ++i)
17518     if (d->perm[i] != elt)
17519       return false;
17520 
17521   if (d->testing_p)
17522     return true;
17523 
17524   /* With one interleave we put two of the desired element adjacent.  */
17525   t0 = gen_reg_rtx (V8QImode);
17526   if (elt < 4)
17527     emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
17528   else
17529     emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
17530 
17531   /* Shuffle that one HImode element into all locations.  */
17532   elt &= 3;
17533   elt *= 0x55;
17534   t1 = gen_reg_rtx (V4HImode);
17535   emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
17536 				  force_reg (SImode, GEN_INT (elt))));
17537 
17538   emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
17539   return true;
17540 }
17541 
17542 static bool
17543 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
17544 {
17545   unsigned int i, nelt = d->nelt;
17546   unsigned char perm2[MAX_VECT_LEN];
17547 
17548   if (d->one_vector_p)
17549     {
17550       /* Try interleave with alternating operands.  */
17551       memcpy (perm2, d->perm, sizeof(perm2));
17552       for (i = 1; i < nelt; i += 2)
17553 	perm2[i] += nelt;
17554       if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
17555 	return true;
17556     }
17557   else
17558     {
17559       if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
17560 				       d->perm, nelt))
17561 	return true;
17562 
17563       /* Try again with swapped operands.  */
17564       for (i = 0; i < nelt; ++i)
17565 	perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
17566       if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
17567 	return true;
17568     }
17569 
17570   if (mips_expand_vpc_loongson_even_odd (d))
17571     return true;
17572   if (mips_expand_vpc_loongson_pshufh (d))
17573     return true;
17574   if (mips_expand_vpc_loongson_bcast (d))
17575     return true;
17576   return false;
17577 }
17578 
17579 /* Expand a vec_perm_const pattern.  */
17580 
17581 bool
17582 mips_expand_vec_perm_const (rtx operands[4])
17583 {
17584   struct expand_vec_perm_d d;
17585   int i, nelt, which;
17586   unsigned char orig_perm[MAX_VECT_LEN];
17587   rtx sel;
17588   bool ok;
17589 
17590   d.target = operands[0];
17591   d.op0 = operands[1];
17592   d.op1 = operands[2];
17593   sel = operands[3];
17594 
17595   d.vmode = GET_MODE (d.target);
17596   gcc_assert (VECTOR_MODE_P (d.vmode));
17597   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
17598   d.testing_p = false;
17599 
17600   for (i = which = 0; i < nelt; ++i)
17601     {
17602       rtx e = XVECEXP (sel, 0, i);
17603       int ei = INTVAL (e) & (2 * nelt - 1);
17604       which |= (ei < nelt ? 1 : 2);
17605       orig_perm[i] = ei;
17606     }
17607   memcpy (d.perm, orig_perm, MAX_VECT_LEN);
17608 
17609   switch (which)
17610     {
17611     default:
17612       gcc_unreachable();
17613 
17614     case 3:
17615       d.one_vector_p = false;
17616       if (!rtx_equal_p (d.op0, d.op1))
17617 	break;
17618       /* FALLTHRU */
17619 
17620     case 2:
17621       for (i = 0; i < nelt; ++i)
17622         d.perm[i] &= nelt - 1;
17623       d.op0 = d.op1;
17624       d.one_vector_p = true;
17625       break;
17626 
17627     case 1:
17628       d.op1 = d.op0;
17629       d.one_vector_p = true;
17630       break;
17631     }
17632 
17633   ok = mips_expand_vec_perm_const_1 (&d);
17634 
17635   /* If we were given a two-vector permutation which just happened to
17636      have both input vectors equal, we folded this into a one-vector
17637      permutation.  There are several loongson patterns that are matched
17638      via direct vec_select+vec_concat expansion, but we do not have
17639      support in mips_expand_vec_perm_const_1 to guess the adjustment
17640      that should be made for a single operand.  Just try again with
17641      the original permutation.  */
17642   if (!ok && which == 3)
17643     {
17644       d.op0 = operands[1];
17645       d.op1 = operands[2];
17646       d.one_vector_p = false;
17647       memcpy (d.perm, orig_perm, MAX_VECT_LEN);
17648       ok = mips_expand_vec_perm_const_1 (&d);
17649     }
17650 
17651   return ok;
17652 }
17653 
17654 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK.  */
17655 
17656 static bool
17657 mips_vectorize_vec_perm_const_ok (enum machine_mode vmode,
17658 				  const unsigned char *sel)
17659 {
17660   struct expand_vec_perm_d d;
17661   unsigned int i, nelt, which;
17662   bool ret;
17663 
17664   d.vmode = vmode;
17665   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
17666   d.testing_p = true;
17667   memcpy (d.perm, sel, nelt);
17668 
17669   /* Categorize the set of elements in the selector.  */
17670   for (i = which = 0; i < nelt; ++i)
17671     {
17672       unsigned char e = d.perm[i];
17673       gcc_assert (e < 2 * nelt);
17674       which |= (e < nelt ? 1 : 2);
17675     }
17676 
17677   /* For all elements from second vector, fold the elements to first.  */
17678   if (which == 2)
17679     for (i = 0; i < nelt; ++i)
17680       d.perm[i] -= nelt;
17681 
17682   /* Check whether the mask can be applied to the vector type.  */
17683   d.one_vector_p = (which != 3);
17684 
17685   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
17686   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
17687   if (!d.one_vector_p)
17688     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
17689 
17690   start_sequence ();
17691   ret = mips_expand_vec_perm_const_1 (&d);
17692   end_sequence ();
17693 
17694   return ret;
17695 }
17696 
17697 /* Expand an integral vector unpack operation.  */
17698 
17699 void
17700 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
17701 {
17702   enum machine_mode imode = GET_MODE (operands[1]);
17703   rtx (*unpack) (rtx, rtx, rtx);
17704   rtx (*cmpgt) (rtx, rtx, rtx);
17705   rtx tmp, dest, zero;
17706 
17707   switch (imode)
17708     {
17709     case V8QImode:
17710       if (high_p)
17711 	unpack = gen_loongson_punpckhbh;
17712       else
17713 	unpack = gen_loongson_punpcklbh;
17714       cmpgt = gen_loongson_pcmpgtb;
17715       break;
17716     case V4HImode:
17717       if (high_p)
17718 	unpack = gen_loongson_punpckhhw;
17719       else
17720 	unpack = gen_loongson_punpcklhw;
17721       cmpgt = gen_loongson_pcmpgth;
17722       break;
17723     default:
17724       gcc_unreachable ();
17725     }
17726 
17727   zero = force_reg (imode, CONST0_RTX (imode));
17728   if (unsigned_p)
17729     tmp = zero;
17730   else
17731     {
17732       tmp = gen_reg_rtx (imode);
17733       emit_insn (cmpgt (tmp, zero, operands[1]));
17734     }
17735 
17736   dest = gen_reg_rtx (imode);
17737   emit_insn (unpack (dest, operands[1], tmp));
17738 
17739   emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
17740 }
17741 
17742 /* A subroutine of mips_expand_vec_init, match constant vector elements.  */
17743 
17744 static inline bool
17745 mips_constant_elt_p (rtx x)
17746 {
17747   return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
17748 }
17749 
17750 /* A subroutine of mips_expand_vec_init, expand via broadcast.  */
17751 
17752 static void
17753 mips_expand_vi_broadcast (enum machine_mode vmode, rtx target, rtx elt)
17754 {
17755   struct expand_vec_perm_d d;
17756   rtx t1;
17757   bool ok;
17758 
17759   if (elt != const0_rtx)
17760     elt = force_reg (GET_MODE_INNER (vmode), elt);
17761   if (REG_P (elt))
17762     elt = gen_lowpart (DImode, elt);
17763 
17764   t1 = gen_reg_rtx (vmode);
17765   switch (vmode)
17766     {
17767     case V8QImode:
17768       emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
17769       break;
17770     case V4HImode:
17771       emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
17772       break;
17773     default:
17774       gcc_unreachable ();
17775     }
17776 
17777   memset (&d, 0, sizeof (d));
17778   d.target = target;
17779   d.op0 = t1;
17780   d.op1 = t1;
17781   d.vmode = vmode;
17782   d.nelt = GET_MODE_NUNITS (vmode);
17783   d.one_vector_p = true;
17784 
17785   ok = mips_expand_vec_perm_const_1 (&d);
17786   gcc_assert (ok);
17787 }
17788 
17789 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
17790    elements of VALS with zeros, copy the constant vector to TARGET.  */
17791 
17792 static void
17793 mips_expand_vi_constant (enum machine_mode vmode, unsigned nelt,
17794 			 rtx target, rtx vals)
17795 {
17796   rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
17797   unsigned i;
17798 
17799   for (i = 0; i < nelt; ++i)
17800     {
17801       if (!mips_constant_elt_p (RTVEC_ELT (vec, i)))
17802 	RTVEC_ELT (vec, i) = const0_rtx;
17803     }
17804 
17805   emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
17806 }
17807 
17808 
17809 /* A subroutine of mips_expand_vec_init, expand via pinsrh.  */
17810 
17811 static void
17812 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
17813 {
17814   mips_expand_vi_constant (V4HImode, 4, target, vals);
17815 
17816   emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
17817 			      GEN_INT (one_var)));
17818 }
17819 
17820 /* A subroutine of mips_expand_vec_init, expand anything via memory.  */
17821 
17822 static void
17823 mips_expand_vi_general (enum machine_mode vmode, enum machine_mode imode,
17824 			unsigned nelt, unsigned nvar, rtx target, rtx vals)
17825 {
17826   rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
17827   unsigned int i, isize = GET_MODE_SIZE (imode);
17828 
17829   if (nvar < nelt)
17830     mips_expand_vi_constant (vmode, nelt, mem, vals);
17831 
17832   for (i = 0; i < nelt; ++i)
17833     {
17834       rtx x = XVECEXP (vals, 0, i);
17835       if (!mips_constant_elt_p (x))
17836 	emit_move_insn (adjust_address (mem, imode, i * isize), x);
17837     }
17838 
17839   emit_move_insn (target, mem);
17840 }
17841 
17842 /* Expand a vector initialization.  */
17843 
17844 void
17845 mips_expand_vector_init (rtx target, rtx vals)
17846 {
17847   enum machine_mode vmode = GET_MODE (target);
17848   enum machine_mode imode = GET_MODE_INNER (vmode);
17849   unsigned i, nelt = GET_MODE_NUNITS (vmode);
17850   unsigned nvar = 0, one_var = -1u;
17851   bool all_same = true;
17852   rtx x;
17853 
17854   for (i = 0; i < nelt; ++i)
17855     {
17856       x = XVECEXP (vals, 0, i);
17857       if (!mips_constant_elt_p (x))
17858 	nvar++, one_var = i;
17859       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
17860 	all_same = false;
17861     }
17862 
17863   /* Load constants from the pool, or whatever's handy.  */
17864   if (nvar == 0)
17865     {
17866       emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
17867       return;
17868     }
17869 
17870   /* For two-part initialization, always use CONCAT.  */
17871   if (nelt == 2)
17872     {
17873       rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
17874       rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
17875       x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
17876       emit_insn (gen_rtx_SET (VOIDmode, target, x));
17877       return;
17878     }
17879 
17880   /* Loongson is the only cpu with vectors with more elements.  */
17881   gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
17882 
17883   /* If all values are identical, broadcast the value.  */
17884   if (all_same)
17885     {
17886       mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
17887       return;
17888     }
17889 
17890   /* If we've only got one non-variable V4HImode, use PINSRH.  */
17891   if (nvar == 1 && vmode == V4HImode)
17892     {
17893       mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
17894       return;
17895     }
17896 
17897   mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
17898 }
17899 
17900 /* Expand a vector reduction.  */
17901 
17902 void
17903 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
17904 {
17905   enum machine_mode vmode = GET_MODE (in);
17906   unsigned char perm2[2];
17907   rtx last, next, fold, x;
17908   bool ok;
17909 
17910   last = in;
17911   fold = gen_reg_rtx (vmode);
17912   switch (vmode)
17913     {
17914     case V2SFmode:
17915       /* Use PUL/PLU to produce { L, H } op { H, L }.
17916 	 By reversing the pair order, rather than a pure interleave high,
17917 	 we avoid erroneous exceptional conditions that we might otherwise
17918 	 produce from the computation of H op H.  */
17919       perm2[0] = 1;
17920       perm2[1] = 2;
17921       ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
17922       gcc_assert (ok);
17923       break;
17924 
17925     case V2SImode:
17926       /* Use interleave to produce { H, L } op { H, H }.  */
17927       emit_insn (gen_loongson_punpckhwd (fold, last, last));
17928       break;
17929 
17930     case V4HImode:
17931       /* Perform the first reduction with interleave,
17932 	 and subsequent reductions with shifts.  */
17933       emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
17934 
17935       next = gen_reg_rtx (vmode);
17936       emit_insn (gen (next, last, fold));
17937       last = next;
17938 
17939       fold = gen_reg_rtx (vmode);
17940       x = force_reg (SImode, GEN_INT (16));
17941       emit_insn (gen_vec_shr_v4hi (fold, last, x));
17942       break;
17943 
17944     case V8QImode:
17945       emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
17946 
17947       next = gen_reg_rtx (vmode);
17948       emit_insn (gen (next, last, fold));
17949       last = next;
17950 
17951       fold = gen_reg_rtx (vmode);
17952       x = force_reg (SImode, GEN_INT (16));
17953       emit_insn (gen_vec_shr_v8qi (fold, last, x));
17954 
17955       next = gen_reg_rtx (vmode);
17956       emit_insn (gen (next, last, fold));
17957       last = next;
17958 
17959       fold = gen_reg_rtx (vmode);
17960       x = force_reg (SImode, GEN_INT (8));
17961       emit_insn (gen_vec_shr_v8qi (fold, last, x));
17962       break;
17963 
17964     default:
17965       gcc_unreachable ();
17966     }
17967 
17968   emit_insn (gen (target, last, fold));
17969 }
17970 
17971 /* Expand a vector minimum/maximum.  */
17972 
17973 void
17974 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
17975 			rtx (*cmp) (rtx, rtx, rtx), bool min_p)
17976 {
17977   enum machine_mode vmode = GET_MODE (target);
17978   rtx tc, t0, t1, x;
17979 
17980   tc = gen_reg_rtx (vmode);
17981   t0 = gen_reg_rtx (vmode);
17982   t1 = gen_reg_rtx (vmode);
17983 
17984   /* op0 > op1 */
17985   emit_insn (cmp (tc, op0, op1));
17986 
17987   x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
17988   emit_insn (gen_rtx_SET (VOIDmode, t0, x));
17989 
17990   x = gen_rtx_NOT (vmode, tc);
17991   x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
17992   emit_insn (gen_rtx_SET (VOIDmode, t1, x));
17993 
17994   x = gen_rtx_IOR (vmode, t0, t1);
17995   emit_insn (gen_rtx_SET (VOIDmode, target, x));
17996 }
17997 
17998 /* Initialize the GCC target structure.  */
17999 #undef TARGET_ASM_ALIGNED_HI_OP
18000 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
18001 #undef TARGET_ASM_ALIGNED_SI_OP
18002 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
18003 #undef TARGET_ASM_ALIGNED_DI_OP
18004 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
18005 
18006 #undef TARGET_OPTION_OVERRIDE
18007 #define TARGET_OPTION_OVERRIDE mips_option_override
18008 
18009 #undef TARGET_LEGITIMIZE_ADDRESS
18010 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
18011 
18012 #undef TARGET_ASM_FUNCTION_PROLOGUE
18013 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
18014 #undef TARGET_ASM_FUNCTION_EPILOGUE
18015 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
18016 #undef TARGET_ASM_SELECT_RTX_SECTION
18017 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
18018 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
18019 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
18020 
18021 #undef TARGET_SCHED_INIT
18022 #define TARGET_SCHED_INIT mips_sched_init
18023 #undef TARGET_SCHED_REORDER
18024 #define TARGET_SCHED_REORDER mips_sched_reorder
18025 #undef TARGET_SCHED_REORDER2
18026 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
18027 #undef TARGET_SCHED_VARIABLE_ISSUE
18028 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
18029 #undef TARGET_SCHED_ADJUST_COST
18030 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
18031 #undef TARGET_SCHED_ISSUE_RATE
18032 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
18033 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
18034 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
18035 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
18036 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
18037 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
18038 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
18039   mips_multipass_dfa_lookahead
18040 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
18041 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
18042   mips_small_register_classes_for_mode_p
18043 
18044 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
18045 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
18046 
18047 #undef TARGET_INSERT_ATTRIBUTES
18048 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
18049 #undef TARGET_MERGE_DECL_ATTRIBUTES
18050 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
18051 #undef TARGET_SET_CURRENT_FUNCTION
18052 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
18053 
18054 #undef TARGET_VALID_POINTER_MODE
18055 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
18056 #undef TARGET_REGISTER_MOVE_COST
18057 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
18058 #undef TARGET_MEMORY_MOVE_COST
18059 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
18060 #undef TARGET_RTX_COSTS
18061 #define TARGET_RTX_COSTS mips_rtx_costs
18062 #undef TARGET_ADDRESS_COST
18063 #define TARGET_ADDRESS_COST mips_address_cost
18064 
18065 #undef TARGET_IN_SMALL_DATA_P
18066 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
18067 
18068 #undef TARGET_MACHINE_DEPENDENT_REORG
18069 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
18070 
18071 #undef  TARGET_PREFERRED_RELOAD_CLASS
18072 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
18073 
18074 #undef TARGET_EXPAND_TO_RTL_HOOK
18075 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
18076 #undef TARGET_ASM_FILE_START
18077 #define TARGET_ASM_FILE_START mips_file_start
18078 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
18079 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
18080 #undef TARGET_ASM_CODE_END
18081 #define TARGET_ASM_CODE_END mips_code_end
18082 
18083 #undef TARGET_INIT_LIBFUNCS
18084 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
18085 
18086 #undef TARGET_BUILD_BUILTIN_VA_LIST
18087 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
18088 #undef TARGET_EXPAND_BUILTIN_VA_START
18089 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
18090 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
18091 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
18092 
18093 #undef  TARGET_PROMOTE_FUNCTION_MODE
18094 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
18095 #undef TARGET_PROMOTE_PROTOTYPES
18096 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
18097 
18098 #undef TARGET_FUNCTION_VALUE
18099 #define TARGET_FUNCTION_VALUE mips_function_value
18100 #undef TARGET_LIBCALL_VALUE
18101 #define TARGET_LIBCALL_VALUE mips_libcall_value
18102 #undef TARGET_FUNCTION_VALUE_REGNO_P
18103 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
18104 #undef TARGET_RETURN_IN_MEMORY
18105 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
18106 #undef TARGET_RETURN_IN_MSB
18107 #define TARGET_RETURN_IN_MSB mips_return_in_msb
18108 
18109 #undef TARGET_ASM_OUTPUT_MI_THUNK
18110 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
18111 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
18112 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
18113 
18114 #undef TARGET_PRINT_OPERAND
18115 #define TARGET_PRINT_OPERAND mips_print_operand
18116 #undef TARGET_PRINT_OPERAND_ADDRESS
18117 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
18118 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
18119 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
18120 
18121 #undef TARGET_SETUP_INCOMING_VARARGS
18122 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
18123 #undef TARGET_STRICT_ARGUMENT_NAMING
18124 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
18125 #undef TARGET_MUST_PASS_IN_STACK
18126 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
18127 #undef TARGET_PASS_BY_REFERENCE
18128 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
18129 #undef TARGET_CALLEE_COPIES
18130 #define TARGET_CALLEE_COPIES mips_callee_copies
18131 #undef TARGET_ARG_PARTIAL_BYTES
18132 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
18133 #undef TARGET_FUNCTION_ARG
18134 #define TARGET_FUNCTION_ARG mips_function_arg
18135 #undef TARGET_FUNCTION_ARG_ADVANCE
18136 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
18137 #undef TARGET_FUNCTION_ARG_BOUNDARY
18138 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
18139 
18140 #undef TARGET_MODE_REP_EXTENDED
18141 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
18142 
18143 #undef TARGET_VECTOR_MODE_SUPPORTED_P
18144 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
18145 
18146 #undef TARGET_SCALAR_MODE_SUPPORTED_P
18147 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
18148 
18149 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
18150 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
18151 
18152 #undef TARGET_INIT_BUILTINS
18153 #define TARGET_INIT_BUILTINS mips_init_builtins
18154 #undef TARGET_BUILTIN_DECL
18155 #define TARGET_BUILTIN_DECL mips_builtin_decl
18156 #undef TARGET_EXPAND_BUILTIN
18157 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
18158 
18159 #undef TARGET_HAVE_TLS
18160 #define TARGET_HAVE_TLS HAVE_AS_TLS
18161 
18162 #undef TARGET_CANNOT_FORCE_CONST_MEM
18163 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
18164 
18165 #undef TARGET_LEGITIMATE_CONSTANT_P
18166 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
18167 
18168 #undef TARGET_ENCODE_SECTION_INFO
18169 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
18170 
18171 #undef TARGET_ATTRIBUTE_TABLE
18172 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
18173 /* All our function attributes are related to how out-of-line copies should
18174    be compiled or called.  They don't in themselves prevent inlining.  */
18175 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
18176 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
18177 
18178 #undef TARGET_EXTRA_LIVE_ON_ENTRY
18179 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
18180 
18181 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
18182 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
18183 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
18184 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
18185 
18186 #undef  TARGET_COMP_TYPE_ATTRIBUTES
18187 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
18188 
18189 #ifdef HAVE_AS_DTPRELWORD
18190 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
18191 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
18192 #endif
18193 #undef TARGET_DWARF_REGISTER_SPAN
18194 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
18195 
18196 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
18197 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
18198 
18199 #undef TARGET_LEGITIMATE_ADDRESS_P
18200 #define TARGET_LEGITIMATE_ADDRESS_P	mips_legitimate_address_p
18201 
18202 #undef TARGET_FRAME_POINTER_REQUIRED
18203 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
18204 
18205 #undef TARGET_CAN_ELIMINATE
18206 #define TARGET_CAN_ELIMINATE mips_can_eliminate
18207 
18208 #undef TARGET_CONDITIONAL_REGISTER_USAGE
18209 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
18210 
18211 #undef TARGET_TRAMPOLINE_INIT
18212 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
18213 
18214 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
18215 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
18216 
18217 #undef TARGET_SHIFT_TRUNCATION_MASK
18218 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
18219 
18220 #undef TARGET_PREPARE_PCH_SAVE
18221 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
18222 
18223 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
18224 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK mips_vectorize_vec_perm_const_ok
18225 
18226 struct gcc_target targetm = TARGET_INITIALIZER;
18227 
18228 #include "gt-mips.h"
18229