1 /* Subroutines used for MIPS code generation.
2    Copyright (C) 1989-2018 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 #define IN_TARGET_CODE 1
25 
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "backend.h"
30 #include "target.h"
31 #include "rtl.h"
32 #include "tree.h"
33 #include "memmodel.h"
34 #include "gimple.h"
35 #include "cfghooks.h"
36 #include "df.h"
37 #include "tm_p.h"
38 #include "stringpool.h"
39 #include "attribs.h"
40 #include "optabs.h"
41 #include "regs.h"
42 #include "emit-rtl.h"
43 #include "recog.h"
44 #include "cgraph.h"
45 #include "diagnostic.h"
46 #include "insn-attr.h"
47 #include "output.h"
48 #include "alias.h"
49 #include "fold-const.h"
50 #include "varasm.h"
51 #include "stor-layout.h"
52 #include "calls.h"
53 #include "explow.h"
54 #include "expr.h"
55 #include "libfuncs.h"
56 #include "reload.h"
57 #include "common/common-target.h"
58 #include "langhooks.h"
59 #include "cfgrtl.h"
60 #include "cfganal.h"
61 #include "sched-int.h"
62 #include "gimplify.h"
63 #include "target-globals.h"
64 #include "tree-pass.h"
65 #include "context.h"
66 #include "builtins.h"
67 #include "rtl-iter.h"
68 
69 /* This file should be included last.  */
70 #include "target-def.h"
71 
72 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF.  */
73 #define UNSPEC_ADDRESS_P(X)					\
74   (GET_CODE (X) == UNSPEC					\
75    && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST			\
76    && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
77 
78 /* Extract the symbol or label from UNSPEC wrapper X.  */
79 #define UNSPEC_ADDRESS(X) \
80   XVECEXP (X, 0, 0)
81 
82 /* Extract the symbol type from UNSPEC wrapper X.  */
83 #define UNSPEC_ADDRESS_TYPE(X) \
84   ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
85 
86 /* The maximum distance between the top of the stack frame and the
87    value $sp has when we save and restore registers.
88 
89    The value for normal-mode code must be a SMALL_OPERAND and must
90    preserve the maximum stack alignment.  We therefore use a value
91    of 0x7ff0 in this case.
92 
93    microMIPS LWM and SWM support 12-bit offsets (from -0x800 to 0x7ff),
94    so we use a maximum of 0x7f0 for TARGET_MICROMIPS.
95 
96    MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
97    up to 0x7f8 bytes and can usually save or restore all the registers
98    that we need to save or restore.  (Note that we can only use these
99    instructions for o32, for which the stack alignment is 8 bytes.)
100 
101    We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
102    RESTORE are not available.  We can then use unextended instructions
103    to save and restore registers, and to allocate and deallocate the top
104    part of the frame.  */
105 #define MIPS_MAX_FIRST_STACK_STEP					\
106   (!TARGET_COMPRESSION ? 0x7ff0						\
107    : TARGET_MICROMIPS || GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8		\
108    : TARGET_64BIT ? 0x100 : 0x400)
109 
110 /* True if INSN is a mips.md pattern or asm statement.  */
111 /* ???	This test exists through the compiler, perhaps it should be
112 	moved to rtl.h.  */
113 #define USEFUL_INSN_P(INSN)						\
114   (NONDEBUG_INSN_P (INSN)						\
115    && GET_CODE (PATTERN (INSN)) != USE					\
116    && GET_CODE (PATTERN (INSN)) != CLOBBER)
117 
118 /* If INSN is a delayed branch sequence, return the first instruction
119    in the sequence, otherwise return INSN itself.  */
120 #define SEQ_BEGIN(INSN)							\
121   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE		\
122    ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN), 0, 0))			\
123    : (INSN))
124 
125 /* Likewise for the last instruction in a delayed branch sequence.  */
126 #define SEQ_END(INSN)							\
127   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE		\
128    ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN),			\
129 				 0,					\
130 				 XVECLEN (PATTERN (INSN), 0) - 1))	\
131    : (INSN))
132 
133 /* Execute the following loop body with SUBINSN set to each instruction
134    between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive.  */
135 #define FOR_EACH_SUBINSN(SUBINSN, INSN)					\
136   for ((SUBINSN) = SEQ_BEGIN (INSN);					\
137        (SUBINSN) != NEXT_INSN (SEQ_END (INSN));				\
138        (SUBINSN) = NEXT_INSN (SUBINSN))
139 
140 /* True if bit BIT is set in VALUE.  */
141 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
142 
143 /* Return the opcode for a ptr_mode load of the form:
144 
145        l[wd]    DEST, OFFSET(BASE).  */
146 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE)	\
147   (((ptr_mode == DImode ? 0x37 : 0x23) << 26)	\
148    | ((BASE) << 21)				\
149    | ((DEST) << 16)				\
150    | (OFFSET))
151 
152 /* Return the opcode to move register SRC into register DEST.  */
153 #define MIPS_MOVE(DEST, SRC)		\
154   ((TARGET_64BIT ? 0x2d : 0x21)		\
155    | ((DEST) << 11)			\
156    | ((SRC) << 21))
157 
158 /* Return the opcode for:
159 
160        lui      DEST, VALUE.  */
161 #define MIPS_LUI(DEST, VALUE) \
162   ((0xf << 26) | ((DEST) << 16) | (VALUE))
163 
164 /* Return the opcode to jump to register DEST.  When the JR opcode is not
165    available use JALR $0, DEST.  */
166 #define MIPS_JR(DEST) \
167   (TARGET_CB_ALWAYS ? ((0x1b << 27) | ((DEST) << 16)) \
168 		    : (((DEST) << 21) | (ISA_HAS_JR ? 0x8 : 0x9)))
169 
170 /* Return the opcode for:
171 
172        bal     . + (1 + OFFSET) * 4.  */
173 #define MIPS_BAL(OFFSET) \
174   ((0x1 << 26) | (0x11 << 16) | (OFFSET))
175 
176 /* Return the usual opcode for a nop.  */
177 #define MIPS_NOP 0
178 
179 /* Classifies an address.
180 
181    ADDRESS_REG
182        A natural register + offset address.  The register satisfies
183        mips_valid_base_register_p and the offset is a const_arith_operand.
184 
185    ADDRESS_LO_SUM
186        A LO_SUM rtx.  The first operand is a valid base register and
187        the second operand is a symbolic address.
188 
189    ADDRESS_CONST_INT
190        A signed 16-bit constant address.
191 
192    ADDRESS_SYMBOLIC:
193        A constant symbolic address.  */
194 enum mips_address_type {
195   ADDRESS_REG,
196   ADDRESS_LO_SUM,
197   ADDRESS_CONST_INT,
198   ADDRESS_SYMBOLIC
199 };
200 
201 /* Macros to create an enumeration identifier for a function prototype.  */
202 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
203 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
204 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
205 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
206 
207 /* Classifies the prototype of a built-in function.  */
208 enum mips_function_type {
209 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
210 #include "config/mips/mips-ftypes.def"
211 #undef DEF_MIPS_FTYPE
212   MIPS_MAX_FTYPE_MAX
213 };
214 
215 /* Specifies how a built-in function should be converted into rtl.  */
216 enum mips_builtin_type {
217   /* The function corresponds directly to an .md pattern.  The return
218      value is mapped to operand 0 and the arguments are mapped to
219      operands 1 and above.  */
220   MIPS_BUILTIN_DIRECT,
221 
222   /* The function corresponds directly to an .md pattern.  There is no return
223      value and the arguments are mapped to operands 0 and above.  */
224   MIPS_BUILTIN_DIRECT_NO_TARGET,
225 
226   /* The function corresponds to a comparison instruction followed by
227      a mips_cond_move_tf_ps pattern.  The first two arguments are the
228      values to compare and the second two arguments are the vector
229      operands for the movt.ps or movf.ps instruction (in assembly order).  */
230   MIPS_BUILTIN_MOVF,
231   MIPS_BUILTIN_MOVT,
232 
233   /* The function corresponds to a V2SF comparison instruction.  Operand 0
234      of this instruction is the result of the comparison, which has mode
235      CCV2 or CCV4.  The function arguments are mapped to operands 1 and
236      above.  The function's return value is an SImode boolean that is
237      true under the following conditions:
238 
239      MIPS_BUILTIN_CMP_ANY: one of the registers is true
240      MIPS_BUILTIN_CMP_ALL: all of the registers are true
241      MIPS_BUILTIN_CMP_LOWER: the first register is true
242      MIPS_BUILTIN_CMP_UPPER: the second register is true.  */
243   MIPS_BUILTIN_CMP_ANY,
244   MIPS_BUILTIN_CMP_ALL,
245   MIPS_BUILTIN_CMP_UPPER,
246   MIPS_BUILTIN_CMP_LOWER,
247 
248   /* As above, but the instruction only sets a single $fcc register.  */
249   MIPS_BUILTIN_CMP_SINGLE,
250 
251   /* The function corresponds to an MSA conditional branch instruction
252      combined with a compare instruction.  */
253   MIPS_BUILTIN_MSA_TEST_BRANCH,
254 
255   /* For generating bposge32 branch instructions in MIPS32 DSP ASE.  */
256   MIPS_BUILTIN_BPOSGE32
257 };
258 
259 /* Invoke MACRO (COND) for each C.cond.fmt condition.  */
260 #define MIPS_FP_CONDITIONS(MACRO) \
261   MACRO (f),	\
262   MACRO (un),	\
263   MACRO (eq),	\
264   MACRO (ueq),	\
265   MACRO (olt),	\
266   MACRO (ult),	\
267   MACRO (ole),	\
268   MACRO (ule),	\
269   MACRO (sf),	\
270   MACRO (ngle),	\
271   MACRO (seq),	\
272   MACRO (ngl),	\
273   MACRO (lt),	\
274   MACRO (nge),	\
275   MACRO (le),	\
276   MACRO (ngt)
277 
278 /* Enumerates the codes above as MIPS_FP_COND_<X>.  */
279 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
280 enum mips_fp_condition {
281   MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
282 };
283 #undef DECLARE_MIPS_COND
284 
285 /* Index X provides the string representation of MIPS_FP_COND_<X>.  */
286 #define STRINGIFY(X) #X
287 static const char *const mips_fp_conditions[] = {
288   MIPS_FP_CONDITIONS (STRINGIFY)
289 };
290 #undef STRINGIFY
291 
292 /* A class used to control a comdat-style stub that we output in each
293    translation unit that needs it.  */
294 class mips_one_only_stub {
295 public:
~mips_one_only_stub()296   virtual ~mips_one_only_stub () {}
297 
298   /* Return the name of the stub.  */
299   virtual const char *get_name () = 0;
300 
301   /* Output the body of the function to asm_out_file.  */
302   virtual void output_body () = 0;
303 };
304 
305 /* Tuning information that is automatically derived from other sources
306    (such as the scheduler).  */
307 static struct {
308   /* The architecture and tuning settings that this structure describes.  */
309   enum processor arch;
310   enum processor tune;
311 
312   /* True if this structure describes MIPS16 settings.  */
313   bool mips16_p;
314 
315   /* True if the structure has been initialized.  */
316   bool initialized_p;
317 
318   /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
319      when optimizing for speed.  */
320   bool fast_mult_zero_zero_p;
321 } mips_tuning_info;
322 
323 /* Information about a single argument.  */
324 struct mips_arg_info {
325   /* True if the argument is passed in a floating-point register, or
326      would have been if we hadn't run out of registers.  */
327   bool fpr_p;
328 
329   /* The number of words passed in registers, rounded up.  */
330   unsigned int reg_words;
331 
332   /* For EABI, the offset of the first register from GP_ARG_FIRST or
333      FP_ARG_FIRST.  For other ABIs, the offset of the first register from
334      the start of the ABI's argument structure (see the CUMULATIVE_ARGS
335      comment for details).
336 
337      The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
338      on the stack.  */
339   unsigned int reg_offset;
340 
341   /* The number of words that must be passed on the stack, rounded up.  */
342   unsigned int stack_words;
343 
344   /* The offset from the start of the stack overflow area of the argument's
345      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
346   unsigned int stack_offset;
347 };
348 
349 /* Information about an address described by mips_address_type.
350 
351    ADDRESS_CONST_INT
352        No fields are used.
353 
354    ADDRESS_REG
355        REG is the base register and OFFSET is the constant offset.
356 
357    ADDRESS_LO_SUM
358        REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
359        is the type of symbol it references.
360 
361    ADDRESS_SYMBOLIC
362        SYMBOL_TYPE is the type of symbol that the address references.  */
363 struct mips_address_info {
364   enum mips_address_type type;
365   rtx reg;
366   rtx offset;
367   enum mips_symbol_type symbol_type;
368 };
369 
370 /* One stage in a constant building sequence.  These sequences have
371    the form:
372 
373 	A = VALUE[0]
374 	A = A CODE[1] VALUE[1]
375 	A = A CODE[2] VALUE[2]
376 	...
377 
378    where A is an accumulator, each CODE[i] is a binary rtl operation
379    and each VALUE[i] is a constant integer.  CODE[0] is undefined.  */
380 struct mips_integer_op {
381   enum rtx_code code;
382   unsigned HOST_WIDE_INT value;
383 };
384 
385 /* The largest number of operations needed to load an integer constant.
386    The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
387    When the lowest bit is clear, we can try, but reject a sequence with
388    an extra SLL at the end.  */
389 #define MIPS_MAX_INTEGER_OPS 7
390 
391 /* Information about a MIPS16e SAVE or RESTORE instruction.  */
392 struct mips16e_save_restore_info {
393   /* The number of argument registers saved by a SAVE instruction.
394      0 for RESTORE instructions.  */
395   unsigned int nargs;
396 
397   /* Bit X is set if the instruction saves or restores GPR X.  */
398   unsigned int mask;
399 
400   /* The total number of bytes to allocate.  */
401   HOST_WIDE_INT size;
402 };
403 
404 /* Costs of various operations on the different architectures.  */
405 
406 struct mips_rtx_cost_data
407 {
408   unsigned short fp_add;
409   unsigned short fp_mult_sf;
410   unsigned short fp_mult_df;
411   unsigned short fp_div_sf;
412   unsigned short fp_div_df;
413   unsigned short int_mult_si;
414   unsigned short int_mult_di;
415   unsigned short int_div_si;
416   unsigned short int_div_di;
417   unsigned short branch_cost;
418   unsigned short memory_latency;
419 };
420 
421 /* Global variables for machine-dependent things.  */
422 
423 /* The -G setting, or the configuration's default small-data limit if
424    no -G option is given.  */
425 static unsigned int mips_small_data_threshold;
426 
427 /* The number of file directives written by mips_output_filename.  */
428 int num_source_filenames;
429 
430 /* The name that appeared in the last .file directive written by
431    mips_output_filename, or "" if mips_output_filename hasn't
432    written anything yet.  */
433 const char *current_function_file = "";
434 
435 /* Arrays that map GCC register numbers to debugger register numbers.  */
436 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
437 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
438 
439 /* Information about the current function's epilogue, used only while
440    expanding it.  */
441 static struct {
442   /* A list of queued REG_CFA_RESTORE notes.  */
443   rtx cfa_restores;
444 
445   /* The CFA is currently defined as CFA_REG + CFA_OFFSET.  */
446   rtx cfa_reg;
447   HOST_WIDE_INT cfa_offset;
448 
449   /* The offset of the CFA from the stack pointer while restoring
450      registers.  */
451   HOST_WIDE_INT cfa_restore_sp_offset;
452 } mips_epilogue;
453 
454 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs.  */
455 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
456 struct mips_asm_switch mips_nomacro = { "macro", 0 };
457 struct mips_asm_switch mips_noat = { "at", 0 };
458 
459 /* True if we're writing out a branch-likely instruction rather than a
460    normal branch.  */
461 static bool mips_branch_likely;
462 
463 /* The current instruction-set architecture.  */
464 enum processor mips_arch;
465 const struct mips_cpu_info *mips_arch_info;
466 
467 /* The processor that we should tune the code for.  */
468 enum processor mips_tune;
469 const struct mips_cpu_info *mips_tune_info;
470 
471 /* The ISA level associated with mips_arch.  */
472 int mips_isa;
473 
474 /* The ISA revision level.  This is 0 for MIPS I to V and N for
475    MIPS{32,64}rN.  */
476 int mips_isa_rev;
477 
478 /* The architecture selected by -mipsN, or null if -mipsN wasn't used.  */
479 static const struct mips_cpu_info *mips_isa_option_info;
480 
481 /* Which cost information to use.  */
482 static const struct mips_rtx_cost_data *mips_cost;
483 
484 /* The ambient target flags, excluding MASK_MIPS16.  */
485 static int mips_base_target_flags;
486 
487 /* The default compression mode.  */
488 unsigned int mips_base_compression_flags;
489 
490 /* The ambient values of other global variables.  */
491 static int mips_base_schedule_insns; /* flag_schedule_insns */
492 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
493 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
494 static int mips_base_align_loops; /* align_loops */
495 static int mips_base_align_jumps; /* align_jumps */
496 static int mips_base_align_functions; /* align_functions */
497 
498 /* Index [M][R] is true if register R is allowed to hold a value of mode M.  */
499 static bool mips_hard_regno_mode_ok_p[MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
500 
501 /* Index C is true if character C is a valid PRINT_OPERAND punctation
502    character.  */
503 static bool mips_print_operand_punct[256];
504 
505 static GTY (()) int mips_output_filename_first_time = 1;
506 
507 /* mips_split_p[X] is true if symbols of type X can be split by
508    mips_split_symbol.  */
509 bool mips_split_p[NUM_SYMBOL_TYPES];
510 
511 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
512    can be split by mips_split_symbol.  */
513 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
514 
515 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
516    forced into a PC-relative constant pool.  */
517 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
518 
519 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
520    appears in a LO_SUM.  It can be null if such LO_SUMs aren't valid or
521    if they are matched by a special .md file pattern.  */
522 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
523 
524 /* Likewise for HIGHs.  */
525 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
526 
527 /* Target state for MIPS16.  */
528 struct target_globals *mips16_globals;
529 
530 /* Target state for MICROMIPS.  */
531 struct target_globals *micromips_globals;
532 
533 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
534    and returned from mips_sched_reorder2.  */
535 static int cached_can_issue_more;
536 
537 /* The stubs for various MIPS16 support functions, if used.   */
538 static mips_one_only_stub *mips16_rdhwr_stub;
539 static mips_one_only_stub *mips16_get_fcsr_stub;
540 static mips_one_only_stub *mips16_set_fcsr_stub;
541 
542 /* Index R is the smallest register class that contains register R.  */
543 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
544   LEA_REGS,        LEA_REGS,        M16_STORE_REGS,  V1_REG,
545   M16_STORE_REGS,  M16_STORE_REGS,  M16_STORE_REGS,  M16_STORE_REGS,
546   LEA_REGS,        LEA_REGS,        LEA_REGS,        LEA_REGS,
547   LEA_REGS,        LEA_REGS,        LEA_REGS,        LEA_REGS,
548   M16_REGS,        M16_STORE_REGS,  LEA_REGS,        LEA_REGS,
549   LEA_REGS,        LEA_REGS,        LEA_REGS,        LEA_REGS,
550   T_REG,           PIC_FN_ADDR_REG, LEA_REGS,        LEA_REGS,
551   LEA_REGS,        M16_SP_REGS,     LEA_REGS,        LEA_REGS,
552 
553   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
554   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
555   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
556   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
557   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
558   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
559   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
560   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
561   MD0_REG,	MD1_REG,	NO_REGS,	ST_REGS,
562   ST_REGS,	ST_REGS,	ST_REGS,	ST_REGS,
563   ST_REGS,	ST_REGS,	ST_REGS,	NO_REGS,
564   NO_REGS,	FRAME_REGS,	FRAME_REGS,	NO_REGS,
565   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
566   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
567   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
568   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
569   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
570   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
571   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
572   COP0_REGS,	COP0_REGS,	COP0_REGS,	COP0_REGS,
573   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
574   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
575   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
576   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
577   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
578   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
579   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
580   COP2_REGS,	COP2_REGS,	COP2_REGS,	COP2_REGS,
581   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
582   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
583   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
584   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
585   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
586   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
587   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
588   COP3_REGS,	COP3_REGS,	COP3_REGS,	COP3_REGS,
589   DSP_ACC_REGS,	DSP_ACC_REGS,	DSP_ACC_REGS,	DSP_ACC_REGS,
590   DSP_ACC_REGS,	DSP_ACC_REGS,	ALL_REGS,	ALL_REGS,
591   ALL_REGS,	ALL_REGS,	ALL_REGS,	ALL_REGS
592 };
593 
594 static tree mips_handle_interrupt_attr (tree *, tree, tree, int, bool *);
595 static tree mips_handle_use_shadow_register_set_attr (tree *, tree, tree, int,
596 						      bool *);
597 
598 /* The value of TARGET_ATTRIBUTE_TABLE.  */
599 static const struct attribute_spec mips_attribute_table[] = {
600   /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
601        affects_type_identity, handler, exclude } */
602   { "long_call",   0, 0, false, true,  true,  false, NULL, NULL },
603   { "short_call",  0, 0, false, true,  true,  false, NULL, NULL },
604   { "far",     	   0, 0, false, true,  true,  false, NULL, NULL },
605   { "near",        0, 0, false, true,  true,  false, NULL, NULL },
606   /* We would really like to treat "mips16" and "nomips16" as type
607      attributes, but GCC doesn't provide the hooks we need to support
608      the right conversion rules.  As declaration attributes, they affect
609      code generation but don't carry other semantics.  */
610   { "mips16", 	   0, 0, true,  false, false, false, NULL, NULL },
611   { "nomips16",    0, 0, true,  false, false, false, NULL, NULL },
612   { "micromips",   0, 0, true,  false, false, false, NULL, NULL },
613   { "nomicromips", 0, 0, true,  false, false, false, NULL, NULL },
614   { "nocompression", 0, 0, true,  false, false, false, NULL, NULL },
615   /* Allow functions to be specified as interrupt handlers */
616   { "interrupt",   0, 1, false, true,  true, false, mips_handle_interrupt_attr,
617     NULL },
618   { "use_shadow_register_set",	0, 1, false, true,  true, false,
619     mips_handle_use_shadow_register_set_attr, NULL },
620   { "keep_interrupts_masked",	0, 0, false, true,  true, false, NULL, NULL },
621   { "use_debug_exception_return", 0, 0, false, true, true, false, NULL, NULL },
622   { NULL,	   0, 0, false, false, false, false, NULL, NULL }
623 };
624 
625 /* A table describing all the processors GCC knows about; see
626    mips-cpus.def for details.  */
627 static const struct mips_cpu_info mips_cpu_info_table[] = {
628 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
629   { NAME, CPU, ISA, FLAGS },
630 #include "mips-cpus.def"
631 #undef MIPS_CPU
632 };
633 
634 /* Default costs.  If these are used for a processor we should look
635    up the actual costs.  */
636 #define DEFAULT_COSTS COSTS_N_INSNS (6),  /* fp_add */       \
637                       COSTS_N_INSNS (7),  /* fp_mult_sf */   \
638                       COSTS_N_INSNS (8),  /* fp_mult_df */   \
639                       COSTS_N_INSNS (23), /* fp_div_sf */    \
640                       COSTS_N_INSNS (36), /* fp_div_df */    \
641                       COSTS_N_INSNS (10), /* int_mult_si */  \
642                       COSTS_N_INSNS (10), /* int_mult_di */  \
643                       COSTS_N_INSNS (69), /* int_div_si */   \
644                       COSTS_N_INSNS (69), /* int_div_di */   \
645                                        2, /* branch_cost */  \
646                                        4  /* memory_latency */
647 
648 /* Floating-point costs for processors without an FPU.  Just assume that
649    all floating-point libcalls are very expensive.  */
650 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */       \
651                       COSTS_N_INSNS (256), /* fp_mult_sf */   \
652                       COSTS_N_INSNS (256), /* fp_mult_df */   \
653                       COSTS_N_INSNS (256), /* fp_div_sf */    \
654                       COSTS_N_INSNS (256)  /* fp_div_df */
655 
656 /* Costs to use when optimizing for size.  */
657 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
658   COSTS_N_INSNS (1),            /* fp_add */
659   COSTS_N_INSNS (1),            /* fp_mult_sf */
660   COSTS_N_INSNS (1),            /* fp_mult_df */
661   COSTS_N_INSNS (1),            /* fp_div_sf */
662   COSTS_N_INSNS (1),            /* fp_div_df */
663   COSTS_N_INSNS (1),            /* int_mult_si */
664   COSTS_N_INSNS (1),            /* int_mult_di */
665   COSTS_N_INSNS (1),            /* int_div_si */
666   COSTS_N_INSNS (1),            /* int_div_di */
667 		   2,           /* branch_cost */
668 		   4            /* memory_latency */
669 };
670 
671 /* Costs to use when optimizing for speed, indexed by processor.  */
672 static const struct mips_rtx_cost_data
673   mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
674   { /* R3000 */
675     COSTS_N_INSNS (2),            /* fp_add */
676     COSTS_N_INSNS (4),            /* fp_mult_sf */
677     COSTS_N_INSNS (5),            /* fp_mult_df */
678     COSTS_N_INSNS (12),           /* fp_div_sf */
679     COSTS_N_INSNS (19),           /* fp_div_df */
680     COSTS_N_INSNS (12),           /* int_mult_si */
681     COSTS_N_INSNS (12),           /* int_mult_di */
682     COSTS_N_INSNS (35),           /* int_div_si */
683     COSTS_N_INSNS (35),           /* int_div_di */
684 		     1,           /* branch_cost */
685 		     4            /* memory_latency */
686   },
687   { /* 4KC */
688     SOFT_FP_COSTS,
689     COSTS_N_INSNS (6),            /* int_mult_si */
690     COSTS_N_INSNS (6),            /* int_mult_di */
691     COSTS_N_INSNS (36),           /* int_div_si */
692     COSTS_N_INSNS (36),           /* int_div_di */
693 		     1,           /* branch_cost */
694 		     4            /* memory_latency */
695   },
696   { /* 4KP */
697     SOFT_FP_COSTS,
698     COSTS_N_INSNS (36),           /* int_mult_si */
699     COSTS_N_INSNS (36),           /* int_mult_di */
700     COSTS_N_INSNS (37),           /* int_div_si */
701     COSTS_N_INSNS (37),           /* int_div_di */
702 		     1,           /* branch_cost */
703 		     4            /* memory_latency */
704   },
705   { /* 5KC */
706     SOFT_FP_COSTS,
707     COSTS_N_INSNS (4),            /* int_mult_si */
708     COSTS_N_INSNS (11),           /* int_mult_di */
709     COSTS_N_INSNS (36),           /* int_div_si */
710     COSTS_N_INSNS (68),           /* int_div_di */
711 		     1,           /* branch_cost */
712 		     4            /* memory_latency */
713   },
714   { /* 5KF */
715     COSTS_N_INSNS (4),            /* fp_add */
716     COSTS_N_INSNS (4),            /* fp_mult_sf */
717     COSTS_N_INSNS (5),            /* fp_mult_df */
718     COSTS_N_INSNS (17),           /* fp_div_sf */
719     COSTS_N_INSNS (32),           /* fp_div_df */
720     COSTS_N_INSNS (4),            /* int_mult_si */
721     COSTS_N_INSNS (11),           /* int_mult_di */
722     COSTS_N_INSNS (36),           /* int_div_si */
723     COSTS_N_INSNS (68),           /* int_div_di */
724 		     1,           /* branch_cost */
725 		     4            /* memory_latency */
726   },
727   { /* 20KC */
728     COSTS_N_INSNS (4),            /* fp_add */
729     COSTS_N_INSNS (4),            /* fp_mult_sf */
730     COSTS_N_INSNS (5),            /* fp_mult_df */
731     COSTS_N_INSNS (17),           /* fp_div_sf */
732     COSTS_N_INSNS (32),           /* fp_div_df */
733     COSTS_N_INSNS (4),            /* int_mult_si */
734     COSTS_N_INSNS (7),            /* int_mult_di */
735     COSTS_N_INSNS (42),           /* int_div_si */
736     COSTS_N_INSNS (72),           /* int_div_di */
737 		     1,           /* branch_cost */
738 		     4            /* memory_latency */
739   },
740   { /* 24KC */
741     SOFT_FP_COSTS,
742     COSTS_N_INSNS (5),            /* int_mult_si */
743     COSTS_N_INSNS (5),            /* int_mult_di */
744     COSTS_N_INSNS (41),           /* int_div_si */
745     COSTS_N_INSNS (41),           /* int_div_di */
746 		     1,           /* branch_cost */
747 		     4            /* memory_latency */
748   },
749   { /* 24KF2_1 */
750     COSTS_N_INSNS (8),            /* fp_add */
751     COSTS_N_INSNS (8),            /* fp_mult_sf */
752     COSTS_N_INSNS (10),           /* fp_mult_df */
753     COSTS_N_INSNS (34),           /* fp_div_sf */
754     COSTS_N_INSNS (64),           /* fp_div_df */
755     COSTS_N_INSNS (5),            /* int_mult_si */
756     COSTS_N_INSNS (5),            /* int_mult_di */
757     COSTS_N_INSNS (41),           /* int_div_si */
758     COSTS_N_INSNS (41),           /* int_div_di */
759 		     1,           /* branch_cost */
760 		     4            /* memory_latency */
761   },
762   { /* 24KF1_1 */
763     COSTS_N_INSNS (4),            /* fp_add */
764     COSTS_N_INSNS (4),            /* fp_mult_sf */
765     COSTS_N_INSNS (5),            /* fp_mult_df */
766     COSTS_N_INSNS (17),           /* fp_div_sf */
767     COSTS_N_INSNS (32),           /* fp_div_df */
768     COSTS_N_INSNS (5),            /* int_mult_si */
769     COSTS_N_INSNS (5),            /* int_mult_di */
770     COSTS_N_INSNS (41),           /* int_div_si */
771     COSTS_N_INSNS (41),           /* int_div_di */
772 		     1,           /* branch_cost */
773 		     4            /* memory_latency */
774   },
775   { /* 74KC */
776     SOFT_FP_COSTS,
777     COSTS_N_INSNS (5),            /* int_mult_si */
778     COSTS_N_INSNS (5),            /* int_mult_di */
779     COSTS_N_INSNS (41),           /* int_div_si */
780     COSTS_N_INSNS (41),           /* int_div_di */
781 		     1,           /* branch_cost */
782 		     4            /* memory_latency */
783   },
784   { /* 74KF2_1 */
785     COSTS_N_INSNS (8),            /* fp_add */
786     COSTS_N_INSNS (8),            /* fp_mult_sf */
787     COSTS_N_INSNS (10),           /* fp_mult_df */
788     COSTS_N_INSNS (34),           /* fp_div_sf */
789     COSTS_N_INSNS (64),           /* fp_div_df */
790     COSTS_N_INSNS (5),            /* int_mult_si */
791     COSTS_N_INSNS (5),            /* int_mult_di */
792     COSTS_N_INSNS (41),           /* int_div_si */
793     COSTS_N_INSNS (41),           /* int_div_di */
794 		     1,           /* branch_cost */
795 		     4            /* memory_latency */
796   },
797   { /* 74KF1_1 */
798     COSTS_N_INSNS (4),            /* fp_add */
799     COSTS_N_INSNS (4),            /* fp_mult_sf */
800     COSTS_N_INSNS (5),            /* fp_mult_df */
801     COSTS_N_INSNS (17),           /* fp_div_sf */
802     COSTS_N_INSNS (32),           /* fp_div_df */
803     COSTS_N_INSNS (5),            /* int_mult_si */
804     COSTS_N_INSNS (5),            /* int_mult_di */
805     COSTS_N_INSNS (41),           /* int_div_si */
806     COSTS_N_INSNS (41),           /* int_div_di */
807 		     1,           /* branch_cost */
808 		     4            /* memory_latency */
809   },
810   { /* 74KF3_2 */
811     COSTS_N_INSNS (6),            /* fp_add */
812     COSTS_N_INSNS (6),            /* fp_mult_sf */
813     COSTS_N_INSNS (7),            /* fp_mult_df */
814     COSTS_N_INSNS (25),           /* fp_div_sf */
815     COSTS_N_INSNS (48),           /* fp_div_df */
816     COSTS_N_INSNS (5),            /* int_mult_si */
817     COSTS_N_INSNS (5),            /* int_mult_di */
818     COSTS_N_INSNS (41),           /* int_div_si */
819     COSTS_N_INSNS (41),           /* int_div_di */
820 		     1,           /* branch_cost */
821 		     4            /* memory_latency */
822   },
823   { /* Loongson-2E */
824     DEFAULT_COSTS
825   },
826   { /* Loongson-2F */
827     DEFAULT_COSTS
828   },
829   { /* Loongson-3A */
830     DEFAULT_COSTS
831   },
832   { /* M4k */
833     DEFAULT_COSTS
834   },
835     /* Octeon */
836   {
837     SOFT_FP_COSTS,
838     COSTS_N_INSNS (5),            /* int_mult_si */
839     COSTS_N_INSNS (5),            /* int_mult_di */
840     COSTS_N_INSNS (72),           /* int_div_si */
841     COSTS_N_INSNS (72),           /* int_div_di */
842                      1,		  /* branch_cost */
843                      4		  /* memory_latency */
844   },
845     /* Octeon II */
846   {
847     SOFT_FP_COSTS,
848     COSTS_N_INSNS (6),            /* int_mult_si */
849     COSTS_N_INSNS (6),            /* int_mult_di */
850     COSTS_N_INSNS (18),           /* int_div_si */
851     COSTS_N_INSNS (35),           /* int_div_di */
852                      4,		  /* branch_cost */
853                      4		  /* memory_latency */
854   },
855     /* Octeon III */
856   {
857     COSTS_N_INSNS (6),            /* fp_add */
858     COSTS_N_INSNS (6),            /* fp_mult_sf */
859     COSTS_N_INSNS (7),            /* fp_mult_df */
860     COSTS_N_INSNS (25),           /* fp_div_sf */
861     COSTS_N_INSNS (48),           /* fp_div_df */
862     COSTS_N_INSNS (6),            /* int_mult_si */
863     COSTS_N_INSNS (6),            /* int_mult_di */
864     COSTS_N_INSNS (18),           /* int_div_si */
865     COSTS_N_INSNS (35),           /* int_div_di */
866                      4,		  /* branch_cost */
867                      4		  /* memory_latency */
868   },
869   { /* R3900 */
870     COSTS_N_INSNS (2),            /* fp_add */
871     COSTS_N_INSNS (4),            /* fp_mult_sf */
872     COSTS_N_INSNS (5),            /* fp_mult_df */
873     COSTS_N_INSNS (12),           /* fp_div_sf */
874     COSTS_N_INSNS (19),           /* fp_div_df */
875     COSTS_N_INSNS (2),            /* int_mult_si */
876     COSTS_N_INSNS (2),            /* int_mult_di */
877     COSTS_N_INSNS (35),           /* int_div_si */
878     COSTS_N_INSNS (35),           /* int_div_di */
879 		     1,           /* branch_cost */
880 		     4            /* memory_latency */
881   },
882   { /* R6000 */
883     COSTS_N_INSNS (3),            /* fp_add */
884     COSTS_N_INSNS (5),            /* fp_mult_sf */
885     COSTS_N_INSNS (6),            /* fp_mult_df */
886     COSTS_N_INSNS (15),           /* fp_div_sf */
887     COSTS_N_INSNS (16),           /* fp_div_df */
888     COSTS_N_INSNS (17),           /* int_mult_si */
889     COSTS_N_INSNS (17),           /* int_mult_di */
890     COSTS_N_INSNS (38),           /* int_div_si */
891     COSTS_N_INSNS (38),           /* int_div_di */
892 		     2,           /* branch_cost */
893 		     6            /* memory_latency */
894   },
895   { /* R4000 */
896      COSTS_N_INSNS (6),           /* fp_add */
897      COSTS_N_INSNS (7),           /* fp_mult_sf */
898      COSTS_N_INSNS (8),           /* fp_mult_df */
899      COSTS_N_INSNS (23),          /* fp_div_sf */
900      COSTS_N_INSNS (36),          /* fp_div_df */
901      COSTS_N_INSNS (10),          /* int_mult_si */
902      COSTS_N_INSNS (10),          /* int_mult_di */
903      COSTS_N_INSNS (69),          /* int_div_si */
904      COSTS_N_INSNS (69),          /* int_div_di */
905 		      2,          /* branch_cost */
906 		      6           /* memory_latency */
907   },
908   { /* R4100 */
909     DEFAULT_COSTS
910   },
911   { /* R4111 */
912     DEFAULT_COSTS
913   },
914   { /* R4120 */
915     DEFAULT_COSTS
916   },
917   { /* R4130 */
918     /* The only costs that appear to be updated here are
919        integer multiplication.  */
920     SOFT_FP_COSTS,
921     COSTS_N_INSNS (4),            /* int_mult_si */
922     COSTS_N_INSNS (6),            /* int_mult_di */
923     COSTS_N_INSNS (69),           /* int_div_si */
924     COSTS_N_INSNS (69),           /* int_div_di */
925 		     1,           /* branch_cost */
926 		     4            /* memory_latency */
927   },
928   { /* R4300 */
929     DEFAULT_COSTS
930   },
931   { /* R4600 */
932     DEFAULT_COSTS
933   },
934   { /* R4650 */
935     DEFAULT_COSTS
936   },
937   { /* R4700 */
938     DEFAULT_COSTS
939   },
940   { /* R5000 */
941     COSTS_N_INSNS (6),            /* fp_add */
942     COSTS_N_INSNS (4),            /* fp_mult_sf */
943     COSTS_N_INSNS (5),            /* fp_mult_df */
944     COSTS_N_INSNS (23),           /* fp_div_sf */
945     COSTS_N_INSNS (36),           /* fp_div_df */
946     COSTS_N_INSNS (5),            /* int_mult_si */
947     COSTS_N_INSNS (5),            /* int_mult_di */
948     COSTS_N_INSNS (36),           /* int_div_si */
949     COSTS_N_INSNS (36),           /* int_div_di */
950 		     1,           /* branch_cost */
951 		     4            /* memory_latency */
952   },
953   { /* R5400 */
954     COSTS_N_INSNS (6),            /* fp_add */
955     COSTS_N_INSNS (5),            /* fp_mult_sf */
956     COSTS_N_INSNS (6),            /* fp_mult_df */
957     COSTS_N_INSNS (30),           /* fp_div_sf */
958     COSTS_N_INSNS (59),           /* fp_div_df */
959     COSTS_N_INSNS (3),            /* int_mult_si */
960     COSTS_N_INSNS (4),            /* int_mult_di */
961     COSTS_N_INSNS (42),           /* int_div_si */
962     COSTS_N_INSNS (74),           /* int_div_di */
963 		     1,           /* branch_cost */
964 		     4            /* memory_latency */
965   },
966   { /* R5500 */
967     COSTS_N_INSNS (6),            /* fp_add */
968     COSTS_N_INSNS (5),            /* fp_mult_sf */
969     COSTS_N_INSNS (6),            /* fp_mult_df */
970     COSTS_N_INSNS (30),           /* fp_div_sf */
971     COSTS_N_INSNS (59),           /* fp_div_df */
972     COSTS_N_INSNS (5),            /* int_mult_si */
973     COSTS_N_INSNS (9),            /* int_mult_di */
974     COSTS_N_INSNS (42),           /* int_div_si */
975     COSTS_N_INSNS (74),           /* int_div_di */
976 		     1,           /* branch_cost */
977 		     4            /* memory_latency */
978   },
979   { /* R5900 */
980     COSTS_N_INSNS (4),            /* fp_add */
981     COSTS_N_INSNS (4),            /* fp_mult_sf */
982     COSTS_N_INSNS (256),          /* fp_mult_df */
983     COSTS_N_INSNS (8),            /* fp_div_sf */
984     COSTS_N_INSNS (256),          /* fp_div_df */
985     COSTS_N_INSNS (4),            /* int_mult_si */
986     COSTS_N_INSNS (256),          /* int_mult_di */
987     COSTS_N_INSNS (37),           /* int_div_si */
988     COSTS_N_INSNS (256),          /* int_div_di */
989 		     1,           /* branch_cost */
990 		     4            /* memory_latency */
991   },
992   { /* R7000 */
993     /* The only costs that are changed here are
994        integer multiplication.  */
995     COSTS_N_INSNS (6),            /* fp_add */
996     COSTS_N_INSNS (7),            /* fp_mult_sf */
997     COSTS_N_INSNS (8),            /* fp_mult_df */
998     COSTS_N_INSNS (23),           /* fp_div_sf */
999     COSTS_N_INSNS (36),           /* fp_div_df */
1000     COSTS_N_INSNS (5),            /* int_mult_si */
1001     COSTS_N_INSNS (9),            /* int_mult_di */
1002     COSTS_N_INSNS (69),           /* int_div_si */
1003     COSTS_N_INSNS (69),           /* int_div_di */
1004 		     1,           /* branch_cost */
1005 		     4            /* memory_latency */
1006   },
1007   { /* R8000 */
1008     DEFAULT_COSTS
1009   },
1010   { /* R9000 */
1011     /* The only costs that are changed here are
1012        integer multiplication.  */
1013     COSTS_N_INSNS (6),            /* fp_add */
1014     COSTS_N_INSNS (7),            /* fp_mult_sf */
1015     COSTS_N_INSNS (8),            /* fp_mult_df */
1016     COSTS_N_INSNS (23),           /* fp_div_sf */
1017     COSTS_N_INSNS (36),           /* fp_div_df */
1018     COSTS_N_INSNS (3),            /* int_mult_si */
1019     COSTS_N_INSNS (8),            /* int_mult_di */
1020     COSTS_N_INSNS (69),           /* int_div_si */
1021     COSTS_N_INSNS (69),           /* int_div_di */
1022 		     1,           /* branch_cost */
1023 		     4            /* memory_latency */
1024   },
1025   { /* R1x000 */
1026     COSTS_N_INSNS (2),            /* fp_add */
1027     COSTS_N_INSNS (2),            /* fp_mult_sf */
1028     COSTS_N_INSNS (2),            /* fp_mult_df */
1029     COSTS_N_INSNS (12),           /* fp_div_sf */
1030     COSTS_N_INSNS (19),           /* fp_div_df */
1031     COSTS_N_INSNS (5),            /* int_mult_si */
1032     COSTS_N_INSNS (9),            /* int_mult_di */
1033     COSTS_N_INSNS (34),           /* int_div_si */
1034     COSTS_N_INSNS (66),           /* int_div_di */
1035 		     1,           /* branch_cost */
1036 		     4            /* memory_latency */
1037   },
1038   { /* SB1 */
1039     /* These costs are the same as the SB-1A below.  */
1040     COSTS_N_INSNS (4),            /* fp_add */
1041     COSTS_N_INSNS (4),            /* fp_mult_sf */
1042     COSTS_N_INSNS (4),            /* fp_mult_df */
1043     COSTS_N_INSNS (24),           /* fp_div_sf */
1044     COSTS_N_INSNS (32),           /* fp_div_df */
1045     COSTS_N_INSNS (3),            /* int_mult_si */
1046     COSTS_N_INSNS (4),            /* int_mult_di */
1047     COSTS_N_INSNS (36),           /* int_div_si */
1048     COSTS_N_INSNS (68),           /* int_div_di */
1049 		     1,           /* branch_cost */
1050 		     4            /* memory_latency */
1051   },
1052   { /* SB1-A */
1053     /* These costs are the same as the SB-1 above.  */
1054     COSTS_N_INSNS (4),            /* fp_add */
1055     COSTS_N_INSNS (4),            /* fp_mult_sf */
1056     COSTS_N_INSNS (4),            /* fp_mult_df */
1057     COSTS_N_INSNS (24),           /* fp_div_sf */
1058     COSTS_N_INSNS (32),           /* fp_div_df */
1059     COSTS_N_INSNS (3),            /* int_mult_si */
1060     COSTS_N_INSNS (4),            /* int_mult_di */
1061     COSTS_N_INSNS (36),           /* int_div_si */
1062     COSTS_N_INSNS (68),           /* int_div_di */
1063 		     1,           /* branch_cost */
1064 		     4            /* memory_latency */
1065   },
1066   { /* SR71000 */
1067     DEFAULT_COSTS
1068   },
1069   { /* XLR */
1070     SOFT_FP_COSTS,
1071     COSTS_N_INSNS (8),            /* int_mult_si */
1072     COSTS_N_INSNS (8),            /* int_mult_di */
1073     COSTS_N_INSNS (72),           /* int_div_si */
1074     COSTS_N_INSNS (72),           /* int_div_di */
1075 		     1,           /* branch_cost */
1076 		     4            /* memory_latency */
1077   },
1078   { /* XLP */
1079     /* These costs are the same as 5KF above.  */
1080     COSTS_N_INSNS (4),            /* fp_add */
1081     COSTS_N_INSNS (4),            /* fp_mult_sf */
1082     COSTS_N_INSNS (5),            /* fp_mult_df */
1083     COSTS_N_INSNS (17),           /* fp_div_sf */
1084     COSTS_N_INSNS (32),           /* fp_div_df */
1085     COSTS_N_INSNS (4),            /* int_mult_si */
1086     COSTS_N_INSNS (11),           /* int_mult_di */
1087     COSTS_N_INSNS (36),           /* int_div_si */
1088     COSTS_N_INSNS (68),           /* int_div_di */
1089 		     1,           /* branch_cost */
1090 		     4            /* memory_latency */
1091   },
1092   { /* P5600 */
1093     COSTS_N_INSNS (4),            /* fp_add */
1094     COSTS_N_INSNS (5),            /* fp_mult_sf */
1095     COSTS_N_INSNS (5),            /* fp_mult_df */
1096     COSTS_N_INSNS (17),           /* fp_div_sf */
1097     COSTS_N_INSNS (17),           /* fp_div_df */
1098     COSTS_N_INSNS (5),            /* int_mult_si */
1099     COSTS_N_INSNS (5),            /* int_mult_di */
1100     COSTS_N_INSNS (8),            /* int_div_si */
1101     COSTS_N_INSNS (8),            /* int_div_di */
1102 		    2,            /* branch_cost */
1103 		    4             /* memory_latency */
1104   },
1105   { /* M5100 */
1106     COSTS_N_INSNS (4),            /* fp_add */
1107     COSTS_N_INSNS (4),            /* fp_mult_sf */
1108     COSTS_N_INSNS (5),            /* fp_mult_df */
1109     COSTS_N_INSNS (17),           /* fp_div_sf */
1110     COSTS_N_INSNS (32),           /* fp_div_df */
1111     COSTS_N_INSNS (5),            /* int_mult_si */
1112     COSTS_N_INSNS (5),            /* int_mult_di */
1113     COSTS_N_INSNS (34),           /* int_div_si */
1114     COSTS_N_INSNS (68),           /* int_div_di */
1115 		     1,           /* branch_cost */
1116 		     4            /* memory_latency */
1117   },
1118   { /* I6400 */
1119     COSTS_N_INSNS (4),            /* fp_add */
1120     COSTS_N_INSNS (5),            /* fp_mult_sf */
1121     COSTS_N_INSNS (5),            /* fp_mult_df */
1122     COSTS_N_INSNS (32),           /* fp_div_sf */
1123     COSTS_N_INSNS (32),           /* fp_div_df */
1124     COSTS_N_INSNS (5),            /* int_mult_si */
1125     COSTS_N_INSNS (5),            /* int_mult_di */
1126     COSTS_N_INSNS (36),           /* int_div_si */
1127     COSTS_N_INSNS (36),           /* int_div_di */
1128 		    2,            /* branch_cost */
1129 		    4             /* memory_latency */
1130   }
1131 };
1132 
1133 static rtx mips_find_pic_call_symbol (rtx_insn *, rtx, bool);
1134 static int mips_register_move_cost (machine_mode, reg_class_t,
1135 				    reg_class_t);
1136 static unsigned int mips_function_arg_boundary (machine_mode, const_tree);
1137 static rtx mips_gen_const_int_vector_shuffle (machine_mode, int);
1138 
1139 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1140    for -mflip_mips16.  It maps decl names onto a boolean mode setting.  */
1141 static GTY (()) hash_map<nofree_string_hash, bool> *mflip_mips16_htab;
1142 
1143 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1144    mode, false if it should next add an attribute for the opposite mode.  */
1145 static GTY(()) bool mips16_flipper;
1146 
1147 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1148    for -mflip-mips16.  Return true if it should use "mips16" and false if
1149    it should use "nomips16".  */
1150 
1151 static bool
mflip_mips16_use_mips16_p(tree decl)1152 mflip_mips16_use_mips16_p (tree decl)
1153 {
1154   const char *name;
1155   bool base_is_mips16 = (mips_base_compression_flags & MASK_MIPS16) != 0;
1156 
1157   /* Use the opposite of the command-line setting for anonymous decls.  */
1158   if (!DECL_NAME (decl))
1159     return !base_is_mips16;
1160 
1161   if (!mflip_mips16_htab)
1162     mflip_mips16_htab = hash_map<nofree_string_hash, bool>::create_ggc (37);
1163 
1164   name = IDENTIFIER_POINTER (DECL_NAME (decl));
1165 
1166   bool existed;
1167   bool *slot = &mflip_mips16_htab->get_or_insert (name, &existed);
1168   if (!existed)
1169     {
1170       mips16_flipper = !mips16_flipper;
1171       *slot = mips16_flipper ? !base_is_mips16 : base_is_mips16;
1172     }
1173   return *slot;
1174 }
1175 
1176 /* Predicates to test for presence of "near"/"short_call" and "far"/"long_call"
1177    attributes on the given TYPE.  */
1178 
1179 static bool
mips_near_type_p(const_tree type)1180 mips_near_type_p (const_tree type)
1181 {
1182   return (lookup_attribute ("short_call", TYPE_ATTRIBUTES (type)) != NULL
1183 	  || lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL);
1184 }
1185 
1186 static bool
mips_far_type_p(const_tree type)1187 mips_far_type_p (const_tree type)
1188 {
1189   return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1190 	  || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1191 }
1192 
1193 
1194 /* Check if the interrupt attribute is set for a function.  */
1195 
1196 static bool
mips_interrupt_type_p(tree type)1197 mips_interrupt_type_p (tree type)
1198 {
1199   return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1200 }
1201 
1202 /* Return the mask for the "interrupt" attribute.  */
1203 
1204 static enum mips_int_mask
mips_interrupt_mask(tree type)1205 mips_interrupt_mask (tree type)
1206 {
1207   tree attr = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type));
1208   tree args, cst;
1209   const char *str;
1210 
1211   /* For missing attributes or no arguments then return 'eic' as a safe
1212      fallback.  */
1213   if (attr == NULL)
1214     return INT_MASK_EIC;
1215 
1216   args = TREE_VALUE (attr);
1217 
1218   if (args == NULL)
1219     return INT_MASK_EIC;
1220 
1221   cst = TREE_VALUE (args);
1222 
1223   if (strcmp (TREE_STRING_POINTER (cst), "eic") == 0)
1224     return INT_MASK_EIC;
1225 
1226   /* The validation code in mips_handle_interrupt_attr guarantees that the
1227      argument is now in the form:
1228      vector=(sw0|sw1|hw0|hw1|hw2|hw3|hw4|hw5).  */
1229   str = TREE_STRING_POINTER (cst);
1230 
1231   gcc_assert (strlen (str) == strlen ("vector=sw0"));
1232 
1233   if (str[7] == 's')
1234     return (enum mips_int_mask) (INT_MASK_SW0 + (str[9] - '0'));
1235 
1236   return (enum mips_int_mask) (INT_MASK_HW0 + (str[9] - '0'));
1237 }
1238 
1239 /* Return the mips_shadow_set if the "use_shadow_register_set" attribute is
1240    set for a function.  */
1241 
1242 static enum mips_shadow_set
mips_use_shadow_register_set(tree type)1243 mips_use_shadow_register_set (tree type)
1244 {
1245   tree attr = lookup_attribute ("use_shadow_register_set",
1246 				TYPE_ATTRIBUTES (type));
1247   tree args;
1248 
1249   /* The validation code in mips_handle_use_shadow_register_set_attr guarantees
1250      that if an argument is present then it means: Assume the shadow register
1251      set has a valid stack pointer in it.  */
1252   if (attr == NULL)
1253     return SHADOW_SET_NO;
1254 
1255   args = TREE_VALUE (attr);
1256 
1257   if (args == NULL)
1258     return SHADOW_SET_YES;
1259 
1260   return SHADOW_SET_INTSTACK;
1261 }
1262 
1263 /* Check if the attribute to keep interrupts masked is set for a function.  */
1264 
1265 static bool
mips_keep_interrupts_masked_p(tree type)1266 mips_keep_interrupts_masked_p (tree type)
1267 {
1268   return lookup_attribute ("keep_interrupts_masked",
1269 			   TYPE_ATTRIBUTES (type)) != NULL;
1270 }
1271 
1272 /* Check if the attribute to use debug exception return is set for
1273    a function.  */
1274 
1275 static bool
mips_use_debug_exception_return_p(tree type)1276 mips_use_debug_exception_return_p (tree type)
1277 {
1278   return lookup_attribute ("use_debug_exception_return",
1279 			   TYPE_ATTRIBUTES (type)) != NULL;
1280 }
1281 
1282 /* Return the set of compression modes that are explicitly required
1283    by the attributes in ATTRIBUTES.  */
1284 
1285 static unsigned int
mips_get_compress_on_flags(tree attributes)1286 mips_get_compress_on_flags (tree attributes)
1287 {
1288   unsigned int flags = 0;
1289 
1290   if (lookup_attribute ("mips16", attributes) != NULL)
1291     flags |= MASK_MIPS16;
1292 
1293   if (lookup_attribute ("micromips", attributes) != NULL)
1294     flags |= MASK_MICROMIPS;
1295 
1296   return flags;
1297 }
1298 
1299 /* Return the set of compression modes that are explicitly forbidden
1300    by the attributes in ATTRIBUTES.  */
1301 
1302 static unsigned int
mips_get_compress_off_flags(tree attributes)1303 mips_get_compress_off_flags (tree attributes)
1304 {
1305   unsigned int flags = 0;
1306 
1307   if (lookup_attribute ("nocompression", attributes) != NULL)
1308     flags |= MASK_MIPS16 | MASK_MICROMIPS;
1309 
1310   if (lookup_attribute ("nomips16", attributes) != NULL)
1311     flags |= MASK_MIPS16;
1312 
1313   if (lookup_attribute ("nomicromips", attributes) != NULL)
1314     flags |= MASK_MICROMIPS;
1315 
1316   return flags;
1317 }
1318 
1319 /* Return the compression mode that should be used for function DECL.
1320    Return the ambient setting if DECL is null.  */
1321 
1322 static unsigned int
mips_get_compress_mode(tree decl)1323 mips_get_compress_mode (tree decl)
1324 {
1325   unsigned int flags, force_on;
1326 
1327   flags = mips_base_compression_flags;
1328   if (decl)
1329     {
1330       /* Nested functions must use the same frame pointer as their
1331 	 parent and must therefore use the same ISA mode.  */
1332       tree parent = decl_function_context (decl);
1333       if (parent)
1334 	decl = parent;
1335       force_on = mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1336       if (force_on)
1337 	return force_on;
1338       flags &= ~mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1339     }
1340   return flags;
1341 }
1342 
1343 /* Return the attribute name associated with MASK_MIPS16 and MASK_MICROMIPS
1344    flags FLAGS.  */
1345 
1346 static const char *
mips_get_compress_on_name(unsigned int flags)1347 mips_get_compress_on_name (unsigned int flags)
1348 {
1349   if (flags == MASK_MIPS16)
1350     return "mips16";
1351   return "micromips";
1352 }
1353 
1354 /* Return the attribute name that forbids MASK_MIPS16 and MASK_MICROMIPS
1355    flags FLAGS.  */
1356 
1357 static const char *
mips_get_compress_off_name(unsigned int flags)1358 mips_get_compress_off_name (unsigned int flags)
1359 {
1360   if (flags == MASK_MIPS16)
1361     return "nomips16";
1362   if (flags == MASK_MICROMIPS)
1363     return "nomicromips";
1364   return "nocompression";
1365 }
1366 
1367 /* Implement TARGET_COMP_TYPE_ATTRIBUTES.  */
1368 
1369 static int
mips_comp_type_attributes(const_tree type1,const_tree type2)1370 mips_comp_type_attributes (const_tree type1, const_tree type2)
1371 {
1372   /* Disallow mixed near/far attributes.  */
1373   if (mips_far_type_p (type1) && mips_near_type_p (type2))
1374     return 0;
1375   if (mips_near_type_p (type1) && mips_far_type_p (type2))
1376     return 0;
1377   return 1;
1378 }
1379 
1380 /* Implement TARGET_INSERT_ATTRIBUTES.  */
1381 
1382 static void
mips_insert_attributes(tree decl,tree * attributes)1383 mips_insert_attributes (tree decl, tree *attributes)
1384 {
1385   const char *name;
1386   unsigned int compression_flags, nocompression_flags;
1387 
1388   /* Check for "mips16" and "nomips16" attributes.  */
1389   compression_flags = mips_get_compress_on_flags (*attributes);
1390   nocompression_flags = mips_get_compress_off_flags (*attributes);
1391 
1392   if (TREE_CODE (decl) != FUNCTION_DECL)
1393     {
1394       if (nocompression_flags)
1395 	error ("%qs attribute only applies to functions",
1396 	       mips_get_compress_off_name (nocompression_flags));
1397 
1398       if (compression_flags)
1399 	error ("%qs attribute only applies to functions",
1400 	       mips_get_compress_on_name (nocompression_flags));
1401     }
1402   else
1403     {
1404       compression_flags |= mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1405       nocompression_flags |=
1406 	mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1407 
1408       if (compression_flags && nocompression_flags)
1409 	error ("%qE cannot have both %qs and %qs attributes",
1410 	       DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
1411 	       mips_get_compress_off_name (nocompression_flags));
1412 
1413       if (compression_flags & MASK_MIPS16
1414           && compression_flags & MASK_MICROMIPS)
1415 	error ("%qE cannot have both %qs and %qs attributes",
1416 	       DECL_NAME (decl), "mips16", "micromips");
1417 
1418       if (TARGET_FLIP_MIPS16
1419 	  && !DECL_ARTIFICIAL (decl)
1420 	  && compression_flags == 0
1421 	  && nocompression_flags == 0)
1422 	{
1423 	  /* Implement -mflip-mips16.  If DECL has neither a "nomips16" nor a
1424 	     "mips16" attribute, arbitrarily pick one.  We must pick the same
1425 	     setting for duplicate declarations of a function.  */
1426 	  name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1427 	  *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1428 	  name = "nomicromips";
1429 	  *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1430 	}
1431     }
1432 }
1433 
1434 /* Implement TARGET_MERGE_DECL_ATTRIBUTES.  */
1435 
1436 static tree
mips_merge_decl_attributes(tree olddecl,tree newdecl)1437 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1438 {
1439   unsigned int diff;
1440 
1441   diff = (mips_get_compress_on_flags (DECL_ATTRIBUTES (olddecl))
1442 	  ^ mips_get_compress_on_flags (DECL_ATTRIBUTES (newdecl)));
1443   if (diff)
1444     error ("%qE redeclared with conflicting %qs attributes",
1445 	   DECL_NAME (newdecl), mips_get_compress_on_name (diff));
1446 
1447   diff = (mips_get_compress_off_flags (DECL_ATTRIBUTES (olddecl))
1448 	  ^ mips_get_compress_off_flags (DECL_ATTRIBUTES (newdecl)));
1449   if (diff)
1450     error ("%qE redeclared with conflicting %qs attributes",
1451 	   DECL_NAME (newdecl), mips_get_compress_off_name (diff));
1452 
1453   return merge_attributes (DECL_ATTRIBUTES (olddecl),
1454 			   DECL_ATTRIBUTES (newdecl));
1455 }
1456 
1457 /* Implement TARGET_CAN_INLINE_P.  */
1458 
1459 static bool
mips_can_inline_p(tree caller,tree callee)1460 mips_can_inline_p (tree caller, tree callee)
1461 {
1462   if (mips_get_compress_mode (callee) != mips_get_compress_mode (caller))
1463     return false;
1464   return default_target_can_inline_p (caller, callee);
1465 }
1466 
1467 /* Handle an "interrupt" attribute with an optional argument.  */
1468 
1469 static tree
mips_handle_interrupt_attr(tree * node ATTRIBUTE_UNUSED,tree name,tree args,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs)1470 mips_handle_interrupt_attr (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
1471 			    int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1472 {
1473   /* Check for an argument.  */
1474   if (is_attribute_p ("interrupt", name) && args != NULL)
1475     {
1476       tree cst;
1477 
1478       cst = TREE_VALUE (args);
1479       if (TREE_CODE (cst) != STRING_CST)
1480 	{
1481 	  warning (OPT_Wattributes,
1482 		   "%qE attribute requires a string argument",
1483 		   name);
1484 	  *no_add_attrs = true;
1485 	}
1486       else if (strcmp (TREE_STRING_POINTER (cst), "eic") != 0
1487 	       && strncmp (TREE_STRING_POINTER (cst), "vector=", 7) != 0)
1488 	{
1489 	  warning (OPT_Wattributes,
1490 		   "argument to %qE attribute is neither eic, nor "
1491 		   "vector=<line>", name);
1492 	  *no_add_attrs = true;
1493 	}
1494       else if (strncmp (TREE_STRING_POINTER (cst), "vector=", 7) == 0)
1495 	{
1496 	  const char *arg = TREE_STRING_POINTER (cst) + 7;
1497 
1498 	  /* Acceptable names are: sw0,sw1,hw0,hw1,hw2,hw3,hw4,hw5.  */
1499 	  if (strlen (arg) != 3
1500 	      || (arg[0] != 's' && arg[0] != 'h')
1501 	      || arg[1] != 'w'
1502 	      || (arg[0] == 's' && arg[2] != '0' && arg[2] != '1')
1503 	      || (arg[0] == 'h' && (arg[2] < '0' || arg[2] > '5')))
1504 	    {
1505 	      warning (OPT_Wattributes,
1506 		       "interrupt vector to %qE attribute is not "
1507 		       "vector=(sw0|sw1|hw0|hw1|hw2|hw3|hw4|hw5)",
1508 		       name);
1509 	      *no_add_attrs = true;
1510 	    }
1511 	}
1512 
1513       return NULL_TREE;
1514     }
1515 
1516   return NULL_TREE;
1517 }
1518 
1519 /* Handle a "use_shadow_register_set" attribute with an optional argument.  */
1520 
1521 static tree
mips_handle_use_shadow_register_set_attr(tree * node ATTRIBUTE_UNUSED,tree name,tree args,int flags ATTRIBUTE_UNUSED,bool * no_add_attrs)1522 mips_handle_use_shadow_register_set_attr (tree *node ATTRIBUTE_UNUSED,
1523 					  tree name, tree args,
1524 					  int flags ATTRIBUTE_UNUSED,
1525 					  bool *no_add_attrs)
1526 {
1527   /* Check for an argument.  */
1528   if (is_attribute_p ("use_shadow_register_set", name) && args != NULL)
1529     {
1530       tree cst;
1531 
1532       cst = TREE_VALUE (args);
1533       if (TREE_CODE (cst) != STRING_CST)
1534 	{
1535 	  warning (OPT_Wattributes,
1536 		   "%qE attribute requires a string argument",
1537 		   name);
1538 	  *no_add_attrs = true;
1539 	}
1540       else if (strcmp (TREE_STRING_POINTER (cst), "intstack") != 0)
1541 	{
1542 	  warning (OPT_Wattributes,
1543 		   "argument to %qE attribute is not intstack", name);
1544 	  *no_add_attrs = true;
1545 	}
1546 
1547       return NULL_TREE;
1548     }
1549 
1550   return NULL_TREE;
1551 }
1552 
1553 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1554    and *OFFSET_PTR.  Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise.  */
1555 
1556 static void
mips_split_plus(rtx x,rtx * base_ptr,HOST_WIDE_INT * offset_ptr)1557 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1558 {
1559   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1560     {
1561       *base_ptr = XEXP (x, 0);
1562       *offset_ptr = INTVAL (XEXP (x, 1));
1563     }
1564   else
1565     {
1566       *base_ptr = x;
1567       *offset_ptr = 0;
1568     }
1569 }
1570 
1571 static unsigned int mips_build_integer (struct mips_integer_op *,
1572 					unsigned HOST_WIDE_INT);
1573 
1574 /* A subroutine of mips_build_integer, with the same interface.
1575    Assume that the final action in the sequence should be a left shift.  */
1576 
1577 static unsigned int
mips_build_shift(struct mips_integer_op * codes,HOST_WIDE_INT value)1578 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1579 {
1580   unsigned int i, shift;
1581 
1582   /* Shift VALUE right until its lowest bit is set.  Shift arithmetically
1583      since signed numbers are easier to load than unsigned ones.  */
1584   shift = 0;
1585   while ((value & 1) == 0)
1586     value /= 2, shift++;
1587 
1588   i = mips_build_integer (codes, value);
1589   codes[i].code = ASHIFT;
1590   codes[i].value = shift;
1591   return i + 1;
1592 }
1593 
1594 /* As for mips_build_shift, but assume that the final action will be
1595    an IOR or PLUS operation.  */
1596 
1597 static unsigned int
mips_build_lower(struct mips_integer_op * codes,unsigned HOST_WIDE_INT value)1598 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1599 {
1600   unsigned HOST_WIDE_INT high;
1601   unsigned int i;
1602 
1603   high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1604   if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1605     {
1606       /* The constant is too complex to load with a simple LUI/ORI pair,
1607 	 so we want to give the recursive call as many trailing zeros as
1608 	 possible.  In this case, we know bit 16 is set and that the
1609 	 low 16 bits form a negative number.  If we subtract that number
1610 	 from VALUE, we will clear at least the lowest 17 bits, maybe more.  */
1611       i = mips_build_integer (codes, CONST_HIGH_PART (value));
1612       codes[i].code = PLUS;
1613       codes[i].value = CONST_LOW_PART (value);
1614     }
1615   else
1616     {
1617       /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1618 	 bits gives a value with at least 17 trailing zeros.  */
1619       i = mips_build_integer (codes, high);
1620       codes[i].code = IOR;
1621       codes[i].value = value & 0xffff;
1622     }
1623   return i + 1;
1624 }
1625 
1626 /* Fill CODES with a sequence of rtl operations to load VALUE.
1627    Return the number of operations needed.  */
1628 
1629 static unsigned int
mips_build_integer(struct mips_integer_op * codes,unsigned HOST_WIDE_INT value)1630 mips_build_integer (struct mips_integer_op *codes,
1631 		    unsigned HOST_WIDE_INT value)
1632 {
1633   if (SMALL_OPERAND (value)
1634       || SMALL_OPERAND_UNSIGNED (value)
1635       || LUI_OPERAND (value))
1636     {
1637       /* The value can be loaded with a single instruction.  */
1638       codes[0].code = UNKNOWN;
1639       codes[0].value = value;
1640       return 1;
1641     }
1642   else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1643     {
1644       /* Either the constant is a simple LUI/ORI combination or its
1645 	 lowest bit is set.  We don't want to shift in this case.  */
1646       return mips_build_lower (codes, value);
1647     }
1648   else if ((value & 0xffff) == 0)
1649     {
1650       /* The constant will need at least three actions.  The lowest
1651 	 16 bits are clear, so the final action will be a shift.  */
1652       return mips_build_shift (codes, value);
1653     }
1654   else
1655     {
1656       /* The final action could be a shift, add or inclusive OR.
1657 	 Rather than use a complex condition to select the best
1658 	 approach, try both mips_build_shift and mips_build_lower
1659 	 and pick the one that gives the shortest sequence.
1660 	 Note that this case is only used once per constant.  */
1661       struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1662       unsigned int cost, alt_cost;
1663 
1664       cost = mips_build_shift (codes, value);
1665       alt_cost = mips_build_lower (alt_codes, value);
1666       if (alt_cost < cost)
1667 	{
1668 	  memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1669 	  cost = alt_cost;
1670 	}
1671       return cost;
1672     }
1673 }
1674 
1675 /* Implement TARGET_LEGITIMATE_CONSTANT_P.  */
1676 
1677 static bool
mips_legitimate_constant_p(machine_mode mode ATTRIBUTE_UNUSED,rtx x)1678 mips_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1679 {
1680   return mips_const_insns (x) > 0;
1681 }
1682 
1683 /* Return a SYMBOL_REF for a MIPS16 function called NAME.  */
1684 
1685 static rtx
mips16_stub_function(const char * name)1686 mips16_stub_function (const char *name)
1687 {
1688   rtx x;
1689 
1690   x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1691   SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1692   return x;
1693 }
1694 
1695 /* Return a legitimate call address for STUB, given that STUB is a MIPS16
1696    support function.  */
1697 
1698 static rtx
mips16_stub_call_address(mips_one_only_stub * stub)1699 mips16_stub_call_address (mips_one_only_stub *stub)
1700 {
1701   rtx fn = mips16_stub_function (stub->get_name ());
1702   SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
1703   if (!call_insn_operand (fn, VOIDmode))
1704     fn = force_reg (Pmode, fn);
1705   return fn;
1706 }
1707 
1708 /* A stub for moving the thread pointer into TLS_GET_TP_REGNUM.  */
1709 
1710 class mips16_rdhwr_one_only_stub : public mips_one_only_stub
1711 {
1712   virtual const char *get_name ();
1713   virtual void output_body ();
1714 };
1715 
1716 const char *
get_name()1717 mips16_rdhwr_one_only_stub::get_name ()
1718 {
1719   return "__mips16_rdhwr";
1720 }
1721 
1722 void
output_body()1723 mips16_rdhwr_one_only_stub::output_body ()
1724 {
1725   fprintf (asm_out_file,
1726 	   "\t.set\tpush\n"
1727 	   "\t.set\tmips32r2\n"
1728 	   "\t.set\tnoreorder\n"
1729 	   "\trdhwr\t$3,$29\n"
1730 	   "\t.set\tpop\n"
1731 	   "\tj\t$31\n");
1732 }
1733 
1734 /* A stub for moving the FCSR into GET_FCSR_REGNUM.  */
1735 class mips16_get_fcsr_one_only_stub : public mips_one_only_stub
1736 {
1737   virtual const char *get_name ();
1738   virtual void output_body ();
1739 };
1740 
1741 const char *
get_name()1742 mips16_get_fcsr_one_only_stub::get_name ()
1743 {
1744   return "__mips16_get_fcsr";
1745 }
1746 
1747 void
output_body()1748 mips16_get_fcsr_one_only_stub::output_body ()
1749 {
1750   fprintf (asm_out_file,
1751 	   "\tcfc1\t%s,$31\n"
1752 	   "\tj\t$31\n", reg_names[GET_FCSR_REGNUM]);
1753 }
1754 
1755 /* A stub for moving SET_FCSR_REGNUM into the FCSR.  */
1756 class mips16_set_fcsr_one_only_stub : public mips_one_only_stub
1757 {
1758   virtual const char *get_name ();
1759   virtual void output_body ();
1760 };
1761 
1762 const char *
get_name()1763 mips16_set_fcsr_one_only_stub::get_name ()
1764 {
1765   return "__mips16_set_fcsr";
1766 }
1767 
1768 void
output_body()1769 mips16_set_fcsr_one_only_stub::output_body ()
1770 {
1771   fprintf (asm_out_file,
1772 	   "\tctc1\t%s,$31\n"
1773 	   "\tj\t$31\n", reg_names[SET_FCSR_REGNUM]);
1774 }
1775 
1776 /* Return true if symbols of type TYPE require a GOT access.  */
1777 
1778 static bool
mips_got_symbol_type_p(enum mips_symbol_type type)1779 mips_got_symbol_type_p (enum mips_symbol_type type)
1780 {
1781   switch (type)
1782     {
1783     case SYMBOL_GOT_PAGE_OFST:
1784     case SYMBOL_GOT_DISP:
1785       return true;
1786 
1787     default:
1788       return false;
1789     }
1790 }
1791 
1792 /* Return true if X is a thread-local symbol.  */
1793 
1794 static bool
mips_tls_symbol_p(rtx x)1795 mips_tls_symbol_p (rtx x)
1796 {
1797   return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1798 }
1799 
1800 /* Return true if SYMBOL_REF X is associated with a global symbol
1801    (in the STB_GLOBAL sense).  */
1802 
1803 static bool
mips_global_symbol_p(const_rtx x)1804 mips_global_symbol_p (const_rtx x)
1805 {
1806   const_tree decl = SYMBOL_REF_DECL (x);
1807 
1808   if (!decl)
1809     return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1810 
1811   /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1812      or weak symbols.  Relocations in the object file will be against
1813      the target symbol, so it's that symbol's binding that matters here.  */
1814   return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1815 }
1816 
1817 /* Return true if function X is a libgcc MIPS16 stub function.  */
1818 
1819 static bool
mips16_stub_function_p(const_rtx x)1820 mips16_stub_function_p (const_rtx x)
1821 {
1822   return (GET_CODE (x) == SYMBOL_REF
1823 	  && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1824 }
1825 
1826 /* Return true if function X is a locally-defined and locally-binding
1827    MIPS16 function.  */
1828 
1829 static bool
mips16_local_function_p(const_rtx x)1830 mips16_local_function_p (const_rtx x)
1831 {
1832   return (GET_CODE (x) == SYMBOL_REF
1833 	  && SYMBOL_REF_LOCAL_P (x)
1834 	  && !SYMBOL_REF_EXTERNAL_P (x)
1835 	  && (mips_get_compress_mode (SYMBOL_REF_DECL (x)) & MASK_MIPS16));
1836 }
1837 
1838 /* Return true if SYMBOL_REF X binds locally.  */
1839 
1840 static bool
mips_symbol_binds_local_p(const_rtx x)1841 mips_symbol_binds_local_p (const_rtx x)
1842 {
1843   return (SYMBOL_REF_DECL (x)
1844 	  ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1845 	  : SYMBOL_REF_LOCAL_P (x));
1846 }
1847 
1848 /* Return true if OP is a constant vector with the number of units in MODE,
1849    and each unit has the same bit set.  */
1850 
1851 bool
mips_const_vector_bitimm_set_p(rtx op,machine_mode mode)1852 mips_const_vector_bitimm_set_p (rtx op, machine_mode mode)
1853 {
1854   if (GET_CODE (op) == CONST_VECTOR && op != CONST0_RTX (mode))
1855     {
1856       unsigned HOST_WIDE_INT val = UINTVAL (CONST_VECTOR_ELT (op, 0));
1857       int vlog2 = exact_log2 (val & GET_MODE_MASK (GET_MODE_INNER (mode)));
1858 
1859       if (vlog2 != -1)
1860 	{
1861 	  gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
1862 	  gcc_assert (vlog2 >= 0 && vlog2 <= GET_MODE_UNIT_BITSIZE (mode) - 1);
1863 	  return mips_const_vector_same_val_p (op, mode);
1864 	}
1865     }
1866 
1867   return false;
1868 }
1869 
1870 /* Return true if OP is a constant vector with the number of units in MODE,
1871    and each unit has the same bit clear.  */
1872 
1873 bool
mips_const_vector_bitimm_clr_p(rtx op,machine_mode mode)1874 mips_const_vector_bitimm_clr_p (rtx op, machine_mode mode)
1875 {
1876   if (GET_CODE (op) == CONST_VECTOR && op != CONSTM1_RTX (mode))
1877     {
1878       unsigned HOST_WIDE_INT val = ~UINTVAL (CONST_VECTOR_ELT (op, 0));
1879       int vlog2 = exact_log2 (val & GET_MODE_MASK (GET_MODE_INNER (mode)));
1880 
1881       if (vlog2 != -1)
1882 	{
1883 	  gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
1884 	  gcc_assert (vlog2 >= 0 && vlog2 <= GET_MODE_UNIT_BITSIZE (mode) - 1);
1885 	  return mips_const_vector_same_val_p (op, mode);
1886 	}
1887     }
1888 
1889   return false;
1890 }
1891 
1892 /* Return true if OP is a constant vector with the number of units in MODE,
1893    and each unit has the same value.  */
1894 
1895 bool
mips_const_vector_same_val_p(rtx op,machine_mode mode)1896 mips_const_vector_same_val_p (rtx op, machine_mode mode)
1897 {
1898   int i, nunits = GET_MODE_NUNITS (mode);
1899   rtx first;
1900 
1901   if (GET_CODE (op) != CONST_VECTOR || GET_MODE (op) != mode)
1902     return false;
1903 
1904   first = CONST_VECTOR_ELT (op, 0);
1905   for (i = 1; i < nunits; i++)
1906     if (!rtx_equal_p (first, CONST_VECTOR_ELT (op, i)))
1907       return false;
1908 
1909   return true;
1910 }
1911 
1912 /* Return true if OP is a constant vector with the number of units in MODE,
1913    and each unit has the same value as well as replicated bytes in the value.
1914 */
1915 
1916 bool
mips_const_vector_same_bytes_p(rtx op,machine_mode mode)1917 mips_const_vector_same_bytes_p (rtx op, machine_mode mode)
1918 {
1919   int i, bytes;
1920   HOST_WIDE_INT val, first_byte;
1921   rtx first;
1922 
1923   if (!mips_const_vector_same_val_p (op, mode))
1924     return false;
1925 
1926   first = CONST_VECTOR_ELT (op, 0);
1927   bytes = GET_MODE_UNIT_SIZE (mode);
1928   val = INTVAL (first);
1929   first_byte = val & 0xff;
1930   for (i = 1; i < bytes; i++)
1931     {
1932       val >>= 8;
1933       if ((val & 0xff) != first_byte)
1934 	return false;
1935     }
1936 
1937   return true;
1938 }
1939 
1940 /* Return true if OP is a constant vector with the number of units in MODE,
1941    and each unit has the same integer value in the range [LOW, HIGH].  */
1942 
1943 bool
mips_const_vector_same_int_p(rtx op,machine_mode mode,HOST_WIDE_INT low,HOST_WIDE_INT high)1944 mips_const_vector_same_int_p (rtx op, machine_mode mode, HOST_WIDE_INT low,
1945 			      HOST_WIDE_INT high)
1946 {
1947   HOST_WIDE_INT value;
1948   rtx elem0;
1949 
1950   if (!mips_const_vector_same_val_p (op, mode))
1951     return false;
1952 
1953   elem0 = CONST_VECTOR_ELT (op, 0);
1954   if (!CONST_INT_P (elem0))
1955     return false;
1956 
1957   value = INTVAL (elem0);
1958   return (value >= low && value <= high);
1959 }
1960 
1961 /* Return true if OP is a constant vector with repeated 4-element sets
1962    in mode MODE.  */
1963 
1964 bool
mips_const_vector_shuffle_set_p(rtx op,machine_mode mode)1965 mips_const_vector_shuffle_set_p (rtx op, machine_mode mode)
1966 {
1967   int nunits = GET_MODE_NUNITS (mode);
1968   int nsets = nunits / 4;
1969   int set = 0;
1970   int i, j;
1971 
1972   /* Check if we have the same 4-element sets.  */
1973   for (j = 0; j < nsets; j++, set = 4 * j)
1974     for (i = 0; i < 4; i++)
1975       if ((INTVAL (XVECEXP (op, 0, i))
1976 	   != (INTVAL (XVECEXP (op, 0, set + i)) - set))
1977 	  || !IN_RANGE (INTVAL (XVECEXP (op, 0, set + i)), 0, set + 3))
1978 	return false;
1979   return true;
1980 }
1981 
1982 /* Return true if rtx constants of mode MODE should be put into a small
1983    data section.  */
1984 
1985 static bool
mips_rtx_constant_in_small_data_p(machine_mode mode)1986 mips_rtx_constant_in_small_data_p (machine_mode mode)
1987 {
1988   return (!TARGET_EMBEDDED_DATA
1989 	  && TARGET_LOCAL_SDATA
1990 	  && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1991 }
1992 
1993 /* Return true if X should not be moved directly into register $25.
1994    We need this because many versions of GAS will treat "la $25,foo" as
1995    part of a call sequence and so allow a global "foo" to be lazily bound.  */
1996 
1997 bool
mips_dangerous_for_la25_p(rtx x)1998 mips_dangerous_for_la25_p (rtx x)
1999 {
2000   return (!TARGET_EXPLICIT_RELOCS
2001 	  && TARGET_USE_GOT
2002 	  && GET_CODE (x) == SYMBOL_REF
2003 	  && mips_global_symbol_p (x));
2004 }
2005 
2006 /* Return true if calls to X might need $25 to be valid on entry.  */
2007 
2008 bool
mips_use_pic_fn_addr_reg_p(const_rtx x)2009 mips_use_pic_fn_addr_reg_p (const_rtx x)
2010 {
2011   if (!TARGET_USE_PIC_FN_ADDR_REG)
2012     return false;
2013 
2014   /* MIPS16 stub functions are guaranteed not to use $25.  */
2015   if (mips16_stub_function_p (x))
2016     return false;
2017 
2018   if (GET_CODE (x) == SYMBOL_REF)
2019     {
2020       /* If PLTs and copy relocations are available, the static linker
2021 	 will make sure that $25 is valid on entry to the target function.  */
2022       if (TARGET_ABICALLS_PIC0)
2023 	return false;
2024 
2025       /* Locally-defined functions use absolute accesses to set up
2026 	 the global pointer.  */
2027       if (TARGET_ABSOLUTE_ABICALLS
2028 	  && mips_symbol_binds_local_p (x)
2029 	  && !SYMBOL_REF_EXTERNAL_P (x))
2030 	return false;
2031     }
2032 
2033   return true;
2034 }
2035 
2036 /* Return the method that should be used to access SYMBOL_REF or
2037    LABEL_REF X in context CONTEXT.  */
2038 
2039 static enum mips_symbol_type
mips_classify_symbol(const_rtx x,enum mips_symbol_context context)2040 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
2041 {
2042   if (TARGET_RTP_PIC)
2043     return SYMBOL_GOT_DISP;
2044 
2045   if (GET_CODE (x) == LABEL_REF)
2046     {
2047       /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
2048 	 code and if we know that the label is in the current function's
2049 	 text section.  LABEL_REFs are used for jump tables as well as
2050 	 text labels, so we must check whether jump tables live in the
2051 	 text section.  */
2052       if (TARGET_MIPS16_SHORT_JUMP_TABLES
2053 	  && !LABEL_REF_NONLOCAL_P (x))
2054 	return SYMBOL_PC_RELATIVE;
2055 
2056       if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
2057 	return SYMBOL_GOT_PAGE_OFST;
2058 
2059       return SYMBOL_ABSOLUTE;
2060     }
2061 
2062   gcc_assert (GET_CODE (x) == SYMBOL_REF);
2063 
2064   if (SYMBOL_REF_TLS_MODEL (x))
2065     return SYMBOL_TLS;
2066 
2067   if (CONSTANT_POOL_ADDRESS_P (x))
2068     {
2069       if (TARGET_MIPS16_TEXT_LOADS)
2070 	return SYMBOL_PC_RELATIVE;
2071 
2072       if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
2073 	return SYMBOL_PC_RELATIVE;
2074 
2075       if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
2076 	return SYMBOL_GP_RELATIVE;
2077     }
2078 
2079   /* Do not use small-data accesses for weak symbols; they may end up
2080      being zero.  */
2081   if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
2082     return SYMBOL_GP_RELATIVE;
2083 
2084   /* Don't use GOT accesses for locally-binding symbols when -mno-shared
2085      is in effect.  */
2086   if (TARGET_ABICALLS_PIC2
2087       && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
2088     {
2089       /* There are three cases to consider:
2090 
2091 	    - o32 PIC (either with or without explicit relocs)
2092 	    - n32/n64 PIC without explicit relocs
2093 	    - n32/n64 PIC with explicit relocs
2094 
2095 	 In the first case, both local and global accesses will use an
2096 	 R_MIPS_GOT16 relocation.  We must correctly predict which of
2097 	 the two semantics (local or global) the assembler and linker
2098 	 will apply.  The choice depends on the symbol's binding rather
2099 	 than its visibility.
2100 
2101 	 In the second case, the assembler will not use R_MIPS_GOT16
2102 	 relocations, but it chooses between local and global accesses
2103 	 in the same way as for o32 PIC.
2104 
2105 	 In the third case we have more freedom since both forms of
2106 	 access will work for any kind of symbol.  However, there seems
2107 	 little point in doing things differently.  */
2108       if (mips_global_symbol_p (x))
2109 	return SYMBOL_GOT_DISP;
2110 
2111       return SYMBOL_GOT_PAGE_OFST;
2112     }
2113 
2114   return SYMBOL_ABSOLUTE;
2115 }
2116 
2117 /* Classify the base of symbolic expression X, given that X appears in
2118    context CONTEXT.  */
2119 
2120 static enum mips_symbol_type
mips_classify_symbolic_expression(rtx x,enum mips_symbol_context context)2121 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
2122 {
2123   rtx offset;
2124 
2125   split_const (x, &x, &offset);
2126   if (UNSPEC_ADDRESS_P (x))
2127     return UNSPEC_ADDRESS_TYPE (x);
2128 
2129   return mips_classify_symbol (x, context);
2130 }
2131 
2132 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
2133    is the alignment in bytes of SYMBOL_REF X.  */
2134 
2135 static bool
mips_offset_within_alignment_p(rtx x,HOST_WIDE_INT offset)2136 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
2137 {
2138   HOST_WIDE_INT align;
2139 
2140   align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
2141   return IN_RANGE (offset, 0, align - 1);
2142 }
2143 
2144 /* Return true if X is a symbolic constant that can be used in context
2145    CONTEXT.  If it is, store the type of the symbol in *SYMBOL_TYPE.  */
2146 
2147 bool
mips_symbolic_constant_p(rtx x,enum mips_symbol_context context,enum mips_symbol_type * symbol_type)2148 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
2149 			  enum mips_symbol_type *symbol_type)
2150 {
2151   rtx offset;
2152 
2153   split_const (x, &x, &offset);
2154   if (UNSPEC_ADDRESS_P (x))
2155     {
2156       *symbol_type = UNSPEC_ADDRESS_TYPE (x);
2157       x = UNSPEC_ADDRESS (x);
2158     }
2159   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
2160     {
2161       *symbol_type = mips_classify_symbol (x, context);
2162       if (*symbol_type == SYMBOL_TLS)
2163 	return false;
2164     }
2165   else
2166     return false;
2167 
2168   if (offset == const0_rtx)
2169     return true;
2170 
2171   /* Check whether a nonzero offset is valid for the underlying
2172      relocations.  */
2173   switch (*symbol_type)
2174     {
2175     case SYMBOL_ABSOLUTE:
2176     case SYMBOL_64_HIGH:
2177     case SYMBOL_64_MID:
2178     case SYMBOL_64_LOW:
2179       /* If the target has 64-bit pointers and the object file only
2180 	 supports 32-bit symbols, the values of those symbols will be
2181 	 sign-extended.  In this case we can't allow an arbitrary offset
2182 	 in case the 32-bit value X + OFFSET has a different sign from X.  */
2183       if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
2184 	return offset_within_block_p (x, INTVAL (offset));
2185 
2186       /* In other cases the relocations can handle any offset.  */
2187       return true;
2188 
2189     case SYMBOL_PC_RELATIVE:
2190       /* Allow constant pool references to be converted to LABEL+CONSTANT.
2191 	 In this case, we no longer have access to the underlying constant,
2192 	 but the original symbol-based access was known to be valid.  */
2193       if (GET_CODE (x) == LABEL_REF)
2194 	return true;
2195 
2196       /* Fall through.  */
2197 
2198     case SYMBOL_GP_RELATIVE:
2199       /* Make sure that the offset refers to something within the
2200 	 same object block.  This should guarantee that the final
2201 	 PC- or GP-relative offset is within the 16-bit limit.  */
2202       return offset_within_block_p (x, INTVAL (offset));
2203 
2204     case SYMBOL_GOT_PAGE_OFST:
2205     case SYMBOL_GOTOFF_PAGE:
2206       /* If the symbol is global, the GOT entry will contain the symbol's
2207 	 address, and we will apply a 16-bit offset after loading it.
2208 	 If the symbol is local, the linker should provide enough local
2209 	 GOT entries for a 16-bit offset, but larger offsets may lead
2210 	 to GOT overflow.  */
2211       return SMALL_INT (offset);
2212 
2213     case SYMBOL_TPREL:
2214     case SYMBOL_DTPREL:
2215       /* There is no carry between the HI and LO REL relocations, so the
2216 	 offset is only valid if we know it won't lead to such a carry.  */
2217       return mips_offset_within_alignment_p (x, INTVAL (offset));
2218 
2219     case SYMBOL_GOT_DISP:
2220     case SYMBOL_GOTOFF_DISP:
2221     case SYMBOL_GOTOFF_CALL:
2222     case SYMBOL_GOTOFF_LOADGP:
2223     case SYMBOL_TLSGD:
2224     case SYMBOL_TLSLDM:
2225     case SYMBOL_GOTTPREL:
2226     case SYMBOL_TLS:
2227     case SYMBOL_HALF:
2228       return false;
2229     }
2230   gcc_unreachable ();
2231 }
2232 
2233 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
2234    single instruction.  We rely on the fact that, in the worst case,
2235    all instructions involved in a MIPS16 address calculation are usually
2236    extended ones.  */
2237 
2238 static int
mips_symbol_insns_1(enum mips_symbol_type type,machine_mode mode)2239 mips_symbol_insns_1 (enum mips_symbol_type type, machine_mode mode)
2240 {
2241   if (mips_use_pcrel_pool_p[(int) type])
2242     {
2243       if (mode == MAX_MACHINE_MODE)
2244 	/* LEAs will be converted into constant-pool references by
2245 	   mips_reorg.  */
2246 	type = SYMBOL_PC_RELATIVE;
2247       else
2248 	/* The constant must be loaded and then dereferenced.  */
2249 	return 0;
2250     }
2251 
2252   switch (type)
2253     {
2254     case SYMBOL_ABSOLUTE:
2255       /* When using 64-bit symbols, we need 5 preparatory instructions,
2256 	 such as:
2257 
2258 	     lui     $at,%highest(symbol)
2259 	     daddiu  $at,$at,%higher(symbol)
2260 	     dsll    $at,$at,16
2261 	     daddiu  $at,$at,%hi(symbol)
2262 	     dsll    $at,$at,16
2263 
2264 	 The final address is then $at + %lo(symbol).  With 32-bit
2265 	 symbols we just need a preparatory LUI for normal mode and
2266 	 a preparatory LI and SLL for MIPS16.  */
2267       return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
2268 
2269     case SYMBOL_GP_RELATIVE:
2270       /* Treat GP-relative accesses as taking a single instruction on
2271 	 MIPS16 too; the copy of $gp can often be shared.  */
2272       return 1;
2273 
2274     case SYMBOL_PC_RELATIVE:
2275       /* PC-relative constants can be only be used with ADDIUPC,
2276 	 DADDIUPC, LWPC and LDPC.  */
2277       if (mode == MAX_MACHINE_MODE
2278 	  || GET_MODE_SIZE (mode) == 4
2279 	  || GET_MODE_SIZE (mode) == 8)
2280 	return 1;
2281 
2282       /* The constant must be loaded using ADDIUPC or DADDIUPC first.  */
2283       return 0;
2284 
2285     case SYMBOL_GOT_DISP:
2286       /* The constant will have to be loaded from the GOT before it
2287 	 is used in an address.  */
2288       if (mode != MAX_MACHINE_MODE)
2289 	return 0;
2290 
2291       /* Fall through.  */
2292 
2293     case SYMBOL_GOT_PAGE_OFST:
2294       /* Unless -funit-at-a-time is in effect, we can't be sure whether the
2295 	 local/global classification is accurate.  The worst cases are:
2296 
2297 	 (1) For local symbols when generating o32 or o64 code.  The assembler
2298 	     will use:
2299 
2300 		 lw	      $at,%got(symbol)
2301 		 nop
2302 
2303 	     ...and the final address will be $at + %lo(symbol).
2304 
2305 	 (2) For global symbols when -mxgot.  The assembler will use:
2306 
2307 	         lui     $at,%got_hi(symbol)
2308 	         (d)addu $at,$at,$gp
2309 
2310 	     ...and the final address will be $at + %got_lo(symbol).  */
2311       return 3;
2312 
2313     case SYMBOL_GOTOFF_PAGE:
2314     case SYMBOL_GOTOFF_DISP:
2315     case SYMBOL_GOTOFF_CALL:
2316     case SYMBOL_GOTOFF_LOADGP:
2317     case SYMBOL_64_HIGH:
2318     case SYMBOL_64_MID:
2319     case SYMBOL_64_LOW:
2320     case SYMBOL_TLSGD:
2321     case SYMBOL_TLSLDM:
2322     case SYMBOL_DTPREL:
2323     case SYMBOL_GOTTPREL:
2324     case SYMBOL_TPREL:
2325     case SYMBOL_HALF:
2326       /* A 16-bit constant formed by a single relocation, or a 32-bit
2327 	 constant formed from a high 16-bit relocation and a low 16-bit
2328 	 relocation.  Use mips_split_p to determine which.  32-bit
2329 	 constants need an "lui; addiu" sequence for normal mode and
2330 	 an "li; sll; addiu" sequence for MIPS16 mode.  */
2331       return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
2332 
2333     case SYMBOL_TLS:
2334       /* We don't treat a bare TLS symbol as a constant.  */
2335       return 0;
2336     }
2337   gcc_unreachable ();
2338 }
2339 
2340 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
2341    to load symbols of type TYPE into a register.  Return 0 if the given
2342    type of symbol cannot be used as an immediate operand.
2343 
2344    Otherwise, return the number of instructions needed to load or store
2345    values of mode MODE to or from addresses of type TYPE.  Return 0 if
2346    the given type of symbol is not valid in addresses.
2347 
2348    In both cases, instruction counts are based off BASE_INSN_LENGTH.  */
2349 
2350 static int
mips_symbol_insns(enum mips_symbol_type type,machine_mode mode)2351 mips_symbol_insns (enum mips_symbol_type type, machine_mode mode)
2352 {
2353   /* MSA LD.* and ST.* cannot support loading symbols via an immediate
2354      operand.  */
2355   if (MSA_SUPPORTED_MODE_P (mode))
2356     return 0;
2357 
2358   return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
2359 }
2360 
2361 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
2362 
2363 static bool
mips_cannot_force_const_mem(machine_mode mode,rtx x)2364 mips_cannot_force_const_mem (machine_mode mode, rtx x)
2365 {
2366   enum mips_symbol_type type;
2367   rtx base, offset;
2368 
2369   /* There is no assembler syntax for expressing an address-sized
2370      high part.  */
2371   if (GET_CODE (x) == HIGH)
2372     return true;
2373 
2374   /* As an optimization, reject constants that mips_legitimize_move
2375      can expand inline.
2376 
2377      Suppose we have a multi-instruction sequence that loads constant C
2378      into register R.  If R does not get allocated a hard register, and
2379      R is used in an operand that allows both registers and memory
2380      references, reload will consider forcing C into memory and using
2381      one of the instruction's memory alternatives.  Returning false
2382      here will force it to use an input reload instead.  */
2383   if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
2384     return true;
2385 
2386   split_const (x, &base, &offset);
2387   if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
2388     {
2389       /* See whether we explicitly want these symbols in the pool.  */
2390       if (mips_use_pcrel_pool_p[(int) type])
2391 	return false;
2392 
2393       /* The same optimization as for CONST_INT.  */
2394       if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2395 	return true;
2396 
2397       /* If MIPS16 constant pools live in the text section, they should
2398 	 not refer to anything that might need run-time relocation.  */
2399       if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2400 	return true;
2401     }
2402 
2403   /* TLS symbols must be computed by mips_legitimize_move.  */
2404   if (tls_referenced_p (x))
2405     return true;
2406 
2407   return false;
2408 }
2409 
2410 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  We can't use blocks for
2411    constants when we're using a per-function constant pool.  */
2412 
2413 static bool
mips_use_blocks_for_constant_p(machine_mode mode ATTRIBUTE_UNUSED,const_rtx x ATTRIBUTE_UNUSED)2414 mips_use_blocks_for_constant_p (machine_mode mode ATTRIBUTE_UNUSED,
2415 				const_rtx x ATTRIBUTE_UNUSED)
2416 {
2417   return !TARGET_MIPS16_PCREL_LOADS;
2418 }
2419 
2420 /* Return true if register REGNO is a valid base register for mode MODE.
2421    STRICT_P is true if REG_OK_STRICT is in effect.  */
2422 
2423 int
mips_regno_mode_ok_for_base_p(int regno,machine_mode mode,bool strict_p)2424 mips_regno_mode_ok_for_base_p (int regno, machine_mode mode,
2425 			       bool strict_p)
2426 {
2427   if (!HARD_REGISTER_NUM_P (regno))
2428     {
2429       if (!strict_p)
2430 	return true;
2431       regno = reg_renumber[regno];
2432     }
2433 
2434   /* These fake registers will be eliminated to either the stack or
2435      hard frame pointer, both of which are usually valid base registers.
2436      Reload deals with the cases where the eliminated form isn't valid.  */
2437   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2438     return true;
2439 
2440   /* In MIPS16 mode, the stack pointer can only address word and doubleword
2441      values, nothing smaller.  */
2442   if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2443     return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2444 
2445   return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2446 }
2447 
2448 /* Return true if X is a valid base register for mode MODE.
2449    STRICT_P is true if REG_OK_STRICT is in effect.  */
2450 
2451 static bool
mips_valid_base_register_p(rtx x,machine_mode mode,bool strict_p)2452 mips_valid_base_register_p (rtx x, machine_mode mode, bool strict_p)
2453 {
2454   if (!strict_p && GET_CODE (x) == SUBREG)
2455     x = SUBREG_REG (x);
2456 
2457   return (REG_P (x)
2458 	  && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2459 }
2460 
2461 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2462    can address a value of mode MODE.  */
2463 
2464 static bool
mips_valid_offset_p(rtx x,machine_mode mode)2465 mips_valid_offset_p (rtx x, machine_mode mode)
2466 {
2467   /* Check that X is a signed 16-bit number.  */
2468   if (!const_arith_operand (x, Pmode))
2469     return false;
2470 
2471   /* We may need to split multiword moves, so make sure that every word
2472      is accessible.  */
2473   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2474       && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2475     return false;
2476 
2477   /* MSA LD.* and ST.* supports 10-bit signed offsets.  */
2478   if (MSA_SUPPORTED_MODE_P (mode)
2479       && !mips_signed_immediate_p (INTVAL (x), 10,
2480 				   mips_ldst_scaled_shift (mode)))
2481     return false;
2482 
2483   return true;
2484 }
2485 
2486 /* Return true if a LO_SUM can address a value of mode MODE when the
2487    LO_SUM symbol has type SYMBOL_TYPE.  */
2488 
2489 static bool
mips_valid_lo_sum_p(enum mips_symbol_type symbol_type,machine_mode mode)2490 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, machine_mode mode)
2491 {
2492   /* Check that symbols of type SYMBOL_TYPE can be used to access values
2493      of mode MODE.  */
2494   if (mips_symbol_insns (symbol_type, mode) == 0)
2495     return false;
2496 
2497   /* Check that there is a known low-part relocation.  */
2498   if (mips_lo_relocs[symbol_type] == NULL)
2499     return false;
2500 
2501   /* We may need to split multiword moves, so make sure that each word
2502      can be accessed without inducing a carry.  This is mainly needed
2503      for o64, which has historically only guaranteed 64-bit alignment
2504      for 128-bit types.  */
2505   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2506       && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2507     return false;
2508 
2509   /* MSA LD.* and ST.* cannot support loading symbols via %lo($base).  */
2510   if (MSA_SUPPORTED_MODE_P (mode))
2511     return false;
2512 
2513   return true;
2514 }
2515 
2516 /* Return true if X is a valid address for machine mode MODE.  If it is,
2517    fill in INFO appropriately.  STRICT_P is true if REG_OK_STRICT is in
2518    effect.  */
2519 
2520 static bool
mips_classify_address(struct mips_address_info * info,rtx x,machine_mode mode,bool strict_p)2521 mips_classify_address (struct mips_address_info *info, rtx x,
2522 		       machine_mode mode, bool strict_p)
2523 {
2524   switch (GET_CODE (x))
2525     {
2526     case REG:
2527     case SUBREG:
2528       info->type = ADDRESS_REG;
2529       info->reg = x;
2530       info->offset = const0_rtx;
2531       return mips_valid_base_register_p (info->reg, mode, strict_p);
2532 
2533     case PLUS:
2534       info->type = ADDRESS_REG;
2535       info->reg = XEXP (x, 0);
2536       info->offset = XEXP (x, 1);
2537       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2538 	      && mips_valid_offset_p (info->offset, mode));
2539 
2540     case LO_SUM:
2541       info->type = ADDRESS_LO_SUM;
2542       info->reg = XEXP (x, 0);
2543       info->offset = XEXP (x, 1);
2544       /* We have to trust the creator of the LO_SUM to do something vaguely
2545 	 sane.  Target-independent code that creates a LO_SUM should also
2546 	 create and verify the matching HIGH.  Target-independent code that
2547 	 adds an offset to a LO_SUM must prove that the offset will not
2548 	 induce a carry.  Failure to do either of these things would be
2549 	 a bug, and we are not required to check for it here.  The MIPS
2550 	 backend itself should only create LO_SUMs for valid symbolic
2551 	 constants, with the high part being either a HIGH or a copy
2552 	 of _gp. */
2553       info->symbol_type
2554 	= mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2555       return (mips_valid_base_register_p (info->reg, mode, strict_p)
2556 	      && mips_valid_lo_sum_p (info->symbol_type, mode));
2557 
2558     case CONST_INT:
2559       /* Small-integer addresses don't occur very often, but they
2560 	 are legitimate if $0 is a valid base register.  */
2561       info->type = ADDRESS_CONST_INT;
2562       return !TARGET_MIPS16 && SMALL_INT (x);
2563 
2564     case CONST:
2565     case LABEL_REF:
2566     case SYMBOL_REF:
2567       info->type = ADDRESS_SYMBOLIC;
2568       return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2569 					&info->symbol_type)
2570 	      && mips_symbol_insns (info->symbol_type, mode) > 0
2571 	      && !mips_split_p[info->symbol_type]);
2572 
2573     default:
2574       return false;
2575     }
2576 }
2577 
2578 /* Implement TARGET_LEGITIMATE_ADDRESS_P.  */
2579 
2580 static bool
mips_legitimate_address_p(machine_mode mode,rtx x,bool strict_p)2581 mips_legitimate_address_p (machine_mode mode, rtx x, bool strict_p)
2582 {
2583   struct mips_address_info addr;
2584 
2585   return mips_classify_address (&addr, x, mode, strict_p);
2586 }
2587 
2588 /* Return true if X is a legitimate $sp-based address for mode MODE.  */
2589 
2590 bool
mips_stack_address_p(rtx x,machine_mode mode)2591 mips_stack_address_p (rtx x, machine_mode mode)
2592 {
2593   struct mips_address_info addr;
2594 
2595   return (mips_classify_address (&addr, x, mode, false)
2596 	  && addr.type == ADDRESS_REG
2597 	  && addr.reg == stack_pointer_rtx);
2598 }
2599 
2600 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2601    address instruction.  Note that such addresses are not considered
2602    legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2603    is so restricted.  */
2604 
2605 static bool
mips_lwxs_address_p(rtx addr)2606 mips_lwxs_address_p (rtx addr)
2607 {
2608   if (ISA_HAS_LWXS
2609       && GET_CODE (addr) == PLUS
2610       && REG_P (XEXP (addr, 1)))
2611     {
2612       rtx offset = XEXP (addr, 0);
2613       if (GET_CODE (offset) == MULT
2614 	  && REG_P (XEXP (offset, 0))
2615 	  && CONST_INT_P (XEXP (offset, 1))
2616 	  && INTVAL (XEXP (offset, 1)) == 4)
2617 	return true;
2618     }
2619   return false;
2620 }
2621 
2622 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load
2623    indexed address instruction.  Note that such addresses are
2624    not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2625    sense, because their use is so restricted.  */
2626 
2627 static bool
mips_lx_address_p(rtx addr,machine_mode mode)2628 mips_lx_address_p (rtx addr, machine_mode mode)
2629 {
2630   if (GET_CODE (addr) != PLUS
2631       || !REG_P (XEXP (addr, 0))
2632       || !REG_P (XEXP (addr, 1)))
2633     return false;
2634   if (ISA_HAS_LBX && mode == QImode)
2635     return true;
2636   if (ISA_HAS_LHX && mode == HImode)
2637     return true;
2638   if (ISA_HAS_LWX && mode == SImode)
2639     return true;
2640   if (ISA_HAS_LDX && mode == DImode)
2641     return true;
2642   if (MSA_SUPPORTED_MODE_P (mode))
2643     return true;
2644   return false;
2645 }
2646 
2647 /* Return true if a value at OFFSET bytes from base register BASE can be
2648    accessed using an unextended MIPS16 instruction.  MODE is the mode of
2649    the value.
2650 
2651    Usually the offset in an unextended instruction is a 5-bit field.
2652    The offset is unsigned and shifted left once for LH and SH, twice
2653    for LW and SW, and so on.  An exception is LWSP and SWSP, which have
2654    an 8-bit immediate field that's shifted left twice.  */
2655 
2656 static bool
mips16_unextended_reference_p(machine_mode mode,rtx base,unsigned HOST_WIDE_INT offset)2657 mips16_unextended_reference_p (machine_mode mode, rtx base,
2658 			       unsigned HOST_WIDE_INT offset)
2659 {
2660   if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2661     {
2662       if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2663 	return offset < 256U * GET_MODE_SIZE (mode);
2664       return offset < 32U * GET_MODE_SIZE (mode);
2665     }
2666   return false;
2667 }
2668 
2669 /* Return the number of instructions needed to load or store a value
2670    of mode MODE at address X, assuming that BASE_INSN_LENGTH is the
2671    length of one instruction.  Return 0 if X isn't valid for MODE.
2672    Assume that multiword moves may need to be split into word moves
2673    if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2674    enough.  */
2675 
2676 int
mips_address_insns(rtx x,machine_mode mode,bool might_split_p)2677 mips_address_insns (rtx x, machine_mode mode, bool might_split_p)
2678 {
2679   struct mips_address_info addr;
2680   int factor;
2681   bool msa_p = (!might_split_p && MSA_SUPPORTED_MODE_P (mode));
2682 
2683   /* BLKmode is used for single unaligned loads and stores and should
2684      not count as a multiword mode.  (GET_MODE_SIZE (BLKmode) is pretty
2685      meaningless, so we have to single it out as a special case one way
2686      or the other.)  */
2687   if (mode != BLKmode && might_split_p)
2688     factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2689   else
2690     factor = 1;
2691 
2692   if (mips_classify_address (&addr, x, mode, false))
2693     switch (addr.type)
2694       {
2695       case ADDRESS_REG:
2696 	if (msa_p)
2697 	  {
2698 	    /* MSA LD.* and ST.* supports 10-bit signed offsets.  */
2699 	    if (mips_signed_immediate_p (INTVAL (addr.offset), 10,
2700 					 mips_ldst_scaled_shift (mode)))
2701 	      return 1;
2702 	    else
2703 	      return 0;
2704 	  }
2705 	if (TARGET_MIPS16
2706 	    && !mips16_unextended_reference_p (mode, addr.reg,
2707 					       UINTVAL (addr.offset)))
2708 	  return factor * 2;
2709 	return factor;
2710 
2711       case ADDRESS_LO_SUM:
2712 	return msa_p ? 0 : TARGET_MIPS16 ? factor * 2 : factor;
2713 
2714       case ADDRESS_CONST_INT:
2715 	return msa_p ? 0 : factor;
2716 
2717       case ADDRESS_SYMBOLIC:
2718 	return msa_p ? 0 : factor * mips_symbol_insns (addr.symbol_type, mode);
2719       }
2720   return 0;
2721 }
2722 
2723 /* Return true if X fits within an unsigned field of BITS bits that is
2724    shifted left SHIFT bits before being used.  */
2725 
2726 bool
2727 mips_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2728 {
2729   return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2730 }
2731 
2732 /* Return true if X fits within a signed field of BITS bits that is
2733    shifted left SHIFT bits before being used.  */
2734 
2735 bool
2736 mips_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2737 {
2738   x += 1 << (bits + shift - 1);
2739   return mips_unsigned_immediate_p (x, bits, shift);
2740 }
2741 
2742 /* Return the scale shift that applied to MSA LD/ST address offset.  */
2743 
2744 int
mips_ldst_scaled_shift(machine_mode mode)2745 mips_ldst_scaled_shift (machine_mode mode)
2746 {
2747   int shift = exact_log2 (GET_MODE_UNIT_SIZE (mode));
2748 
2749   if (shift < 0 || shift > 8)
2750     gcc_unreachable ();
2751 
2752   return shift;
2753 }
2754 
2755 /* Return true if X is legitimate for accessing values of mode MODE,
2756    if it is based on a MIPS16 register, and if the offset satisfies
2757    OFFSET_PREDICATE.  */
2758 
2759 bool
m16_based_address_p(rtx x,machine_mode mode,insn_operand_predicate_fn offset_predicate)2760 m16_based_address_p (rtx x, machine_mode mode,
2761 		     insn_operand_predicate_fn offset_predicate)
2762 {
2763   struct mips_address_info addr;
2764 
2765   return (mips_classify_address (&addr, x, mode, false)
2766 	  && addr.type == ADDRESS_REG
2767 	  && M16_REG_P (REGNO (addr.reg))
2768 	  && offset_predicate (addr.offset, mode));
2769 }
2770 
2771 /* Return true if X is a legitimate address that conforms to the requirements
2772    for a microMIPS LWSP or SWSP insn.  */
2773 
2774 bool
lwsp_swsp_address_p(rtx x,machine_mode mode)2775 lwsp_swsp_address_p (rtx x, machine_mode mode)
2776 {
2777   struct mips_address_info addr;
2778 
2779   return (mips_classify_address (&addr, x, mode, false)
2780 	  && addr.type == ADDRESS_REG
2781 	  && REGNO (addr.reg) == STACK_POINTER_REGNUM
2782 	  && uw5_operand (addr.offset, mode));
2783 }
2784 
2785 /* Return true if X is a legitimate address with a 12-bit offset.
2786    MODE is the mode of the value being accessed.  */
2787 
2788 bool
umips_12bit_offset_address_p(rtx x,machine_mode mode)2789 umips_12bit_offset_address_p (rtx x, machine_mode mode)
2790 {
2791   struct mips_address_info addr;
2792 
2793   return (mips_classify_address (&addr, x, mode, false)
2794 	  && addr.type == ADDRESS_REG
2795 	  && CONST_INT_P (addr.offset)
2796 	  && UMIPS_12BIT_OFFSET_P (INTVAL (addr.offset)));
2797 }
2798 
2799 /* Return true if X is a legitimate address with a 9-bit offset.
2800    MODE is the mode of the value being accessed.  */
2801 
2802 bool
mips_9bit_offset_address_p(rtx x,machine_mode mode)2803 mips_9bit_offset_address_p (rtx x, machine_mode mode)
2804 {
2805   struct mips_address_info addr;
2806 
2807   return (mips_classify_address (&addr, x, mode, false)
2808 	  && addr.type == ADDRESS_REG
2809 	  && CONST_INT_P (addr.offset)
2810 	  && MIPS_9BIT_OFFSET_P (INTVAL (addr.offset)));
2811 }
2812 
2813 /* Return the number of instructions needed to load constant X,
2814    assuming that BASE_INSN_LENGTH is the length of one instruction.
2815    Return 0 if X isn't a valid constant.  */
2816 
2817 int
mips_const_insns(rtx x)2818 mips_const_insns (rtx x)
2819 {
2820   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2821   enum mips_symbol_type symbol_type;
2822   rtx offset;
2823 
2824   switch (GET_CODE (x))
2825     {
2826     case HIGH:
2827       if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2828 				     &symbol_type)
2829 	  || !mips_split_p[symbol_type])
2830 	return 0;
2831 
2832       /* This is simply an LUI for normal mode.  It is an extended
2833 	 LI followed by an extended SLL for MIPS16.  */
2834       return TARGET_MIPS16 ? 4 : 1;
2835 
2836     case CONST_INT:
2837       if (TARGET_MIPS16)
2838 	/* Unsigned 8-bit constants can be loaded using an unextended
2839 	   LI instruction.  Unsigned 16-bit constants can be loaded
2840 	   using an extended LI.  Negative constants must be loaded
2841 	   using LI and then negated.  */
2842 	return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2843 		: SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2844 		: IN_RANGE (-INTVAL (x), 0, 255) ? 2
2845 		: SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2846 		: 0);
2847 
2848       return mips_build_integer (codes, INTVAL (x));
2849 
2850     case CONST_VECTOR:
2851       if (ISA_HAS_MSA
2852 	  && mips_const_vector_same_int_p (x, GET_MODE (x), -512, 511))
2853 	return 1;
2854       /* Fall through.  */
2855     case CONST_DOUBLE:
2856       /* Allow zeros for normal mode, where we can use $0.  */
2857       return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2858 
2859     case CONST:
2860       if (CONST_GP_P (x))
2861 	return 1;
2862 
2863       /* See if we can refer to X directly.  */
2864       if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2865 	return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2866 
2867       /* Otherwise try splitting the constant into a base and offset.
2868 	 If the offset is a 16-bit value, we can load the base address
2869 	 into a register and then use (D)ADDIU to add in the offset.
2870 	 If the offset is larger, we can load the base and offset
2871 	 into separate registers and add them together with (D)ADDU.
2872 	 However, the latter is only possible before reload; during
2873 	 and after reload, we must have the option of forcing the
2874 	 constant into the pool instead.  */
2875       split_const (x, &x, &offset);
2876       if (offset != 0)
2877 	{
2878 	  int n = mips_const_insns (x);
2879 	  if (n != 0)
2880 	    {
2881 	      if (SMALL_INT (offset))
2882 		return n + 1;
2883 	      else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2884 		return n + 1 + mips_build_integer (codes, INTVAL (offset));
2885 	    }
2886 	}
2887       return 0;
2888 
2889     case SYMBOL_REF:
2890     case LABEL_REF:
2891       return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2892 				MAX_MACHINE_MODE);
2893 
2894     default:
2895       return 0;
2896     }
2897 }
2898 
2899 /* X is a doubleword constant that can be handled by splitting it into
2900    two words and loading each word separately.  Return the number of
2901    instructions required to do this, assuming that BASE_INSN_LENGTH
2902    is the length of one instruction.  */
2903 
2904 int
mips_split_const_insns(rtx x)2905 mips_split_const_insns (rtx x)
2906 {
2907   unsigned int low, high;
2908 
2909   low = mips_const_insns (mips_subword (x, false));
2910   high = mips_const_insns (mips_subword (x, true));
2911   gcc_assert (low > 0 && high > 0);
2912   return low + high;
2913 }
2914 
2915 /* Return one word of 128-bit value OP, taking into account the fixed
2916    endianness of certain registers.  BYTE selects from the byte address.  */
2917 
2918 rtx
mips_subword_at_byte(rtx op,unsigned int byte)2919 mips_subword_at_byte (rtx op, unsigned int byte)
2920 {
2921   machine_mode mode;
2922 
2923   mode = GET_MODE (op);
2924   if (mode == VOIDmode)
2925     mode = TImode;
2926 
2927   gcc_assert (!FP_REG_RTX_P (op));
2928 
2929   if (MEM_P (op))
2930     return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
2931 
2932   return simplify_gen_subreg (word_mode, op, mode, byte);
2933 }
2934 
2935 /* Return the number of instructions needed to implement INSN,
2936    given that it loads from or stores to MEM.  Assume that
2937    BASE_INSN_LENGTH is the length of one instruction.  */
2938 
2939 int
mips_load_store_insns(rtx mem,rtx_insn * insn)2940 mips_load_store_insns (rtx mem, rtx_insn *insn)
2941 {
2942   machine_mode mode;
2943   bool might_split_p;
2944   rtx set;
2945 
2946   gcc_assert (MEM_P (mem));
2947   mode = GET_MODE (mem);
2948 
2949   /* Try to prove that INSN does not need to be split.  */
2950   might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2951   if (might_split_p)
2952     {
2953       set = single_set (insn);
2954       if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
2955 	might_split_p = false;
2956     }
2957 
2958   return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2959 }
2960 
2961 /* Return the number of instructions needed for an integer division,
2962    assuming that BASE_INSN_LENGTH is the length of one instruction.  */
2963 
2964 int
mips_idiv_insns(machine_mode mode)2965 mips_idiv_insns (machine_mode mode)
2966 {
2967   int count;
2968 
2969   count = 1;
2970   if (TARGET_CHECK_ZERO_DIV)
2971     {
2972       if (GENERATE_DIVIDE_TRAPS && !MSA_SUPPORTED_MODE_P (mode))
2973         count++;
2974       else
2975         count += 2;
2976     }
2977 
2978   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2979     count++;
2980   return count;
2981 }
2982 
2983 
2984 /* Emit a move from SRC to DEST.  Assume that the move expanders can
2985    handle all moves if !can_create_pseudo_p ().  The distinction is
2986    important because, unlike emit_move_insn, the move expanders know
2987    how to force Pmode objects into the constant pool even when the
2988    constant pool address is not itself legitimate.  */
2989 
2990 rtx_insn *
mips_emit_move(rtx dest,rtx src)2991 mips_emit_move (rtx dest, rtx src)
2992 {
2993   return (can_create_pseudo_p ()
2994 	  ? emit_move_insn (dest, src)
2995 	  : emit_move_insn_1 (dest, src));
2996 }
2997 
2998 /* Emit a move from SRC to DEST, splitting compound moves into individual
2999    instructions.  SPLIT_TYPE is the type of split to perform.  */
3000 
3001 static void
mips_emit_move_or_split(rtx dest,rtx src,enum mips_split_type split_type)3002 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
3003 {
3004   if (mips_split_move_p (dest, src, split_type))
3005     mips_split_move (dest, src, split_type);
3006   else
3007     mips_emit_move (dest, src);
3008 }
3009 
3010 /* Emit an instruction of the form (set TARGET (CODE OP0)).  */
3011 
3012 static void
mips_emit_unary(enum rtx_code code,rtx target,rtx op0)3013 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
3014 {
3015   emit_insn (gen_rtx_SET (target, gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
3016 }
3017 
3018 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
3019    Return that new register.  */
3020 
3021 static rtx
mips_force_unary(machine_mode mode,enum rtx_code code,rtx op0)3022 mips_force_unary (machine_mode mode, enum rtx_code code, rtx op0)
3023 {
3024   rtx reg;
3025 
3026   reg = gen_reg_rtx (mode);
3027   mips_emit_unary (code, reg, op0);
3028   return reg;
3029 }
3030 
3031 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)).  */
3032 
3033 void
mips_emit_binary(enum rtx_code code,rtx target,rtx op0,rtx op1)3034 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
3035 {
3036   emit_insn (gen_rtx_SET (target, gen_rtx_fmt_ee (code, GET_MODE (target),
3037 						  op0, op1)));
3038 }
3039 
3040 /* Compute (CODE OP0 OP1) and store the result in a new register
3041    of mode MODE.  Return that new register.  */
3042 
3043 static rtx
mips_force_binary(machine_mode mode,enum rtx_code code,rtx op0,rtx op1)3044 mips_force_binary (machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
3045 {
3046   rtx reg;
3047 
3048   reg = gen_reg_rtx (mode);
3049   mips_emit_binary (code, reg, op0, op1);
3050   return reg;
3051 }
3052 
3053 /* Copy VALUE to a register and return that register.  If new pseudos
3054    are allowed, copy it into a new register, otherwise use DEST.  */
3055 
3056 static rtx
mips_force_temporary(rtx dest,rtx value)3057 mips_force_temporary (rtx dest, rtx value)
3058 {
3059   if (can_create_pseudo_p ())
3060     return force_reg (Pmode, value);
3061   else
3062     {
3063       mips_emit_move (dest, value);
3064       return dest;
3065     }
3066 }
3067 
3068 /* Emit a call sequence with call pattern PATTERN and return the call
3069    instruction itself (which is not necessarily the last instruction
3070    emitted).  ORIG_ADDR is the original, unlegitimized address,
3071    ADDR is the legitimized form, and LAZY_P is true if the call
3072    address is lazily-bound.  */
3073 
3074 static rtx_insn *
mips_emit_call_insn(rtx pattern,rtx orig_addr,rtx addr,bool lazy_p)3075 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
3076 {
3077   rtx_insn *insn;
3078   rtx reg;
3079 
3080   insn = emit_call_insn (pattern);
3081 
3082   if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
3083     {
3084       /* MIPS16 JALRs only take MIPS16 registers.  If the target
3085 	 function requires $25 to be valid on entry, we must copy it
3086 	 there separately.  The move instruction can be put in the
3087 	 call's delay slot.  */
3088       reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
3089       emit_insn_before (gen_move_insn (reg, addr), insn);
3090       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
3091     }
3092 
3093   if (lazy_p)
3094     /* Lazy-binding stubs require $gp to be valid on entry.  */
3095     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
3096 
3097   if (TARGET_USE_GOT)
3098     {
3099       /* See the comment above load_call<mode> for details.  */
3100       use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
3101 	       gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
3102       emit_insn (gen_update_got_version ());
3103     }
3104 
3105   if (TARGET_MIPS16
3106       && TARGET_EXPLICIT_RELOCS
3107       && TARGET_CALL_CLOBBERED_GP)
3108     {
3109       rtx post_call_tmp_reg = gen_rtx_REG (word_mode, POST_CALL_TMP_REG);
3110       clobber_reg (&CALL_INSN_FUNCTION_USAGE (insn), post_call_tmp_reg);
3111     }
3112 
3113   return insn;
3114 }
3115 
3116 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
3117    then add CONST_INT OFFSET to the result.  */
3118 
3119 static rtx
mips_unspec_address_offset(rtx base,rtx offset,enum mips_symbol_type symbol_type)3120 mips_unspec_address_offset (rtx base, rtx offset,
3121 			    enum mips_symbol_type symbol_type)
3122 {
3123   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
3124 			 UNSPEC_ADDRESS_FIRST + symbol_type);
3125   if (offset != const0_rtx)
3126     base = gen_rtx_PLUS (Pmode, base, offset);
3127   return gen_rtx_CONST (Pmode, base);
3128 }
3129 
3130 /* Return an UNSPEC address with underlying address ADDRESS and symbol
3131    type SYMBOL_TYPE.  */
3132 
3133 rtx
mips_unspec_address(rtx address,enum mips_symbol_type symbol_type)3134 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
3135 {
3136   rtx base, offset;
3137 
3138   split_const (address, &base, &offset);
3139   return mips_unspec_address_offset (base, offset, symbol_type);
3140 }
3141 
3142 /* If OP is an UNSPEC address, return the address to which it refers,
3143    otherwise return OP itself.  */
3144 
3145 rtx
mips_strip_unspec_address(rtx op)3146 mips_strip_unspec_address (rtx op)
3147 {
3148   rtx base, offset;
3149 
3150   split_const (op, &base, &offset);
3151   if (UNSPEC_ADDRESS_P (base))
3152     op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
3153   return op;
3154 }
3155 
3156 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
3157    high part to BASE and return the result.  Just return BASE otherwise.
3158    TEMP is as for mips_force_temporary.
3159 
3160    The returned expression can be used as the first operand to a LO_SUM.  */
3161 
3162 static rtx
mips_unspec_offset_high(rtx temp,rtx base,rtx addr,enum mips_symbol_type symbol_type)3163 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
3164 			 enum mips_symbol_type symbol_type)
3165 {
3166   if (mips_split_p[symbol_type])
3167     {
3168       addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
3169       addr = mips_force_temporary (temp, addr);
3170       base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
3171     }
3172   return base;
3173 }
3174 
3175 /* Return an instruction that copies $gp into register REG.  We want
3176    GCC to treat the register's value as constant, so that its value
3177    can be rematerialized on demand.  */
3178 
3179 static rtx
gen_load_const_gp(rtx reg)3180 gen_load_const_gp (rtx reg)
3181 {
3182   return PMODE_INSN (gen_load_const_gp, (reg));
3183 }
3184 
3185 /* Return a pseudo register that contains the value of $gp throughout
3186    the current function.  Such registers are needed by MIPS16 functions,
3187    for which $gp itself is not a valid base register or addition operand.  */
3188 
3189 static rtx
mips16_gp_pseudo_reg(void)3190 mips16_gp_pseudo_reg (void)
3191 {
3192   if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
3193     {
3194       rtx_insn *scan;
3195 
3196       cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
3197 
3198       push_topmost_sequence ();
3199 
3200       scan = get_insns ();
3201       while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
3202 	scan = NEXT_INSN (scan);
3203 
3204       rtx set = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
3205       rtx_insn *insn = emit_insn_after (set, scan);
3206       INSN_LOCATION (insn) = 0;
3207 
3208       pop_topmost_sequence ();
3209     }
3210 
3211   return cfun->machine->mips16_gp_pseudo_rtx;
3212 }
3213 
3214 /* Return a base register that holds pic_offset_table_rtx.
3215    TEMP, if nonnull, is a scratch Pmode base register.  */
3216 
3217 rtx
mips_pic_base_register(rtx temp)3218 mips_pic_base_register (rtx temp)
3219 {
3220   if (!TARGET_MIPS16)
3221     return pic_offset_table_rtx;
3222 
3223   if (currently_expanding_to_rtl)
3224     return mips16_gp_pseudo_reg ();
3225 
3226   if (can_create_pseudo_p ())
3227     temp = gen_reg_rtx (Pmode);
3228 
3229   if (TARGET_USE_GOT)
3230     /* The first post-reload split exposes all references to $gp
3231        (both uses and definitions).  All references must remain
3232        explicit after that point.
3233 
3234        It is safe to introduce uses of $gp at any time, so for
3235        simplicity, we do that before the split too.  */
3236     mips_emit_move (temp, pic_offset_table_rtx);
3237   else
3238     emit_insn (gen_load_const_gp (temp));
3239   return temp;
3240 }
3241 
3242 /* Return the RHS of a load_call<mode> insn.  */
3243 
3244 static rtx
mips_unspec_call(rtx reg,rtx symbol)3245 mips_unspec_call (rtx reg, rtx symbol)
3246 {
3247   rtvec vec;
3248 
3249   vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
3250   return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
3251 }
3252 
3253 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
3254    reference.  Return NULL_RTX otherwise.  */
3255 
3256 static rtx
mips_strip_unspec_call(rtx src)3257 mips_strip_unspec_call (rtx src)
3258 {
3259   if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
3260     return mips_strip_unspec_address (XVECEXP (src, 0, 1));
3261   return NULL_RTX;
3262 }
3263 
3264 /* Create and return a GOT reference of type TYPE for address ADDR.
3265    TEMP, if nonnull, is a scratch Pmode base register.  */
3266 
3267 rtx
mips_got_load(rtx temp,rtx addr,enum mips_symbol_type type)3268 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
3269 {
3270   rtx base, high, lo_sum_symbol;
3271 
3272   base = mips_pic_base_register (temp);
3273 
3274   /* If we used the temporary register to load $gp, we can't use
3275      it for the high part as well.  */
3276   if (temp != NULL && reg_overlap_mentioned_p (base, temp))
3277     temp = NULL;
3278 
3279   high = mips_unspec_offset_high (temp, base, addr, type);
3280   lo_sum_symbol = mips_unspec_address (addr, type);
3281 
3282   if (type == SYMBOL_GOTOFF_CALL)
3283     return mips_unspec_call (high, lo_sum_symbol);
3284   else
3285     return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
3286 }
3287 
3288 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
3289    it appears in a MEM of that mode.  Return true if ADDR is a legitimate
3290    constant in that context and can be split into high and low parts.
3291    If so, and if LOW_OUT is nonnull, emit the high part and store the
3292    low part in *LOW_OUT.  Leave *LOW_OUT unchanged otherwise.
3293 
3294    TEMP is as for mips_force_temporary and is used to load the high
3295    part into a register.
3296 
3297    When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
3298    a legitimize SET_SRC for an .md pattern, otherwise the low part
3299    is guaranteed to be a legitimate address for mode MODE.  */
3300 
3301 bool
mips_split_symbol(rtx temp,rtx addr,machine_mode mode,rtx * low_out)3302 mips_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
3303 {
3304   enum mips_symbol_context context;
3305   enum mips_symbol_type symbol_type;
3306   rtx high;
3307 
3308   context = (mode == MAX_MACHINE_MODE
3309 	     ? SYMBOL_CONTEXT_LEA
3310 	     : SYMBOL_CONTEXT_MEM);
3311   if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
3312     {
3313       addr = XEXP (addr, 0);
3314       if (mips_symbolic_constant_p (addr, context, &symbol_type)
3315 	  && mips_symbol_insns (symbol_type, mode) > 0
3316 	  && mips_split_hi_p[symbol_type])
3317 	{
3318 	  if (low_out)
3319 	    switch (symbol_type)
3320 	      {
3321 	      case SYMBOL_GOT_PAGE_OFST:
3322 		/* The high part of a page/ofst pair is loaded from the GOT.  */
3323 		*low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
3324 		break;
3325 
3326 	      default:
3327 		gcc_unreachable ();
3328 	      }
3329 	  return true;
3330 	}
3331     }
3332   else
3333     {
3334       if (mips_symbolic_constant_p (addr, context, &symbol_type)
3335 	  && mips_symbol_insns (symbol_type, mode) > 0
3336 	  && mips_split_p[symbol_type])
3337 	{
3338 	  if (low_out)
3339 	    switch (symbol_type)
3340 	      {
3341 	      case SYMBOL_GOT_DISP:
3342 		/* SYMBOL_GOT_DISP symbols are loaded from the GOT.  */
3343 		*low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
3344 		break;
3345 
3346 	      case SYMBOL_GP_RELATIVE:
3347 		high = mips_pic_base_register (temp);
3348 		*low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3349 		break;
3350 
3351 	      default:
3352 		high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
3353 		high = mips_force_temporary (temp, high);
3354 		*low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3355 		break;
3356 	      }
3357 	  return true;
3358 	}
3359     }
3360   return false;
3361 }
3362 
3363 /* Return a legitimate address for REG + OFFSET.  TEMP is as for
3364    mips_force_temporary; it is only needed when OFFSET is not a
3365    SMALL_OPERAND.  */
3366 
3367 static rtx
mips_add_offset(rtx temp,rtx reg,HOST_WIDE_INT offset)3368 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
3369 {
3370   if (!SMALL_OPERAND (offset))
3371     {
3372       rtx high;
3373 
3374       if (TARGET_MIPS16)
3375 	{
3376 	  /* Load the full offset into a register so that we can use
3377 	     an unextended instruction for the address itself.  */
3378 	  high = GEN_INT (offset);
3379 	  offset = 0;
3380 	}
3381       else
3382 	{
3383 	  /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
3384 	     The addition inside the macro CONST_HIGH_PART may cause an
3385 	     overflow, so we need to force a sign-extension check.  */
3386 	  high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
3387 	  offset = CONST_LOW_PART (offset);
3388 	}
3389       high = mips_force_temporary (temp, high);
3390       reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
3391     }
3392   return plus_constant (Pmode, reg, offset);
3393 }
3394 
3395 /* The __tls_get_attr symbol.  */
3396 static GTY(()) rtx mips_tls_symbol;
3397 
3398 /* Return an instruction sequence that calls __tls_get_addr.  SYM is
3399    the TLS symbol we are referencing and TYPE is the symbol type to use
3400    (either global dynamic or local dynamic).  V0 is an RTX for the
3401    return value location.  */
3402 
3403 static rtx_insn *
mips_call_tls_get_addr(rtx sym,enum mips_symbol_type type,rtx v0)3404 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
3405 {
3406   rtx loc, a0;
3407   rtx_insn *insn;
3408 
3409   a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3410 
3411   if (!mips_tls_symbol)
3412     mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
3413 
3414   loc = mips_unspec_address (sym, type);
3415 
3416   start_sequence ();
3417 
3418   emit_insn (gen_rtx_SET (a0, gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx,
3419 					      loc)));
3420   insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
3421 			   const0_rtx, NULL_RTX, false);
3422   RTL_CONST_CALL_P (insn) = 1;
3423   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
3424   insn = get_insns ();
3425 
3426   end_sequence ();
3427 
3428   return insn;
3429 }
3430 
3431 /* Return a pseudo register that contains the current thread pointer.  */
3432 
3433 rtx
mips_expand_thread_pointer(rtx tp)3434 mips_expand_thread_pointer (rtx tp)
3435 {
3436   rtx fn;
3437 
3438   if (TARGET_MIPS16)
3439     {
3440       if (!mips16_rdhwr_stub)
3441 	mips16_rdhwr_stub = new mips16_rdhwr_one_only_stub ();
3442       fn = mips16_stub_call_address (mips16_rdhwr_stub);
3443       emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
3444     }
3445   else
3446     emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
3447   return tp;
3448 }
3449 
3450 static rtx
mips_get_tp(void)3451 mips_get_tp (void)
3452 {
3453   return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
3454 }
3455 
3456 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
3457    its address.  The return value will be both a valid address and a valid
3458    SET_SRC (either a REG or a LO_SUM).  */
3459 
3460 static rtx
mips_legitimize_tls_address(rtx loc)3461 mips_legitimize_tls_address (rtx loc)
3462 {
3463   rtx dest, v0, tp, tmp1, tmp2, eqv, offset;
3464   enum tls_model model;
3465 
3466   model = SYMBOL_REF_TLS_MODEL (loc);
3467   /* Only TARGET_ABICALLS code can have more than one module; other
3468      code must be static and should not use a GOT.  All TLS models
3469      reduce to local exec in this situation.  */
3470   if (!TARGET_ABICALLS)
3471     model = TLS_MODEL_LOCAL_EXEC;
3472 
3473   switch (model)
3474     {
3475     case TLS_MODEL_GLOBAL_DYNAMIC:
3476       {
3477 	v0 = gen_rtx_REG (Pmode, GP_RETURN);
3478 	rtx_insn *insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
3479 	dest = gen_reg_rtx (Pmode);
3480 	emit_libcall_block (insn, dest, v0, loc);
3481 	break;
3482       }
3483 
3484     case TLS_MODEL_LOCAL_DYNAMIC:
3485       {
3486 	v0 = gen_rtx_REG (Pmode, GP_RETURN);
3487 	rtx_insn *insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
3488 	tmp1 = gen_reg_rtx (Pmode);
3489 
3490 	/* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3491 	   share the LDM result with other LD model accesses.  */
3492 	eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3493 			      UNSPEC_TLS_LDM);
3494 	emit_libcall_block (insn, tmp1, v0, eqv);
3495 
3496 	offset = mips_unspec_address (loc, SYMBOL_DTPREL);
3497 	if (mips_split_p[SYMBOL_DTPREL])
3498 	  {
3499 	    tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
3500 	    dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3501 	  }
3502 	else
3503 	  dest = expand_binop (Pmode, add_optab, tmp1, offset,
3504 			       0, 0, OPTAB_DIRECT);
3505 	break;
3506       }
3507 
3508     case TLS_MODEL_INITIAL_EXEC:
3509       tp = mips_get_tp ();
3510       tmp1 = gen_reg_rtx (Pmode);
3511       tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
3512       if (Pmode == DImode)
3513 	emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
3514       else
3515 	emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
3516       dest = gen_reg_rtx (Pmode);
3517       emit_insn (gen_add3_insn (dest, tmp1, tp));
3518       break;
3519 
3520     case TLS_MODEL_LOCAL_EXEC:
3521       tmp1 = mips_get_tp ();
3522       offset = mips_unspec_address (loc, SYMBOL_TPREL);
3523       if (mips_split_p[SYMBOL_TPREL])
3524 	{
3525 	  tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
3526 	  dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3527 	}
3528       else
3529 	dest = expand_binop (Pmode, add_optab, tmp1, offset,
3530 			     0, 0, OPTAB_DIRECT);
3531       break;
3532 
3533     default:
3534       gcc_unreachable ();
3535     }
3536   return dest;
3537 }
3538 
3539 /* Implement "TARGET = __builtin_mips_get_fcsr ()" for MIPS16,
3540    using a stub.  */
3541 
3542 void
mips16_expand_get_fcsr(rtx target)3543 mips16_expand_get_fcsr (rtx target)
3544 {
3545   if (!mips16_get_fcsr_stub)
3546     mips16_get_fcsr_stub = new mips16_get_fcsr_one_only_stub ();
3547   rtx fn = mips16_stub_call_address (mips16_get_fcsr_stub);
3548   emit_insn (PMODE_INSN (gen_mips_get_fcsr_mips16, (fn)));
3549   emit_move_insn (target, gen_rtx_REG (SImode, GET_FCSR_REGNUM));
3550 }
3551 
3552 /* Implement __builtin_mips_set_fcsr (TARGET) for MIPS16, using a stub.  */
3553 
3554 void
mips16_expand_set_fcsr(rtx newval)3555 mips16_expand_set_fcsr (rtx newval)
3556 {
3557   if (!mips16_set_fcsr_stub)
3558     mips16_set_fcsr_stub = new mips16_set_fcsr_one_only_stub ();
3559   rtx fn = mips16_stub_call_address (mips16_set_fcsr_stub);
3560   emit_move_insn (gen_rtx_REG (SImode, SET_FCSR_REGNUM), newval);
3561   emit_insn (PMODE_INSN (gen_mips_set_fcsr_mips16, (fn)));
3562 }
3563 
3564 /* If X is not a valid address for mode MODE, force it into a register.  */
3565 
3566 static rtx
mips_force_address(rtx x,machine_mode mode)3567 mips_force_address (rtx x, machine_mode mode)
3568 {
3569   if (!mips_legitimate_address_p (mode, x, false))
3570     x = force_reg (Pmode, x);
3571   return x;
3572 }
3573 
3574 /* This function is used to implement LEGITIMIZE_ADDRESS.  If X can
3575    be legitimized in a way that the generic machinery might not expect,
3576    return a new address, otherwise return NULL.  MODE is the mode of
3577    the memory being accessed.  */
3578 
3579 static rtx
mips_legitimize_address(rtx x,rtx oldx ATTRIBUTE_UNUSED,machine_mode mode)3580 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3581 			 machine_mode mode)
3582 {
3583   rtx base, addr;
3584   HOST_WIDE_INT offset;
3585 
3586   if (mips_tls_symbol_p (x))
3587     return mips_legitimize_tls_address (x);
3588 
3589   /* See if the address can split into a high part and a LO_SUM.  */
3590   if (mips_split_symbol (NULL, x, mode, &addr))
3591     return mips_force_address (addr, mode);
3592 
3593   /* Handle BASE + OFFSET using mips_add_offset.  */
3594   mips_split_plus (x, &base, &offset);
3595   if (offset != 0)
3596     {
3597       if (!mips_valid_base_register_p (base, mode, false))
3598 	base = copy_to_mode_reg (Pmode, base);
3599       addr = mips_add_offset (NULL, base, offset);
3600       return mips_force_address (addr, mode);
3601     }
3602 
3603   return x;
3604 }
3605 
3606 /* Load VALUE into DEST.  TEMP is as for mips_force_temporary.  */
3607 
3608 void
mips_move_integer(rtx temp,rtx dest,unsigned HOST_WIDE_INT value)3609 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3610 {
3611   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3612   machine_mode mode;
3613   unsigned int i, num_ops;
3614   rtx x;
3615 
3616   mode = GET_MODE (dest);
3617   num_ops = mips_build_integer (codes, value);
3618 
3619   /* Apply each binary operation to X.  Invariant: X is a legitimate
3620      source operand for a SET pattern.  */
3621   x = GEN_INT (codes[0].value);
3622   for (i = 1; i < num_ops; i++)
3623     {
3624       if (!can_create_pseudo_p ())
3625 	{
3626 	  emit_insn (gen_rtx_SET (temp, x));
3627 	  x = temp;
3628 	}
3629       else
3630 	x = force_reg (mode, x);
3631       x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3632     }
3633 
3634   emit_insn (gen_rtx_SET (dest, x));
3635 }
3636 
3637 /* Subroutine of mips_legitimize_move.  Move constant SRC into register
3638    DEST given that SRC satisfies immediate_operand but doesn't satisfy
3639    move_operand.  */
3640 
3641 static void
mips_legitimize_const_move(machine_mode mode,rtx dest,rtx src)3642 mips_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
3643 {
3644   rtx base, offset;
3645 
3646   /* Split moves of big integers into smaller pieces.  */
3647   if (splittable_const_int_operand (src, mode))
3648     {
3649       mips_move_integer (dest, dest, INTVAL (src));
3650       return;
3651     }
3652 
3653   /* Split moves of symbolic constants into high/low pairs.  */
3654   if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3655     {
3656       emit_insn (gen_rtx_SET (dest, src));
3657       return;
3658     }
3659 
3660   /* Generate the appropriate access sequences for TLS symbols.  */
3661   if (mips_tls_symbol_p (src))
3662     {
3663       mips_emit_move (dest, mips_legitimize_tls_address (src));
3664       return;
3665     }
3666 
3667   /* If we have (const (plus symbol offset)), and that expression cannot
3668      be forced into memory, load the symbol first and add in the offset.
3669      In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3670      forced into memory, as it usually produces better code.  */
3671   split_const (src, &base, &offset);
3672   if (offset != const0_rtx
3673       && (targetm.cannot_force_const_mem (mode, src)
3674 	  || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3675     {
3676       base = mips_force_temporary (dest, base);
3677       mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3678       return;
3679     }
3680 
3681   src = force_const_mem (mode, src);
3682 
3683   /* When using explicit relocs, constant pool references are sometimes
3684      not legitimate addresses.  */
3685   mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3686   mips_emit_move (dest, src);
3687 }
3688 
3689 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3690    sequence that is valid.  */
3691 
3692 bool
mips_legitimize_move(machine_mode mode,rtx dest,rtx src)3693 mips_legitimize_move (machine_mode mode, rtx dest, rtx src)
3694 {
3695   /* Both src and dest are non-registers;  one special case is supported where
3696      the source is (const_int 0) and the store can source the zero register.
3697      MIPS16 and MSA are never able to source the zero register directly in
3698      memory operations.  */
3699   if (!register_operand (dest, mode)
3700       && !register_operand (src, mode)
3701       && (TARGET_MIPS16 || !const_0_operand (src, mode)
3702 	  || MSA_SUPPORTED_MODE_P (mode)))
3703     {
3704       mips_emit_move (dest, force_reg (mode, src));
3705       return true;
3706     }
3707 
3708   /* We need to deal with constants that would be legitimate
3709      immediate_operands but aren't legitimate move_operands.  */
3710   if (CONSTANT_P (src) && !move_operand (src, mode))
3711     {
3712       mips_legitimize_const_move (mode, dest, src);
3713       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3714       return true;
3715     }
3716   return false;
3717 }
3718 
3719 /* Return true if value X in context CONTEXT is a small-data address
3720    that can be rewritten as a LO_SUM.  */
3721 
3722 static bool
mips_rewrite_small_data_p(rtx x,enum mips_symbol_context context)3723 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3724 {
3725   enum mips_symbol_type symbol_type;
3726 
3727   return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3728 	  && !mips_split_p[SYMBOL_GP_RELATIVE]
3729 	  && mips_symbolic_constant_p (x, context, &symbol_type)
3730 	  && symbol_type == SYMBOL_GP_RELATIVE);
3731 }
3732 
3733 /* Return true if OP refers to small data symbols directly, not through
3734    a LO_SUM.  CONTEXT is the context in which X appears.  */
3735 
3736 static int
mips_small_data_pattern_1(rtx x,enum mips_symbol_context context)3737 mips_small_data_pattern_1 (rtx x, enum mips_symbol_context context)
3738 {
3739   subrtx_var_iterator::array_type array;
3740   FOR_EACH_SUBRTX_VAR (iter, array, x, ALL)
3741     {
3742       rtx x = *iter;
3743 
3744       /* Ignore things like "g" constraints in asms.  We make no particular
3745 	 guarantee about which symbolic constants are acceptable as asm operands
3746 	 versus which must be forced into a GPR.  */
3747       if (GET_CODE (x) == LO_SUM || GET_CODE (x) == ASM_OPERANDS)
3748 	iter.skip_subrtxes ();
3749       else if (MEM_P (x))
3750 	{
3751 	  if (mips_small_data_pattern_1 (XEXP (x, 0), SYMBOL_CONTEXT_MEM))
3752 	    return true;
3753 	  iter.skip_subrtxes ();
3754 	}
3755       else if (mips_rewrite_small_data_p (x, context))
3756 	return true;
3757     }
3758   return false;
3759 }
3760 
3761 /* Return true if OP refers to small data symbols directly, not through
3762    a LO_SUM.  */
3763 
3764 bool
mips_small_data_pattern_p(rtx op)3765 mips_small_data_pattern_p (rtx op)
3766 {
3767   return mips_small_data_pattern_1 (op, SYMBOL_CONTEXT_LEA);
3768 }
3769 
3770 /* Rewrite *LOC so that it refers to small data using explicit
3771    relocations.  CONTEXT is the context in which *LOC appears.  */
3772 
3773 static void
mips_rewrite_small_data_1(rtx * loc,enum mips_symbol_context context)3774 mips_rewrite_small_data_1 (rtx *loc, enum mips_symbol_context context)
3775 {
3776   subrtx_ptr_iterator::array_type array;
3777   FOR_EACH_SUBRTX_PTR (iter, array, loc, ALL)
3778     {
3779       rtx *loc = *iter;
3780       if (MEM_P (*loc))
3781 	{
3782 	  mips_rewrite_small_data_1 (&XEXP (*loc, 0), SYMBOL_CONTEXT_MEM);
3783 	  iter.skip_subrtxes ();
3784 	}
3785       else if (mips_rewrite_small_data_p (*loc, context))
3786 	{
3787 	  *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3788 	  iter.skip_subrtxes ();
3789 	}
3790       else if (GET_CODE (*loc) == LO_SUM)
3791 	iter.skip_subrtxes ();
3792     }
3793 }
3794 
3795 /* Rewrite instruction pattern PATTERN so that it refers to small data
3796    using explicit relocations.  */
3797 
3798 rtx
mips_rewrite_small_data(rtx pattern)3799 mips_rewrite_small_data (rtx pattern)
3800 {
3801   pattern = copy_insn (pattern);
3802   mips_rewrite_small_data_1 (&pattern, SYMBOL_CONTEXT_LEA);
3803   return pattern;
3804 }
3805 
3806 /* The cost of loading values from the constant pool.  It should be
3807    larger than the cost of any constant we want to synthesize inline.  */
3808 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3809 
3810 /* Return the cost of X when used as an operand to the MIPS16 instruction
3811    that implements CODE.  Return -1 if there is no such instruction, or if
3812    X is not a valid immediate operand for it.  */
3813 
3814 static int
mips16_constant_cost(int code,HOST_WIDE_INT x)3815 mips16_constant_cost (int code, HOST_WIDE_INT x)
3816 {
3817   switch (code)
3818     {
3819     case ASHIFT:
3820     case ASHIFTRT:
3821     case LSHIFTRT:
3822       /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3823 	 other shifts are extended.  The shift patterns truncate the shift
3824 	 count to the right size, so there are no out-of-range values.  */
3825       if (IN_RANGE (x, 1, 8))
3826 	return 0;
3827       return COSTS_N_INSNS (1);
3828 
3829     case PLUS:
3830       if (IN_RANGE (x, -128, 127))
3831 	return 0;
3832       if (SMALL_OPERAND (x))
3833 	return COSTS_N_INSNS (1);
3834       return -1;
3835 
3836     case LEU:
3837       /* Like LE, but reject the always-true case.  */
3838       if (x == -1)
3839 	return -1;
3840       /* FALLTHRU */
3841     case LE:
3842       /* We add 1 to the immediate and use SLT.  */
3843       x += 1;
3844       /* FALLTHRU */
3845     case XOR:
3846       /* We can use CMPI for an xor with an unsigned 16-bit X.  */
3847     case LT:
3848     case LTU:
3849       if (IN_RANGE (x, 0, 255))
3850 	return 0;
3851       if (SMALL_OPERAND_UNSIGNED (x))
3852 	return COSTS_N_INSNS (1);
3853       return -1;
3854 
3855     case EQ:
3856     case NE:
3857       /* Equality comparisons with 0 are cheap.  */
3858       if (x == 0)
3859 	return 0;
3860       return -1;
3861 
3862     default:
3863       return -1;
3864     }
3865 }
3866 
3867 /* Return true if there is a non-MIPS16 instruction that implements CODE
3868    and if that instruction accepts X as an immediate operand.  */
3869 
3870 static int
mips_immediate_operand_p(int code,HOST_WIDE_INT x)3871 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3872 {
3873   switch (code)
3874     {
3875     case ASHIFT:
3876     case ASHIFTRT:
3877     case LSHIFTRT:
3878       /* All shift counts are truncated to a valid constant.  */
3879       return true;
3880 
3881     case ROTATE:
3882     case ROTATERT:
3883       /* Likewise rotates, if the target supports rotates at all.  */
3884       return ISA_HAS_ROR;
3885 
3886     case AND:
3887     case IOR:
3888     case XOR:
3889       /* These instructions take 16-bit unsigned immediates.  */
3890       return SMALL_OPERAND_UNSIGNED (x);
3891 
3892     case PLUS:
3893     case LT:
3894     case LTU:
3895       /* These instructions take 16-bit signed immediates.  */
3896       return SMALL_OPERAND (x);
3897 
3898     case EQ:
3899     case NE:
3900     case GT:
3901     case GTU:
3902       /* The "immediate" forms of these instructions are really
3903 	 implemented as comparisons with register 0.  */
3904       return x == 0;
3905 
3906     case GE:
3907     case GEU:
3908       /* Likewise, meaning that the only valid immediate operand is 1.  */
3909       return x == 1;
3910 
3911     case LE:
3912       /* We add 1 to the immediate and use SLT.  */
3913       return SMALL_OPERAND (x + 1);
3914 
3915     case LEU:
3916       /* Likewise SLTU, but reject the always-true case.  */
3917       return SMALL_OPERAND (x + 1) && x + 1 != 0;
3918 
3919     case SIGN_EXTRACT:
3920     case ZERO_EXTRACT:
3921       /* The bit position and size are immediate operands.  */
3922       return ISA_HAS_EXT_INS;
3923 
3924     default:
3925       /* By default assume that $0 can be used for 0.  */
3926       return x == 0;
3927     }
3928 }
3929 
3930 /* Return the cost of binary operation X, given that the instruction
3931    sequence for a word-sized or smaller operation has cost SINGLE_COST
3932    and that the sequence of a double-word operation has cost DOUBLE_COST.
3933    If SPEED is true, optimize for speed otherwise optimize for size.  */
3934 
3935 static int
mips_binary_cost(rtx x,int single_cost,int double_cost,bool speed)3936 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3937 {
3938   int cost;
3939 
3940   if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3941     cost = double_cost;
3942   else
3943     cost = single_cost;
3944   return (cost
3945 	  + set_src_cost (XEXP (x, 0), GET_MODE (x), speed)
3946 	  + rtx_cost (XEXP (x, 1), GET_MODE (x), GET_CODE (x), 1, speed));
3947 }
3948 
3949 /* Return the cost of floating-point multiplications of mode MODE.  */
3950 
3951 static int
mips_fp_mult_cost(machine_mode mode)3952 mips_fp_mult_cost (machine_mode mode)
3953 {
3954   return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3955 }
3956 
3957 /* Return the cost of floating-point divisions of mode MODE.  */
3958 
3959 static int
mips_fp_div_cost(machine_mode mode)3960 mips_fp_div_cost (machine_mode mode)
3961 {
3962   return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3963 }
3964 
3965 /* Return the cost of sign-extending OP to mode MODE, not including the
3966    cost of OP itself.  */
3967 
3968 static int
mips_sign_extend_cost(machine_mode mode,rtx op)3969 mips_sign_extend_cost (machine_mode mode, rtx op)
3970 {
3971   if (MEM_P (op))
3972     /* Extended loads are as cheap as unextended ones.  */
3973     return 0;
3974 
3975   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3976     /* A sign extension from SImode to DImode in 64-bit mode is free.  */
3977     return 0;
3978 
3979   if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3980     /* We can use SEB or SEH.  */
3981     return COSTS_N_INSNS (1);
3982 
3983   /* We need to use a shift left and a shift right.  */
3984   return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3985 }
3986 
3987 /* Return the cost of zero-extending OP to mode MODE, not including the
3988    cost of OP itself.  */
3989 
3990 static int
mips_zero_extend_cost(machine_mode mode,rtx op)3991 mips_zero_extend_cost (machine_mode mode, rtx op)
3992 {
3993   if (MEM_P (op))
3994     /* Extended loads are as cheap as unextended ones.  */
3995     return 0;
3996 
3997   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3998     /* We need a shift left by 32 bits and a shift right by 32 bits.  */
3999     return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
4000 
4001   if (GENERATE_MIPS16E)
4002     /* We can use ZEB or ZEH.  */
4003     return COSTS_N_INSNS (1);
4004 
4005   if (TARGET_MIPS16)
4006     /* We need to load 0xff or 0xffff into a register and use AND.  */
4007     return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
4008 
4009   /* We can use ANDI.  */
4010   return COSTS_N_INSNS (1);
4011 }
4012 
4013 /* Return the cost of moving between two registers of mode MODE,
4014    assuming that the move will be in pieces of at most UNITS bytes.  */
4015 
4016 static int
mips_set_reg_reg_piece_cost(machine_mode mode,unsigned int units)4017 mips_set_reg_reg_piece_cost (machine_mode mode, unsigned int units)
4018 {
4019   return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
4020 }
4021 
4022 /* Return the cost of moving between two registers of mode MODE.  */
4023 
4024 static int
mips_set_reg_reg_cost(machine_mode mode)4025 mips_set_reg_reg_cost (machine_mode mode)
4026 {
4027   switch (GET_MODE_CLASS (mode))
4028     {
4029     case MODE_CC:
4030       return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
4031 
4032     case MODE_FLOAT:
4033     case MODE_COMPLEX_FLOAT:
4034     case MODE_VECTOR_FLOAT:
4035       if (TARGET_HARD_FLOAT)
4036 	return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
4037       /* Fall through */
4038 
4039     default:
4040       return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
4041     }
4042 }
4043 
4044 /* Implement TARGET_RTX_COSTS.  */
4045 
4046 static bool
mips_rtx_costs(rtx x,machine_mode mode,int outer_code,int opno ATTRIBUTE_UNUSED,int * total,bool speed)4047 mips_rtx_costs (rtx x, machine_mode mode, int outer_code,
4048 		int opno ATTRIBUTE_UNUSED, int *total, bool speed)
4049 {
4050   int code = GET_CODE (x);
4051   bool float_mode_p = FLOAT_MODE_P (mode);
4052   int cost;
4053   rtx addr;
4054 
4055   /* The cost of a COMPARE is hard to define for MIPS.  COMPAREs don't
4056      appear in the instruction stream, and the cost of a comparison is
4057      really the cost of the branch or scc condition.  At the time of
4058      writing, GCC only uses an explicit outer COMPARE code when optabs
4059      is testing whether a constant is expensive enough to force into a
4060      register.  We want optabs to pass such constants through the MIPS
4061      expanders instead, so make all constants very cheap here.  */
4062   if (outer_code == COMPARE)
4063     {
4064       gcc_assert (CONSTANT_P (x));
4065       *total = 0;
4066       return true;
4067     }
4068 
4069   switch (code)
4070     {
4071     case CONST_INT:
4072       /* Treat *clear_upper32-style ANDs as having zero cost in the
4073 	 second operand.  The cost is entirely in the first operand.
4074 
4075 	 ??? This is needed because we would otherwise try to CSE
4076 	 the constant operand.  Although that's the right thing for
4077 	 instructions that continue to be a register operation throughout
4078 	 compilation, it is disastrous for instructions that could
4079 	 later be converted into a memory operation.  */
4080       if (TARGET_64BIT
4081 	  && outer_code == AND
4082 	  && UINTVAL (x) == 0xffffffff)
4083 	{
4084 	  *total = 0;
4085 	  return true;
4086 	}
4087 
4088       if (TARGET_MIPS16)
4089 	{
4090 	  cost = mips16_constant_cost (outer_code, INTVAL (x));
4091 	  if (cost >= 0)
4092 	    {
4093 	      *total = cost;
4094 	      return true;
4095 	    }
4096 	}
4097       else
4098 	{
4099 	  /* When not optimizing for size, we care more about the cost
4100 	     of hot code, and hot code is often in a loop.  If a constant
4101 	     operand needs to be forced into a register, we will often be
4102 	     able to hoist the constant load out of the loop, so the load
4103 	     should not contribute to the cost.  */
4104 	  if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
4105 	    {
4106 	      *total = 0;
4107 	      return true;
4108 	    }
4109 	}
4110       /* Fall through.  */
4111 
4112     case CONST:
4113     case SYMBOL_REF:
4114     case LABEL_REF:
4115     case CONST_DOUBLE:
4116       if (force_to_mem_operand (x, VOIDmode))
4117 	{
4118 	  *total = COSTS_N_INSNS (1);
4119 	  return true;
4120 	}
4121       cost = mips_const_insns (x);
4122       if (cost > 0)
4123 	{
4124 	  /* If the constant is likely to be stored in a GPR, SETs of
4125 	     single-insn constants are as cheap as register sets; we
4126 	     never want to CSE them.
4127 
4128 	     Don't reduce the cost of storing a floating-point zero in
4129 	     FPRs.  If we have a zero in an FPR for other reasons, we
4130 	     can get better cfg-cleanup and delayed-branch results by
4131 	     using it consistently, rather than using $0 sometimes and
4132 	     an FPR at other times.  Also, moves between floating-point
4133 	     registers are sometimes cheaper than (D)MTC1 $0.  */
4134 	  if (cost == 1
4135 	      && outer_code == SET
4136 	      && !(float_mode_p && TARGET_HARD_FLOAT))
4137 	    cost = 0;
4138 	  /* When non-MIPS16 code loads a constant N>1 times, we rarely
4139 	     want to CSE the constant itself.  It is usually better to
4140 	     have N copies of the last operation in the sequence and one
4141 	     shared copy of the other operations.  (Note that this is
4142 	     not true for MIPS16 code, where the final operation in the
4143 	     sequence is often an extended instruction.)
4144 
4145 	     Also, if we have a CONST_INT, we don't know whether it is
4146 	     for a word or doubleword operation, so we cannot rely on
4147 	     the result of mips_build_integer.  */
4148 	  else if (!TARGET_MIPS16
4149 		   && (outer_code == SET || GET_MODE (x) == VOIDmode))
4150 	    cost = 1;
4151 	  *total = COSTS_N_INSNS (cost);
4152 	  return true;
4153 	}
4154       /* The value will need to be fetched from the constant pool.  */
4155       *total = CONSTANT_POOL_COST;
4156       return true;
4157 
4158     case MEM:
4159       /* If the address is legitimate, return the number of
4160 	 instructions it needs.  */
4161       addr = XEXP (x, 0);
4162       cost = mips_address_insns (addr, mode, true);
4163       if (cost > 0)
4164 	{
4165 	  *total = COSTS_N_INSNS (cost + 1);
4166 	  return true;
4167 	}
4168       /* Check for a scaled indexed address.  */
4169       if (mips_lwxs_address_p (addr)
4170 	  || mips_lx_address_p (addr, mode))
4171 	{
4172 	  *total = COSTS_N_INSNS (2);
4173 	  return true;
4174 	}
4175       /* Otherwise use the default handling.  */
4176       return false;
4177 
4178     case FFS:
4179       *total = COSTS_N_INSNS (6);
4180       return false;
4181 
4182     case NOT:
4183       *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
4184       return false;
4185 
4186     case AND:
4187       /* Check for a *clear_upper32 pattern and treat it like a zero
4188 	 extension.  See the pattern's comment for details.  */
4189       if (TARGET_64BIT
4190 	  && mode == DImode
4191 	  && CONST_INT_P (XEXP (x, 1))
4192 	  && UINTVAL (XEXP (x, 1)) == 0xffffffff)
4193 	{
4194 	  *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
4195 		    + set_src_cost (XEXP (x, 0), mode, speed));
4196 	  return true;
4197 	}
4198       if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
4199 	{
4200 	  rtx op = XEXP (x, 0);
4201 	  if (GET_CODE (op) == ASHIFT
4202 	      && CONST_INT_P (XEXP (op, 1))
4203 	      && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
4204 	    {
4205 	      *total = COSTS_N_INSNS (1);
4206 	      *total += set_src_cost (XEXP (op, 0), mode, speed);
4207 	      return true;
4208 	    }
4209 	}
4210       /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
4211 	 a single instruction.  */
4212       if (!TARGET_MIPS16
4213 	  && GET_CODE (XEXP (x, 0)) == NOT
4214 	  && GET_CODE (XEXP (x, 1)) == NOT)
4215 	{
4216 	  cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
4217           *total = (COSTS_N_INSNS (cost)
4218 		    + set_src_cost (XEXP (XEXP (x, 0), 0), mode, speed)
4219 		    + set_src_cost (XEXP (XEXP (x, 1), 0), mode, speed));
4220 	  return true;
4221 	}
4222 
4223       /* Fall through.  */
4224 
4225     case IOR:
4226     case XOR:
4227       /* Double-word operations use two single-word operations.  */
4228       *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
4229 				 speed);
4230       return true;
4231 
4232     case ASHIFT:
4233     case ASHIFTRT:
4234     case LSHIFTRT:
4235     case ROTATE:
4236     case ROTATERT:
4237       if (CONSTANT_P (XEXP (x, 1)))
4238 	*total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
4239 				   speed);
4240       else
4241 	*total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
4242 				   speed);
4243       return true;
4244 
4245     case ABS:
4246       if (float_mode_p)
4247         *total = mips_cost->fp_add;
4248       else
4249         *total = COSTS_N_INSNS (4);
4250       return false;
4251 
4252     case LO_SUM:
4253       /* Low-part immediates need an extended MIPS16 instruction.  */
4254       *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
4255 		+ set_src_cost (XEXP (x, 0), mode, speed));
4256       return true;
4257 
4258     case LT:
4259     case LTU:
4260     case LE:
4261     case LEU:
4262     case GT:
4263     case GTU:
4264     case GE:
4265     case GEU:
4266     case EQ:
4267     case NE:
4268     case UNORDERED:
4269     case LTGT:
4270     case UNGE:
4271     case UNGT:
4272     case UNLE:
4273     case UNLT:
4274       /* Branch comparisons have VOIDmode, so use the first operand's
4275 	 mode instead.  */
4276       mode = GET_MODE (XEXP (x, 0));
4277       if (FLOAT_MODE_P (mode))
4278 	{
4279 	  *total = mips_cost->fp_add;
4280 	  return false;
4281 	}
4282       *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
4283 				 speed);
4284       return true;
4285 
4286     case MINUS:
4287       if (float_mode_p && ISA_HAS_UNFUSED_MADD4 && !HONOR_SIGNED_ZEROS (mode))
4288 	{
4289 	  /* See if we can use NMADD or NMSUB via the *nmadd4<mode>_fastmath
4290 	     or *nmsub4<mode>_fastmath patterns.  These patterns check for
4291 	     HONOR_SIGNED_ZEROS so we check here too.  */
4292 	  rtx op0 = XEXP (x, 0);
4293 	  rtx op1 = XEXP (x, 1);
4294 	  if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
4295 	    {
4296 	      *total = (mips_fp_mult_cost (mode)
4297 			+ set_src_cost (XEXP (XEXP (op0, 0), 0), mode, speed)
4298 			+ set_src_cost (XEXP (op0, 1), mode, speed)
4299 			+ set_src_cost (op1, mode, speed));
4300 	      return true;
4301 	    }
4302 	  if (GET_CODE (op1) == MULT)
4303 	    {
4304 	      *total = (mips_fp_mult_cost (mode)
4305 			+ set_src_cost (op0, mode, speed)
4306 			+ set_src_cost (XEXP (op1, 0), mode, speed)
4307 			+ set_src_cost (XEXP (op1, 1), mode, speed));
4308 	      return true;
4309 	    }
4310 	}
4311       /* Fall through.  */
4312 
4313     case PLUS:
4314       if (float_mode_p)
4315 	{
4316 	  /* If this is part of a MADD or MSUB, treat the PLUS as
4317 	     being free.  */
4318 	  if (ISA_HAS_UNFUSED_MADD4 && GET_CODE (XEXP (x, 0)) == MULT)
4319 	    *total = 0;
4320 	  else
4321 	    *total = mips_cost->fp_add;
4322 	  return false;
4323 	}
4324 
4325       /* If it's an add + mult (which is equivalent to shift left) and
4326          it's immediate operand satisfies const_immlsa_operand predicate.  */
4327       if (((ISA_HAS_LSA && mode == SImode)
4328 	   || (ISA_HAS_DLSA && mode == DImode))
4329 	  && GET_CODE (XEXP (x, 0)) == MULT)
4330 	{
4331 	  rtx op2 = XEXP (XEXP (x, 0), 1);
4332 	  if (const_immlsa_operand (op2, mode))
4333 	    {
4334 	      *total = (COSTS_N_INSNS (1)
4335 			+ set_src_cost (XEXP (XEXP (x, 0), 0), mode, speed)
4336 			+ set_src_cost (XEXP (x, 1), mode, speed));
4337 	      return true;
4338 	    }
4339 	}
4340 
4341       /* Double-word operations require three single-word operations and
4342 	 an SLTU.  The MIPS16 version then needs to move the result of
4343 	 the SLTU from $24 to a MIPS16 register.  */
4344       *total = mips_binary_cost (x, COSTS_N_INSNS (1),
4345 				 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
4346 				 speed);
4347       return true;
4348 
4349     case NEG:
4350       if (float_mode_p && ISA_HAS_UNFUSED_MADD4)
4351 	{
4352 	  /* See if we can use NMADD or NMSUB via the *nmadd4<mode> or
4353 	     *nmsub4<mode> patterns.  */
4354 	  rtx op = XEXP (x, 0);
4355 	  if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
4356 	      && GET_CODE (XEXP (op, 0)) == MULT)
4357 	    {
4358 	      *total = (mips_fp_mult_cost (mode)
4359 			+ set_src_cost (XEXP (XEXP (op, 0), 0), mode, speed)
4360 			+ set_src_cost (XEXP (XEXP (op, 0), 1), mode, speed)
4361 			+ set_src_cost (XEXP (op, 1), mode, speed));
4362 	      return true;
4363 	    }
4364 	}
4365 
4366       if (float_mode_p)
4367 	*total = mips_cost->fp_add;
4368       else
4369 	*total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
4370       return false;
4371 
4372     case FMA:
4373       *total = mips_fp_mult_cost (mode);
4374       return false;
4375 
4376     case MULT:
4377       if (float_mode_p)
4378 	*total = mips_fp_mult_cost (mode);
4379       else if (mode == DImode && !TARGET_64BIT)
4380 	/* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
4381 	   where the mulsidi3 always includes an MFHI and an MFLO.  */
4382 	*total = (speed
4383 		  ? mips_cost->int_mult_si * 3 + 6
4384 		  : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
4385       else if (!speed)
4386 	*total = COSTS_N_INSNS ((ISA_HAS_MUL3 || ISA_HAS_R6MUL) ? 1 : 2) + 1;
4387       else if (mode == DImode)
4388 	*total = mips_cost->int_mult_di;
4389       else
4390 	*total = mips_cost->int_mult_si;
4391       return false;
4392 
4393     case DIV:
4394       /* Check for a reciprocal.  */
4395       if (float_mode_p
4396 	  && ISA_HAS_FP_RECIP_RSQRT (mode)
4397 	  && flag_unsafe_math_optimizations
4398 	  && XEXP (x, 0) == CONST1_RTX (mode))
4399 	{
4400 	  if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
4401 	    /* An rsqrt<mode>a or rsqrt<mode>b pattern.  Count the
4402 	       division as being free.  */
4403 	    *total = set_src_cost (XEXP (x, 1), mode, speed);
4404 	  else
4405 	    *total = (mips_fp_div_cost (mode)
4406 		      + set_src_cost (XEXP (x, 1), mode, speed));
4407 	  return true;
4408 	}
4409       /* Fall through.  */
4410 
4411     case SQRT:
4412     case MOD:
4413       if (float_mode_p)
4414 	{
4415 	  *total = mips_fp_div_cost (mode);
4416 	  return false;
4417 	}
4418       /* Fall through.  */
4419 
4420     case UDIV:
4421     case UMOD:
4422       if (!speed)
4423 	{
4424 	  /* It is our responsibility to make division by a power of 2
4425 	     as cheap as 2 register additions if we want the division
4426 	     expanders to be used for such operations; see the setting
4427 	     of sdiv_pow2_cheap in optabs.c.  Using (D)DIV for MIPS16
4428 	     should always produce shorter code than using
4429 	     expand_sdiv2_pow2.  */
4430 	  if (TARGET_MIPS16
4431 	      && CONST_INT_P (XEXP (x, 1))
4432 	      && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
4433 	    {
4434 	      *total = COSTS_N_INSNS (2);
4435 	      *total += set_src_cost (XEXP (x, 0), mode, speed);
4436 	      return true;
4437 	    }
4438 	  *total = COSTS_N_INSNS (mips_idiv_insns (mode));
4439 	}
4440       else if (mode == DImode)
4441         *total = mips_cost->int_div_di;
4442       else
4443 	*total = mips_cost->int_div_si;
4444       return false;
4445 
4446     case SIGN_EXTEND:
4447       *total = mips_sign_extend_cost (mode, XEXP (x, 0));
4448       return false;
4449 
4450     case ZERO_EXTEND:
4451       if (outer_code == SET
4452 	  && ISA_HAS_BADDU
4453 	  && (GET_CODE (XEXP (x, 0)) == TRUNCATE
4454 	      || GET_CODE (XEXP (x, 0)) == SUBREG)
4455 	  && GET_MODE (XEXP (x, 0)) == QImode
4456 	  && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
4457 	{
4458 	  *total = set_src_cost (XEXP (XEXP (x, 0), 0), VOIDmode, speed);
4459 	  return true;
4460 	}
4461       *total = mips_zero_extend_cost (mode, XEXP (x, 0));
4462       return false;
4463     case TRUNCATE:
4464       /* Costings for highpart multiplies.  Matching patterns of the form:
4465 
4466 	 (lshiftrt:DI (mult:DI (sign_extend:DI (...)
4467 			       (sign_extend:DI (...))
4468 		      (const_int 32)
4469       */
4470       if (ISA_HAS_R6MUL
4471 	  && (GET_CODE (XEXP (x, 0)) == ASHIFTRT
4472 	      || GET_CODE (XEXP (x, 0)) == LSHIFTRT)
4473 	  && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4474 	  && ((INTVAL (XEXP (XEXP (x, 0), 1)) == 32
4475 	       && GET_MODE (XEXP (x, 0)) == DImode)
4476 	      || (ISA_HAS_R6DMUL
4477 		  && INTVAL (XEXP (XEXP (x, 0), 1)) == 64
4478 		  && GET_MODE (XEXP (x, 0)) == TImode))
4479 	  && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4480 	  && ((GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND
4481 	       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SIGN_EXTEND)
4482 	      || (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4483 		  && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1))
4484 		      == ZERO_EXTEND))))
4485 	{
4486 	  if (!speed)
4487 	    *total = COSTS_N_INSNS (1) + 1;
4488 	  else if (mode == DImode)
4489 	    *total = mips_cost->int_mult_di;
4490 	  else
4491 	    *total = mips_cost->int_mult_si;
4492 
4493 	  /* Sign extension is free, zero extension costs for DImode when
4494 	     on a 64bit core / when DMUL is present.  */
4495 	  for (int i = 0; i < 2; ++i)
4496 	    {
4497 	      rtx op = XEXP (XEXP (XEXP (x, 0), 0), i);
4498 	      if (ISA_HAS_R6DMUL
4499 		  && GET_CODE (op) == ZERO_EXTEND
4500 		  && GET_MODE (op) == DImode)
4501 		*total += rtx_cost (op, DImode, MULT, i, speed);
4502 	      else
4503 		*total += rtx_cost (XEXP (op, 0), VOIDmode, GET_CODE (op),
4504 				    0, speed);
4505 	    }
4506 
4507 	  return true;
4508 	}
4509       return false;
4510 
4511     case FLOAT:
4512     case UNSIGNED_FLOAT:
4513     case FIX:
4514     case FLOAT_EXTEND:
4515     case FLOAT_TRUNCATE:
4516       *total = mips_cost->fp_add;
4517       return false;
4518 
4519     case SET:
4520       if (register_operand (SET_DEST (x), VOIDmode)
4521 	  && reg_or_0_operand (SET_SRC (x), VOIDmode))
4522 	{
4523 	  *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4524 	  return true;
4525 	}
4526       return false;
4527 
4528     default:
4529       return false;
4530     }
4531 }
4532 
4533 /* Implement TARGET_ADDRESS_COST.  */
4534 
4535 static int
mips_address_cost(rtx addr,machine_mode mode,addr_space_t as ATTRIBUTE_UNUSED,bool speed ATTRIBUTE_UNUSED)4536 mips_address_cost (rtx addr, machine_mode mode,
4537 		   addr_space_t as ATTRIBUTE_UNUSED,
4538 		   bool speed ATTRIBUTE_UNUSED)
4539 {
4540   return mips_address_insns (addr, mode, false);
4541 }
4542 
4543 /* Implement TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P.  */
4544 
4545 static bool
mips_no_speculation_in_delay_slots_p()4546 mips_no_speculation_in_delay_slots_p ()
4547 {
4548   return TARGET_CB_MAYBE;
4549 }
4550 
4551 /* Information about a single instruction in a multi-instruction
4552    asm sequence.  */
4553 struct mips_multi_member {
4554   /* True if this is a label, false if it is code.  */
4555   bool is_label_p;
4556 
4557   /* The output_asm_insn format of the instruction.  */
4558   const char *format;
4559 
4560   /* The operands to the instruction.  */
4561   rtx operands[MAX_RECOG_OPERANDS];
4562 };
4563 typedef struct mips_multi_member mips_multi_member;
4564 
4565 /* The instructions that make up the current multi-insn sequence.  */
4566 static vec<mips_multi_member> mips_multi_members;
4567 
4568 /* How many instructions (as opposed to labels) are in the current
4569    multi-insn sequence.  */
4570 static unsigned int mips_multi_num_insns;
4571 
4572 /* Start a new multi-insn sequence.  */
4573 
4574 static void
mips_multi_start(void)4575 mips_multi_start (void)
4576 {
4577   mips_multi_members.truncate (0);
4578   mips_multi_num_insns = 0;
4579 }
4580 
4581 /* Add a new, zero initialized member to the current multi-insn sequence.  */
4582 
4583 static struct mips_multi_member *
mips_multi_add(void)4584 mips_multi_add (void)
4585 {
4586   mips_multi_member empty;
4587   memset (&empty, 0, sizeof (empty));
4588   return mips_multi_members.safe_push (empty);
4589 }
4590 
4591 /* Add a normal insn with the given asm format to the current multi-insn
4592    sequence.  The other arguments are a null-terminated list of operands.  */
4593 
4594 static void
mips_multi_add_insn(const char * format,...)4595 mips_multi_add_insn (const char *format, ...)
4596 {
4597   struct mips_multi_member *member;
4598   va_list ap;
4599   unsigned int i;
4600   rtx op;
4601 
4602   member = mips_multi_add ();
4603   member->is_label_p = false;
4604   member->format = format;
4605   va_start (ap, format);
4606   i = 0;
4607   while ((op = va_arg (ap, rtx)))
4608     member->operands[i++] = op;
4609   va_end (ap);
4610   mips_multi_num_insns++;
4611 }
4612 
4613 /* Add the given label definition to the current multi-insn sequence.
4614    The definition should include the colon.  */
4615 
4616 static void
mips_multi_add_label(const char * label)4617 mips_multi_add_label (const char *label)
4618 {
4619   struct mips_multi_member *member;
4620 
4621   member = mips_multi_add ();
4622   member->is_label_p = true;
4623   member->format = label;
4624 }
4625 
4626 /* Return the index of the last member of the current multi-insn sequence.  */
4627 
4628 static unsigned int
mips_multi_last_index(void)4629 mips_multi_last_index (void)
4630 {
4631   return mips_multi_members.length () - 1;
4632 }
4633 
4634 /* Add a copy of an existing instruction to the current multi-insn
4635    sequence.  I is the index of the instruction that should be copied.  */
4636 
4637 static void
mips_multi_copy_insn(unsigned int i)4638 mips_multi_copy_insn (unsigned int i)
4639 {
4640   struct mips_multi_member *member;
4641 
4642   member = mips_multi_add ();
4643   memcpy (member, &mips_multi_members[i], sizeof (*member));
4644   gcc_assert (!member->is_label_p);
4645 }
4646 
4647 /* Change the operand of an existing instruction in the current
4648    multi-insn sequence.  I is the index of the instruction,
4649    OP is the index of the operand, and X is the new value.  */
4650 
4651 static void
mips_multi_set_operand(unsigned int i,unsigned int op,rtx x)4652 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4653 {
4654   mips_multi_members[i].operands[op] = x;
4655 }
4656 
4657 /* Write out the asm code for the current multi-insn sequence.  */
4658 
4659 static void
mips_multi_write(void)4660 mips_multi_write (void)
4661 {
4662   struct mips_multi_member *member;
4663   unsigned int i;
4664 
4665   FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4666     if (member->is_label_p)
4667       fprintf (asm_out_file, "%s\n", member->format);
4668     else
4669       output_asm_insn (member->format, member->operands);
4670 }
4671 
4672 /* Return one word of double-word value OP, taking into account the fixed
4673    endianness of certain registers.  HIGH_P is true to select the high part,
4674    false to select the low part.  */
4675 
4676 rtx
mips_subword(rtx op,bool high_p)4677 mips_subword (rtx op, bool high_p)
4678 {
4679   unsigned int byte, offset;
4680   machine_mode mode;
4681 
4682   mode = GET_MODE (op);
4683   if (mode == VOIDmode)
4684     mode = TARGET_64BIT ? TImode : DImode;
4685 
4686   if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4687     byte = UNITS_PER_WORD;
4688   else
4689     byte = 0;
4690 
4691   if (FP_REG_RTX_P (op))
4692     {
4693       /* Paired FPRs are always ordered little-endian.  */
4694       offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4695       return gen_rtx_REG (word_mode, REGNO (op) + offset);
4696     }
4697 
4698   if (MEM_P (op))
4699     return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4700 
4701   return simplify_gen_subreg (word_mode, op, mode, byte);
4702 }
4703 
4704 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4705    SPLIT_TYPE is the condition under which moves should be split.  */
4706 
4707 static bool
mips_mult_move_p(rtx dest,rtx src,enum mips_split_type split_type)4708 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4709 {
4710   return ((split_type != SPLIT_FOR_SPEED
4711 	   || mips_tuning_info.fast_mult_zero_zero_p)
4712 	  && src == const0_rtx
4713 	  && REG_P (dest)
4714 	  && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4715 	  && (ISA_HAS_DSP_MULT
4716 	      ? ACC_REG_P (REGNO (dest))
4717 	      : MD_REG_P (REGNO (dest))));
4718 }
4719 
4720 /* Return true if a move from SRC to DEST should be split into two.
4721    SPLIT_TYPE describes the split condition.  */
4722 
4723 bool
mips_split_move_p(rtx dest,rtx src,enum mips_split_type split_type)4724 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4725 {
4726   /* Check whether the move can be done using some variant of MULT $0,$0.  */
4727   if (mips_mult_move_p (dest, src, split_type))
4728     return false;
4729 
4730   /* FPR-to-FPR moves can be done in a single instruction, if they're
4731      allowed at all.  */
4732   unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4733   if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4734     return false;
4735 
4736   /* Check for floating-point loads and stores.  */
4737   if (size == 8 && ISA_HAS_LDC1_SDC1)
4738     {
4739       if (FP_REG_RTX_P (dest) && MEM_P (src))
4740 	return false;
4741       if (FP_REG_RTX_P (src) && MEM_P (dest))
4742 	return false;
4743     }
4744 
4745   /* Check if MSA moves need splitting.  */
4746   if (MSA_SUPPORTED_MODE_P (GET_MODE (dest)))
4747     return mips_split_128bit_move_p (dest, src);
4748 
4749   /* Otherwise split all multiword moves.  */
4750   return size > UNITS_PER_WORD;
4751 }
4752 
4753 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
4754    SPLIT_TYPE describes the split condition.  */
4755 
4756 void
mips_split_move(rtx dest,rtx src,enum mips_split_type split_type)4757 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type)
4758 {
4759   rtx low_dest;
4760 
4761   gcc_checking_assert (mips_split_move_p (dest, src, split_type));
4762   if (MSA_SUPPORTED_MODE_P (GET_MODE (dest)))
4763     mips_split_128bit_move (dest, src);
4764   else if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4765     {
4766       if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4767 	emit_insn (gen_move_doubleword_fprdi (dest, src));
4768       else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4769 	emit_insn (gen_move_doubleword_fprdf (dest, src));
4770       else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4771 	emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4772       else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4773 	emit_insn (gen_move_doubleword_fprv2si (dest, src));
4774       else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4775 	emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4776       else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4777 	emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4778       else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4779 	emit_insn (gen_move_doubleword_fprtf (dest, src));
4780       else
4781 	gcc_unreachable ();
4782     }
4783   else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4784     {
4785       low_dest = mips_subword (dest, false);
4786       mips_emit_move (low_dest, mips_subword (src, false));
4787       if (TARGET_64BIT)
4788 	emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4789       else
4790 	emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4791     }
4792   else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4793     {
4794       mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4795       if (TARGET_64BIT)
4796 	emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4797       else
4798 	emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4799     }
4800   else
4801     {
4802       /* The operation can be split into two normal moves.  Decide in
4803 	 which order to do them.  */
4804       low_dest = mips_subword (dest, false);
4805       if (REG_P (low_dest)
4806 	  && reg_overlap_mentioned_p (low_dest, src))
4807 	{
4808 	  mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4809 	  mips_emit_move (low_dest, mips_subword (src, false));
4810 	}
4811       else
4812 	{
4813 	  mips_emit_move (low_dest, mips_subword (src, false));
4814 	  mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4815 	}
4816     }
4817 }
4818 
4819 /* Return the split type for instruction INSN.  */
4820 
4821 static enum mips_split_type
mips_insn_split_type(rtx insn)4822 mips_insn_split_type (rtx insn)
4823 {
4824   basic_block bb = BLOCK_FOR_INSN (insn);
4825   if (bb)
4826     {
4827       if (optimize_bb_for_speed_p (bb))
4828 	return SPLIT_FOR_SPEED;
4829       else
4830 	return SPLIT_FOR_SIZE;
4831     }
4832   /* Once CFG information has been removed, we should trust the optimization
4833      decisions made by previous passes and only split where necessary.  */
4834   return SPLIT_IF_NECESSARY;
4835 }
4836 
4837 /* Return true if a 128-bit move from SRC to DEST should be split.  */
4838 
4839 bool
mips_split_128bit_move_p(rtx dest,rtx src)4840 mips_split_128bit_move_p (rtx dest, rtx src)
4841 {
4842   /* MSA-to-MSA moves can be done in a single instruction.  */
4843   if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4844     return false;
4845 
4846   /* Check for MSA loads and stores.  */
4847   if (FP_REG_RTX_P (dest) && MEM_P (src))
4848     return false;
4849   if (FP_REG_RTX_P (src) && MEM_P (dest))
4850     return false;
4851 
4852   /* Check for MSA set to an immediate const vector with valid replicated
4853      element.  */
4854   if (FP_REG_RTX_P (dest)
4855       && mips_const_vector_same_int_p (src, GET_MODE (src), -512, 511))
4856     return false;
4857 
4858   /* Check for MSA load zero immediate.  */
4859   if (FP_REG_RTX_P (dest) && src == CONST0_RTX (GET_MODE (src)))
4860     return false;
4861 
4862   return true;
4863 }
4864 
4865 /* Split a 128-bit move from SRC to DEST.  */
4866 
4867 void
mips_split_128bit_move(rtx dest,rtx src)4868 mips_split_128bit_move (rtx dest, rtx src)
4869 {
4870   int byte, index;
4871   rtx low_dest, low_src, d, s;
4872 
4873   if (FP_REG_RTX_P (dest))
4874     {
4875       gcc_assert (!MEM_P (src));
4876 
4877       rtx new_dest = dest;
4878       if (!TARGET_64BIT)
4879 	{
4880 	  if (GET_MODE (dest) != V4SImode)
4881 	    new_dest = simplify_gen_subreg (V4SImode, dest, GET_MODE (dest), 0);
4882 	}
4883       else
4884 	{
4885 	  if (GET_MODE (dest) != V2DImode)
4886 	    new_dest = simplify_gen_subreg (V2DImode, dest, GET_MODE (dest), 0);
4887 	}
4888 
4889       for (byte = 0, index = 0; byte < GET_MODE_SIZE (TImode);
4890 	   byte += UNITS_PER_WORD, index++)
4891 	{
4892 	  s = mips_subword_at_byte (src, byte);
4893 	  if (!TARGET_64BIT)
4894 	    emit_insn (gen_msa_insert_w (new_dest, s, new_dest,
4895 					 GEN_INT (1 << index)));
4896 	  else
4897 	    emit_insn (gen_msa_insert_d (new_dest, s, new_dest,
4898 					 GEN_INT (1 << index)));
4899 	}
4900     }
4901   else if (FP_REG_RTX_P (src))
4902     {
4903       gcc_assert (!MEM_P (dest));
4904 
4905       rtx new_src = src;
4906       if (!TARGET_64BIT)
4907 	{
4908 	  if (GET_MODE (src) != V4SImode)
4909 	    new_src = simplify_gen_subreg (V4SImode, src, GET_MODE (src), 0);
4910 	}
4911       else
4912 	{
4913 	  if (GET_MODE (src) != V2DImode)
4914 	    new_src = simplify_gen_subreg (V2DImode, src, GET_MODE (src), 0);
4915 	}
4916 
4917       for (byte = 0, index = 0; byte < GET_MODE_SIZE (TImode);
4918 	   byte += UNITS_PER_WORD, index++)
4919 	{
4920 	  d = mips_subword_at_byte (dest, byte);
4921 	  if (!TARGET_64BIT)
4922 	    emit_insn (gen_msa_copy_s_w (d, new_src, GEN_INT (index)));
4923 	  else
4924 	    emit_insn (gen_msa_copy_s_d (d, new_src, GEN_INT (index)));
4925 	}
4926     }
4927   else
4928     {
4929       low_dest = mips_subword_at_byte (dest, 0);
4930       low_src = mips_subword_at_byte (src, 0);
4931       gcc_assert (REG_P (low_dest) && REG_P (low_src));
4932       /* Make sure the source register is not written before reading.  */
4933       if (REGNO (low_dest) <= REGNO (low_src))
4934 	{
4935 	  for (byte = 0; byte < GET_MODE_SIZE (TImode);
4936 	       byte += UNITS_PER_WORD)
4937 	    {
4938 	      d = mips_subword_at_byte (dest, byte);
4939 	      s = mips_subword_at_byte (src, byte);
4940 	      mips_emit_move (d, s);
4941 	    }
4942 	}
4943       else
4944 	{
4945 	  for (byte = GET_MODE_SIZE (TImode) - UNITS_PER_WORD; byte >= 0;
4946 	       byte -= UNITS_PER_WORD)
4947 	    {
4948 	      d = mips_subword_at_byte (dest, byte);
4949 	      s = mips_subword_at_byte (src, byte);
4950 	      mips_emit_move (d, s);
4951 	    }
4952 	}
4953     }
4954 }
4955 
4956 /* Split a COPY_S.D with operands DEST, SRC and INDEX.  GEN is a function
4957    used to generate subregs.  */
4958 
4959 void
mips_split_msa_copy_d(rtx dest,rtx src,rtx index,rtx (* gen_fn)(rtx,rtx,rtx))4960 mips_split_msa_copy_d (rtx dest, rtx src, rtx index,
4961 		       rtx (*gen_fn)(rtx, rtx, rtx))
4962 {
4963   gcc_assert ((GET_MODE (src) == V2DImode && GET_MODE (dest) == DImode)
4964 	      || (GET_MODE (src) == V2DFmode && GET_MODE (dest) == DFmode));
4965 
4966   /* Note that low is always from the lower index, and high is always
4967      from the higher index.  */
4968   rtx low = mips_subword (dest, false);
4969   rtx high = mips_subword (dest, true);
4970   rtx new_src = simplify_gen_subreg (V4SImode, src, GET_MODE (src), 0);
4971 
4972   emit_insn (gen_fn (low, new_src, GEN_INT (INTVAL (index) * 2)));
4973   emit_insn (gen_fn (high, new_src, GEN_INT (INTVAL (index) * 2 + 1)));
4974 }
4975 
4976 /* Split a INSERT.D with operand DEST, SRC1.INDEX and SRC2.  */
4977 
4978 void
mips_split_msa_insert_d(rtx dest,rtx src1,rtx index,rtx src2)4979 mips_split_msa_insert_d (rtx dest, rtx src1, rtx index, rtx src2)
4980 {
4981   int i;
4982   gcc_assert (GET_MODE (dest) == GET_MODE (src1));
4983   gcc_assert ((GET_MODE (dest) == V2DImode
4984 	       && (GET_MODE (src2) == DImode || src2 == const0_rtx))
4985 	      || (GET_MODE (dest) == V2DFmode && GET_MODE (src2) == DFmode));
4986 
4987   /* Note that low is always from the lower index, and high is always
4988      from the higher index.  */
4989   rtx low = mips_subword (src2, false);
4990   rtx high = mips_subword (src2, true);
4991   rtx new_dest = simplify_gen_subreg (V4SImode, dest, GET_MODE (dest), 0);
4992   rtx new_src1 = simplify_gen_subreg (V4SImode, src1, GET_MODE (src1), 0);
4993   i = exact_log2 (INTVAL (index));
4994   gcc_assert (i != -1);
4995 
4996   emit_insn (gen_msa_insert_w (new_dest, low, new_src1,
4997 			       GEN_INT (1 << (i * 2))));
4998   emit_insn (gen_msa_insert_w (new_dest, high, new_dest,
4999 			       GEN_INT (1 << (i * 2 + 1))));
5000 }
5001 
5002 /* Split FILL.D.  */
5003 
5004 void
mips_split_msa_fill_d(rtx dest,rtx src)5005 mips_split_msa_fill_d (rtx dest, rtx src)
5006 {
5007   gcc_assert ((GET_MODE (dest) == V2DImode
5008 	       && (GET_MODE (src) == DImode || src == const0_rtx))
5009 	      || (GET_MODE (dest) == V2DFmode && GET_MODE (src) == DFmode));
5010 
5011   /* Note that low is always from the lower index, and high is always
5012      from the higher index.  */
5013   rtx low, high;
5014   if (src == const0_rtx)
5015     {
5016       low = src;
5017       high = src;
5018     }
5019   else
5020     {
5021       low = mips_subword (src, false);
5022       high = mips_subword (src, true);
5023     }
5024   rtx new_dest = simplify_gen_subreg (V4SImode, dest, GET_MODE (dest), 0);
5025   emit_insn (gen_msa_fill_w (new_dest, low));
5026   emit_insn (gen_msa_insert_w (new_dest, high, new_dest, GEN_INT (1 << 1)));
5027   emit_insn (gen_msa_insert_w (new_dest, high, new_dest, GEN_INT (1 << 3)));
5028 }
5029 
5030 /* Return true if a move from SRC to DEST in INSN should be split.  */
5031 
5032 bool
mips_split_move_insn_p(rtx dest,rtx src,rtx insn)5033 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
5034 {
5035   return mips_split_move_p (dest, src, mips_insn_split_type (insn));
5036 }
5037 
5038 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
5039    holds.  */
5040 
5041 void
mips_split_move_insn(rtx dest,rtx src,rtx insn)5042 mips_split_move_insn (rtx dest, rtx src, rtx insn)
5043 {
5044   mips_split_move (dest, src, mips_insn_split_type (insn));
5045 }
5046 
5047 /* Return the appropriate instructions to move SRC into DEST.  Assume
5048    that SRC is operand 1 and DEST is operand 0.  */
5049 
5050 const char *
mips_output_move(rtx dest,rtx src)5051 mips_output_move (rtx dest, rtx src)
5052 {
5053   enum rtx_code dest_code = GET_CODE (dest);
5054   enum rtx_code src_code = GET_CODE (src);
5055   machine_mode mode = GET_MODE (dest);
5056   bool dbl_p = (GET_MODE_SIZE (mode) == 8);
5057   bool msa_p = MSA_SUPPORTED_MODE_P (mode);
5058   enum mips_symbol_type symbol_type;
5059 
5060   if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
5061     return "#";
5062 
5063   if (msa_p
5064       && dest_code == REG && FP_REG_P (REGNO (dest))
5065       && src_code == CONST_VECTOR
5066       && CONST_INT_P (CONST_VECTOR_ELT (src, 0)))
5067     {
5068       gcc_assert (mips_const_vector_same_int_p (src, mode, -512, 511));
5069       return "ldi.%v0\t%w0,%E1";
5070     }
5071 
5072   if ((src_code == REG && GP_REG_P (REGNO (src)))
5073       || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
5074     {
5075       if (dest_code == REG)
5076 	{
5077 	  if (GP_REG_P (REGNO (dest)))
5078 	    return "move\t%0,%z1";
5079 
5080 	  if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
5081 	    {
5082 	      if (ISA_HAS_DSP_MULT)
5083 		return "mult\t%q0,%.,%.";
5084 	      else
5085 		return "mult\t%.,%.";
5086 	    }
5087 
5088 	  /* Moves to HI are handled by special .md insns.  */
5089 	  if (REGNO (dest) == LO_REGNUM)
5090 	    return "mtlo\t%z1";
5091 
5092 	  if (DSP_ACC_REG_P (REGNO (dest)))
5093 	    {
5094 	      static char retval[] = "mt__\t%z1,%q0";
5095 
5096 	      retval[2] = reg_names[REGNO (dest)][4];
5097 	      retval[3] = reg_names[REGNO (dest)][5];
5098 	      return retval;
5099 	    }
5100 
5101 	  if (FP_REG_P (REGNO (dest)))
5102 	    {
5103 	      if (msa_p)
5104 		{
5105 		  gcc_assert (src == CONST0_RTX (GET_MODE (src)));
5106 		  return "ldi.%v0\t%w0,0";
5107 		}
5108 
5109 	      return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
5110 	    }
5111 
5112 	  if (ALL_COP_REG_P (REGNO (dest)))
5113 	    {
5114 	      static char retval[] = "dmtc_\t%z1,%0";
5115 
5116 	      retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
5117 	      return dbl_p ? retval : retval + 1;
5118 	    }
5119 	}
5120       if (dest_code == MEM)
5121 	switch (GET_MODE_SIZE (mode))
5122 	  {
5123 	  case 1: return "sb\t%z1,%0";
5124 	  case 2: return "sh\t%z1,%0";
5125 	  case 4: return "sw\t%z1,%0";
5126 	  case 8: return "sd\t%z1,%0";
5127 	  default: gcc_unreachable ();
5128 	  }
5129     }
5130   if (dest_code == REG && GP_REG_P (REGNO (dest)))
5131     {
5132       if (src_code == REG)
5133 	{
5134 	  /* Moves from HI are handled by special .md insns.  */
5135 	  if (REGNO (src) == LO_REGNUM)
5136 	    {
5137 	      /* When generating VR4120 or VR4130 code, we use MACC and
5138 		 DMACC instead of MFLO.  This avoids both the normal
5139 		 MIPS III HI/LO hazards and the errata related to
5140 		 -mfix-vr4130.  */
5141 	      if (ISA_HAS_MACCHI)
5142 		return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
5143 	      return "mflo\t%0";
5144 	    }
5145 
5146 	  if (DSP_ACC_REG_P (REGNO (src)))
5147 	    {
5148 	      static char retval[] = "mf__\t%0,%q1";
5149 
5150 	      retval[2] = reg_names[REGNO (src)][4];
5151 	      retval[3] = reg_names[REGNO (src)][5];
5152 	      return retval;
5153 	    }
5154 
5155 	  if (FP_REG_P (REGNO (src)))
5156 	    {
5157 	      gcc_assert (!msa_p);
5158 	      return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
5159 	    }
5160 
5161 	  if (ALL_COP_REG_P (REGNO (src)))
5162 	    {
5163 	      static char retval[] = "dmfc_\t%0,%1";
5164 
5165 	      retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
5166 	      return dbl_p ? retval : retval + 1;
5167 	    }
5168 	}
5169 
5170       if (src_code == MEM)
5171 	switch (GET_MODE_SIZE (mode))
5172 	  {
5173 	  case 1: return "lbu\t%0,%1";
5174 	  case 2: return "lhu\t%0,%1";
5175 	  case 4: return "lw\t%0,%1";
5176 	  case 8: return "ld\t%0,%1";
5177 	  default: gcc_unreachable ();
5178 	  }
5179 
5180       if (src_code == CONST_INT)
5181 	{
5182 	  /* Don't use the X format for the operand itself, because that
5183 	     will give out-of-range numbers for 64-bit hosts and 32-bit
5184 	     targets.  */
5185 	  if (!TARGET_MIPS16)
5186 	    return "li\t%0,%1\t\t\t# %X1";
5187 
5188 	  if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
5189 	    return "li\t%0,%1";
5190 
5191 	  if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
5192 	    return "#";
5193 	}
5194 
5195       if (src_code == HIGH)
5196 	return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
5197 
5198       if (CONST_GP_P (src))
5199 	return "move\t%0,%1";
5200 
5201       if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
5202 	  && mips_lo_relocs[symbol_type] != 0)
5203 	{
5204 	  /* A signed 16-bit constant formed by applying a relocation
5205 	     operator to a symbolic address.  */
5206 	  gcc_assert (!mips_split_p[symbol_type]);
5207 	  return "li\t%0,%R1";
5208 	}
5209 
5210       if (symbolic_operand (src, VOIDmode))
5211 	{
5212 	  gcc_assert (TARGET_MIPS16
5213 		      ? TARGET_MIPS16_TEXT_LOADS
5214 		      : !TARGET_EXPLICIT_RELOCS);
5215 	  return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
5216 	}
5217     }
5218   if (src_code == REG && FP_REG_P (REGNO (src)))
5219     {
5220       if (dest_code == REG && FP_REG_P (REGNO (dest)))
5221 	{
5222 	  if (GET_MODE (dest) == V2SFmode)
5223 	    return "mov.ps\t%0,%1";
5224 	  else if (msa_p)
5225 	    return "move.v\t%w0,%w1";
5226 	  else
5227 	    return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
5228 	}
5229 
5230       if (dest_code == MEM)
5231 	{
5232 	  if (msa_p)
5233 	    return "st.%v1\t%w1,%0";
5234 
5235 	  return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
5236 	}
5237     }
5238   if (dest_code == REG && FP_REG_P (REGNO (dest)))
5239     {
5240       if (src_code == MEM)
5241 	{
5242 	  if (msa_p)
5243 	    return "ld.%v0\t%w0,%1";
5244 
5245 	  return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
5246 	}
5247     }
5248   if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
5249     {
5250       static char retval[] = "l_c_\t%0,%1";
5251 
5252       retval[1] = (dbl_p ? 'd' : 'w');
5253       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
5254       return retval;
5255     }
5256   if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
5257     {
5258       static char retval[] = "s_c_\t%1,%0";
5259 
5260       retval[1] = (dbl_p ? 'd' : 'w');
5261       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
5262       return retval;
5263     }
5264   gcc_unreachable ();
5265 }
5266 
5267 /* Return true if CMP1 is a suitable second operand for integer ordering
5268    test CODE.  See also the *sCC patterns in mips.md.  */
5269 
5270 static bool
mips_int_order_operand_ok_p(enum rtx_code code,rtx cmp1)5271 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
5272 {
5273   switch (code)
5274     {
5275     case GT:
5276     case GTU:
5277       return reg_or_0_operand (cmp1, VOIDmode);
5278 
5279     case GE:
5280     case GEU:
5281       return !TARGET_MIPS16 && cmp1 == const1_rtx;
5282 
5283     case LT:
5284     case LTU:
5285       return arith_operand (cmp1, VOIDmode);
5286 
5287     case LE:
5288       return sle_operand (cmp1, VOIDmode);
5289 
5290     case LEU:
5291       return sleu_operand (cmp1, VOIDmode);
5292 
5293     default:
5294       gcc_unreachable ();
5295     }
5296 }
5297 
5298 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
5299    integer ordering test *CODE, or if an equivalent combination can
5300    be formed by adjusting *CODE and *CMP1.  When returning true, update
5301    *CODE and *CMP1 with the chosen code and operand, otherwise leave
5302    them alone.  */
5303 
5304 static bool
mips_canonicalize_int_order_test(enum rtx_code * code,rtx * cmp1,machine_mode mode)5305 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
5306 				  machine_mode mode)
5307 {
5308   HOST_WIDE_INT plus_one;
5309 
5310   if (mips_int_order_operand_ok_p (*code, *cmp1))
5311     return true;
5312 
5313   if (CONST_INT_P (*cmp1))
5314     switch (*code)
5315       {
5316       case LE:
5317 	plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
5318 	if (INTVAL (*cmp1) < plus_one)
5319 	  {
5320 	    *code = LT;
5321 	    *cmp1 = force_reg (mode, GEN_INT (plus_one));
5322 	    return true;
5323 	  }
5324 	break;
5325 
5326       case LEU:
5327 	plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
5328 	if (plus_one != 0)
5329 	  {
5330 	    *code = LTU;
5331 	    *cmp1 = force_reg (mode, GEN_INT (plus_one));
5332 	    return true;
5333 	  }
5334 	break;
5335 
5336       default:
5337 	break;
5338       }
5339   return false;
5340 }
5341 
5342 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
5343    in TARGET.  CMP0 and TARGET are register_operands.  If INVERT_PTR
5344    is nonnull, it's OK to set TARGET to the inverse of the result and
5345    flip *INVERT_PTR instead.  */
5346 
5347 static void
mips_emit_int_order_test(enum rtx_code code,bool * invert_ptr,rtx target,rtx cmp0,rtx cmp1)5348 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
5349 			  rtx target, rtx cmp0, rtx cmp1)
5350 {
5351   machine_mode mode;
5352 
5353   /* First see if there is a MIPS instruction that can do this operation.
5354      If not, try doing the same for the inverse operation.  If that also
5355      fails, force CMP1 into a register and try again.  */
5356   mode = GET_MODE (cmp0);
5357   if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
5358     mips_emit_binary (code, target, cmp0, cmp1);
5359   else
5360     {
5361       enum rtx_code inv_code = reverse_condition (code);
5362       if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
5363 	{
5364 	  cmp1 = force_reg (mode, cmp1);
5365 	  mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
5366 	}
5367       else if (invert_ptr == 0)
5368 	{
5369 	  rtx inv_target;
5370 
5371 	  inv_target = mips_force_binary (GET_MODE (target),
5372 					  inv_code, cmp0, cmp1);
5373 	  mips_emit_binary (XOR, target, inv_target, const1_rtx);
5374 	}
5375       else
5376 	{
5377 	  *invert_ptr = !*invert_ptr;
5378 	  mips_emit_binary (inv_code, target, cmp0, cmp1);
5379 	}
5380     }
5381 }
5382 
5383 /* Return a register that is zero iff CMP0 and CMP1 are equal.
5384    The register will have the same mode as CMP0.  */
5385 
5386 static rtx
mips_zero_if_equal(rtx cmp0,rtx cmp1)5387 mips_zero_if_equal (rtx cmp0, rtx cmp1)
5388 {
5389   if (cmp1 == const0_rtx)
5390     return cmp0;
5391 
5392   if (uns_arith_operand (cmp1, VOIDmode))
5393     return expand_binop (GET_MODE (cmp0), xor_optab,
5394 			 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
5395 
5396   return expand_binop (GET_MODE (cmp0), sub_optab,
5397 		       cmp0, cmp1, 0, 0, OPTAB_DIRECT);
5398 }
5399 
5400 /* Convert *CODE into a code that can be used in a floating-point
5401    scc instruction (C.cond.fmt).  Return true if the values of
5402    the condition code registers will be inverted, with 0 indicating
5403    that the condition holds.  */
5404 
5405 static bool
mips_reversed_fp_cond(enum rtx_code * code)5406 mips_reversed_fp_cond (enum rtx_code *code)
5407 {
5408   switch (*code)
5409     {
5410     case NE:
5411     case LTGT:
5412     case ORDERED:
5413       *code = reverse_condition_maybe_unordered (*code);
5414       return true;
5415 
5416     default:
5417       return false;
5418     }
5419 }
5420 
5421 /* Allocate a floating-point condition-code register of mode MODE.
5422 
5423    These condition code registers are used for certain kinds
5424    of compound operation, such as compare and branches, vconds,
5425    and built-in functions.  At expand time, their use is entirely
5426    controlled by MIPS-specific code and is entirely internal
5427    to these compound operations.
5428 
5429    We could (and did in the past) expose condition-code values
5430    as pseudo registers and leave the register allocator to pick
5431    appropriate registers.  The problem is that it is not practically
5432    possible for the rtl optimizers to guarantee that no spills will
5433    be needed, even when AVOID_CCMODE_COPIES is defined.  We would
5434    therefore need spill and reload sequences to handle the worst case.
5435 
5436    Although such sequences do exist, they are very expensive and are
5437    not something we'd want to use.  This is especially true of CCV2 and
5438    CCV4, where all the shuffling would greatly outweigh whatever benefit
5439    the vectorization itself provides.
5440 
5441    The main benefit of having more than one condition-code register
5442    is to allow the pipelining of operations, especially those involving
5443    comparisons and conditional moves.  We don't really expect the
5444    registers to be live for long periods, and certainly never want
5445    them to be live across calls.
5446 
5447    Also, there should be no penalty attached to using all the available
5448    registers.  They are simply bits in the same underlying FPU control
5449    register.
5450 
5451    We therefore expose the hardware registers from the outset and use
5452    a simple round-robin allocation scheme.  */
5453 
5454 static rtx
mips_allocate_fcc(machine_mode mode)5455 mips_allocate_fcc (machine_mode mode)
5456 {
5457   unsigned int regno, count;
5458 
5459   gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
5460 
5461   if (mode == CCmode)
5462     count = 1;
5463   else if (mode == CCV2mode)
5464     count = 2;
5465   else if (mode == CCV4mode)
5466     count = 4;
5467   else
5468     gcc_unreachable ();
5469 
5470   cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
5471   if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
5472     cfun->machine->next_fcc = 0;
5473   regno = ST_REG_FIRST + cfun->machine->next_fcc;
5474   cfun->machine->next_fcc += count;
5475   return gen_rtx_REG (mode, regno);
5476 }
5477 
5478 /* Convert a comparison into something that can be used in a branch or
5479    conditional move.  On entry, *OP0 and *OP1 are the values being
5480    compared and *CODE is the code used to compare them.
5481 
5482    Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
5483    If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
5484    otherwise any standard branch condition can be used.  The standard branch
5485    conditions are:
5486 
5487       - EQ or NE between two registers.
5488       - any comparison between a register and zero.
5489       - if compact branches are available then any condition is valid.  */
5490 
5491 static void
mips_emit_compare(enum rtx_code * code,rtx * op0,rtx * op1,bool need_eq_ne_p)5492 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
5493 {
5494   rtx cmp_op0 = *op0;
5495   rtx cmp_op1 = *op1;
5496 
5497   if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
5498     {
5499       if (!need_eq_ne_p && *op1 == const0_rtx)
5500 	;
5501       else if (*code == EQ || *code == NE)
5502 	{
5503 	  if (need_eq_ne_p)
5504 	    {
5505 	      *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
5506 	      *op1 = const0_rtx;
5507 	    }
5508 	  else
5509 	    *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
5510 	}
5511       else if (!need_eq_ne_p && TARGET_CB_MAYBE)
5512 	{
5513 	  bool swap = false;
5514 	  switch (*code)
5515 	    {
5516 	    case LE:
5517 	      swap = true;
5518 	      *code = GE;
5519 	      break;
5520 	    case GT:
5521 	      swap = true;
5522 	      *code = LT;
5523 	      break;
5524 	    case LEU:
5525 	      swap = true;
5526 	      *code = GEU;
5527 	      break;
5528 	    case GTU:
5529 	      swap = true;
5530 	      *code = LTU;
5531 	      break;
5532 	    case GE:
5533 	    case LT:
5534 	    case GEU:
5535 	    case LTU:
5536 	      /* Do nothing.  */
5537 	      break;
5538 	    default:
5539 	      gcc_unreachable ();
5540 	    }
5541 	  *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
5542 	  if (swap)
5543 	    {
5544 	      rtx tmp = *op1;
5545 	      *op1 = *op0;
5546 	      *op0 = tmp;
5547 	    }
5548 	}
5549       else
5550 	{
5551 	  /* The comparison needs a separate scc instruction.  Store the
5552 	     result of the scc in *OP0 and compare it against zero.  */
5553 	  bool invert = false;
5554 	  *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
5555 	  mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
5556 	  *code = (invert ? EQ : NE);
5557 	  *op1 = const0_rtx;
5558 	}
5559     }
5560   else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
5561     {
5562       *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
5563       mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
5564       *code = NE;
5565       *op1 = const0_rtx;
5566     }
5567   else
5568     {
5569       enum rtx_code cmp_code;
5570 
5571       /* Floating-point tests use a separate C.cond.fmt or CMP.cond.fmt
5572 	 comparison to set a register.  The branch or conditional move will
5573 	 then compare that register against zero.
5574 
5575 	 Set CMP_CODE to the code of the comparison instruction and
5576 	 *CODE to the code that the branch or move should use.  */
5577       cmp_code = *code;
5578       if (ISA_HAS_CCF)
5579 	{
5580 	  /* All FP conditions can be implemented directly with CMP.cond.fmt
5581 	     or by reversing the operands.  */
5582 	  *code = NE;
5583 	  *op0 = gen_reg_rtx (CCFmode);
5584 	}
5585       else
5586 	{
5587 	  /* Three FP conditions cannot be implemented by reversing the
5588 	     operands for C.cond.fmt, instead a reversed condition code is
5589 	     required and a test for false.  */
5590 	  *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
5591 	  if (ISA_HAS_8CC)
5592 	    *op0 = mips_allocate_fcc (CCmode);
5593 	  else
5594 	    *op0 = gen_rtx_REG (CCmode, FPSW_REGNUM);
5595 	}
5596 
5597       *op1 = const0_rtx;
5598       mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
5599     }
5600 }
5601 
5602 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
5603    and OPERAND[3].  Store the result in OPERANDS[0].
5604 
5605    On 64-bit targets, the mode of the comparison and target will always be
5606    SImode, thus possibly narrower than that of the comparison's operands.  */
5607 
5608 void
mips_expand_scc(rtx operands[])5609 mips_expand_scc (rtx operands[])
5610 {
5611   rtx target = operands[0];
5612   enum rtx_code code = GET_CODE (operands[1]);
5613   rtx op0 = operands[2];
5614   rtx op1 = operands[3];
5615 
5616   gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
5617 
5618   if (code == EQ || code == NE)
5619     {
5620       if (ISA_HAS_SEQ_SNE
5621 	  && reg_imm10_operand (op1, GET_MODE (op1)))
5622 	mips_emit_binary (code, target, op0, op1);
5623       else
5624 	{
5625 	  rtx zie = mips_zero_if_equal (op0, op1);
5626 	  mips_emit_binary (code, target, zie, const0_rtx);
5627 	}
5628     }
5629   else
5630     mips_emit_int_order_test (code, 0, target, op0, op1);
5631 }
5632 
5633 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
5634    CODE and jump to OPERANDS[3] if the condition holds.  */
5635 
5636 void
mips_expand_conditional_branch(rtx * operands)5637 mips_expand_conditional_branch (rtx *operands)
5638 {
5639   enum rtx_code code = GET_CODE (operands[0]);
5640   rtx op0 = operands[1];
5641   rtx op1 = operands[2];
5642   rtx condition;
5643 
5644   mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
5645   condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
5646   emit_jump_insn (gen_condjump (condition, operands[3]));
5647 }
5648 
5649 /* Implement:
5650 
5651    (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
5652    (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS))  */
5653 
5654 void
mips_expand_vcondv2sf(rtx dest,rtx true_src,rtx false_src,enum rtx_code cond,rtx cmp_op0,rtx cmp_op1)5655 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
5656 		       enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
5657 {
5658   rtx cmp_result;
5659   bool reversed_p;
5660 
5661   reversed_p = mips_reversed_fp_cond (&cond);
5662   cmp_result = mips_allocate_fcc (CCV2mode);
5663   emit_insn (gen_scc_ps (cmp_result,
5664 			 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
5665   if (reversed_p)
5666     emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
5667 					 cmp_result));
5668   else
5669     emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
5670 					 cmp_result));
5671 }
5672 
5673 /* Perform the comparison in OPERANDS[1].  Move OPERANDS[2] into OPERANDS[0]
5674    if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0].  */
5675 
5676 void
mips_expand_conditional_move(rtx * operands)5677 mips_expand_conditional_move (rtx *operands)
5678 {
5679   rtx cond;
5680   enum rtx_code code = GET_CODE (operands[1]);
5681   rtx op0 = XEXP (operands[1], 0);
5682   rtx op1 = XEXP (operands[1], 1);
5683 
5684   mips_emit_compare (&code, &op0, &op1, true);
5685   cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
5686 
5687   /* There is no direct support for general conditional GP move involving
5688      two registers using SEL.  */
5689   if (ISA_HAS_SEL
5690       && INTEGRAL_MODE_P (GET_MODE (operands[2]))
5691       && register_operand (operands[2], VOIDmode)
5692       && register_operand (operands[3], VOIDmode))
5693     {
5694       machine_mode mode = GET_MODE (operands[0]);
5695       rtx temp = gen_reg_rtx (mode);
5696       rtx temp2 = gen_reg_rtx (mode);
5697 
5698       emit_insn (gen_rtx_SET (temp,
5699 			      gen_rtx_IF_THEN_ELSE (mode, cond,
5700 						    operands[2], const0_rtx)));
5701 
5702       /* Flip the test for the second operand.  */
5703       cond = gen_rtx_fmt_ee ((code == EQ) ? NE : EQ, GET_MODE (op0), op0, op1);
5704 
5705       emit_insn (gen_rtx_SET (temp2,
5706 			      gen_rtx_IF_THEN_ELSE (mode, cond,
5707 						    operands[3], const0_rtx)));
5708 
5709       /* Merge the two results, at least one is guaranteed to be zero.  */
5710       emit_insn (gen_rtx_SET (operands[0], gen_rtx_IOR (mode, temp, temp2)));
5711     }
5712   else
5713     {
5714       if (FLOAT_MODE_P (GET_MODE (operands[2])) && !ISA_HAS_SEL)
5715 	{
5716 	  operands[2] = force_reg (GET_MODE (operands[0]), operands[2]);
5717 	  operands[3] = force_reg (GET_MODE (operands[0]), operands[3]);
5718 	}
5719 
5720       emit_insn (gen_rtx_SET (operands[0],
5721 			      gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
5722 						    operands[2], operands[3])));
5723     }
5724 }
5725 
5726 /* Perform the comparison in COMPARISON, then trap if the condition holds.  */
5727 
5728 void
mips_expand_conditional_trap(rtx comparison)5729 mips_expand_conditional_trap (rtx comparison)
5730 {
5731   rtx op0, op1;
5732   machine_mode mode;
5733   enum rtx_code code;
5734 
5735   /* MIPS conditional trap instructions don't have GT or LE flavors,
5736      so we must swap the operands and convert to LT and GE respectively.  */
5737   code = GET_CODE (comparison);
5738   switch (code)
5739     {
5740     case GT:
5741     case LE:
5742     case GTU:
5743     case LEU:
5744       code = swap_condition (code);
5745       op0 = XEXP (comparison, 1);
5746       op1 = XEXP (comparison, 0);
5747       break;
5748 
5749     default:
5750       op0 = XEXP (comparison, 0);
5751       op1 = XEXP (comparison, 1);
5752       break;
5753     }
5754 
5755   mode = GET_MODE (XEXP (comparison, 0));
5756   op0 = force_reg (mode, op0);
5757   if (!(ISA_HAS_COND_TRAPI
5758 	? arith_operand (op1, mode)
5759 	: reg_or_0_operand (op1, mode)))
5760     op1 = force_reg (mode, op1);
5761 
5762   emit_insn (gen_rtx_TRAP_IF (VOIDmode,
5763 			      gen_rtx_fmt_ee (code, mode, op0, op1),
5764 			      const0_rtx));
5765 }
5766 
5767 /* Initialize *CUM for a call to a function of type FNTYPE.  */
5768 
5769 void
mips_init_cumulative_args(CUMULATIVE_ARGS * cum,tree fntype)5770 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
5771 {
5772   memset (cum, 0, sizeof (*cum));
5773   cum->prototype = (fntype && prototype_p (fntype));
5774   cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
5775 }
5776 
5777 /* Fill INFO with information about a single argument.  CUM is the
5778    cumulative state for earlier arguments.  MODE is the mode of this
5779    argument and TYPE is its type (if known).  NAMED is true if this
5780    is a named (fixed) argument rather than a variable one.  */
5781 
5782 static void
mips_get_arg_info(struct mips_arg_info * info,const CUMULATIVE_ARGS * cum,machine_mode mode,const_tree type,bool named)5783 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
5784 		   machine_mode mode, const_tree type, bool named)
5785 {
5786   bool doubleword_aligned_p;
5787   unsigned int num_bytes, num_words, max_regs;
5788 
5789   /* Work out the size of the argument.  */
5790   num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5791   num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5792 
5793   /* Decide whether it should go in a floating-point register, assuming
5794      one is free.  Later code checks for availability.
5795 
5796      The checks against UNITS_PER_FPVALUE handle the soft-float and
5797      single-float cases.  */
5798   switch (mips_abi)
5799     {
5800     case ABI_EABI:
5801       /* The EABI conventions have traditionally been defined in terms
5802 	 of TYPE_MODE, regardless of the actual type.  */
5803       info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
5804 		      || mode == V2SFmode)
5805 		     && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5806       break;
5807 
5808     case ABI_32:
5809     case ABI_O64:
5810       /* Only leading floating-point scalars are passed in
5811 	 floating-point registers.  We also handle vector floats the same
5812 	 say, which is OK because they are not covered by the standard ABI.  */
5813       gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
5814       info->fpr_p = (!cum->gp_reg_found
5815 		     && cum->arg_number < 2
5816 		     && (type == 0
5817 			 || SCALAR_FLOAT_TYPE_P (type)
5818 			 || VECTOR_FLOAT_TYPE_P (type))
5819 		     && (GET_MODE_CLASS (mode) == MODE_FLOAT
5820 			 || mode == V2SFmode)
5821 		     && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5822       break;
5823 
5824     case ABI_N32:
5825     case ABI_64:
5826       /* Scalar, complex and vector floating-point types are passed in
5827 	 floating-point registers, as long as this is a named rather
5828 	 than a variable argument.  */
5829       gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
5830       info->fpr_p = (named
5831 		     && (type == 0 || FLOAT_TYPE_P (type))
5832 		     && (GET_MODE_CLASS (mode) == MODE_FLOAT
5833 			 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5834 			 || mode == V2SFmode)
5835 		     && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
5836 
5837       /* ??? According to the ABI documentation, the real and imaginary
5838 	 parts of complex floats should be passed in individual registers.
5839 	 The real and imaginary parts of stack arguments are supposed
5840 	 to be contiguous and there should be an extra word of padding
5841 	 at the end.
5842 
5843 	 This has two problems.  First, it makes it impossible to use a
5844 	 single "void *" va_list type, since register and stack arguments
5845 	 are passed differently.  (At the time of writing, MIPSpro cannot
5846 	 handle complex float varargs correctly.)  Second, it's unclear
5847 	 what should happen when there is only one register free.
5848 
5849 	 For now, we assume that named complex floats should go into FPRs
5850 	 if there are two FPRs free, otherwise they should be passed in the
5851 	 same way as a struct containing two floats.  */
5852       if (info->fpr_p
5853 	  && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5854 	  && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
5855 	{
5856 	  if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
5857 	    info->fpr_p = false;
5858 	  else
5859 	    num_words = 2;
5860 	}
5861       break;
5862 
5863     default:
5864       gcc_unreachable ();
5865     }
5866 
5867   /* See whether the argument has doubleword alignment.  */
5868   doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
5869 			  > BITS_PER_WORD);
5870 
5871   /* Set REG_OFFSET to the register count we're interested in.
5872      The EABI allocates the floating-point registers separately,
5873      but the other ABIs allocate them like integer registers.  */
5874   info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
5875 		      ? cum->num_fprs
5876 		      : cum->num_gprs);
5877 
5878   /* Advance to an even register if the argument is doubleword-aligned.  */
5879   if (doubleword_aligned_p)
5880     info->reg_offset += info->reg_offset & 1;
5881 
5882   /* Work out the offset of a stack argument.  */
5883   info->stack_offset = cum->stack_words;
5884   if (doubleword_aligned_p)
5885     info->stack_offset += info->stack_offset & 1;
5886 
5887   max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
5888 
5889   /* Partition the argument between registers and stack.  */
5890   info->reg_words = MIN (num_words, max_regs);
5891   info->stack_words = num_words - info->reg_words;
5892 }
5893 
5894 /* INFO describes a register argument that has the normal format for the
5895    argument's mode.  Return the register it uses, assuming that FPRs are
5896    available if HARD_FLOAT_P.  */
5897 
5898 static unsigned int
mips_arg_regno(const struct mips_arg_info * info,bool hard_float_p)5899 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
5900 {
5901   if (!info->fpr_p || !hard_float_p)
5902     return GP_ARG_FIRST + info->reg_offset;
5903   else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
5904     /* In o32, the second argument is always passed in $f14
5905        for TARGET_DOUBLE_FLOAT, regardless of whether the
5906        first argument was a word or doubleword.  */
5907     return FP_ARG_FIRST + 2;
5908   else
5909     return FP_ARG_FIRST + info->reg_offset;
5910 }
5911 
5912 /* Implement TARGET_STRICT_ARGUMENT_NAMING.  */
5913 
5914 static bool
mips_strict_argument_naming(cumulative_args_t ca ATTRIBUTE_UNUSED)5915 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
5916 {
5917   return !TARGET_OLDABI;
5918 }
5919 
5920 /* Implement TARGET_FUNCTION_ARG.  */
5921 
5922 static rtx
mips_function_arg(cumulative_args_t cum_v,machine_mode mode,const_tree type,bool named)5923 mips_function_arg (cumulative_args_t cum_v, machine_mode mode,
5924 		   const_tree type, bool named)
5925 {
5926   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5927   struct mips_arg_info info;
5928 
5929   /* We will be called with a mode of VOIDmode after the last argument
5930      has been seen.  Whatever we return will be passed to the call expander.
5931      If we need a MIPS16 fp_code, return a REG with the code stored as
5932      the mode.  */
5933   if (mode == VOIDmode)
5934     {
5935       if (TARGET_MIPS16 && cum->fp_code != 0)
5936 	return gen_rtx_REG ((machine_mode) cum->fp_code, 0);
5937       else
5938 	return NULL;
5939     }
5940 
5941   mips_get_arg_info (&info, cum, mode, type, named);
5942 
5943   /* Return straight away if the whole argument is passed on the stack.  */
5944   if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
5945     return NULL;
5946 
5947   /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
5948      contains a double in its entirety, then that 64-bit chunk is passed
5949      in a floating-point register.  */
5950   if (TARGET_NEWABI
5951       && TARGET_HARD_FLOAT
5952       && named
5953       && type != 0
5954       && TREE_CODE (type) == RECORD_TYPE
5955       && TYPE_SIZE_UNIT (type)
5956       && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
5957     {
5958       tree field;
5959 
5960       /* First check to see if there is any such field.  */
5961       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5962 	if (TREE_CODE (field) == FIELD_DECL
5963 	    && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5964 	    && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
5965 	    && tree_fits_shwi_p (bit_position (field))
5966 	    && int_bit_position (field) % BITS_PER_WORD == 0)
5967 	  break;
5968 
5969       if (field != 0)
5970 	{
5971 	  /* Now handle the special case by returning a PARALLEL
5972 	     indicating where each 64-bit chunk goes.  INFO.REG_WORDS
5973 	     chunks are passed in registers.  */
5974 	  unsigned int i;
5975 	  HOST_WIDE_INT bitpos;
5976 	  rtx ret;
5977 
5978 	  /* assign_parms checks the mode of ENTRY_PARM, so we must
5979 	     use the actual mode here.  */
5980 	  ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
5981 
5982 	  bitpos = 0;
5983 	  field = TYPE_FIELDS (type);
5984 	  for (i = 0; i < info.reg_words; i++)
5985 	    {
5986 	      rtx reg;
5987 
5988 	      for (; field; field = DECL_CHAIN (field))
5989 		if (TREE_CODE (field) == FIELD_DECL
5990 		    && int_bit_position (field) >= bitpos)
5991 		  break;
5992 
5993 	      if (field
5994 		  && int_bit_position (field) == bitpos
5995 		  && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5996 		  && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
5997 		reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
5998 	      else
5999 		reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
6000 
6001 	      XVECEXP (ret, 0, i)
6002 		= gen_rtx_EXPR_LIST (VOIDmode, reg,
6003 				     GEN_INT (bitpos / BITS_PER_UNIT));
6004 
6005 	      bitpos += BITS_PER_WORD;
6006 	    }
6007 	  return ret;
6008 	}
6009     }
6010 
6011   /* Handle the n32/n64 conventions for passing complex floating-point
6012      arguments in FPR pairs.  The real part goes in the lower register
6013      and the imaginary part goes in the upper register.  */
6014   if (TARGET_NEWABI
6015       && info.fpr_p
6016       && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
6017     {
6018       rtx real, imag;
6019       machine_mode inner;
6020       unsigned int regno;
6021 
6022       inner = GET_MODE_INNER (mode);
6023       regno = FP_ARG_FIRST + info.reg_offset;
6024       if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
6025 	{
6026 	  /* Real part in registers, imaginary part on stack.  */
6027 	  gcc_assert (info.stack_words == info.reg_words);
6028 	  return gen_rtx_REG (inner, regno);
6029 	}
6030       else
6031 	{
6032 	  gcc_assert (info.stack_words == 0);
6033 	  real = gen_rtx_EXPR_LIST (VOIDmode,
6034 				    gen_rtx_REG (inner, regno),
6035 				    const0_rtx);
6036 	  imag = gen_rtx_EXPR_LIST (VOIDmode,
6037 				    gen_rtx_REG (inner,
6038 						 regno + info.reg_words / 2),
6039 				    GEN_INT (GET_MODE_SIZE (inner)));
6040 	  return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
6041 	}
6042     }
6043 
6044   return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
6045 }
6046 
6047 /* Implement TARGET_FUNCTION_ARG_ADVANCE.  */
6048 
6049 static void
mips_function_arg_advance(cumulative_args_t cum_v,machine_mode mode,const_tree type,bool named)6050 mips_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
6051 			   const_tree type, bool named)
6052 {
6053   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6054   struct mips_arg_info info;
6055 
6056   mips_get_arg_info (&info, cum, mode, type, named);
6057 
6058   if (!info.fpr_p)
6059     cum->gp_reg_found = true;
6060 
6061   /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
6062      an explanation of what this code does.  It assumes that we're using
6063      either the o32 or the o64 ABI, both of which pass at most 2 arguments
6064      in FPRs.  */
6065   if (cum->arg_number < 2 && info.fpr_p)
6066     cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
6067 
6068   /* Advance the register count.  This has the effect of setting
6069      num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
6070      argument required us to skip the final GPR and pass the whole
6071      argument on the stack.  */
6072   if (mips_abi != ABI_EABI || !info.fpr_p)
6073     cum->num_gprs = info.reg_offset + info.reg_words;
6074   else if (info.reg_words > 0)
6075     cum->num_fprs += MAX_FPRS_PER_FMT;
6076 
6077   /* Advance the stack word count.  */
6078   if (info.stack_words > 0)
6079     cum->stack_words = info.stack_offset + info.stack_words;
6080 
6081   cum->arg_number++;
6082 }
6083 
6084 /* Implement TARGET_ARG_PARTIAL_BYTES.  */
6085 
6086 static int
mips_arg_partial_bytes(cumulative_args_t cum,machine_mode mode,tree type,bool named)6087 mips_arg_partial_bytes (cumulative_args_t cum,
6088 			machine_mode mode, tree type, bool named)
6089 {
6090   struct mips_arg_info info;
6091 
6092   mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
6093   return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
6094 }
6095 
6096 /* Implement TARGET_FUNCTION_ARG_BOUNDARY.  Every parameter gets at
6097    least PARM_BOUNDARY bits of alignment, but will be given anything up
6098    to STACK_BOUNDARY bits if the type requires it.  */
6099 
6100 static unsigned int
mips_function_arg_boundary(machine_mode mode,const_tree type)6101 mips_function_arg_boundary (machine_mode mode, const_tree type)
6102 {
6103   unsigned int alignment;
6104 
6105   alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
6106   if (alignment < PARM_BOUNDARY)
6107     alignment = PARM_BOUNDARY;
6108   if (alignment > STACK_BOUNDARY)
6109     alignment = STACK_BOUNDARY;
6110   return alignment;
6111 }
6112 
6113 /* Implement TARGET_GET_RAW_RESULT_MODE and TARGET_GET_RAW_ARG_MODE.  */
6114 
6115 static fixed_size_mode
mips_get_reg_raw_mode(int regno)6116 mips_get_reg_raw_mode (int regno)
6117 {
6118   if (TARGET_FLOATXX && FP_REG_P (regno))
6119     return DFmode;
6120   return default_get_reg_raw_mode (regno);
6121 }
6122 
6123 /* Implement TARGET_FUNCTION_ARG_PADDING; return PAD_UPWARD if the first
6124    byte of the stack slot has useful data, PAD_DOWNWARD if the last byte
6125    does.  */
6126 
6127 static pad_direction
mips_function_arg_padding(machine_mode mode,const_tree type)6128 mips_function_arg_padding (machine_mode mode, const_tree type)
6129 {
6130   /* On little-endian targets, the first byte of every stack argument
6131      is passed in the first byte of the stack slot.  */
6132   if (!BYTES_BIG_ENDIAN)
6133     return PAD_UPWARD;
6134 
6135   /* Otherwise, integral types are padded downward: the last byte of a
6136      stack argument is passed in the last byte of the stack slot.  */
6137   if (type != 0
6138       ? (INTEGRAL_TYPE_P (type)
6139 	 || POINTER_TYPE_P (type)
6140 	 || FIXED_POINT_TYPE_P (type))
6141       : (SCALAR_INT_MODE_P (mode)
6142 	 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
6143     return PAD_DOWNWARD;
6144 
6145   /* Big-endian o64 pads floating-point arguments downward.  */
6146   if (mips_abi == ABI_O64)
6147     if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
6148       return PAD_DOWNWARD;
6149 
6150   /* Other types are padded upward for o32, o64, n32 and n64.  */
6151   if (mips_abi != ABI_EABI)
6152     return PAD_UPWARD;
6153 
6154   /* Arguments smaller than a stack slot are padded downward.  */
6155   if (mode != BLKmode
6156       ? GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY
6157       : int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT))
6158     return PAD_UPWARD;
6159 
6160   return PAD_DOWNWARD;
6161 }
6162 
6163 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...).  Return !BYTES_BIG_ENDIAN
6164    if the least significant byte of the register has useful data.  Return
6165    the opposite if the most significant byte does.  */
6166 
6167 bool
mips_pad_reg_upward(machine_mode mode,tree type)6168 mips_pad_reg_upward (machine_mode mode, tree type)
6169 {
6170   /* No shifting is required for floating-point arguments.  */
6171   if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
6172     return !BYTES_BIG_ENDIAN;
6173 
6174   /* Otherwise, apply the same padding to register arguments as we do
6175      to stack arguments.  */
6176   return mips_function_arg_padding (mode, type) == PAD_UPWARD;
6177 }
6178 
6179 /* Return nonzero when an argument must be passed by reference.  */
6180 
6181 static bool
mips_pass_by_reference(cumulative_args_t cum ATTRIBUTE_UNUSED,machine_mode mode,const_tree type,bool named ATTRIBUTE_UNUSED)6182 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
6183 			machine_mode mode, const_tree type,
6184 			bool named ATTRIBUTE_UNUSED)
6185 {
6186   if (mips_abi == ABI_EABI)
6187     {
6188       int size;
6189 
6190       /* ??? How should SCmode be handled?  */
6191       if (mode == DImode || mode == DFmode
6192 	  || mode == DQmode || mode == UDQmode
6193 	  || mode == DAmode || mode == UDAmode)
6194 	return 0;
6195 
6196       size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
6197       return size == -1 || size > UNITS_PER_WORD;
6198     }
6199   else
6200     {
6201       /* If we have a variable-sized parameter, we have no choice.  */
6202       return targetm.calls.must_pass_in_stack (mode, type);
6203     }
6204 }
6205 
6206 /* Implement TARGET_CALLEE_COPIES.  */
6207 
6208 static bool
mips_callee_copies(cumulative_args_t cum ATTRIBUTE_UNUSED,machine_mode mode ATTRIBUTE_UNUSED,const_tree type ATTRIBUTE_UNUSED,bool named)6209 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
6210 		    machine_mode mode ATTRIBUTE_UNUSED,
6211 		    const_tree type ATTRIBUTE_UNUSED, bool named)
6212 {
6213   return mips_abi == ABI_EABI && named;
6214 }
6215 
6216 /* See whether VALTYPE is a record whose fields should be returned in
6217    floating-point registers.  If so, return the number of fields and
6218    list them in FIELDS (which should have two elements).  Return 0
6219    otherwise.
6220 
6221    For n32 & n64, a structure with one or two fields is returned in
6222    floating-point registers as long as every field has a floating-point
6223    type.  */
6224 
6225 static int
mips_fpr_return_fields(const_tree valtype,tree * fields)6226 mips_fpr_return_fields (const_tree valtype, tree *fields)
6227 {
6228   tree field;
6229   int i;
6230 
6231   if (!TARGET_NEWABI)
6232     return 0;
6233 
6234   if (TREE_CODE (valtype) != RECORD_TYPE)
6235     return 0;
6236 
6237   i = 0;
6238   for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
6239     {
6240       if (TREE_CODE (field) != FIELD_DECL)
6241 	continue;
6242 
6243       if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
6244 	return 0;
6245 
6246       if (i == 2)
6247 	return 0;
6248 
6249       fields[i++] = field;
6250     }
6251   return i;
6252 }
6253 
6254 /* Implement TARGET_RETURN_IN_MSB.  For n32 & n64, we should return
6255    a value in the most significant part of $2/$3 if:
6256 
6257       - the target is big-endian;
6258 
6259       - the value has a structure or union type (we generalize this to
6260 	cover aggregates from other languages too); and
6261 
6262       - the structure is not returned in floating-point registers.  */
6263 
6264 static bool
mips_return_in_msb(const_tree valtype)6265 mips_return_in_msb (const_tree valtype)
6266 {
6267   tree fields[2];
6268 
6269   return (TARGET_NEWABI
6270 	  && TARGET_BIG_ENDIAN
6271 	  && AGGREGATE_TYPE_P (valtype)
6272 	  && mips_fpr_return_fields (valtype, fields) == 0);
6273 }
6274 
6275 /* Return true if the function return value MODE will get returned in a
6276    floating-point register.  */
6277 
6278 static bool
mips_return_mode_in_fpr_p(machine_mode mode)6279 mips_return_mode_in_fpr_p (machine_mode mode)
6280 {
6281   gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
6282   return ((GET_MODE_CLASS (mode) == MODE_FLOAT
6283 	   || mode == V2SFmode
6284 	   || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
6285 	  && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
6286 }
6287 
6288 /* Return the representation of an FPR return register when the
6289    value being returned in FP_RETURN has mode VALUE_MODE and the
6290    return type itself has mode TYPE_MODE.  On NewABI targets,
6291    the two modes may be different for structures like:
6292 
6293        struct __attribute__((packed)) foo { float f; }
6294 
6295    where we return the SFmode value of "f" in FP_RETURN, but where
6296    the structure itself has mode BLKmode.  */
6297 
6298 static rtx
mips_return_fpr_single(machine_mode type_mode,machine_mode value_mode)6299 mips_return_fpr_single (machine_mode type_mode,
6300 			machine_mode value_mode)
6301 {
6302   rtx x;
6303 
6304   x = gen_rtx_REG (value_mode, FP_RETURN);
6305   if (type_mode != value_mode)
6306     {
6307       x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
6308       x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
6309     }
6310   return x;
6311 }
6312 
6313 /* Return a composite value in a pair of floating-point registers.
6314    MODE1 and OFFSET1 are the mode and byte offset for the first value,
6315    likewise MODE2 and OFFSET2 for the second.  MODE is the mode of the
6316    complete value.
6317 
6318    For n32 & n64, $f0 always holds the first value and $f2 the second.
6319    Otherwise the values are packed together as closely as possible.  */
6320 
6321 static rtx
mips_return_fpr_pair(machine_mode mode,machine_mode mode1,HOST_WIDE_INT offset1,machine_mode mode2,HOST_WIDE_INT offset2)6322 mips_return_fpr_pair (machine_mode mode,
6323 		      machine_mode mode1, HOST_WIDE_INT offset1,
6324 		      machine_mode mode2, HOST_WIDE_INT offset2)
6325 {
6326   int inc;
6327 
6328   inc = (TARGET_NEWABI || mips_abi == ABI_32 ? 2 : MAX_FPRS_PER_FMT);
6329   return gen_rtx_PARALLEL
6330     (mode,
6331      gen_rtvec (2,
6332 		gen_rtx_EXPR_LIST (VOIDmode,
6333 				   gen_rtx_REG (mode1, FP_RETURN),
6334 				   GEN_INT (offset1)),
6335 		gen_rtx_EXPR_LIST (VOIDmode,
6336 				   gen_rtx_REG (mode2, FP_RETURN + inc),
6337 				   GEN_INT (offset2))));
6338 
6339 }
6340 
6341 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
6342    For normal calls, VALTYPE is the return type and MODE is VOIDmode.
6343    For libcalls, VALTYPE is null and MODE is the mode of the return value.  */
6344 
6345 static rtx
mips_function_value_1(const_tree valtype,const_tree fn_decl_or_type,machine_mode mode)6346 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
6347 		       machine_mode mode)
6348 {
6349   if (valtype)
6350     {
6351       tree fields[2];
6352       int unsigned_p;
6353       const_tree func;
6354 
6355       if (fn_decl_or_type && DECL_P (fn_decl_or_type))
6356 	func = fn_decl_or_type;
6357       else
6358 	func = NULL;
6359 
6360       mode = TYPE_MODE (valtype);
6361       unsigned_p = TYPE_UNSIGNED (valtype);
6362 
6363       /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
6364 	 return values, promote the mode here too.  */
6365       mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
6366 
6367       /* Handle structures whose fields are returned in $f0/$f2.  */
6368       switch (mips_fpr_return_fields (valtype, fields))
6369 	{
6370 	case 1:
6371 	  return mips_return_fpr_single (mode,
6372 					 TYPE_MODE (TREE_TYPE (fields[0])));
6373 
6374 	case 2:
6375 	  return mips_return_fpr_pair (mode,
6376 				       TYPE_MODE (TREE_TYPE (fields[0])),
6377 				       int_byte_position (fields[0]),
6378 				       TYPE_MODE (TREE_TYPE (fields[1])),
6379 				       int_byte_position (fields[1]));
6380 	}
6381 
6382       /* If a value is passed in the most significant part of a register, see
6383 	 whether we have to round the mode up to a whole number of words.  */
6384       if (mips_return_in_msb (valtype))
6385 	{
6386 	  HOST_WIDE_INT size = int_size_in_bytes (valtype);
6387 	  if (size % UNITS_PER_WORD != 0)
6388 	    {
6389 	      size += UNITS_PER_WORD - size % UNITS_PER_WORD;
6390 	      mode = int_mode_for_size (size * BITS_PER_UNIT, 0).require ();
6391 	    }
6392 	}
6393 
6394       /* For EABI, the class of return register depends entirely on MODE.
6395 	 For example, "struct { some_type x; }" and "union { some_type x; }"
6396 	 are returned in the same way as a bare "some_type" would be.
6397 	 Other ABIs only use FPRs for scalar, complex or vector types.  */
6398       if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
6399 	return gen_rtx_REG (mode, GP_RETURN);
6400     }
6401 
6402   if (!TARGET_MIPS16)
6403     {
6404       /* Handle long doubles for n32 & n64.  */
6405       if (mode == TFmode)
6406 	return mips_return_fpr_pair (mode,
6407 				     DImode, 0,
6408 				     DImode, GET_MODE_SIZE (mode) / 2);
6409 
6410       if (mips_return_mode_in_fpr_p (mode))
6411 	{
6412 	  if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
6413 	    return mips_return_fpr_pair (mode,
6414 					 GET_MODE_INNER (mode), 0,
6415 					 GET_MODE_INNER (mode),
6416 					 GET_MODE_SIZE (mode) / 2);
6417 	  else
6418 	    return gen_rtx_REG (mode, FP_RETURN);
6419 	}
6420     }
6421 
6422   return gen_rtx_REG (mode, GP_RETURN);
6423 }
6424 
6425 /* Implement TARGET_FUNCTION_VALUE.  */
6426 
6427 static rtx
mips_function_value(const_tree valtype,const_tree fn_decl_or_type,bool outgoing ATTRIBUTE_UNUSED)6428 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
6429 		     bool outgoing ATTRIBUTE_UNUSED)
6430 {
6431   return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
6432 }
6433 
6434 /* Implement TARGET_LIBCALL_VALUE.  */
6435 
6436 static rtx
mips_libcall_value(machine_mode mode,const_rtx fun ATTRIBUTE_UNUSED)6437 mips_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
6438 {
6439   return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
6440 }
6441 
6442 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
6443 
6444    On the MIPS, R2 R3 and F0 F2 are the only register thus used.  */
6445 
6446 static bool
mips_function_value_regno_p(const unsigned int regno)6447 mips_function_value_regno_p (const unsigned int regno)
6448 {
6449   /* Most types only require one GPR or one FPR for return values but for
6450      hard-float two FPRs can be used for _Complex types (for all ABIs)
6451      and long doubles (for n64).  */
6452   if (regno == GP_RETURN
6453       || regno == FP_RETURN
6454       || (FP_RETURN != GP_RETURN
6455 	  && regno == FP_RETURN + 2))
6456     return true;
6457 
6458   /* For o32 FP32, _Complex double will be returned in four 32-bit registers.
6459      This does not apply to o32 FPXX as floating-point function argument and
6460      return registers are described as 64-bit even though floating-point
6461      registers are primarily described as 32-bit internally.
6462      See: mips_get_reg_raw_mode.  */
6463   if ((mips_abi == ABI_32 && TARGET_FLOAT32)
6464       && FP_RETURN != GP_RETURN
6465       && (regno == FP_RETURN + 1
6466 	  || regno == FP_RETURN + 3))
6467     return true;
6468 
6469   return false;
6470 }
6471 
6472 /* Implement TARGET_RETURN_IN_MEMORY.  Under the o32 and o64 ABIs,
6473    all BLKmode objects are returned in memory.  Under the n32, n64
6474    and embedded ABIs, small structures are returned in a register.
6475    Objects with varying size must still be returned in memory, of
6476    course.  */
6477 
6478 static bool
mips_return_in_memory(const_tree type,const_tree fndecl ATTRIBUTE_UNUSED)6479 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
6480 {
6481   if (TARGET_OLDABI)
6482     /* Ensure that any floating point vector types are returned via memory
6483        even if they are supported through a vector mode with some ASEs.  */
6484     return (VECTOR_FLOAT_TYPE_P (type)
6485 	    || TYPE_MODE (type) == BLKmode);
6486 
6487   return (!IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
6488 }
6489 
6490 /* Implement TARGET_SETUP_INCOMING_VARARGS.  */
6491 
6492 static void
mips_setup_incoming_varargs(cumulative_args_t cum,machine_mode mode,tree type,int * pretend_size ATTRIBUTE_UNUSED,int no_rtl)6493 mips_setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
6494 			     tree type, int *pretend_size ATTRIBUTE_UNUSED,
6495 			     int no_rtl)
6496 {
6497   CUMULATIVE_ARGS local_cum;
6498   int gp_saved, fp_saved;
6499 
6500   /* The caller has advanced CUM up to, but not beyond, the last named
6501      argument.  Advance a local copy of CUM past the last "real" named
6502      argument, to find out how many registers are left over.  */
6503   local_cum = *get_cumulative_args (cum);
6504   mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
6505 			     true);
6506 
6507   /* Found out how many registers we need to save.  */
6508   gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
6509   fp_saved = (EABI_FLOAT_VARARGS_P
6510 	      ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
6511 	      : 0);
6512 
6513   if (!no_rtl)
6514     {
6515       if (gp_saved > 0)
6516 	{
6517 	  rtx ptr, mem;
6518 
6519 	  ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
6520 			       REG_PARM_STACK_SPACE (cfun->decl)
6521 			       - gp_saved * UNITS_PER_WORD);
6522 	  mem = gen_frame_mem (BLKmode, ptr);
6523 	  set_mem_alias_set (mem, get_varargs_alias_set ());
6524 
6525 	  move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
6526 			       mem, gp_saved);
6527 	}
6528       if (fp_saved > 0)
6529 	{
6530 	  /* We can't use move_block_from_reg, because it will use
6531 	     the wrong mode.  */
6532 	  machine_mode mode;
6533 	  int off, i;
6534 
6535 	  /* Set OFF to the offset from virtual_incoming_args_rtx of
6536 	     the first float register.  The FP save area lies below
6537 	     the integer one, and is aligned to UNITS_PER_FPVALUE bytes.  */
6538 	  off = ROUND_DOWN (-gp_saved * UNITS_PER_WORD, UNITS_PER_FPVALUE);
6539 	  off -= fp_saved * UNITS_PER_FPREG;
6540 
6541 	  mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
6542 
6543 	  for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
6544 	       i += MAX_FPRS_PER_FMT)
6545 	    {
6546 	      rtx ptr, mem;
6547 
6548 	      ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
6549 	      mem = gen_frame_mem (mode, ptr);
6550 	      set_mem_alias_set (mem, get_varargs_alias_set ());
6551 	      mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
6552 	      off += UNITS_PER_HWFPVALUE;
6553 	    }
6554 	}
6555     }
6556   if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
6557     cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
6558 				   + fp_saved * UNITS_PER_FPREG);
6559 }
6560 
6561 /* Implement TARGET_BUILTIN_VA_LIST.  */
6562 
6563 static tree
mips_build_builtin_va_list(void)6564 mips_build_builtin_va_list (void)
6565 {
6566   if (EABI_FLOAT_VARARGS_P)
6567     {
6568       /* We keep 3 pointers, and two offsets.
6569 
6570 	 Two pointers are to the overflow area, which starts at the CFA.
6571 	 One of these is constant, for addressing into the GPR save area
6572 	 below it.  The other is advanced up the stack through the
6573 	 overflow region.
6574 
6575 	 The third pointer is to the bottom of the GPR save area.
6576 	 Since the FPR save area is just below it, we can address
6577 	 FPR slots off this pointer.
6578 
6579 	 We also keep two one-byte offsets, which are to be subtracted
6580 	 from the constant pointers to yield addresses in the GPR and
6581 	 FPR save areas.  These are downcounted as float or non-float
6582 	 arguments are used, and when they get to zero, the argument
6583 	 must be obtained from the overflow region.  */
6584       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
6585       tree array, index;
6586 
6587       record = lang_hooks.types.make_type (RECORD_TYPE);
6588 
6589       f_ovfl = build_decl (BUILTINS_LOCATION,
6590 			   FIELD_DECL, get_identifier ("__overflow_argptr"),
6591 			   ptr_type_node);
6592       f_gtop = build_decl (BUILTINS_LOCATION,
6593 			   FIELD_DECL, get_identifier ("__gpr_top"),
6594 			   ptr_type_node);
6595       f_ftop = build_decl (BUILTINS_LOCATION,
6596 			   FIELD_DECL, get_identifier ("__fpr_top"),
6597 			   ptr_type_node);
6598       f_goff = build_decl (BUILTINS_LOCATION,
6599 			   FIELD_DECL, get_identifier ("__gpr_offset"),
6600 			   unsigned_char_type_node);
6601       f_foff = build_decl (BUILTINS_LOCATION,
6602 			   FIELD_DECL, get_identifier ("__fpr_offset"),
6603 			   unsigned_char_type_node);
6604       /* Explicitly pad to the size of a pointer, so that -Wpadded won't
6605 	 warn on every user file.  */
6606       index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
6607       array = build_array_type (unsigned_char_type_node,
6608 			        build_index_type (index));
6609       f_res = build_decl (BUILTINS_LOCATION,
6610 			  FIELD_DECL, get_identifier ("__reserved"), array);
6611 
6612       DECL_FIELD_CONTEXT (f_ovfl) = record;
6613       DECL_FIELD_CONTEXT (f_gtop) = record;
6614       DECL_FIELD_CONTEXT (f_ftop) = record;
6615       DECL_FIELD_CONTEXT (f_goff) = record;
6616       DECL_FIELD_CONTEXT (f_foff) = record;
6617       DECL_FIELD_CONTEXT (f_res) = record;
6618 
6619       TYPE_FIELDS (record) = f_ovfl;
6620       DECL_CHAIN (f_ovfl) = f_gtop;
6621       DECL_CHAIN (f_gtop) = f_ftop;
6622       DECL_CHAIN (f_ftop) = f_goff;
6623       DECL_CHAIN (f_goff) = f_foff;
6624       DECL_CHAIN (f_foff) = f_res;
6625 
6626       layout_type (record);
6627       return record;
6628     }
6629   else
6630     /* Otherwise, we use 'void *'.  */
6631     return ptr_type_node;
6632 }
6633 
6634 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
6635 
6636 static void
mips_va_start(tree valist,rtx nextarg)6637 mips_va_start (tree valist, rtx nextarg)
6638 {
6639   if (EABI_FLOAT_VARARGS_P)
6640     {
6641       const CUMULATIVE_ARGS *cum;
6642       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
6643       tree ovfl, gtop, ftop, goff, foff;
6644       tree t;
6645       int gpr_save_area_size;
6646       int fpr_save_area_size;
6647       int fpr_offset;
6648 
6649       cum = &crtl->args.info;
6650       gpr_save_area_size
6651 	= (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
6652       fpr_save_area_size
6653 	= (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
6654 
6655       f_ovfl = TYPE_FIELDS (va_list_type_node);
6656       f_gtop = DECL_CHAIN (f_ovfl);
6657       f_ftop = DECL_CHAIN (f_gtop);
6658       f_goff = DECL_CHAIN (f_ftop);
6659       f_foff = DECL_CHAIN (f_goff);
6660 
6661       ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6662 		     NULL_TREE);
6663       gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
6664 		     NULL_TREE);
6665       ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
6666 		     NULL_TREE);
6667       goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
6668 		     NULL_TREE);
6669       foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
6670 		     NULL_TREE);
6671 
6672       /* Emit code to initialize OVFL, which points to the next varargs
6673 	 stack argument.  CUM->STACK_WORDS gives the number of stack
6674 	 words used by named arguments.  */
6675       t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
6676       if (cum->stack_words > 0)
6677 	t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
6678       t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
6679       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6680 
6681       /* Emit code to initialize GTOP, the top of the GPR save area.  */
6682       t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
6683       t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
6684       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6685 
6686       /* Emit code to initialize FTOP, the top of the FPR save area.
6687 	 This address is gpr_save_area_bytes below GTOP, rounded
6688 	 down to the next fp-aligned boundary.  */
6689       t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
6690       fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
6691       fpr_offset &= -UNITS_PER_FPVALUE;
6692       if (fpr_offset)
6693 	t = fold_build_pointer_plus_hwi (t, -fpr_offset);
6694       t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
6695       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6696 
6697       /* Emit code to initialize GOFF, the offset from GTOP of the
6698 	 next GPR argument.  */
6699       t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
6700 		  build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
6701       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6702 
6703       /* Likewise emit code to initialize FOFF, the offset from FTOP
6704 	 of the next FPR argument.  */
6705       t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
6706 		  build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
6707       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6708     }
6709   else
6710     {
6711       nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
6712       std_expand_builtin_va_start (valist, nextarg);
6713     }
6714 }
6715 
6716 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
6717    types as well.  */
6718 
6719 static tree
mips_std_gimplify_va_arg_expr(tree valist,tree type,gimple_seq * pre_p,gimple_seq * post_p)6720 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6721 			       gimple_seq *post_p)
6722 {
6723   tree addr, t, type_size, rounded_size, valist_tmp;
6724   unsigned HOST_WIDE_INT align, boundary;
6725   bool indirect;
6726 
6727   indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
6728   if (indirect)
6729     type = build_pointer_type (type);
6730 
6731   align = PARM_BOUNDARY / BITS_PER_UNIT;
6732   boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
6733 
6734   /* When we align parameter on stack for caller, if the parameter
6735      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
6736      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
6737      here with caller.  */
6738   if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
6739     boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
6740 
6741   boundary /= BITS_PER_UNIT;
6742 
6743   /* Hoist the valist value into a temporary for the moment.  */
6744   valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
6745 
6746   /* va_list pointer is aligned to PARM_BOUNDARY.  If argument actually
6747      requires greater alignment, we must perform dynamic alignment.  */
6748   if (boundary > align)
6749     {
6750       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6751 		  fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
6752       gimplify_and_add (t, pre_p);
6753 
6754       t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6755 		  fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
6756 			       valist_tmp,
6757 			       build_int_cst (TREE_TYPE (valist), -boundary)));
6758       gimplify_and_add (t, pre_p);
6759     }
6760   else
6761     boundary = align;
6762 
6763   /* If the actual alignment is less than the alignment of the type,
6764      adjust the type accordingly so that we don't assume strict alignment
6765      when dereferencing the pointer.  */
6766   boundary *= BITS_PER_UNIT;
6767   if (boundary < TYPE_ALIGN (type))
6768     {
6769       type = build_variant_type_copy (type);
6770       SET_TYPE_ALIGN (type, boundary);
6771     }
6772 
6773   /* Compute the rounded size of the type.  */
6774   type_size = size_in_bytes (type);
6775   rounded_size = round_up (type_size, align);
6776 
6777   /* Reduce rounded_size so it's sharable with the postqueue.  */
6778   gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
6779 
6780   /* Get AP.  */
6781   addr = valist_tmp;
6782   if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
6783     {
6784       /* Small args are padded downward.  */
6785       t = fold_build2_loc (input_location, GT_EXPR, sizetype,
6786 		       rounded_size, size_int (align));
6787       t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
6788 		       size_binop (MINUS_EXPR, rounded_size, type_size));
6789       addr = fold_build_pointer_plus (addr, t);
6790     }
6791 
6792   /* Compute new value for AP.  */
6793   t = fold_build_pointer_plus (valist_tmp, rounded_size);
6794   t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
6795   gimplify_and_add (t, pre_p);
6796 
6797   addr = fold_convert (build_pointer_type (type), addr);
6798 
6799   if (indirect)
6800     addr = build_va_arg_indirect_ref (addr);
6801 
6802   return build_va_arg_indirect_ref (addr);
6803 }
6804 
6805 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.  */
6806 
6807 static tree
mips_gimplify_va_arg_expr(tree valist,tree type,gimple_seq * pre_p,gimple_seq * post_p)6808 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6809 			   gimple_seq *post_p)
6810 {
6811   tree addr;
6812   bool indirect_p;
6813 
6814   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
6815   if (indirect_p)
6816     type = build_pointer_type (type);
6817 
6818   if (!EABI_FLOAT_VARARGS_P)
6819     addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
6820   else
6821     {
6822       tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
6823       tree ovfl, top, off, align;
6824       HOST_WIDE_INT size, rsize, osize;
6825       tree t, u;
6826 
6827       f_ovfl = TYPE_FIELDS (va_list_type_node);
6828       f_gtop = DECL_CHAIN (f_ovfl);
6829       f_ftop = DECL_CHAIN (f_gtop);
6830       f_goff = DECL_CHAIN (f_ftop);
6831       f_foff = DECL_CHAIN (f_goff);
6832 
6833       /* Let:
6834 
6835 	 TOP be the top of the GPR or FPR save area;
6836 	 OFF be the offset from TOP of the next register;
6837 	 ADDR_RTX be the address of the argument;
6838 	 SIZE be the number of bytes in the argument type;
6839 	 RSIZE be the number of bytes used to store the argument
6840 	   when it's in the register save area; and
6841 	 OSIZE be the number of bytes used to store it when it's
6842 	   in the stack overflow area.
6843 
6844 	 The code we want is:
6845 
6846 	 1: off &= -rsize;	  // round down
6847 	 2: if (off != 0)
6848 	 3:   {
6849 	 4:	addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
6850 	 5:	off -= rsize;
6851 	 6:   }
6852 	 7: else
6853 	 8:   {
6854 	 9:	ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
6855 	 10:	addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
6856 	 11:	ovfl += osize;
6857 	 14:  }
6858 
6859 	 [1] and [9] can sometimes be optimized away.  */
6860 
6861       ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6862 		     NULL_TREE);
6863       size = int_size_in_bytes (type);
6864 
6865       if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
6866 	  && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
6867 	{
6868 	  top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
6869 			unshare_expr (valist), f_ftop, NULL_TREE);
6870 	  off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
6871 			unshare_expr (valist), f_foff, NULL_TREE);
6872 
6873 	  /* When va_start saves FPR arguments to the stack, each slot
6874 	     takes up UNITS_PER_HWFPVALUE bytes, regardless of the
6875 	     argument's precision.  */
6876 	  rsize = UNITS_PER_HWFPVALUE;
6877 
6878 	  /* Overflow arguments are padded to UNITS_PER_WORD bytes
6879 	     (= PARM_BOUNDARY bits).  This can be different from RSIZE
6880 	     in two cases:
6881 
6882 	     (1) On 32-bit targets when TYPE is a structure such as:
6883 
6884 	     struct s { float f; };
6885 
6886 	     Such structures are passed in paired FPRs, so RSIZE
6887 	     will be 8 bytes.  However, the structure only takes
6888 	     up 4 bytes of memory, so OSIZE will only be 4.
6889 
6890 	     (2) In combinations such as -mgp64 -msingle-float
6891 	     -fshort-double.  Doubles passed in registers will then take
6892 	     up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
6893 	     stack take up UNITS_PER_WORD bytes.  */
6894 	  osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
6895 	}
6896       else
6897 	{
6898 	  top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
6899 			unshare_expr (valist), f_gtop, NULL_TREE);
6900 	  off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
6901 			unshare_expr (valist), f_goff, NULL_TREE);
6902 	  rsize = ROUND_UP (size, UNITS_PER_WORD);
6903 	  if (rsize > UNITS_PER_WORD)
6904 	    {
6905 	      /* [1] Emit code for: off &= -rsize.	*/
6906 	      t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
6907 			  build_int_cst (TREE_TYPE (off), -rsize));
6908 	      gimplify_assign (unshare_expr (off), t, pre_p);
6909 	    }
6910 	  osize = rsize;
6911 	}
6912 
6913       /* [2] Emit code to branch if off == 0.  */
6914       t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
6915 		  build_int_cst (TREE_TYPE (off), 0));
6916       addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
6917 
6918       /* [5] Emit code for: off -= rsize.  We do this as a form of
6919 	 post-decrement not available to C.  */
6920       t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
6921       t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
6922 
6923       /* [4] Emit code for:
6924 	 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0).  */
6925       t = fold_convert (sizetype, t);
6926       t = fold_build1 (NEGATE_EXPR, sizetype, t);
6927       t = fold_build_pointer_plus (top, t);
6928       if (BYTES_BIG_ENDIAN && rsize > size)
6929 	t = fold_build_pointer_plus_hwi (t, rsize - size);
6930       COND_EXPR_THEN (addr) = t;
6931 
6932       if (osize > UNITS_PER_WORD)
6933 	{
6934 	  /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize.  */
6935 	  t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
6936 	  u = build_int_cst (TREE_TYPE (t), -osize);
6937 	  t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
6938 	  align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
6939 			  unshare_expr (ovfl), t);
6940 	}
6941       else
6942 	align = NULL;
6943 
6944       /* [10, 11] Emit code for:
6945 	 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
6946 	 ovfl += osize.  */
6947       u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
6948       t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
6949       if (BYTES_BIG_ENDIAN && osize > size)
6950 	t = fold_build_pointer_plus_hwi (t, osize - size);
6951 
6952       /* String [9] and [10, 11] together.  */
6953       if (align)
6954 	t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
6955       COND_EXPR_ELSE (addr) = t;
6956 
6957       addr = fold_convert (build_pointer_type (type), addr);
6958       addr = build_va_arg_indirect_ref (addr);
6959     }
6960 
6961   if (indirect_p)
6962     addr = build_va_arg_indirect_ref (addr);
6963 
6964   return addr;
6965 }
6966 
6967 /* Declare a unique, locally-binding function called NAME, then start
6968    its definition.  */
6969 
6970 static void
mips_start_unique_function(const char * name)6971 mips_start_unique_function (const char *name)
6972 {
6973   tree decl;
6974 
6975   decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
6976 		     get_identifier (name),
6977 		     build_function_type_list (void_type_node, NULL_TREE));
6978   DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
6979 				   NULL_TREE, void_type_node);
6980   TREE_PUBLIC (decl) = 1;
6981   TREE_STATIC (decl) = 1;
6982 
6983   cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
6984 
6985   targetm.asm_out.unique_section (decl, 0);
6986   switch_to_section (get_named_section (decl, NULL, 0));
6987 
6988   targetm.asm_out.globalize_label (asm_out_file, name);
6989   fputs ("\t.hidden\t", asm_out_file);
6990   assemble_name (asm_out_file, name);
6991   putc ('\n', asm_out_file);
6992 }
6993 
6994 /* Start a definition of function NAME.  MIPS16_P indicates whether the
6995    function contains MIPS16 code.  */
6996 
6997 static void
mips_start_function_definition(const char * name,bool mips16_p)6998 mips_start_function_definition (const char *name, bool mips16_p)
6999 {
7000   if (mips16_p)
7001     fprintf (asm_out_file, "\t.set\tmips16\n");
7002   else
7003     fprintf (asm_out_file, "\t.set\tnomips16\n");
7004 
7005   if (TARGET_MICROMIPS)
7006     fprintf (asm_out_file, "\t.set\tmicromips\n");
7007 #ifdef HAVE_GAS_MICROMIPS
7008   else
7009     fprintf (asm_out_file, "\t.set\tnomicromips\n");
7010 #endif
7011 
7012   if (!flag_inhibit_size_directive)
7013     {
7014       fputs ("\t.ent\t", asm_out_file);
7015       assemble_name (asm_out_file, name);
7016       fputs ("\n", asm_out_file);
7017     }
7018 
7019   ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
7020 
7021   /* Start the definition proper.  */
7022   assemble_name (asm_out_file, name);
7023   fputs (":\n", asm_out_file);
7024 }
7025 
7026 /* End a function definition started by mips_start_function_definition.  */
7027 
7028 static void
mips_end_function_definition(const char * name)7029 mips_end_function_definition (const char *name)
7030 {
7031   if (!flag_inhibit_size_directive)
7032     {
7033       fputs ("\t.end\t", asm_out_file);
7034       assemble_name (asm_out_file, name);
7035       fputs ("\n", asm_out_file);
7036     }
7037 }
7038 
7039 /* If *STUB_PTR points to a stub, output a comdat-style definition for it,
7040    then free *STUB_PTR.  */
7041 
7042 static void
mips_finish_stub(mips_one_only_stub ** stub_ptr)7043 mips_finish_stub (mips_one_only_stub **stub_ptr)
7044 {
7045   mips_one_only_stub *stub = *stub_ptr;
7046   if (!stub)
7047     return;
7048 
7049   const char *name = stub->get_name ();
7050   mips_start_unique_function (name);
7051   mips_start_function_definition (name, false);
7052   stub->output_body ();
7053   mips_end_function_definition (name);
7054   delete stub;
7055   *stub_ptr = 0;
7056 }
7057 
7058 /* Return true if calls to X can use R_MIPS_CALL* relocations.  */
7059 
7060 static bool
mips_ok_for_lazy_binding_p(rtx x)7061 mips_ok_for_lazy_binding_p (rtx x)
7062 {
7063   return (TARGET_USE_GOT
7064 	  && GET_CODE (x) == SYMBOL_REF
7065 	  && !SYMBOL_REF_BIND_NOW_P (x)
7066 	  && !mips_symbol_binds_local_p (x));
7067 }
7068 
7069 /* Load function address ADDR into register DEST.  TYPE is as for
7070    mips_expand_call.  Return true if we used an explicit lazy-binding
7071    sequence.  */
7072 
7073 static bool
mips_load_call_address(enum mips_call_type type,rtx dest,rtx addr)7074 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
7075 {
7076   /* If we're generating PIC, and this call is to a global function,
7077      try to allow its address to be resolved lazily.  This isn't
7078      possible for sibcalls when $gp is call-saved because the value
7079      of $gp on entry to the stub would be our caller's gp, not ours.  */
7080   if (TARGET_EXPLICIT_RELOCS
7081       && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
7082       && mips_ok_for_lazy_binding_p (addr))
7083     {
7084       addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
7085       emit_insn (gen_rtx_SET (dest, addr));
7086       return true;
7087     }
7088   else
7089     {
7090       mips_emit_move (dest, addr);
7091       return false;
7092     }
7093 }
7094 
7095 /* Each locally-defined hard-float MIPS16 function has a local symbol
7096    associated with it.  This hash table maps the function symbol (FUNC)
7097    to the local symbol (LOCAL). */
7098 static GTY (()) hash_map<nofree_string_hash, rtx> *mips16_local_aliases;
7099 
7100 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
7101    Return a local alias for it, creating a new one if necessary.  */
7102 
7103 static rtx
mips16_local_alias(rtx func)7104 mips16_local_alias (rtx func)
7105 {
7106   /* Create the hash table if this is the first call.  */
7107   if (mips16_local_aliases == NULL)
7108     mips16_local_aliases = hash_map<nofree_string_hash, rtx>::create_ggc (37);
7109 
7110   /* Look up the function symbol, creating a new entry if need be.  */
7111   bool existed;
7112   const char *func_name = XSTR (func, 0);
7113   rtx *slot = &mips16_local_aliases->get_or_insert (func_name, &existed);
7114   gcc_assert (slot != NULL);
7115 
7116   if (!existed)
7117     {
7118       rtx local;
7119 
7120       /* Create a new SYMBOL_REF for the local symbol.  The choice of
7121 	 __fn_local_* is based on the __fn_stub_* names that we've
7122 	 traditionally used for the non-MIPS16 stub.  */
7123       func_name = targetm.strip_name_encoding (XSTR (func, 0));
7124       const char *local_name = ACONCAT (("__fn_local_", func_name, NULL));
7125       local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
7126       SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
7127 
7128       /* Create a new structure to represent the mapping.  */
7129       *slot = local;
7130     }
7131   return *slot;
7132 }
7133 
7134 /* A chained list of functions for which mips16_build_call_stub has already
7135    generated a stub.  NAME is the name of the function and FP_RET_P is true
7136    if the function returns a value in floating-point registers.  */
7137 struct mips16_stub {
7138   struct mips16_stub *next;
7139   char *name;
7140   bool fp_ret_p;
7141 };
7142 static struct mips16_stub *mips16_stubs;
7143 
7144 /* Return the two-character string that identifies floating-point
7145    return mode MODE in the name of a MIPS16 function stub.  */
7146 
7147 static const char *
mips16_call_stub_mode_suffix(machine_mode mode)7148 mips16_call_stub_mode_suffix (machine_mode mode)
7149 {
7150   if (mode == SFmode)
7151     return "sf";
7152   else if (mode == DFmode)
7153     return "df";
7154   else if (mode == SCmode)
7155     return "sc";
7156   else if (mode == DCmode)
7157     return "dc";
7158   else if (mode == V2SFmode)
7159     {
7160       gcc_assert (TARGET_PAIRED_SINGLE_FLOAT);
7161       return "df";
7162     }
7163   else
7164     gcc_unreachable ();
7165 }
7166 
7167 /* Write instructions to move a 32-bit value between general register
7168    GPREG and floating-point register FPREG.  DIRECTION is 't' to move
7169    from GPREG to FPREG and 'f' to move in the opposite direction.  */
7170 
7171 static void
mips_output_32bit_xfer(char direction,unsigned int gpreg,unsigned int fpreg)7172 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
7173 {
7174   fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
7175 	   reg_names[gpreg], reg_names[fpreg]);
7176 }
7177 
7178 /* Likewise for 64-bit values.  */
7179 
7180 static void
mips_output_64bit_xfer(char direction,unsigned int gpreg,unsigned int fpreg)7181 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
7182 {
7183   if (TARGET_64BIT)
7184     fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
7185  	     reg_names[gpreg], reg_names[fpreg]);
7186   else if (ISA_HAS_MXHC1)
7187     {
7188       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
7189  	       reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
7190       fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
7191  	       reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
7192     }
7193   else if (TARGET_FLOATXX && direction == 't')
7194     {
7195       /* Use the argument save area to move via memory.  */
7196       fprintf (asm_out_file, "\tsw\t%s,0($sp)\n", reg_names[gpreg]);
7197       fprintf (asm_out_file, "\tsw\t%s,4($sp)\n", reg_names[gpreg + 1]);
7198       fprintf (asm_out_file, "\tldc1\t%s,0($sp)\n", reg_names[fpreg]);
7199     }
7200   else if (TARGET_FLOATXX && direction == 'f')
7201     {
7202       /* Use the argument save area to move via memory.  */
7203       fprintf (asm_out_file, "\tsdc1\t%s,0($sp)\n", reg_names[fpreg]);
7204       fprintf (asm_out_file, "\tlw\t%s,0($sp)\n", reg_names[gpreg]);
7205       fprintf (asm_out_file, "\tlw\t%s,4($sp)\n", reg_names[gpreg + 1]);
7206     }
7207   else
7208     {
7209       /* Move the least-significant word.  */
7210       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
7211 	       reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
7212       /* ...then the most significant word.  */
7213       fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
7214 	       reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
7215     }
7216 }
7217 
7218 /* Write out code to move floating-point arguments into or out of
7219    general registers.  FP_CODE is the code describing which arguments
7220    are present (see the comment above the definition of CUMULATIVE_ARGS
7221    in mips.h).  DIRECTION is as for mips_output_32bit_xfer.  */
7222 
7223 static void
mips_output_args_xfer(int fp_code,char direction)7224 mips_output_args_xfer (int fp_code, char direction)
7225 {
7226   unsigned int gparg, fparg, f;
7227   CUMULATIVE_ARGS cum;
7228 
7229   /* This code only works for o32 and o64.  */
7230   gcc_assert (TARGET_OLDABI);
7231 
7232   mips_init_cumulative_args (&cum, NULL);
7233 
7234   for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7235     {
7236       machine_mode mode;
7237       struct mips_arg_info info;
7238 
7239       if ((f & 3) == 1)
7240 	mode = SFmode;
7241       else if ((f & 3) == 2)
7242 	mode = DFmode;
7243       else
7244 	gcc_unreachable ();
7245 
7246       mips_get_arg_info (&info, &cum, mode, NULL, true);
7247       gparg = mips_arg_regno (&info, false);
7248       fparg = mips_arg_regno (&info, true);
7249 
7250       if (mode == SFmode)
7251 	mips_output_32bit_xfer (direction, gparg, fparg);
7252       else
7253 	mips_output_64bit_xfer (direction, gparg, fparg);
7254 
7255       mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
7256     }
7257 }
7258 
7259 /* Write a MIPS16 stub for the current function.  This stub is used
7260    for functions which take arguments in the floating-point registers.
7261    It is normal-mode code that moves the floating-point arguments
7262    into the general registers and then jumps to the MIPS16 code.  */
7263 
7264 static void
mips16_build_function_stub(void)7265 mips16_build_function_stub (void)
7266 {
7267   const char *fnname, *alias_name, *separator;
7268   char *secname, *stubname;
7269   tree stubdecl;
7270   unsigned int f;
7271   rtx symbol, alias;
7272 
7273   /* Create the name of the stub, and its unique section.  */
7274   symbol = XEXP (DECL_RTL (current_function_decl), 0);
7275   alias = mips16_local_alias (symbol);
7276 
7277   fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
7278   alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
7279   secname = ACONCAT ((".mips16.fn.", fnname, NULL));
7280   stubname = ACONCAT (("__fn_stub_", fnname, NULL));
7281 
7282   /* Build a decl for the stub.  */
7283   stubdecl = build_decl (BUILTINS_LOCATION,
7284 			 FUNCTION_DECL, get_identifier (stubname),
7285 			 build_function_type_list (void_type_node, NULL_TREE));
7286   set_decl_section_name (stubdecl, secname);
7287   DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
7288 				       RESULT_DECL, NULL_TREE, void_type_node);
7289 
7290   /* Output a comment.  */
7291   fprintf (asm_out_file, "\t# Stub function for %s (",
7292 	   current_function_name ());
7293   separator = "";
7294   for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
7295     {
7296       fprintf (asm_out_file, "%s%s", separator,
7297 	       (f & 3) == 1 ? "float" : "double");
7298       separator = ", ";
7299     }
7300   fprintf (asm_out_file, ")\n");
7301 
7302   /* Start the function definition.  */
7303   assemble_start_function (stubdecl, stubname);
7304   mips_start_function_definition (stubname, false);
7305 
7306   /* If generating pic2 code, either set up the global pointer or
7307      switch to pic0.  */
7308   if (TARGET_ABICALLS_PIC2)
7309     {
7310       if (TARGET_ABSOLUTE_ABICALLS)
7311 	fprintf (asm_out_file, "\t.option\tpic0\n");
7312       else
7313 	{
7314 	  output_asm_insn ("%(.cpload\t%^%)", NULL);
7315 	  /* Emit an R_MIPS_NONE relocation to tell the linker what the
7316 	     target function is.  Use a local GOT access when loading the
7317 	     symbol, to cut down on the number of unnecessary GOT entries
7318 	     for stubs that aren't needed.  */
7319 	  output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
7320 	  symbol = alias;
7321 	}
7322     }
7323 
7324   /* Load the address of the MIPS16 function into $25.  Do this first so
7325      that targets with coprocessor interlocks can use an MFC1 to fill the
7326      delay slot.  */
7327   output_asm_insn ("la\t%^,%0", &symbol);
7328 
7329   /* Move the arguments from floating-point registers to general registers.  */
7330   mips_output_args_xfer (crtl->args.info.fp_code, 'f');
7331 
7332   /* Jump to the MIPS16 function.  */
7333   output_asm_insn ("jr\t%^", NULL);
7334 
7335   if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
7336     fprintf (asm_out_file, "\t.option\tpic2\n");
7337 
7338   mips_end_function_definition (stubname);
7339 
7340   /* If the linker needs to create a dynamic symbol for the target
7341      function, it will associate the symbol with the stub (which,
7342      unlike the target function, follows the proper calling conventions).
7343      It is therefore useful to have a local alias for the target function,
7344      so that it can still be identified as MIPS16 code.  As an optimization,
7345      this symbol can also be used for indirect MIPS16 references from
7346      within this file.  */
7347   ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
7348 
7349   switch_to_section (function_section (current_function_decl));
7350 }
7351 
7352 /* The current function is a MIPS16 function that returns a value in an FPR.
7353    Copy the return value from its soft-float to its hard-float location.
7354    libgcc2 has special non-MIPS16 helper functions for each case.  */
7355 
7356 static void
mips16_copy_fpr_return_value(void)7357 mips16_copy_fpr_return_value (void)
7358 {
7359   rtx fn, insn, retval;
7360   tree return_type;
7361   machine_mode return_mode;
7362   const char *name;
7363 
7364   return_type = DECL_RESULT (current_function_decl);
7365   return_mode = DECL_MODE (return_type);
7366 
7367   name = ACONCAT (("__mips16_ret_",
7368 		   mips16_call_stub_mode_suffix (return_mode),
7369 		   NULL));
7370   fn = mips16_stub_function (name);
7371 
7372   /* The function takes arguments in $2 (and possibly $3), so calls
7373      to it cannot be lazily bound.  */
7374   SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
7375 
7376   /* Model the call as something that takes the GPR return value as
7377      argument and returns an "updated" value.  */
7378   retval = gen_rtx_REG (return_mode, GP_RETURN);
7379   insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
7380 			   const0_rtx, NULL_RTX, false);
7381   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
7382 }
7383 
7384 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
7385    RETVAL is the location of the return value, or null if this is
7386    a "call" rather than a "call_value".  ARGS_SIZE is the size of the
7387    arguments and FP_CODE is the code built by mips_function_arg;
7388    see the comment before the fp_code field in CUMULATIVE_ARGS for details.
7389 
7390    There are three alternatives:
7391 
7392    - If a stub was needed, emit the call and return the call insn itself.
7393 
7394    - If we can avoid using a stub by redirecting the call, set *FN_PTR
7395      to the new target and return null.
7396 
7397    - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
7398      unmodified.
7399 
7400    A stub is needed for calls to functions that, in normal mode,
7401    receive arguments in FPRs or return values in FPRs.  The stub
7402    copies the arguments from their soft-float positions to their
7403    hard-float positions, calls the real function, then copies the
7404    return value from its hard-float position to its soft-float
7405    position.
7406 
7407    We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
7408    If *FN_PTR turns out to be to a non-MIPS16 function, the linker
7409    automatically redirects the JAL to the stub, otherwise the JAL
7410    continues to call FN directly.  */
7411 
7412 static rtx_insn *
mips16_build_call_stub(rtx retval,rtx * fn_ptr,rtx args_size,int fp_code)7413 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
7414 {
7415   const char *fnname;
7416   bool fp_ret_p;
7417   struct mips16_stub *l;
7418   rtx_insn *insn;
7419   rtx pattern, fn;
7420 
7421   /* We don't need to do anything if we aren't in MIPS16 mode, or if
7422      we were invoked with the -msoft-float option.  */
7423   if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
7424     return NULL;
7425 
7426   /* Figure out whether the value might come back in a floating-point
7427      register.  */
7428   fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
7429 
7430   /* We don't need to do anything if there were no floating-point
7431      arguments and the value will not be returned in a floating-point
7432      register.  */
7433   if (fp_code == 0 && !fp_ret_p)
7434     return NULL;
7435 
7436   /* We don't need to do anything if this is a call to a special
7437      MIPS16 support function.  */
7438   fn = *fn_ptr;
7439   if (mips16_stub_function_p (fn))
7440     return NULL;
7441 
7442   /* If we're calling a locally-defined MIPS16 function, we know that
7443      it will return values in both the "soft-float" and "hard-float"
7444      registers.  There is no need to use a stub to move the latter
7445      to the former.  */
7446   if (fp_code == 0 && mips16_local_function_p (fn))
7447     return NULL;
7448 
7449   /* This code will only work for o32 and o64 abis.  The other ABI's
7450      require more sophisticated support.  */
7451   gcc_assert (TARGET_OLDABI);
7452 
7453   /* If we're calling via a function pointer, use one of the magic
7454      libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
7455      Each stub expects the function address to arrive in register $2.  */
7456   if (GET_CODE (fn) != SYMBOL_REF
7457       || !call_insn_operand (fn, VOIDmode))
7458     {
7459       char buf[32];
7460       rtx stub_fn, addr;
7461       rtx_insn *insn;
7462       bool lazy_p;
7463 
7464       /* If this is a locally-defined and locally-binding function,
7465 	 avoid the stub by calling the local alias directly.  */
7466       if (mips16_local_function_p (fn))
7467 	{
7468 	  *fn_ptr = mips16_local_alias (fn);
7469 	  return NULL;
7470 	}
7471 
7472       /* Create a SYMBOL_REF for the libgcc.a function.  */
7473       if (fp_ret_p)
7474 	sprintf (buf, "__mips16_call_stub_%s_%d",
7475 		 mips16_call_stub_mode_suffix (GET_MODE (retval)),
7476 		 fp_code);
7477       else
7478 	sprintf (buf, "__mips16_call_stub_%d", fp_code);
7479       stub_fn = mips16_stub_function (buf);
7480 
7481       /* The function uses $2 as an argument, so calls to it
7482 	 cannot be lazily bound.  */
7483       SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
7484 
7485       /* Load the target function into $2.  */
7486       addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
7487       lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
7488 
7489       /* Emit the call.  */
7490       insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
7491 			       args_size, NULL_RTX, lazy_p);
7492 
7493       /* Tell GCC that this call does indeed use the value of $2.  */
7494       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
7495 
7496       /* If we are handling a floating-point return value, we need to
7497          save $18 in the function prologue.  Putting a note on the
7498          call will mean that df_regs_ever_live_p ($18) will be true if the
7499          call is not eliminated, and we can check that in the prologue
7500          code.  */
7501       if (fp_ret_p)
7502 	CALL_INSN_FUNCTION_USAGE (insn) =
7503 	  gen_rtx_EXPR_LIST (VOIDmode,
7504 			     gen_rtx_CLOBBER (VOIDmode,
7505 					      gen_rtx_REG (word_mode, 18)),
7506 			     CALL_INSN_FUNCTION_USAGE (insn));
7507 
7508       return insn;
7509     }
7510 
7511   /* We know the function we are going to call.  If we have already
7512      built a stub, we don't need to do anything further.  */
7513   fnname = targetm.strip_name_encoding (XSTR (fn, 0));
7514   for (l = mips16_stubs; l != NULL; l = l->next)
7515     if (strcmp (l->name, fnname) == 0)
7516       break;
7517 
7518   if (l == NULL)
7519     {
7520       const char *separator;
7521       char *secname, *stubname;
7522       tree stubid, stubdecl;
7523       unsigned int f;
7524 
7525       /* If the function does not return in FPRs, the special stub
7526 	 section is named
7527 	     .mips16.call.FNNAME
7528 
7529 	 If the function does return in FPRs, the stub section is named
7530 	     .mips16.call.fp.FNNAME
7531 
7532 	 Build a decl for the stub.  */
7533       secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
7534 			  fnname, NULL));
7535       stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
7536 			   fnname, NULL));
7537       stubid = get_identifier (stubname);
7538       stubdecl = build_decl (BUILTINS_LOCATION,
7539 			     FUNCTION_DECL, stubid,
7540 			     build_function_type_list (void_type_node,
7541 						       NULL_TREE));
7542       set_decl_section_name (stubdecl, secname);
7543       DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
7544 					   RESULT_DECL, NULL_TREE,
7545 					   void_type_node);
7546 
7547       /* Output a comment.  */
7548       fprintf (asm_out_file, "\t# Stub function to call %s%s (",
7549 	       (fp_ret_p
7550 		? (GET_MODE (retval) == SFmode ? "float " : "double ")
7551 		: ""),
7552 	       fnname);
7553       separator = "";
7554       for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7555 	{
7556 	  fprintf (asm_out_file, "%s%s", separator,
7557 		   (f & 3) == 1 ? "float" : "double");
7558 	  separator = ", ";
7559 	}
7560       fprintf (asm_out_file, ")\n");
7561 
7562       /* Start the function definition.  */
7563       assemble_start_function (stubdecl, stubname);
7564       mips_start_function_definition (stubname, false);
7565 
7566       if (fp_ret_p)
7567 	{
7568 	  fprintf (asm_out_file, "\t.cfi_startproc\n");
7569 
7570 	  /* Create a fake CFA 4 bytes below the stack pointer.
7571 	     This works around unwinders (like libgcc's) that expect
7572 	     the CFA for non-signal frames to be unique.  */
7573 	  fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
7574 
7575 	  /* "Save" $sp in itself so we don't use the fake CFA.
7576 	     This is: DW_CFA_val_expression r29, { DW_OP_reg29 }.  */
7577 	  fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
7578 
7579 	  /* Save the return address in $18.  The stub's caller knows
7580 	     that $18 might be clobbered, even though $18 is usually
7581 	     a call-saved register.
7582 
7583 	     Do it early on in case the last move to a floating-point
7584 	     register can be scheduled into the delay slot of the
7585 	     call we are about to make.  */
7586 	  fprintf (asm_out_file, "\tmove\t%s,%s\n",
7587 		   reg_names[GP_REG_FIRST + 18],
7588 		   reg_names[RETURN_ADDR_REGNUM]);
7589 	}
7590       else
7591 	{
7592 	  /* Load the address of the MIPS16 function into $25.  Do this
7593 	     first so that targets with coprocessor interlocks can use
7594 	     an MFC1 to fill the delay slot.  */
7595 	  if (TARGET_EXPLICIT_RELOCS)
7596 	    {
7597 	      output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
7598 	      output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
7599 	    }
7600 	  else
7601 	    output_asm_insn ("la\t%^,%0", &fn);
7602 	}
7603 
7604       /* Move the arguments from general registers to floating-point
7605 	 registers.  */
7606       mips_output_args_xfer (fp_code, 't');
7607 
7608       if (fp_ret_p)
7609 	{
7610 	  /* Now call the non-MIPS16 function.  */
7611 	  output_asm_insn (mips_output_jump (&fn, 0, -1, true), &fn);
7612 	  fprintf (asm_out_file, "\t.cfi_register 31,18\n");
7613 
7614 	  /* Move the result from floating-point registers to
7615 	     general registers.  */
7616 	  switch (GET_MODE (retval))
7617 	    {
7618 	    case E_SCmode:
7619 	      mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
7620 				      TARGET_BIG_ENDIAN
7621 				      ? FP_REG_FIRST + 2
7622 				      : FP_REG_FIRST);
7623 	      mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
7624 				      TARGET_LITTLE_ENDIAN
7625 				      ? FP_REG_FIRST + 2
7626 				      : FP_REG_FIRST);
7627 	      if (GET_MODE (retval) == SCmode && TARGET_64BIT)
7628 		{
7629 		  /* On 64-bit targets, complex floats are returned in
7630 		     a single GPR, such that "sd" on a suitably-aligned
7631 		     target would store the value correctly.  */
7632 		  fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
7633 			   reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
7634 			   reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
7635 		  fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
7636 			   reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
7637 			   reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
7638 		  fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
7639 			   reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
7640 			   reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
7641 		  fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
7642 			   reg_names[GP_RETURN],
7643 			   reg_names[GP_RETURN],
7644 			   reg_names[GP_RETURN + 1]);
7645 		}
7646 	      break;
7647 
7648 	    case E_SFmode:
7649 	      mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
7650 	      break;
7651 
7652 	    case E_DCmode:
7653 	      mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
7654 				      FP_REG_FIRST + 2);
7655 	      /* FALLTHRU */
7656  	    case E_DFmode:
7657 	    case E_V2SFmode:
7658 	      gcc_assert (TARGET_PAIRED_SINGLE_FLOAT
7659 			  || GET_MODE (retval) != V2SFmode);
7660 	      mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
7661 	      break;
7662 
7663 	    default:
7664 	      gcc_unreachable ();
7665 	    }
7666 	  fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
7667 	  fprintf (asm_out_file, "\t.cfi_endproc\n");
7668 	}
7669       else
7670 	{
7671 	  /* Jump to the previously-loaded address.  */
7672 	  output_asm_insn ("jr\t%^", NULL);
7673 	}
7674 
7675 #ifdef ASM_DECLARE_FUNCTION_SIZE
7676       ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
7677 #endif
7678 
7679       mips_end_function_definition (stubname);
7680 
7681       /* Record this stub.  */
7682       l = XNEW (struct mips16_stub);
7683       l->name = xstrdup (fnname);
7684       l->fp_ret_p = fp_ret_p;
7685       l->next = mips16_stubs;
7686       mips16_stubs = l;
7687     }
7688 
7689   /* If we expect a floating-point return value, but we've built a
7690      stub which does not expect one, then we're in trouble.  We can't
7691      use the existing stub, because it won't handle the floating-point
7692      value.  We can't build a new stub, because the linker won't know
7693      which stub to use for the various calls in this object file.
7694      Fortunately, this case is illegal, since it means that a function
7695      was declared in two different ways in a single compilation.  */
7696   if (fp_ret_p && !l->fp_ret_p)
7697     error ("cannot handle inconsistent calls to %qs", fnname);
7698 
7699   if (retval == NULL_RTX)
7700     pattern = gen_call_internal_direct (fn, args_size);
7701   else
7702     pattern = gen_call_value_internal_direct (retval, fn, args_size);
7703   insn = mips_emit_call_insn (pattern, fn, fn, false);
7704 
7705   /* If we are calling a stub which handles a floating-point return
7706      value, we need to arrange to save $18 in the prologue.  We do this
7707      by marking the function call as using the register.  The prologue
7708      will later see that it is used, and emit code to save it.  */
7709   if (fp_ret_p)
7710     CALL_INSN_FUNCTION_USAGE (insn) =
7711       gen_rtx_EXPR_LIST (VOIDmode,
7712 			 gen_rtx_CLOBBER (VOIDmode,
7713 					  gen_rtx_REG (word_mode, 18)),
7714 			 CALL_INSN_FUNCTION_USAGE (insn));
7715 
7716   return insn;
7717 }
7718 
7719 /* Expand a call of type TYPE.  RESULT is where the result will go (null
7720    for "call"s and "sibcall"s), ADDR is the address of the function,
7721    ARGS_SIZE is the size of the arguments and AUX is the value passed
7722    to us by mips_function_arg.  LAZY_P is true if this call already
7723    involves a lazily-bound function address (such as when calling
7724    functions through a MIPS16 hard-float stub).
7725 
7726    Return the call itself.  */
7727 
7728 rtx_insn *
mips_expand_call(enum mips_call_type type,rtx result,rtx addr,rtx args_size,rtx aux,bool lazy_p)7729 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
7730 		  rtx args_size, rtx aux, bool lazy_p)
7731 {
7732   rtx orig_addr, pattern;
7733   rtx_insn *insn;
7734   int fp_code;
7735 
7736   fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
7737   insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
7738   if (insn)
7739     {
7740       gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
7741       return insn;
7742     }
7743 
7744   orig_addr = addr;
7745   if (!call_insn_operand (addr, VOIDmode))
7746     {
7747       if (type == MIPS_CALL_EPILOGUE)
7748 	addr = MIPS_EPILOGUE_TEMP (Pmode);
7749       else
7750 	addr = gen_reg_rtx (Pmode);
7751       lazy_p |= mips_load_call_address (type, addr, orig_addr);
7752     }
7753 
7754   if (result == 0)
7755     {
7756       rtx (*fn) (rtx, rtx);
7757 
7758       if (type == MIPS_CALL_SIBCALL)
7759 	fn = gen_sibcall_internal;
7760       else
7761 	fn = gen_call_internal;
7762 
7763       pattern = fn (addr, args_size);
7764     }
7765   else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
7766     {
7767       /* Handle return values created by mips_return_fpr_pair.  */
7768       rtx (*fn) (rtx, rtx, rtx, rtx);
7769       rtx reg1, reg2;
7770 
7771       if (type == MIPS_CALL_SIBCALL)
7772 	fn = gen_sibcall_value_multiple_internal;
7773       else
7774 	fn = gen_call_value_multiple_internal;
7775 
7776       reg1 = XEXP (XVECEXP (result, 0, 0), 0);
7777       reg2 = XEXP (XVECEXP (result, 0, 1), 0);
7778       pattern = fn (reg1, addr, args_size, reg2);
7779     }
7780   else
7781     {
7782       rtx (*fn) (rtx, rtx, rtx);
7783 
7784       if (type == MIPS_CALL_SIBCALL)
7785 	fn = gen_sibcall_value_internal;
7786       else
7787 	fn = gen_call_value_internal;
7788 
7789       /* Handle return values created by mips_return_fpr_single.  */
7790       if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
7791 	result = XEXP (XVECEXP (result, 0, 0), 0);
7792       pattern = fn (result, addr, args_size);
7793     }
7794 
7795   return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
7796 }
7797 
7798 /* Split call instruction INSN into a $gp-clobbering call and
7799    (where necessary) an instruction to restore $gp from its save slot.
7800    CALL_PATTERN is the pattern of the new call.  */
7801 
7802 void
mips_split_call(rtx insn,rtx call_pattern)7803 mips_split_call (rtx insn, rtx call_pattern)
7804 {
7805   emit_call_insn (call_pattern);
7806   if (!find_reg_note (insn, REG_NORETURN, 0))
7807     mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode,
7808 						      POST_CALL_TMP_REG));
7809 }
7810 
7811 /* Return true if a call to DECL may need to use JALX.  */
7812 
7813 static bool
mips_call_may_need_jalx_p(tree decl)7814 mips_call_may_need_jalx_p (tree decl)
7815 {
7816   /* If the current translation unit would use a different mode for DECL,
7817      assume that the call needs JALX.  */
7818   if (mips_get_compress_mode (decl) != TARGET_COMPRESSION)
7819     return true;
7820 
7821   /* mips_get_compress_mode is always accurate for locally-binding
7822      functions in the current translation unit.  */
7823   if (!DECL_EXTERNAL (decl) && targetm.binds_local_p (decl))
7824     return false;
7825 
7826   /* When -minterlink-compressed is in effect, assume that functions
7827      could use a different encoding mode unless an attribute explicitly
7828      tells us otherwise.  */
7829   if (TARGET_INTERLINK_COMPRESSED)
7830     {
7831       if (!TARGET_COMPRESSION
7832 	  && mips_get_compress_off_flags (DECL_ATTRIBUTES (decl)) ==0)
7833 	return true;
7834       if (TARGET_COMPRESSION
7835 	  && mips_get_compress_on_flags (DECL_ATTRIBUTES (decl)) == 0)
7836 	return true;
7837     }
7838 
7839   return false;
7840 }
7841 
7842 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL.  */
7843 
7844 static bool
mips_function_ok_for_sibcall(tree decl,tree exp ATTRIBUTE_UNUSED)7845 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
7846 {
7847   if (!TARGET_SIBCALLS)
7848     return false;
7849 
7850   /* Interrupt handlers need special epilogue code and therefore can't
7851      use sibcalls.  */
7852   if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
7853     return false;
7854 
7855   /* Direct Js are only possible to functions that use the same ISA encoding.
7856      There is no JX counterpoart of JALX.  */
7857   if (decl
7858       && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode)
7859       && mips_call_may_need_jalx_p (decl))
7860     return false;
7861 
7862   /* Sibling calls should not prevent lazy binding.  Lazy-binding stubs
7863      require $gp to be valid on entry, so sibcalls can only use stubs
7864      if $gp is call-clobbered.  */
7865   if (decl
7866       && TARGET_CALL_SAVED_GP
7867       && !TARGET_ABICALLS_PIC0
7868       && !targetm.binds_local_p (decl))
7869     return false;
7870 
7871   /* Otherwise OK.  */
7872   return true;
7873 }
7874 
7875 /* Implement TARGET_USE_MOVE_BY_PIECES_INFRASTRUCTURE_P.  */
7876 
7877 bool
mips_use_by_pieces_infrastructure_p(unsigned HOST_WIDE_INT size,unsigned int align,enum by_pieces_operation op,bool speed_p)7878 mips_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
7879 				     unsigned int align,
7880 				     enum by_pieces_operation op,
7881 				     bool speed_p)
7882 {
7883   if (op == STORE_BY_PIECES)
7884     return mips_store_by_pieces_p (size, align);
7885   if (op == MOVE_BY_PIECES && HAVE_movmemsi)
7886     {
7887       /* movmemsi is meant to generate code that is at least as good as
7888 	 move_by_pieces.  However, movmemsi effectively uses a by-pieces
7889 	 implementation both for moves smaller than a word and for
7890 	 word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
7891 	 bytes.  We should allow the tree-level optimisers to do such
7892 	 moves by pieces, as it often exposes other optimization
7893 	 opportunities.  We might as well continue to use movmemsi at
7894 	 the rtl level though, as it produces better code when
7895 	 scheduling is disabled (such as at -O).  */
7896       if (currently_expanding_to_rtl)
7897 	return false;
7898       if (align < BITS_PER_WORD)
7899 	return size < UNITS_PER_WORD;
7900       return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
7901     }
7902 
7903   return default_use_by_pieces_infrastructure_p (size, align, op, speed_p);
7904 }
7905 
7906 /* Implement a handler for STORE_BY_PIECES operations
7907    for TARGET_USE_MOVE_BY_PIECES_INFRASTRUCTURE_P.  */
7908 
7909 bool
mips_store_by_pieces_p(unsigned HOST_WIDE_INT size,unsigned int align)7910 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7911 {
7912   /* Storing by pieces involves moving constants into registers
7913      of size MIN (ALIGN, BITS_PER_WORD), then storing them.
7914      We need to decide whether it is cheaper to load the address of
7915      constant data into a register and use a block move instead.  */
7916 
7917   /* If the data is only byte aligned, then:
7918 
7919      (a1) A block move of less than 4 bytes would involve three 3 LBs and
7920 	  3 SBs.  We might as well use 3 single-instruction LIs and 3 SBs
7921 	  instead.
7922 
7923      (a2) A block move of 4 bytes from aligned source data can use an
7924 	  LW/SWL/SWR sequence.  This is often better than the 4 LIs and
7925 	  4 SBs that we would generate when storing by pieces.  */
7926   if (align <= BITS_PER_UNIT)
7927     return size < 4;
7928 
7929   /* If the data is 2-byte aligned, then:
7930 
7931      (b1) A block move of less than 4 bytes would use a combination of LBs,
7932 	  LHs, SBs and SHs.  We get better code by using single-instruction
7933 	  LIs, SBs and SHs instead.
7934 
7935      (b2) A block move of 4 bytes from aligned source data would again use
7936 	  an LW/SWL/SWR sequence.  In most cases, loading the address of
7937 	  the source data would require at least one extra instruction.
7938 	  It is often more efficient to use 2 single-instruction LIs and
7939 	  2 SHs instead.
7940 
7941      (b3) A block move of up to 3 additional bytes would be like (b1).
7942 
7943      (b4) A block move of 8 bytes from aligned source data can use two
7944 	  LW/SWL/SWR sequences or a single LD/SDL/SDR sequence.  Both
7945 	  sequences are better than the 4 LIs and 4 SHs that we'd generate
7946 	  when storing by pieces.
7947 
7948      The reasoning for higher alignments is similar:
7949 
7950      (c1) A block move of less than 4 bytes would be the same as (b1).
7951 
7952      (c2) A block move of 4 bytes would use an LW/SW sequence.  Again,
7953 	  loading the address of the source data would typically require
7954 	  at least one extra instruction.  It is generally better to use
7955 	  LUI/ORI/SW instead.
7956 
7957      (c3) A block move of up to 3 additional bytes would be like (b1).
7958 
7959      (c4) A block move of 8 bytes can use two LW/SW sequences or a single
7960 	  LD/SD sequence, and in these cases we've traditionally preferred
7961 	  the memory copy over the more bulky constant moves.  */
7962   return size < 8;
7963 }
7964 
7965 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
7966    Assume that the areas do not overlap.  */
7967 
7968 static void
mips_block_move_straight(rtx dest,rtx src,HOST_WIDE_INT length)7969 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
7970 {
7971   HOST_WIDE_INT offset, delta;
7972   unsigned HOST_WIDE_INT bits;
7973   int i;
7974   machine_mode mode;
7975   rtx *regs;
7976 
7977   /* Work out how many bits to move at a time.  If both operands have
7978      half-word alignment, it is usually better to move in half words.
7979      For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
7980      and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
7981      Otherwise move word-sized chunks.
7982 
7983      For ISA_HAS_LWL_LWR we rely on the lwl/lwr & swl/swr load. Otherwise
7984      picking the minimum of alignment or BITS_PER_WORD gets us the
7985      desired size for bits.  */
7986 
7987   if (!ISA_HAS_LWL_LWR)
7988     bits = MIN (BITS_PER_WORD, MIN (MEM_ALIGN (src), MEM_ALIGN (dest)));
7989   else
7990     {
7991       if (MEM_ALIGN (src) == BITS_PER_WORD / 2
7992 	  && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
7993 	bits = BITS_PER_WORD / 2;
7994       else
7995 	bits = BITS_PER_WORD;
7996     }
7997 
7998   mode = int_mode_for_size (bits, 0).require ();
7999   delta = bits / BITS_PER_UNIT;
8000 
8001   /* Allocate a buffer for the temporary registers.  */
8002   regs = XALLOCAVEC (rtx, length / delta);
8003 
8004   /* Load as many BITS-sized chunks as possible.  Use a normal load if
8005      the source has enough alignment, otherwise use left/right pairs.  */
8006   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
8007     {
8008       regs[i] = gen_reg_rtx (mode);
8009       if (MEM_ALIGN (src) >= bits)
8010 	mips_emit_move (regs[i], adjust_address (src, mode, offset));
8011       else
8012 	{
8013 	  rtx part = adjust_address (src, BLKmode, offset);
8014 	  set_mem_size (part, delta);
8015 	  if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
8016 	    gcc_unreachable ();
8017 	}
8018     }
8019 
8020   /* Copy the chunks to the destination.  */
8021   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
8022     if (MEM_ALIGN (dest) >= bits)
8023       mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
8024     else
8025       {
8026 	rtx part = adjust_address (dest, BLKmode, offset);
8027 	set_mem_size (part, delta);
8028 	if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
8029 	  gcc_unreachable ();
8030       }
8031 
8032   /* Mop up any left-over bytes.  */
8033   if (offset < length)
8034     {
8035       src = adjust_address (src, BLKmode, offset);
8036       dest = adjust_address (dest, BLKmode, offset);
8037       move_by_pieces (dest, src, length - offset,
8038 		      MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
8039     }
8040 }
8041 
8042 /* Helper function for doing a loop-based block operation on memory
8043    reference MEM.  Each iteration of the loop will operate on LENGTH
8044    bytes of MEM.
8045 
8046    Create a new base register for use within the loop and point it to
8047    the start of MEM.  Create a new memory reference that uses this
8048    register.  Store them in *LOOP_REG and *LOOP_MEM respectively.  */
8049 
8050 static void
mips_adjust_block_mem(rtx mem,HOST_WIDE_INT length,rtx * loop_reg,rtx * loop_mem)8051 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
8052 		       rtx *loop_reg, rtx *loop_mem)
8053 {
8054   *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
8055 
8056   /* Although the new mem does not refer to a known location,
8057      it does keep up to LENGTH bytes of alignment.  */
8058   *loop_mem = change_address (mem, BLKmode, *loop_reg);
8059   set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
8060 }
8061 
8062 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
8063    bytes at a time.  LENGTH must be at least BYTES_PER_ITER.  Assume that
8064    the memory regions do not overlap.  */
8065 
8066 static void
mips_block_move_loop(rtx dest,rtx src,HOST_WIDE_INT length,HOST_WIDE_INT bytes_per_iter)8067 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
8068 		      HOST_WIDE_INT bytes_per_iter)
8069 {
8070   rtx_code_label *label;
8071   rtx src_reg, dest_reg, final_src, test;
8072   HOST_WIDE_INT leftover;
8073 
8074   leftover = length % bytes_per_iter;
8075   length -= leftover;
8076 
8077   /* Create registers and memory references for use within the loop.  */
8078   mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
8079   mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
8080 
8081   /* Calculate the value that SRC_REG should have after the last iteration
8082      of the loop.  */
8083   final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
8084 				   0, 0, OPTAB_WIDEN);
8085 
8086   /* Emit the start of the loop.  */
8087   label = gen_label_rtx ();
8088   emit_label (label);
8089 
8090   /* Emit the loop body.  */
8091   mips_block_move_straight (dest, src, bytes_per_iter);
8092 
8093   /* Move on to the next block.  */
8094   mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
8095   mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
8096 
8097   /* Emit the loop condition.  */
8098   test = gen_rtx_NE (VOIDmode, src_reg, final_src);
8099   if (Pmode == DImode)
8100     emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
8101   else
8102     emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
8103 
8104   /* Mop up any left-over bytes.  */
8105   if (leftover)
8106     mips_block_move_straight (dest, src, leftover);
8107   else
8108     /* Temporary fix for PR79150.  */
8109     emit_insn (gen_nop ());
8110 }
8111 
8112 /* Expand a movmemsi instruction, which copies LENGTH bytes from
8113    memory reference SRC to memory reference DEST.  */
8114 
8115 bool
mips_expand_block_move(rtx dest,rtx src,rtx length)8116 mips_expand_block_move (rtx dest, rtx src, rtx length)
8117 {
8118   if (!ISA_HAS_LWL_LWR
8119       && (MEM_ALIGN (src) < MIPS_MIN_MOVE_MEM_ALIGN
8120 	  || MEM_ALIGN (dest) < MIPS_MIN_MOVE_MEM_ALIGN))
8121     return false;
8122 
8123   if (CONST_INT_P (length))
8124     {
8125       if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
8126 	{
8127 	  mips_block_move_straight (dest, src, INTVAL (length));
8128 	  return true;
8129 	}
8130       else if (optimize)
8131 	{
8132 	  mips_block_move_loop (dest, src, INTVAL (length),
8133 				MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
8134 	  return true;
8135 	}
8136     }
8137   return false;
8138 }
8139 
8140 /* Expand a loop of synci insns for the address range [BEGIN, END).  */
8141 
8142 void
mips_expand_synci_loop(rtx begin,rtx end)8143 mips_expand_synci_loop (rtx begin, rtx end)
8144 {
8145   rtx inc, cmp_result, mask, length;
8146   rtx_code_label *label, *end_label;
8147 
8148   /* Create end_label.  */
8149   end_label = gen_label_rtx ();
8150 
8151   /* Check if begin equals end.  */
8152   cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
8153   emit_jump_insn (gen_condjump (cmp_result, end_label));
8154 
8155   /* Load INC with the cache line size (rdhwr INC,$1).  */
8156   inc = gen_reg_rtx (Pmode);
8157   emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
8158 
8159   /* Check if inc is 0.  */
8160   cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
8161   emit_jump_insn (gen_condjump (cmp_result, end_label));
8162 
8163   /* Calculate mask.  */
8164   mask = mips_force_unary (Pmode, NEG, inc);
8165 
8166   /* Mask out begin by mask.  */
8167   begin = mips_force_binary (Pmode, AND, begin, mask);
8168 
8169   /* Calculate length.  */
8170   length = mips_force_binary (Pmode, MINUS, end, begin);
8171 
8172   /* Loop back to here.  */
8173     label = gen_label_rtx ();
8174   emit_label (label);
8175 
8176   emit_insn (gen_synci (begin));
8177 
8178   /* Update length.  */
8179   mips_emit_binary (MINUS, length, length, inc);
8180 
8181   /* Update begin.  */
8182   mips_emit_binary (PLUS, begin, begin, inc);
8183 
8184   /* Check if length is greater than 0.  */
8185   cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
8186   emit_jump_insn (gen_condjump (cmp_result, label));
8187 
8188   emit_label (end_label);
8189 }
8190 
8191 /* Expand a QI or HI mode atomic memory operation.
8192 
8193    GENERATOR contains a pointer to the gen_* function that generates
8194    the SI mode underlying atomic operation using masks that we
8195    calculate.
8196 
8197    RESULT is the return register for the operation.  Its value is NULL
8198    if unused.
8199 
8200    MEM is the location of the atomic access.
8201 
8202    OLDVAL is the first operand for the operation.
8203 
8204    NEWVAL is the optional second operand for the operation.  Its value
8205    is NULL if unused.  */
8206 
8207 void
mips_expand_atomic_qihi(union mips_gen_fn_ptrs generator,rtx result,rtx mem,rtx oldval,rtx newval)8208 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
8209                          rtx result, rtx mem, rtx oldval, rtx newval)
8210 {
8211   rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
8212   rtx unshifted_mask_reg, mask, inverted_mask, si_op;
8213   rtx res = NULL;
8214   machine_mode mode;
8215 
8216   mode = GET_MODE (mem);
8217 
8218   /* Compute the address of the containing SImode value.  */
8219   orig_addr = force_reg (Pmode, XEXP (mem, 0));
8220   memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
8221 				  force_reg (Pmode, GEN_INT (-4)));
8222 
8223   /* Create a memory reference for it.  */
8224   memsi = gen_rtx_MEM (SImode, memsi_addr);
8225   set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
8226   MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
8227 
8228   /* Work out the byte offset of the QImode or HImode value,
8229      counting from the least significant byte.  */
8230   shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
8231   if (TARGET_BIG_ENDIAN)
8232     mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
8233 
8234   /* Multiply by eight to convert the shift value from bytes to bits.  */
8235   mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
8236 
8237   /* Make the final shift an SImode value, so that it can be used in
8238      SImode operations.  */
8239   shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
8240 
8241   /* Set MASK to an inclusive mask of the QImode or HImode value.  */
8242   unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
8243   unshifted_mask_reg = force_reg (SImode, unshifted_mask);
8244   mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
8245 
8246   /* Compute the equivalent exclusive mask.  */
8247   inverted_mask = gen_reg_rtx (SImode);
8248   emit_insn (gen_rtx_SET (inverted_mask, gen_rtx_NOT (SImode, mask)));
8249 
8250   /* Shift the old value into place.  */
8251   if (oldval != const0_rtx)
8252     {
8253       oldval = convert_modes (SImode, mode, oldval, true);
8254       oldval = force_reg (SImode, oldval);
8255       oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
8256     }
8257 
8258   /* Do the same for the new value.  */
8259   if (newval && newval != const0_rtx)
8260     {
8261       newval = convert_modes (SImode, mode, newval, true);
8262       newval = force_reg (SImode, newval);
8263       newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
8264     }
8265 
8266   /* Do the SImode atomic access.  */
8267   if (result)
8268     res = gen_reg_rtx (SImode);
8269   if (newval)
8270     si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
8271   else if (result)
8272     si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
8273   else
8274     si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
8275 
8276   emit_insn (si_op);
8277 
8278   if (result)
8279     {
8280       /* Shift and convert the result.  */
8281       mips_emit_binary (AND, res, res, mask);
8282       mips_emit_binary (LSHIFTRT, res, res, shiftsi);
8283       mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
8284     }
8285 }
8286 
8287 /* Return true if it is possible to use left/right accesses for a
8288    bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
8289    When returning true, update *LEFT and *RIGHT as follows:
8290 
8291    *LEFT is a QImode reference to the first byte if big endian or
8292    the last byte if little endian.  This address can be used in the
8293    left-side instructions (LWL, SWL, LDL, SDL).
8294 
8295    *RIGHT is a QImode reference to the opposite end of the field and
8296    can be used in the patterning right-side instruction.  */
8297 
8298 static bool
mips_get_unaligned_mem(rtx op,HOST_WIDE_INT width,HOST_WIDE_INT bitpos,rtx * left,rtx * right)8299 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
8300 			rtx *left, rtx *right)
8301 {
8302   rtx first, last;
8303 
8304   /* Check that the size is valid.  */
8305   if (width != 32 && (!TARGET_64BIT || width != 64))
8306     return false;
8307 
8308   /* We can only access byte-aligned values.  Since we are always passed
8309      a reference to the first byte of the field, it is not necessary to
8310      do anything with BITPOS after this check.  */
8311   if (bitpos % BITS_PER_UNIT != 0)
8312     return false;
8313 
8314   /* Reject aligned bitfields: we want to use a normal load or store
8315      instead of a left/right pair.  */
8316   if (MEM_ALIGN (op) >= width)
8317     return false;
8318 
8319   /* Get references to both ends of the field.  */
8320   first = adjust_address (op, QImode, 0);
8321   last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
8322 
8323   /* Allocate to LEFT and RIGHT according to endianness.  LEFT should
8324      correspond to the MSB and RIGHT to the LSB.  */
8325   if (TARGET_BIG_ENDIAN)
8326     *left = first, *right = last;
8327   else
8328     *left = last, *right = first;
8329 
8330   return true;
8331 }
8332 
8333 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
8334    DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
8335    the operation is the equivalent of:
8336 
8337       (set DEST (*_extract SRC WIDTH BITPOS))
8338 
8339    Return true on success.  */
8340 
8341 bool
mips_expand_ext_as_unaligned_load(rtx dest,rtx src,HOST_WIDE_INT width,HOST_WIDE_INT bitpos,bool unsigned_p)8342 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
8343 				   HOST_WIDE_INT bitpos, bool unsigned_p)
8344 {
8345   rtx left, right, temp;
8346   rtx dest1 = NULL_RTX;
8347 
8348   /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
8349      be a DImode, create a new temp and emit a zero extend at the end.  */
8350   if (GET_MODE (dest) == DImode
8351       && REG_P (dest)
8352       && GET_MODE_BITSIZE (SImode) == width)
8353     {
8354       dest1 = dest;
8355       dest = gen_reg_rtx (SImode);
8356     }
8357 
8358   if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
8359     return false;
8360 
8361   temp = gen_reg_rtx (GET_MODE (dest));
8362   if (GET_MODE (dest) == DImode)
8363     {
8364       emit_insn (gen_mov_ldl (temp, src, left));
8365       emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
8366     }
8367   else
8368     {
8369       emit_insn (gen_mov_lwl (temp, src, left));
8370       emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
8371     }
8372 
8373   /* If we were loading 32bits and the original register was DI then
8374      sign/zero extend into the orignal dest.  */
8375   if (dest1)
8376     {
8377       if (unsigned_p)
8378         emit_insn (gen_zero_extendsidi2 (dest1, dest));
8379       else
8380         emit_insn (gen_extendsidi2 (dest1, dest));
8381     }
8382   return true;
8383 }
8384 
8385 /* Try to use left/right stores to expand an "ins" pattern.  DEST, WIDTH,
8386    BITPOS and SRC are the operands passed to the expander; the operation
8387    is the equivalent of:
8388 
8389        (set (zero_extract DEST WIDTH BITPOS) SRC)
8390 
8391    Return true on success.  */
8392 
8393 bool
mips_expand_ins_as_unaligned_store(rtx dest,rtx src,HOST_WIDE_INT width,HOST_WIDE_INT bitpos)8394 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
8395 				    HOST_WIDE_INT bitpos)
8396 {
8397   rtx left, right;
8398   machine_mode mode;
8399 
8400   if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
8401     return false;
8402 
8403   mode = int_mode_for_size (width, 0).require ();
8404   src = gen_lowpart (mode, src);
8405   if (mode == DImode)
8406     {
8407       emit_insn (gen_mov_sdl (dest, src, left));
8408       emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
8409     }
8410   else
8411     {
8412       emit_insn (gen_mov_swl (dest, src, left));
8413       emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
8414     }
8415   return true;
8416 }
8417 
8418 /* Return true if X is a MEM with the same size as MODE.  */
8419 
8420 bool
mips_mem_fits_mode_p(machine_mode mode,rtx x)8421 mips_mem_fits_mode_p (machine_mode mode, rtx x)
8422 {
8423   return (MEM_P (x)
8424 	  && MEM_SIZE_KNOWN_P (x)
8425 	  && MEM_SIZE (x) == GET_MODE_SIZE (mode));
8426 }
8427 
8428 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
8429    source of an "ext" instruction or the destination of an "ins"
8430    instruction.  OP must be a register operand and the following
8431    conditions must hold:
8432 
8433      0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
8434      0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
8435      0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
8436 
8437    Also reject lengths equal to a word as they are better handled
8438    by the move patterns.  */
8439 
8440 bool
mips_use_ins_ext_p(rtx op,HOST_WIDE_INT width,HOST_WIDE_INT bitpos)8441 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
8442 {
8443   if (!ISA_HAS_EXT_INS
8444       || !register_operand (op, VOIDmode)
8445       || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
8446     return false;
8447 
8448   if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
8449     return false;
8450 
8451   if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
8452     return false;
8453 
8454   return true;
8455 }
8456 
8457 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
8458    operation if MAXLEN is the maxium length of consecutive bits that
8459    can make up MASK.  MODE is the mode of the operation.  See
8460    mask_low_and_shift_len for the actual definition.  */
8461 
8462 bool
mask_low_and_shift_p(machine_mode mode,rtx mask,rtx shift,int maxlen)8463 mask_low_and_shift_p (machine_mode mode, rtx mask, rtx shift, int maxlen)
8464 {
8465   return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
8466 }
8467 
8468 /* Return true iff OP1 and OP2 are valid operands together for the
8469    *and<MODE>3 and *and<MODE>3_mips16 patterns.  For the cases to consider,
8470    see the table in the comment before the pattern.  */
8471 
8472 bool
and_operands_ok(machine_mode mode,rtx op1,rtx op2)8473 and_operands_ok (machine_mode mode, rtx op1, rtx op2)
8474 {
8475 
8476   if (memory_operand (op1, mode))
8477     {
8478       if (TARGET_MIPS16) {
8479 	struct mips_address_info addr;
8480 	if (!mips_classify_address (&addr, op1, mode, false))
8481 	  return false;
8482       }
8483       return and_load_operand (op2, mode);
8484     }
8485   else
8486     return and_reg_operand (op2, mode);
8487 }
8488 
8489 /* The canonical form of a mask-low-and-shift-left operation is
8490    (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
8491    cleared.  Thus we need to shift MASK to the right before checking if it
8492    is a valid mask value.  MODE is the mode of the operation.  If true
8493    return the length of the mask, otherwise return -1.  */
8494 
8495 int
mask_low_and_shift_len(machine_mode mode,rtx mask,rtx shift)8496 mask_low_and_shift_len (machine_mode mode, rtx mask, rtx shift)
8497 {
8498   HOST_WIDE_INT shval;
8499 
8500   shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
8501   return exact_log2 ((UINTVAL (mask) >> shval) + 1);
8502 }
8503 
8504 /* Return true if -msplit-addresses is selected and should be honored.
8505 
8506    -msplit-addresses is a half-way house between explicit relocations
8507    and the traditional assembler macros.  It can split absolute 32-bit
8508    symbolic constants into a high/lo_sum pair but uses macros for other
8509    sorts of access.
8510 
8511    Like explicit relocation support for REL targets, it relies
8512    on GNU extensions in the assembler and the linker.
8513 
8514    Although this code should work for -O0, it has traditionally
8515    been treated as an optimization.  */
8516 
8517 static bool
mips_split_addresses_p(void)8518 mips_split_addresses_p (void)
8519 {
8520   return (TARGET_SPLIT_ADDRESSES
8521 	  && optimize
8522 	  && !TARGET_MIPS16
8523 	  && !flag_pic
8524 	  && !ABI_HAS_64BIT_SYMBOLS);
8525 }
8526 
8527 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs.  */
8528 
8529 static void
mips_init_relocs(void)8530 mips_init_relocs (void)
8531 {
8532   memset (mips_split_p, '\0', sizeof (mips_split_p));
8533   memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
8534   memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
8535   memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
8536   memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
8537 
8538   if (TARGET_MIPS16_PCREL_LOADS)
8539     mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
8540   else
8541     {
8542       if (ABI_HAS_64BIT_SYMBOLS)
8543 	{
8544 	  if (TARGET_EXPLICIT_RELOCS)
8545 	    {
8546 	      mips_split_p[SYMBOL_64_HIGH] = true;
8547 	      mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
8548 	      mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
8549 
8550 	      mips_split_p[SYMBOL_64_MID] = true;
8551 	      mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
8552 	      mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
8553 
8554 	      mips_split_p[SYMBOL_64_LOW] = true;
8555 	      mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
8556 	      mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
8557 
8558 	      mips_split_p[SYMBOL_ABSOLUTE] = true;
8559 	      mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
8560 	    }
8561 	}
8562       else
8563 	{
8564 	  if (TARGET_EXPLICIT_RELOCS
8565 	      || mips_split_addresses_p ()
8566 	      || TARGET_MIPS16)
8567 	    {
8568 	      mips_split_p[SYMBOL_ABSOLUTE] = true;
8569 	      mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
8570 	      mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
8571 	    }
8572 	}
8573     }
8574 
8575   if (TARGET_MIPS16)
8576     {
8577       /* The high part is provided by a pseudo copy of $gp.  */
8578       mips_split_p[SYMBOL_GP_RELATIVE] = true;
8579       mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
8580     }
8581   else if (TARGET_EXPLICIT_RELOCS)
8582     /* Small data constants are kept whole until after reload,
8583        then lowered by mips_rewrite_small_data.  */
8584     mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
8585 
8586   if (TARGET_EXPLICIT_RELOCS)
8587     {
8588       mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
8589       if (TARGET_NEWABI)
8590 	{
8591 	  mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
8592 	  mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
8593 	}
8594       else
8595 	{
8596 	  mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
8597 	  mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
8598 	}
8599       if (TARGET_MIPS16)
8600 	/* Expose the use of $28 as soon as possible.  */
8601 	mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
8602 
8603       if (TARGET_XGOT)
8604 	{
8605 	  /* The HIGH and LO_SUM are matched by special .md patterns.  */
8606 	  mips_split_p[SYMBOL_GOT_DISP] = true;
8607 
8608 	  mips_split_p[SYMBOL_GOTOFF_DISP] = true;
8609 	  mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
8610 	  mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
8611 
8612 	  mips_split_p[SYMBOL_GOTOFF_CALL] = true;
8613 	  mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
8614 	  mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
8615 	}
8616       else
8617 	{
8618 	  if (TARGET_NEWABI)
8619 	    mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
8620 	  else
8621 	    mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
8622 	  mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
8623 	  if (TARGET_MIPS16)
8624 	    /* Expose the use of $28 as soon as possible.  */
8625 	    mips_split_p[SYMBOL_GOT_DISP] = true;
8626 	}
8627     }
8628 
8629   if (TARGET_NEWABI)
8630     {
8631       mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
8632       mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
8633       mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
8634     }
8635 
8636   mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
8637   mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
8638 
8639   if (TARGET_MIPS16_PCREL_LOADS)
8640     {
8641       mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
8642       mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
8643     }
8644   else
8645     {
8646       mips_split_p[SYMBOL_DTPREL] = true;
8647       mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
8648       mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
8649 
8650       mips_split_p[SYMBOL_TPREL] = true;
8651       mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
8652       mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
8653     }
8654 
8655   mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
8656   mips_lo_relocs[SYMBOL_HALF] = "%half(";
8657 }
8658 
8659 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
8660    in context CONTEXT.  RELOCS is the array of relocations to use.  */
8661 
8662 static void
mips_print_operand_reloc(FILE * file,rtx op,enum mips_symbol_context context,const char ** relocs)8663 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
8664 			  const char **relocs)
8665 {
8666   enum mips_symbol_type symbol_type;
8667   const char *p;
8668 
8669   symbol_type = mips_classify_symbolic_expression (op, context);
8670   gcc_assert (relocs[symbol_type]);
8671 
8672   fputs (relocs[symbol_type], file);
8673   output_addr_const (file, mips_strip_unspec_address (op));
8674   for (p = relocs[symbol_type]; *p != 0; p++)
8675     if (*p == '(')
8676       fputc (')', file);
8677 }
8678 
8679 /* Start a new block with the given asm switch enabled.  If we need
8680    to print a directive, emit PREFIX before it and SUFFIX after it.  */
8681 
8682 static void
mips_push_asm_switch_1(struct mips_asm_switch * asm_switch,const char * prefix,const char * suffix)8683 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
8684 			const char *prefix, const char *suffix)
8685 {
8686   if (asm_switch->nesting_level == 0)
8687     fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
8688   asm_switch->nesting_level++;
8689 }
8690 
8691 /* Likewise, but end a block.  */
8692 
8693 static void
mips_pop_asm_switch_1(struct mips_asm_switch * asm_switch,const char * prefix,const char * suffix)8694 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
8695 		       const char *prefix, const char *suffix)
8696 {
8697   gcc_assert (asm_switch->nesting_level);
8698   asm_switch->nesting_level--;
8699   if (asm_switch->nesting_level == 0)
8700     fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
8701 }
8702 
8703 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
8704    that either print a complete line or print nothing.  */
8705 
8706 void
mips_push_asm_switch(struct mips_asm_switch * asm_switch)8707 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
8708 {
8709   mips_push_asm_switch_1 (asm_switch, "\t", "\n");
8710 }
8711 
8712 void
mips_pop_asm_switch(struct mips_asm_switch * asm_switch)8713 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
8714 {
8715   mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
8716 }
8717 
8718 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
8719    The punctuation characters are:
8720 
8721    '('	Start a nested ".set noreorder" block.
8722    ')'	End a nested ".set noreorder" block.
8723    '['	Start a nested ".set noat" block.
8724    ']'	End a nested ".set noat" block.
8725    '<'	Start a nested ".set nomacro" block.
8726    '>'	End a nested ".set nomacro" block.
8727    '*'	Behave like %(%< if generating a delayed-branch sequence.
8728    '#'	Print a nop if in a ".set noreorder" block.
8729    '/'	Like '#', but do nothing within a delayed-branch sequence.
8730    '?'	Print "l" if mips_branch_likely is true
8731    '~'	Print a nop if mips_branch_likely is true
8732    '.'	Print the name of the register with a hard-wired zero (zero or $0).
8733    '@'	Print the name of the assembler temporary register (at or $1).
8734    '^'	Print the name of the pic call-through register (t9 or $25).
8735    '+'	Print the name of the gp register (usually gp or $28).
8736    '$'	Print the name of the stack pointer register (sp or $29).
8737    ':'  Print "c" to use the compact version if the delay slot is a nop.
8738    '!'  Print "s" to use the short version if the delay slot contains a
8739 	16-bit instruction.
8740 
8741    See also mips_init_print_operand_punct.  */
8742 
8743 static void
mips_print_operand_punctuation(FILE * file,int ch)8744 mips_print_operand_punctuation (FILE *file, int ch)
8745 {
8746   switch (ch)
8747     {
8748     case '(':
8749       mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
8750       break;
8751 
8752     case ')':
8753       mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
8754       break;
8755 
8756     case '[':
8757       mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
8758       break;
8759 
8760     case ']':
8761       mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
8762       break;
8763 
8764     case '<':
8765       mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
8766       break;
8767 
8768     case '>':
8769       mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
8770       break;
8771 
8772     case '*':
8773       if (final_sequence != 0)
8774 	{
8775 	  mips_print_operand_punctuation (file, '(');
8776 	  mips_print_operand_punctuation (file, '<');
8777 	}
8778       break;
8779 
8780     case '#':
8781       if (mips_noreorder.nesting_level > 0)
8782 	fputs ("\n\tnop", file);
8783       break;
8784 
8785     case '/':
8786       /* Print an extra newline so that the delayed insn is separated
8787 	 from the following ones.  This looks neater and is consistent
8788 	 with non-nop delayed sequences.  */
8789       if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
8790 	fputs ("\n\tnop\n", file);
8791       break;
8792 
8793     case '?':
8794       if (mips_branch_likely)
8795 	putc ('l', file);
8796       break;
8797 
8798     case '~':
8799       if (mips_branch_likely)
8800 	fputs ("\n\tnop", file);
8801       break;
8802 
8803     case '.':
8804       fputs (reg_names[GP_REG_FIRST + 0], file);
8805       break;
8806 
8807     case '@':
8808       fputs (reg_names[AT_REGNUM], file);
8809       break;
8810 
8811     case '^':
8812       fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
8813       break;
8814 
8815     case '+':
8816       fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
8817       break;
8818 
8819     case '$':
8820       fputs (reg_names[STACK_POINTER_REGNUM], file);
8821       break;
8822 
8823     case ':':
8824       /* When final_sequence is 0, the delay slot will be a nop.  We can
8825 	 use the compact version where available.  The %: formatter will
8826 	 only be present if a compact form of the branch is available.  */
8827       if (final_sequence == 0)
8828 	putc ('c', file);
8829       break;
8830 
8831     case '!':
8832       /* If the delay slot instruction is short, then use the
8833 	 compact version.  */
8834       if (TARGET_MICROMIPS && !TARGET_INTERLINK_COMPRESSED && mips_isa_rev <= 5
8835 	  && (final_sequence == 0
8836 	      || get_attr_length (final_sequence->insn (1)) == 2))
8837 	putc ('s', file);
8838       break;
8839 
8840     default:
8841       gcc_unreachable ();
8842       break;
8843     }
8844 }
8845 
8846 /* Initialize mips_print_operand_punct.  */
8847 
8848 static void
mips_init_print_operand_punct(void)8849 mips_init_print_operand_punct (void)
8850 {
8851   const char *p;
8852 
8853   for (p = "()[]<>*#/?~.@^+$:!"; *p; p++)
8854     mips_print_operand_punct[(unsigned char) *p] = true;
8855 }
8856 
8857 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
8858    associated with condition CODE.  Print the condition part of the
8859    opcode to FILE.  */
8860 
8861 static void
mips_print_int_branch_condition(FILE * file,enum rtx_code code,int letter)8862 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
8863 {
8864   switch (code)
8865     {
8866     case EQ:
8867     case NE:
8868     case GT:
8869     case GE:
8870     case LT:
8871     case LE:
8872     case GTU:
8873     case GEU:
8874     case LTU:
8875     case LEU:
8876       /* Conveniently, the MIPS names for these conditions are the same
8877 	 as their RTL equivalents.  */
8878       fputs (GET_RTX_NAME (code), file);
8879       break;
8880 
8881     default:
8882       output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8883       break;
8884     }
8885 }
8886 
8887 /* Likewise floating-point branches.  */
8888 
8889 static void
mips_print_float_branch_condition(FILE * file,enum rtx_code code,int letter)8890 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
8891 {
8892   switch (code)
8893     {
8894     case EQ:
8895       if (ISA_HAS_CCF)
8896 	fputs ("c1eqz", file);
8897       else
8898 	fputs ("c1f", file);
8899       break;
8900 
8901     case NE:
8902       if (ISA_HAS_CCF)
8903 	fputs ("c1nez", file);
8904       else
8905 	fputs ("c1t", file);
8906       break;
8907 
8908     default:
8909       output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8910       break;
8911     }
8912 }
8913 
8914 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P.  */
8915 
8916 static bool
mips_print_operand_punct_valid_p(unsigned char code)8917 mips_print_operand_punct_valid_p (unsigned char code)
8918 {
8919   return mips_print_operand_punct[code];
8920 }
8921 
8922 /* Implement TARGET_PRINT_OPERAND.  The MIPS-specific operand codes are:
8923 
8924    'E'	Print CONST_INT OP element 0 of a replicated CONST_VECTOR in decimal.
8925    'X'	Print CONST_INT OP in hexadecimal format.
8926    'x'	Print the low 16 bits of CONST_INT OP in hexadecimal format.
8927    'd'	Print CONST_INT OP in decimal.
8928    'B'	Print CONST_INT OP element 0 of a replicated CONST_VECTOR
8929 	  as an unsigned byte [0..255].
8930    'm'	Print one less than CONST_INT OP in decimal.
8931    'y'	Print exact log2 of CONST_INT OP in decimal.
8932    'h'	Print the high-part relocation associated with OP, after stripping
8933 	  any outermost HIGH.
8934    'R'	Print the low-part relocation associated with OP.
8935    'C'	Print the integer branch condition for comparison OP.
8936    'N'	Print the inverse of the integer branch condition for comparison OP.
8937    'F'	Print the FPU branch condition for comparison OP.
8938    'W'	Print the inverse of the FPU branch condition for comparison OP.
8939    'w'	Print a MSA register.
8940    'T'	Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
8941 	      'z' for (eq:?I ...), 'n' for (ne:?I ...).
8942    't'	Like 'T', but with the EQ/NE cases reversed
8943    'Y'	Print mips_fp_conditions[INTVAL (OP)]
8944    'Z'	Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
8945    'q'	Print a DSP accumulator register.
8946    'D'	Print the second part of a double-word register or memory operand.
8947    'L'	Print the low-order register in a double-word register operand.
8948    'M'	Print high-order register in a double-word register operand.
8949    'z'	Print $0 if OP is zero, otherwise print OP normally.
8950    'b'	Print the address of a memory operand, without offset.
8951    'v'	Print the insn size suffix b, h, w or d for vector modes V16QI, V8HI,
8952 	  V4SI, V2SI, and w, d for vector modes V4SF, V2DF respectively.
8953    'V'	Print exact log2 of CONST_INT OP element 0 of a replicated
8954 	  CONST_VECTOR in decimal.  */
8955 
8956 static void
mips_print_operand(FILE * file,rtx op,int letter)8957 mips_print_operand (FILE *file, rtx op, int letter)
8958 {
8959   enum rtx_code code;
8960 
8961   if (mips_print_operand_punct_valid_p (letter))
8962     {
8963       mips_print_operand_punctuation (file, letter);
8964       return;
8965     }
8966 
8967   gcc_assert (op);
8968   code = GET_CODE (op);
8969 
8970   switch (letter)
8971     {
8972     case 'E':
8973       if (GET_CODE (op) == CONST_VECTOR)
8974 	{
8975 	  gcc_assert (mips_const_vector_same_val_p (op, GET_MODE (op)));
8976 	  op = CONST_VECTOR_ELT (op, 0);
8977 	  gcc_assert (CONST_INT_P (op));
8978 	  fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
8979 	}
8980       else
8981 	output_operand_lossage ("invalid use of '%%%c'", letter);
8982       break;
8983 
8984     case 'X':
8985       if (CONST_INT_P (op))
8986 	fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
8987       else
8988 	output_operand_lossage ("invalid use of '%%%c'", letter);
8989       break;
8990 
8991     case 'x':
8992       if (CONST_INT_P (op))
8993 	fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
8994       else
8995 	output_operand_lossage ("invalid use of '%%%c'", letter);
8996       break;
8997 
8998     case 'd':
8999       if (CONST_INT_P (op))
9000 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
9001       else
9002 	output_operand_lossage ("invalid use of '%%%c'", letter);
9003       break;
9004 
9005     case 'B':
9006       if (GET_CODE (op) == CONST_VECTOR)
9007 	{
9008 	  gcc_assert (mips_const_vector_same_val_p (op, GET_MODE (op)));
9009 	  op = CONST_VECTOR_ELT (op, 0);
9010 	  gcc_assert (CONST_INT_P (op));
9011 	  unsigned HOST_WIDE_INT val8 = UINTVAL (op) & GET_MODE_MASK (QImode);
9012 	  fprintf (file, HOST_WIDE_INT_PRINT_UNSIGNED, val8);
9013 	}
9014       else
9015 	output_operand_lossage ("invalid use of '%%%c'", letter);
9016       break;
9017 
9018     case 'm':
9019       if (CONST_INT_P (op))
9020 	fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
9021       else
9022 	output_operand_lossage ("invalid use of '%%%c'", letter);
9023       break;
9024 
9025     case 'y':
9026       if (CONST_INT_P (op))
9027 	{
9028 	  int val = exact_log2 (INTVAL (op));
9029 	  if (val != -1)
9030 	    fprintf (file, "%d", val);
9031 	  else
9032 	    output_operand_lossage ("invalid use of '%%%c'", letter);
9033 	}
9034       else
9035 	output_operand_lossage ("invalid use of '%%%c'", letter);
9036       break;
9037 
9038     case 'V':
9039       if (GET_CODE (op) == CONST_VECTOR)
9040 	{
9041 	  machine_mode mode = GET_MODE_INNER (GET_MODE (op));
9042 	  unsigned HOST_WIDE_INT val = UINTVAL (CONST_VECTOR_ELT (op, 0));
9043 	  int vlog2 = exact_log2 (val & GET_MODE_MASK (mode));
9044 	  if (vlog2 != -1)
9045 	    fprintf (file, "%d", vlog2);
9046 	  else
9047 	    output_operand_lossage ("invalid use of '%%%c'", letter);
9048 	}
9049       else
9050 	output_operand_lossage ("invalid use of '%%%c'", letter);
9051       break;
9052 
9053     case 'h':
9054       if (code == HIGH)
9055 	op = XEXP (op, 0);
9056       mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
9057       break;
9058 
9059     case 'R':
9060       mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
9061       break;
9062 
9063     case 'C':
9064       mips_print_int_branch_condition (file, code, letter);
9065       break;
9066 
9067     case 'N':
9068       mips_print_int_branch_condition (file, reverse_condition (code), letter);
9069       break;
9070 
9071     case 'F':
9072       mips_print_float_branch_condition (file, code, letter);
9073       break;
9074 
9075     case 'W':
9076       mips_print_float_branch_condition (file, reverse_condition (code),
9077 					 letter);
9078       break;
9079 
9080     case 'T':
9081     case 't':
9082       {
9083 	int truth = (code == NE) == (letter == 'T');
9084 	fputc ("zfnt"[truth * 2 + ST_REG_P (REGNO (XEXP (op, 0)))], file);
9085       }
9086       break;
9087 
9088     case 'Y':
9089       if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
9090 	fputs (mips_fp_conditions[UINTVAL (op)], file);
9091       else
9092 	output_operand_lossage ("'%%%c' is not a valid operand prefix",
9093 				letter);
9094       break;
9095 
9096     case 'Z':
9097       if (ISA_HAS_8CC || ISA_HAS_CCF)
9098 	{
9099 	  mips_print_operand (file, op, 0);
9100 	  fputc (',', file);
9101 	}
9102       break;
9103 
9104     case 'q':
9105       if (code == REG && MD_REG_P (REGNO (op)))
9106 	fprintf (file, "$ac0");
9107       else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
9108 	fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
9109       else
9110 	output_operand_lossage ("invalid use of '%%%c'", letter);
9111       break;
9112 
9113     case 'w':
9114       if (code == REG && MSA_REG_P (REGNO (op)))
9115 	fprintf (file, "$w%s", &reg_names[REGNO (op)][2]);
9116       else
9117 	output_operand_lossage ("invalid use of '%%%c'", letter);
9118       break;
9119 
9120     case 'v':
9121       switch (GET_MODE (op))
9122 	{
9123 	case E_V16QImode:
9124 	  fprintf (file, "b");
9125 	  break;
9126 	case E_V8HImode:
9127 	  fprintf (file, "h");
9128 	  break;
9129 	case E_V4SImode:
9130 	case E_V4SFmode:
9131 	  fprintf (file, "w");
9132 	  break;
9133 	case E_V2DImode:
9134 	case E_V2DFmode:
9135 	  fprintf (file, "d");
9136 	  break;
9137 	default:
9138 	  output_operand_lossage ("invalid use of '%%%c'", letter);
9139 	}
9140       break;
9141 
9142     default:
9143       switch (code)
9144 	{
9145 	case REG:
9146 	  {
9147 	    unsigned int regno = REGNO (op);
9148 	    if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
9149 		|| (letter == 'L' && TARGET_BIG_ENDIAN)
9150 		|| letter == 'D')
9151 	      regno++;
9152 	    else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
9153 	      output_operand_lossage ("invalid use of '%%%c'", letter);
9154 	    /* We need to print $0 .. $31 for COP0 registers.  */
9155 	    if (COP0_REG_P (regno))
9156 	      fprintf (file, "$%s", &reg_names[regno][4]);
9157 	    else
9158 	      fprintf (file, "%s", reg_names[regno]);
9159 	  }
9160 	  break;
9161 
9162 	case MEM:
9163 	  if (letter == 'D')
9164 	    output_address (GET_MODE (op), plus_constant (Pmode,
9165 							  XEXP (op, 0), 4));
9166 	  else if (letter == 'b')
9167 	    {
9168 	      gcc_assert (REG_P (XEXP (op, 0)));
9169 	      mips_print_operand (file, XEXP (op, 0), 0);
9170 	    }
9171 	  else if (letter && letter != 'z')
9172 	    output_operand_lossage ("invalid use of '%%%c'", letter);
9173 	  else
9174 	    output_address (GET_MODE (op), XEXP (op, 0));
9175 	  break;
9176 
9177 	default:
9178 	  if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
9179 	    fputs (reg_names[GP_REG_FIRST], file);
9180 	  else if (letter && letter != 'z')
9181 	    output_operand_lossage ("invalid use of '%%%c'", letter);
9182 	  else if (CONST_GP_P (op))
9183 	    fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
9184 	  else
9185 	    output_addr_const (file, mips_strip_unspec_address (op));
9186 	  break;
9187 	}
9188     }
9189 }
9190 
9191 /* Implement TARGET_PRINT_OPERAND_ADDRESS.  */
9192 
9193 static void
mips_print_operand_address(FILE * file,machine_mode,rtx x)9194 mips_print_operand_address (FILE *file, machine_mode /*mode*/, rtx x)
9195 {
9196   struct mips_address_info addr;
9197 
9198   if (mips_classify_address (&addr, x, word_mode, true))
9199     switch (addr.type)
9200       {
9201       case ADDRESS_REG:
9202 	mips_print_operand (file, addr.offset, 0);
9203 	fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
9204 	return;
9205 
9206       case ADDRESS_LO_SUM:
9207 	mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
9208 				  mips_lo_relocs);
9209 	fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
9210 	return;
9211 
9212       case ADDRESS_CONST_INT:
9213 	output_addr_const (file, x);
9214 	fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
9215 	return;
9216 
9217       case ADDRESS_SYMBOLIC:
9218 	output_addr_const (file, mips_strip_unspec_address (x));
9219 	return;
9220       }
9221   gcc_unreachable ();
9222 }
9223 
9224 /* Implement TARGET_ENCODE_SECTION_INFO.  */
9225 
9226 static void
mips_encode_section_info(tree decl,rtx rtl,int first)9227 mips_encode_section_info (tree decl, rtx rtl, int first)
9228 {
9229   default_encode_section_info (decl, rtl, first);
9230 
9231   if (TREE_CODE (decl) == FUNCTION_DECL)
9232     {
9233       rtx symbol = XEXP (rtl, 0);
9234       tree type = TREE_TYPE (decl);
9235 
9236       /* Encode whether the symbol is short or long.  */
9237       if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
9238 	  || mips_far_type_p (type))
9239 	SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
9240     }
9241 }
9242 
9243 /* Implement TARGET_SELECT_RTX_SECTION.  */
9244 
9245 static section *
mips_select_rtx_section(machine_mode mode,rtx x,unsigned HOST_WIDE_INT align)9246 mips_select_rtx_section (machine_mode mode, rtx x,
9247 			 unsigned HOST_WIDE_INT align)
9248 {
9249   /* ??? Consider using mergeable small data sections.  */
9250   if (mips_rtx_constant_in_small_data_p (mode))
9251     return get_named_section (NULL, ".sdata", 0);
9252 
9253   return default_elf_select_rtx_section (mode, x, align);
9254 }
9255 
9256 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
9257 
9258    The complication here is that, with the combination TARGET_ABICALLS
9259    && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
9260    absolute addresses, and should therefore not be included in the
9261    read-only part of a DSO.  Handle such cases by selecting a normal
9262    data section instead of a read-only one.  The logic apes that in
9263    default_function_rodata_section.  */
9264 
9265 static section *
mips_function_rodata_section(tree decl)9266 mips_function_rodata_section (tree decl)
9267 {
9268   if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
9269     return default_function_rodata_section (decl);
9270 
9271   if (decl && DECL_SECTION_NAME (decl))
9272     {
9273       const char *name = DECL_SECTION_NAME (decl);
9274       if (DECL_COMDAT_GROUP (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
9275 	{
9276 	  char *rname = ASTRDUP (name);
9277 	  rname[14] = 'd';
9278 	  return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
9279 	}
9280       else if (flag_function_sections
9281 	       && flag_data_sections
9282 	       && strncmp (name, ".text.", 6) == 0)
9283 	{
9284 	  char *rname = ASTRDUP (name);
9285 	  memcpy (rname + 1, "data", 4);
9286 	  return get_section (rname, SECTION_WRITE, decl);
9287 	}
9288     }
9289   return data_section;
9290 }
9291 
9292 /* Implement TARGET_IN_SMALL_DATA_P.  */
9293 
9294 static bool
mips_in_small_data_p(const_tree decl)9295 mips_in_small_data_p (const_tree decl)
9296 {
9297   unsigned HOST_WIDE_INT size;
9298 
9299   if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
9300     return false;
9301 
9302   /* We don't yet generate small-data references for -mabicalls
9303      or VxWorks RTP code.  See the related -G handling in
9304      mips_option_override.  */
9305   if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
9306     return false;
9307 
9308   if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
9309     {
9310       const char *name;
9311 
9312       /* Reject anything that isn't in a known small-data section.  */
9313       name = DECL_SECTION_NAME (decl);
9314       if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
9315 	return false;
9316 
9317       /* If a symbol is defined externally, the assembler will use the
9318 	 usual -G rules when deciding how to implement macros.  */
9319       if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
9320 	return true;
9321     }
9322   else if (TARGET_EMBEDDED_DATA)
9323     {
9324       /* Don't put constants into the small data section: we want them
9325 	 to be in ROM rather than RAM.  */
9326       if (TREE_CODE (decl) != VAR_DECL)
9327 	return false;
9328 
9329       if (TREE_READONLY (decl)
9330 	  && !TREE_SIDE_EFFECTS (decl)
9331 	  && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
9332 	return false;
9333     }
9334 
9335   /* Enforce -mlocal-sdata.  */
9336   if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
9337     return false;
9338 
9339   /* Enforce -mextern-sdata.  */
9340   if (!TARGET_EXTERN_SDATA && DECL_P (decl))
9341     {
9342       if (DECL_EXTERNAL (decl))
9343 	return false;
9344       if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
9345 	return false;
9346     }
9347 
9348   /* We have traditionally not treated zero-sized objects as small data,
9349      so this is now effectively part of the ABI.  */
9350   size = int_size_in_bytes (TREE_TYPE (decl));
9351   return size > 0 && size <= mips_small_data_threshold;
9352 }
9353 
9354 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P.  We don't want to use
9355    anchors for small data: the GP register acts as an anchor in that
9356    case.  We also don't want to use them for PC-relative accesses,
9357    where the PC acts as an anchor.  */
9358 
9359 static bool
mips_use_anchors_for_symbol_p(const_rtx symbol)9360 mips_use_anchors_for_symbol_p (const_rtx symbol)
9361 {
9362   switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
9363     {
9364     case SYMBOL_PC_RELATIVE:
9365     case SYMBOL_GP_RELATIVE:
9366       return false;
9367 
9368     default:
9369       return default_use_anchors_for_symbol_p (symbol);
9370     }
9371 }
9372 
9373 /* The MIPS debug format wants all automatic variables and arguments
9374    to be in terms of the virtual frame pointer (stack pointer before
9375    any adjustment in the function), while the MIPS 3.0 linker wants
9376    the frame pointer to be the stack pointer after the initial
9377    adjustment.  So, we do the adjustment here.  The arg pointer (which
9378    is eliminated) points to the virtual frame pointer, while the frame
9379    pointer (which may be eliminated) points to the stack pointer after
9380    the initial adjustments.  */
9381 
9382 HOST_WIDE_INT
mips_debugger_offset(rtx addr,HOST_WIDE_INT offset)9383 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
9384 {
9385   rtx offset2 = const0_rtx;
9386   rtx reg = eliminate_constant_term (addr, &offset2);
9387 
9388   if (offset == 0)
9389     offset = INTVAL (offset2);
9390 
9391   if (reg == stack_pointer_rtx
9392       || reg == frame_pointer_rtx
9393       || reg == hard_frame_pointer_rtx)
9394     {
9395       offset -= cfun->machine->frame.total_size;
9396       if (reg == hard_frame_pointer_rtx)
9397 	offset += cfun->machine->frame.hard_frame_pointer_offset;
9398     }
9399 
9400   return offset;
9401 }
9402 
9403 /* Implement ASM_OUTPUT_EXTERNAL.  */
9404 
9405 void
mips_output_external(FILE * file,tree decl,const char * name)9406 mips_output_external (FILE *file, tree decl, const char *name)
9407 {
9408   default_elf_asm_output_external (file, decl, name);
9409 
9410   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
9411      set in order to avoid putting out names that are never really
9412      used. */
9413   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
9414     {
9415       if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
9416 	{
9417 	  /* When using assembler macros, emit .extern directives for
9418 	     all small-data externs so that the assembler knows how
9419 	     big they are.
9420 
9421 	     In most cases it would be safe (though pointless) to emit
9422 	     .externs for other symbols too.  One exception is when an
9423 	     object is within the -G limit but declared by the user to
9424 	     be in a section other than .sbss or .sdata.  */
9425 	  fputs ("\t.extern\t", file);
9426 	  assemble_name (file, name);
9427 	  fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
9428 		   int_size_in_bytes (TREE_TYPE (decl)));
9429 	}
9430     }
9431 }
9432 
9433 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME.  */
9434 
9435 static void
mips_output_filename(FILE * stream,const char * name)9436 mips_output_filename (FILE *stream, const char *name)
9437 {
9438   /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
9439      directives.  */
9440   if (write_symbols == DWARF2_DEBUG)
9441     return;
9442   else if (mips_output_filename_first_time)
9443     {
9444       mips_output_filename_first_time = 0;
9445       num_source_filenames += 1;
9446       current_function_file = name;
9447       fprintf (stream, "\t.file\t%d ", num_source_filenames);
9448       output_quoted_string (stream, name);
9449       putc ('\n', stream);
9450     }
9451   /* If we are emitting stabs, let dbxout.c handle this (except for
9452      the mips_output_filename_first_time case).  */
9453   else if (write_symbols == DBX_DEBUG)
9454     return;
9455   else if (name != current_function_file
9456 	   && strcmp (name, current_function_file) != 0)
9457     {
9458       num_source_filenames += 1;
9459       current_function_file = name;
9460       fprintf (stream, "\t.file\t%d ", num_source_filenames);
9461       output_quoted_string (stream, name);
9462       putc ('\n', stream);
9463     }
9464 }
9465 
9466 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
9467 
9468 static void ATTRIBUTE_UNUSED
mips_output_dwarf_dtprel(FILE * file,int size,rtx x)9469 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
9470 {
9471   switch (size)
9472     {
9473     case 4:
9474       fputs ("\t.dtprelword\t", file);
9475       break;
9476 
9477     case 8:
9478       fputs ("\t.dtpreldword\t", file);
9479       break;
9480 
9481     default:
9482       gcc_unreachable ();
9483     }
9484   output_addr_const (file, x);
9485   fputs ("+0x8000", file);
9486 }
9487 
9488 /* Implement TARGET_DWARF_REGISTER_SPAN.  */
9489 
9490 static rtx
mips_dwarf_register_span(rtx reg)9491 mips_dwarf_register_span (rtx reg)
9492 {
9493   rtx high, low;
9494   machine_mode mode;
9495 
9496   /* TARGET_FLOATXX is implemented as 32-bit floating-point registers but
9497      ensures that double-precision registers are treated as if they were
9498      64-bit physical registers.  The code will run correctly with 32-bit or
9499      64-bit registers which means that dwarf information cannot be precise
9500      for all scenarios.  We choose to state that the 64-bit values are stored
9501      in a single 64-bit 'piece'.  This slightly unusual construct can then be
9502      interpreted as either a pair of registers if the registers are 32-bit or
9503      a single 64-bit register depending on hardware.  */
9504   mode = GET_MODE (reg);
9505   if (FP_REG_P (REGNO (reg))
9506       && TARGET_FLOATXX
9507       && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
9508     {
9509       return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, reg));
9510     }
9511   /* By default, GCC maps increasing register numbers to increasing
9512      memory locations, but paired FPRs are always little-endian,
9513      regardless of the prevailing endianness.  */
9514   else if (FP_REG_P (REGNO (reg))
9515 	   && TARGET_BIG_ENDIAN
9516 	   && MAX_FPRS_PER_FMT > 1
9517 	   && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
9518     {
9519       gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
9520       high = mips_subword (reg, true);
9521       low = mips_subword (reg, false);
9522       return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
9523     }
9524 
9525   return NULL_RTX;
9526 }
9527 
9528 /* Implement TARGET_DWARF_FRAME_REG_MODE.  */
9529 
9530 static machine_mode
mips_dwarf_frame_reg_mode(int regno)9531 mips_dwarf_frame_reg_mode (int regno)
9532 {
9533   machine_mode mode = default_dwarf_frame_reg_mode (regno);
9534 
9535   if (FP_REG_P (regno) && mips_abi == ABI_32 && !TARGET_FLOAT32)
9536     mode = SImode;
9537 
9538   return mode;
9539 }
9540 
9541 /* DSP ALU can bypass data with no delays for the following pairs. */
9542 enum insn_code dspalu_bypass_table[][2] =
9543 {
9544   {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
9545   {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
9546   {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
9547   {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
9548   {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
9549   {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
9550   {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
9551   {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
9552 };
9553 
9554 int
mips_dspalu_bypass_p(rtx out_insn,rtx in_insn)9555 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
9556 {
9557   int i;
9558   int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
9559   enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
9560   enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
9561 
9562   for (i = 0; i < num_bypass; i++)
9563     {
9564       if (out_icode == dspalu_bypass_table[i][0]
9565 	  && in_icode == dspalu_bypass_table[i][1])
9566        return true;
9567     }
9568 
9569   return false;
9570 }
9571 /* Implement ASM_OUTPUT_ASCII.  */
9572 
9573 void
mips_output_ascii(FILE * stream,const char * string,size_t len)9574 mips_output_ascii (FILE *stream, const char *string, size_t len)
9575 {
9576   size_t i;
9577   int cur_pos;
9578 
9579   cur_pos = 17;
9580   fprintf (stream, "\t.ascii\t\"");
9581   for (i = 0; i < len; i++)
9582     {
9583       int c;
9584 
9585       c = (unsigned char) string[i];
9586       if (ISPRINT (c))
9587 	{
9588 	  if (c == '\\' || c == '\"')
9589 	    {
9590 	      putc ('\\', stream);
9591 	      cur_pos++;
9592 	    }
9593 	  putc (c, stream);
9594 	  cur_pos++;
9595 	}
9596       else
9597 	{
9598 	  fprintf (stream, "\\%03o", c);
9599 	  cur_pos += 4;
9600 	}
9601 
9602       if (cur_pos > 72 && i+1 < len)
9603 	{
9604 	  cur_pos = 17;
9605 	  fprintf (stream, "\"\n\t.ascii\t\"");
9606 	}
9607     }
9608   fprintf (stream, "\"\n");
9609 }
9610 
9611 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
9612    Update *ADDR with the operand that should be printed.  */
9613 
9614 const char *
mips_output_tls_reloc_directive(rtx * addr)9615 mips_output_tls_reloc_directive (rtx *addr)
9616 {
9617   enum mips_symbol_type type;
9618 
9619   type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
9620   *addr = mips_strip_unspec_address (*addr);
9621   switch (type)
9622     {
9623     case SYMBOL_DTPREL:
9624       return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
9625 
9626     case SYMBOL_TPREL:
9627       return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
9628 
9629     default:
9630       gcc_unreachable ();
9631     }
9632 }
9633 
9634 /* Emit either a label, .comm, or .lcomm directive.  When using assembler
9635    macros, mark the symbol as written so that mips_asm_output_external
9636    won't emit an .extern for it.  STREAM is the output file, NAME is the
9637    name of the symbol, INIT_STRING is the string that should be written
9638    before the symbol and FINAL_STRING is the string that should be
9639    written after it.  FINAL_STRING is a printf format that consumes the
9640    remaining arguments.  */
9641 
9642 void
mips_declare_object(FILE * stream,const char * name,const char * init_string,const char * final_string,...)9643 mips_declare_object (FILE *stream, const char *name, const char *init_string,
9644 		     const char *final_string, ...)
9645 {
9646   va_list ap;
9647 
9648   fputs (init_string, stream);
9649   assemble_name (stream, name);
9650   va_start (ap, final_string);
9651   vfprintf (stream, final_string, ap);
9652   va_end (ap);
9653 
9654   if (!TARGET_EXPLICIT_RELOCS)
9655     {
9656       tree name_tree = get_identifier (name);
9657       TREE_ASM_WRITTEN (name_tree) = 1;
9658     }
9659 }
9660 
9661 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
9662    NAME is the name of the object and ALIGN is the required alignment
9663    in bytes.  TAKES_ALIGNMENT_P is true if the directive takes a third
9664    alignment argument.  */
9665 
9666 void
mips_declare_common_object(FILE * stream,const char * name,const char * init_string,unsigned HOST_WIDE_INT size,unsigned int align,bool takes_alignment_p)9667 mips_declare_common_object (FILE *stream, const char *name,
9668 			    const char *init_string,
9669 			    unsigned HOST_WIDE_INT size,
9670 			    unsigned int align, bool takes_alignment_p)
9671 {
9672   if (!takes_alignment_p)
9673     {
9674       size += (align / BITS_PER_UNIT) - 1;
9675       size -= size % (align / BITS_PER_UNIT);
9676       mips_declare_object (stream, name, init_string,
9677 			   "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
9678     }
9679   else
9680     mips_declare_object (stream, name, init_string,
9681 			 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
9682 			 size, align / BITS_PER_UNIT);
9683 }
9684 
9685 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON.  This is usually the same as the
9686    elfos.h version, but we also need to handle -muninit-const-in-rodata.  */
9687 
9688 void
mips_output_aligned_decl_common(FILE * stream,tree decl,const char * name,unsigned HOST_WIDE_INT size,unsigned int align)9689 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
9690 				 unsigned HOST_WIDE_INT size,
9691 				 unsigned int align)
9692 {
9693   /* If the target wants uninitialized const declarations in
9694      .rdata then don't put them in .comm.  */
9695   if (TARGET_EMBEDDED_DATA
9696       && TARGET_UNINIT_CONST_IN_RODATA
9697       && TREE_CODE (decl) == VAR_DECL
9698       && TREE_READONLY (decl)
9699       && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
9700     {
9701       if (TREE_PUBLIC (decl) && DECL_NAME (decl))
9702 	targetm.asm_out.globalize_label (stream, name);
9703 
9704       switch_to_section (readonly_data_section);
9705       ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
9706       mips_declare_object (stream, name, "",
9707 			   ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
9708 			   size);
9709     }
9710   else
9711     mips_declare_common_object (stream, name, "\n\t.comm\t",
9712 				size, align, true);
9713 }
9714 
9715 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
9716 extern int size_directive_output;
9717 
9718 /* Implement ASM_DECLARE_OBJECT_NAME.  This is like most of the standard ELF
9719    definitions except that it uses mips_declare_object to emit the label.  */
9720 
9721 void
mips_declare_object_name(FILE * stream,const char * name,tree decl ATTRIBUTE_UNUSED)9722 mips_declare_object_name (FILE *stream, const char *name,
9723 			  tree decl ATTRIBUTE_UNUSED)
9724 {
9725 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
9726   ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
9727 #endif
9728 
9729   size_directive_output = 0;
9730   if (!flag_inhibit_size_directive && DECL_SIZE (decl))
9731     {
9732       HOST_WIDE_INT size;
9733 
9734       size_directive_output = 1;
9735       size = int_size_in_bytes (TREE_TYPE (decl));
9736       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
9737     }
9738 
9739   mips_declare_object (stream, name, "", ":\n");
9740 }
9741 
9742 /* Implement ASM_FINISH_DECLARE_OBJECT.  This is generic ELF stuff.  */
9743 
9744 void
mips_finish_declare_object(FILE * stream,tree decl,int top_level,int at_end)9745 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
9746 {
9747   const char *name;
9748 
9749   name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
9750   if (!flag_inhibit_size_directive
9751       && DECL_SIZE (decl) != 0
9752       && !at_end
9753       && top_level
9754       && DECL_INITIAL (decl) == error_mark_node
9755       && !size_directive_output)
9756     {
9757       HOST_WIDE_INT size;
9758 
9759       size_directive_output = 1;
9760       size = int_size_in_bytes (TREE_TYPE (decl));
9761       ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
9762     }
9763 }
9764 #endif
9765 
9766 /* Mark text contents as code or data, mainly for the purpose of correct
9767    disassembly.  Emit a local symbol and set its type appropriately for
9768    that purpose.  Also emit `.insn' if marking contents as code so that
9769    the ISA mode is recorded and any padding that follows is disassembled
9770    as correct instructions.  */
9771 
9772 void
mips_set_text_contents_type(FILE * file ATTRIBUTE_UNUSED,const char * prefix ATTRIBUTE_UNUSED,unsigned long num ATTRIBUTE_UNUSED,bool function_p ATTRIBUTE_UNUSED)9773 mips_set_text_contents_type (FILE *file ATTRIBUTE_UNUSED,
9774 			     const char *prefix ATTRIBUTE_UNUSED,
9775 			     unsigned long num ATTRIBUTE_UNUSED,
9776 			     bool function_p ATTRIBUTE_UNUSED)
9777 {
9778 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
9779   char buf[(sizeof (num) * 10) / 4 + 2];
9780   const char *fnname;
9781   char *sname;
9782   rtx symbol;
9783 
9784   sprintf (buf, "%lu", num);
9785   symbol = XEXP (DECL_RTL (current_function_decl), 0);
9786   fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
9787   sname = ACONCAT ((prefix, fnname, "_", buf, NULL));
9788 
9789   ASM_OUTPUT_TYPE_DIRECTIVE (file, sname, function_p ? "function" : "object");
9790   assemble_name (file, sname);
9791   fputs (":\n", file);
9792   if (function_p)
9793     fputs ("\t.insn\n", file);
9794 #endif
9795 }
9796 
9797 /* Return the FOO in the name of the ".mdebug.FOO" section associated
9798    with the current ABI.  */
9799 
9800 static const char *
mips_mdebug_abi_name(void)9801 mips_mdebug_abi_name (void)
9802 {
9803   switch (mips_abi)
9804     {
9805     case ABI_32:
9806       return "abi32";
9807     case ABI_O64:
9808       return "abiO64";
9809     case ABI_N32:
9810       return "abiN32";
9811     case ABI_64:
9812       return "abi64";
9813     case ABI_EABI:
9814       return TARGET_64BIT ? "eabi64" : "eabi32";
9815     default:
9816       gcc_unreachable ();
9817     }
9818 }
9819 
9820 /* Implement TARGET_ASM_FILE_START.  */
9821 
9822 static void
mips_file_start(void)9823 mips_file_start (void)
9824 {
9825   default_file_start ();
9826 
9827   /* Generate a special section to describe the ABI switches used to
9828      produce the resultant binary.  */
9829 
9830   /* Record the ABI itself.  Modern versions of binutils encode
9831      this information in the ELF header flags, but GDB needs the
9832      information in order to correctly debug binaries produced by
9833      older binutils.  See the function mips_gdbarch_init in
9834      gdb/mips-tdep.c.  */
9835   fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
9836 	   mips_mdebug_abi_name ());
9837 
9838   /* There is no ELF header flag to distinguish long32 forms of the
9839      EABI from long64 forms.  Emit a special section to help tools
9840      such as GDB.  Do the same for o64, which is sometimes used with
9841      -mlong64.  */
9842   if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
9843     fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
9844 	     "\t.previous\n", TARGET_LONG64 ? 64 : 32);
9845 
9846   /* Record the NaN encoding.  */
9847   if (HAVE_AS_NAN || mips_nan != MIPS_IEEE_754_DEFAULT)
9848     fprintf (asm_out_file, "\t.nan\t%s\n",
9849 	     mips_nan == MIPS_IEEE_754_2008 ? "2008" : "legacy");
9850 
9851 #ifdef HAVE_AS_DOT_MODULE
9852   /* Record the FP ABI.  See below for comments.  */
9853   if (TARGET_NO_FLOAT)
9854 #ifdef HAVE_AS_GNU_ATTRIBUTE
9855     fputs ("\t.gnu_attribute 4, 0\n", asm_out_file);
9856 #else
9857     ;
9858 #endif
9859   else if (!TARGET_HARD_FLOAT_ABI)
9860     fputs ("\t.module\tsoftfloat\n", asm_out_file);
9861   else if (!TARGET_DOUBLE_FLOAT)
9862     fputs ("\t.module\tsinglefloat\n", asm_out_file);
9863   else if (TARGET_FLOATXX)
9864     fputs ("\t.module\tfp=xx\n", asm_out_file);
9865   else if (TARGET_FLOAT64)
9866     fputs ("\t.module\tfp=64\n", asm_out_file);
9867   else
9868     fputs ("\t.module\tfp=32\n", asm_out_file);
9869 
9870   if (TARGET_ODD_SPREG)
9871     fputs ("\t.module\toddspreg\n", asm_out_file);
9872   else
9873     fputs ("\t.module\tnooddspreg\n", asm_out_file);
9874 
9875 #else
9876 #ifdef HAVE_AS_GNU_ATTRIBUTE
9877   {
9878     int attr;
9879 
9880     /* No floating-point operations, -mno-float.  */
9881     if (TARGET_NO_FLOAT)
9882       attr = 0;
9883     /* Soft-float code, -msoft-float.  */
9884     else if (!TARGET_HARD_FLOAT_ABI)
9885       attr = 3;
9886     /* Single-float code, -msingle-float.  */
9887     else if (!TARGET_DOUBLE_FLOAT)
9888       attr = 2;
9889     /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64.
9890        Reserved attr=4.
9891        This case used 12 callee-saved double-precision registers
9892        and is deprecated.  */
9893     /* 64-bit or 32-bit FP registers on a 32-bit target, -mfpxx.  */
9894     else if (TARGET_FLOATXX)
9895       attr = 5;
9896     /* 64-bit FP registers on a 32-bit target, -mfp64 -modd-spreg.  */
9897     else if (mips_abi == ABI_32 && TARGET_FLOAT64 && TARGET_ODD_SPREG)
9898       attr = 6;
9899     /* 64-bit FP registers on a 32-bit target, -mfp64 -mno-odd-spreg.  */
9900     else if (mips_abi == ABI_32 && TARGET_FLOAT64)
9901       attr = 7;
9902     /* Regular FP code, FP regs same size as GP regs, -mdouble-float.  */
9903     else
9904       attr = 1;
9905 
9906     fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
9907 
9908     /* 128-bit MSA.  */
9909     if (ISA_HAS_MSA)
9910       fprintf (asm_out_file, "\t.gnu_attribute 8, 1\n");
9911   }
9912 #endif
9913 #endif
9914 
9915   /* If TARGET_ABICALLS, tell GAS to generate -KPIC code.  */
9916   if (TARGET_ABICALLS)
9917     {
9918       fprintf (asm_out_file, "\t.abicalls\n");
9919       if (TARGET_ABICALLS_PIC0)
9920 	fprintf (asm_out_file, "\t.option\tpic0\n");
9921     }
9922 
9923   if (flag_verbose_asm)
9924     fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
9925 	     ASM_COMMENT_START,
9926 	     mips_small_data_threshold, mips_arch_info->name, mips_isa);
9927 }
9928 
9929 /* Implement TARGET_ASM_CODE_END.  */
9930 
9931 static void
mips_code_end(void)9932 mips_code_end (void)
9933 {
9934   mips_finish_stub (&mips16_rdhwr_stub);
9935   mips_finish_stub (&mips16_get_fcsr_stub);
9936   mips_finish_stub (&mips16_set_fcsr_stub);
9937 }
9938 
9939 /* Make the last instruction frame-related and note that it performs
9940    the operation described by FRAME_PATTERN.  */
9941 
9942 static void
mips_set_frame_expr(rtx frame_pattern)9943 mips_set_frame_expr (rtx frame_pattern)
9944 {
9945   rtx_insn *insn;
9946 
9947   insn = get_last_insn ();
9948   RTX_FRAME_RELATED_P (insn) = 1;
9949   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9950 				      frame_pattern,
9951 				      REG_NOTES (insn));
9952 }
9953 
9954 /* Return a frame-related rtx that stores REG at MEM.
9955    REG must be a single register.  */
9956 
9957 static rtx
mips_frame_set(rtx mem,rtx reg)9958 mips_frame_set (rtx mem, rtx reg)
9959 {
9960   rtx set;
9961 
9962   set = gen_rtx_SET (mem, reg);
9963   RTX_FRAME_RELATED_P (set) = 1;
9964 
9965   return set;
9966 }
9967 
9968 /* Record that the epilogue has restored call-saved register REG.  */
9969 
9970 static void
mips_add_cfa_restore(rtx reg)9971 mips_add_cfa_restore (rtx reg)
9972 {
9973   mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
9974 					       mips_epilogue.cfa_restores);
9975 }
9976 
9977 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
9978    mips16e_s2_s8_regs[X], it must also save the registers in indexes
9979    X + 1 onwards.  Likewise mips16e_a0_a3_regs.  */
9980 static const unsigned char mips16e_s2_s8_regs[] = {
9981   30, 23, 22, 21, 20, 19, 18
9982 };
9983 static const unsigned char mips16e_a0_a3_regs[] = {
9984   4, 5, 6, 7
9985 };
9986 
9987 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
9988    ordered from the uppermost in memory to the lowest in memory.  */
9989 static const unsigned char mips16e_save_restore_regs[] = {
9990   31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
9991 };
9992 
9993 /* Return the index of the lowest X in the range [0, SIZE) for which
9994    bit REGS[X] is set in MASK.  Return SIZE if there is no such X.  */
9995 
9996 static unsigned int
mips16e_find_first_register(unsigned int mask,const unsigned char * regs,unsigned int size)9997 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
9998 			     unsigned int size)
9999 {
10000   unsigned int i;
10001 
10002   for (i = 0; i < size; i++)
10003     if (BITSET_P (mask, regs[i]))
10004       break;
10005 
10006   return i;
10007 }
10008 
10009 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
10010    is the number of set bits.  If *MASK_PTR contains REGS[X] for some X
10011    in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
10012    is true for all indexes (X, SIZE).  */
10013 
10014 static void
mips16e_mask_registers(unsigned int * mask_ptr,const unsigned char * regs,unsigned int size,unsigned int * num_regs_ptr)10015 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
10016 			unsigned int size, unsigned int *num_regs_ptr)
10017 {
10018   unsigned int i;
10019 
10020   i = mips16e_find_first_register (*mask_ptr, regs, size);
10021   for (i++; i < size; i++)
10022     if (!BITSET_P (*mask_ptr, regs[i]))
10023       {
10024 	*num_regs_ptr += 1;
10025 	*mask_ptr |= 1 << regs[i];
10026       }
10027 }
10028 
10029 /* Return a simplified form of X using the register values in REG_VALUES.
10030    REG_VALUES[R] is the last value assigned to hard register R, or null
10031    if R has not been modified.
10032 
10033    This function is rather limited, but is good enough for our purposes.  */
10034 
10035 static rtx
mips16e_collect_propagate_value(rtx x,rtx * reg_values)10036 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
10037 {
10038   x = avoid_constant_pool_reference (x);
10039 
10040   if (UNARY_P (x))
10041     {
10042       rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
10043       return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
10044 				 x0, GET_MODE (XEXP (x, 0)));
10045     }
10046 
10047   if (ARITHMETIC_P (x))
10048     {
10049       rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
10050       rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
10051       return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
10052     }
10053 
10054   if (REG_P (x)
10055       && reg_values[REGNO (x)]
10056       && !rtx_unstable_p (reg_values[REGNO (x)]))
10057     return reg_values[REGNO (x)];
10058 
10059   return x;
10060 }
10061 
10062 /* Return true if (set DEST SRC) stores an argument register into its
10063    caller-allocated save slot, storing the number of that argument
10064    register in *REGNO_PTR if so.  REG_VALUES is as for
10065    mips16e_collect_propagate_value.  */
10066 
10067 static bool
mips16e_collect_argument_save_p(rtx dest,rtx src,rtx * reg_values,unsigned int * regno_ptr)10068 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
10069 				 unsigned int *regno_ptr)
10070 {
10071   unsigned int argno, regno;
10072   HOST_WIDE_INT offset, required_offset;
10073   rtx addr, base;
10074 
10075   /* Check that this is a word-mode store.  */
10076   if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
10077     return false;
10078 
10079   /* Check that the register being saved is an unmodified argument
10080      register.  */
10081   regno = REGNO (src);
10082   if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
10083     return false;
10084   argno = regno - GP_ARG_FIRST;
10085 
10086   /* Check whether the address is an appropriate stack-pointer or
10087      frame-pointer access.  */
10088   addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
10089   mips_split_plus (addr, &base, &offset);
10090   required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
10091   if (base == hard_frame_pointer_rtx)
10092     required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
10093   else if (base != stack_pointer_rtx)
10094     return false;
10095   if (offset != required_offset)
10096     return false;
10097 
10098   *regno_ptr = regno;
10099   return true;
10100 }
10101 
10102 /* A subroutine of mips_expand_prologue, called only when generating
10103    MIPS16e SAVE instructions.  Search the start of the function for any
10104    instructions that save argument registers into their caller-allocated
10105    save slots.  Delete such instructions and return a value N such that
10106    saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
10107    instructions redundant.  */
10108 
10109 static unsigned int
mips16e_collect_argument_saves(void)10110 mips16e_collect_argument_saves (void)
10111 {
10112   rtx reg_values[FIRST_PSEUDO_REGISTER];
10113   rtx_insn *insn, *next;
10114   rtx set, dest, src;
10115   unsigned int nargs, regno;
10116 
10117   push_topmost_sequence ();
10118   nargs = 0;
10119   memset (reg_values, 0, sizeof (reg_values));
10120   for (insn = get_insns (); insn; insn = next)
10121     {
10122       next = NEXT_INSN (insn);
10123       if (NOTE_P (insn) || DEBUG_INSN_P (insn))
10124 	continue;
10125 
10126       if (!INSN_P (insn))
10127 	break;
10128 
10129       set = PATTERN (insn);
10130       if (GET_CODE (set) != SET)
10131 	break;
10132 
10133       dest = SET_DEST (set);
10134       src = SET_SRC (set);
10135       if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
10136 	{
10137 	  if (!BITSET_P (cfun->machine->frame.mask, regno))
10138 	    {
10139 	      delete_insn (insn);
10140 	      nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
10141 	    }
10142 	}
10143       else if (REG_P (dest) && GET_MODE (dest) == word_mode)
10144 	reg_values[REGNO (dest)]
10145 	  = mips16e_collect_propagate_value (src, reg_values);
10146       else
10147 	break;
10148     }
10149   pop_topmost_sequence ();
10150 
10151   return nargs;
10152 }
10153 
10154 /* Return a move between register REGNO and memory location SP + OFFSET.
10155    REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
10156    Make the move a load if RESTORE_P, otherwise make it a store.  */
10157 
10158 static rtx
mips16e_save_restore_reg(bool restore_p,bool reg_parm_p,HOST_WIDE_INT offset,unsigned int regno)10159 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
10160 			  HOST_WIDE_INT offset, unsigned int regno)
10161 {
10162   rtx reg, mem;
10163 
10164   mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
10165 					      offset));
10166   reg = gen_rtx_REG (SImode, regno);
10167   if (restore_p)
10168     {
10169       mips_add_cfa_restore (reg);
10170       return gen_rtx_SET (reg, mem);
10171     }
10172   if (reg_parm_p)
10173     return gen_rtx_SET (mem, reg);
10174   return mips_frame_set (mem, reg);
10175 }
10176 
10177 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
10178    The instruction must:
10179 
10180      - Allocate or deallocate SIZE bytes in total; SIZE is known
10181        to be nonzero.
10182 
10183      - Save or restore as many registers in *MASK_PTR as possible.
10184        The instruction saves the first registers at the top of the
10185        allocated area, with the other registers below it.
10186 
10187      - Save NARGS argument registers above the allocated area.
10188 
10189    (NARGS is always zero if RESTORE_P.)
10190 
10191    The SAVE and RESTORE instructions cannot save and restore all general
10192    registers, so there may be some registers left over for the caller to
10193    handle.  Destructively modify *MASK_PTR so that it contains the registers
10194    that still need to be saved or restored.  The caller can save these
10195    registers in the memory immediately below *OFFSET_PTR, which is a
10196    byte offset from the bottom of the allocated stack area.  */
10197 
10198 static rtx
mips16e_build_save_restore(bool restore_p,unsigned int * mask_ptr,HOST_WIDE_INT * offset_ptr,unsigned int nargs,HOST_WIDE_INT size)10199 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
10200 			    HOST_WIDE_INT *offset_ptr, unsigned int nargs,
10201 			    HOST_WIDE_INT size)
10202 {
10203   rtx pattern, set;
10204   HOST_WIDE_INT offset, top_offset;
10205   unsigned int i, regno;
10206   int n;
10207 
10208   gcc_assert (cfun->machine->frame.num_fp == 0);
10209 
10210   /* Calculate the number of elements in the PARALLEL.  We need one element
10211      for the stack adjustment, one for each argument register save, and one
10212      for each additional register move.  */
10213   n = 1 + nargs;
10214   for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
10215     if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
10216       n++;
10217 
10218   /* Create the final PARALLEL.  */
10219   pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
10220   n = 0;
10221 
10222   /* Add the stack pointer adjustment.  */
10223   set = gen_rtx_SET (stack_pointer_rtx,
10224 		     plus_constant (Pmode, stack_pointer_rtx,
10225 				    restore_p ? size : -size));
10226   RTX_FRAME_RELATED_P (set) = 1;
10227   XVECEXP (pattern, 0, n++) = set;
10228 
10229   /* Stack offsets in the PARALLEL are relative to the old stack pointer.  */
10230   top_offset = restore_p ? size : 0;
10231 
10232   /* Save the arguments.  */
10233   for (i = 0; i < nargs; i++)
10234     {
10235       offset = top_offset + i * UNITS_PER_WORD;
10236       set = mips16e_save_restore_reg (restore_p, true, offset,
10237 				      GP_ARG_FIRST + i);
10238       XVECEXP (pattern, 0, n++) = set;
10239     }
10240 
10241   /* Then fill in the other register moves.  */
10242   offset = top_offset;
10243   for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
10244     {
10245       regno = mips16e_save_restore_regs[i];
10246       if (BITSET_P (*mask_ptr, regno))
10247 	{
10248 	  offset -= UNITS_PER_WORD;
10249 	  set = mips16e_save_restore_reg (restore_p, false, offset, regno);
10250 	  XVECEXP (pattern, 0, n++) = set;
10251 	  *mask_ptr &= ~(1 << regno);
10252 	}
10253     }
10254 
10255   /* Tell the caller what offset it should use for the remaining registers.  */
10256   *offset_ptr = size + (offset - top_offset);
10257 
10258   gcc_assert (n == XVECLEN (pattern, 0));
10259 
10260   return pattern;
10261 }
10262 
10263 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
10264    pointer.  Return true if PATTERN matches the kind of instruction
10265    generated by mips16e_build_save_restore.  If INFO is nonnull,
10266    initialize it when returning true.  */
10267 
10268 bool
mips16e_save_restore_pattern_p(rtx pattern,HOST_WIDE_INT adjust,struct mips16e_save_restore_info * info)10269 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
10270 				struct mips16e_save_restore_info *info)
10271 {
10272   unsigned int i, nargs, mask, extra;
10273   HOST_WIDE_INT top_offset, save_offset, offset;
10274   rtx set, reg, mem, base;
10275   int n;
10276 
10277   if (!GENERATE_MIPS16E_SAVE_RESTORE)
10278     return false;
10279 
10280   /* Stack offsets in the PARALLEL are relative to the old stack pointer.  */
10281   top_offset = adjust > 0 ? adjust : 0;
10282 
10283   /* Interpret all other members of the PARALLEL.  */
10284   save_offset = top_offset - UNITS_PER_WORD;
10285   mask = 0;
10286   nargs = 0;
10287   i = 0;
10288   for (n = 1; n < XVECLEN (pattern, 0); n++)
10289     {
10290       /* Check that we have a SET.  */
10291       set = XVECEXP (pattern, 0, n);
10292       if (GET_CODE (set) != SET)
10293 	return false;
10294 
10295       /* Check that the SET is a load (if restoring) or a store
10296 	 (if saving).  */
10297       mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
10298       if (!MEM_P (mem))
10299 	return false;
10300 
10301       /* Check that the address is the sum of the stack pointer and a
10302 	 possibly-zero constant offset.  */
10303       mips_split_plus (XEXP (mem, 0), &base, &offset);
10304       if (base != stack_pointer_rtx)
10305 	return false;
10306 
10307       /* Check that SET's other operand is a register.  */
10308       reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
10309       if (!REG_P (reg))
10310 	return false;
10311 
10312       /* Check for argument saves.  */
10313       if (offset == top_offset + nargs * UNITS_PER_WORD
10314 	  && REGNO (reg) == GP_ARG_FIRST + nargs)
10315 	nargs++;
10316       else if (offset == save_offset)
10317 	{
10318 	  while (mips16e_save_restore_regs[i++] != REGNO (reg))
10319 	    if (i == ARRAY_SIZE (mips16e_save_restore_regs))
10320 	      return false;
10321 
10322 	  mask |= 1 << REGNO (reg);
10323 	  save_offset -= UNITS_PER_WORD;
10324 	}
10325       else
10326 	return false;
10327     }
10328 
10329   /* Check that the restrictions on register ranges are met.  */
10330   extra = 0;
10331   mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
10332 			  ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
10333   mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
10334 			  ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
10335   if (extra != 0)
10336     return false;
10337 
10338   /* Make sure that the topmost argument register is not saved twice.
10339      The checks above ensure that the same is then true for the other
10340      argument registers.  */
10341   if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
10342     return false;
10343 
10344   /* Pass back information, if requested.  */
10345   if (info)
10346     {
10347       info->nargs = nargs;
10348       info->mask = mask;
10349       info->size = (adjust > 0 ? adjust : -adjust);
10350     }
10351 
10352   return true;
10353 }
10354 
10355 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
10356    for the register range [MIN_REG, MAX_REG].  Return a pointer to
10357    the null terminator.  */
10358 
10359 static char *
mips16e_add_register_range(char * s,unsigned int min_reg,unsigned int max_reg)10360 mips16e_add_register_range (char *s, unsigned int min_reg,
10361 			    unsigned int max_reg)
10362 {
10363   if (min_reg != max_reg)
10364     s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
10365   else
10366     s += sprintf (s, ",%s", reg_names[min_reg]);
10367   return s;
10368 }
10369 
10370 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
10371    PATTERN and ADJUST are as for mips16e_save_restore_pattern_p.  */
10372 
10373 const char *
mips16e_output_save_restore(rtx pattern,HOST_WIDE_INT adjust)10374 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
10375 {
10376   static char buffer[300];
10377 
10378   struct mips16e_save_restore_info info;
10379   unsigned int i, end;
10380   char *s;
10381 
10382   /* Parse the pattern.  */
10383   if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
10384     gcc_unreachable ();
10385 
10386   /* Add the mnemonic.  */
10387   s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
10388   s += strlen (s);
10389 
10390   /* Save the arguments.  */
10391   if (info.nargs > 1)
10392     s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
10393 		  reg_names[GP_ARG_FIRST + info.nargs - 1]);
10394   else if (info.nargs == 1)
10395     s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
10396 
10397   /* Emit the amount of stack space to allocate or deallocate.  */
10398   s += sprintf (s, "%d", (int) info.size);
10399 
10400   /* Save or restore $16.  */
10401   if (BITSET_P (info.mask, 16))
10402     s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
10403 
10404   /* Save or restore $17.  */
10405   if (BITSET_P (info.mask, 17))
10406     s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
10407 
10408   /* Save or restore registers in the range $s2...$s8, which
10409      mips16e_s2_s8_regs lists in decreasing order.  Note that this
10410      is a software register range; the hardware registers are not
10411      numbered consecutively.  */
10412   end = ARRAY_SIZE (mips16e_s2_s8_regs);
10413   i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
10414   if (i < end)
10415     s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
10416 				    mips16e_s2_s8_regs[i]);
10417 
10418   /* Save or restore registers in the range $a0...$a3.  */
10419   end = ARRAY_SIZE (mips16e_a0_a3_regs);
10420   i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
10421   if (i < end)
10422     s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
10423 				    mips16e_a0_a3_regs[end - 1]);
10424 
10425   /* Save or restore $31.  */
10426   if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
10427     s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
10428 
10429   return buffer;
10430 }
10431 
10432 /* Return true if the current function returns its value in a floating-point
10433    register in MIPS16 mode.  */
10434 
10435 static bool
mips16_cfun_returns_in_fpr_p(void)10436 mips16_cfun_returns_in_fpr_p (void)
10437 {
10438   tree return_type = DECL_RESULT (current_function_decl);
10439   return (TARGET_MIPS16
10440 	  && TARGET_HARD_FLOAT_ABI
10441 	  && !aggregate_value_p (return_type, current_function_decl)
10442  	  && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
10443 }
10444 
10445 /* Return true if predicate PRED is true for at least one instruction.
10446    Cache the result in *CACHE, and assume that the result is true
10447    if *CACHE is already true.  */
10448 
10449 static bool
mips_find_gp_ref(bool * cache,bool (* pred)(rtx_insn *))10450 mips_find_gp_ref (bool *cache, bool (*pred) (rtx_insn *))
10451 {
10452   rtx_insn *insn, *subinsn;
10453 
10454   if (!*cache)
10455     {
10456       push_topmost_sequence ();
10457       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10458 	FOR_EACH_SUBINSN (subinsn, insn)
10459 	  if (USEFUL_INSN_P (subinsn) && pred (subinsn))
10460 	    {
10461 	      *cache = true;
10462 	      break;
10463 	    }
10464       pop_topmost_sequence ();
10465     }
10466   return *cache;
10467 }
10468 
10469 /* Return true if INSN refers to the global pointer in an "inflexible" way.
10470    See mips_cfun_has_inflexible_gp_ref_p for details.  */
10471 
10472 static bool
mips_insn_has_inflexible_gp_ref_p(rtx_insn * insn)10473 mips_insn_has_inflexible_gp_ref_p (rtx_insn *insn)
10474 {
10475   /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
10476      indicate that the target could be a traditional MIPS
10477      lazily-binding stub.  */
10478   return find_reg_fusage (insn, USE, pic_offset_table_rtx);
10479 }
10480 
10481 /* Return true if the current function refers to the global pointer
10482    in a way that forces $28 to be valid.  This means that we can't
10483    change the choice of global pointer, even for NewABI code.
10484 
10485    One example of this (and one which needs several checks) is that
10486    $28 must be valid when calling traditional MIPS lazy-binding stubs.
10487    (This restriction does not apply to PLTs.)  */
10488 
10489 static bool
mips_cfun_has_inflexible_gp_ref_p(void)10490 mips_cfun_has_inflexible_gp_ref_p (void)
10491 {
10492   /* If the function has a nonlocal goto, $28 must hold the correct
10493      global pointer for the target function.  That is, the target
10494      of the goto implicitly uses $28.  */
10495   if (crtl->has_nonlocal_goto)
10496     return true;
10497 
10498   if (TARGET_ABICALLS_PIC2)
10499     {
10500       /* Symbolic accesses implicitly use the global pointer unless
10501 	 -mexplicit-relocs is in effect.  JAL macros to symbolic addresses
10502 	 might go to traditional MIPS lazy-binding stubs.  */
10503       if (!TARGET_EXPLICIT_RELOCS)
10504 	return true;
10505 
10506       /* FUNCTION_PROFILER includes a JAL to _mcount, which again
10507 	 can be lazily-bound.  */
10508       if (crtl->profile)
10509 	return true;
10510 
10511       /* MIPS16 functions that return in FPRs need to call an
10512 	 external libgcc routine.  This call is only made explict
10513 	 during mips_expand_epilogue, and it too might be lazily bound.  */
10514       if (mips16_cfun_returns_in_fpr_p ())
10515 	return true;
10516     }
10517 
10518   return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
10519 			   mips_insn_has_inflexible_gp_ref_p);
10520 }
10521 
10522 /* Return true if INSN refers to the global pointer in a "flexible" way.
10523    See mips_cfun_has_flexible_gp_ref_p for details.  */
10524 
10525 static bool
mips_insn_has_flexible_gp_ref_p(rtx_insn * insn)10526 mips_insn_has_flexible_gp_ref_p (rtx_insn *insn)
10527 {
10528   return (get_attr_got (insn) != GOT_UNSET
10529 	  || mips_small_data_pattern_p (PATTERN (insn))
10530 	  || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
10531 }
10532 
10533 /* Return true if the current function references the global pointer,
10534    but if those references do not inherently require the global pointer
10535    to be $28.  Assume !mips_cfun_has_inflexible_gp_ref_p ().  */
10536 
10537 static bool
mips_cfun_has_flexible_gp_ref_p(void)10538 mips_cfun_has_flexible_gp_ref_p (void)
10539 {
10540   /* Reload can sometimes introduce constant pool references
10541      into a function that otherwise didn't need them.  For example,
10542      suppose we have an instruction like:
10543 
10544 	(set (reg:DF R1) (float:DF (reg:SI R2)))
10545 
10546      If R2 turns out to be a constant such as 1, the instruction may
10547      have a REG_EQUAL note saying that R1 == 1.0.  Reload then has
10548      the option of using this constant if R2 doesn't get allocated
10549      to a register.
10550 
10551      In cases like these, reload will have added the constant to the
10552      pool but no instruction will yet refer to it.  */
10553   if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
10554     return true;
10555 
10556   return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
10557 			   mips_insn_has_flexible_gp_ref_p);
10558 }
10559 
10560 /* Return the register that should be used as the global pointer
10561    within this function.  Return INVALID_REGNUM if the function
10562    doesn't need a global pointer.  */
10563 
10564 static unsigned int
mips_global_pointer(void)10565 mips_global_pointer (void)
10566 {
10567   unsigned int regno;
10568 
10569   /* $gp is always available unless we're using a GOT.  */
10570   if (!TARGET_USE_GOT)
10571     return GLOBAL_POINTER_REGNUM;
10572 
10573   /* If there are inflexible references to $gp, we must use the
10574      standard register.  */
10575   if (mips_cfun_has_inflexible_gp_ref_p ())
10576     return GLOBAL_POINTER_REGNUM;
10577 
10578   /* If there are no current references to $gp, then the only uses
10579      we can introduce later are those involved in long branches.  */
10580   if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
10581     return INVALID_REGNUM;
10582 
10583   /* If the global pointer is call-saved, try to use a call-clobbered
10584      alternative.  */
10585   if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
10586     for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
10587       if (!df_regs_ever_live_p (regno)
10588 	  && call_really_used_regs[regno]
10589 	  && !fixed_regs[regno]
10590 	  && regno != PIC_FUNCTION_ADDR_REGNUM)
10591 	return regno;
10592 
10593   return GLOBAL_POINTER_REGNUM;
10594 }
10595 
10596 /* Return true if the current function's prologue must load the global
10597    pointer value into pic_offset_table_rtx and store the same value in
10598    the function's cprestore slot (if any).
10599 
10600    One problem we have to deal with is that, when emitting GOT-based
10601    position independent code, long-branch sequences will need to load
10602    the address of the branch target from the GOT.  We don't know until
10603    the very end of compilation whether (and where) the function needs
10604    long branches, so we must ensure that _any_ branch can access the
10605    global pointer in some form.  However, we do not want to pessimize
10606    the usual case in which all branches are short.
10607 
10608    We handle this as follows:
10609 
10610    (1) During reload, we set cfun->machine->global_pointer to
10611        INVALID_REGNUM if we _know_ that the current function
10612        doesn't need a global pointer.  This is only valid if
10613        long branches don't need the GOT.
10614 
10615        Otherwise, we assume that we might need a global pointer
10616        and pick an appropriate register.
10617 
10618    (2) If cfun->machine->global_pointer != INVALID_REGNUM,
10619        we ensure that the global pointer is available at every
10620        block boundary bar entry and exit.  We do this in one of two ways:
10621 
10622        - If the function has a cprestore slot, we ensure that this
10623 	 slot is valid at every branch.  However, as explained in
10624 	 point (6) below, there is no guarantee that pic_offset_table_rtx
10625 	 itself is valid if new uses of the global pointer are introduced
10626 	 after the first post-epilogue split.
10627 
10628 	 We guarantee that the cprestore slot is valid by loading it
10629 	 into a fake register, CPRESTORE_SLOT_REGNUM.  We then make
10630 	 this register live at every block boundary bar function entry
10631 	 and exit.  It is then invalid to move the load (and thus the
10632 	 preceding store) across a block boundary.
10633 
10634        - If the function has no cprestore slot, we guarantee that
10635 	 pic_offset_table_rtx itself is valid at every branch.
10636 
10637        See mips_eh_uses for the handling of the register liveness.
10638 
10639    (3) During prologue and epilogue generation, we emit "ghost"
10640        placeholder instructions to manipulate the global pointer.
10641 
10642    (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
10643        and cfun->machine->must_restore_gp_when_clobbered_p if we already know
10644        that the function needs a global pointer.  (There is no need to set
10645        them earlier than this, and doing it as late as possible leads to
10646        fewer false positives.)
10647 
10648    (5) If cfun->machine->must_initialize_gp_p is true during a
10649        split_insns pass, we split the ghost instructions into real
10650        instructions.  These split instructions can then be optimized in
10651        the usual way.  Otherwise, we keep the ghost instructions intact,
10652        and optimize for the case where they aren't needed.  We still
10653        have the option of splitting them later, if we need to introduce
10654        new uses of the global pointer.
10655 
10656        For example, the scheduler ignores a ghost instruction that
10657        stores $28 to the stack, but it handles the split form of
10658        the ghost instruction as an ordinary store.
10659 
10660    (6) [OldABI only.]  If cfun->machine->must_restore_gp_when_clobbered_p
10661        is true during the first post-epilogue split_insns pass, we split
10662        calls and restore_gp patterns into instructions that explicitly
10663        load pic_offset_table_rtx from the cprestore slot.  Otherwise,
10664        we split these patterns into instructions that _don't_ load from
10665        the cprestore slot.
10666 
10667        If cfun->machine->must_restore_gp_when_clobbered_p is true at the
10668        time of the split, then any instructions that exist at that time
10669        can make free use of pic_offset_table_rtx.  However, if we want
10670        to introduce new uses of the global pointer after the split,
10671        we must explicitly load the value from the cprestore slot, since
10672        pic_offset_table_rtx itself might not be valid at a given point
10673        in the function.
10674 
10675        The idea is that we want to be able to delete redundant
10676        loads from the cprestore slot in the usual case where no
10677        long branches are needed.
10678 
10679    (7) If cfun->machine->must_initialize_gp_p is still false at the end
10680        of md_reorg, we decide whether the global pointer is needed for
10681        long branches.  If so, we set cfun->machine->must_initialize_gp_p
10682        to true and split the ghost instructions into real instructions
10683        at that stage.
10684 
10685    Note that the ghost instructions must have a zero length for three reasons:
10686 
10687    - Giving the length of the underlying $gp sequence might cause
10688      us to use long branches in cases where they aren't really needed.
10689 
10690    - They would perturb things like alignment calculations.
10691 
10692    - More importantly, the hazard detection in md_reorg relies on
10693      empty instructions having a zero length.
10694 
10695    If we find a long branch and split the ghost instructions at the
10696    end of md_reorg, the split could introduce more long branches.
10697    That isn't a problem though, because we still do the split before
10698    the final shorten_branches pass.
10699 
10700    This is extremely ugly, but it seems like the best compromise between
10701    correctness and efficiency.  */
10702 
10703 bool
mips_must_initialize_gp_p(void)10704 mips_must_initialize_gp_p (void)
10705 {
10706   return cfun->machine->must_initialize_gp_p;
10707 }
10708 
10709 /* Return true if REGNO is a register that is ordinarily call-clobbered
10710    but must nevertheless be preserved by an interrupt handler.  */
10711 
10712 static bool
mips_interrupt_extra_call_saved_reg_p(unsigned int regno)10713 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
10714 {
10715   if ((ISA_HAS_HILO || TARGET_DSP)
10716       && MD_REG_P (regno))
10717     return true;
10718 
10719   if (TARGET_DSP && DSP_ACC_REG_P (regno))
10720     return true;
10721 
10722   if (GP_REG_P (regno)
10723       && cfun->machine->use_shadow_register_set == SHADOW_SET_NO)
10724     {
10725       /* $0 is hard-wired.  */
10726       if (regno == GP_REG_FIRST)
10727 	return false;
10728 
10729       /* The interrupt handler can treat kernel registers as
10730 	 scratch registers.  */
10731       if (KERNEL_REG_P (regno))
10732 	return false;
10733 
10734       /* The function will return the stack pointer to its original value
10735 	 anyway.  */
10736       if (regno == STACK_POINTER_REGNUM)
10737 	return false;
10738 
10739       /* Otherwise, return true for registers that aren't ordinarily
10740 	 call-clobbered.  */
10741       return call_really_used_regs[regno];
10742     }
10743 
10744   return false;
10745 }
10746 
10747 /* Return true if the current function should treat register REGNO
10748    as call-saved.  */
10749 
10750 static bool
mips_cfun_call_saved_reg_p(unsigned int regno)10751 mips_cfun_call_saved_reg_p (unsigned int regno)
10752 {
10753   /* If the user makes an ordinarily-call-saved register global,
10754      that register is no longer call-saved.  */
10755   if (global_regs[regno])
10756     return false;
10757 
10758   /* Interrupt handlers need to save extra registers.  */
10759   if (cfun->machine->interrupt_handler_p
10760       && mips_interrupt_extra_call_saved_reg_p (regno))
10761     return true;
10762 
10763   /* call_insns preserve $28 unless they explicitly say otherwise,
10764      so call_really_used_regs[] treats $28 as call-saved.  However,
10765      we want the ABI property rather than the default call_insn
10766      property here.  */
10767   return (regno == GLOBAL_POINTER_REGNUM
10768 	  ? TARGET_CALL_SAVED_GP
10769 	  : !call_really_used_regs[regno]);
10770 }
10771 
10772 /* Return true if the function body might clobber register REGNO.
10773    We know that REGNO is call-saved.  */
10774 
10775 static bool
mips_cfun_might_clobber_call_saved_reg_p(unsigned int regno)10776 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
10777 {
10778   /* Some functions should be treated as clobbering all call-saved
10779      registers.  */
10780   if (crtl->saves_all_registers)
10781     return true;
10782 
10783   /* DF handles cases where a register is explicitly referenced in
10784      the rtl.  Incoming values are passed in call-clobbered registers,
10785      so we can assume that any live call-saved register is set within
10786      the function.  */
10787   if (df_regs_ever_live_p (regno))
10788     return true;
10789 
10790   /* Check for registers that are clobbered by FUNCTION_PROFILER.
10791      These clobbers are not explicit in the rtl.  */
10792   if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
10793     return true;
10794 
10795   /* If we're using a call-saved global pointer, the function's
10796      prologue will need to set it up.  */
10797   if (cfun->machine->global_pointer == regno)
10798     return true;
10799 
10800   /* The function's prologue will need to set the frame pointer if
10801      frame_pointer_needed.  */
10802   if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
10803     return true;
10804 
10805   /* If a MIPS16 function returns a value in FPRs, its epilogue
10806      will need to call an external libgcc routine.  This yet-to-be
10807      generated call_insn will clobber $31.  */
10808   if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
10809     return true;
10810 
10811   /* If REGNO is ordinarily call-clobbered, we must assume that any
10812      called function could modify it.  */
10813   if (cfun->machine->interrupt_handler_p
10814       && !crtl->is_leaf
10815       && mips_interrupt_extra_call_saved_reg_p (regno))
10816     return true;
10817 
10818   return false;
10819 }
10820 
10821 /* Return true if the current function must save register REGNO.  */
10822 
10823 static bool
mips_save_reg_p(unsigned int regno)10824 mips_save_reg_p (unsigned int regno)
10825 {
10826   if (mips_cfun_call_saved_reg_p (regno))
10827     {
10828       if (mips_cfun_might_clobber_call_saved_reg_p (regno))
10829 	return true;
10830 
10831       /* Save both registers in an FPR pair if either one is used.  This is
10832 	 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
10833 	 register to be used without the even register.  */
10834       if (FP_REG_P (regno)
10835 	  && MAX_FPRS_PER_FMT == 2
10836 	  && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
10837 	return true;
10838     }
10839 
10840   /* We need to save the incoming return address if __builtin_eh_return
10841      is being used to set a different return address.  */
10842   if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
10843     return true;
10844 
10845   return false;
10846 }
10847 
10848 /* Populate the current function's mips_frame_info structure.
10849 
10850    MIPS stack frames look like:
10851 
10852 	+-------------------------------+
10853 	|                               |
10854 	|  incoming stack arguments     |
10855 	|                               |
10856 	+-------------------------------+
10857 	|                               |
10858 	|  caller-allocated save area   |
10859       A |  for register arguments       |
10860 	|                               |
10861 	+-------------------------------+ <-- incoming stack pointer
10862 	|                               |
10863 	|  callee-allocated save area   |
10864       B |  for arguments that are       |
10865 	|  split between registers and  |
10866 	|  the stack                    |
10867 	|                               |
10868 	+-------------------------------+ <-- arg_pointer_rtx
10869 	|                               |
10870       C |  callee-allocated save area   |
10871 	|  for register varargs         |
10872 	|                               |
10873 	+-------------------------------+ <-- frame_pointer_rtx
10874 	|                               |       + cop0_sp_offset
10875 	|  COP0 reg save area           |	+ UNITS_PER_WORD
10876 	|                               |
10877 	+-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
10878 	|                               |       + UNITS_PER_WORD
10879 	|  accumulator save area        |
10880 	|                               |
10881 	+-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
10882 	|                               |       + UNITS_PER_HWFPVALUE
10883 	|  FPR save area                |
10884 	|                               |
10885 	+-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
10886 	|                               |       + UNITS_PER_WORD
10887 	|  GPR save area                |
10888 	|                               |
10889 	+-------------------------------+ <-- frame_pointer_rtx with
10890 	|                               | \     -fstack-protector
10891 	|  local variables              |  | var_size
10892 	|                               | /
10893 	+-------------------------------+
10894 	|                               | \
10895 	|  $gp save area                |  | cprestore_size
10896 	|                               | /
10897       P +-------------------------------+ <-- hard_frame_pointer_rtx for
10898 	|                               | \     MIPS16 code
10899 	|  outgoing stack arguments     |  |
10900 	|                               |  |
10901 	+-------------------------------+  | args_size
10902 	|                               |  |
10903 	|  caller-allocated save area   |  |
10904 	|  for register arguments       |  |
10905 	|                               | /
10906 	+-------------------------------+ <-- stack_pointer_rtx
10907 					      frame_pointer_rtx without
10908 					        -fstack-protector
10909 					      hard_frame_pointer_rtx for
10910 						non-MIPS16 code.
10911 
10912    At least two of A, B and C will be empty.
10913 
10914    Dynamic stack allocations such as alloca insert data at point P.
10915    They decrease stack_pointer_rtx but leave frame_pointer_rtx and
10916    hard_frame_pointer_rtx unchanged.  */
10917 
10918 static void
mips_compute_frame_info(void)10919 mips_compute_frame_info (void)
10920 {
10921   struct mips_frame_info *frame;
10922   HOST_WIDE_INT offset, size;
10923   unsigned int regno, i;
10924 
10925   /* Skip re-computing the frame info after reload completed.  */
10926   if (reload_completed)
10927     return;
10928 
10929   /* Set this function's interrupt properties.  */
10930   if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
10931     {
10932       if (mips_isa_rev < 2)
10933 	error ("the %<interrupt%> attribute requires a MIPS32r2 processor or greater");
10934       else if (TARGET_MIPS16)
10935 	error ("interrupt handlers cannot be MIPS16 functions");
10936       else
10937 	{
10938 	  cfun->machine->interrupt_handler_p = true;
10939 	  cfun->machine->int_mask =
10940 	    mips_interrupt_mask (TREE_TYPE (current_function_decl));
10941 	  cfun->machine->use_shadow_register_set =
10942 	    mips_use_shadow_register_set (TREE_TYPE (current_function_decl));
10943 	  cfun->machine->keep_interrupts_masked_p =
10944 	    mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
10945 	  cfun->machine->use_debug_exception_return_p =
10946 	    mips_use_debug_exception_return_p (TREE_TYPE
10947 					       (current_function_decl));
10948 	}
10949     }
10950 
10951   frame = &cfun->machine->frame;
10952   memset (frame, 0, sizeof (*frame));
10953   size = get_frame_size ();
10954 
10955   /* The first two blocks contain the outgoing argument area and the $gp save
10956      slot.  This area isn't needed in leaf functions.  We can also skip it
10957      if we know that none of the called functions will use this space.
10958 
10959      But if the target-independent frame size is nonzero, we have already
10960      committed to allocating these in TARGET_STARTING_FRAME_OFFSET for
10961      !FRAME_GROWS_DOWNWARD.  */
10962 
10963   if ((size == 0 || FRAME_GROWS_DOWNWARD)
10964       && (crtl->is_leaf || (cfun->machine->optimize_call_stack && !flag_pic)))
10965     {
10966       /* The MIPS 3.0 linker does not like functions that dynamically
10967 	 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
10968 	 looks like we are trying to create a second frame pointer to the
10969 	 function, so allocate some stack space to make it happy.  */
10970       if (cfun->calls_alloca)
10971 	frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
10972       else
10973 	frame->args_size = 0;
10974       frame->cprestore_size = 0;
10975     }
10976   else
10977     {
10978       frame->args_size = crtl->outgoing_args_size;
10979       frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
10980     }
10981 
10982   /* MIPS16 code offsets the frame pointer by the size of the outgoing
10983      arguments.  This tends to increase the chances of using unextended
10984      instructions for local variables and incoming arguments.  */
10985   if (TARGET_MIPS16)
10986     frame->hard_frame_pointer_offset = frame->args_size;
10987 
10988   /* PR 69129 / 69012: Beware of a possible race condition.  mips_global_pointer
10989      might call mips_cfun_has_inflexible_gp_ref_p which in turn can call
10990      mips_find_gp_ref which will iterate over the current insn sequence.
10991      If any of these insns use the cprestore_save_slot_operand or
10992      cprestore_load_slot_operand predicates in order to be recognised then
10993      they will call mips_cprestore_address_p which calls
10994      mips_get_cprestore_base_and_offset which expects the frame information
10995      to be filled in...  In fact mips_get_cprestore_base_and_offset only
10996      needs the args_size and hard_frame_pointer_offset fields to be filled
10997      in, which is why the global_pointer field is initialised here and not
10998      earlier.  */
10999   cfun->machine->global_pointer = mips_global_pointer ();
11000 
11001   offset = frame->args_size + frame->cprestore_size;
11002 
11003   /* Move above the local variables.  */
11004   frame->var_size = MIPS_STACK_ALIGN (size);
11005   offset += frame->var_size;
11006 
11007   /* Find out which GPRs we need to save.  */
11008   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
11009     if (mips_save_reg_p (regno))
11010       {
11011 	frame->num_gp++;
11012 	frame->mask |= 1 << (regno - GP_REG_FIRST);
11013       }
11014 
11015   /* If this function calls eh_return, we must also save and restore the
11016      EH data registers.  */
11017   if (crtl->calls_eh_return)
11018     for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
11019       {
11020 	frame->num_gp++;
11021 	frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
11022       }
11023 
11024   /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
11025      $a3-$a0 and $s2-$s8.  If we save one register in the range, we must
11026      save all later registers too.  */
11027   if (GENERATE_MIPS16E_SAVE_RESTORE)
11028     {
11029       mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
11030  			      ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
11031       mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
11032  			      ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
11033     }
11034 
11035   /* Move above the GPR save area.  */
11036   if (frame->num_gp > 0)
11037     {
11038       offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
11039       frame->gp_sp_offset = offset - UNITS_PER_WORD;
11040     }
11041 
11042   /* Find out which FPRs we need to save.  This loop must iterate over
11043      the same space as its companion in mips_for_each_saved_gpr_and_fpr.  */
11044   if (TARGET_HARD_FLOAT)
11045     for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
11046       if (mips_save_reg_p (regno))
11047 	{
11048 	  frame->num_fp += MAX_FPRS_PER_FMT;
11049 	  frame->fmask |= ~(~0U << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
11050 	}
11051 
11052   /* Move above the FPR save area.  */
11053   if (frame->num_fp > 0)
11054     {
11055       offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
11056       frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
11057     }
11058 
11059   /* Add in space for the interrupt context information.  */
11060   if (cfun->machine->interrupt_handler_p)
11061     {
11062       /* Check HI/LO.  */
11063       if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
11064 	{
11065 	  frame->num_acc++;
11066 	  frame->acc_mask |= (1 << 0);
11067 	}
11068 
11069       /* Check accumulators 1, 2, 3.  */
11070       for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
11071 	if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
11072 	  {
11073 	    frame->num_acc++;
11074 	    frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
11075 	  }
11076 
11077       /* All interrupt context functions need space to preserve STATUS.  */
11078       frame->num_cop0_regs++;
11079 
11080       /* We need to save EPC regardless of whether interrupts remain masked
11081 	 as exceptions will corrupt EPC.  */
11082       frame->num_cop0_regs++;
11083     }
11084 
11085   /* Move above the accumulator save area.  */
11086   if (frame->num_acc > 0)
11087     {
11088       /* Each accumulator needs 2 words.  */
11089       offset += frame->num_acc * 2 * UNITS_PER_WORD;
11090       frame->acc_sp_offset = offset - UNITS_PER_WORD;
11091     }
11092 
11093   /* Move above the COP0 register save area.  */
11094   if (frame->num_cop0_regs > 0)
11095     {
11096       offset += frame->num_cop0_regs * UNITS_PER_WORD;
11097       frame->cop0_sp_offset = offset - UNITS_PER_WORD;
11098     }
11099 
11100   /* Determine if we can save the callee-saved registers in the frame
11101      header.  Restrict this to functions where there is no other reason
11102      to allocate stack space so that we can eliminate the instructions
11103      that modify the stack pointer.  */
11104 
11105   if (TARGET_OLDABI
11106       && optimize > 0
11107       && flag_frame_header_optimization
11108       && !MAIN_NAME_P (DECL_NAME (current_function_decl))
11109       && cfun->machine->varargs_size == 0
11110       && crtl->args.pretend_args_size == 0
11111       && frame->var_size == 0
11112       && frame->num_acc == 0
11113       && frame->num_cop0_regs == 0
11114       && frame->num_fp == 0
11115       && frame->num_gp > 0
11116       && frame->num_gp <= MAX_ARGS_IN_REGISTERS
11117       && !GENERATE_MIPS16E_SAVE_RESTORE
11118       && !cfun->machine->interrupt_handler_p
11119       && cfun->machine->does_not_use_frame_header
11120       && cfun->machine->optimize_call_stack
11121       && !cfun->machine->callers_may_not_allocate_frame
11122       && !mips_cfun_has_cprestore_slot_p ())
11123     {
11124       offset = 0;
11125       frame->gp_sp_offset = REG_PARM_STACK_SPACE(cfun) - UNITS_PER_WORD;
11126       cfun->machine->use_frame_header_for_callee_saved_regs = true;
11127     }
11128 
11129   /* Move above the callee-allocated varargs save area.  */
11130   offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
11131   frame->arg_pointer_offset = offset;
11132 
11133   /* Move above the callee-allocated area for pretend stack arguments.  */
11134   offset += crtl->args.pretend_args_size;
11135   frame->total_size = offset;
11136 
11137   /* Work out the offsets of the save areas from the top of the frame.  */
11138   if (frame->gp_sp_offset > 0)
11139     frame->gp_save_offset = frame->gp_sp_offset - offset;
11140   if (frame->fp_sp_offset > 0)
11141     frame->fp_save_offset = frame->fp_sp_offset - offset;
11142   if (frame->acc_sp_offset > 0)
11143     frame->acc_save_offset = frame->acc_sp_offset - offset;
11144   if (frame->num_cop0_regs > 0)
11145     frame->cop0_save_offset = frame->cop0_sp_offset - offset;
11146 }
11147 
11148 /* Return the style of GP load sequence that is being used for the
11149    current function.  */
11150 
11151 enum mips_loadgp_style
mips_current_loadgp_style(void)11152 mips_current_loadgp_style (void)
11153 {
11154   if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
11155     return LOADGP_NONE;
11156 
11157   if (TARGET_RTP_PIC)
11158     return LOADGP_RTP;
11159 
11160   if (TARGET_ABSOLUTE_ABICALLS)
11161     return LOADGP_ABSOLUTE;
11162 
11163   return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
11164 }
11165 
11166 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
11167 
11168 static bool
mips_frame_pointer_required(void)11169 mips_frame_pointer_required (void)
11170 {
11171   /* If the function contains dynamic stack allocations, we need to
11172      use the frame pointer to access the static parts of the frame.  */
11173   if (cfun->calls_alloca)
11174     return true;
11175 
11176   /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
11177      reload may be unable to compute the address of a local variable,
11178      since there is no way to add a large constant to the stack pointer
11179      without using a second temporary register.  */
11180   if (TARGET_MIPS16)
11181     {
11182       mips_compute_frame_info ();
11183       if (!SMALL_OPERAND (cfun->machine->frame.total_size))
11184 	return true;
11185     }
11186 
11187   return false;
11188 }
11189 
11190 /* Make sure that we're not trying to eliminate to the wrong hard frame
11191    pointer.  */
11192 
11193 static bool
mips_can_eliminate(const int from ATTRIBUTE_UNUSED,const int to)11194 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
11195 {
11196   return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
11197 }
11198 
11199 /* Implement INITIAL_ELIMINATION_OFFSET.  FROM is either the frame pointer
11200    or argument pointer.  TO is either the stack pointer or hard frame
11201    pointer.  */
11202 
11203 HOST_WIDE_INT
mips_initial_elimination_offset(int from,int to)11204 mips_initial_elimination_offset (int from, int to)
11205 {
11206   HOST_WIDE_INT offset;
11207 
11208   mips_compute_frame_info ();
11209 
11210   /* Set OFFSET to the offset from the end-of-prologue stack pointer.  */
11211   switch (from)
11212     {
11213     case FRAME_POINTER_REGNUM:
11214       if (FRAME_GROWS_DOWNWARD)
11215 	offset = (cfun->machine->frame.args_size
11216 		  + cfun->machine->frame.cprestore_size
11217 		  + cfun->machine->frame.var_size);
11218       else
11219 	offset = 0;
11220       break;
11221 
11222     case ARG_POINTER_REGNUM:
11223       offset = cfun->machine->frame.arg_pointer_offset;
11224       break;
11225 
11226     default:
11227       gcc_unreachable ();
11228     }
11229 
11230   if (to == HARD_FRAME_POINTER_REGNUM)
11231     offset -= cfun->machine->frame.hard_frame_pointer_offset;
11232 
11233   return offset;
11234 }
11235 
11236 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY.  */
11237 
11238 static void
mips_extra_live_on_entry(bitmap regs)11239 mips_extra_live_on_entry (bitmap regs)
11240 {
11241   if (TARGET_USE_GOT)
11242     {
11243       /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
11244 	 the global pointer.   */
11245       if (!TARGET_ABSOLUTE_ABICALLS)
11246 	bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
11247 
11248       /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
11249 	 the global pointer.  */
11250       if (TARGET_MIPS16)
11251 	bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
11252 
11253       /* See the comment above load_call<mode> for details.  */
11254       bitmap_set_bit (regs, GOT_VERSION_REGNUM);
11255     }
11256 }
11257 
11258 /* Implement RETURN_ADDR_RTX.  We do not support moving back to a
11259    previous frame.  */
11260 
11261 rtx
mips_return_addr(int count,rtx frame ATTRIBUTE_UNUSED)11262 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
11263 {
11264   if (count != 0)
11265     return const0_rtx;
11266 
11267   return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
11268 }
11269 
11270 /* Emit code to change the current function's return address to
11271    ADDRESS.  SCRATCH is available as a scratch register, if needed.
11272    ADDRESS and SCRATCH are both word-mode GPRs.  */
11273 
11274 void
mips_set_return_address(rtx address,rtx scratch)11275 mips_set_return_address (rtx address, rtx scratch)
11276 {
11277   rtx slot_address;
11278 
11279   gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
11280   slot_address = mips_add_offset (scratch, stack_pointer_rtx,
11281 				  cfun->machine->frame.gp_sp_offset);
11282   mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
11283 }
11284 
11285 /* Return true if the current function has a cprestore slot.  */
11286 
11287 bool
mips_cfun_has_cprestore_slot_p(void)11288 mips_cfun_has_cprestore_slot_p (void)
11289 {
11290   return (cfun->machine->global_pointer != INVALID_REGNUM
11291 	  && cfun->machine->frame.cprestore_size > 0);
11292 }
11293 
11294 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
11295    cprestore slot.  LOAD_P is true if the caller wants to load from
11296    the cprestore slot; it is false if the caller wants to store to
11297    the slot.  */
11298 
11299 static void
mips_get_cprestore_base_and_offset(rtx * base,HOST_WIDE_INT * offset,bool load_p)11300 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
11301 				    bool load_p)
11302 {
11303   const struct mips_frame_info *frame;
11304 
11305   frame = &cfun->machine->frame;
11306   /* .cprestore always uses the stack pointer instead of the frame pointer.
11307      We have a free choice for direct stores for non-MIPS16 functions,
11308      and for MIPS16 functions whose cprestore slot is in range of the
11309      stack pointer.  Using the stack pointer would sometimes give more
11310      (early) scheduling freedom, but using the frame pointer would
11311      sometimes give more (late) scheduling freedom.  It's hard to
11312      predict which applies to a given function, so let's keep things
11313      simple.
11314 
11315      Loads must always use the frame pointer in functions that call
11316      alloca, and there's little benefit to using the stack pointer
11317      otherwise.  */
11318   if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
11319     {
11320       *base = hard_frame_pointer_rtx;
11321       *offset = frame->args_size - frame->hard_frame_pointer_offset;
11322     }
11323   else
11324     {
11325       *base = stack_pointer_rtx;
11326       *offset = frame->args_size;
11327     }
11328 }
11329 
11330 /* Return true if X is the load or store address of the cprestore slot;
11331    LOAD_P says which.  */
11332 
11333 bool
mips_cprestore_address_p(rtx x,bool load_p)11334 mips_cprestore_address_p (rtx x, bool load_p)
11335 {
11336   rtx given_base, required_base;
11337   HOST_WIDE_INT given_offset, required_offset;
11338 
11339   mips_split_plus (x, &given_base, &given_offset);
11340   mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
11341   return given_base == required_base && given_offset == required_offset;
11342 }
11343 
11344 /* Return a MEM rtx for the cprestore slot.  LOAD_P is true if we are
11345    going to load from it, false if we are going to store to it.
11346    Use TEMP as a temporary register if need be.  */
11347 
11348 static rtx
mips_cprestore_slot(rtx temp,bool load_p)11349 mips_cprestore_slot (rtx temp, bool load_p)
11350 {
11351   rtx base;
11352   HOST_WIDE_INT offset;
11353 
11354   mips_get_cprestore_base_and_offset (&base, &offset, load_p);
11355   return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
11356 }
11357 
11358 /* Emit instructions to save global pointer value GP into cprestore
11359    slot MEM.  OFFSET is the offset that MEM applies to the base register.
11360 
11361    MEM may not be a legitimate address.  If it isn't, TEMP is a
11362    temporary register that can be used, otherwise it is a SCRATCH.  */
11363 
11364 void
mips_save_gp_to_cprestore_slot(rtx mem,rtx offset,rtx gp,rtx temp)11365 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
11366 {
11367   if (TARGET_CPRESTORE_DIRECTIVE)
11368     {
11369       gcc_assert (gp == pic_offset_table_rtx);
11370       emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
11371     }
11372   else
11373     mips_emit_move (mips_cprestore_slot (temp, false), gp);
11374 }
11375 
11376 /* Restore $gp from its save slot, using TEMP as a temporary base register
11377    if need be.  This function is for o32 and o64 abicalls only.
11378 
11379    See mips_must_initialize_gp_p for details about how we manage the
11380    global pointer.  */
11381 
11382 void
mips_restore_gp_from_cprestore_slot(rtx temp)11383 mips_restore_gp_from_cprestore_slot (rtx temp)
11384 {
11385   gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
11386 
11387   if (!cfun->machine->must_restore_gp_when_clobbered_p)
11388     {
11389       emit_note (NOTE_INSN_DELETED);
11390       return;
11391     }
11392 
11393   if (TARGET_MIPS16)
11394     {
11395       mips_emit_move (temp, mips_cprestore_slot (temp, true));
11396       mips_emit_move (pic_offset_table_rtx, temp);
11397     }
11398   else
11399     mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
11400   if (!TARGET_EXPLICIT_RELOCS)
11401     emit_insn (gen_blockage ());
11402 }
11403 
11404 /* A function to save or store a register.  The first argument is the
11405    register and the second is the stack slot.  */
11406 typedef void (*mips_save_restore_fn) (rtx, rtx);
11407 
11408 /* Use FN to save or restore register REGNO.  MODE is the register's
11409    mode and OFFSET is the offset of its save slot from the current
11410    stack pointer.  */
11411 
11412 static void
mips_save_restore_reg(machine_mode mode,int regno,HOST_WIDE_INT offset,mips_save_restore_fn fn)11413 mips_save_restore_reg (machine_mode mode, int regno,
11414 		       HOST_WIDE_INT offset, mips_save_restore_fn fn)
11415 {
11416   rtx mem;
11417 
11418   mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
11419 					    offset));
11420   fn (gen_rtx_REG (mode, regno), mem);
11421 }
11422 
11423 /* Call FN for each accumulator that is saved by the current function.
11424    SP_OFFSET is the offset of the current stack pointer from the start
11425    of the frame.  */
11426 
11427 static void
mips_for_each_saved_acc(HOST_WIDE_INT sp_offset,mips_save_restore_fn fn)11428 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
11429 {
11430   HOST_WIDE_INT offset;
11431   int regno;
11432 
11433   offset = cfun->machine->frame.acc_sp_offset - sp_offset;
11434   if (BITSET_P (cfun->machine->frame.acc_mask, 0))
11435     {
11436       mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
11437       offset -= UNITS_PER_WORD;
11438       mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
11439       offset -= UNITS_PER_WORD;
11440     }
11441 
11442   for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
11443     if (BITSET_P (cfun->machine->frame.acc_mask,
11444 		  ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
11445       {
11446 	mips_save_restore_reg (word_mode, regno, offset, fn);
11447 	offset -= UNITS_PER_WORD;
11448       }
11449 }
11450 
11451 /* Save register REG to MEM.  Make the instruction frame-related.  */
11452 
11453 static void
mips_save_reg(rtx reg,rtx mem)11454 mips_save_reg (rtx reg, rtx mem)
11455 {
11456   if (GET_MODE (reg) == DFmode
11457       && (!TARGET_FLOAT64
11458 	  || mips_abi == ABI_32))
11459     {
11460       rtx x1, x2;
11461 
11462       mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
11463 
11464       x1 = mips_frame_set (mips_subword (mem, false),
11465 			   mips_subword (reg, false));
11466       x2 = mips_frame_set (mips_subword (mem, true),
11467 			   mips_subword (reg, true));
11468       mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
11469     }
11470   else
11471     mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
11472 }
11473 
11474 /* Capture the register combinations that are allowed in a SWM or LWM
11475    instruction.  The entries are ordered by number of registers set in
11476    the mask.  We also ignore the single register encodings because a
11477    normal SW/LW is preferred.  */
11478 
11479 static const unsigned int umips_swm_mask[17] = {
11480   0xc0ff0000, 0x80ff0000, 0x40ff0000, 0x807f0000,
11481   0x00ff0000, 0x803f0000, 0x007f0000, 0x801f0000,
11482   0x003f0000, 0x800f0000, 0x001f0000, 0x80070000,
11483   0x000f0000, 0x80030000, 0x00070000, 0x80010000,
11484   0x00030000
11485 };
11486 
11487 static const unsigned int umips_swm_encoding[17] = {
11488   25, 24, 9, 23, 8, 22, 7, 21, 6, 20, 5, 19, 4, 18, 3, 17, 2
11489 };
11490 
11491 /* Try to use a microMIPS LWM or SWM instruction to save or restore
11492    as many GPRs in *MASK as possible.  *OFFSET is the offset from the
11493    stack pointer of the topmost save slot.
11494 
11495    Remove from *MASK all registers that were handled using LWM and SWM.
11496    Update *OFFSET so that it points to the first unused save slot.  */
11497 
11498 static bool
umips_build_save_restore(mips_save_restore_fn fn,unsigned * mask,HOST_WIDE_INT * offset)11499 umips_build_save_restore (mips_save_restore_fn fn,
11500 			  unsigned *mask, HOST_WIDE_INT *offset)
11501 {
11502   int nregs;
11503   unsigned int i, j;
11504   rtx pattern, set, reg, mem;
11505   HOST_WIDE_INT this_offset;
11506   rtx this_base;
11507 
11508   /* Try matching $16 to $31 (s0 to ra).  */
11509   for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
11510     if ((*mask & 0xffff0000) == umips_swm_mask[i])
11511       break;
11512 
11513   if (i == ARRAY_SIZE (umips_swm_mask))
11514     return false;
11515 
11516   /* Get the offset of the lowest save slot.  */
11517   nregs = (umips_swm_encoding[i] & 0xf) + (umips_swm_encoding[i] >> 4);
11518   this_offset = *offset - UNITS_PER_WORD * (nregs - 1);
11519 
11520   /* LWM/SWM can only support offsets from -2048 to 2047.  */
11521   if (!UMIPS_12BIT_OFFSET_P (this_offset))
11522     return false;
11523 
11524   /* Create the final PARALLEL.  */
11525   pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
11526   this_base = stack_pointer_rtx;
11527 
11528   /* For registers $16-$23 and $30.  */
11529   for (j = 0; j < (umips_swm_encoding[i] & 0xf); j++)
11530     {
11531       HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
11532       mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
11533       unsigned int regno = (j != 8) ? 16 + j : 30;
11534       *mask &= ~(1 << regno);
11535       reg = gen_rtx_REG (SImode, regno);
11536       if (fn == mips_save_reg)
11537 	set = mips_frame_set (mem, reg);
11538       else
11539 	{
11540 	  set = gen_rtx_SET (reg, mem);
11541 	  mips_add_cfa_restore (reg);
11542 	}
11543       XVECEXP (pattern, 0, j) = set;
11544     }
11545 
11546   /* For register $31.  */
11547   if (umips_swm_encoding[i] >> 4)
11548     {
11549       HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
11550       *mask &= ~(1 << 31);
11551       mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
11552       reg = gen_rtx_REG (SImode, 31);
11553       if (fn == mips_save_reg)
11554 	set = mips_frame_set (mem, reg);
11555       else
11556 	{
11557 	  set = gen_rtx_SET (reg, mem);
11558 	  mips_add_cfa_restore (reg);
11559 	}
11560       XVECEXP (pattern, 0, j) = set;
11561     }
11562 
11563   pattern = emit_insn (pattern);
11564   if (fn == mips_save_reg)
11565     RTX_FRAME_RELATED_P (pattern) = 1;
11566 
11567   /* Adjust the last offset.  */
11568   *offset -= UNITS_PER_WORD * nregs;
11569 
11570   return true;
11571 }
11572 
11573 /* Call FN for each register that is saved by the current function.
11574    SP_OFFSET is the offset of the current stack pointer from the start
11575    of the frame.  */
11576 
11577 static void
mips_for_each_saved_gpr_and_fpr(HOST_WIDE_INT sp_offset,mips_save_restore_fn fn)11578 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
11579 				 mips_save_restore_fn fn)
11580 {
11581   machine_mode fpr_mode;
11582   int regno;
11583   const struct mips_frame_info *frame = &cfun->machine->frame;
11584   HOST_WIDE_INT offset;
11585   unsigned int mask;
11586 
11587   /* Save registers starting from high to low.  The debuggers prefer at least
11588      the return register be stored at func+4, and also it allows us not to
11589      need a nop in the epilogue if at least one register is reloaded in
11590      addition to return address.  */
11591   offset = frame->gp_sp_offset - sp_offset;
11592   mask = frame->mask;
11593 
11594   if (TARGET_MICROMIPS)
11595     umips_build_save_restore (fn, &mask, &offset);
11596 
11597   for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
11598     if (BITSET_P (mask, regno - GP_REG_FIRST))
11599       {
11600 	/* Record the ra offset for use by mips_function_profiler.  */
11601 	if (regno == RETURN_ADDR_REGNUM)
11602 	  cfun->machine->frame.ra_fp_offset = offset + sp_offset;
11603 	mips_save_restore_reg (word_mode, regno, offset, fn);
11604 	offset -= UNITS_PER_WORD;
11605       }
11606 
11607   /* This loop must iterate over the same space as its companion in
11608      mips_compute_frame_info.  */
11609   offset = cfun->machine->frame.fp_sp_offset - sp_offset;
11610   fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
11611   for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
11612        regno >= FP_REG_FIRST;
11613        regno -= MAX_FPRS_PER_FMT)
11614     if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
11615       {
11616 	if (!TARGET_FLOAT64 && TARGET_DOUBLE_FLOAT
11617 	    && (fixed_regs[regno] || fixed_regs[regno + 1]))
11618 	  {
11619 	    if (fixed_regs[regno])
11620 	      mips_save_restore_reg (SFmode, regno + 1, offset, fn);
11621 	    else
11622 	      mips_save_restore_reg (SFmode, regno, offset, fn);
11623 	  }
11624 	else
11625 	  mips_save_restore_reg (fpr_mode, regno, offset, fn);
11626 	offset -= GET_MODE_SIZE (fpr_mode);
11627       }
11628 }
11629 
11630 /* Return true if a move between register REGNO and its save slot (MEM)
11631    can be done in a single move.  LOAD_P is true if we are loading
11632    from the slot, false if we are storing to it.  */
11633 
11634 static bool
mips_direct_save_slot_move_p(unsigned int regno,rtx mem,bool load_p)11635 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
11636 {
11637   /* There is a specific MIPS16 instruction for saving $31 to the stack.  */
11638   if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
11639     return false;
11640 
11641   return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
11642 				      GET_MODE (mem), mem, load_p) == NO_REGS;
11643 }
11644 
11645 /* Emit a move from SRC to DEST, given that one of them is a register
11646    save slot and that the other is a register.  TEMP is a temporary
11647    GPR of the same mode that is available if need be.  */
11648 
11649 void
mips_emit_save_slot_move(rtx dest,rtx src,rtx temp)11650 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
11651 {
11652   unsigned int regno;
11653   rtx mem;
11654 
11655   if (REG_P (src))
11656     {
11657       regno = REGNO (src);
11658       mem = dest;
11659     }
11660   else
11661     {
11662       regno = REGNO (dest);
11663       mem = src;
11664     }
11665 
11666   if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
11667     {
11668       /* We don't yet know whether we'll need this instruction or not.
11669 	 Postpone the decision by emitting a ghost move.  This move
11670 	 is specifically not frame-related; only the split version is.  */
11671       if (TARGET_64BIT)
11672 	emit_insn (gen_move_gpdi (dest, src));
11673       else
11674 	emit_insn (gen_move_gpsi (dest, src));
11675       return;
11676     }
11677 
11678   if (regno == HI_REGNUM)
11679     {
11680       if (REG_P (dest))
11681 	{
11682 	  mips_emit_move (temp, src);
11683 	  if (TARGET_64BIT)
11684 	    emit_insn (gen_mthidi_ti (gen_rtx_REG (TImode, MD_REG_FIRST),
11685 				      temp, gen_rtx_REG (DImode, LO_REGNUM)));
11686 	  else
11687 	    emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
11688 				      temp, gen_rtx_REG (SImode, LO_REGNUM)));
11689 	}
11690       else
11691 	{
11692 	  if (TARGET_64BIT)
11693 	    emit_insn (gen_mfhidi_ti (temp,
11694 				      gen_rtx_REG (TImode, MD_REG_FIRST)));
11695 	  else
11696 	    emit_insn (gen_mfhisi_di (temp,
11697 				      gen_rtx_REG (DImode, MD_REG_FIRST)));
11698 	  mips_emit_move (dest, temp);
11699 	}
11700     }
11701   else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
11702     mips_emit_move (dest, src);
11703   else
11704     {
11705       gcc_assert (!reg_overlap_mentioned_p (dest, temp));
11706       mips_emit_move (temp, src);
11707       mips_emit_move (dest, temp);
11708     }
11709   if (MEM_P (dest))
11710     mips_set_frame_expr (mips_frame_set (dest, src));
11711 }
11712 
11713 /* If we're generating n32 or n64 abicalls, and the current function
11714    does not use $28 as its global pointer, emit a cplocal directive.
11715    Use pic_offset_table_rtx as the argument to the directive.  */
11716 
11717 static void
mips_output_cplocal(void)11718 mips_output_cplocal (void)
11719 {
11720   if (!TARGET_EXPLICIT_RELOCS
11721       && mips_must_initialize_gp_p ()
11722       && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
11723     output_asm_insn (".cplocal %+", 0);
11724 }
11725 
11726 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE.  */
11727 
11728 static void
mips_output_function_prologue(FILE * file)11729 mips_output_function_prologue (FILE *file)
11730 {
11731   const char *fnname;
11732 
11733   /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
11734      floating-point arguments.  */
11735   if (TARGET_MIPS16
11736       && TARGET_HARD_FLOAT_ABI
11737       && crtl->args.info.fp_code != 0)
11738     mips16_build_function_stub ();
11739 
11740   /* Get the function name the same way that toplev.c does before calling
11741      assemble_start_function.  This is needed so that the name used here
11742      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
11743   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
11744   mips_start_function_definition (fnname, TARGET_MIPS16);
11745 
11746   /* Output MIPS-specific frame information.  */
11747   if (!flag_inhibit_size_directive)
11748     {
11749       const struct mips_frame_info *frame;
11750 
11751       frame = &cfun->machine->frame;
11752 
11753       /* .frame FRAMEREG, FRAMESIZE, RETREG.  */
11754       fprintf (file,
11755 	       "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
11756 	       "# vars= " HOST_WIDE_INT_PRINT_DEC
11757 	       ", regs= %d/%d"
11758 	       ", args= " HOST_WIDE_INT_PRINT_DEC
11759 	       ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
11760 	       reg_names[frame_pointer_needed
11761 			 ? HARD_FRAME_POINTER_REGNUM
11762 			 : STACK_POINTER_REGNUM],
11763 	       (frame_pointer_needed
11764 		? frame->total_size - frame->hard_frame_pointer_offset
11765 		: frame->total_size),
11766 	       reg_names[RETURN_ADDR_REGNUM],
11767 	       frame->var_size,
11768 	       frame->num_gp, frame->num_fp,
11769 	       frame->args_size,
11770 	       frame->cprestore_size);
11771 
11772       /* .mask MASK, OFFSET.  */
11773       fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
11774 	       frame->mask, frame->gp_save_offset);
11775 
11776       /* .fmask MASK, OFFSET.  */
11777       fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
11778 	       frame->fmask, frame->fp_save_offset);
11779     }
11780 
11781   /* Handle the initialization of $gp for SVR4 PIC, if applicable.
11782      Also emit the ".set noreorder; .set nomacro" sequence for functions
11783      that need it.  */
11784   if (mips_must_initialize_gp_p ()
11785       && mips_current_loadgp_style () == LOADGP_OLDABI)
11786     {
11787       if (TARGET_MIPS16)
11788 	{
11789 	  /* This is a fixed-form sequence.  The position of the
11790 	     first two instructions is important because of the
11791 	     way _gp_disp is defined.  */
11792 	  output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
11793 	  output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
11794 	  output_asm_insn ("sll\t$2,16", 0);
11795 	  output_asm_insn ("addu\t$2,$3", 0);
11796 	}
11797       else
11798 	{
11799 	  /* .cpload must be in a .set noreorder but not a
11800 	     .set nomacro block.  */
11801 	  mips_push_asm_switch (&mips_noreorder);
11802 	  output_asm_insn (".cpload\t%^", 0);
11803 	  if (!cfun->machine->all_noreorder_p)
11804 	    mips_pop_asm_switch (&mips_noreorder);
11805 	  else
11806 	    mips_push_asm_switch (&mips_nomacro);
11807 	}
11808     }
11809   else if (cfun->machine->all_noreorder_p)
11810     {
11811       mips_push_asm_switch (&mips_noreorder);
11812       mips_push_asm_switch (&mips_nomacro);
11813     }
11814 
11815   /* Tell the assembler which register we're using as the global
11816      pointer.  This is needed for thunks, since they can use either
11817      explicit relocs or assembler macros.  */
11818   mips_output_cplocal ();
11819 }
11820 
11821 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE.  */
11822 
11823 static void
mips_output_function_epilogue(FILE *)11824 mips_output_function_epilogue (FILE *)
11825 {
11826   const char *fnname;
11827 
11828   /* Reinstate the normal $gp.  */
11829   SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
11830   mips_output_cplocal ();
11831 
11832   if (cfun->machine->all_noreorder_p)
11833     {
11834       mips_pop_asm_switch (&mips_nomacro);
11835       mips_pop_asm_switch (&mips_noreorder);
11836     }
11837 
11838   /* Get the function name the same way that toplev.c does before calling
11839      assemble_start_function.  This is needed so that the name used here
11840      exactly matches the name used in ASM_DECLARE_FUNCTION_NAME.  */
11841   fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
11842   mips_end_function_definition (fnname);
11843 }
11844 
11845 /* Emit an optimisation barrier for accesses to the current frame.  */
11846 
11847 static void
mips_frame_barrier(void)11848 mips_frame_barrier (void)
11849 {
11850   emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
11851 }
11852 
11853 
11854 /* The __gnu_local_gp symbol.  */
11855 
11856 static GTY(()) rtx mips_gnu_local_gp;
11857 
11858 /* If we're generating n32 or n64 abicalls, emit instructions
11859    to set up the global pointer.  */
11860 
11861 static void
mips_emit_loadgp(void)11862 mips_emit_loadgp (void)
11863 {
11864   rtx addr, offset, incoming_address, base, index, pic_reg;
11865 
11866   pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
11867   switch (mips_current_loadgp_style ())
11868     {
11869     case LOADGP_ABSOLUTE:
11870       if (mips_gnu_local_gp == NULL)
11871 	{
11872 	  mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
11873 	  SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
11874 	}
11875       emit_insn (PMODE_INSN (gen_loadgp_absolute,
11876 			     (pic_reg, mips_gnu_local_gp)));
11877       break;
11878 
11879     case LOADGP_OLDABI:
11880       /* Added by mips_output_function_prologue.  */
11881       break;
11882 
11883     case LOADGP_NEWABI:
11884       addr = XEXP (DECL_RTL (current_function_decl), 0);
11885       offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
11886       incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
11887       emit_insn (PMODE_INSN (gen_loadgp_newabi,
11888 			     (pic_reg, offset, incoming_address)));
11889       break;
11890 
11891     case LOADGP_RTP:
11892       base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
11893       index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
11894       emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
11895       break;
11896 
11897     default:
11898       return;
11899     }
11900 
11901   if (TARGET_MIPS16)
11902     emit_insn (PMODE_INSN (gen_copygp_mips16,
11903 			   (pic_offset_table_rtx, pic_reg)));
11904 
11905   /* Emit a blockage if there are implicit uses of the GP register.
11906      This includes profiled functions, because FUNCTION_PROFILE uses
11907      a jal macro.  */
11908   if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
11909     emit_insn (gen_loadgp_blockage ());
11910 }
11911 
11912 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
11913 
11914 #if PROBE_INTERVAL > 32768
11915 #error Cannot use indexed addressing mode for stack probing
11916 #endif
11917 
11918 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
11919    inclusive.  These are offsets from the current stack pointer.  */
11920 
11921 static void
mips_emit_probe_stack_range(HOST_WIDE_INT first,HOST_WIDE_INT size)11922 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
11923 {
11924   if (TARGET_MIPS16)
11925     sorry ("-fstack-check=specific not implemented for MIPS16");
11926 
11927   /* See if we have a constant small number of probes to generate.  If so,
11928      that's the easy case.  */
11929   if (first + size <= 32768)
11930     {
11931       HOST_WIDE_INT i;
11932 
11933       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
11934 	 it exceeds SIZE.  If only one probe is needed, this will not
11935 	 generate any code.  Then probe at FIRST + SIZE.  */
11936       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
11937         emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
11938 					 -(first + i)));
11939 
11940       emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
11941 				       -(first + size)));
11942     }
11943 
11944   /* Otherwise, do the same as above, but in a loop.  Note that we must be
11945      extra careful with variables wrapping around because we might be at
11946      the very top (or the very bottom) of the address space and we have
11947      to be able to handle this case properly; in particular, we use an
11948      equality test for the loop condition.  */
11949   else
11950     {
11951       HOST_WIDE_INT rounded_size;
11952       rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
11953       rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
11954 
11955       /* Sanity check for the addressing mode we're going to use.  */
11956       gcc_assert (first <= 32768);
11957 
11958 
11959       /* Step 1: round SIZE to the previous multiple of the interval.  */
11960 
11961       rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
11962 
11963 
11964       /* Step 2: compute initial and final value of the loop counter.  */
11965 
11966       /* TEST_ADDR = SP + FIRST.  */
11967       emit_insn (gen_rtx_SET (r3, plus_constant (Pmode, stack_pointer_rtx,
11968 						 -first)));
11969 
11970       /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE.  */
11971       if (rounded_size > 32768)
11972 	{
11973           emit_move_insn (r12, GEN_INT (rounded_size));
11974 	  emit_insn (gen_rtx_SET (r12, gen_rtx_MINUS (Pmode, r3, r12)));
11975 	}
11976       else
11977 	emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, r3,
11978 						    -rounded_size)));
11979 
11980 
11981       /* Step 3: the loop
11982 
11983 	do
11984 	  {
11985 	    TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
11986 	    probe at TEST_ADDR
11987 	  }
11988 	while (TEST_ADDR != LAST_ADDR)
11989 
11990 	probes at FIRST + N * PROBE_INTERVAL for values of N from 1
11991 	until it is equal to ROUNDED_SIZE.  */
11992 
11993       emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
11994 
11995 
11996       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
11997 	 that SIZE is equal to ROUNDED_SIZE.  */
11998 
11999       if (size != rounded_size)
12000 	emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
12001     }
12002 
12003   /* Make sure nothing is scheduled before we are done.  */
12004   emit_insn (gen_blockage ());
12005 }
12006 
12007 /* Probe a range of stack addresses from REG1 to REG2 inclusive.  These are
12008    absolute addresses.  */
12009 
12010 const char *
mips_output_probe_stack_range(rtx reg1,rtx reg2)12011 mips_output_probe_stack_range (rtx reg1, rtx reg2)
12012 {
12013   static int labelno = 0;
12014   char loop_lab[32], tmp[64];
12015   rtx xops[2];
12016 
12017   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
12018 
12019   /* Loop.  */
12020   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
12021 
12022   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
12023   xops[0] = reg1;
12024   xops[1] = GEN_INT (-PROBE_INTERVAL);
12025   if (TARGET_64BIT && TARGET_LONG64)
12026     output_asm_insn ("daddiu\t%0,%0,%1", xops);
12027   else
12028     output_asm_insn ("addiu\t%0,%0,%1", xops);
12029 
12030   /* Probe at TEST_ADDR, test if TEST_ADDR == LAST_ADDR and branch.  */
12031   xops[1] = reg2;
12032   strcpy (tmp, "%(%<bne\t%0,%1,");
12033   output_asm_insn (strcat (tmp, &loop_lab[1]), xops);
12034   if (TARGET_64BIT)
12035     output_asm_insn ("sd\t$0,0(%0)%)", xops);
12036   else
12037     output_asm_insn ("sw\t$0,0(%0)%)", xops);
12038 
12039   return "";
12040 }
12041 
12042 /* Return true if X contains a kernel register.  */
12043 
12044 static bool
mips_refers_to_kernel_reg_p(const_rtx x)12045 mips_refers_to_kernel_reg_p (const_rtx x)
12046 {
12047   subrtx_iterator::array_type array;
12048   FOR_EACH_SUBRTX (iter, array, x, NONCONST)
12049     if (REG_P (*iter) && KERNEL_REG_P (REGNO (*iter)))
12050       return true;
12051   return false;
12052 }
12053 
12054 /* Expand the "prologue" pattern.  */
12055 
12056 void
mips_expand_prologue(void)12057 mips_expand_prologue (void)
12058 {
12059   const struct mips_frame_info *frame;
12060   HOST_WIDE_INT size;
12061   unsigned int nargs;
12062 
12063   if (cfun->machine->global_pointer != INVALID_REGNUM)
12064     {
12065       /* Check whether an insn uses pic_offset_table_rtx, either explicitly
12066 	 or implicitly.  If so, we can commit to using a global pointer
12067 	 straight away, otherwise we need to defer the decision.  */
12068       if (mips_cfun_has_inflexible_gp_ref_p ()
12069 	  || mips_cfun_has_flexible_gp_ref_p ())
12070 	{
12071 	  cfun->machine->must_initialize_gp_p = true;
12072 	  cfun->machine->must_restore_gp_when_clobbered_p = true;
12073 	}
12074 
12075       SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
12076     }
12077 
12078   frame = &cfun->machine->frame;
12079   size = frame->total_size;
12080 
12081   if (flag_stack_usage_info)
12082     current_function_static_stack_size = size;
12083 
12084   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK
12085       || flag_stack_clash_protection)
12086     {
12087       if (crtl->is_leaf && !cfun->calls_alloca)
12088 	{
12089 	  if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
12090 	    mips_emit_probe_stack_range (get_stack_check_protect (),
12091 					 size - get_stack_check_protect ());
12092 	}
12093       else if (size > 0)
12094 	mips_emit_probe_stack_range (get_stack_check_protect (), size);
12095     }
12096 
12097   /* Save the registers.  Allocate up to MIPS_MAX_FIRST_STACK_STEP
12098      bytes beforehand; this is enough to cover the register save area
12099      without going out of range.  */
12100   if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
12101       || frame->num_cop0_regs > 0)
12102     {
12103       HOST_WIDE_INT step1;
12104 
12105       step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
12106       if (GENERATE_MIPS16E_SAVE_RESTORE)
12107  	{
12108  	  HOST_WIDE_INT offset;
12109  	  unsigned int mask, regno;
12110 
12111 	  /* Try to merge argument stores into the save instruction.  */
12112 	  nargs = mips16e_collect_argument_saves ();
12113 
12114 	  /* Build the save instruction.  */
12115 	  mask = frame->mask;
12116 	  rtx insn = mips16e_build_save_restore (false, &mask, &offset,
12117 						 nargs, step1);
12118 	  RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
12119 	  mips_frame_barrier ();
12120  	  size -= step1;
12121 
12122  	  /* Check if we need to save other registers.  */
12123  	  for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
12124  	    if (BITSET_P (mask, regno - GP_REG_FIRST))
12125  	      {
12126 		offset -= UNITS_PER_WORD;
12127 		mips_save_restore_reg (word_mode, regno,
12128 				       offset, mips_save_reg);
12129  	      }
12130  	}
12131       else
12132  	{
12133 	  if (cfun->machine->interrupt_handler_p)
12134 	    {
12135 	      HOST_WIDE_INT offset;
12136 	      rtx mem;
12137 
12138 	      /* If this interrupt is using a shadow register set, we need to
12139 		 get the stack pointer from the previous register set.  */
12140 	      if (cfun->machine->use_shadow_register_set == SHADOW_SET_YES)
12141 		emit_insn (PMODE_INSN (gen_mips_rdpgpr, (stack_pointer_rtx,
12142 							 stack_pointer_rtx)));
12143 
12144 	      if (!cfun->machine->keep_interrupts_masked_p)
12145 		{
12146 		  if (cfun->machine->int_mask == INT_MASK_EIC)
12147 		    /* Move from COP0 Cause to K0.  */
12148 		    emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
12149 			gen_rtx_REG (SImode, COP0_CAUSE_REG_NUM)));
12150 		}
12151 	      /* Move from COP0 EPC to K1.  */
12152 	      emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
12153 					gen_rtx_REG (SImode,
12154 						     COP0_EPC_REG_NUM)));
12155 
12156 	      /* Allocate the first part of the frame.  */
12157 	      rtx insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
12158 					GEN_INT (-step1));
12159 	      RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
12160 	      mips_frame_barrier ();
12161 	      size -= step1;
12162 
12163 	      /* Start at the uppermost location for saving.  */
12164 	      offset = frame->cop0_sp_offset - size;
12165 
12166 	      /* Push EPC into its stack slot.  */
12167 	      mem = gen_frame_mem (word_mode,
12168 				   plus_constant (Pmode, stack_pointer_rtx,
12169 						  offset));
12170 	      mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
12171 	      offset -= UNITS_PER_WORD;
12172 
12173 	      /* Move from COP0 Status to K1.  */
12174 	      emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
12175 					gen_rtx_REG (SImode,
12176 						     COP0_STATUS_REG_NUM)));
12177 
12178 	      /* Right justify the RIPL in k0.  */
12179 	      if (!cfun->machine->keep_interrupts_masked_p
12180 		  && cfun->machine->int_mask == INT_MASK_EIC)
12181 		emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
12182 					gen_rtx_REG (SImode, K0_REG_NUM),
12183 					GEN_INT (CAUSE_IPL)));
12184 
12185 	      /* Push Status into its stack slot.  */
12186 	      mem = gen_frame_mem (word_mode,
12187 				   plus_constant (Pmode, stack_pointer_rtx,
12188 						  offset));
12189 	      mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
12190 	      offset -= UNITS_PER_WORD;
12191 
12192 	      /* Insert the RIPL into our copy of SR (k1) as the new IPL.  */
12193 	      if (!cfun->machine->keep_interrupts_masked_p
12194 		  && cfun->machine->int_mask == INT_MASK_EIC)
12195 		emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12196 				       GEN_INT (6),
12197 				       GEN_INT (SR_IPL),
12198 				       gen_rtx_REG (SImode, K0_REG_NUM)));
12199 
12200 	      /* Clear all interrupt mask bits up to and including the
12201 		 handler's interrupt line.  */
12202 	      if (!cfun->machine->keep_interrupts_masked_p
12203 		  && cfun->machine->int_mask != INT_MASK_EIC)
12204 		emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12205 				       GEN_INT (cfun->machine->int_mask + 1),
12206 				       GEN_INT (SR_IM0),
12207 				       gen_rtx_REG (SImode, GP_REG_FIRST)));
12208 
12209 	      if (!cfun->machine->keep_interrupts_masked_p)
12210 		/* Enable interrupts by clearing the KSU ERL and EXL bits.
12211 		   IE is already the correct value, so we don't have to do
12212 		   anything explicit.  */
12213 		emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12214 				       GEN_INT (4),
12215 				       GEN_INT (SR_EXL),
12216 				       gen_rtx_REG (SImode, GP_REG_FIRST)));
12217 	      else
12218 		/* Disable interrupts by clearing the KSU, ERL, EXL,
12219 		   and IE bits.  */
12220 		emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12221 				       GEN_INT (5),
12222 				       GEN_INT (SR_IE),
12223 				       gen_rtx_REG (SImode, GP_REG_FIRST)));
12224 
12225 	      if (TARGET_HARD_FLOAT)
12226 		/* Disable COP1 for hard-float.  This will lead to an exception
12227 		   if floating-point code is executed in an ISR.  */
12228 		emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12229 				       GEN_INT (1),
12230 				       GEN_INT (SR_COP1),
12231 				       gen_rtx_REG (SImode, GP_REG_FIRST)));
12232 	    }
12233 	  else
12234 	    {
12235 	      if (step1 != 0)
12236 		{
12237 		  rtx insn = gen_add3_insn (stack_pointer_rtx,
12238 					    stack_pointer_rtx,
12239 					    GEN_INT (-step1));
12240 		  RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
12241 		  mips_frame_barrier ();
12242 		  size -= step1;
12243 		}
12244 	    }
12245 	  mips_for_each_saved_acc (size, mips_save_reg);
12246 	  mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
12247 	}
12248     }
12249 
12250   /* Allocate the rest of the frame.  */
12251   if (size > 0)
12252     {
12253       if (SMALL_OPERAND (-size))
12254 	RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
12255 						       stack_pointer_rtx,
12256 						       GEN_INT (-size)))) = 1;
12257       else
12258 	{
12259 	  mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
12260 	  if (TARGET_MIPS16)
12261 	    {
12262 	      /* There are no instructions to add or subtract registers
12263 		 from the stack pointer, so use the frame pointer as a
12264 		 temporary.  We should always be using a frame pointer
12265 		 in this case anyway.  */
12266 	      gcc_assert (frame_pointer_needed);
12267 	      mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
12268 	      emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
12269 					hard_frame_pointer_rtx,
12270 					MIPS_PROLOGUE_TEMP (Pmode)));
12271 	      mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
12272 	    }
12273 	  else
12274 	    emit_insn (gen_sub3_insn (stack_pointer_rtx,
12275 				      stack_pointer_rtx,
12276 				      MIPS_PROLOGUE_TEMP (Pmode)));
12277 
12278 	  /* Describe the combined effect of the previous instructions.  */
12279 	  mips_set_frame_expr
12280 	    (gen_rtx_SET (stack_pointer_rtx,
12281 			  plus_constant (Pmode, stack_pointer_rtx, -size)));
12282 	}
12283       mips_frame_barrier ();
12284     }
12285 
12286   /* Set up the frame pointer, if we're using one.  */
12287   if (frame_pointer_needed)
12288     {
12289       HOST_WIDE_INT offset;
12290 
12291       offset = frame->hard_frame_pointer_offset;
12292       if (offset == 0)
12293 	{
12294 	  rtx insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
12295 	  RTX_FRAME_RELATED_P (insn) = 1;
12296 	}
12297       else if (SMALL_OPERAND (offset))
12298 	{
12299 	  rtx insn = gen_add3_insn (hard_frame_pointer_rtx,
12300 				    stack_pointer_rtx, GEN_INT (offset));
12301 	  RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
12302 	}
12303       else
12304 	{
12305 	  mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
12306 	  mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
12307 	  emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
12308 				    hard_frame_pointer_rtx,
12309 				    MIPS_PROLOGUE_TEMP (Pmode)));
12310 	  mips_set_frame_expr
12311 	    (gen_rtx_SET (hard_frame_pointer_rtx,
12312 			  plus_constant (Pmode, stack_pointer_rtx, offset)));
12313 	}
12314     }
12315 
12316   mips_emit_loadgp ();
12317 
12318   /* Initialize the $gp save slot.  */
12319   if (mips_cfun_has_cprestore_slot_p ())
12320     {
12321       rtx base, mem, gp, temp;
12322       HOST_WIDE_INT offset;
12323 
12324       mips_get_cprestore_base_and_offset (&base, &offset, false);
12325       mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
12326       gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
12327       temp = (SMALL_OPERAND (offset)
12328 	      ? gen_rtx_SCRATCH (Pmode)
12329 	      : MIPS_PROLOGUE_TEMP (Pmode));
12330       emit_insn (PMODE_INSN (gen_potential_cprestore,
12331 			     (mem, GEN_INT (offset), gp, temp)));
12332 
12333       mips_get_cprestore_base_and_offset (&base, &offset, true);
12334       mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
12335       emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
12336     }
12337 
12338   /* We need to search back to the last use of K0 or K1.  */
12339   if (cfun->machine->interrupt_handler_p)
12340     {
12341       rtx_insn *insn;
12342       for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
12343 	if (INSN_P (insn)
12344 	    && mips_refers_to_kernel_reg_p (PATTERN (insn)))
12345 	  break;
12346       /* Emit a move from K1 to COP0 Status after insn.  */
12347       gcc_assert (insn != NULL_RTX);
12348       emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
12349 				      gen_rtx_REG (SImode, K1_REG_NUM)),
12350 		       insn);
12351     }
12352 
12353   /* If we are profiling, make sure no instructions are scheduled before
12354      the call to mcount.  */
12355   if (crtl->profile)
12356     emit_insn (gen_blockage ());
12357 }
12358 
12359 /* Attach all pending register saves to the previous instruction.
12360    Return that instruction.  */
12361 
12362 static rtx_insn *
mips_epilogue_emit_cfa_restores(void)12363 mips_epilogue_emit_cfa_restores (void)
12364 {
12365   rtx_insn *insn;
12366 
12367   insn = get_last_insn ();
12368   if (mips_epilogue.cfa_restores)
12369     {
12370       gcc_assert (insn && !REG_NOTES (insn));
12371       RTX_FRAME_RELATED_P (insn) = 1;
12372       REG_NOTES (insn) = mips_epilogue.cfa_restores;
12373       mips_epilogue.cfa_restores = 0;
12374     }
12375   return insn;
12376 }
12377 
12378 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
12379    now at REG + OFFSET.  */
12380 
12381 static void
mips_epilogue_set_cfa(rtx reg,HOST_WIDE_INT offset)12382 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
12383 {
12384   rtx_insn *insn;
12385 
12386   insn = mips_epilogue_emit_cfa_restores ();
12387   if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
12388     {
12389       RTX_FRAME_RELATED_P (insn) = 1;
12390       REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
12391 					 plus_constant (Pmode, reg, offset),
12392 					 REG_NOTES (insn));
12393       mips_epilogue.cfa_reg = reg;
12394       mips_epilogue.cfa_offset = offset;
12395     }
12396 }
12397 
12398 /* Emit instructions to restore register REG from slot MEM.  Also update
12399    the cfa_restores list.  */
12400 
12401 static void
mips_restore_reg(rtx reg,rtx mem)12402 mips_restore_reg (rtx reg, rtx mem)
12403 {
12404   /* There's no MIPS16 instruction to load $31 directly.  Load into
12405      $7 instead and adjust the return insn appropriately.  */
12406   if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
12407     reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
12408   else if (GET_MODE (reg) == DFmode
12409 	   && (!TARGET_FLOAT64
12410 	       || mips_abi == ABI_32))
12411     {
12412       mips_add_cfa_restore (mips_subword (reg, true));
12413       mips_add_cfa_restore (mips_subword (reg, false));
12414     }
12415   else
12416     mips_add_cfa_restore (reg);
12417 
12418   mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
12419   if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
12420     /* The CFA is currently defined in terms of the register whose
12421        value we have just restored.  Redefine the CFA in terms of
12422        the stack pointer.  */
12423     mips_epilogue_set_cfa (stack_pointer_rtx,
12424 			   mips_epilogue.cfa_restore_sp_offset);
12425 }
12426 
12427 /* Emit code to set the stack pointer to BASE + OFFSET, given that
12428    BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
12429    BASE, if not the stack pointer, is available as a temporary.  */
12430 
12431 static void
mips_deallocate_stack(rtx base,rtx offset,HOST_WIDE_INT new_frame_size)12432 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
12433 {
12434   if (base == stack_pointer_rtx && offset == const0_rtx)
12435     return;
12436 
12437   mips_frame_barrier ();
12438   if (offset == const0_rtx)
12439     {
12440       emit_move_insn (stack_pointer_rtx, base);
12441       mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
12442     }
12443   else if (TARGET_MIPS16 && base != stack_pointer_rtx)
12444     {
12445       emit_insn (gen_add3_insn (base, base, offset));
12446       mips_epilogue_set_cfa (base, new_frame_size);
12447       emit_move_insn (stack_pointer_rtx, base);
12448     }
12449   else
12450     {
12451       emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
12452       mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
12453     }
12454 }
12455 
12456 /* Emit any instructions needed before a return.  */
12457 
12458 void
mips_expand_before_return(void)12459 mips_expand_before_return (void)
12460 {
12461   /* When using a call-clobbered gp, we start out with unified call
12462      insns that include instructions to restore the gp.  We then split
12463      these unified calls after reload.  These split calls explicitly
12464      clobber gp, so there is no need to define
12465      PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
12466 
12467      For consistency, we should also insert an explicit clobber of $28
12468      before return insns, so that the post-reload optimizers know that
12469      the register is not live on exit.  */
12470   if (TARGET_CALL_CLOBBERED_GP)
12471     emit_clobber (pic_offset_table_rtx);
12472 }
12473 
12474 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
12475    says which.  */
12476 
12477 void
mips_expand_epilogue(bool sibcall_p)12478 mips_expand_epilogue (bool sibcall_p)
12479 {
12480   const struct mips_frame_info *frame;
12481   HOST_WIDE_INT step1, step2;
12482   rtx base, adjust;
12483   rtx_insn *insn;
12484   bool use_jraddiusp_p = false;
12485 
12486   if (!sibcall_p && mips_can_use_return_insn ())
12487     {
12488       emit_jump_insn (gen_return ());
12489       return;
12490     }
12491 
12492   /* In MIPS16 mode, if the return value should go into a floating-point
12493      register, we need to call a helper routine to copy it over.  */
12494   if (mips16_cfun_returns_in_fpr_p ())
12495     mips16_copy_fpr_return_value ();
12496 
12497   /* Split the frame into two.  STEP1 is the amount of stack we should
12498      deallocate before restoring the registers.  STEP2 is the amount we
12499      should deallocate afterwards.
12500 
12501      Start off by assuming that no registers need to be restored.  */
12502   frame = &cfun->machine->frame;
12503   step1 = frame->total_size;
12504   step2 = 0;
12505 
12506   /* Work out which register holds the frame address.  */
12507   if (!frame_pointer_needed)
12508     base = stack_pointer_rtx;
12509   else
12510     {
12511       base = hard_frame_pointer_rtx;
12512       step1 -= frame->hard_frame_pointer_offset;
12513     }
12514   mips_epilogue.cfa_reg = base;
12515   mips_epilogue.cfa_offset = step1;
12516   mips_epilogue.cfa_restores = NULL_RTX;
12517 
12518   /* If we need to restore registers, deallocate as much stack as
12519      possible in the second step without going out of range.  */
12520   if ((frame->mask | frame->fmask | frame->acc_mask) != 0
12521       || frame->num_cop0_regs > 0)
12522     {
12523       step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
12524       step1 -= step2;
12525     }
12526 
12527   /* Get an rtx for STEP1 that we can add to BASE.  */
12528   adjust = GEN_INT (step1);
12529   if (!SMALL_OPERAND (step1))
12530     {
12531       mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
12532       adjust = MIPS_EPILOGUE_TEMP (Pmode);
12533     }
12534   mips_deallocate_stack (base, adjust, step2);
12535 
12536   /* If we're using addressing macros, $gp is implicitly used by all
12537      SYMBOL_REFs.  We must emit a blockage insn before restoring $gp
12538      from the stack.  */
12539   if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
12540     emit_insn (gen_blockage ());
12541 
12542   mips_epilogue.cfa_restore_sp_offset = step2;
12543   if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
12544     {
12545       unsigned int regno, mask;
12546       HOST_WIDE_INT offset;
12547       rtx restore;
12548 
12549       /* Generate the restore instruction.  */
12550       mask = frame->mask;
12551       restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
12552 
12553       /* Restore any other registers manually.  */
12554       for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
12555  	if (BITSET_P (mask, regno - GP_REG_FIRST))
12556  	  {
12557  	    offset -= UNITS_PER_WORD;
12558  	    mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
12559  	  }
12560 
12561       /* Restore the remaining registers and deallocate the final bit
12562 	 of the frame.  */
12563       mips_frame_barrier ();
12564       emit_insn (restore);
12565       mips_epilogue_set_cfa (stack_pointer_rtx, 0);
12566     }
12567   else
12568     {
12569       /* Restore the registers.  */
12570       mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
12571       mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
12572 				       mips_restore_reg);
12573 
12574       if (cfun->machine->interrupt_handler_p)
12575 	{
12576 	  HOST_WIDE_INT offset;
12577 	  rtx mem;
12578 
12579 	  offset = frame->cop0_sp_offset - (frame->total_size - step2);
12580 
12581 	  /* Restore the original EPC.  */
12582 	  mem = gen_frame_mem (word_mode,
12583 			       plus_constant (Pmode, stack_pointer_rtx,
12584 					      offset));
12585 	  mips_emit_move (gen_rtx_REG (word_mode, K1_REG_NUM), mem);
12586 	  offset -= UNITS_PER_WORD;
12587 
12588 	  /* Move to COP0 EPC.  */
12589 	  emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
12590 				    gen_rtx_REG (SImode, K1_REG_NUM)));
12591 
12592 	  /* Restore the original Status.  */
12593 	  mem = gen_frame_mem (word_mode,
12594 			       plus_constant (Pmode, stack_pointer_rtx,
12595 					      offset));
12596 	  mips_emit_move (gen_rtx_REG (word_mode, K1_REG_NUM), mem);
12597 	  offset -= UNITS_PER_WORD;
12598 
12599 	  /* If we don't use shadow register set, we need to update SP.  */
12600 	  if (cfun->machine->use_shadow_register_set == SHADOW_SET_NO)
12601 	    mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
12602 	  else
12603 	    /* The choice of position is somewhat arbitrary in this case.  */
12604 	    mips_epilogue_emit_cfa_restores ();
12605 
12606 	  /* Move to COP0 Status.  */
12607 	  emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
12608 				    gen_rtx_REG (SImode, K1_REG_NUM)));
12609 	}
12610       else if (TARGET_MICROMIPS
12611 	       && !crtl->calls_eh_return
12612 	       && !sibcall_p
12613 	       && step2 > 0
12614 	       && mips_unsigned_immediate_p (step2, 5, 2))
12615 	use_jraddiusp_p = true;
12616       else
12617 	/* Deallocate the final bit of the frame.  */
12618 	mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
12619     }
12620 
12621   if (cfun->machine->use_frame_header_for_callee_saved_regs)
12622     mips_epilogue_emit_cfa_restores ();
12623   else if (!use_jraddiusp_p)
12624     gcc_assert (!mips_epilogue.cfa_restores);
12625 
12626   /* Add in the __builtin_eh_return stack adjustment.  We need to
12627      use a temporary in MIPS16 code.  */
12628   if (crtl->calls_eh_return)
12629     {
12630       if (TARGET_MIPS16)
12631 	{
12632 	  mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
12633 	  emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
12634 				    MIPS_EPILOGUE_TEMP (Pmode),
12635 				    EH_RETURN_STACKADJ_RTX));
12636 	  mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
12637 	}
12638       else
12639 	emit_insn (gen_add3_insn (stack_pointer_rtx,
12640 				  stack_pointer_rtx,
12641 				  EH_RETURN_STACKADJ_RTX));
12642     }
12643 
12644   if (!sibcall_p)
12645     {
12646       mips_expand_before_return ();
12647       if (cfun->machine->interrupt_handler_p)
12648 	{
12649 	  /* Interrupt handlers generate eret or deret.  */
12650 	  if (cfun->machine->use_debug_exception_return_p)
12651 	    emit_jump_insn (gen_mips_deret ());
12652 	  else
12653 	    emit_jump_insn (gen_mips_eret ());
12654 	}
12655       else
12656 	{
12657 	  rtx pat;
12658 
12659 	  /* When generating MIPS16 code, the normal
12660 	     mips_for_each_saved_gpr_and_fpr path will restore the return
12661 	     address into $7 rather than $31.  */
12662 	  if (TARGET_MIPS16
12663 	      && !GENERATE_MIPS16E_SAVE_RESTORE
12664 	      && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
12665 	    {
12666 	      /* simple_returns cannot rely on values that are only available
12667 		 on paths through the epilogue (because return paths that do
12668 		 not pass through the epilogue may nevertheless reuse a
12669 		 simple_return that occurs at the end of the epilogue).
12670 		 Use a normal return here instead.  */
12671 	      rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
12672 	      pat = gen_return_internal (reg);
12673 	    }
12674 	  else if (use_jraddiusp_p)
12675 	    pat = gen_jraddiusp (GEN_INT (step2));
12676 	  else
12677 	    {
12678 	      rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
12679 	      pat = gen_simple_return_internal (reg);
12680 	    }
12681 	  emit_jump_insn (pat);
12682 	  if (use_jraddiusp_p)
12683 	    mips_epilogue_set_cfa (stack_pointer_rtx, step2);
12684 	}
12685     }
12686 
12687   /* Search from the beginning to the first use of K0 or K1.  */
12688   if (cfun->machine->interrupt_handler_p
12689       && !cfun->machine->keep_interrupts_masked_p)
12690     {
12691       for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
12692 	if (INSN_P (insn)
12693 	    && mips_refers_to_kernel_reg_p (PATTERN (insn)))
12694 	  break;
12695       gcc_assert (insn != NULL_RTX);
12696       /* Insert disable interrupts before the first use of K0 or K1.  */
12697       emit_insn_before (gen_mips_di (), insn);
12698       emit_insn_before (gen_mips_ehb (), insn);
12699     }
12700 }
12701 
12702 /* Return nonzero if this function is known to have a null epilogue.
12703    This allows the optimizer to omit jumps to jumps if no stack
12704    was created.  */
12705 
12706 bool
mips_can_use_return_insn(void)12707 mips_can_use_return_insn (void)
12708 {
12709   /* Interrupt handlers need to go through the epilogue.  */
12710   if (cfun->machine->interrupt_handler_p)
12711     return false;
12712 
12713   if (!reload_completed)
12714     return false;
12715 
12716   if (crtl->profile)
12717     return false;
12718 
12719   /* In MIPS16 mode, a function that returns a floating-point value
12720      needs to arrange to copy the return value into the floating-point
12721      registers.  */
12722   if (mips16_cfun_returns_in_fpr_p ())
12723     return false;
12724 
12725   return (cfun->machine->frame.total_size == 0
12726 	  && !cfun->machine->use_frame_header_for_callee_saved_regs);
12727 }
12728 
12729 /* Return true if register REGNO can store a value of mode MODE.
12730    The result of this function is cached in mips_hard_regno_mode_ok.  */
12731 
12732 static bool
mips_hard_regno_mode_ok_uncached(unsigned int regno,machine_mode mode)12733 mips_hard_regno_mode_ok_uncached (unsigned int regno, machine_mode mode)
12734 {
12735   unsigned int size;
12736   enum mode_class mclass;
12737 
12738   if (mode == CCV2mode)
12739     return (ISA_HAS_8CC
12740 	    && ST_REG_P (regno)
12741 	    && (regno - ST_REG_FIRST) % 2 == 0);
12742 
12743   if (mode == CCV4mode)
12744     return (ISA_HAS_8CC
12745 	    && ST_REG_P (regno)
12746 	    && (regno - ST_REG_FIRST) % 4 == 0);
12747 
12748   if (mode == CCmode)
12749     return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
12750 
12751   size = GET_MODE_SIZE (mode);
12752   mclass = GET_MODE_CLASS (mode);
12753 
12754   if (GP_REG_P (regno) && mode != CCFmode && !MSA_SUPPORTED_MODE_P (mode))
12755     return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
12756 
12757   /* For MSA, allow TImode and 128-bit vector modes in all FPR.  */
12758   if (FP_REG_P (regno) && MSA_SUPPORTED_MODE_P (mode))
12759     return true;
12760 
12761   if (FP_REG_P (regno)
12762       && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
12763 	  || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
12764     {
12765       /* Deny use of odd-numbered registers for 32-bit data for
12766 	 the o32 FP64A ABI.  */
12767       if (TARGET_O32_FP64A_ABI && size <= 4 && (regno & 1) != 0)
12768 	return false;
12769 
12770       /* The FPXX ABI requires double-precision values to be placed in
12771 	 even-numbered registers.  Disallow odd-numbered registers with
12772 	 CCFmode because CCFmode double-precision compares will write a
12773 	 64-bit value to a register.  */
12774       if (mode == CCFmode)
12775 	return !(TARGET_FLOATXX && (regno & 1) != 0);
12776 
12777       /* Allow 64-bit vector modes for Loongson-2E/2F.  */
12778       if (TARGET_LOONGSON_VECTORS
12779 	  && (mode == V2SImode
12780 	      || mode == V4HImode
12781 	      || mode == V8QImode
12782 	      || mode == DImode))
12783 	return true;
12784 
12785       if (mclass == MODE_FLOAT
12786 	  || mclass == MODE_COMPLEX_FLOAT
12787 	  || mclass == MODE_VECTOR_FLOAT)
12788 	return size <= UNITS_PER_FPVALUE;
12789 
12790       /* Allow integer modes that fit into a single register.  We need
12791 	 to put integers into FPRs when using instructions like CVT
12792 	 and TRUNC.  There's no point allowing sizes smaller than a word,
12793 	 because the FPU has no appropriate load/store instructions.  */
12794       if (mclass == MODE_INT)
12795 	return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
12796     }
12797 
12798   /* Don't allow vector modes in accumulators.  */
12799   if (ACC_REG_P (regno)
12800       && !VECTOR_MODE_P (mode)
12801       && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
12802     {
12803       if (MD_REG_P (regno))
12804 	{
12805 	  /* After a multiplication or division, clobbering HI makes
12806 	     the value of LO unpredictable, and vice versa.  This means
12807 	     that, for all interesting cases, HI and LO are effectively
12808 	     a single register.
12809 
12810 	     We model this by requiring that any value that uses HI
12811 	     also uses LO.  */
12812 	  if (size <= UNITS_PER_WORD * 2)
12813 	    return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
12814 	}
12815       else
12816 	{
12817 	  /* DSP accumulators do not have the same restrictions as
12818 	     HI and LO, so we can treat them as normal doubleword
12819 	     registers.  */
12820 	  if (size <= UNITS_PER_WORD)
12821 	    return true;
12822 
12823 	  if (size <= UNITS_PER_WORD * 2
12824 	      && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
12825 	    return true;
12826 	}
12827     }
12828 
12829   if (ALL_COP_REG_P (regno))
12830     return mclass == MODE_INT && size <= UNITS_PER_WORD;
12831 
12832   if (regno == GOT_VERSION_REGNUM)
12833     return mode == SImode;
12834 
12835   return false;
12836 }
12837 
12838 /* Implement TARGET_HARD_REGNO_MODE_OK.  */
12839 
12840 static bool
mips_hard_regno_mode_ok(unsigned int regno,machine_mode mode)12841 mips_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
12842 {
12843   return mips_hard_regno_mode_ok_p[mode][regno];
12844 }
12845 
12846 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG.  */
12847 
12848 bool
mips_hard_regno_rename_ok(unsigned int old_reg ATTRIBUTE_UNUSED,unsigned int new_reg)12849 mips_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
12850 			   unsigned int new_reg)
12851 {
12852   /* Interrupt functions can only use registers that have already been
12853      saved by the prologue, even if they would normally be call-clobbered.  */
12854   if (cfun->machine->interrupt_handler_p && !df_regs_ever_live_p (new_reg))
12855     return false;
12856 
12857   return true;
12858 }
12859 
12860 /* Return nonzero if register REGNO can be used as a scratch register
12861    in peephole2.  */
12862 
12863 bool
mips_hard_regno_scratch_ok(unsigned int regno)12864 mips_hard_regno_scratch_ok (unsigned int regno)
12865 {
12866   /* See mips_hard_regno_rename_ok.  */
12867   if (cfun->machine->interrupt_handler_p && !df_regs_ever_live_p (regno))
12868     return false;
12869 
12870   return true;
12871 }
12872 
12873 /* Implement TARGET_HARD_REGNO_CALL_PART_CLOBBERED.  Odd-numbered
12874    single-precision registers are not considered callee-saved for o32
12875    FPXX as they will be clobbered when run on an FR=1 FPU.  MSA vector
12876    registers with MODE > 64 bits are part clobbered too.  */
12877 
12878 static bool
mips_hard_regno_call_part_clobbered(unsigned int regno,machine_mode mode)12879 mips_hard_regno_call_part_clobbered (unsigned int regno, machine_mode mode)
12880 {
12881   if (TARGET_FLOATXX
12882       && hard_regno_nregs (regno, mode) == 1
12883       && FP_REG_P (regno)
12884       && (regno & 1) != 0)
12885     return true;
12886 
12887   if (ISA_HAS_MSA && FP_REG_P (regno) && GET_MODE_SIZE (mode) > 8)
12888     return true;
12889 
12890   return false;
12891 }
12892 
12893 /* Implement TARGET_HARD_REGNO_NREGS.  */
12894 
12895 static unsigned int
mips_hard_regno_nregs(unsigned int regno,machine_mode mode)12896 mips_hard_regno_nregs (unsigned int regno, machine_mode mode)
12897 {
12898   if (ST_REG_P (regno))
12899     /* The size of FP status registers is always 4, because they only hold
12900        CCmode values, and CCmode is always considered to be 4 bytes wide.  */
12901     return (GET_MODE_SIZE (mode) + 3) / 4;
12902 
12903   if (FP_REG_P (regno))
12904     {
12905       if (MSA_SUPPORTED_MODE_P (mode))
12906 	return 1;
12907 
12908       return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
12909     }
12910 
12911   /* All other registers are word-sized.  */
12912   return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
12913 }
12914 
12915 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
12916    in mips_hard_regno_nregs.  */
12917 
12918 int
mips_class_max_nregs(enum reg_class rclass,machine_mode mode)12919 mips_class_max_nregs (enum reg_class rclass, machine_mode mode)
12920 {
12921   int size;
12922   HARD_REG_SET left;
12923 
12924   size = 0x8000;
12925   COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
12926   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
12927     {
12928       if (mips_hard_regno_mode_ok (ST_REG_FIRST, mode))
12929 	size = MIN (size, 4);
12930 
12931       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
12932     }
12933   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
12934     {
12935       if (mips_hard_regno_mode_ok (FP_REG_FIRST, mode))
12936 	{
12937 	  if (MSA_SUPPORTED_MODE_P (mode))
12938 	    size = MIN (size, UNITS_PER_MSA_REG);
12939 	  else
12940 	    size = MIN (size, UNITS_PER_FPREG);
12941 	}
12942 
12943       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
12944     }
12945   if (!hard_reg_set_empty_p (left))
12946     size = MIN (size, UNITS_PER_WORD);
12947   return (GET_MODE_SIZE (mode) + size - 1) / size;
12948 }
12949 
12950 /* Implement TARGET_CAN_CHANGE_MODE_CLASS.  */
12951 
12952 static bool
mips_can_change_mode_class(machine_mode from,machine_mode to,reg_class_t rclass)12953 mips_can_change_mode_class (machine_mode from,
12954 			    machine_mode to, reg_class_t rclass)
12955 {
12956   /* Allow conversions between different Loongson integer vectors,
12957      and between those vectors and DImode.  */
12958   if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
12959       && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
12960     return true;
12961 
12962   /* Allow conversions between different MSA vector modes.  */
12963   if (MSA_SUPPORTED_MODE_P (from) && MSA_SUPPORTED_MODE_P (to))
12964     return true;
12965 
12966   /* Otherwise, there are several problems with changing the modes of
12967      values in floating-point registers:
12968 
12969      - When a multi-word value is stored in paired floating-point
12970        registers, the first register always holds the low word.  We
12971        therefore can't allow FPRs to change between single-word and
12972        multi-word modes on big-endian targets.
12973 
12974      - GCC assumes that each word of a multiword register can be
12975        accessed individually using SUBREGs.  This is not true for
12976        floating-point registers if they are bigger than a word.
12977 
12978      - Loading a 32-bit value into a 64-bit floating-point register
12979        will not sign-extend the value, despite what LOAD_EXTEND_OP
12980        says.  We can't allow FPRs to change from SImode to a wider
12981        mode on 64-bit targets.
12982 
12983      - If the FPU has already interpreted a value in one format, we
12984        must not ask it to treat the value as having a different
12985        format.
12986 
12987      We therefore disallow all mode changes involving FPRs.  */
12988 
12989   return !reg_classes_intersect_p (FP_REGS, rclass);
12990 }
12991 
12992 /* Implement target hook small_register_classes_for_mode_p.  */
12993 
12994 static bool
mips_small_register_classes_for_mode_p(machine_mode mode ATTRIBUTE_UNUSED)12995 mips_small_register_classes_for_mode_p (machine_mode mode
12996 					ATTRIBUTE_UNUSED)
12997 {
12998   return TARGET_MIPS16;
12999 }
13000 
13001 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction,
13002    or use the MSA's move.v instruction.  */
13003 
13004 static bool
mips_mode_ok_for_mov_fmt_p(machine_mode mode)13005 mips_mode_ok_for_mov_fmt_p (machine_mode mode)
13006 {
13007   switch (mode)
13008     {
13009     case E_CCFmode:
13010     case E_SFmode:
13011       return TARGET_HARD_FLOAT;
13012 
13013     case E_DFmode:
13014       return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
13015 
13016     case E_V2SFmode:
13017       return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
13018 
13019     default:
13020       return MSA_SUPPORTED_MODE_P (mode);
13021     }
13022 }
13023 
13024 /* Implement TARGET_MODES_TIEABLE_P.  */
13025 
13026 static bool
mips_modes_tieable_p(machine_mode mode1,machine_mode mode2)13027 mips_modes_tieable_p (machine_mode mode1, machine_mode mode2)
13028 {
13029   /* FPRs allow no mode punning, so it's not worth tying modes if we'd
13030      prefer to put one of them in FPRs.  */
13031   return (mode1 == mode2
13032 	  || (!mips_mode_ok_for_mov_fmt_p (mode1)
13033 	      && !mips_mode_ok_for_mov_fmt_p (mode2)));
13034 }
13035 
13036 /* Implement TARGET_PREFERRED_RELOAD_CLASS.  */
13037 
13038 static reg_class_t
mips_preferred_reload_class(rtx x,reg_class_t rclass)13039 mips_preferred_reload_class (rtx x, reg_class_t rclass)
13040 {
13041   if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
13042     return LEA_REGS;
13043 
13044   if (reg_class_subset_p (FP_REGS, rclass)
13045       && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
13046     return FP_REGS;
13047 
13048   if (reg_class_subset_p (GR_REGS, rclass))
13049     rclass = GR_REGS;
13050 
13051   if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
13052     rclass = M16_REGS;
13053 
13054   return rclass;
13055 }
13056 
13057 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
13058    Return a "canonical" class to represent it in later calculations.  */
13059 
13060 static reg_class_t
mips_canonicalize_move_class(reg_class_t rclass)13061 mips_canonicalize_move_class (reg_class_t rclass)
13062 {
13063   /* All moves involving accumulator registers have the same cost.  */
13064   if (reg_class_subset_p (rclass, ACC_REGS))
13065     rclass = ACC_REGS;
13066 
13067   /* Likewise promote subclasses of general registers to the most
13068      interesting containing class.  */
13069   if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
13070     rclass = M16_REGS;
13071   else if (reg_class_subset_p (rclass, GENERAL_REGS))
13072     rclass = GENERAL_REGS;
13073 
13074   return rclass;
13075 }
13076 
13077 /* Return the cost of moving a value from a register of class FROM to a GPR.
13078    Return 0 for classes that are unions of other classes handled by this
13079    function.  */
13080 
13081 static int
mips_move_to_gpr_cost(reg_class_t from)13082 mips_move_to_gpr_cost (reg_class_t from)
13083 {
13084   switch (from)
13085     {
13086     case M16_REGS:
13087     case GENERAL_REGS:
13088       /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro.  */
13089       return 2;
13090 
13091     case ACC_REGS:
13092       /* MFLO and MFHI.  */
13093       return 6;
13094 
13095     case FP_REGS:
13096       /* MFC1, etc.  */
13097       return 4;
13098 
13099     case COP0_REGS:
13100     case COP2_REGS:
13101     case COP3_REGS:
13102       /* This choice of value is historical.  */
13103       return 5;
13104 
13105     default:
13106       return 0;
13107     }
13108 }
13109 
13110 /* Return the cost of moving a value from a GPR to a register of class TO.
13111    Return 0 for classes that are unions of other classes handled by this
13112    function.  */
13113 
13114 static int
mips_move_from_gpr_cost(reg_class_t to)13115 mips_move_from_gpr_cost (reg_class_t to)
13116 {
13117   switch (to)
13118     {
13119     case M16_REGS:
13120     case GENERAL_REGS:
13121       /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro.  */
13122       return 2;
13123 
13124     case ACC_REGS:
13125       /* MTLO and MTHI.  */
13126       return 6;
13127 
13128     case FP_REGS:
13129       /* MTC1, etc.  */
13130       return 4;
13131 
13132     case COP0_REGS:
13133     case COP2_REGS:
13134     case COP3_REGS:
13135       /* This choice of value is historical.  */
13136       return 5;
13137 
13138     default:
13139       return 0;
13140     }
13141 }
13142 
13143 /* Implement TARGET_REGISTER_MOVE_COST.  Return 0 for classes that are the
13144    maximum of the move costs for subclasses; regclass will work out
13145    the maximum for us.  */
13146 
13147 static int
mips_register_move_cost(machine_mode mode,reg_class_t from,reg_class_t to)13148 mips_register_move_cost (machine_mode mode,
13149 			 reg_class_t from, reg_class_t to)
13150 {
13151   reg_class_t dregs;
13152   int cost1, cost2;
13153 
13154   from = mips_canonicalize_move_class (from);
13155   to = mips_canonicalize_move_class (to);
13156 
13157   /* Handle moves that can be done without using general-purpose registers.  */
13158   if (from == FP_REGS)
13159     {
13160       if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
13161 	/* MOV.FMT.  */
13162 	return 4;
13163     }
13164 
13165   /* Handle cases in which only one class deviates from the ideal.  */
13166   dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
13167   if (from == dregs)
13168     return mips_move_from_gpr_cost (to);
13169   if (to == dregs)
13170     return mips_move_to_gpr_cost (from);
13171 
13172   /* Handles cases that require a GPR temporary.  */
13173   cost1 = mips_move_to_gpr_cost (from);
13174   if (cost1 != 0)
13175     {
13176       cost2 = mips_move_from_gpr_cost (to);
13177       if (cost2 != 0)
13178 	return cost1 + cost2;
13179     }
13180 
13181   return 0;
13182 }
13183 
13184 /* Implement TARGET_REGISTER_PRIORITY.  */
13185 
13186 static int
mips_register_priority(int hard_regno)13187 mips_register_priority (int hard_regno)
13188 {
13189   /* Treat MIPS16 registers with higher priority than other regs.  */
13190   if (TARGET_MIPS16
13191       && TEST_HARD_REG_BIT (reg_class_contents[M16_REGS], hard_regno))
13192     return 1;
13193   return 0;
13194 }
13195 
13196 /* Implement TARGET_MEMORY_MOVE_COST.  */
13197 
13198 static int
mips_memory_move_cost(machine_mode mode,reg_class_t rclass,bool in)13199 mips_memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
13200 {
13201   return (mips_cost->memory_latency
13202 	  + memory_move_secondary_cost (mode, rclass, in));
13203 }
13204 
13205 /* Implement TARGET_SECONDARY_MEMORY_NEEDED.
13206 
13207    When targeting the o32 FPXX ABI, all moves with a length of doubleword
13208    or greater must be performed by FR-mode-aware instructions.
13209    This can be achieved using MFHC1/MTHC1 when these instructions are
13210    available but otherwise moves must go via memory.
13211    For the o32 FP64A ABI, all odd-numbered moves with a length of
13212    doubleword or greater are required to use memory.  Using MTC1/MFC1
13213    to access the lower-half of these registers would require a forbidden
13214    single-precision access.  We require all double-word moves to use
13215    memory because adding even and odd floating-point registers classes
13216    would have a significant impact on the backend.  */
13217 
13218 static bool
mips_secondary_memory_needed(machine_mode mode,reg_class_t class1,reg_class_t class2)13219 mips_secondary_memory_needed (machine_mode mode, reg_class_t class1,
13220 			      reg_class_t class2)
13221 {
13222   /* Ignore spilled pseudos.  */
13223   if (lra_in_progress && (class1 == NO_REGS || class2 == NO_REGS))
13224     return false;
13225 
13226   if (((class1 == FP_REGS) != (class2 == FP_REGS))
13227       && ((TARGET_FLOATXX && !ISA_HAS_MXHC1)
13228 	  || TARGET_O32_FP64A_ABI)
13229       && GET_MODE_SIZE (mode) >= 8)
13230     return true;
13231 
13232   return false;
13233 }
13234 
13235 /* Return the register class required for a secondary register when
13236    copying between one of the registers in RCLASS and value X, which
13237    has mode MODE.  X is the source of the move if IN_P, otherwise it
13238    is the destination.  Return NO_REGS if no secondary register is
13239    needed.  */
13240 
13241 enum reg_class
mips_secondary_reload_class(enum reg_class rclass,machine_mode mode,rtx x,bool)13242 mips_secondary_reload_class (enum reg_class rclass,
13243 			     machine_mode mode, rtx x, bool)
13244 {
13245   int regno;
13246 
13247   /* If X is a constant that cannot be loaded into $25, it must be loaded
13248      into some other GPR.  No other register class allows a direct move.  */
13249   if (mips_dangerous_for_la25_p (x))
13250     return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
13251 
13252   regno = true_regnum (x);
13253   if (TARGET_MIPS16)
13254     {
13255       /* In MIPS16 mode, every move must involve a member of M16_REGS.  */
13256       if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
13257 	return M16_REGS;
13258 
13259       return NO_REGS;
13260     }
13261 
13262   /* Copying from accumulator registers to anywhere other than a general
13263      register requires a temporary general register.  */
13264   if (reg_class_subset_p (rclass, ACC_REGS))
13265     return GP_REG_P (regno) ? NO_REGS : GR_REGS;
13266   if (ACC_REG_P (regno))
13267     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
13268 
13269   if (reg_class_subset_p (rclass, FP_REGS))
13270     {
13271       if (regno < 0
13272 	  || (MEM_P (x)
13273 	      && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)))
13274 	/* In this case we can use lwc1, swc1, ldc1 or sdc1.  We'll use
13275 	   pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported.  */
13276 	return NO_REGS;
13277 
13278       if (MEM_P (x) && MSA_SUPPORTED_MODE_P (mode))
13279 	/* In this case we can use MSA LD.* and ST.*.  */
13280 	return NO_REGS;
13281 
13282       if (GP_REG_P (regno) || x == CONST0_RTX (mode))
13283 	/* In this case we can use mtc1, mfc1, dmtc1 or dmfc1.  */
13284 	return NO_REGS;
13285 
13286       if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
13287 	/* We can force the constant to memory and use lwc1
13288 	   and ldc1.  As above, we will use pairs of lwc1s if
13289 	   ldc1 is not supported.  */
13290 	return NO_REGS;
13291 
13292       if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
13293 	/* In this case we can use mov.fmt.  */
13294 	return NO_REGS;
13295 
13296       /* Otherwise, we need to reload through an integer register.  */
13297       return GR_REGS;
13298     }
13299   if (FP_REG_P (regno))
13300     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
13301 
13302   return NO_REGS;
13303 }
13304 
13305 /* Implement TARGET_MODE_REP_EXTENDED.  */
13306 
13307 static int
mips_mode_rep_extended(scalar_int_mode mode,scalar_int_mode mode_rep)13308 mips_mode_rep_extended (scalar_int_mode mode, scalar_int_mode mode_rep)
13309 {
13310   /* On 64-bit targets, SImode register values are sign-extended to DImode.  */
13311   if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
13312     return SIGN_EXTEND;
13313 
13314   return UNKNOWN;
13315 }
13316 
13317 /* Implement TARGET_VALID_POINTER_MODE.  */
13318 
13319 static bool
mips_valid_pointer_mode(scalar_int_mode mode)13320 mips_valid_pointer_mode (scalar_int_mode mode)
13321 {
13322   return mode == SImode || (TARGET_64BIT && mode == DImode);
13323 }
13324 
13325 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P.  */
13326 
13327 static bool
mips_vector_mode_supported_p(machine_mode mode)13328 mips_vector_mode_supported_p (machine_mode mode)
13329 {
13330   switch (mode)
13331     {
13332     case E_V2SFmode:
13333       return TARGET_PAIRED_SINGLE_FLOAT;
13334 
13335     case E_V2HImode:
13336     case E_V4QImode:
13337     case E_V2HQmode:
13338     case E_V2UHQmode:
13339     case E_V2HAmode:
13340     case E_V2UHAmode:
13341     case E_V4QQmode:
13342     case E_V4UQQmode:
13343       return TARGET_DSP;
13344 
13345     case E_V2SImode:
13346     case E_V4HImode:
13347     case E_V8QImode:
13348       return TARGET_LOONGSON_VECTORS;
13349 
13350     default:
13351       return MSA_SUPPORTED_MODE_P (mode);
13352     }
13353 }
13354 
13355 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.  */
13356 
13357 static bool
mips_scalar_mode_supported_p(scalar_mode mode)13358 mips_scalar_mode_supported_p (scalar_mode mode)
13359 {
13360   if (ALL_FIXED_POINT_MODE_P (mode)
13361       && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
13362     return true;
13363 
13364   return default_scalar_mode_supported_p (mode);
13365 }
13366 
13367 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE.  */
13368 
13369 static machine_mode
mips_preferred_simd_mode(scalar_mode mode)13370 mips_preferred_simd_mode (scalar_mode mode)
13371 {
13372   if (TARGET_PAIRED_SINGLE_FLOAT
13373       && mode == SFmode)
13374     return V2SFmode;
13375 
13376   if (!ISA_HAS_MSA)
13377     return word_mode;
13378 
13379   switch (mode)
13380     {
13381     case E_QImode:
13382       return V16QImode;
13383     case E_HImode:
13384       return V8HImode;
13385     case E_SImode:
13386       return V4SImode;
13387     case E_DImode:
13388       return V2DImode;
13389 
13390     case E_SFmode:
13391       return V4SFmode;
13392 
13393     case E_DFmode:
13394       return V2DFmode;
13395 
13396     default:
13397       break;
13398     }
13399   return word_mode;
13400 }
13401 
13402 /* Implement TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES.  */
13403 
13404 static void
mips_autovectorize_vector_sizes(vector_sizes * sizes)13405 mips_autovectorize_vector_sizes (vector_sizes *sizes)
13406 {
13407   if (ISA_HAS_MSA)
13408     sizes->safe_push (16);
13409 }
13410 
13411 /* Implement TARGET_INIT_LIBFUNCS.  */
13412 
13413 static void
mips_init_libfuncs(void)13414 mips_init_libfuncs (void)
13415 {
13416   if (TARGET_FIX_VR4120)
13417     {
13418       /* Register the special divsi3 and modsi3 functions needed to work
13419 	 around VR4120 division errata.  */
13420       set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
13421       set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
13422     }
13423 
13424   if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
13425     {
13426       /* Register the MIPS16 -mhard-float stubs.  */
13427       set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
13428       set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
13429       set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
13430       set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
13431 
13432       set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
13433       set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
13434       set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
13435       set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
13436       set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
13437       set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
13438       set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
13439 
13440       set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
13441       set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
13442       set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
13443 
13444       if (TARGET_DOUBLE_FLOAT)
13445 	{
13446 	  set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
13447 	  set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
13448 	  set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
13449 	  set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
13450 
13451 	  set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
13452 	  set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
13453 	  set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
13454 	  set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
13455 	  set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
13456 	  set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
13457 	  set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
13458 
13459 	  set_conv_libfunc (sext_optab, DFmode, SFmode,
13460 			    "__mips16_extendsfdf2");
13461 	  set_conv_libfunc (trunc_optab, SFmode, DFmode,
13462 			    "__mips16_truncdfsf2");
13463 	  set_conv_libfunc (sfix_optab, SImode, DFmode,
13464 			    "__mips16_fix_truncdfsi");
13465 	  set_conv_libfunc (sfloat_optab, DFmode, SImode,
13466 			    "__mips16_floatsidf");
13467 	  set_conv_libfunc (ufloat_optab, DFmode, SImode,
13468 			    "__mips16_floatunsidf");
13469 	}
13470     }
13471 
13472   /* The MIPS16 ISA does not have an encoding for "sync", so we rely
13473      on an external non-MIPS16 routine to implement __sync_synchronize.
13474      Similarly for the rest of the ll/sc libfuncs.  */
13475   if (TARGET_MIPS16)
13476     {
13477       synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
13478       init_sync_libfuncs (UNITS_PER_WORD);
13479     }
13480 }
13481 
13482 /* Build up a multi-insn sequence that loads label TARGET into $AT.  */
13483 
13484 static void
mips_process_load_label(rtx target)13485 mips_process_load_label (rtx target)
13486 {
13487   rtx base, gp, intop;
13488   HOST_WIDE_INT offset;
13489 
13490   mips_multi_start ();
13491   switch (mips_abi)
13492     {
13493     case ABI_N32:
13494       mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
13495       mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
13496       break;
13497 
13498     case ABI_64:
13499       mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
13500       mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
13501       break;
13502 
13503     default:
13504       gp = pic_offset_table_rtx;
13505       if (mips_cfun_has_cprestore_slot_p ())
13506 	{
13507 	  gp = gen_rtx_REG (Pmode, AT_REGNUM);
13508 	  mips_get_cprestore_base_and_offset (&base, &offset, true);
13509 	  if (!SMALL_OPERAND (offset))
13510 	    {
13511 	      intop = GEN_INT (CONST_HIGH_PART (offset));
13512 	      mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
13513 	      mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
13514 
13515 	      base = gp;
13516 	      offset = CONST_LOW_PART (offset);
13517 	    }
13518 	  intop = GEN_INT (offset);
13519 	  if (ISA_HAS_LOAD_DELAY)
13520 	    mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
13521 	  else
13522 	    mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
13523 	}
13524       if (ISA_HAS_LOAD_DELAY)
13525 	mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
13526       else
13527 	mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
13528       mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
13529       break;
13530     }
13531 }
13532 
13533 /* Return the number of instructions needed to load a label into $AT.  */
13534 
13535 static unsigned int
mips_load_label_num_insns(void)13536 mips_load_label_num_insns (void)
13537 {
13538   if (cfun->machine->load_label_num_insns == 0)
13539     {
13540       mips_process_load_label (pc_rtx);
13541       cfun->machine->load_label_num_insns = mips_multi_num_insns;
13542     }
13543   return cfun->machine->load_label_num_insns;
13544 }
13545 
13546 /* Emit an asm sequence to start a noat block and load the address
13547    of a label into $1.  */
13548 
13549 void
mips_output_load_label(rtx target)13550 mips_output_load_label (rtx target)
13551 {
13552   mips_push_asm_switch (&mips_noat);
13553   if (TARGET_EXPLICIT_RELOCS)
13554     {
13555       mips_process_load_label (target);
13556       mips_multi_write ();
13557     }
13558   else
13559     {
13560       if (Pmode == DImode)
13561 	output_asm_insn ("dla\t%@,%0", &target);
13562       else
13563 	output_asm_insn ("la\t%@,%0", &target);
13564     }
13565 }
13566 
13567 /* Return the length of INSN.  LENGTH is the initial length computed by
13568    attributes in the machine-description file.  */
13569 
13570 int
mips_adjust_insn_length(rtx_insn * insn,int length)13571 mips_adjust_insn_length (rtx_insn *insn, int length)
13572 {
13573   /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
13574      of a PIC long-branch sequence.  Substitute the correct value.  */
13575   if (length == MAX_PIC_BRANCH_LENGTH
13576       && JUMP_P (insn)
13577       && INSN_CODE (insn) >= 0
13578       && get_attr_type (insn) == TYPE_BRANCH)
13579     {
13580       /* Add the branch-over instruction and its delay slot, if this
13581 	 is a conditional branch.  */
13582       length = simplejump_p (insn) ? 0 : 8;
13583 
13584       /* Add the size of a load into $AT.  */
13585       length += BASE_INSN_LENGTH * mips_load_label_num_insns ();
13586 
13587       /* Add the length of an indirect jump, ignoring the delay slot.  */
13588       length += TARGET_COMPRESSION ? 2 : 4;
13589     }
13590 
13591   /* A unconditional jump has an unfilled delay slot if it is not part
13592      of a sequence.  A conditional jump normally has a delay slot, but
13593      does not on MIPS16.  */
13594   if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
13595     length += TARGET_MIPS16 ? 2 : 4;
13596 
13597   /* See how many nops might be needed to avoid hardware hazards.  */
13598   if (!cfun->machine->ignore_hazard_length_p
13599       && INSN_P (insn)
13600       && INSN_CODE (insn) >= 0)
13601     switch (get_attr_hazard (insn))
13602       {
13603       case HAZARD_NONE:
13604 	break;
13605 
13606       case HAZARD_DELAY:
13607       case HAZARD_FORBIDDEN_SLOT:
13608 	length += NOP_INSN_LENGTH;
13609 	break;
13610 
13611       case HAZARD_HILO:
13612 	length += NOP_INSN_LENGTH * 2;
13613 	break;
13614       }
13615 
13616   return length;
13617 }
13618 
13619 /* Return the asm template for a call.  OPERANDS are the operands, TARGET_OPNO
13620    is the operand number of the target.  SIZE_OPNO is the operand number of
13621    the argument size operand that can optionally hold the call attributes.  If
13622    SIZE_OPNO is not -1 and the call is indirect, use the function symbol from
13623    the call attributes to attach a R_MIPS_JALR relocation to the call.  LINK_P
13624    indicates whether the jump is a call and needs to set the link register.
13625 
13626    When generating GOT code without explicit relocation operators, all calls
13627    should use assembly macros.  Otherwise, all indirect calls should use "jr"
13628    or "jalr"; we will arrange to restore $gp afterwards if necessary.  Finally,
13629    we can only generate direct calls for -mabicalls by temporarily switching
13630    to non-PIC mode.
13631 
13632    For microMIPS jal(r), we try to generate jal(r)s when a 16-bit
13633    instruction is in the delay slot of jal(r).
13634 
13635    Where compact branches are available, we try to use them if the delay slot
13636    has a NOP (or equivalently delay slots were not enabled for the instruction
13637    anyway).  */
13638 
13639 const char *
mips_output_jump(rtx * operands,int target_opno,int size_opno,bool link_p)13640 mips_output_jump (rtx *operands, int target_opno, int size_opno, bool link_p)
13641 {
13642   static char buffer[300];
13643   char *s = buffer;
13644   bool reg_p = REG_P (operands[target_opno]);
13645 
13646   const char *and_link = link_p ? "al" : "";
13647   const char *reg = reg_p ? "r" : "";
13648   const char *compact = "";
13649   const char *nop = "%/";
13650   const char *short_delay = link_p ? "%!" : "";
13651   const char *insn_name = TARGET_CB_NEVER || reg_p ? "j" : "b";
13652 
13653   /* Compact branches can only be described when the ISA has support for them
13654      as both the compact formatter '%:' and the delay slot NOP formatter '%/'
13655      work as a mutually exclusive pair.  I.e. a NOP is never required if a
13656      compact form is available.  */
13657   if (!final_sequence
13658       && (TARGET_CB_MAYBE
13659 	  || (ISA_HAS_JRC && !link_p && reg_p)))
13660     {
13661       compact = "c";
13662       nop = "";
13663     }
13664 
13665   if (TARGET_USE_GOT && !TARGET_EXPLICIT_RELOCS)
13666     sprintf (s, "%%*%s%s\t%%%d%%/", insn_name, and_link, target_opno);
13667   else
13668     {
13669       if (!reg_p && TARGET_ABICALLS_PIC2)
13670 	s += sprintf (s, ".option\tpic0\n\t");
13671 
13672       if (reg_p && mips_get_pic_call_symbol (operands, size_opno))
13673 	s += sprintf (s, "%%*.reloc\t1f,%s,%%%d\n1:\t",
13674 		      TARGET_MICROMIPS ? "R_MICROMIPS_JALR" : "R_MIPS_JALR",
13675 		      size_opno);
13676       else
13677 	s += sprintf (s, "%%*");
13678 
13679       s += sprintf (s, "%s%s%s%s%s\t%%%d%s",
13680 		    insn_name, and_link, reg, compact, short_delay,
13681 		    target_opno, nop);
13682 
13683       if (!reg_p && TARGET_ABICALLS_PIC2)
13684 	s += sprintf (s, "\n\t.option\tpic2");
13685     }
13686   return buffer;
13687 }
13688 
13689 /* Return the assembly code for INSN, which has the operands given by
13690    OPERANDS, and which branches to OPERANDS[0] if some condition is true.
13691    BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
13692    is in range of a direct branch.  BRANCH_IF_FALSE is an inverted
13693    version of BRANCH_IF_TRUE.  */
13694 
13695 const char *
mips_output_conditional_branch(rtx_insn * insn,rtx * operands,const char * branch_if_true,const char * branch_if_false)13696 mips_output_conditional_branch (rtx_insn *insn, rtx *operands,
13697 				const char *branch_if_true,
13698 				const char *branch_if_false)
13699 {
13700   unsigned int length;
13701   rtx taken;
13702 
13703   gcc_assert (LABEL_P (operands[0]));
13704 
13705   length = get_attr_length (insn);
13706   if (length <= 8)
13707     {
13708       /* Just a simple conditional branch.  */
13709       mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
13710       return branch_if_true;
13711     }
13712 
13713   /* Generate a reversed branch around a direct jump.  This fallback does
13714      not use branch-likely instructions.  */
13715   mips_branch_likely = false;
13716   rtx_code_label *not_taken = gen_label_rtx ();
13717   taken = operands[0];
13718 
13719   /* Generate the reversed branch to NOT_TAKEN.  */
13720   operands[0] = not_taken;
13721   output_asm_insn (branch_if_false, operands);
13722 
13723   /* If INSN has a delay slot, we must provide delay slots for both the
13724      branch to NOT_TAKEN and the conditional jump.  We must also ensure
13725      that INSN's delay slot is executed in the appropriate cases.  */
13726   if (final_sequence)
13727     {
13728       /* This first delay slot will always be executed, so use INSN's
13729 	 delay slot if is not annulled.  */
13730       if (!INSN_ANNULLED_BRANCH_P (insn))
13731 	{
13732 	  final_scan_insn (final_sequence->insn (1),
13733 			   asm_out_file, optimize, 1, NULL);
13734 	  final_sequence->insn (1)->set_deleted ();
13735 	}
13736       else
13737 	output_asm_insn ("nop", 0);
13738       fprintf (asm_out_file, "\n");
13739     }
13740 
13741   /* Output the unconditional branch to TAKEN.  */
13742   if (TARGET_ABSOLUTE_JUMPS && TARGET_CB_MAYBE)
13743     {
13744       /* Add a hazard nop.  */
13745       if (!final_sequence)
13746 	{
13747 	  output_asm_insn ("nop\t\t# hazard nop", 0);
13748 	  fprintf (asm_out_file, "\n");
13749 	}
13750       output_asm_insn (MIPS_ABSOLUTE_JUMP ("bc\t%0"), &taken);
13751     }
13752   else if (TARGET_ABSOLUTE_JUMPS)
13753     output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
13754   else
13755     {
13756       mips_output_load_label (taken);
13757       if (TARGET_CB_MAYBE)
13758 	output_asm_insn ("jrc\t%@%]", 0);
13759       else
13760 	output_asm_insn ("jr\t%@%]%/", 0);
13761     }
13762 
13763   /* Now deal with its delay slot; see above.  */
13764   if (final_sequence)
13765     {
13766       /* This delay slot will only be executed if the branch is taken.
13767 	 Use INSN's delay slot if is annulled.  */
13768       if (INSN_ANNULLED_BRANCH_P (insn))
13769 	{
13770 	  final_scan_insn (final_sequence->insn (1),
13771 			   asm_out_file, optimize, 1, NULL);
13772 	  final_sequence->insn (1)->set_deleted ();
13773 	}
13774       else if (TARGET_CB_NEVER)
13775 	output_asm_insn ("nop", 0);
13776       fprintf (asm_out_file, "\n");
13777     }
13778 
13779   /* Output NOT_TAKEN.  */
13780   targetm.asm_out.internal_label (asm_out_file, "L",
13781 				  CODE_LABEL_NUMBER (not_taken));
13782   return "";
13783 }
13784 
13785 /* Return the assembly code for INSN, which branches to OPERANDS[0]
13786    if some equality condition is true.  The condition is given by
13787    OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
13788    OPERANDS[1].  OPERANDS[2] is the comparison's first operand;
13789    OPERANDS[3] is the second operand and may be zero or a register.  */
13790 
13791 const char *
mips_output_equal_conditional_branch(rtx_insn * insn,rtx * operands,bool inverted_p)13792 mips_output_equal_conditional_branch (rtx_insn* insn, rtx *operands,
13793 				      bool inverted_p)
13794 {
13795   const char *branch[2];
13796   /* For a simple BNEZ or BEQZ microMIPSr3 branch.  */
13797   if (TARGET_MICROMIPS
13798       && mips_isa_rev <= 5
13799       && operands[3] == const0_rtx
13800       && get_attr_length (insn) <= 8)
13801     {
13802       if (mips_cb == MIPS_CB_OPTIMAL)
13803 	{
13804 	  branch[!inverted_p] = "%*b%C1z%:\t%2,%0";
13805 	  branch[inverted_p] = "%*b%N1z%:\t%2,%0";
13806 	}
13807       else
13808 	{
13809 	  branch[!inverted_p] = "%*b%C1z\t%2,%0%/";
13810 	  branch[inverted_p] = "%*b%N1z\t%2,%0%/";
13811 	}
13812     }
13813   else if (TARGET_CB_MAYBE)
13814     {
13815       if (operands[3] == const0_rtx)
13816 	{
13817 	  branch[!inverted_p] = MIPS_BRANCH_C ("b%C1z", "%2,%0");
13818 	  branch[inverted_p] = MIPS_BRANCH_C ("b%N1z", "%2,%0");
13819 	}
13820       else if (REGNO (operands[2]) != REGNO (operands[3]))
13821 	{
13822 	  branch[!inverted_p] = MIPS_BRANCH_C ("b%C1", "%2,%3,%0");
13823 	  branch[inverted_p] = MIPS_BRANCH_C ("b%N1", "%2,%3,%0");
13824 	}
13825       else
13826 	{
13827 	  /* This case is degenerate.  It should not happen, but does.  */
13828 	  if (GET_CODE (operands[1]) == NE)
13829 	    inverted_p = !inverted_p;
13830 
13831 	  branch[!inverted_p] = MIPS_BRANCH_C ("b", "%0");
13832 	  branch[inverted_p] = "%*\t\t# branch never";
13833 	}
13834     }
13835   else
13836     {
13837       branch[!inverted_p] = MIPS_BRANCH ("b%C1", "%2,%z3,%0");
13838       branch[inverted_p] = MIPS_BRANCH ("b%N1", "%2,%z3,%0");
13839     }
13840 
13841   return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
13842 }
13843 
13844 /* Return the assembly code for INSN, which branches to OPERANDS[0]
13845    if some ordering condition is true.  The condition is given by
13846    OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
13847    OPERANDS[1].  OPERANDS[2] is the comparison's first operand;
13848    OPERANDS[3] is the second operand and may be zero or a register.  */
13849 
13850 const char *
mips_output_order_conditional_branch(rtx_insn * insn,rtx * operands,bool inverted_p)13851 mips_output_order_conditional_branch (rtx_insn *insn, rtx *operands,
13852 				      bool inverted_p)
13853 {
13854   const char *branch[2];
13855 
13856   /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
13857      Make BRANCH[0] branch on the inverse condition.  */
13858   if (operands[3] != const0_rtx)
13859     {
13860       /* Handle degenerate cases that should not, but do, occur.  */
13861       if (REGNO (operands[2]) == REGNO (operands[3]))
13862 	{
13863 	  switch (GET_CODE (operands[1]))
13864 	    {
13865 	    case LT:
13866 	    case LTU:
13867 	      inverted_p = !inverted_p;
13868 	      /* Fall through.  */
13869 	    case GE:
13870 	    case GEU:
13871 	      branch[!inverted_p] = MIPS_BRANCH_C ("b", "%0");
13872 	      branch[inverted_p] = "%*\t\t# branch never";
13873 	      break;
13874 	   default:
13875 	      gcc_unreachable ();
13876 	    }
13877 	}
13878       else
13879 	{
13880 	  branch[!inverted_p] = MIPS_BRANCH_C ("b%C1", "%2,%3,%0");
13881 	  branch[inverted_p] = MIPS_BRANCH_C ("b%N1", "%2,%3,%0");
13882 	}
13883     }
13884   else
13885     {
13886       switch (GET_CODE (operands[1]))
13887 	{
13888 	  /* These cases are equivalent to comparisons against zero.  */
13889 	case LEU:
13890 	  inverted_p = !inverted_p;
13891 	  /* Fall through.  */
13892 	case GTU:
13893 	  if (TARGET_CB_MAYBE)
13894 	    {
13895 	      branch[!inverted_p] = MIPS_BRANCH_C ("bnez", "%2,%0");
13896 	      branch[inverted_p] = MIPS_BRANCH_C ("beqz", "%2,%0");
13897 	    }
13898 	  else
13899 	    {
13900 	      branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
13901 	      branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
13902 	    }
13903 	  break;
13904 
13905 	  /* These cases are always true or always false.  */
13906 	case LTU:
13907 	  inverted_p = !inverted_p;
13908 	  /* Fall through.  */
13909 	case GEU:
13910 	  if (TARGET_CB_MAYBE)
13911 	    {
13912 	      branch[!inverted_p] = MIPS_BRANCH_C ("b", "%0");
13913 	      branch[inverted_p] = "%*\t\t# branch never";
13914 	    }
13915 	  else
13916 	    {
13917 	      branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
13918 	      branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
13919 	    }
13920 	  break;
13921 
13922 	default:
13923 	  if (TARGET_CB_MAYBE)
13924 	    {
13925 	      branch[!inverted_p] = MIPS_BRANCH_C ("b%C1z", "%2,%0");
13926 	      branch[inverted_p] = MIPS_BRANCH_C ("b%N1z", "%2,%0");
13927 	    }
13928 	  else
13929 	    {
13930 	      branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
13931 	      branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
13932 	    }
13933 	  break;
13934 	}
13935     }
13936   return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
13937 }
13938 
13939 /* Start a block of code that needs access to the LL, SC and SYNC
13940    instructions.  */
13941 
13942 static void
mips_start_ll_sc_sync_block(void)13943 mips_start_ll_sc_sync_block (void)
13944 {
13945   if (!ISA_HAS_LL_SC)
13946     {
13947       output_asm_insn (".set\tpush", 0);
13948       if (TARGET_64BIT)
13949 	output_asm_insn (".set\tmips3", 0);
13950       else
13951 	output_asm_insn (".set\tmips2", 0);
13952     }
13953 }
13954 
13955 /* End a block started by mips_start_ll_sc_sync_block.  */
13956 
13957 static void
mips_end_ll_sc_sync_block(void)13958 mips_end_ll_sc_sync_block (void)
13959 {
13960   if (!ISA_HAS_LL_SC)
13961     output_asm_insn (".set\tpop", 0);
13962 }
13963 
13964 /* Output and/or return the asm template for a sync instruction.  */
13965 
13966 const char *
mips_output_sync(void)13967 mips_output_sync (void)
13968 {
13969   mips_start_ll_sc_sync_block ();
13970   output_asm_insn ("sync", 0);
13971   mips_end_ll_sc_sync_block ();
13972   return "";
13973 }
13974 
13975 /* Return the asm template associated with sync_insn1 value TYPE.
13976    IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation.  */
13977 
13978 static const char *
mips_sync_insn1_template(enum attr_sync_insn1 type,bool is_64bit_p)13979 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
13980 {
13981   switch (type)
13982     {
13983     case SYNC_INSN1_MOVE:
13984       return "move\t%0,%z2";
13985     case SYNC_INSN1_LI:
13986       return "li\t%0,%2";
13987     case SYNC_INSN1_ADDU:
13988       return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
13989     case SYNC_INSN1_ADDIU:
13990       return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
13991     case SYNC_INSN1_SUBU:
13992       return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
13993     case SYNC_INSN1_AND:
13994       return "and\t%0,%1,%z2";
13995     case SYNC_INSN1_ANDI:
13996       return "andi\t%0,%1,%2";
13997     case SYNC_INSN1_OR:
13998       return "or\t%0,%1,%z2";
13999     case SYNC_INSN1_ORI:
14000       return "ori\t%0,%1,%2";
14001     case SYNC_INSN1_XOR:
14002       return "xor\t%0,%1,%z2";
14003     case SYNC_INSN1_XORI:
14004       return "xori\t%0,%1,%2";
14005     }
14006   gcc_unreachable ();
14007 }
14008 
14009 /* Return the asm template associated with sync_insn2 value TYPE.  */
14010 
14011 static const char *
mips_sync_insn2_template(enum attr_sync_insn2 type)14012 mips_sync_insn2_template (enum attr_sync_insn2 type)
14013 {
14014   switch (type)
14015     {
14016     case SYNC_INSN2_NOP:
14017       gcc_unreachable ();
14018     case SYNC_INSN2_AND:
14019       return "and\t%0,%1,%z2";
14020     case SYNC_INSN2_XOR:
14021       return "xor\t%0,%1,%z2";
14022     case SYNC_INSN2_NOT:
14023       return "nor\t%0,%1,%.";
14024     }
14025   gcc_unreachable ();
14026 }
14027 
14028 /* OPERANDS are the operands to a sync loop instruction and INDEX is
14029    the value of the one of the sync_* attributes.  Return the operand
14030    referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
14031    have the associated attribute.  */
14032 
14033 static rtx
mips_get_sync_operand(rtx * operands,int index,rtx default_value)14034 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
14035 {
14036   if (index > 0)
14037     default_value = operands[index - 1];
14038   return default_value;
14039 }
14040 
14041 /* INSN is a sync loop with operands OPERANDS.  Build up a multi-insn
14042    sequence for it.  */
14043 
14044 static void
mips_process_sync_loop(rtx_insn * insn,rtx * operands)14045 mips_process_sync_loop (rtx_insn *insn, rtx *operands)
14046 {
14047   rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
14048   rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
14049   unsigned int tmp3_insn;
14050   enum attr_sync_insn1 insn1;
14051   enum attr_sync_insn2 insn2;
14052   bool is_64bit_p;
14053   int memmodel_attr;
14054   enum memmodel model;
14055 
14056   /* Read an operand from the sync_WHAT attribute and store it in
14057      variable WHAT.  DEFAULT is the default value if no attribute
14058      is specified.  */
14059 #define READ_OPERAND(WHAT, DEFAULT) \
14060   WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
14061   				DEFAULT)
14062 
14063   /* Read the memory.  */
14064   READ_OPERAND (mem, 0);
14065   gcc_assert (mem);
14066   is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
14067 
14068   /* Read the other attributes.  */
14069   at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
14070   READ_OPERAND (oldval, at);
14071   READ_OPERAND (cmp, 0);
14072   READ_OPERAND (newval, at);
14073   READ_OPERAND (inclusive_mask, 0);
14074   READ_OPERAND (exclusive_mask, 0);
14075   READ_OPERAND (required_oldval, 0);
14076   READ_OPERAND (insn1_op2, 0);
14077   insn1 = get_attr_sync_insn1 (insn);
14078   insn2 = get_attr_sync_insn2 (insn);
14079 
14080   /* Don't bother setting CMP result that is never used.  */
14081   if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
14082     cmp = 0;
14083 
14084   memmodel_attr = get_attr_sync_memmodel (insn);
14085   switch (memmodel_attr)
14086     {
14087     case 10:
14088       model = MEMMODEL_ACQ_REL;
14089       break;
14090     case 11:
14091       model = MEMMODEL_ACQUIRE;
14092       break;
14093     default:
14094       model = memmodel_from_int (INTVAL (operands[memmodel_attr]));
14095     }
14096 
14097   mips_multi_start ();
14098 
14099   /* Output the release side of the memory barrier.  */
14100   if (need_atomic_barrier_p (model, true))
14101     {
14102       if (required_oldval == 0 && TARGET_OCTEON)
14103 	{
14104 	  /* Octeon doesn't reorder reads, so a full barrier can be
14105 	     created by using SYNCW to order writes combined with the
14106 	     write from the following SC.  When the SC successfully
14107 	     completes, we know that all preceding writes are also
14108 	     committed to the coherent memory system.  It is possible
14109 	     for a single SYNCW to fail, but a pair of them will never
14110 	     fail, so we use two.  */
14111 	  mips_multi_add_insn ("syncw", NULL);
14112 	  mips_multi_add_insn ("syncw", NULL);
14113 	}
14114       else
14115 	mips_multi_add_insn ("sync", NULL);
14116     }
14117 
14118   /* Output the branch-back label.  */
14119   mips_multi_add_label ("1:");
14120 
14121   /* OLDVAL = *MEM.  */
14122   mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
14123 		       oldval, mem, NULL);
14124 
14125   /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2.  */
14126   if (required_oldval)
14127     {
14128       if (inclusive_mask == 0)
14129 	tmp1 = oldval;
14130       else
14131 	{
14132 	  gcc_assert (oldval != at);
14133 	  mips_multi_add_insn ("and\t%0,%1,%2",
14134 			       at, oldval, inclusive_mask, NULL);
14135 	  tmp1 = at;
14136 	}
14137       if (TARGET_CB_NEVER)
14138 	mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
14139 
14140       /* CMP = 0 [delay slot].  */
14141       if (cmp)
14142         mips_multi_add_insn ("li\t%0,0", cmp, NULL);
14143 
14144       if (TARGET_CB_MAYBE && required_oldval == const0_rtx)
14145 	mips_multi_add_insn ("bnezc\t%0,2f", tmp1, NULL);
14146       else if (TARGET_CB_MAYBE)
14147 	mips_multi_add_insn ("bnec\t%0,%1,2f", tmp1, required_oldval, NULL);
14148 
14149     }
14150 
14151   /* $TMP1 = OLDVAL & EXCLUSIVE_MASK.  */
14152   if (exclusive_mask == 0)
14153     tmp1 = const0_rtx;
14154   else
14155     {
14156       gcc_assert (oldval != at);
14157       mips_multi_add_insn ("and\t%0,%1,%z2",
14158 			   at, oldval, exclusive_mask, NULL);
14159       tmp1 = at;
14160     }
14161 
14162   /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
14163 
14164      We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
14165      at least one instruction in that case.  */
14166   if (insn1 == SYNC_INSN1_MOVE
14167       && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
14168     tmp2 = insn1_op2;
14169   else
14170     {
14171       mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
14172 			   newval, oldval, insn1_op2, NULL);
14173       tmp2 = newval;
14174     }
14175 
14176   /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK).  */
14177   if (insn2 == SYNC_INSN2_NOP)
14178     tmp3 = tmp2;
14179   else
14180     {
14181       mips_multi_add_insn (mips_sync_insn2_template (insn2),
14182 			   newval, tmp2, inclusive_mask, NULL);
14183       tmp3 = newval;
14184     }
14185   tmp3_insn = mips_multi_last_index ();
14186 
14187   /* $AT = $TMP1 | $TMP3.  */
14188   if (tmp1 == const0_rtx || tmp3 == const0_rtx)
14189     {
14190       mips_multi_set_operand (tmp3_insn, 0, at);
14191       tmp3 = at;
14192     }
14193   else
14194     {
14195       gcc_assert (tmp1 != tmp3);
14196       mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
14197     }
14198 
14199   /* if (!commit (*MEM = $AT)) goto 1.
14200 
14201      This will sometimes be a delayed branch; see the write code below
14202      for details.  */
14203   mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
14204 
14205   /* When using branch likely (-mfix-r10000), the delay slot instruction
14206      will be annulled on false.  The normal delay slot instructions
14207      calculate the overall result of the atomic operation and must not
14208      be annulled.  To ensure this behavior unconditionally use a NOP
14209      in the delay slot for the branch likely case.  */
14210 
14211   if (TARGET_CB_MAYBE)
14212     mips_multi_add_insn ("beqzc\t%0,1b", at, NULL);
14213   else
14214     mips_multi_add_insn ("beq%?\t%0,%.,1b%~", at, NULL);
14215 
14216   /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot].  */
14217   if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
14218     {
14219       mips_multi_copy_insn (tmp3_insn);
14220       mips_multi_set_operand (mips_multi_last_index (), 0, newval);
14221     }
14222   else if (!(required_oldval && cmp) && !mips_branch_likely)
14223     mips_multi_add_insn ("nop", NULL);
14224 
14225   /* CMP = 1 -- either standalone or in a delay slot.  */
14226   if (required_oldval && cmp)
14227     mips_multi_add_insn ("li\t%0,1", cmp, NULL);
14228 
14229   /* Output the acquire side of the memory barrier.  */
14230   if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
14231     mips_multi_add_insn ("sync", NULL);
14232 
14233   /* Output the exit label, if needed.  */
14234   if (required_oldval)
14235     mips_multi_add_label ("2:");
14236 
14237 #undef READ_OPERAND
14238 }
14239 
14240 /* Output and/or return the asm template for sync loop INSN, which has
14241    the operands given by OPERANDS.  */
14242 
14243 const char *
mips_output_sync_loop(rtx_insn * insn,rtx * operands)14244 mips_output_sync_loop (rtx_insn *insn, rtx *operands)
14245 {
14246   /* Use branch-likely instructions to work around the LL/SC R10000
14247      errata.  */
14248   mips_branch_likely = TARGET_FIX_R10000;
14249 
14250   mips_process_sync_loop (insn, operands);
14251 
14252   mips_push_asm_switch (&mips_noreorder);
14253   mips_push_asm_switch (&mips_nomacro);
14254   mips_push_asm_switch (&mips_noat);
14255   mips_start_ll_sc_sync_block ();
14256 
14257   mips_multi_write ();
14258 
14259   mips_end_ll_sc_sync_block ();
14260   mips_pop_asm_switch (&mips_noat);
14261   mips_pop_asm_switch (&mips_nomacro);
14262   mips_pop_asm_switch (&mips_noreorder);
14263 
14264   return "";
14265 }
14266 
14267 /* Return the number of individual instructions in sync loop INSN,
14268    which has the operands given by OPERANDS.  */
14269 
14270 unsigned int
mips_sync_loop_insns(rtx_insn * insn,rtx * operands)14271 mips_sync_loop_insns (rtx_insn *insn, rtx *operands)
14272 {
14273   /* Use branch-likely instructions to work around the LL/SC R10000
14274      errata.  */
14275   mips_branch_likely = TARGET_FIX_R10000;
14276   mips_process_sync_loop (insn, operands);
14277   return mips_multi_num_insns;
14278 }
14279 
14280 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
14281    the operands given by OPERANDS.  Add in a divide-by-zero check if needed.
14282 
14283    When working around R4000 and R4400 errata, we need to make sure that
14284    the division is not immediately followed by a shift[1][2].  We also
14285    need to stop the division from being put into a branch delay slot[3].
14286    The easiest way to avoid both problems is to add a nop after the
14287    division.  When a divide-by-zero check is needed, this nop can be
14288    used to fill the branch delay slot.
14289 
14290    [1] If a double-word or a variable shift executes immediately
14291        after starting an integer division, the shift may give an
14292        incorrect result.  See quotations of errata #16 and #28 from
14293        "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
14294        in mips.md for details.
14295 
14296    [2] A similar bug to [1] exists for all revisions of the
14297        R4000 and the R4400 when run in an MC configuration.
14298        From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
14299 
14300        "19. In this following sequence:
14301 
14302 		    ddiv		(or ddivu or div or divu)
14303 		    dsll32		(or dsrl32, dsra32)
14304 
14305 	    if an MPT stall occurs, while the divide is slipping the cpu
14306 	    pipeline, then the following double shift would end up with an
14307 	    incorrect result.
14308 
14309 	    Workaround: The compiler needs to avoid generating any
14310 	    sequence with divide followed by extended double shift."
14311 
14312        This erratum is also present in "MIPS R4400MC Errata, Processor
14313        Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
14314        & 3.0" as errata #10 and #4, respectively.
14315 
14316    [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
14317        (also valid for MIPS R4000MC processors):
14318 
14319        "52. R4000SC: This bug does not apply for the R4000PC.
14320 
14321 	    There are two flavors of this bug:
14322 
14323 	    1) If the instruction just after divide takes an RF exception
14324 	       (tlb-refill, tlb-invalid) and gets an instruction cache
14325 	       miss (both primary and secondary) and the line which is
14326 	       currently in secondary cache at this index had the first
14327 	       data word, where the bits 5..2 are set, then R4000 would
14328 	       get a wrong result for the div.
14329 
14330 	    ##1
14331 		    nop
14332 		    div	r8, r9
14333 		    -------------------		# end-of page. -tlb-refill
14334 		    nop
14335 	    ##2
14336 		    nop
14337 		    div	r8, r9
14338 		    -------------------		# end-of page. -tlb-invalid
14339 		    nop
14340 
14341 	    2) If the divide is in the taken branch delay slot, where the
14342 	       target takes RF exception and gets an I-cache miss for the
14343 	       exception vector or where I-cache miss occurs for the
14344 	       target address, under the above mentioned scenarios, the
14345 	       div would get wrong results.
14346 
14347 	    ##1
14348 		    j	r2		# to next page mapped or unmapped
14349 		    div	r8,r9		# this bug would be there as long
14350 					# as there is an ICache miss and
14351 		    nop			# the "data pattern" is present
14352 
14353 	    ##2
14354 		    beq	r0, r0, NextPage	# to Next page
14355 		    div	r8,r9
14356 		    nop
14357 
14358 	    This bug is present for div, divu, ddiv, and ddivu
14359 	    instructions.
14360 
14361 	    Workaround: For item 1), OS could make sure that the next page
14362 	    after the divide instruction is also mapped.  For item 2), the
14363 	    compiler could make sure that the divide instruction is not in
14364 	    the branch delay slot."
14365 
14366        These processors have PRId values of 0x00004220 and 0x00004300 for
14367        the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400.  */
14368 
14369 const char *
mips_output_division(const char * division,rtx * operands)14370 mips_output_division (const char *division, rtx *operands)
14371 {
14372   const char *s;
14373 
14374   s = division;
14375   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
14376     {
14377       output_asm_insn (s, operands);
14378       s = "nop";
14379     }
14380   if (TARGET_CHECK_ZERO_DIV)
14381     {
14382       if (TARGET_MIPS16)
14383 	{
14384 	  output_asm_insn (s, operands);
14385 	  s = "bnez\t%2,1f\n\tbreak\t7\n1:";
14386 	}
14387       else if (GENERATE_DIVIDE_TRAPS)
14388 	{
14389 	  /* Avoid long replay penalty on load miss by putting the trap before
14390 	     the divide.  */
14391 	  if (TUNE_74K)
14392 	    output_asm_insn ("teq\t%2,%.,7", operands);
14393 	  else
14394 	    {
14395 	      output_asm_insn (s, operands);
14396 	      s = "teq\t%2,%.,7";
14397 	    }
14398 	}
14399       else
14400 	{
14401 	  if (flag_delayed_branch)
14402 	    {
14403 	      output_asm_insn ("%(bne\t%2,%.,1f", operands);
14404 	      output_asm_insn (s, operands);
14405 	      s = "break\t7%)\n1:";
14406 	    }
14407 	  else
14408 	    {
14409 	      output_asm_insn (s, operands);
14410 	      s = "bne\t%2,%.,1f\n\tnop\n\tbreak\t7\n1:";
14411 	    }
14412 	}
14413     }
14414   return s;
14415 }
14416 
14417 /* Return the assembly code for MSA DIV_{S,U}.DF or MOD_{S,U}.DF instructions,
14418    which has the operands given by OPERANDS.  Add in a divide-by-zero check
14419    if needed.  */
14420 
14421 const char *
mips_msa_output_division(const char * division,rtx * operands)14422 mips_msa_output_division (const char *division, rtx *operands)
14423 {
14424   const char *s;
14425 
14426   s = division;
14427   if (TARGET_CHECK_ZERO_DIV)
14428     {
14429       output_asm_insn ("%(bnz.%v0\t%w2,1f", operands);
14430       output_asm_insn (s, operands);
14431       s = "break\t7%)\n1:";
14432     }
14433   return s;
14434 }
14435 
14436 /* Return true if destination of IN_INSN is used as add source in
14437    OUT_INSN. Both IN_INSN and OUT_INSN are of type fmadd. Example:
14438    madd.s dst, x, y, z
14439    madd.s a, dst, b, c  */
14440 
14441 bool
mips_fmadd_bypass(rtx_insn * out_insn,rtx_insn * in_insn)14442 mips_fmadd_bypass (rtx_insn *out_insn, rtx_insn *in_insn)
14443 {
14444   int dst_reg, src_reg;
14445 
14446   gcc_assert (get_attr_type (in_insn) == TYPE_FMADD);
14447   gcc_assert (get_attr_type (out_insn) == TYPE_FMADD);
14448 
14449   extract_insn (in_insn);
14450   dst_reg = REG_P (recog_data.operand[0]);
14451 
14452   extract_insn (out_insn);
14453   src_reg = REG_P (recog_data.operand[1]);
14454 
14455   if (dst_reg == src_reg)
14456     return true;
14457 
14458   return false;
14459 }
14460 
14461 /* Return true if IN_INSN is a multiply-add or multiply-subtract
14462    instruction and if OUT_INSN assigns to the accumulator operand.  */
14463 
14464 bool
mips_linked_madd_p(rtx_insn * out_insn,rtx_insn * in_insn)14465 mips_linked_madd_p (rtx_insn *out_insn, rtx_insn *in_insn)
14466 {
14467   enum attr_accum_in accum_in;
14468   int accum_in_opnum;
14469   rtx accum_in_op;
14470 
14471   if (recog_memoized (in_insn) < 0)
14472     return false;
14473 
14474   accum_in = get_attr_accum_in (in_insn);
14475   if (accum_in == ACCUM_IN_NONE)
14476     return false;
14477 
14478   accum_in_opnum = accum_in - ACCUM_IN_0;
14479 
14480   extract_insn (in_insn);
14481   gcc_assert (accum_in_opnum < recog_data.n_operands);
14482   accum_in_op = recog_data.operand[accum_in_opnum];
14483 
14484   return reg_set_p (accum_in_op, out_insn);
14485 }
14486 
14487 /* True if the dependency between OUT_INSN and IN_INSN is on the store
14488    data rather than the address.  We need this because the cprestore
14489    pattern is type "store", but is defined using an UNSPEC_VOLATILE,
14490    which causes the default routine to abort.  We just return false
14491    for that case.  */
14492 
14493 bool
mips_store_data_bypass_p(rtx_insn * out_insn,rtx_insn * in_insn)14494 mips_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
14495 {
14496   if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
14497     return false;
14498 
14499   return store_data_bypass_p (out_insn, in_insn);
14500 }
14501 
14502 
14503 /* Variables and flags used in scheduler hooks when tuning for
14504    Loongson 2E/2F.  */
14505 static struct
14506 {
14507   /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
14508      strategy.  */
14509 
14510   /* If true, then next ALU1/2 instruction will go to ALU1.  */
14511   bool alu1_turn_p;
14512 
14513   /* If true, then next FALU1/2 unstruction will go to FALU1.  */
14514   bool falu1_turn_p;
14515 
14516   /* Codes to query if [f]alu{1,2}_core units are subscribed or not.  */
14517   int alu1_core_unit_code;
14518   int alu2_core_unit_code;
14519   int falu1_core_unit_code;
14520   int falu2_core_unit_code;
14521 
14522   /* True if current cycle has a multi instruction.
14523      This flag is used in mips_ls2_dfa_post_advance_cycle.  */
14524   bool cycle_has_multi_p;
14525 
14526   /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
14527      These are used in mips_ls2_dfa_post_advance_cycle to initialize
14528      DFA state.
14529      E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
14530      instruction to go ALU1.  */
14531   rtx_insn *alu1_turn_enabled_insn;
14532   rtx_insn *alu2_turn_enabled_insn;
14533   rtx_insn *falu1_turn_enabled_insn;
14534   rtx_insn *falu2_turn_enabled_insn;
14535 } mips_ls2;
14536 
14537 /* Implement TARGET_SCHED_ADJUST_COST.  We assume that anti and output
14538    dependencies have no cost, except on the 20Kc where output-dependence
14539    is treated like input-dependence.  */
14540 
14541 static int
mips_adjust_cost(rtx_insn *,int dep_type,rtx_insn *,int cost,unsigned int)14542 mips_adjust_cost (rtx_insn *, int dep_type, rtx_insn *, int cost, unsigned int)
14543 {
14544   if (dep_type != 0 && (dep_type != REG_DEP_OUTPUT || !TUNE_20KC))
14545     return 0;
14546   return cost;
14547 }
14548 
14549 /* Return the number of instructions that can be issued per cycle.  */
14550 
14551 static int
mips_issue_rate(void)14552 mips_issue_rate (void)
14553 {
14554   switch (mips_tune)
14555     {
14556     case PROCESSOR_74KC:
14557     case PROCESSOR_74KF2_1:
14558     case PROCESSOR_74KF1_1:
14559     case PROCESSOR_74KF3_2:
14560       /* The 74k is not strictly quad-issue cpu, but can be seen as one
14561 	 by the scheduler.  It can issue 1 ALU, 1 AGEN and 2 FPU insns,
14562 	 but in reality only a maximum of 3 insns can be issued as
14563 	 floating-point loads and stores also require a slot in the
14564 	 AGEN pipe.  */
14565     case PROCESSOR_R10000:
14566       /* All R10K Processors are quad-issue (being the first MIPS
14567          processors to support this feature). */
14568       return 4;
14569 
14570     case PROCESSOR_20KC:
14571     case PROCESSOR_R4130:
14572     case PROCESSOR_R5400:
14573     case PROCESSOR_R5500:
14574     case PROCESSOR_R5900:
14575     case PROCESSOR_R7000:
14576     case PROCESSOR_R9000:
14577     case PROCESSOR_OCTEON:
14578     case PROCESSOR_OCTEON2:
14579     case PROCESSOR_OCTEON3:
14580     case PROCESSOR_I6400:
14581       return 2;
14582 
14583     case PROCESSOR_SB1:
14584     case PROCESSOR_SB1A:
14585       /* This is actually 4, but we get better performance if we claim 3.
14586 	 This is partly because of unwanted speculative code motion with the
14587 	 larger number, and partly because in most common cases we can't
14588 	 reach the theoretical max of 4.  */
14589       return 3;
14590 
14591     case PROCESSOR_LOONGSON_2E:
14592     case PROCESSOR_LOONGSON_2F:
14593     case PROCESSOR_LOONGSON_3A:
14594     case PROCESSOR_P5600:
14595       return 4;
14596 
14597     case PROCESSOR_XLP:
14598       return (reload_completed ? 4 : 3);
14599 
14600     default:
14601       return 1;
14602     }
14603 }
14604 
14605 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2.  */
14606 
14607 static void
mips_ls2_init_dfa_post_cycle_insn(void)14608 mips_ls2_init_dfa_post_cycle_insn (void)
14609 {
14610   start_sequence ();
14611   emit_insn (gen_ls2_alu1_turn_enabled_insn ());
14612   mips_ls2.alu1_turn_enabled_insn = get_insns ();
14613   end_sequence ();
14614 
14615   start_sequence ();
14616   emit_insn (gen_ls2_alu2_turn_enabled_insn ());
14617   mips_ls2.alu2_turn_enabled_insn = get_insns ();
14618   end_sequence ();
14619 
14620   start_sequence ();
14621   emit_insn (gen_ls2_falu1_turn_enabled_insn ());
14622   mips_ls2.falu1_turn_enabled_insn = get_insns ();
14623   end_sequence ();
14624 
14625   start_sequence ();
14626   emit_insn (gen_ls2_falu2_turn_enabled_insn ());
14627   mips_ls2.falu2_turn_enabled_insn = get_insns ();
14628   end_sequence ();
14629 
14630   mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
14631   mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
14632   mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
14633   mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
14634 }
14635 
14636 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
14637    Init data used in mips_dfa_post_advance_cycle.  */
14638 
14639 static void
mips_init_dfa_post_cycle_insn(void)14640 mips_init_dfa_post_cycle_insn (void)
14641 {
14642   if (TUNE_LOONGSON_2EF)
14643     mips_ls2_init_dfa_post_cycle_insn ();
14644 }
14645 
14646 /* Initialize STATE when scheduling for Loongson 2E/2F.
14647    Support round-robin dispatch scheme by enabling only one of
14648    ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
14649    respectively.  */
14650 
14651 static void
mips_ls2_dfa_post_advance_cycle(state_t state)14652 mips_ls2_dfa_post_advance_cycle (state_t state)
14653 {
14654   if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
14655     {
14656       /* Though there are no non-pipelined ALU1 insns,
14657 	 we can get an instruction of type 'multi' before reload.  */
14658       gcc_assert (mips_ls2.cycle_has_multi_p);
14659       mips_ls2.alu1_turn_p = false;
14660     }
14661 
14662   mips_ls2.cycle_has_multi_p = false;
14663 
14664   if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
14665     /* We have a non-pipelined alu instruction in the core,
14666        adjust round-robin counter.  */
14667     mips_ls2.alu1_turn_p = true;
14668 
14669   if (mips_ls2.alu1_turn_p)
14670     {
14671       if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
14672 	gcc_unreachable ();
14673     }
14674   else
14675     {
14676       if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
14677 	gcc_unreachable ();
14678     }
14679 
14680   if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
14681     {
14682       /* There are no non-pipelined FALU1 insns.  */
14683       gcc_unreachable ();
14684       mips_ls2.falu1_turn_p = false;
14685     }
14686 
14687   if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
14688     /* We have a non-pipelined falu instruction in the core,
14689        adjust round-robin counter.  */
14690     mips_ls2.falu1_turn_p = true;
14691 
14692   if (mips_ls2.falu1_turn_p)
14693     {
14694       if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
14695 	gcc_unreachable ();
14696     }
14697   else
14698     {
14699       if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
14700 	gcc_unreachable ();
14701     }
14702 }
14703 
14704 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
14705    This hook is being called at the start of each cycle.  */
14706 
14707 static void
mips_dfa_post_advance_cycle(void)14708 mips_dfa_post_advance_cycle (void)
14709 {
14710   if (TUNE_LOONGSON_2EF)
14711     mips_ls2_dfa_post_advance_cycle (curr_state);
14712 }
14713 
14714 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD.  This should
14715    be as wide as the scheduling freedom in the DFA.  */
14716 
14717 static int
mips_multipass_dfa_lookahead(void)14718 mips_multipass_dfa_lookahead (void)
14719 {
14720   /* Can schedule up to 4 of the 6 function units in any one cycle.  */
14721   if (TUNE_SB1)
14722     return 4;
14723 
14724   if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
14725     return 4;
14726 
14727   if (TUNE_OCTEON)
14728     return 2;
14729 
14730   if (TUNE_P5600 || TUNE_I6400)
14731     return 4;
14732 
14733   return 0;
14734 }
14735 
14736 /* Remove the instruction at index LOWER from ready queue READY and
14737    reinsert it in front of the instruction at index HIGHER.  LOWER must
14738    be <= HIGHER.  */
14739 
14740 static void
mips_promote_ready(rtx_insn ** ready,int lower,int higher)14741 mips_promote_ready (rtx_insn **ready, int lower, int higher)
14742 {
14743   rtx_insn *new_head;
14744   int i;
14745 
14746   new_head = ready[lower];
14747   for (i = lower; i < higher; i++)
14748     ready[i] = ready[i + 1];
14749   ready[i] = new_head;
14750 }
14751 
14752 /* If the priority of the instruction at POS2 in the ready queue READY
14753    is within LIMIT units of that of the instruction at POS1, swap the
14754    instructions if POS2 is not already less than POS1.  */
14755 
14756 static void
mips_maybe_swap_ready(rtx_insn ** ready,int pos1,int pos2,int limit)14757 mips_maybe_swap_ready (rtx_insn **ready, int pos1, int pos2, int limit)
14758 {
14759   if (pos1 < pos2
14760       && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
14761     {
14762       rtx_insn *temp;
14763 
14764       temp = ready[pos1];
14765       ready[pos1] = ready[pos2];
14766       ready[pos2] = temp;
14767     }
14768 }
14769 
14770 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
14771    that may clobber hi or lo.  */
14772 static rtx_insn *mips_macc_chains_last_hilo;
14773 
14774 /* A TUNE_MACC_CHAINS helper function.  Record that instruction INSN has
14775    been scheduled, updating mips_macc_chains_last_hilo appropriately.  */
14776 
14777 static void
mips_macc_chains_record(rtx_insn * insn)14778 mips_macc_chains_record (rtx_insn *insn)
14779 {
14780   if (get_attr_may_clobber_hilo (insn))
14781     mips_macc_chains_last_hilo = insn;
14782 }
14783 
14784 /* A TUNE_MACC_CHAINS helper function.  Search ready queue READY, which
14785    has NREADY elements, looking for a multiply-add or multiply-subtract
14786    instruction that is cumulative with mips_macc_chains_last_hilo.
14787    If there is one, promote it ahead of anything else that might
14788    clobber hi or lo.  */
14789 
14790 static void
mips_macc_chains_reorder(rtx_insn ** ready,int nready)14791 mips_macc_chains_reorder (rtx_insn **ready, int nready)
14792 {
14793   int i, j;
14794 
14795   if (mips_macc_chains_last_hilo != 0)
14796     for (i = nready - 1; i >= 0; i--)
14797       if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
14798 	{
14799 	  for (j = nready - 1; j > i; j--)
14800 	    if (recog_memoized (ready[j]) >= 0
14801 		&& get_attr_may_clobber_hilo (ready[j]))
14802 	      {
14803 		mips_promote_ready (ready, i, j);
14804 		break;
14805 	      }
14806 	  break;
14807 	}
14808 }
14809 
14810 /* The last instruction to be scheduled.  */
14811 static rtx_insn *vr4130_last_insn;
14812 
14813 /* A note_stores callback used by vr4130_true_reg_dependence_p.  DATA
14814    points to an rtx that is initially an instruction.  Nullify the rtx
14815    if the instruction uses the value of register X.  */
14816 
14817 static void
vr4130_true_reg_dependence_p_1(rtx x,const_rtx pat ATTRIBUTE_UNUSED,void * data)14818 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
14819 				void *data)
14820 {
14821   rtx *insn_ptr;
14822 
14823   insn_ptr = (rtx *) data;
14824   if (REG_P (x)
14825       && *insn_ptr != 0
14826       && reg_referenced_p (x, PATTERN (*insn_ptr)))
14827     *insn_ptr = 0;
14828 }
14829 
14830 /* Return true if there is true register dependence between vr4130_last_insn
14831    and INSN.  */
14832 
14833 static bool
vr4130_true_reg_dependence_p(rtx insn)14834 vr4130_true_reg_dependence_p (rtx insn)
14835 {
14836   note_stores (PATTERN (vr4130_last_insn),
14837 	       vr4130_true_reg_dependence_p_1, &insn);
14838   return insn == 0;
14839 }
14840 
14841 /* A TUNE_MIPS4130 helper function.  Given that INSN1 is at the head of
14842    the ready queue and that INSN2 is the instruction after it, return
14843    true if it is worth promoting INSN2 ahead of INSN1.  Look for cases
14844    in which INSN1 and INSN2 can probably issue in parallel, but for
14845    which (INSN2, INSN1) should be less sensitive to instruction
14846    alignment than (INSN1, INSN2).  See 4130.md for more details.  */
14847 
14848 static bool
vr4130_swap_insns_p(rtx_insn * insn1,rtx_insn * insn2)14849 vr4130_swap_insns_p (rtx_insn *insn1, rtx_insn *insn2)
14850 {
14851   sd_iterator_def sd_it;
14852   dep_t dep;
14853 
14854   /* Check for the following case:
14855 
14856      1) there is some other instruction X with an anti dependence on INSN1;
14857      2) X has a higher priority than INSN2; and
14858      3) X is an arithmetic instruction (and thus has no unit restrictions).
14859 
14860      If INSN1 is the last instruction blocking X, it would better to
14861      choose (INSN1, X) over (INSN2, INSN1).  */
14862   FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
14863     if (DEP_TYPE (dep) == REG_DEP_ANTI
14864 	&& INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
14865 	&& recog_memoized (DEP_CON (dep)) >= 0
14866 	&& get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
14867       return false;
14868 
14869   if (vr4130_last_insn != 0
14870       && recog_memoized (insn1) >= 0
14871       && recog_memoized (insn2) >= 0)
14872     {
14873       /* See whether INSN1 and INSN2 use different execution units,
14874 	 or if they are both ALU-type instructions.  If so, they can
14875 	 probably execute in parallel.  */
14876       enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
14877       enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
14878       if (class1 != class2 || class1 == VR4130_CLASS_ALU)
14879 	{
14880 	  /* If only one of the instructions has a dependence on
14881 	     vr4130_last_insn, prefer to schedule the other one first.  */
14882 	  bool dep1_p = vr4130_true_reg_dependence_p (insn1);
14883 	  bool dep2_p = vr4130_true_reg_dependence_p (insn2);
14884 	  if (dep1_p != dep2_p)
14885 	    return dep1_p;
14886 
14887 	  /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
14888 	     is not an ALU-type instruction and if INSN1 uses the same
14889 	     execution unit.  (Note that if this condition holds, we already
14890 	     know that INSN2 uses a different execution unit.)  */
14891 	  if (class1 != VR4130_CLASS_ALU
14892 	      && recog_memoized (vr4130_last_insn) >= 0
14893 	      && class1 == get_attr_vr4130_class (vr4130_last_insn))
14894 	    return true;
14895 	}
14896     }
14897   return false;
14898 }
14899 
14900 /* A TUNE_MIPS4130 helper function.  (READY, NREADY) describes a ready
14901    queue with at least two instructions.  Swap the first two if
14902    vr4130_swap_insns_p says that it could be worthwhile.  */
14903 
14904 static void
vr4130_reorder(rtx_insn ** ready,int nready)14905 vr4130_reorder (rtx_insn **ready, int nready)
14906 {
14907   if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
14908     mips_promote_ready (ready, nready - 2, nready - 1);
14909 }
14910 
14911 /* Record whether last 74k AGEN instruction was a load or store.  */
14912 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
14913 
14914 /* Initialize mips_last_74k_agen_insn from INSN.  A null argument
14915    resets to TYPE_UNKNOWN state.  */
14916 
14917 static void
mips_74k_agen_init(rtx_insn * insn)14918 mips_74k_agen_init (rtx_insn *insn)
14919 {
14920   if (!insn || CALL_P (insn) || JUMP_P (insn))
14921     mips_last_74k_agen_insn = TYPE_UNKNOWN;
14922   else
14923     {
14924       enum attr_type type = get_attr_type (insn);
14925       if (type == TYPE_LOAD || type == TYPE_STORE)
14926 	mips_last_74k_agen_insn = type;
14927     }
14928 }
14929 
14930 /* A TUNE_74K helper function.  The 74K AGEN pipeline likes multiple
14931    loads to be grouped together, and multiple stores to be grouped
14932    together.  Swap things around in the ready queue to make this happen.  */
14933 
14934 static void
mips_74k_agen_reorder(rtx_insn ** ready,int nready)14935 mips_74k_agen_reorder (rtx_insn **ready, int nready)
14936 {
14937   int i;
14938   int store_pos, load_pos;
14939 
14940   store_pos = -1;
14941   load_pos = -1;
14942 
14943   for (i = nready - 1; i >= 0; i--)
14944     {
14945       rtx_insn *insn = ready[i];
14946       if (USEFUL_INSN_P (insn))
14947 	switch (get_attr_type (insn))
14948 	  {
14949 	  case TYPE_STORE:
14950 	    if (store_pos == -1)
14951 	      store_pos = i;
14952 	    break;
14953 
14954 	  case TYPE_LOAD:
14955 	    if (load_pos == -1)
14956 	      load_pos = i;
14957 	    break;
14958 
14959 	  default:
14960 	    break;
14961 	  }
14962     }
14963 
14964   if (load_pos == -1 || store_pos == -1)
14965     return;
14966 
14967   switch (mips_last_74k_agen_insn)
14968     {
14969     case TYPE_UNKNOWN:
14970       /* Prefer to schedule loads since they have a higher latency.  */
14971     case TYPE_LOAD:
14972       /* Swap loads to the front of the queue.  */
14973       mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
14974       break;
14975     case TYPE_STORE:
14976       /* Swap stores to the front of the queue.  */
14977       mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
14978       break;
14979     default:
14980       break;
14981     }
14982 }
14983 
14984 /* Implement TARGET_SCHED_INIT.  */
14985 
14986 static void
mips_sched_init(FILE * file ATTRIBUTE_UNUSED,int verbose ATTRIBUTE_UNUSED,int max_ready ATTRIBUTE_UNUSED)14987 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
14988 		 int max_ready ATTRIBUTE_UNUSED)
14989 {
14990   mips_macc_chains_last_hilo = 0;
14991   vr4130_last_insn = 0;
14992   mips_74k_agen_init (NULL);
14993 
14994   /* When scheduling for Loongson2, branch instructions go to ALU1,
14995      therefore basic block is most likely to start with round-robin counter
14996      pointed to ALU2.  */
14997   mips_ls2.alu1_turn_p = false;
14998   mips_ls2.falu1_turn_p = true;
14999 }
15000 
15001 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2.  */
15002 
15003 static void
mips_sched_reorder_1(FILE * file ATTRIBUTE_UNUSED,int verbose ATTRIBUTE_UNUSED,rtx_insn ** ready,int * nreadyp,int cycle ATTRIBUTE_UNUSED)15004 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15005 		      rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
15006 {
15007   if (!reload_completed
15008       && TUNE_MACC_CHAINS
15009       && *nreadyp > 0)
15010     mips_macc_chains_reorder (ready, *nreadyp);
15011 
15012   if (reload_completed
15013       && TUNE_MIPS4130
15014       && !TARGET_VR4130_ALIGN
15015       && *nreadyp > 1)
15016     vr4130_reorder (ready, *nreadyp);
15017 
15018   if (TUNE_74K)
15019     mips_74k_agen_reorder (ready, *nreadyp);
15020 }
15021 
15022 /* Implement TARGET_SCHED_REORDER.  */
15023 
15024 static int
mips_sched_reorder(FILE * file ATTRIBUTE_UNUSED,int verbose ATTRIBUTE_UNUSED,rtx_insn ** ready,int * nreadyp,int cycle ATTRIBUTE_UNUSED)15025 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15026 		    rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
15027 {
15028   mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
15029   return mips_issue_rate ();
15030 }
15031 
15032 /* Implement TARGET_SCHED_REORDER2.  */
15033 
15034 static int
mips_sched_reorder2(FILE * file ATTRIBUTE_UNUSED,int verbose ATTRIBUTE_UNUSED,rtx_insn ** ready,int * nreadyp,int cycle ATTRIBUTE_UNUSED)15035 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15036 		     rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
15037 {
15038   mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
15039   return cached_can_issue_more;
15040 }
15041 
15042 /* Update round-robin counters for ALU1/2 and FALU1/2.  */
15043 
15044 static void
mips_ls2_variable_issue(rtx_insn * insn)15045 mips_ls2_variable_issue (rtx_insn *insn)
15046 {
15047   if (mips_ls2.alu1_turn_p)
15048     {
15049       if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
15050 	mips_ls2.alu1_turn_p = false;
15051     }
15052   else
15053     {
15054       if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
15055 	mips_ls2.alu1_turn_p = true;
15056     }
15057 
15058   if (mips_ls2.falu1_turn_p)
15059     {
15060       if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
15061 	mips_ls2.falu1_turn_p = false;
15062     }
15063   else
15064     {
15065       if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
15066 	mips_ls2.falu1_turn_p = true;
15067     }
15068 
15069   if (recog_memoized (insn) >= 0)
15070     mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
15071 }
15072 
15073 /* Implement TARGET_SCHED_VARIABLE_ISSUE.  */
15074 
15075 static int
mips_variable_issue(FILE * file ATTRIBUTE_UNUSED,int verbose ATTRIBUTE_UNUSED,rtx_insn * insn,int more)15076 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15077 		     rtx_insn *insn, int more)
15078 {
15079   /* Ignore USEs and CLOBBERs; don't count them against the issue rate.  */
15080   if (USEFUL_INSN_P (insn))
15081     {
15082       if (get_attr_type (insn) != TYPE_GHOST)
15083 	more--;
15084       if (!reload_completed && TUNE_MACC_CHAINS)
15085 	mips_macc_chains_record (insn);
15086       vr4130_last_insn = insn;
15087       if (TUNE_74K)
15088 	mips_74k_agen_init (insn);
15089       else if (TUNE_LOONGSON_2EF)
15090 	mips_ls2_variable_issue (insn);
15091     }
15092 
15093   /* Instructions of type 'multi' should all be split before
15094      the second scheduling pass.  */
15095   gcc_assert (!reload_completed
15096 	      || recog_memoized (insn) < 0
15097 	      || get_attr_type (insn) != TYPE_MULTI);
15098 
15099   cached_can_issue_more = more;
15100   return more;
15101 }
15102 
15103 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
15104    return the first operand of the associated PREF or PREFX insn.  */
15105 
15106 rtx
mips_prefetch_cookie(rtx write,rtx locality)15107 mips_prefetch_cookie (rtx write, rtx locality)
15108 {
15109   /* store_streamed / load_streamed.  */
15110   if (INTVAL (locality) <= 0)
15111     return GEN_INT (INTVAL (write) + 4);
15112 
15113   /* store / load.  */
15114   if (INTVAL (locality) <= 2)
15115     return write;
15116 
15117   /* store_retained / load_retained.  */
15118   return GEN_INT (INTVAL (write) + 6);
15119 }
15120 
15121 /* Flags that indicate when a built-in function is available.
15122 
15123    BUILTIN_AVAIL_NON_MIPS16
15124 	The function is available on the current target if !TARGET_MIPS16.
15125 
15126    BUILTIN_AVAIL_MIPS16
15127 	The function is available on the current target if TARGET_MIPS16.  */
15128 #define BUILTIN_AVAIL_NON_MIPS16 1
15129 #define BUILTIN_AVAIL_MIPS16 2
15130 
15131 /* Declare an availability predicate for built-in functions that
15132    require non-MIPS16 mode and also require COND to be true.
15133    NAME is the main part of the predicate's name.  */
15134 #define AVAIL_NON_MIPS16(NAME, COND)					\
15135  static unsigned int							\
15136  mips_builtin_avail_##NAME (void)					\
15137  {									\
15138    return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0;			\
15139  }
15140 
15141 /* Declare an availability predicate for built-in functions that
15142    support both MIPS16 and non-MIPS16 code and also require COND
15143    to be true.  NAME is the main part of the predicate's name.  */
15144 #define AVAIL_ALL(NAME, COND)						\
15145  static unsigned int							\
15146  mips_builtin_avail_##NAME (void)					\
15147  {									\
15148    return (COND) ? BUILTIN_AVAIL_NON_MIPS16 | BUILTIN_AVAIL_MIPS16 : 0;	\
15149  }
15150 
15151 /* This structure describes a single built-in function.  */
15152 struct mips_builtin_description {
15153   /* The code of the main .md file instruction.  See mips_builtin_type
15154      for more information.  */
15155   enum insn_code icode;
15156 
15157   /* The floating-point comparison code to use with ICODE, if any.  */
15158   enum mips_fp_condition cond;
15159 
15160   /* The name of the built-in function.  */
15161   const char *name;
15162 
15163   /* Specifies how the function should be expanded.  */
15164   enum mips_builtin_type builtin_type;
15165 
15166   /* The function's prototype.  */
15167   enum mips_function_type function_type;
15168 
15169   /* Whether the function is available.  */
15170   unsigned int (*avail) (void);
15171 };
15172 
15173 AVAIL_ALL (hard_float, TARGET_HARD_FLOAT_ABI)
15174 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
15175 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
15176 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
15177 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
15178 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
15179 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
15180 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
15181 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
15182 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
15183 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
15184 AVAIL_NON_MIPS16 (msa, TARGET_MSA)
15185 
15186 /* Construct a mips_builtin_description from the given arguments.
15187 
15188    INSN is the name of the associated instruction pattern, without the
15189    leading CODE_FOR_mips_.
15190 
15191    CODE is the floating-point condition code associated with the
15192    function.  It can be 'f' if the field is not applicable.
15193 
15194    NAME is the name of the function itself, without the leading
15195    "__builtin_mips_".
15196 
15197    BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
15198 
15199    AVAIL is the name of the availability predicate, without the leading
15200    mips_builtin_avail_.  */
15201 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE,			\
15202 		     FUNCTION_TYPE, AVAIL)				\
15203   { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND,			\
15204     "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE,		\
15205     mips_builtin_avail_ ## AVAIL }
15206 
15207 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
15208    mapped to instruction CODE_FOR_mips_<INSN>,  FUNCTION_TYPE and AVAIL
15209    are as for MIPS_BUILTIN.  */
15210 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)			\
15211   MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
15212 
15213 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
15214    are subject to mips_builtin_avail_<AVAIL>.  */
15215 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL)				\
15216   MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s",		\
15217 		MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL),	\
15218   MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d",		\
15219 		MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
15220 
15221 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
15222    The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
15223    while the any and all forms are subject to mips_builtin_avail_mips3d.  */
15224 #define CMP_PS_BUILTINS(INSN, COND, AVAIL)				\
15225   MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps",	\
15226 		MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF,		\
15227 		mips3d),						\
15228   MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps",	\
15229 		MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF,		\
15230 		mips3d),						\
15231   MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps",	\
15232 		MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF,	\
15233 		AVAIL),							\
15234   MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps",	\
15235 		MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF,	\
15236 		AVAIL)
15237 
15238 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s.  The functions
15239    are subject to mips_builtin_avail_mips3d.  */
15240 #define CMP_4S_BUILTINS(INSN, COND)					\
15241   MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s",	\
15242 		MIPS_BUILTIN_CMP_ANY,					\
15243 		MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d),		\
15244   MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s",	\
15245 		MIPS_BUILTIN_CMP_ALL,					\
15246 		MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
15247 
15248 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps.  The comparison
15249    instruction requires mips_builtin_avail_<AVAIL>.  */
15250 #define MOVTF_BUILTINS(INSN, COND, AVAIL)				\
15251   MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps",	\
15252 		MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF,	\
15253 		AVAIL),							\
15254   MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps",	\
15255 		MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF,	\
15256 		AVAIL)
15257 
15258 /* Define all the built-in functions related to C.cond.fmt condition COND.  */
15259 #define CMP_BUILTINS(COND)						\
15260   MOVTF_BUILTINS (c, COND, paired_single),				\
15261   MOVTF_BUILTINS (cabs, COND, mips3d),					\
15262   CMP_SCALAR_BUILTINS (cabs, COND, mips3d),				\
15263   CMP_PS_BUILTINS (c, COND, paired_single),				\
15264   CMP_PS_BUILTINS (cabs, COND, mips3d),					\
15265   CMP_4S_BUILTINS (c, COND),						\
15266   CMP_4S_BUILTINS (cabs, COND)
15267 
15268 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
15269    function mapped to instruction CODE_FOR_mips_<INSN>,  FUNCTION_TYPE
15270    and AVAIL are as for MIPS_BUILTIN.  */
15271 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)		\
15272   MIPS_BUILTIN (INSN, f, #INSN,	MIPS_BUILTIN_DIRECT_NO_TARGET,		\
15273 		FUNCTION_TYPE, AVAIL)
15274 
15275 /* Define __builtin_mips_bposge<VALUE>.  <VALUE> is 32 for the MIPS32 DSP
15276    branch instruction.  AVAIL is as for MIPS_BUILTIN.  */
15277 #define BPOSGE_BUILTIN(VALUE, AVAIL)					\
15278   MIPS_BUILTIN (bposge, f, "bposge" #VALUE,				\
15279 		MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
15280 
15281 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
15282    for instruction CODE_FOR_loongson_<INSN>.  FUNCTION_TYPE is a
15283    builtin_description field.  */
15284 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE)		\
15285   { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f,				\
15286     "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT,		\
15287     FUNCTION_TYPE, mips_builtin_avail_loongson }
15288 
15289 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
15290    for instruction CODE_FOR_loongson_<INSN>.  FUNCTION_TYPE is a
15291    builtin_description field.  */
15292 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE)				\
15293   LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
15294 
15295 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
15296    We use functions of this form when the same insn can be usefully applied
15297    to more than one datatype.  */
15298 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE)		\
15299   LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
15300 
15301 /* Define an MSA MIPS_BUILTIN_DIRECT function __builtin_msa_<INSN>
15302    for instruction CODE_FOR_msa_<INSN>.  FUNCTION_TYPE is a builtin_description
15303    field.  */
15304 #define MSA_BUILTIN(INSN, FUNCTION_TYPE)				\
15305     { CODE_FOR_msa_ ## INSN, MIPS_FP_COND_f,				\
15306     "__builtin_msa_" #INSN,  MIPS_BUILTIN_DIRECT,			\
15307     FUNCTION_TYPE, mips_builtin_avail_msa }
15308 
15309 /* Define a remapped MSA MIPS_BUILTIN_DIRECT function __builtin_msa_<INSN>
15310    for instruction CODE_FOR_msa_<INSN2>.  FUNCTION_TYPE is
15311    a builtin_description field.  */
15312 #define MSA_BUILTIN_REMAP(INSN, INSN2, FUNCTION_TYPE)	\
15313     { CODE_FOR_msa_ ## INSN2, MIPS_FP_COND_f,				\
15314     "__builtin_msa_" #INSN,  MIPS_BUILTIN_DIRECT,			\
15315     FUNCTION_TYPE, mips_builtin_avail_msa }
15316 
15317 /* Define an MSA MIPS_BUILTIN_MSA_TEST_BRANCH function __builtin_msa_<INSN>
15318    for instruction CODE_FOR_msa_<INSN>.  FUNCTION_TYPE is a builtin_description
15319    field.  */
15320 #define MSA_BUILTIN_TEST_BRANCH(INSN, FUNCTION_TYPE)			\
15321     { CODE_FOR_msa_ ## INSN, MIPS_FP_COND_f,				\
15322     "__builtin_msa_" #INSN, MIPS_BUILTIN_MSA_TEST_BRANCH,		\
15323     FUNCTION_TYPE, mips_builtin_avail_msa }
15324 
15325 /* Define an MSA MIPS_BUILTIN_DIRECT_NO_TARGET function __builtin_msa_<INSN>
15326    for instruction CODE_FOR_msa_<INSN>.  FUNCTION_TYPE is a builtin_description
15327    field.  */
15328 #define MSA_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE)			\
15329     { CODE_FOR_msa_ ## INSN, MIPS_FP_COND_f,				\
15330     "__builtin_msa_" #INSN,  MIPS_BUILTIN_DIRECT_NO_TARGET,		\
15331     FUNCTION_TYPE, mips_builtin_avail_msa }
15332 
15333 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
15334 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
15335 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
15336 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
15337 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
15338 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
15339 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
15340 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
15341 
15342 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
15343 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
15344 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
15345 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
15346 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
15347 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
15348 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
15349 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
15350 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
15351 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
15352 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
15353 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
15354 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
15355 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
15356 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
15357 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
15358 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
15359 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
15360 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
15361 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
15362 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
15363 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
15364 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
15365 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
15366 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
15367 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
15368 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
15369 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
15370 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
15371 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
15372 
15373 #define CODE_FOR_msa_adds_s_b CODE_FOR_ssaddv16qi3
15374 #define CODE_FOR_msa_adds_s_h CODE_FOR_ssaddv8hi3
15375 #define CODE_FOR_msa_adds_s_w CODE_FOR_ssaddv4si3
15376 #define CODE_FOR_msa_adds_s_d CODE_FOR_ssaddv2di3
15377 #define CODE_FOR_msa_adds_u_b CODE_FOR_usaddv16qi3
15378 #define CODE_FOR_msa_adds_u_h CODE_FOR_usaddv8hi3
15379 #define CODE_FOR_msa_adds_u_w CODE_FOR_usaddv4si3
15380 #define CODE_FOR_msa_adds_u_d CODE_FOR_usaddv2di3
15381 #define CODE_FOR_msa_addv_b CODE_FOR_addv16qi3
15382 #define CODE_FOR_msa_addv_h CODE_FOR_addv8hi3
15383 #define CODE_FOR_msa_addv_w CODE_FOR_addv4si3
15384 #define CODE_FOR_msa_addv_d CODE_FOR_addv2di3
15385 #define CODE_FOR_msa_addvi_b CODE_FOR_addv16qi3
15386 #define CODE_FOR_msa_addvi_h CODE_FOR_addv8hi3
15387 #define CODE_FOR_msa_addvi_w CODE_FOR_addv4si3
15388 #define CODE_FOR_msa_addvi_d CODE_FOR_addv2di3
15389 #define CODE_FOR_msa_and_v CODE_FOR_andv16qi3
15390 #define CODE_FOR_msa_andi_b CODE_FOR_andv16qi3
15391 #define CODE_FOR_msa_bmnz_v CODE_FOR_msa_bmnz_b
15392 #define CODE_FOR_msa_bmnzi_b CODE_FOR_msa_bmnz_b
15393 #define CODE_FOR_msa_bmz_v CODE_FOR_msa_bmz_b
15394 #define CODE_FOR_msa_bmzi_b CODE_FOR_msa_bmz_b
15395 #define CODE_FOR_msa_bnz_v CODE_FOR_msa_bnz_v_b
15396 #define CODE_FOR_msa_bz_v CODE_FOR_msa_bz_v_b
15397 #define CODE_FOR_msa_bsel_v CODE_FOR_msa_bsel_b
15398 #define CODE_FOR_msa_bseli_b CODE_FOR_msa_bsel_b
15399 #define CODE_FOR_msa_ceqi_b CODE_FOR_msa_ceq_b
15400 #define CODE_FOR_msa_ceqi_h CODE_FOR_msa_ceq_h
15401 #define CODE_FOR_msa_ceqi_w CODE_FOR_msa_ceq_w
15402 #define CODE_FOR_msa_ceqi_d CODE_FOR_msa_ceq_d
15403 #define CODE_FOR_msa_clti_s_b CODE_FOR_msa_clt_s_b
15404 #define CODE_FOR_msa_clti_s_h CODE_FOR_msa_clt_s_h
15405 #define CODE_FOR_msa_clti_s_w CODE_FOR_msa_clt_s_w
15406 #define CODE_FOR_msa_clti_s_d CODE_FOR_msa_clt_s_d
15407 #define CODE_FOR_msa_clti_u_b CODE_FOR_msa_clt_u_b
15408 #define CODE_FOR_msa_clti_u_h CODE_FOR_msa_clt_u_h
15409 #define CODE_FOR_msa_clti_u_w CODE_FOR_msa_clt_u_w
15410 #define CODE_FOR_msa_clti_u_d CODE_FOR_msa_clt_u_d
15411 #define CODE_FOR_msa_clei_s_b CODE_FOR_msa_cle_s_b
15412 #define CODE_FOR_msa_clei_s_h CODE_FOR_msa_cle_s_h
15413 #define CODE_FOR_msa_clei_s_w CODE_FOR_msa_cle_s_w
15414 #define CODE_FOR_msa_clei_s_d CODE_FOR_msa_cle_s_d
15415 #define CODE_FOR_msa_clei_u_b CODE_FOR_msa_cle_u_b
15416 #define CODE_FOR_msa_clei_u_h CODE_FOR_msa_cle_u_h
15417 #define CODE_FOR_msa_clei_u_w CODE_FOR_msa_cle_u_w
15418 #define CODE_FOR_msa_clei_u_d CODE_FOR_msa_cle_u_d
15419 #define CODE_FOR_msa_div_s_b CODE_FOR_divv16qi3
15420 #define CODE_FOR_msa_div_s_h CODE_FOR_divv8hi3
15421 #define CODE_FOR_msa_div_s_w CODE_FOR_divv4si3
15422 #define CODE_FOR_msa_div_s_d CODE_FOR_divv2di3
15423 #define CODE_FOR_msa_div_u_b CODE_FOR_udivv16qi3
15424 #define CODE_FOR_msa_div_u_h CODE_FOR_udivv8hi3
15425 #define CODE_FOR_msa_div_u_w CODE_FOR_udivv4si3
15426 #define CODE_FOR_msa_div_u_d CODE_FOR_udivv2di3
15427 #define CODE_FOR_msa_fadd_w CODE_FOR_addv4sf3
15428 #define CODE_FOR_msa_fadd_d CODE_FOR_addv2df3
15429 #define CODE_FOR_msa_fexdo_w CODE_FOR_vec_pack_trunc_v2df
15430 #define CODE_FOR_msa_ftrunc_s_w CODE_FOR_fix_truncv4sfv4si2
15431 #define CODE_FOR_msa_ftrunc_s_d CODE_FOR_fix_truncv2dfv2di2
15432 #define CODE_FOR_msa_ftrunc_u_w CODE_FOR_fixuns_truncv4sfv4si2
15433 #define CODE_FOR_msa_ftrunc_u_d CODE_FOR_fixuns_truncv2dfv2di2
15434 #define CODE_FOR_msa_ffint_s_w CODE_FOR_floatv4siv4sf2
15435 #define CODE_FOR_msa_ffint_s_d CODE_FOR_floatv2div2df2
15436 #define CODE_FOR_msa_ffint_u_w CODE_FOR_floatunsv4siv4sf2
15437 #define CODE_FOR_msa_ffint_u_d CODE_FOR_floatunsv2div2df2
15438 #define CODE_FOR_msa_fsub_w CODE_FOR_subv4sf3
15439 #define CODE_FOR_msa_fsub_d CODE_FOR_subv2df3
15440 #define CODE_FOR_msa_fmadd_w CODE_FOR_fmav4sf4
15441 #define CODE_FOR_msa_fmadd_d CODE_FOR_fmav2df4
15442 #define CODE_FOR_msa_fmsub_w CODE_FOR_fnmav4sf4
15443 #define CODE_FOR_msa_fmsub_d CODE_FOR_fnmav2df4
15444 #define CODE_FOR_msa_fmul_w CODE_FOR_mulv4sf3
15445 #define CODE_FOR_msa_fmul_d CODE_FOR_mulv2df3
15446 #define CODE_FOR_msa_fdiv_w CODE_FOR_divv4sf3
15447 #define CODE_FOR_msa_fdiv_d CODE_FOR_divv2df3
15448 #define CODE_FOR_msa_fmax_w CODE_FOR_smaxv4sf3
15449 #define CODE_FOR_msa_fmax_d CODE_FOR_smaxv2df3
15450 #define CODE_FOR_msa_fmin_w CODE_FOR_sminv4sf3
15451 #define CODE_FOR_msa_fmin_d CODE_FOR_sminv2df3
15452 #define CODE_FOR_msa_fsqrt_w CODE_FOR_sqrtv4sf2
15453 #define CODE_FOR_msa_fsqrt_d CODE_FOR_sqrtv2df2
15454 #define CODE_FOR_msa_max_s_b CODE_FOR_smaxv16qi3
15455 #define CODE_FOR_msa_max_s_h CODE_FOR_smaxv8hi3
15456 #define CODE_FOR_msa_max_s_w CODE_FOR_smaxv4si3
15457 #define CODE_FOR_msa_max_s_d CODE_FOR_smaxv2di3
15458 #define CODE_FOR_msa_maxi_s_b CODE_FOR_smaxv16qi3
15459 #define CODE_FOR_msa_maxi_s_h CODE_FOR_smaxv8hi3
15460 #define CODE_FOR_msa_maxi_s_w CODE_FOR_smaxv4si3
15461 #define CODE_FOR_msa_maxi_s_d CODE_FOR_smaxv2di3
15462 #define CODE_FOR_msa_max_u_b CODE_FOR_umaxv16qi3
15463 #define CODE_FOR_msa_max_u_h CODE_FOR_umaxv8hi3
15464 #define CODE_FOR_msa_max_u_w CODE_FOR_umaxv4si3
15465 #define CODE_FOR_msa_max_u_d CODE_FOR_umaxv2di3
15466 #define CODE_FOR_msa_maxi_u_b CODE_FOR_umaxv16qi3
15467 #define CODE_FOR_msa_maxi_u_h CODE_FOR_umaxv8hi3
15468 #define CODE_FOR_msa_maxi_u_w CODE_FOR_umaxv4si3
15469 #define CODE_FOR_msa_maxi_u_d CODE_FOR_umaxv2di3
15470 #define CODE_FOR_msa_min_s_b CODE_FOR_sminv16qi3
15471 #define CODE_FOR_msa_min_s_h CODE_FOR_sminv8hi3
15472 #define CODE_FOR_msa_min_s_w CODE_FOR_sminv4si3
15473 #define CODE_FOR_msa_min_s_d CODE_FOR_sminv2di3
15474 #define CODE_FOR_msa_mini_s_b CODE_FOR_sminv16qi3
15475 #define CODE_FOR_msa_mini_s_h CODE_FOR_sminv8hi3
15476 #define CODE_FOR_msa_mini_s_w CODE_FOR_sminv4si3
15477 #define CODE_FOR_msa_mini_s_d CODE_FOR_sminv2di3
15478 #define CODE_FOR_msa_min_u_b CODE_FOR_uminv16qi3
15479 #define CODE_FOR_msa_min_u_h CODE_FOR_uminv8hi3
15480 #define CODE_FOR_msa_min_u_w CODE_FOR_uminv4si3
15481 #define CODE_FOR_msa_min_u_d CODE_FOR_uminv2di3
15482 #define CODE_FOR_msa_mini_u_b CODE_FOR_uminv16qi3
15483 #define CODE_FOR_msa_mini_u_h CODE_FOR_uminv8hi3
15484 #define CODE_FOR_msa_mini_u_w CODE_FOR_uminv4si3
15485 #define CODE_FOR_msa_mini_u_d CODE_FOR_uminv2di3
15486 #define CODE_FOR_msa_mod_s_b CODE_FOR_modv16qi3
15487 #define CODE_FOR_msa_mod_s_h CODE_FOR_modv8hi3
15488 #define CODE_FOR_msa_mod_s_w CODE_FOR_modv4si3
15489 #define CODE_FOR_msa_mod_s_d CODE_FOR_modv2di3
15490 #define CODE_FOR_msa_mod_u_b CODE_FOR_umodv16qi3
15491 #define CODE_FOR_msa_mod_u_h CODE_FOR_umodv8hi3
15492 #define CODE_FOR_msa_mod_u_w CODE_FOR_umodv4si3
15493 #define CODE_FOR_msa_mod_u_d CODE_FOR_umodv2di3
15494 #define CODE_FOR_msa_mod_s_b CODE_FOR_modv16qi3
15495 #define CODE_FOR_msa_mod_s_h CODE_FOR_modv8hi3
15496 #define CODE_FOR_msa_mod_s_w CODE_FOR_modv4si3
15497 #define CODE_FOR_msa_mod_s_d CODE_FOR_modv2di3
15498 #define CODE_FOR_msa_mod_u_b CODE_FOR_umodv16qi3
15499 #define CODE_FOR_msa_mod_u_h CODE_FOR_umodv8hi3
15500 #define CODE_FOR_msa_mod_u_w CODE_FOR_umodv4si3
15501 #define CODE_FOR_msa_mod_u_d CODE_FOR_umodv2di3
15502 #define CODE_FOR_msa_mulv_b CODE_FOR_mulv16qi3
15503 #define CODE_FOR_msa_mulv_h CODE_FOR_mulv8hi3
15504 #define CODE_FOR_msa_mulv_w CODE_FOR_mulv4si3
15505 #define CODE_FOR_msa_mulv_d CODE_FOR_mulv2di3
15506 #define CODE_FOR_msa_nlzc_b CODE_FOR_clzv16qi2
15507 #define CODE_FOR_msa_nlzc_h CODE_FOR_clzv8hi2
15508 #define CODE_FOR_msa_nlzc_w CODE_FOR_clzv4si2
15509 #define CODE_FOR_msa_nlzc_d CODE_FOR_clzv2di2
15510 #define CODE_FOR_msa_nor_v CODE_FOR_msa_nor_b
15511 #define CODE_FOR_msa_or_v CODE_FOR_iorv16qi3
15512 #define CODE_FOR_msa_ori_b CODE_FOR_iorv16qi3
15513 #define CODE_FOR_msa_nori_b CODE_FOR_msa_nor_b
15514 #define CODE_FOR_msa_pcnt_b CODE_FOR_popcountv16qi2
15515 #define CODE_FOR_msa_pcnt_h CODE_FOR_popcountv8hi2
15516 #define CODE_FOR_msa_pcnt_w CODE_FOR_popcountv4si2
15517 #define CODE_FOR_msa_pcnt_d CODE_FOR_popcountv2di2
15518 #define CODE_FOR_msa_xor_v CODE_FOR_xorv16qi3
15519 #define CODE_FOR_msa_xori_b CODE_FOR_xorv16qi3
15520 #define CODE_FOR_msa_sll_b CODE_FOR_vashlv16qi3
15521 #define CODE_FOR_msa_sll_h CODE_FOR_vashlv8hi3
15522 #define CODE_FOR_msa_sll_w CODE_FOR_vashlv4si3
15523 #define CODE_FOR_msa_sll_d CODE_FOR_vashlv2di3
15524 #define CODE_FOR_msa_slli_b CODE_FOR_vashlv16qi3
15525 #define CODE_FOR_msa_slli_h CODE_FOR_vashlv8hi3
15526 #define CODE_FOR_msa_slli_w CODE_FOR_vashlv4si3
15527 #define CODE_FOR_msa_slli_d CODE_FOR_vashlv2di3
15528 #define CODE_FOR_msa_sra_b CODE_FOR_vashrv16qi3
15529 #define CODE_FOR_msa_sra_h CODE_FOR_vashrv8hi3
15530 #define CODE_FOR_msa_sra_w CODE_FOR_vashrv4si3
15531 #define CODE_FOR_msa_sra_d CODE_FOR_vashrv2di3
15532 #define CODE_FOR_msa_srai_b CODE_FOR_vashrv16qi3
15533 #define CODE_FOR_msa_srai_h CODE_FOR_vashrv8hi3
15534 #define CODE_FOR_msa_srai_w CODE_FOR_vashrv4si3
15535 #define CODE_FOR_msa_srai_d CODE_FOR_vashrv2di3
15536 #define CODE_FOR_msa_srl_b CODE_FOR_vlshrv16qi3
15537 #define CODE_FOR_msa_srl_h CODE_FOR_vlshrv8hi3
15538 #define CODE_FOR_msa_srl_w CODE_FOR_vlshrv4si3
15539 #define CODE_FOR_msa_srl_d CODE_FOR_vlshrv2di3
15540 #define CODE_FOR_msa_srli_b CODE_FOR_vlshrv16qi3
15541 #define CODE_FOR_msa_srli_h CODE_FOR_vlshrv8hi3
15542 #define CODE_FOR_msa_srli_w CODE_FOR_vlshrv4si3
15543 #define CODE_FOR_msa_srli_d CODE_FOR_vlshrv2di3
15544 #define CODE_FOR_msa_subv_b CODE_FOR_subv16qi3
15545 #define CODE_FOR_msa_subv_h CODE_FOR_subv8hi3
15546 #define CODE_FOR_msa_subv_w CODE_FOR_subv4si3
15547 #define CODE_FOR_msa_subv_d CODE_FOR_subv2di3
15548 #define CODE_FOR_msa_subvi_b CODE_FOR_subv16qi3
15549 #define CODE_FOR_msa_subvi_h CODE_FOR_subv8hi3
15550 #define CODE_FOR_msa_subvi_w CODE_FOR_subv4si3
15551 #define CODE_FOR_msa_subvi_d CODE_FOR_subv2di3
15552 
15553 #define CODE_FOR_msa_move_v CODE_FOR_movv16qi
15554 
15555 #define CODE_FOR_msa_vshf_b CODE_FOR_vec_permv16qi
15556 #define CODE_FOR_msa_vshf_h CODE_FOR_vec_permv8hi
15557 #define CODE_FOR_msa_vshf_w CODE_FOR_vec_permv4si
15558 #define CODE_FOR_msa_vshf_d CODE_FOR_vec_permv2di
15559 
15560 #define CODE_FOR_msa_ilvod_d CODE_FOR_msa_ilvl_d
15561 #define CODE_FOR_msa_ilvev_d CODE_FOR_msa_ilvr_d
15562 #define CODE_FOR_msa_pckod_d CODE_FOR_msa_ilvl_d
15563 #define CODE_FOR_msa_pckev_d CODE_FOR_msa_ilvr_d
15564 
15565 #define CODE_FOR_msa_ldi_b CODE_FOR_msa_ldiv16qi
15566 #define CODE_FOR_msa_ldi_h CODE_FOR_msa_ldiv8hi
15567 #define CODE_FOR_msa_ldi_w CODE_FOR_msa_ldiv4si
15568 #define CODE_FOR_msa_ldi_d CODE_FOR_msa_ldiv2di
15569 
15570 static const struct mips_builtin_description mips_builtins[] = {
15571 #define MIPS_GET_FCSR 0
15572   DIRECT_BUILTIN (get_fcsr, MIPS_USI_FTYPE_VOID, hard_float),
15573 #define MIPS_SET_FCSR 1
15574   DIRECT_NO_TARGET_BUILTIN (set_fcsr, MIPS_VOID_FTYPE_USI, hard_float),
15575 
15576   DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
15577   DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
15578   DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
15579   DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
15580   DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
15581   DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
15582   DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
15583   DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
15584 
15585   DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
15586   DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
15587   DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
15588   DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
15589   DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
15590 
15591   DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
15592   DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
15593   DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
15594   DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
15595   DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
15596   DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
15597 
15598   DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
15599   DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
15600   DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
15601   DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
15602   DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
15603   DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
15604 
15605   MIPS_FP_CONDITIONS (CMP_BUILTINS),
15606 
15607   /* Built-in functions for the SB-1 processor.  */
15608   DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
15609 
15610   /* Built-in functions for the DSP ASE (32-bit and 64-bit).  */
15611   DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
15612   DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
15613   DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
15614   DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
15615   DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
15616   DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
15617   DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
15618   DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
15619   DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
15620   DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
15621   DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
15622   DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
15623   DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
15624   DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
15625   DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
15626   DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
15627   DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
15628   DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
15629   DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
15630   DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
15631   DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
15632   DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
15633   DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
15634   DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
15635   DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
15636   DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
15637   DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
15638   DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
15639   DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
15640   DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
15641   DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
15642   DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
15643   DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
15644   DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
15645   DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
15646   DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
15647   DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
15648   DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
15649   DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
15650   DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
15651   DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
15652   DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
15653   DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
15654   DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
15655   DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
15656   DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
15657   DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
15658   DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
15659   DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
15660   DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
15661   DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
15662   DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
15663   DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
15664   DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
15665   DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
15666   DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
15667   DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
15668   DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
15669   DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
15670   DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
15671   DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
15672   DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
15673   DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
15674   DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
15675   BPOSGE_BUILTIN (32, dsp),
15676 
15677   /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit).  */
15678   DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
15679   DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15680   DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15681   DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
15682   DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
15683   DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
15684   DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
15685   DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
15686   DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
15687   DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
15688   DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15689   DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15690   DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
15691   DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15692   DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
15693   DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
15694   DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
15695   DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
15696   DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
15697   DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
15698   DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
15699   DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
15700   DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15701   DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15702   DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
15703   DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
15704   DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15705   DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15706   DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
15707   DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
15708   DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15709   DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
15710   DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
15711   DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
15712 
15713   /* Built-in functions for the DSP ASE (32-bit only).  */
15714   DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
15715   DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
15716   DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
15717   DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
15718   DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
15719   DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
15720   DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
15721   DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
15722   DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
15723   DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
15724   DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
15725   DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
15726   DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
15727   DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
15728   DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
15729   DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
15730   DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
15731   DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
15732   DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
15733   DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
15734   DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
15735   DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
15736   DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
15737   DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
15738   DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
15739   DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
15740   DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
15741 
15742   /* Built-in functions for the DSP ASE (64-bit only).  */
15743   DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
15744 
15745   /* The following are for the MIPS DSP ASE REV 2 (32-bit only).  */
15746   DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15747   DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15748   DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15749   DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15750   DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15751   DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15752   DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15753   DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15754   DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
15755 
15756   /* Builtin functions for ST Microelectronics Loongson-2E/2F cores.  */
15757   LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
15758   LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
15759   LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
15760   LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
15761   LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15762   LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15763   LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
15764   LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15765   LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
15766   LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
15767   LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
15768   LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
15769   LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
15770   LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15771   LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15772   LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
15773   LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
15774   LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15775   LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15776   LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
15777   LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
15778   LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
15779   LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
15780   LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15781   LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15782   LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
15783   LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15784   LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15785   LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
15786   LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15787   LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
15788   LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
15789   LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15790   LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15791   LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
15792   LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15793   LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
15794   LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
15795   LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
15796   LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15797   LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15798   LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15799   LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15800   LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15801   LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15802   LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15803   LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15804   LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
15805   LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
15806   LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15807   LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
15808   LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15809   LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
15810   LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
15811   LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15812   LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
15813   LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
15814   LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
15815   LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15816   LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
15817   LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
15818   LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
15819   LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
15820   LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
15821   LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
15822   LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
15823   LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
15824   LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
15825   LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
15826   LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
15827   LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
15828   LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
15829   LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
15830   LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
15831   LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
15832   LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
15833   LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15834   LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15835   LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
15836   LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15837   LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
15838   LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
15839   LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
15840   LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
15841   LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
15842   LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15843   LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15844   LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15845   LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15846   LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
15847   LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
15848   LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15849   LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
15850   LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
15851   LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
15852   LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
15853   LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
15854   LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
15855   LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
15856 
15857   /* Sundry other built-in functions.  */
15858   DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache),
15859 
15860   /* Built-in functions for MSA.  */
15861   MSA_BUILTIN (sll_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15862   MSA_BUILTIN (sll_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15863   MSA_BUILTIN (sll_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15864   MSA_BUILTIN (sll_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15865   MSA_BUILTIN (slli_b, MIPS_V16QI_FTYPE_V16QI_UQI),
15866   MSA_BUILTIN (slli_h, MIPS_V8HI_FTYPE_V8HI_UQI),
15867   MSA_BUILTIN (slli_w, MIPS_V4SI_FTYPE_V4SI_UQI),
15868   MSA_BUILTIN (slli_d, MIPS_V2DI_FTYPE_V2DI_UQI),
15869   MSA_BUILTIN (sra_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15870   MSA_BUILTIN (sra_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15871   MSA_BUILTIN (sra_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15872   MSA_BUILTIN (sra_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15873   MSA_BUILTIN (srai_b, MIPS_V16QI_FTYPE_V16QI_UQI),
15874   MSA_BUILTIN (srai_h, MIPS_V8HI_FTYPE_V8HI_UQI),
15875   MSA_BUILTIN (srai_w, MIPS_V4SI_FTYPE_V4SI_UQI),
15876   MSA_BUILTIN (srai_d, MIPS_V2DI_FTYPE_V2DI_UQI),
15877   MSA_BUILTIN (srar_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15878   MSA_BUILTIN (srar_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15879   MSA_BUILTIN (srar_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15880   MSA_BUILTIN (srar_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15881   MSA_BUILTIN (srari_b, MIPS_V16QI_FTYPE_V16QI_UQI),
15882   MSA_BUILTIN (srari_h, MIPS_V8HI_FTYPE_V8HI_UQI),
15883   MSA_BUILTIN (srari_w, MIPS_V4SI_FTYPE_V4SI_UQI),
15884   MSA_BUILTIN (srari_d, MIPS_V2DI_FTYPE_V2DI_UQI),
15885   MSA_BUILTIN (srl_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15886   MSA_BUILTIN (srl_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15887   MSA_BUILTIN (srl_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15888   MSA_BUILTIN (srl_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15889   MSA_BUILTIN (srli_b, MIPS_V16QI_FTYPE_V16QI_UQI),
15890   MSA_BUILTIN (srli_h, MIPS_V8HI_FTYPE_V8HI_UQI),
15891   MSA_BUILTIN (srli_w, MIPS_V4SI_FTYPE_V4SI_UQI),
15892   MSA_BUILTIN (srli_d, MIPS_V2DI_FTYPE_V2DI_UQI),
15893   MSA_BUILTIN (srlr_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15894   MSA_BUILTIN (srlr_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15895   MSA_BUILTIN (srlr_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15896   MSA_BUILTIN (srlr_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15897   MSA_BUILTIN (srlri_b, MIPS_V16QI_FTYPE_V16QI_UQI),
15898   MSA_BUILTIN (srlri_h, MIPS_V8HI_FTYPE_V8HI_UQI),
15899   MSA_BUILTIN (srlri_w, MIPS_V4SI_FTYPE_V4SI_UQI),
15900   MSA_BUILTIN (srlri_d, MIPS_V2DI_FTYPE_V2DI_UQI),
15901   MSA_BUILTIN (bclr_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
15902   MSA_BUILTIN (bclr_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
15903   MSA_BUILTIN (bclr_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
15904   MSA_BUILTIN (bclr_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
15905   MSA_BUILTIN (bclri_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
15906   MSA_BUILTIN (bclri_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
15907   MSA_BUILTIN (bclri_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
15908   MSA_BUILTIN (bclri_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
15909   MSA_BUILTIN (bset_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
15910   MSA_BUILTIN (bset_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
15911   MSA_BUILTIN (bset_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
15912   MSA_BUILTIN (bset_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
15913   MSA_BUILTIN (bseti_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
15914   MSA_BUILTIN (bseti_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
15915   MSA_BUILTIN (bseti_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
15916   MSA_BUILTIN (bseti_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
15917   MSA_BUILTIN (bneg_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
15918   MSA_BUILTIN (bneg_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
15919   MSA_BUILTIN (bneg_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
15920   MSA_BUILTIN (bneg_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
15921   MSA_BUILTIN (bnegi_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
15922   MSA_BUILTIN (bnegi_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
15923   MSA_BUILTIN (bnegi_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
15924   MSA_BUILTIN (bnegi_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
15925   MSA_BUILTIN (binsl_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
15926   MSA_BUILTIN (binsl_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI_UV8HI),
15927   MSA_BUILTIN (binsl_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI_UV4SI),
15928   MSA_BUILTIN (binsl_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI_UV2DI),
15929   MSA_BUILTIN (binsli_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
15930   MSA_BUILTIN (binsli_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI_UQI),
15931   MSA_BUILTIN (binsli_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI_UQI),
15932   MSA_BUILTIN (binsli_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI_UQI),
15933   MSA_BUILTIN (binsr_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
15934   MSA_BUILTIN (binsr_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI_UV8HI),
15935   MSA_BUILTIN (binsr_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI_UV4SI),
15936   MSA_BUILTIN (binsr_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI_UV2DI),
15937   MSA_BUILTIN (binsri_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
15938   MSA_BUILTIN (binsri_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI_UQI),
15939   MSA_BUILTIN (binsri_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI_UQI),
15940   MSA_BUILTIN (binsri_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI_UQI),
15941   MSA_BUILTIN (addv_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15942   MSA_BUILTIN (addv_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15943   MSA_BUILTIN (addv_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15944   MSA_BUILTIN (addv_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15945   MSA_BUILTIN (addvi_b, MIPS_V16QI_FTYPE_V16QI_UQI),
15946   MSA_BUILTIN (addvi_h, MIPS_V8HI_FTYPE_V8HI_UQI),
15947   MSA_BUILTIN (addvi_w, MIPS_V4SI_FTYPE_V4SI_UQI),
15948   MSA_BUILTIN (addvi_d, MIPS_V2DI_FTYPE_V2DI_UQI),
15949   MSA_BUILTIN (subv_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15950   MSA_BUILTIN (subv_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15951   MSA_BUILTIN (subv_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15952   MSA_BUILTIN (subv_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15953   MSA_BUILTIN (subvi_b, MIPS_V16QI_FTYPE_V16QI_UQI),
15954   MSA_BUILTIN (subvi_h, MIPS_V8HI_FTYPE_V8HI_UQI),
15955   MSA_BUILTIN (subvi_w, MIPS_V4SI_FTYPE_V4SI_UQI),
15956   MSA_BUILTIN (subvi_d, MIPS_V2DI_FTYPE_V2DI_UQI),
15957   MSA_BUILTIN (max_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15958   MSA_BUILTIN (max_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15959   MSA_BUILTIN (max_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15960   MSA_BUILTIN (max_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15961   MSA_BUILTIN (maxi_s_b, MIPS_V16QI_FTYPE_V16QI_QI),
15962   MSA_BUILTIN (maxi_s_h, MIPS_V8HI_FTYPE_V8HI_QI),
15963   MSA_BUILTIN (maxi_s_w, MIPS_V4SI_FTYPE_V4SI_QI),
15964   MSA_BUILTIN (maxi_s_d, MIPS_V2DI_FTYPE_V2DI_QI),
15965   MSA_BUILTIN (max_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
15966   MSA_BUILTIN (max_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
15967   MSA_BUILTIN (max_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
15968   MSA_BUILTIN (max_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
15969   MSA_BUILTIN (maxi_u_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
15970   MSA_BUILTIN (maxi_u_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
15971   MSA_BUILTIN (maxi_u_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
15972   MSA_BUILTIN (maxi_u_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
15973   MSA_BUILTIN (min_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15974   MSA_BUILTIN (min_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15975   MSA_BUILTIN (min_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15976   MSA_BUILTIN (min_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15977   MSA_BUILTIN (mini_s_b, MIPS_V16QI_FTYPE_V16QI_QI),
15978   MSA_BUILTIN (mini_s_h, MIPS_V8HI_FTYPE_V8HI_QI),
15979   MSA_BUILTIN (mini_s_w, MIPS_V4SI_FTYPE_V4SI_QI),
15980   MSA_BUILTIN (mini_s_d, MIPS_V2DI_FTYPE_V2DI_QI),
15981   MSA_BUILTIN (min_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
15982   MSA_BUILTIN (min_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
15983   MSA_BUILTIN (min_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
15984   MSA_BUILTIN (min_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
15985   MSA_BUILTIN (mini_u_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
15986   MSA_BUILTIN (mini_u_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
15987   MSA_BUILTIN (mini_u_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
15988   MSA_BUILTIN (mini_u_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
15989   MSA_BUILTIN (max_a_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15990   MSA_BUILTIN (max_a_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15991   MSA_BUILTIN (max_a_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15992   MSA_BUILTIN (max_a_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15993   MSA_BUILTIN (min_a_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15994   MSA_BUILTIN (min_a_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15995   MSA_BUILTIN (min_a_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
15996   MSA_BUILTIN (min_a_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
15997   MSA_BUILTIN (ceq_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
15998   MSA_BUILTIN (ceq_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
15999   MSA_BUILTIN (ceq_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16000   MSA_BUILTIN (ceq_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16001   MSA_BUILTIN (ceqi_b, MIPS_V16QI_FTYPE_V16QI_QI),
16002   MSA_BUILTIN (ceqi_h, MIPS_V8HI_FTYPE_V8HI_QI),
16003   MSA_BUILTIN (ceqi_w, MIPS_V4SI_FTYPE_V4SI_QI),
16004   MSA_BUILTIN (ceqi_d, MIPS_V2DI_FTYPE_V2DI_QI),
16005   MSA_BUILTIN (clt_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16006   MSA_BUILTIN (clt_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16007   MSA_BUILTIN (clt_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16008   MSA_BUILTIN (clt_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16009   MSA_BUILTIN (clti_s_b, MIPS_V16QI_FTYPE_V16QI_QI),
16010   MSA_BUILTIN (clti_s_h, MIPS_V8HI_FTYPE_V8HI_QI),
16011   MSA_BUILTIN (clti_s_w, MIPS_V4SI_FTYPE_V4SI_QI),
16012   MSA_BUILTIN (clti_s_d, MIPS_V2DI_FTYPE_V2DI_QI),
16013   MSA_BUILTIN (clt_u_b, MIPS_V16QI_FTYPE_UV16QI_UV16QI),
16014   MSA_BUILTIN (clt_u_h, MIPS_V8HI_FTYPE_UV8HI_UV8HI),
16015   MSA_BUILTIN (clt_u_w, MIPS_V4SI_FTYPE_UV4SI_UV4SI),
16016   MSA_BUILTIN (clt_u_d, MIPS_V2DI_FTYPE_UV2DI_UV2DI),
16017   MSA_BUILTIN (clti_u_b, MIPS_V16QI_FTYPE_UV16QI_UQI),
16018   MSA_BUILTIN (clti_u_h, MIPS_V8HI_FTYPE_UV8HI_UQI),
16019   MSA_BUILTIN (clti_u_w, MIPS_V4SI_FTYPE_UV4SI_UQI),
16020   MSA_BUILTIN (clti_u_d, MIPS_V2DI_FTYPE_UV2DI_UQI),
16021   MSA_BUILTIN (cle_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16022   MSA_BUILTIN (cle_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16023   MSA_BUILTIN (cle_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16024   MSA_BUILTIN (cle_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16025   MSA_BUILTIN (clei_s_b, MIPS_V16QI_FTYPE_V16QI_QI),
16026   MSA_BUILTIN (clei_s_h, MIPS_V8HI_FTYPE_V8HI_QI),
16027   MSA_BUILTIN (clei_s_w, MIPS_V4SI_FTYPE_V4SI_QI),
16028   MSA_BUILTIN (clei_s_d, MIPS_V2DI_FTYPE_V2DI_QI),
16029   MSA_BUILTIN (cle_u_b, MIPS_V16QI_FTYPE_UV16QI_UV16QI),
16030   MSA_BUILTIN (cle_u_h, MIPS_V8HI_FTYPE_UV8HI_UV8HI),
16031   MSA_BUILTIN (cle_u_w, MIPS_V4SI_FTYPE_UV4SI_UV4SI),
16032   MSA_BUILTIN (cle_u_d, MIPS_V2DI_FTYPE_UV2DI_UV2DI),
16033   MSA_BUILTIN (clei_u_b, MIPS_V16QI_FTYPE_UV16QI_UQI),
16034   MSA_BUILTIN (clei_u_h, MIPS_V8HI_FTYPE_UV8HI_UQI),
16035   MSA_BUILTIN (clei_u_w, MIPS_V4SI_FTYPE_UV4SI_UQI),
16036   MSA_BUILTIN (clei_u_d, MIPS_V2DI_FTYPE_UV2DI_UQI),
16037   MSA_BUILTIN (ld_b, MIPS_V16QI_FTYPE_CVPOINTER_SI),
16038   MSA_BUILTIN (ld_h, MIPS_V8HI_FTYPE_CVPOINTER_SI),
16039   MSA_BUILTIN (ld_w, MIPS_V4SI_FTYPE_CVPOINTER_SI),
16040   MSA_BUILTIN (ld_d, MIPS_V2DI_FTYPE_CVPOINTER_SI),
16041   MSA_NO_TARGET_BUILTIN (st_b, MIPS_VOID_FTYPE_V16QI_CVPOINTER_SI),
16042   MSA_NO_TARGET_BUILTIN (st_h, MIPS_VOID_FTYPE_V8HI_CVPOINTER_SI),
16043   MSA_NO_TARGET_BUILTIN (st_w, MIPS_VOID_FTYPE_V4SI_CVPOINTER_SI),
16044   MSA_NO_TARGET_BUILTIN (st_d, MIPS_VOID_FTYPE_V2DI_CVPOINTER_SI),
16045   MSA_BUILTIN (sat_s_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16046   MSA_BUILTIN (sat_s_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16047   MSA_BUILTIN (sat_s_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16048   MSA_BUILTIN (sat_s_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16049   MSA_BUILTIN (sat_u_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16050   MSA_BUILTIN (sat_u_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
16051   MSA_BUILTIN (sat_u_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
16052   MSA_BUILTIN (sat_u_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
16053   MSA_BUILTIN (add_a_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16054   MSA_BUILTIN (add_a_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16055   MSA_BUILTIN (add_a_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16056   MSA_BUILTIN (add_a_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16057   MSA_BUILTIN (adds_a_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16058   MSA_BUILTIN (adds_a_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16059   MSA_BUILTIN (adds_a_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16060   MSA_BUILTIN (adds_a_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16061   MSA_BUILTIN (adds_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16062   MSA_BUILTIN (adds_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16063   MSA_BUILTIN (adds_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16064   MSA_BUILTIN (adds_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16065   MSA_BUILTIN (adds_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16066   MSA_BUILTIN (adds_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16067   MSA_BUILTIN (adds_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16068   MSA_BUILTIN (adds_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16069   MSA_BUILTIN (ave_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16070   MSA_BUILTIN (ave_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16071   MSA_BUILTIN (ave_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16072   MSA_BUILTIN (ave_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16073   MSA_BUILTIN (ave_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16074   MSA_BUILTIN (ave_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16075   MSA_BUILTIN (ave_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16076   MSA_BUILTIN (ave_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16077   MSA_BUILTIN (aver_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16078   MSA_BUILTIN (aver_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16079   MSA_BUILTIN (aver_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16080   MSA_BUILTIN (aver_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16081   MSA_BUILTIN (aver_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16082   MSA_BUILTIN (aver_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16083   MSA_BUILTIN (aver_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16084   MSA_BUILTIN (aver_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16085   MSA_BUILTIN (subs_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16086   MSA_BUILTIN (subs_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16087   MSA_BUILTIN (subs_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16088   MSA_BUILTIN (subs_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16089   MSA_BUILTIN (subs_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16090   MSA_BUILTIN (subs_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16091   MSA_BUILTIN (subs_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16092   MSA_BUILTIN (subs_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16093   MSA_BUILTIN (subsuu_s_b, MIPS_V16QI_FTYPE_UV16QI_UV16QI),
16094   MSA_BUILTIN (subsuu_s_h, MIPS_V8HI_FTYPE_UV8HI_UV8HI),
16095   MSA_BUILTIN (subsuu_s_w, MIPS_V4SI_FTYPE_UV4SI_UV4SI),
16096   MSA_BUILTIN (subsuu_s_d, MIPS_V2DI_FTYPE_UV2DI_UV2DI),
16097   MSA_BUILTIN (subsus_u_b, MIPS_UV16QI_FTYPE_UV16QI_V16QI),
16098   MSA_BUILTIN (subsus_u_h, MIPS_UV8HI_FTYPE_UV8HI_V8HI),
16099   MSA_BUILTIN (subsus_u_w, MIPS_UV4SI_FTYPE_UV4SI_V4SI),
16100   MSA_BUILTIN (subsus_u_d, MIPS_UV2DI_FTYPE_UV2DI_V2DI),
16101   MSA_BUILTIN (asub_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16102   MSA_BUILTIN (asub_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16103   MSA_BUILTIN (asub_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16104   MSA_BUILTIN (asub_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16105   MSA_BUILTIN (asub_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16106   MSA_BUILTIN (asub_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16107   MSA_BUILTIN (asub_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16108   MSA_BUILTIN (asub_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16109   MSA_BUILTIN (mulv_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16110   MSA_BUILTIN (mulv_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16111   MSA_BUILTIN (mulv_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16112   MSA_BUILTIN (mulv_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16113   MSA_BUILTIN (maddv_b, MIPS_V16QI_FTYPE_V16QI_V16QI_V16QI),
16114   MSA_BUILTIN (maddv_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16115   MSA_BUILTIN (maddv_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16116   MSA_BUILTIN (maddv_d, MIPS_V2DI_FTYPE_V2DI_V2DI_V2DI),
16117   MSA_BUILTIN (msubv_b, MIPS_V16QI_FTYPE_V16QI_V16QI_V16QI),
16118   MSA_BUILTIN (msubv_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16119   MSA_BUILTIN (msubv_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16120   MSA_BUILTIN (msubv_d, MIPS_V2DI_FTYPE_V2DI_V2DI_V2DI),
16121   MSA_BUILTIN (div_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16122   MSA_BUILTIN (div_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16123   MSA_BUILTIN (div_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16124   MSA_BUILTIN (div_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16125   MSA_BUILTIN (div_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16126   MSA_BUILTIN (div_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16127   MSA_BUILTIN (div_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16128   MSA_BUILTIN (div_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16129   MSA_BUILTIN (hadd_s_h, MIPS_V8HI_FTYPE_V16QI_V16QI),
16130   MSA_BUILTIN (hadd_s_w, MIPS_V4SI_FTYPE_V8HI_V8HI),
16131   MSA_BUILTIN (hadd_s_d, MIPS_V2DI_FTYPE_V4SI_V4SI),
16132   MSA_BUILTIN (hadd_u_h, MIPS_UV8HI_FTYPE_UV16QI_UV16QI),
16133   MSA_BUILTIN (hadd_u_w, MIPS_UV4SI_FTYPE_UV8HI_UV8HI),
16134   MSA_BUILTIN (hadd_u_d, MIPS_UV2DI_FTYPE_UV4SI_UV4SI),
16135   MSA_BUILTIN (hsub_s_h, MIPS_V8HI_FTYPE_V16QI_V16QI),
16136   MSA_BUILTIN (hsub_s_w, MIPS_V4SI_FTYPE_V8HI_V8HI),
16137   MSA_BUILTIN (hsub_s_d, MIPS_V2DI_FTYPE_V4SI_V4SI),
16138   MSA_BUILTIN (hsub_u_h, MIPS_V8HI_FTYPE_UV16QI_UV16QI),
16139   MSA_BUILTIN (hsub_u_w, MIPS_V4SI_FTYPE_UV8HI_UV8HI),
16140   MSA_BUILTIN (hsub_u_d, MIPS_V2DI_FTYPE_UV4SI_UV4SI),
16141   MSA_BUILTIN (mod_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16142   MSA_BUILTIN (mod_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16143   MSA_BUILTIN (mod_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16144   MSA_BUILTIN (mod_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16145   MSA_BUILTIN (mod_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16146   MSA_BUILTIN (mod_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16147   MSA_BUILTIN (mod_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16148   MSA_BUILTIN (mod_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16149   MSA_BUILTIN (dotp_s_h, MIPS_V8HI_FTYPE_V16QI_V16QI),
16150   MSA_BUILTIN (dotp_s_w, MIPS_V4SI_FTYPE_V8HI_V8HI),
16151   MSA_BUILTIN (dotp_s_d, MIPS_V2DI_FTYPE_V4SI_V4SI),
16152   MSA_BUILTIN (dotp_u_h, MIPS_UV8HI_FTYPE_UV16QI_UV16QI),
16153   MSA_BUILTIN (dotp_u_w, MIPS_UV4SI_FTYPE_UV8HI_UV8HI),
16154   MSA_BUILTIN (dotp_u_d, MIPS_UV2DI_FTYPE_UV4SI_UV4SI),
16155   MSA_BUILTIN (dpadd_s_h, MIPS_V8HI_FTYPE_V8HI_V16QI_V16QI),
16156   MSA_BUILTIN (dpadd_s_w, MIPS_V4SI_FTYPE_V4SI_V8HI_V8HI),
16157   MSA_BUILTIN (dpadd_s_d, MIPS_V2DI_FTYPE_V2DI_V4SI_V4SI),
16158   MSA_BUILTIN (dpadd_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV16QI_UV16QI),
16159   MSA_BUILTIN (dpadd_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV8HI_UV8HI),
16160   MSA_BUILTIN (dpadd_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV4SI_UV4SI),
16161   MSA_BUILTIN (dpsub_s_h, MIPS_V8HI_FTYPE_V8HI_V16QI_V16QI),
16162   MSA_BUILTIN (dpsub_s_w, MIPS_V4SI_FTYPE_V4SI_V8HI_V8HI),
16163   MSA_BUILTIN (dpsub_s_d, MIPS_V2DI_FTYPE_V2DI_V4SI_V4SI),
16164   MSA_BUILTIN (dpsub_u_h, MIPS_V8HI_FTYPE_V8HI_UV16QI_UV16QI),
16165   MSA_BUILTIN (dpsub_u_w, MIPS_V4SI_FTYPE_V4SI_UV8HI_UV8HI),
16166   MSA_BUILTIN (dpsub_u_d, MIPS_V2DI_FTYPE_V2DI_UV4SI_UV4SI),
16167   MSA_BUILTIN (sld_b, MIPS_V16QI_FTYPE_V16QI_V16QI_SI),
16168   MSA_BUILTIN (sld_h, MIPS_V8HI_FTYPE_V8HI_V8HI_SI),
16169   MSA_BUILTIN (sld_w, MIPS_V4SI_FTYPE_V4SI_V4SI_SI),
16170   MSA_BUILTIN (sld_d, MIPS_V2DI_FTYPE_V2DI_V2DI_SI),
16171   MSA_BUILTIN (sldi_b, MIPS_V16QI_FTYPE_V16QI_V16QI_UQI),
16172   MSA_BUILTIN (sldi_h, MIPS_V8HI_FTYPE_V8HI_V8HI_UQI),
16173   MSA_BUILTIN (sldi_w, MIPS_V4SI_FTYPE_V4SI_V4SI_UQI),
16174   MSA_BUILTIN (sldi_d, MIPS_V2DI_FTYPE_V2DI_V2DI_UQI),
16175   MSA_BUILTIN (splat_b, MIPS_V16QI_FTYPE_V16QI_SI),
16176   MSA_BUILTIN (splat_h, MIPS_V8HI_FTYPE_V8HI_SI),
16177   MSA_BUILTIN (splat_w, MIPS_V4SI_FTYPE_V4SI_SI),
16178   MSA_BUILTIN (splat_d, MIPS_V2DI_FTYPE_V2DI_SI),
16179   MSA_BUILTIN (splati_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16180   MSA_BUILTIN (splati_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16181   MSA_BUILTIN (splati_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16182   MSA_BUILTIN (splati_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16183   MSA_BUILTIN (pckev_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16184   MSA_BUILTIN (pckev_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16185   MSA_BUILTIN (pckev_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16186   MSA_BUILTIN (pckev_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16187   MSA_BUILTIN (pckod_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16188   MSA_BUILTIN (pckod_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16189   MSA_BUILTIN (pckod_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16190   MSA_BUILTIN (pckod_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16191   MSA_BUILTIN (ilvl_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16192   MSA_BUILTIN (ilvl_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16193   MSA_BUILTIN (ilvl_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16194   MSA_BUILTIN (ilvl_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16195   MSA_BUILTIN (ilvr_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16196   MSA_BUILTIN (ilvr_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16197   MSA_BUILTIN (ilvr_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16198   MSA_BUILTIN (ilvr_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16199   MSA_BUILTIN (ilvev_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16200   MSA_BUILTIN (ilvev_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16201   MSA_BUILTIN (ilvev_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16202   MSA_BUILTIN (ilvev_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16203   MSA_BUILTIN (ilvod_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16204   MSA_BUILTIN (ilvod_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16205   MSA_BUILTIN (ilvod_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16206   MSA_BUILTIN (ilvod_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16207   MSA_BUILTIN (vshf_b, MIPS_V16QI_FTYPE_V16QI_V16QI_V16QI),
16208   MSA_BUILTIN (vshf_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16209   MSA_BUILTIN (vshf_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16210   MSA_BUILTIN (vshf_d, MIPS_V2DI_FTYPE_V2DI_V2DI_V2DI),
16211   MSA_BUILTIN (and_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16212   MSA_BUILTIN (andi_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16213   MSA_BUILTIN (or_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16214   MSA_BUILTIN (ori_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16215   MSA_BUILTIN (nor_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16216   MSA_BUILTIN (nori_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16217   MSA_BUILTIN (xor_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16218   MSA_BUILTIN (xori_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16219   MSA_BUILTIN (bmnz_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
16220   MSA_BUILTIN (bmnzi_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
16221   MSA_BUILTIN (bmz_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
16222   MSA_BUILTIN (bmzi_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
16223   MSA_BUILTIN (bsel_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
16224   MSA_BUILTIN (bseli_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
16225   MSA_BUILTIN (shf_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16226   MSA_BUILTIN (shf_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16227   MSA_BUILTIN (shf_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16228   MSA_BUILTIN_TEST_BRANCH (bnz_v, MIPS_SI_FTYPE_UV16QI),
16229   MSA_BUILTIN_TEST_BRANCH (bz_v, MIPS_SI_FTYPE_UV16QI),
16230   MSA_BUILTIN (fill_b, MIPS_V16QI_FTYPE_SI),
16231   MSA_BUILTIN (fill_h, MIPS_V8HI_FTYPE_SI),
16232   MSA_BUILTIN (fill_w, MIPS_V4SI_FTYPE_SI),
16233   MSA_BUILTIN (fill_d, MIPS_V2DI_FTYPE_DI),
16234   MSA_BUILTIN (pcnt_b, MIPS_V16QI_FTYPE_V16QI),
16235   MSA_BUILTIN (pcnt_h, MIPS_V8HI_FTYPE_V8HI),
16236   MSA_BUILTIN (pcnt_w, MIPS_V4SI_FTYPE_V4SI),
16237   MSA_BUILTIN (pcnt_d, MIPS_V2DI_FTYPE_V2DI),
16238   MSA_BUILTIN (nloc_b, MIPS_V16QI_FTYPE_V16QI),
16239   MSA_BUILTIN (nloc_h, MIPS_V8HI_FTYPE_V8HI),
16240   MSA_BUILTIN (nloc_w, MIPS_V4SI_FTYPE_V4SI),
16241   MSA_BUILTIN (nloc_d, MIPS_V2DI_FTYPE_V2DI),
16242   MSA_BUILTIN (nlzc_b, MIPS_V16QI_FTYPE_V16QI),
16243   MSA_BUILTIN (nlzc_h, MIPS_V8HI_FTYPE_V8HI),
16244   MSA_BUILTIN (nlzc_w, MIPS_V4SI_FTYPE_V4SI),
16245   MSA_BUILTIN (nlzc_d, MIPS_V2DI_FTYPE_V2DI),
16246   MSA_BUILTIN (copy_s_b, MIPS_SI_FTYPE_V16QI_UQI),
16247   MSA_BUILTIN (copy_s_h, MIPS_SI_FTYPE_V8HI_UQI),
16248   MSA_BUILTIN (copy_s_w, MIPS_SI_FTYPE_V4SI_UQI),
16249   MSA_BUILTIN (copy_s_d, MIPS_DI_FTYPE_V2DI_UQI),
16250   MSA_BUILTIN (copy_u_b, MIPS_USI_FTYPE_V16QI_UQI),
16251   MSA_BUILTIN (copy_u_h, MIPS_USI_FTYPE_V8HI_UQI),
16252   MSA_BUILTIN_REMAP (copy_u_w, copy_s_w, MIPS_USI_FTYPE_V4SI_UQI),
16253   MSA_BUILTIN_REMAP (copy_u_d, copy_s_d, MIPS_UDI_FTYPE_V2DI_UQI),
16254   MSA_BUILTIN (insert_b, MIPS_V16QI_FTYPE_V16QI_UQI_SI),
16255   MSA_BUILTIN (insert_h, MIPS_V8HI_FTYPE_V8HI_UQI_SI),
16256   MSA_BUILTIN (insert_w, MIPS_V4SI_FTYPE_V4SI_UQI_SI),
16257   MSA_BUILTIN (insert_d, MIPS_V2DI_FTYPE_V2DI_UQI_DI),
16258   MSA_BUILTIN (insve_b, MIPS_V16QI_FTYPE_V16QI_UQI_V16QI),
16259   MSA_BUILTIN (insve_h, MIPS_V8HI_FTYPE_V8HI_UQI_V8HI),
16260   MSA_BUILTIN (insve_w, MIPS_V4SI_FTYPE_V4SI_UQI_V4SI),
16261   MSA_BUILTIN (insve_d, MIPS_V2DI_FTYPE_V2DI_UQI_V2DI),
16262   MSA_BUILTIN_TEST_BRANCH (bnz_b, MIPS_SI_FTYPE_UV16QI),
16263   MSA_BUILTIN_TEST_BRANCH (bnz_h, MIPS_SI_FTYPE_UV8HI),
16264   MSA_BUILTIN_TEST_BRANCH (bnz_w, MIPS_SI_FTYPE_UV4SI),
16265   MSA_BUILTIN_TEST_BRANCH (bnz_d, MIPS_SI_FTYPE_UV2DI),
16266   MSA_BUILTIN_TEST_BRANCH (bz_b, MIPS_SI_FTYPE_UV16QI),
16267   MSA_BUILTIN_TEST_BRANCH (bz_h, MIPS_SI_FTYPE_UV8HI),
16268   MSA_BUILTIN_TEST_BRANCH (bz_w, MIPS_SI_FTYPE_UV4SI),
16269   MSA_BUILTIN_TEST_BRANCH (bz_d, MIPS_SI_FTYPE_UV2DI),
16270   MSA_BUILTIN (ldi_b, MIPS_V16QI_FTYPE_HI),
16271   MSA_BUILTIN (ldi_h, MIPS_V8HI_FTYPE_HI),
16272   MSA_BUILTIN (ldi_w, MIPS_V4SI_FTYPE_HI),
16273   MSA_BUILTIN (ldi_d, MIPS_V2DI_FTYPE_HI),
16274   MSA_BUILTIN (fcaf_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16275   MSA_BUILTIN (fcaf_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16276   MSA_BUILTIN (fcor_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16277   MSA_BUILTIN (fcor_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16278   MSA_BUILTIN (fcun_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16279   MSA_BUILTIN (fcun_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16280   MSA_BUILTIN (fcune_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16281   MSA_BUILTIN (fcune_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16282   MSA_BUILTIN (fcueq_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16283   MSA_BUILTIN (fcueq_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16284   MSA_BUILTIN (fceq_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16285   MSA_BUILTIN (fceq_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16286   MSA_BUILTIN (fcne_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16287   MSA_BUILTIN (fcne_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16288   MSA_BUILTIN (fclt_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16289   MSA_BUILTIN (fclt_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16290   MSA_BUILTIN (fcult_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16291   MSA_BUILTIN (fcult_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16292   MSA_BUILTIN (fcle_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16293   MSA_BUILTIN (fcle_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16294   MSA_BUILTIN (fcule_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16295   MSA_BUILTIN (fcule_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16296   MSA_BUILTIN (fsaf_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16297   MSA_BUILTIN (fsaf_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16298   MSA_BUILTIN (fsor_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16299   MSA_BUILTIN (fsor_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16300   MSA_BUILTIN (fsun_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16301   MSA_BUILTIN (fsun_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16302   MSA_BUILTIN (fsune_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16303   MSA_BUILTIN (fsune_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16304   MSA_BUILTIN (fsueq_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16305   MSA_BUILTIN (fsueq_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16306   MSA_BUILTIN (fseq_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16307   MSA_BUILTIN (fseq_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16308   MSA_BUILTIN (fsne_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16309   MSA_BUILTIN (fsne_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16310   MSA_BUILTIN (fslt_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16311   MSA_BUILTIN (fslt_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16312   MSA_BUILTIN (fsult_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16313   MSA_BUILTIN (fsult_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16314   MSA_BUILTIN (fsle_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16315   MSA_BUILTIN (fsle_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16316   MSA_BUILTIN (fsule_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16317   MSA_BUILTIN (fsule_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16318   MSA_BUILTIN (fadd_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16319   MSA_BUILTIN (fadd_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16320   MSA_BUILTIN (fsub_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16321   MSA_BUILTIN (fsub_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16322   MSA_BUILTIN (fmul_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16323   MSA_BUILTIN (fmul_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16324   MSA_BUILTIN (fdiv_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16325   MSA_BUILTIN (fdiv_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16326   MSA_BUILTIN (fmadd_w, MIPS_V4SF_FTYPE_V4SF_V4SF_V4SF),
16327   MSA_BUILTIN (fmadd_d, MIPS_V2DF_FTYPE_V2DF_V2DF_V2DF),
16328   MSA_BUILTIN (fmsub_w, MIPS_V4SF_FTYPE_V4SF_V4SF_V4SF),
16329   MSA_BUILTIN (fmsub_d, MIPS_V2DF_FTYPE_V2DF_V2DF_V2DF),
16330   MSA_BUILTIN (fexp2_w, MIPS_V4SF_FTYPE_V4SF_V4SI),
16331   MSA_BUILTIN (fexp2_d, MIPS_V2DF_FTYPE_V2DF_V2DI),
16332   MSA_BUILTIN (fexdo_h, MIPS_V8HI_FTYPE_V4SF_V4SF),
16333   MSA_BUILTIN (fexdo_w, MIPS_V4SF_FTYPE_V2DF_V2DF),
16334   MSA_BUILTIN (ftq_h, MIPS_V8HI_FTYPE_V4SF_V4SF),
16335   MSA_BUILTIN (ftq_w, MIPS_V4SI_FTYPE_V2DF_V2DF),
16336   MSA_BUILTIN (fmin_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16337   MSA_BUILTIN (fmin_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16338   MSA_BUILTIN (fmin_a_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16339   MSA_BUILTIN (fmin_a_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16340   MSA_BUILTIN (fmax_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16341   MSA_BUILTIN (fmax_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16342   MSA_BUILTIN (fmax_a_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16343   MSA_BUILTIN (fmax_a_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16344   MSA_BUILTIN (mul_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16345   MSA_BUILTIN (mul_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16346   MSA_BUILTIN (mulr_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16347   MSA_BUILTIN (mulr_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16348   MSA_BUILTIN (madd_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16349   MSA_BUILTIN (madd_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16350   MSA_BUILTIN (maddr_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16351   MSA_BUILTIN (maddr_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16352   MSA_BUILTIN (msub_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16353   MSA_BUILTIN (msub_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16354   MSA_BUILTIN (msubr_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16355   MSA_BUILTIN (msubr_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16356   MSA_BUILTIN (fclass_w, MIPS_V4SI_FTYPE_V4SF),
16357   MSA_BUILTIN (fclass_d, MIPS_V2DI_FTYPE_V2DF),
16358   MSA_BUILTIN (fsqrt_w, MIPS_V4SF_FTYPE_V4SF),
16359   MSA_BUILTIN (fsqrt_d, MIPS_V2DF_FTYPE_V2DF),
16360   MSA_BUILTIN (frcp_w, MIPS_V4SF_FTYPE_V4SF),
16361   MSA_BUILTIN (frcp_d, MIPS_V2DF_FTYPE_V2DF),
16362   MSA_BUILTIN (frint_w, MIPS_V4SF_FTYPE_V4SF),
16363   MSA_BUILTIN (frint_d, MIPS_V2DF_FTYPE_V2DF),
16364   MSA_BUILTIN (frsqrt_w, MIPS_V4SF_FTYPE_V4SF),
16365   MSA_BUILTIN (frsqrt_d, MIPS_V2DF_FTYPE_V2DF),
16366   MSA_BUILTIN (flog2_w, MIPS_V4SF_FTYPE_V4SF),
16367   MSA_BUILTIN (flog2_d, MIPS_V2DF_FTYPE_V2DF),
16368   MSA_BUILTIN (fexupl_w, MIPS_V4SF_FTYPE_V8HI),
16369   MSA_BUILTIN (fexupl_d, MIPS_V2DF_FTYPE_V4SF),
16370   MSA_BUILTIN (fexupr_w, MIPS_V4SF_FTYPE_V8HI),
16371   MSA_BUILTIN (fexupr_d, MIPS_V2DF_FTYPE_V4SF),
16372   MSA_BUILTIN (ffql_w, MIPS_V4SF_FTYPE_V8HI),
16373   MSA_BUILTIN (ffql_d, MIPS_V2DF_FTYPE_V4SI),
16374   MSA_BUILTIN (ffqr_w, MIPS_V4SF_FTYPE_V8HI),
16375   MSA_BUILTIN (ffqr_d, MIPS_V2DF_FTYPE_V4SI),
16376   MSA_BUILTIN (ftint_s_w, MIPS_V4SI_FTYPE_V4SF),
16377   MSA_BUILTIN (ftint_s_d, MIPS_V2DI_FTYPE_V2DF),
16378   MSA_BUILTIN (ftint_u_w, MIPS_UV4SI_FTYPE_V4SF),
16379   MSA_BUILTIN (ftint_u_d, MIPS_UV2DI_FTYPE_V2DF),
16380   MSA_BUILTIN (ftrunc_s_w, MIPS_V4SI_FTYPE_V4SF),
16381   MSA_BUILTIN (ftrunc_s_d, MIPS_V2DI_FTYPE_V2DF),
16382   MSA_BUILTIN (ftrunc_u_w, MIPS_UV4SI_FTYPE_V4SF),
16383   MSA_BUILTIN (ftrunc_u_d, MIPS_UV2DI_FTYPE_V2DF),
16384   MSA_BUILTIN (ffint_s_w, MIPS_V4SF_FTYPE_V4SI),
16385   MSA_BUILTIN (ffint_s_d, MIPS_V2DF_FTYPE_V2DI),
16386   MSA_BUILTIN (ffint_u_w, MIPS_V4SF_FTYPE_UV4SI),
16387   MSA_BUILTIN (ffint_u_d, MIPS_V2DF_FTYPE_UV2DI),
16388   MSA_NO_TARGET_BUILTIN (ctcmsa, MIPS_VOID_FTYPE_UQI_SI),
16389   MSA_BUILTIN (cfcmsa, MIPS_SI_FTYPE_UQI),
16390   MSA_BUILTIN (move_v, MIPS_V16QI_FTYPE_V16QI),
16391 };
16392 
16393 /* Index I is the function declaration for mips_builtins[I], or null if the
16394    function isn't defined on this target.  */
16395 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
16396 /* Get the index I of the function declaration for mips_builtin_decls[I]
16397    using the instruction code or return null if not defined for the target.  */
16398 static GTY(()) int mips_get_builtin_decl_index[NUM_INSN_CODES];
16399 
16400 /* MODE is a vector mode whose elements have type TYPE.  Return the type
16401    of the vector itself.  */
16402 
16403 static tree
mips_builtin_vector_type(tree type,machine_mode mode)16404 mips_builtin_vector_type (tree type, machine_mode mode)
16405 {
16406   static tree types[2 * (int) MAX_MACHINE_MODE];
16407   int mode_index;
16408 
16409   mode_index = (int) mode;
16410 
16411   if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
16412     mode_index += MAX_MACHINE_MODE;
16413 
16414   if (types[mode_index] == NULL_TREE)
16415     types[mode_index] = build_vector_type_for_mode (type, mode);
16416   return types[mode_index];
16417 }
16418 
16419 /* Return a type for 'const volatile void *'.  */
16420 
16421 static tree
mips_build_cvpointer_type(void)16422 mips_build_cvpointer_type (void)
16423 {
16424   static tree cache;
16425 
16426   if (cache == NULL_TREE)
16427     cache = build_pointer_type (build_qualified_type
16428 				(void_type_node,
16429 				 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
16430   return cache;
16431 }
16432 
16433 /* Source-level argument types.  */
16434 #define MIPS_ATYPE_VOID void_type_node
16435 #define MIPS_ATYPE_INT integer_type_node
16436 #define MIPS_ATYPE_POINTER ptr_type_node
16437 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
16438 
16439 /* Standard mode-based argument types.  */
16440 #define MIPS_ATYPE_QI intQI_type_node
16441 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
16442 #define MIPS_ATYPE_HI intHI_type_node
16443 #define MIPS_ATYPE_SI intSI_type_node
16444 #define MIPS_ATYPE_USI unsigned_intSI_type_node
16445 #define MIPS_ATYPE_DI intDI_type_node
16446 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
16447 #define MIPS_ATYPE_SF float_type_node
16448 #define MIPS_ATYPE_DF double_type_node
16449 
16450 /* Vector argument types.  */
16451 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
16452 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
16453 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
16454 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
16455 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
16456 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
16457 
16458 #define MIPS_ATYPE_V2DI						\
16459   mips_builtin_vector_type (long_long_integer_type_node, V2DImode)
16460 #define MIPS_ATYPE_V4SI mips_builtin_vector_type (intSI_type_node, V4SImode)
16461 #define MIPS_ATYPE_V8HI mips_builtin_vector_type (intHI_type_node, V8HImode)
16462 #define MIPS_ATYPE_V16QI mips_builtin_vector_type (intQI_type_node, V16QImode)
16463 #define MIPS_ATYPE_V2DF mips_builtin_vector_type (double_type_node, V2DFmode)
16464 #define MIPS_ATYPE_V4SF mips_builtin_vector_type (float_type_node, V4SFmode)
16465 
16466 #define MIPS_ATYPE_UV2DI					\
16467   mips_builtin_vector_type (long_long_unsigned_type_node, V2DImode)
16468 #define MIPS_ATYPE_UV4SI					\
16469   mips_builtin_vector_type (unsigned_intSI_type_node, V4SImode)
16470 #define MIPS_ATYPE_UV8HI					\
16471   mips_builtin_vector_type (unsigned_intHI_type_node, V8HImode)
16472 #define MIPS_ATYPE_UV16QI					\
16473   mips_builtin_vector_type (unsigned_intQI_type_node, V16QImode)
16474 
16475 #define MIPS_ATYPE_UV2SI					\
16476   mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
16477 #define MIPS_ATYPE_UV4HI					\
16478   mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
16479 #define MIPS_ATYPE_UV8QI					\
16480   mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
16481 
16482 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
16483    their associated MIPS_ATYPEs.  */
16484 #define MIPS_FTYPE_ATYPES1(A, B) \
16485   MIPS_ATYPE_##A, MIPS_ATYPE_##B
16486 
16487 #define MIPS_FTYPE_ATYPES2(A, B, C) \
16488   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
16489 
16490 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
16491   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
16492 
16493 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
16494   MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
16495   MIPS_ATYPE_##E
16496 
16497 /* Return the function type associated with function prototype TYPE.  */
16498 
16499 static tree
mips_build_function_type(enum mips_function_type type)16500 mips_build_function_type (enum mips_function_type type)
16501 {
16502   static tree types[(int) MIPS_MAX_FTYPE_MAX];
16503 
16504   if (types[(int) type] == NULL_TREE)
16505     switch (type)
16506       {
16507 #define DEF_MIPS_FTYPE(NUM, ARGS)					\
16508   case MIPS_FTYPE_NAME##NUM ARGS:					\
16509     types[(int) type]							\
16510       = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS,		\
16511 				  NULL_TREE);				\
16512     break;
16513 #include "config/mips/mips-ftypes.def"
16514 #undef DEF_MIPS_FTYPE
16515       default:
16516 	gcc_unreachable ();
16517       }
16518 
16519   return types[(int) type];
16520 }
16521 
16522 /* Implement TARGET_INIT_BUILTINS.  */
16523 
16524 static void
mips_init_builtins(void)16525 mips_init_builtins (void)
16526 {
16527   const struct mips_builtin_description *d;
16528   unsigned int i;
16529 
16530   /* Iterate through all of the bdesc arrays, initializing all of the
16531      builtin functions.  */
16532   for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
16533     {
16534       d = &mips_builtins[i];
16535       if (d->avail ())
16536 	{
16537 	  mips_builtin_decls[i]
16538 	    = add_builtin_function (d->name,
16539 				    mips_build_function_type (d->function_type),
16540 				    i, BUILT_IN_MD, NULL, NULL);
16541 	  mips_get_builtin_decl_index[d->icode] = i;
16542 	}
16543     }
16544 }
16545 
16546 /* Implement TARGET_BUILTIN_DECL.  */
16547 
16548 static tree
mips_builtin_decl(unsigned int code,bool initialize_p ATTRIBUTE_UNUSED)16549 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
16550 {
16551   if (code >= ARRAY_SIZE (mips_builtins))
16552     return error_mark_node;
16553   return mips_builtin_decls[code];
16554 }
16555 
16556 /* Implement TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION.  */
16557 
16558 static tree
mips_builtin_vectorized_function(unsigned int fn,tree type_out,tree type_in)16559 mips_builtin_vectorized_function (unsigned int fn, tree type_out, tree type_in)
16560 {
16561   machine_mode in_mode, out_mode;
16562   int in_n, out_n;
16563 
16564   if (TREE_CODE (type_out) != VECTOR_TYPE
16565       || TREE_CODE (type_in) != VECTOR_TYPE
16566       || !ISA_HAS_MSA)
16567     return NULL_TREE;
16568 
16569   out_mode = TYPE_MODE (TREE_TYPE (type_out));
16570   out_n = TYPE_VECTOR_SUBPARTS (type_out);
16571   in_mode = TYPE_MODE (TREE_TYPE (type_in));
16572   in_n = TYPE_VECTOR_SUBPARTS (type_in);
16573 
16574   /* INSN is the name of the associated instruction pattern, without
16575      the leading CODE_FOR_.  */
16576 #define MIPS_GET_BUILTIN(INSN) \
16577   mips_builtin_decls[mips_get_builtin_decl_index[CODE_FOR_##INSN]]
16578 
16579   switch (fn)
16580     {
16581     case BUILT_IN_SQRT:
16582       if (out_mode == DFmode && out_n == 2
16583 	  && in_mode == DFmode && in_n == 2)
16584 	return MIPS_GET_BUILTIN (msa_fsqrt_d);
16585       break;
16586     case BUILT_IN_SQRTF:
16587       if (out_mode == SFmode && out_n == 4
16588 	  && in_mode == SFmode && in_n == 4)
16589 	return MIPS_GET_BUILTIN (msa_fsqrt_w);
16590       break;
16591     default:
16592       break;
16593     }
16594 
16595   return NULL_TREE;
16596 }
16597 
16598 /* Take argument ARGNO from EXP's argument list and convert it into
16599    an expand operand.  Store the operand in *OP.  */
16600 
16601 static void
mips_prepare_builtin_arg(struct expand_operand * op,tree exp,unsigned int argno)16602 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
16603 			  unsigned int argno)
16604 {
16605   tree arg;
16606   rtx value;
16607 
16608   arg = CALL_EXPR_ARG (exp, argno);
16609   value = expand_normal (arg);
16610   create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
16611 }
16612 
16613 /* Expand instruction ICODE as part of a built-in function sequence.
16614    Use the first NOPS elements of OPS as the instruction's operands.
16615    HAS_TARGET_P is true if operand 0 is a target; it is false if the
16616    instruction has no target.
16617 
16618    Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx.  */
16619 
16620 static rtx
mips_expand_builtin_insn(enum insn_code icode,unsigned int nops,struct expand_operand * ops,bool has_target_p)16621 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
16622 			  struct expand_operand *ops, bool has_target_p)
16623 {
16624   machine_mode imode;
16625   int rangelo = 0, rangehi = 0, error_opno = 0;
16626   rtx sireg;
16627 
16628   switch (icode)
16629     {
16630     /* The third operand of these instructions is in SImode, so we need to
16631        bring the corresponding builtin argument from QImode into SImode.  */
16632     case CODE_FOR_loongson_pshufh:
16633     case CODE_FOR_loongson_psllh:
16634     case CODE_FOR_loongson_psllw:
16635     case CODE_FOR_loongson_psrah:
16636     case CODE_FOR_loongson_psraw:
16637     case CODE_FOR_loongson_psrlh:
16638     case CODE_FOR_loongson_psrlw:
16639       gcc_assert (has_target_p && nops == 3 && ops[2].mode == QImode);
16640       sireg = gen_reg_rtx (SImode);
16641       emit_insn (gen_zero_extendqisi2 (sireg,
16642 				       force_reg (QImode, ops[2].value)));
16643       ops[2].value = sireg;
16644       ops[2].mode = SImode;
16645       break;
16646 
16647     case CODE_FOR_msa_addvi_b:
16648     case CODE_FOR_msa_addvi_h:
16649     case CODE_FOR_msa_addvi_w:
16650     case CODE_FOR_msa_addvi_d:
16651     case CODE_FOR_msa_clti_u_b:
16652     case CODE_FOR_msa_clti_u_h:
16653     case CODE_FOR_msa_clti_u_w:
16654     case CODE_FOR_msa_clti_u_d:
16655     case CODE_FOR_msa_clei_u_b:
16656     case CODE_FOR_msa_clei_u_h:
16657     case CODE_FOR_msa_clei_u_w:
16658     case CODE_FOR_msa_clei_u_d:
16659     case CODE_FOR_msa_maxi_u_b:
16660     case CODE_FOR_msa_maxi_u_h:
16661     case CODE_FOR_msa_maxi_u_w:
16662     case CODE_FOR_msa_maxi_u_d:
16663     case CODE_FOR_msa_mini_u_b:
16664     case CODE_FOR_msa_mini_u_h:
16665     case CODE_FOR_msa_mini_u_w:
16666     case CODE_FOR_msa_mini_u_d:
16667     case CODE_FOR_msa_subvi_b:
16668     case CODE_FOR_msa_subvi_h:
16669     case CODE_FOR_msa_subvi_w:
16670     case CODE_FOR_msa_subvi_d:
16671       gcc_assert (has_target_p && nops == 3);
16672       /* We only generate a vector of constants iff the second argument
16673 	 is an immediate.  We also validate the range of the immediate.  */
16674       if (CONST_INT_P (ops[2].value))
16675 	{
16676 	  rangelo = 0;
16677 	  rangehi = 31;
16678 	  if (IN_RANGE (INTVAL (ops[2].value), rangelo, rangehi))
16679 	    {
16680 	      ops[2].mode = ops[0].mode;
16681 	      ops[2].value = mips_gen_const_int_vector (ops[2].mode,
16682 							INTVAL (ops[2].value));
16683 	    }
16684 	  else
16685 	    error_opno = 2;
16686 	}
16687       break;
16688 
16689     case CODE_FOR_msa_ceqi_b:
16690     case CODE_FOR_msa_ceqi_h:
16691     case CODE_FOR_msa_ceqi_w:
16692     case CODE_FOR_msa_ceqi_d:
16693     case CODE_FOR_msa_clti_s_b:
16694     case CODE_FOR_msa_clti_s_h:
16695     case CODE_FOR_msa_clti_s_w:
16696     case CODE_FOR_msa_clti_s_d:
16697     case CODE_FOR_msa_clei_s_b:
16698     case CODE_FOR_msa_clei_s_h:
16699     case CODE_FOR_msa_clei_s_w:
16700     case CODE_FOR_msa_clei_s_d:
16701     case CODE_FOR_msa_maxi_s_b:
16702     case CODE_FOR_msa_maxi_s_h:
16703     case CODE_FOR_msa_maxi_s_w:
16704     case CODE_FOR_msa_maxi_s_d:
16705     case CODE_FOR_msa_mini_s_b:
16706     case CODE_FOR_msa_mini_s_h:
16707     case CODE_FOR_msa_mini_s_w:
16708     case CODE_FOR_msa_mini_s_d:
16709       gcc_assert (has_target_p && nops == 3);
16710       /* We only generate a vector of constants iff the second argument
16711 	 is an immediate.  We also validate the range of the immediate.  */
16712       if (CONST_INT_P (ops[2].value))
16713 	{
16714 	  rangelo = -16;
16715 	  rangehi = 15;
16716 	  if (IN_RANGE (INTVAL (ops[2].value), rangelo, rangehi))
16717 	    {
16718 	      ops[2].mode = ops[0].mode;
16719 	      ops[2].value = mips_gen_const_int_vector (ops[2].mode,
16720 							INTVAL (ops[2].value));
16721 	    }
16722 	  else
16723 	    error_opno = 2;
16724 	}
16725       break;
16726 
16727     case CODE_FOR_msa_andi_b:
16728     case CODE_FOR_msa_ori_b:
16729     case CODE_FOR_msa_nori_b:
16730     case CODE_FOR_msa_xori_b:
16731       gcc_assert (has_target_p && nops == 3);
16732       if (!CONST_INT_P (ops[2].value))
16733 	break;
16734       ops[2].mode = ops[0].mode;
16735       ops[2].value = mips_gen_const_int_vector (ops[2].mode,
16736 						INTVAL (ops[2].value));
16737       break;
16738 
16739     case CODE_FOR_msa_bmzi_b:
16740     case CODE_FOR_msa_bmnzi_b:
16741     case CODE_FOR_msa_bseli_b:
16742       gcc_assert (has_target_p && nops == 4);
16743       if (!CONST_INT_P (ops[3].value))
16744 	break;
16745       ops[3].mode = ops[0].mode;
16746       ops[3].value = mips_gen_const_int_vector (ops[3].mode,
16747 						INTVAL (ops[3].value));
16748       break;
16749 
16750     case CODE_FOR_msa_fill_b:
16751     case CODE_FOR_msa_fill_h:
16752     case CODE_FOR_msa_fill_w:
16753     case CODE_FOR_msa_fill_d:
16754       /* Map the built-ins to vector fill operations.  We need fix up the mode
16755 	 for the element being inserted.  */
16756       gcc_assert (has_target_p && nops == 2);
16757       imode = GET_MODE_INNER (ops[0].mode);
16758       ops[1].value = lowpart_subreg (imode, ops[1].value, ops[1].mode);
16759       ops[1].mode = imode;
16760       break;
16761 
16762     case CODE_FOR_msa_ilvl_b:
16763     case CODE_FOR_msa_ilvl_h:
16764     case CODE_FOR_msa_ilvl_w:
16765     case CODE_FOR_msa_ilvl_d:
16766     case CODE_FOR_msa_ilvr_b:
16767     case CODE_FOR_msa_ilvr_h:
16768     case CODE_FOR_msa_ilvr_w:
16769     case CODE_FOR_msa_ilvr_d:
16770     case CODE_FOR_msa_ilvev_b:
16771     case CODE_FOR_msa_ilvev_h:
16772     case CODE_FOR_msa_ilvev_w:
16773     case CODE_FOR_msa_ilvod_b:
16774     case CODE_FOR_msa_ilvod_h:
16775     case CODE_FOR_msa_ilvod_w:
16776     case CODE_FOR_msa_pckev_b:
16777     case CODE_FOR_msa_pckev_h:
16778     case CODE_FOR_msa_pckev_w:
16779     case CODE_FOR_msa_pckod_b:
16780     case CODE_FOR_msa_pckod_h:
16781     case CODE_FOR_msa_pckod_w:
16782       /* Swap the operands 1 and 2 for interleave operations.  Built-ins follow
16783 	 convention of ISA, which have op1 as higher component and op2 as lower
16784 	 component.  However, the VEC_PERM op in tree and vec_concat in RTL
16785 	 expects first operand to be lower component, because of which this
16786 	 swap is needed for builtins.  */
16787       gcc_assert (has_target_p && nops == 3);
16788       std::swap (ops[1], ops[2]);
16789       break;
16790 
16791     case CODE_FOR_msa_maddv_b:
16792     case CODE_FOR_msa_maddv_h:
16793     case CODE_FOR_msa_maddv_w:
16794     case CODE_FOR_msa_maddv_d:
16795     case CODE_FOR_msa_fmadd_w:
16796     case CODE_FOR_msa_fmadd_d:
16797     case CODE_FOR_msa_fmsub_w:
16798     case CODE_FOR_msa_fmsub_d:
16799       /* fma(a, b, c) results into (a * b + c), however builtin_msa_fmadd expects
16800 	 it to be (a + b * c).  Swap the 1st and 3rd operands.  */
16801       std::swap (ops[1], ops[3]);
16802       break;
16803 
16804     case CODE_FOR_msa_slli_b:
16805     case CODE_FOR_msa_slli_h:
16806     case CODE_FOR_msa_slli_w:
16807     case CODE_FOR_msa_slli_d:
16808     case CODE_FOR_msa_srai_b:
16809     case CODE_FOR_msa_srai_h:
16810     case CODE_FOR_msa_srai_w:
16811     case CODE_FOR_msa_srai_d:
16812     case CODE_FOR_msa_srli_b:
16813     case CODE_FOR_msa_srli_h:
16814     case CODE_FOR_msa_srli_w:
16815     case CODE_FOR_msa_srli_d:
16816       gcc_assert (has_target_p && nops == 3);
16817       if (CONST_INT_P (ops[2].value))
16818 	{
16819 	  rangelo = 0;
16820 	  rangehi = GET_MODE_UNIT_BITSIZE (ops[0].mode) - 1;
16821 	  if (IN_RANGE (INTVAL (ops[2].value), rangelo, rangehi))
16822 	    {
16823 	      ops[2].mode = ops[0].mode;
16824 	      ops[2].value = mips_gen_const_int_vector (ops[2].mode,
16825 							INTVAL (ops[2].value));
16826 	    }
16827 	  else
16828 	    error_opno = 2;
16829 	}
16830       break;
16831 
16832     case CODE_FOR_msa_insert_b:
16833     case CODE_FOR_msa_insert_h:
16834     case CODE_FOR_msa_insert_w:
16835     case CODE_FOR_msa_insert_d:
16836       /* Map the built-ins to insert operations.  We need to swap operands,
16837 	 fix up the mode for the element being inserted, and generate
16838 	 a bit mask for vec_merge.  */
16839       gcc_assert (has_target_p && nops == 4);
16840       std::swap (ops[1], ops[2]);
16841       std::swap (ops[1], ops[3]);
16842       imode = GET_MODE_INNER (ops[0].mode);
16843       ops[1].value = lowpart_subreg (imode, ops[1].value, ops[1].mode);
16844       ops[1].mode = imode;
16845       rangelo = 0;
16846       rangehi = GET_MODE_NUNITS (ops[0].mode) - 1;
16847       if (CONST_INT_P (ops[3].value)
16848 	  && IN_RANGE (INTVAL (ops[3].value), rangelo, rangehi))
16849 	ops[3].value = GEN_INT (1 << INTVAL (ops[3].value));
16850       else
16851 	error_opno = 2;
16852       break;
16853 
16854     case CODE_FOR_msa_insve_b:
16855     case CODE_FOR_msa_insve_h:
16856     case CODE_FOR_msa_insve_w:
16857     case CODE_FOR_msa_insve_d:
16858       /* Map the built-ins to element insert operations.  We need to swap
16859 	 operands and generate a bit mask.  */
16860       gcc_assert (has_target_p && nops == 4);
16861       std::swap (ops[1], ops[2]);
16862       std::swap (ops[1], ops[3]);
16863       rangelo = 0;
16864       rangehi = GET_MODE_NUNITS (ops[0].mode) - 1;
16865       if (CONST_INT_P (ops[3].value)
16866 	  && IN_RANGE (INTVAL (ops[3].value), rangelo, rangehi))
16867 	ops[3].value = GEN_INT (1 << INTVAL (ops[3].value));
16868       else
16869 	error_opno = 2;
16870       break;
16871 
16872     case CODE_FOR_msa_shf_b:
16873     case CODE_FOR_msa_shf_h:
16874     case CODE_FOR_msa_shf_w:
16875     case CODE_FOR_msa_shf_w_f:
16876       gcc_assert (has_target_p && nops == 3);
16877       ops[2].value = mips_gen_const_int_vector_shuffle (ops[0].mode,
16878 							INTVAL (ops[2].value));
16879       break;
16880 
16881     case CODE_FOR_msa_vshf_b:
16882     case CODE_FOR_msa_vshf_h:
16883     case CODE_FOR_msa_vshf_w:
16884     case CODE_FOR_msa_vshf_d:
16885       gcc_assert (has_target_p && nops == 4);
16886       std::swap (ops[1], ops[3]);
16887       break;
16888 
16889     default:
16890       break;
16891   }
16892 
16893   if (error_opno != 0)
16894     {
16895       error ("argument %d to the built-in must be a constant"
16896 	     " in range %d to %d", error_opno, rangelo, rangehi);
16897       return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
16898     }
16899   else if (!maybe_expand_insn (icode, nops, ops))
16900     {
16901       error ("invalid argument to built-in function");
16902       return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
16903     }
16904   return has_target_p ? ops[0].value : const0_rtx;
16905 }
16906 
16907 /* Expand a floating-point comparison for built-in function call EXP.
16908    The first NARGS arguments are the values to be compared.  ICODE is
16909    the .md pattern that does the comparison and COND is the condition
16910    that is being tested.  Return an rtx for the result.  */
16911 
16912 static rtx
mips_expand_builtin_compare_1(enum insn_code icode,enum mips_fp_condition cond,tree exp,int nargs)16913 mips_expand_builtin_compare_1 (enum insn_code icode,
16914 			       enum mips_fp_condition cond,
16915 			       tree exp, int nargs)
16916 {
16917   struct expand_operand ops[MAX_RECOG_OPERANDS];
16918   rtx output;
16919   int opno, argno;
16920 
16921   /* The instruction should have a target operand, an operand for each
16922      argument, and an operand for COND.  */
16923   gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
16924 
16925   output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
16926   opno = 0;
16927   create_fixed_operand (&ops[opno++], output);
16928   for (argno = 0; argno < nargs; argno++)
16929     mips_prepare_builtin_arg (&ops[opno++], exp, argno);
16930   create_integer_operand (&ops[opno++], (int) cond);
16931   return mips_expand_builtin_insn (icode, opno, ops, true);
16932 }
16933 
16934 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
16935    HAS_TARGET_P says which.  EXP is the CALL_EXPR that calls the function
16936    and ICODE is the code of the associated .md pattern.  TARGET, if nonnull,
16937    suggests a good place to put the result.  */
16938 
16939 static rtx
mips_expand_builtin_direct(enum insn_code icode,rtx target,tree exp,bool has_target_p)16940 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
16941 			    bool has_target_p)
16942 {
16943   struct expand_operand ops[MAX_RECOG_OPERANDS];
16944   int opno, argno;
16945 
16946   /* Map any target to operand 0.  */
16947   opno = 0;
16948   if (has_target_p)
16949     create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
16950 
16951   /* Map the arguments to the other operands.  */
16952   gcc_assert (opno + call_expr_nargs (exp)
16953 	      == insn_data[icode].n_generator_args);
16954   for (argno = 0; argno < call_expr_nargs (exp); argno++)
16955     mips_prepare_builtin_arg (&ops[opno++], exp, argno);
16956 
16957   return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
16958 }
16959 
16960 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
16961    function; TYPE says which.  EXP is the CALL_EXPR that calls the
16962    function, ICODE is the instruction that should be used to compare
16963    the first two arguments, and COND is the condition it should test.
16964    TARGET, if nonnull, suggests a good place to put the result.  */
16965 
16966 static rtx
mips_expand_builtin_movtf(enum mips_builtin_type type,enum insn_code icode,enum mips_fp_condition cond,rtx target,tree exp)16967 mips_expand_builtin_movtf (enum mips_builtin_type type,
16968 			   enum insn_code icode, enum mips_fp_condition cond,
16969 			   rtx target, tree exp)
16970 {
16971   struct expand_operand ops[4];
16972   rtx cmp_result;
16973 
16974   cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
16975   create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
16976   if (type == MIPS_BUILTIN_MOVT)
16977     {
16978       mips_prepare_builtin_arg (&ops[2], exp, 2);
16979       mips_prepare_builtin_arg (&ops[1], exp, 3);
16980     }
16981   else
16982     {
16983       mips_prepare_builtin_arg (&ops[1], exp, 2);
16984       mips_prepare_builtin_arg (&ops[2], exp, 3);
16985     }
16986   create_fixed_operand (&ops[3], cmp_result);
16987   return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
16988 				   4, ops, true);
16989 }
16990 
16991 /* Expand an MSA built-in for a compare and branch instruction specified by
16992    ICODE, set a general-purpose register to 1 if the branch was taken,
16993    0 otherwise.  */
16994 
16995 static rtx
mips_expand_builtin_msa_test_branch(enum insn_code icode,tree exp)16996 mips_expand_builtin_msa_test_branch (enum insn_code icode, tree exp)
16997 {
16998   struct expand_operand ops[3];
16999   rtx_insn *cbranch;
17000   rtx_code_label *true_label, *done_label;
17001   rtx cmp_result;
17002 
17003   true_label = gen_label_rtx ();
17004   done_label = gen_label_rtx ();
17005 
17006   create_input_operand (&ops[0], true_label, TYPE_MODE (TREE_TYPE (exp)));
17007   mips_prepare_builtin_arg (&ops[1], exp, 0);
17008   create_fixed_operand (&ops[2], const0_rtx);
17009 
17010   /* Make sure that the operand 1 is a REG.  */
17011   if (GET_CODE (ops[1].value) != REG)
17012     ops[1].value = force_reg (ops[1].mode, ops[1].value);
17013 
17014   if ((cbranch = maybe_gen_insn (icode, 3, ops)) == NULL_RTX)
17015     error ("failed to expand built-in function");
17016 
17017   cmp_result = gen_reg_rtx (SImode);
17018 
17019   /* First assume that CMP_RESULT is false.  */
17020   mips_emit_move (cmp_result, const0_rtx);
17021 
17022   /* Branch to TRUE_LABEL if CBRANCH is taken and DONE_LABEL otherwise.  */
17023   emit_jump_insn (cbranch);
17024   emit_jump_insn (gen_jump (done_label));
17025   emit_barrier ();
17026 
17027   /* Set CMP_RESULT to true if the branch was taken.  */
17028   emit_label (true_label);
17029   mips_emit_move (cmp_result, const1_rtx);
17030 
17031   emit_label (done_label);
17032   return cmp_result;
17033 }
17034 
17035 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
17036    into TARGET otherwise.  Return TARGET.  */
17037 
17038 static rtx
mips_builtin_branch_and_move(rtx condition,rtx target,rtx value_if_true,rtx value_if_false)17039 mips_builtin_branch_and_move (rtx condition, rtx target,
17040 			      rtx value_if_true, rtx value_if_false)
17041 {
17042   rtx_code_label *true_label, *done_label;
17043 
17044   true_label = gen_label_rtx ();
17045   done_label = gen_label_rtx ();
17046 
17047   /* First assume that CONDITION is false.  */
17048   mips_emit_move (target, value_if_false);
17049 
17050   /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise.  */
17051   emit_jump_insn (gen_condjump (condition, true_label));
17052   emit_jump_insn (gen_jump (done_label));
17053   emit_barrier ();
17054 
17055   /* Fix TARGET if CONDITION is true.  */
17056   emit_label (true_label);
17057   mips_emit_move (target, value_if_true);
17058 
17059   emit_label (done_label);
17060   return target;
17061 }
17062 
17063 /* Expand a comparison built-in function of type BUILTIN_TYPE.  EXP is
17064    the CALL_EXPR that calls the function, ICODE is the code of the
17065    comparison instruction, and COND is the condition it should test.
17066    TARGET, if nonnull, suggests a good place to put the boolean result.  */
17067 
17068 static rtx
mips_expand_builtin_compare(enum mips_builtin_type builtin_type,enum insn_code icode,enum mips_fp_condition cond,rtx target,tree exp)17069 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
17070 			     enum insn_code icode, enum mips_fp_condition cond,
17071 			     rtx target, tree exp)
17072 {
17073   rtx offset, condition, cmp_result;
17074 
17075   if (target == 0 || GET_MODE (target) != SImode)
17076     target = gen_reg_rtx (SImode);
17077   cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
17078 					      call_expr_nargs (exp));
17079 
17080   /* If the comparison sets more than one register, we define the result
17081      to be 0 if all registers are false and -1 if all registers are true.
17082      The value of the complete result is indeterminate otherwise.  */
17083   switch (builtin_type)
17084     {
17085     case MIPS_BUILTIN_CMP_ALL:
17086       condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
17087       return mips_builtin_branch_and_move (condition, target,
17088 					   const0_rtx, const1_rtx);
17089 
17090     case MIPS_BUILTIN_CMP_UPPER:
17091     case MIPS_BUILTIN_CMP_LOWER:
17092       offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
17093       condition = gen_single_cc (cmp_result, offset);
17094       return mips_builtin_branch_and_move (condition, target,
17095 					   const1_rtx, const0_rtx);
17096 
17097     default:
17098       condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
17099       return mips_builtin_branch_and_move (condition, target,
17100 					   const1_rtx, const0_rtx);
17101     }
17102 }
17103 
17104 /* Expand a bposge built-in function of type BUILTIN_TYPE.  TARGET,
17105    if nonnull, suggests a good place to put the boolean result.  */
17106 
17107 static rtx
mips_expand_builtin_bposge(enum mips_builtin_type builtin_type,rtx target)17108 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
17109 {
17110   rtx condition, cmp_result;
17111   int cmp_value;
17112 
17113   if (target == 0 || GET_MODE (target) != SImode)
17114     target = gen_reg_rtx (SImode);
17115 
17116   cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
17117 
17118   if (builtin_type == MIPS_BUILTIN_BPOSGE32)
17119     cmp_value = 32;
17120   else
17121     gcc_assert (0);
17122 
17123   condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
17124   return mips_builtin_branch_and_move (condition, target,
17125 				       const1_rtx, const0_rtx);
17126 }
17127 
17128 /* Implement TARGET_EXPAND_BUILTIN.  */
17129 
17130 static rtx
mips_expand_builtin(tree exp,rtx target,rtx subtarget ATTRIBUTE_UNUSED,machine_mode mode,int ignore)17131 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
17132 		     machine_mode mode, int ignore)
17133 {
17134   tree fndecl;
17135   unsigned int fcode, avail;
17136   const struct mips_builtin_description *d;
17137 
17138   fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
17139   fcode = DECL_FUNCTION_CODE (fndecl);
17140   gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
17141   d = &mips_builtins[fcode];
17142   avail = d->avail ();
17143   gcc_assert (avail != 0);
17144   if (TARGET_MIPS16 && !(avail & BUILTIN_AVAIL_MIPS16))
17145     {
17146       error ("built-in function %qE not supported for MIPS16",
17147 	     DECL_NAME (fndecl));
17148       return ignore ? const0_rtx : CONST0_RTX (mode);
17149     }
17150   switch (d->builtin_type)
17151     {
17152     case MIPS_BUILTIN_DIRECT:
17153       return mips_expand_builtin_direct (d->icode, target, exp, true);
17154 
17155     case MIPS_BUILTIN_DIRECT_NO_TARGET:
17156       return mips_expand_builtin_direct (d->icode, target, exp, false);
17157 
17158     case MIPS_BUILTIN_MOVT:
17159     case MIPS_BUILTIN_MOVF:
17160       return mips_expand_builtin_movtf (d->builtin_type, d->icode,
17161 					d->cond, target, exp);
17162 
17163     case MIPS_BUILTIN_CMP_ANY:
17164     case MIPS_BUILTIN_CMP_ALL:
17165     case MIPS_BUILTIN_CMP_UPPER:
17166     case MIPS_BUILTIN_CMP_LOWER:
17167     case MIPS_BUILTIN_CMP_SINGLE:
17168       return mips_expand_builtin_compare (d->builtin_type, d->icode,
17169 					  d->cond, target, exp);
17170 
17171     case MIPS_BUILTIN_MSA_TEST_BRANCH:
17172       return mips_expand_builtin_msa_test_branch (d->icode, exp);
17173 
17174     case MIPS_BUILTIN_BPOSGE32:
17175       return mips_expand_builtin_bposge (d->builtin_type, target);
17176     }
17177   gcc_unreachable ();
17178 }
17179 
17180 /* An entry in the MIPS16 constant pool.  VALUE is the pool constant,
17181    MODE is its mode, and LABEL is the CODE_LABEL associated with it.  */
17182 struct mips16_constant {
17183   struct mips16_constant *next;
17184   rtx value;
17185   rtx_code_label *label;
17186   machine_mode mode;
17187 };
17188 
17189 /* Information about an incomplete MIPS16 constant pool.  FIRST is the
17190    first constant, HIGHEST_ADDRESS is the highest address that the first
17191    byte of the pool can have, and INSN_ADDRESS is the current instruction
17192    address.  */
17193 struct mips16_constant_pool {
17194   struct mips16_constant *first;
17195   int highest_address;
17196   int insn_address;
17197 };
17198 
17199 /* Add constant VALUE to POOL and return its label.  MODE is the
17200    value's mode (used for CONST_INTs, etc.).  */
17201 
17202 static rtx_code_label *
mips16_add_constant(struct mips16_constant_pool * pool,rtx value,machine_mode mode)17203 mips16_add_constant (struct mips16_constant_pool *pool,
17204 		     rtx value, machine_mode mode)
17205 {
17206   struct mips16_constant **p, *c;
17207   bool first_of_size_p;
17208 
17209   /* See whether the constant is already in the pool.  If so, return the
17210      existing label, otherwise leave P pointing to the place where the
17211      constant should be added.
17212 
17213      Keep the pool sorted in increasing order of mode size so that we can
17214      reduce the number of alignments needed.  */
17215   first_of_size_p = true;
17216   for (p = &pool->first; *p != 0; p = &(*p)->next)
17217     {
17218       if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
17219 	return (*p)->label;
17220       if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
17221 	break;
17222       if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
17223 	first_of_size_p = false;
17224     }
17225 
17226   /* In the worst case, the constant needed by the earliest instruction
17227      will end up at the end of the pool.  The entire pool must then be
17228      accessible from that instruction.
17229 
17230      When adding the first constant, set the pool's highest address to
17231      the address of the first out-of-range byte.  Adjust this address
17232      downwards each time a new constant is added.  */
17233   if (pool->first == 0)
17234     /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
17235        of the instruction with the lowest two bits clear.  The base PC
17236        value for LDPC has the lowest three bits clear.  Assume the worst
17237        case here; namely that the PC-relative instruction occupies the
17238        last 2 bytes in an aligned word.  */
17239     pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
17240   pool->highest_address -= GET_MODE_SIZE (mode);
17241   if (first_of_size_p)
17242     /* Take into account the worst possible padding due to alignment.  */
17243     pool->highest_address -= GET_MODE_SIZE (mode) - 1;
17244 
17245   /* Create a new entry.  */
17246   c = XNEW (struct mips16_constant);
17247   c->value = value;
17248   c->mode = mode;
17249   c->label = gen_label_rtx ();
17250   c->next = *p;
17251   *p = c;
17252 
17253   return c->label;
17254 }
17255 
17256 /* Output constant VALUE after instruction INSN and return the last
17257    instruction emitted.  MODE is the mode of the constant.  */
17258 
17259 static rtx_insn *
mips16_emit_constants_1(machine_mode mode,rtx value,rtx_insn * insn)17260 mips16_emit_constants_1 (machine_mode mode, rtx value, rtx_insn *insn)
17261 {
17262   if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
17263     {
17264       rtx size = GEN_INT (GET_MODE_SIZE (mode));
17265       return emit_insn_after (gen_consttable_int (value, size), insn);
17266     }
17267 
17268   if (SCALAR_FLOAT_MODE_P (mode))
17269     return emit_insn_after (gen_consttable_float (value), insn);
17270 
17271   if (VECTOR_MODE_P (mode))
17272     {
17273       int i;
17274 
17275       for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
17276 	insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
17277 					CONST_VECTOR_ELT (value, i), insn);
17278       return insn;
17279     }
17280 
17281   gcc_unreachable ();
17282 }
17283 
17284 /* Dump out the constants in CONSTANTS after INSN.  Record the initial
17285    label number in the `consttable' and `consttable_end' insns emitted
17286    at the beginning and the end of the constant pool respectively, so
17287    that individual pools can be uniquely marked as data for the purpose
17288    of disassembly.  */
17289 
17290 static void
mips16_emit_constants(struct mips16_constant * constants,rtx_insn * insn)17291 mips16_emit_constants (struct mips16_constant *constants, rtx_insn *insn)
17292 {
17293   int label_num = constants ? CODE_LABEL_NUMBER (constants->label) : 0;
17294   struct mips16_constant *c, *next;
17295   int align;
17296 
17297   align = 0;
17298   if (constants)
17299     insn = emit_insn_after (gen_consttable (GEN_INT (label_num)), insn);
17300   for (c = constants; c != NULL; c = next)
17301     {
17302       /* If necessary, increase the alignment of PC.  */
17303       if (align < GET_MODE_SIZE (c->mode))
17304 	{
17305 	  int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
17306 	  insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
17307 	}
17308       align = GET_MODE_SIZE (c->mode);
17309 
17310       insn = emit_label_after (c->label, insn);
17311       insn = mips16_emit_constants_1 (c->mode, c->value, insn);
17312 
17313       next = c->next;
17314       free (c);
17315     }
17316   if (constants)
17317     insn = emit_insn_after (gen_consttable_end (GEN_INT (label_num)), insn);
17318 
17319   emit_barrier_after (insn);
17320 }
17321 
17322 /* Return the length of instruction INSN.  */
17323 
17324 static int
mips16_insn_length(rtx_insn * insn)17325 mips16_insn_length (rtx_insn *insn)
17326 {
17327   if (JUMP_TABLE_DATA_P (insn))
17328     {
17329       rtx body = PATTERN (insn);
17330       if (GET_CODE (body) == ADDR_VEC)
17331 	return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
17332       else if (GET_CODE (body) == ADDR_DIFF_VEC)
17333 	return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
17334       else
17335 	gcc_unreachable ();
17336     }
17337   return get_attr_length (insn);
17338 }
17339 
17340 /* If *X is a symbolic constant that refers to the constant pool, add
17341    the constant to POOL and rewrite *X to use the constant's label.  */
17342 
17343 static void
mips16_rewrite_pool_constant(struct mips16_constant_pool * pool,rtx * x)17344 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
17345 {
17346   rtx base, offset;
17347   rtx_code_label *label;
17348 
17349   split_const (*x, &base, &offset);
17350   if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
17351     {
17352       label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
17353 				   get_pool_mode (base));
17354       base = gen_rtx_LABEL_REF (Pmode, label);
17355       *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
17356     }
17357 }
17358 
17359 /* Rewrite INSN so that constant pool references refer to the constant's
17360    label instead.  */
17361 
17362 static void
mips16_rewrite_pool_refs(rtx_insn * insn,struct mips16_constant_pool * pool)17363 mips16_rewrite_pool_refs (rtx_insn *insn, struct mips16_constant_pool *pool)
17364 {
17365   subrtx_ptr_iterator::array_type array;
17366   FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), ALL)
17367     {
17368       rtx *loc = *iter;
17369 
17370       if (force_to_mem_operand (*loc, Pmode))
17371 	{
17372 	  rtx mem = force_const_mem (GET_MODE (*loc), *loc);
17373 	  validate_change (insn, loc, mem, false);
17374 	}
17375 
17376       if (MEM_P (*loc))
17377 	{
17378 	  mips16_rewrite_pool_constant (pool, &XEXP (*loc, 0));
17379 	  iter.skip_subrtxes ();
17380 	}
17381       else
17382 	{
17383 	  if (TARGET_MIPS16_TEXT_LOADS)
17384 	    mips16_rewrite_pool_constant (pool, loc);
17385 	  if (GET_CODE (*loc) == CONST
17386 	      /* Don't rewrite the __mips16_rdwr symbol.  */
17387 	      || (GET_CODE (*loc) == UNSPEC
17388 		  && XINT (*loc, 1) == UNSPEC_TLS_GET_TP))
17389 	    iter.skip_subrtxes ();
17390 	}
17391     }
17392 }
17393 
17394 /* Return whether CFG is used in mips_reorg.  */
17395 
17396 static bool
mips_cfg_in_reorg(void)17397 mips_cfg_in_reorg (void)
17398 {
17399   return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
17400 	  || TARGET_RELAX_PIC_CALLS);
17401 }
17402 
17403 /* Build MIPS16 constant pools.  Split the instructions if SPLIT_P,
17404    otherwise assume that they are already split.  */
17405 
17406 static void
mips16_lay_out_constants(bool split_p)17407 mips16_lay_out_constants (bool split_p)
17408 {
17409   struct mips16_constant_pool pool;
17410   rtx_insn *insn, *barrier;
17411 
17412   if (!TARGET_MIPS16_PCREL_LOADS)
17413     return;
17414 
17415   if (split_p)
17416     {
17417       if (mips_cfg_in_reorg ())
17418 	split_all_insns ();
17419       else
17420 	split_all_insns_noflow ();
17421     }
17422   barrier = 0;
17423   memset (&pool, 0, sizeof (pool));
17424   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17425     {
17426       /* Rewrite constant pool references in INSN.  */
17427       if (USEFUL_INSN_P (insn))
17428 	mips16_rewrite_pool_refs (insn, &pool);
17429 
17430       pool.insn_address += mips16_insn_length (insn);
17431 
17432       if (pool.first != NULL)
17433 	{
17434 	  /* If there are no natural barriers between the first user of
17435 	     the pool and the highest acceptable address, we'll need to
17436 	     create a new instruction to jump around the constant pool.
17437 	     In the worst case, this instruction will be 4 bytes long.
17438 
17439 	     If it's too late to do this transformation after INSN,
17440 	     do it immediately before INSN.  */
17441 	  if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
17442 	    {
17443 	      rtx_code_label *label;
17444 	      rtx_insn *jump;
17445 
17446 	      label = gen_label_rtx ();
17447 
17448 	      jump = emit_jump_insn_before (gen_jump (label), insn);
17449 	      JUMP_LABEL (jump) = label;
17450 	      LABEL_NUSES (label) = 1;
17451 	      barrier = emit_barrier_after (jump);
17452 
17453 	      emit_label_after (label, barrier);
17454 	      pool.insn_address += 4;
17455 	    }
17456 
17457 	  /* See whether the constant pool is now out of range of the first
17458 	     user.  If so, output the constants after the previous barrier.
17459 	     Note that any instructions between BARRIER and INSN (inclusive)
17460 	     will use negative offsets to refer to the pool.  */
17461 	  if (pool.insn_address > pool.highest_address)
17462 	    {
17463 	      mips16_emit_constants (pool.first, barrier);
17464 	      pool.first = NULL;
17465 	      barrier = 0;
17466 	    }
17467 	  else if (BARRIER_P (insn))
17468 	    barrier = insn;
17469 	}
17470     }
17471   mips16_emit_constants (pool.first, get_last_insn ());
17472 }
17473 
17474 /* Return true if it is worth r10k_simplify_address's while replacing
17475    an address with X.  We are looking for constants, and for addresses
17476    at a known offset from the incoming stack pointer.  */
17477 
17478 static bool
r10k_simplified_address_p(rtx x)17479 r10k_simplified_address_p (rtx x)
17480 {
17481   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
17482     x = XEXP (x, 0);
17483   return x == virtual_incoming_args_rtx || CONSTANT_P (x);
17484 }
17485 
17486 /* X is an expression that appears in INSN.  Try to use the UD chains
17487    to simplify it, returning the simplified form on success and the
17488    original form otherwise.  Replace the incoming value of $sp with
17489    virtual_incoming_args_rtx (which should never occur in X otherwise).  */
17490 
17491 static rtx
r10k_simplify_address(rtx x,rtx_insn * insn)17492 r10k_simplify_address (rtx x, rtx_insn *insn)
17493 {
17494   rtx newx, op0, op1, set, note;
17495   rtx_insn *def_insn;
17496   df_ref use, def;
17497   struct df_link *defs;
17498 
17499   newx = NULL_RTX;
17500   if (UNARY_P (x))
17501     {
17502       op0 = r10k_simplify_address (XEXP (x, 0), insn);
17503       if (op0 != XEXP (x, 0))
17504 	newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
17505 				   op0, GET_MODE (XEXP (x, 0)));
17506     }
17507   else if (BINARY_P (x))
17508     {
17509       op0 = r10k_simplify_address (XEXP (x, 0), insn);
17510       op1 = r10k_simplify_address (XEXP (x, 1), insn);
17511       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
17512 	newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
17513     }
17514   else if (GET_CODE (x) == LO_SUM)
17515     {
17516       /* LO_SUMs can be offset from HIGHs, if we know they won't
17517 	 overflow.  See mips_classify_address for the rationale behind
17518 	 the lax check.  */
17519       op0 = r10k_simplify_address (XEXP (x, 0), insn);
17520       if (GET_CODE (op0) == HIGH)
17521 	newx = XEXP (x, 1);
17522     }
17523   else if (REG_P (x))
17524     {
17525       /* Uses are recorded by regno_reg_rtx, not X itself.  */
17526       use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
17527       gcc_assert (use);
17528       defs = DF_REF_CHAIN (use);
17529 
17530       /* Require a single definition.  */
17531       if (defs && defs->next == NULL)
17532 	{
17533 	  def = defs->ref;
17534 	  if (DF_REF_IS_ARTIFICIAL (def))
17535 	    {
17536 	      /* Replace the incoming value of $sp with
17537 		 virtual_incoming_args_rtx.  */
17538 	      if (x == stack_pointer_rtx
17539 		  && DF_REF_BB (def) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
17540 		newx = virtual_incoming_args_rtx;
17541 	    }
17542 	  else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
17543 				   DF_REF_BB (def)))
17544 	    {
17545 	      /* Make sure that DEF_INSN is a single set of REG.  */
17546 	      def_insn = DF_REF_INSN (def);
17547 	      if (NONJUMP_INSN_P (def_insn))
17548 		{
17549 		  set = single_set (def_insn);
17550 		  if (set && rtx_equal_p (SET_DEST (set), x))
17551 		    {
17552 		      /* Prefer to use notes, since the def-use chains
17553 			 are often shorter.  */
17554 		      note = find_reg_equal_equiv_note (def_insn);
17555 		      if (note)
17556 			newx = XEXP (note, 0);
17557 		      else
17558 			newx = SET_SRC (set);
17559 		      newx = r10k_simplify_address (newx, def_insn);
17560 		    }
17561 		}
17562 	    }
17563 	}
17564     }
17565   if (newx && r10k_simplified_address_p (newx))
17566     return newx;
17567   return x;
17568 }
17569 
17570 /* Return true if ADDRESS is known to be an uncached address
17571    on R10K systems.  */
17572 
17573 static bool
r10k_uncached_address_p(unsigned HOST_WIDE_INT address)17574 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
17575 {
17576   unsigned HOST_WIDE_INT upper;
17577 
17578   /* Check for KSEG1.  */
17579   if (address + 0x60000000 < 0x20000000)
17580     return true;
17581 
17582   /* Check for uncached XKPHYS addresses.  */
17583   if (Pmode == DImode)
17584     {
17585       upper = (address >> 40) & 0xf9ffff;
17586       if (upper == 0x900000 || upper == 0xb80000)
17587 	return true;
17588     }
17589   return false;
17590 }
17591 
17592 /* Return true if we can prove that an access to address X in instruction
17593    INSN would be safe from R10K speculation.  This X is a general
17594    expression; it might not be a legitimate address.  */
17595 
17596 static bool
r10k_safe_address_p(rtx x,rtx_insn * insn)17597 r10k_safe_address_p (rtx x, rtx_insn *insn)
17598 {
17599   rtx base, offset;
17600   HOST_WIDE_INT offset_val;
17601 
17602   x = r10k_simplify_address (x, insn);
17603 
17604   /* Check for references to the stack frame.  It doesn't really matter
17605      how much of the frame has been allocated at INSN; -mr10k-cache-barrier
17606      allows us to assume that accesses to any part of the eventual frame
17607      is safe from speculation at any point in the function.  */
17608   mips_split_plus (x, &base, &offset_val);
17609   if (base == virtual_incoming_args_rtx
17610       && offset_val >= -cfun->machine->frame.total_size
17611       && offset_val < cfun->machine->frame.args_size)
17612     return true;
17613 
17614   /* Check for uncached addresses.  */
17615   if (CONST_INT_P (x))
17616     return r10k_uncached_address_p (INTVAL (x));
17617 
17618   /* Check for accesses to a static object.  */
17619   split_const (x, &base, &offset);
17620   return offset_within_block_p (base, INTVAL (offset));
17621 }
17622 
17623 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
17624    an in-range access to an automatic variable, or to an object with
17625    a link-time-constant address.  */
17626 
17627 static bool
r10k_safe_mem_expr_p(tree expr,unsigned HOST_WIDE_INT offset)17628 r10k_safe_mem_expr_p (tree expr, unsigned HOST_WIDE_INT offset)
17629 {
17630   poly_int64 bitoffset, bitsize;
17631   tree inner, var_offset;
17632   machine_mode mode;
17633   int unsigned_p, reverse_p, volatile_p;
17634 
17635   inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
17636 			       &unsigned_p, &reverse_p, &volatile_p);
17637   if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
17638     return false;
17639 
17640   offset += bitoffset / BITS_PER_UNIT;
17641   return offset < tree_to_uhwi (DECL_SIZE_UNIT (inner));
17642 }
17643 
17644 /* Return true if X contains a MEM that is not safe from R10K speculation.
17645    INSN is the instruction that contains X.  */
17646 
17647 static bool
r10k_needs_protection_p_1(rtx x,rtx_insn * insn)17648 r10k_needs_protection_p_1 (rtx x, rtx_insn *insn)
17649 {
17650   subrtx_var_iterator::array_type array;
17651   FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
17652     {
17653       rtx mem = *iter;
17654       if (MEM_P (mem))
17655 	{
17656 	  if ((MEM_EXPR (mem)
17657 	       && MEM_OFFSET_KNOWN_P (mem)
17658 	       && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
17659 	      || r10k_safe_address_p (XEXP (mem, 0), insn))
17660 	    iter.skip_subrtxes ();
17661 	  else
17662 	    return true;
17663 	}
17664     }
17665   return false;
17666 }
17667 
17668 /* A note_stores callback for which DATA points to an instruction pointer.
17669    If *DATA is nonnull, make it null if it X contains a MEM that is not
17670    safe from R10K speculation.  */
17671 
17672 static void
r10k_needs_protection_p_store(rtx x,const_rtx pat ATTRIBUTE_UNUSED,void * data)17673 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
17674 			       void *data)
17675 {
17676   rtx_insn **insn_ptr;
17677 
17678   insn_ptr = (rtx_insn **) data;
17679   if (*insn_ptr && r10k_needs_protection_p_1 (x, *insn_ptr))
17680     *insn_ptr = NULL;
17681 }
17682 
17683 /* X is the pattern of a call instruction.  Return true if the call is
17684    not to a declared function.  */
17685 
17686 static bool
r10k_needs_protection_p_call(const_rtx x)17687 r10k_needs_protection_p_call (const_rtx x)
17688 {
17689   subrtx_iterator::array_type array;
17690   FOR_EACH_SUBRTX (iter, array, x, NONCONST)
17691     {
17692       const_rtx mem = *iter;
17693       if (MEM_P (mem))
17694 	{
17695 	  const_rtx addr = XEXP (mem, 0);
17696 	  if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DECL (addr))
17697 	    iter.skip_subrtxes ();
17698 	  else
17699 	    return true;
17700 	}
17701     }
17702   return false;
17703 }
17704 
17705 /* Return true if instruction INSN needs to be protected by an R10K
17706    cache barrier.  */
17707 
17708 static bool
r10k_needs_protection_p(rtx_insn * insn)17709 r10k_needs_protection_p (rtx_insn *insn)
17710 {
17711   if (CALL_P (insn))
17712     return r10k_needs_protection_p_call (PATTERN (insn));
17713 
17714   if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
17715     {
17716       note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
17717       return insn == NULL_RTX;
17718     }
17719 
17720   return r10k_needs_protection_p_1 (PATTERN (insn), insn);
17721 }
17722 
17723 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
17724    edge is unconditional.  */
17725 
17726 static bool
r10k_protected_bb_p(basic_block bb,sbitmap protected_bbs)17727 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
17728 {
17729   edge_iterator ei;
17730   edge e;
17731 
17732   FOR_EACH_EDGE (e, ei, bb->preds)
17733     if (!single_succ_p (e->src)
17734 	|| !bitmap_bit_p (protected_bbs, e->src->index)
17735 	|| (e->flags & EDGE_COMPLEX) != 0)
17736       return false;
17737   return true;
17738 }
17739 
17740 /* Implement -mr10k-cache-barrier= for the current function.  */
17741 
17742 static void
r10k_insert_cache_barriers(void)17743 r10k_insert_cache_barriers (void)
17744 {
17745   int *rev_post_order;
17746   unsigned int i, n;
17747   basic_block bb;
17748   sbitmap protected_bbs;
17749   rtx_insn *insn, *end;
17750   rtx unprotected_region;
17751 
17752   if (TARGET_MIPS16)
17753     {
17754       sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
17755       return;
17756     }
17757 
17758   /* Calculate dominators.  */
17759   calculate_dominance_info (CDI_DOMINATORS);
17760 
17761   /* Bit X of PROTECTED_BBS is set if the last operation in basic block
17762      X is protected by a cache barrier.  */
17763   protected_bbs = sbitmap_alloc (last_basic_block_for_fn (cfun));
17764   bitmap_clear (protected_bbs);
17765 
17766   /* Iterate over the basic blocks in reverse post-order.  */
17767   rev_post_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
17768   n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
17769   for (i = 0; i < n; i++)
17770     {
17771       bb = BASIC_BLOCK_FOR_FN (cfun, rev_post_order[i]);
17772 
17773       /* If this block is only reached by unconditional edges, and if the
17774 	 source of every edge is protected, the beginning of the block is
17775 	 also protected.  */
17776       if (r10k_protected_bb_p (bb, protected_bbs))
17777 	unprotected_region = NULL_RTX;
17778       else
17779 	unprotected_region = pc_rtx;
17780       end = NEXT_INSN (BB_END (bb));
17781 
17782       /* UNPROTECTED_REGION is:
17783 
17784 	 - null if we are processing a protected region,
17785 	 - pc_rtx if we are processing an unprotected region but have
17786 	   not yet found the first instruction in it
17787 	 - the first instruction in an unprotected region otherwise.  */
17788       for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
17789 	{
17790 	  if (unprotected_region && USEFUL_INSN_P (insn))
17791 	    {
17792 	      if (recog_memoized (insn) == CODE_FOR_mips_cache)
17793 		/* This CACHE instruction protects the following code.  */
17794 		unprotected_region = NULL_RTX;
17795 	      else
17796 		{
17797 		  /* See if INSN is the first instruction in this
17798 		     unprotected region.  */
17799 		  if (unprotected_region == pc_rtx)
17800 		    unprotected_region = insn;
17801 
17802 		  /* See if INSN needs to be protected.  If so,
17803 		     we must insert a cache barrier somewhere between
17804 		     PREV_INSN (UNPROTECTED_REGION) and INSN.  It isn't
17805 		     clear which position is better performance-wise,
17806 		     but as a tie-breaker, we assume that it is better
17807 		     to allow delay slots to be back-filled where
17808 		     possible, and that it is better not to insert
17809 		     barriers in the middle of already-scheduled code.
17810 		     We therefore insert the barrier at the beginning
17811 		     of the region.  */
17812 		  if (r10k_needs_protection_p (insn))
17813 		    {
17814 		      emit_insn_before (gen_r10k_cache_barrier (),
17815 					unprotected_region);
17816 		      unprotected_region = NULL_RTX;
17817 		    }
17818 		}
17819 	    }
17820 
17821 	  if (CALL_P (insn))
17822 	    /* The called function is not required to protect the exit path.
17823 	       The code that follows a call is therefore unprotected.  */
17824 	    unprotected_region = pc_rtx;
17825 	}
17826 
17827       /* Record whether the end of this block is protected.  */
17828       if (unprotected_region == NULL_RTX)
17829 	bitmap_set_bit (protected_bbs, bb->index);
17830     }
17831   XDELETEVEC (rev_post_order);
17832 
17833   sbitmap_free (protected_bbs);
17834 
17835   free_dominance_info (CDI_DOMINATORS);
17836 }
17837 
17838 /* If INSN is a call, return the underlying CALL expr.  Return NULL_RTX
17839    otherwise.  If INSN has two call rtx, then store the second one in
17840    SECOND_CALL.  */
17841 
17842 static rtx
mips_call_expr_from_insn(rtx_insn * insn,rtx * second_call)17843 mips_call_expr_from_insn (rtx_insn *insn, rtx *second_call)
17844 {
17845   rtx x;
17846   rtx x2;
17847 
17848   if (!CALL_P (insn))
17849     return NULL_RTX;
17850 
17851   x = PATTERN (insn);
17852   if (GET_CODE (x) == PARALLEL)
17853     {
17854       /* Calls returning complex values have two CALL rtx.  Look for the second
17855 	 one here, and return it via the SECOND_CALL arg.  */
17856       x2 = XVECEXP (x, 0, 1);
17857       if (GET_CODE (x2) == SET)
17858 	x2 = XEXP (x2, 1);
17859       if (GET_CODE (x2) == CALL)
17860 	*second_call = x2;
17861 
17862       x = XVECEXP (x, 0, 0);
17863     }
17864   if (GET_CODE (x) == SET)
17865     x = XEXP (x, 1);
17866   gcc_assert (GET_CODE (x) == CALL);
17867 
17868   return x;
17869 }
17870 
17871 /* REG is set in DEF.  See if the definition is one of the ways we load a
17872    register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
17873    If it is, return the symbol reference of the function, otherwise return
17874    NULL_RTX.
17875 
17876    If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
17877    the values of source registers, otherwise treat such registers as
17878    having an unknown value.  */
17879 
17880 static rtx
mips_pic_call_symbol_from_set(df_ref def,rtx reg,bool recurse_p)17881 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
17882 {
17883   rtx_insn *def_insn;
17884   rtx set;
17885 
17886   if (DF_REF_IS_ARTIFICIAL (def))
17887     return NULL_RTX;
17888 
17889   def_insn = DF_REF_INSN (def);
17890   set = single_set (def_insn);
17891   if (set && rtx_equal_p (SET_DEST (set), reg))
17892     {
17893       rtx note, src, symbol;
17894 
17895       /* First see whether the source is a plain symbol.  This is used
17896 	 when calling symbols that are not lazily bound.  */
17897       src = SET_SRC (set);
17898       if (GET_CODE (src) == SYMBOL_REF)
17899 	return src;
17900 
17901       /* Handle %call16 references.  */
17902       symbol = mips_strip_unspec_call (src);
17903       if (symbol)
17904 	{
17905 	  gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
17906 	  return symbol;
17907 	}
17908 
17909       /* If we have something more complicated, look for a
17910 	 REG_EQUAL or REG_EQUIV note.  */
17911       note = find_reg_equal_equiv_note (def_insn);
17912       if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
17913 	return XEXP (note, 0);
17914 
17915       /* Follow at most one simple register copy.  Such copies are
17916 	 interesting in cases like:
17917 
17918 	     for (...)
17919 	       {
17920 	         locally_binding_fn (...);
17921 	       }
17922 
17923 	 and:
17924 
17925 	     locally_binding_fn (...);
17926 	     ...
17927 	     locally_binding_fn (...);
17928 
17929 	 where the load of locally_binding_fn can legitimately be
17930 	 hoisted or shared.  However, we do not expect to see complex
17931 	 chains of copies, so a full worklist solution to the problem
17932 	 would probably be overkill.  */
17933       if (recurse_p && REG_P (src))
17934 	return mips_find_pic_call_symbol (def_insn, src, false);
17935     }
17936 
17937   return NULL_RTX;
17938 }
17939 
17940 /* Find the definition of the use of REG in INSN.  See if the definition
17941    is one of the ways we load a register with a symbol address for a
17942    mips_use_pic_fn_addr_reg_p call.  If it is return the symbol reference
17943    of the function, otherwise return NULL_RTX.  RECURSE_P is as for
17944    mips_pic_call_symbol_from_set.  */
17945 
17946 static rtx
mips_find_pic_call_symbol(rtx_insn * insn,rtx reg,bool recurse_p)17947 mips_find_pic_call_symbol (rtx_insn *insn, rtx reg, bool recurse_p)
17948 {
17949   df_ref use;
17950   struct df_link *defs;
17951   rtx symbol;
17952 
17953   use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
17954   if (!use)
17955     return NULL_RTX;
17956   defs = DF_REF_CHAIN (use);
17957   if (!defs)
17958     return NULL_RTX;
17959   symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
17960   if (!symbol)
17961     return NULL_RTX;
17962 
17963   /* If we have more than one definition, they need to be identical.  */
17964   for (defs = defs->next; defs; defs = defs->next)
17965     {
17966       rtx other;
17967 
17968       other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
17969       if (!rtx_equal_p (symbol, other))
17970 	return NULL_RTX;
17971     }
17972 
17973   return symbol;
17974 }
17975 
17976 /* Replace the args_size operand of the call expression CALL with the
17977    call-attribute UNSPEC and fill in SYMBOL as the function symbol.  */
17978 
17979 static void
mips_annotate_pic_call_expr(rtx call,rtx symbol)17980 mips_annotate_pic_call_expr (rtx call, rtx symbol)
17981 {
17982   rtx args_size;
17983 
17984   args_size = XEXP (call, 1);
17985   XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
17986 				   gen_rtvec (2, args_size, symbol),
17987 				   UNSPEC_CALL_ATTR);
17988 }
17989 
17990 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression.  See
17991    if instead of the arg_size argument it contains the call attributes.  If
17992    yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
17993    symbol from the call attributes.  Also return false if ARGS_SIZE_OPNO is
17994    -1.  */
17995 
17996 bool
mips_get_pic_call_symbol(rtx * operands,int args_size_opno)17997 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
17998 {
17999   rtx args_size, symbol;
18000 
18001   if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
18002     return false;
18003 
18004   args_size = operands[args_size_opno];
18005   if (GET_CODE (args_size) != UNSPEC)
18006     return false;
18007   gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
18008 
18009   symbol = XVECEXP (args_size, 0, 1);
18010   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
18011 
18012   operands[args_size_opno] = symbol;
18013   return true;
18014 }
18015 
18016 /* Use DF to annotate PIC indirect calls with the function symbol they
18017    dispatch to.  */
18018 
18019 static void
mips_annotate_pic_calls(void)18020 mips_annotate_pic_calls (void)
18021 {
18022   basic_block bb;
18023   rtx_insn *insn;
18024 
18025   FOR_EACH_BB_FN (bb, cfun)
18026     FOR_BB_INSNS (bb, insn)
18027     {
18028       rtx call, reg, symbol, second_call;
18029 
18030       second_call = 0;
18031       call = mips_call_expr_from_insn (insn, &second_call);
18032       if (!call)
18033 	continue;
18034       gcc_assert (MEM_P (XEXP (call, 0)));
18035       reg = XEXP (XEXP (call, 0), 0);
18036       if (!REG_P (reg))
18037 	continue;
18038 
18039       symbol = mips_find_pic_call_symbol (insn, reg, true);
18040       if (symbol)
18041 	{
18042 	  mips_annotate_pic_call_expr (call, symbol);
18043 	  if (second_call)
18044 	    mips_annotate_pic_call_expr (second_call, symbol);
18045 	}
18046     }
18047 }
18048 
18049 /* A temporary variable used by note_uses callbacks, etc.  */
18050 static rtx_insn *mips_sim_insn;
18051 
18052 /* A structure representing the state of the processor pipeline.
18053    Used by the mips_sim_* family of functions.  */
18054 struct mips_sim {
18055   /* The maximum number of instructions that can be issued in a cycle.
18056      (Caches mips_issue_rate.)  */
18057   unsigned int issue_rate;
18058 
18059   /* The current simulation time.  */
18060   unsigned int time;
18061 
18062   /* How many more instructions can be issued in the current cycle.  */
18063   unsigned int insns_left;
18064 
18065   /* LAST_SET[X].INSN is the last instruction to set register X.
18066      LAST_SET[X].TIME is the time at which that instruction was issued.
18067      INSN is null if no instruction has yet set register X.  */
18068   struct {
18069     rtx_insn *insn;
18070     unsigned int time;
18071   } last_set[FIRST_PSEUDO_REGISTER];
18072 
18073   /* The pipeline's current DFA state.  */
18074   state_t dfa_state;
18075 };
18076 
18077 /* Reset STATE to the initial simulation state.  */
18078 
18079 static void
mips_sim_reset(struct mips_sim * state)18080 mips_sim_reset (struct mips_sim *state)
18081 {
18082   curr_state = state->dfa_state;
18083 
18084   state->time = 0;
18085   state->insns_left = state->issue_rate;
18086   memset (&state->last_set, 0, sizeof (state->last_set));
18087   state_reset (curr_state);
18088 
18089   targetm.sched.init (0, false, 0);
18090   advance_state (curr_state);
18091 }
18092 
18093 /* Initialize STATE before its first use.  DFA_STATE points to an
18094    allocated but uninitialized DFA state.  */
18095 
18096 static void
mips_sim_init(struct mips_sim * state,state_t dfa_state)18097 mips_sim_init (struct mips_sim *state, state_t dfa_state)
18098 {
18099   if (targetm.sched.init_dfa_pre_cycle_insn)
18100     targetm.sched.init_dfa_pre_cycle_insn ();
18101 
18102   if (targetm.sched.init_dfa_post_cycle_insn)
18103     targetm.sched.init_dfa_post_cycle_insn ();
18104 
18105   state->issue_rate = mips_issue_rate ();
18106   state->dfa_state = dfa_state;
18107   mips_sim_reset (state);
18108 }
18109 
18110 /* Advance STATE by one clock cycle.  */
18111 
18112 static void
mips_sim_next_cycle(struct mips_sim * state)18113 mips_sim_next_cycle (struct mips_sim *state)
18114 {
18115   curr_state = state->dfa_state;
18116 
18117   state->time++;
18118   state->insns_left = state->issue_rate;
18119   advance_state (curr_state);
18120 }
18121 
18122 /* Advance simulation state STATE until instruction INSN can read
18123    register REG.  */
18124 
18125 static void
mips_sim_wait_reg(struct mips_sim * state,rtx_insn * insn,rtx reg)18126 mips_sim_wait_reg (struct mips_sim *state, rtx_insn *insn, rtx reg)
18127 {
18128   unsigned int regno, end_regno;
18129 
18130   end_regno = END_REGNO (reg);
18131   for (regno = REGNO (reg); regno < end_regno; regno++)
18132     if (state->last_set[regno].insn != 0)
18133       {
18134 	unsigned int t;
18135 
18136 	t = (state->last_set[regno].time
18137 	     + insn_latency (state->last_set[regno].insn, insn));
18138 	while (state->time < t)
18139 	  mips_sim_next_cycle (state);
18140     }
18141 }
18142 
18143 /* A note_uses callback.  For each register in *X, advance simulation
18144    state DATA until mips_sim_insn can read the register's value.  */
18145 
18146 static void
mips_sim_wait_regs_1(rtx * x,void * data)18147 mips_sim_wait_regs_1 (rtx *x, void *data)
18148 {
18149   subrtx_var_iterator::array_type array;
18150   FOR_EACH_SUBRTX_VAR (iter, array, *x, NONCONST)
18151     if (REG_P (*iter))
18152       mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *iter);
18153 }
18154 
18155 /* Advance simulation state STATE until all of INSN's register
18156    dependencies are satisfied.  */
18157 
18158 static void
mips_sim_wait_regs(struct mips_sim * state,rtx_insn * insn)18159 mips_sim_wait_regs (struct mips_sim *state, rtx_insn *insn)
18160 {
18161   mips_sim_insn = insn;
18162   note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
18163 }
18164 
18165 /* Advance simulation state STATE until the units required by
18166    instruction INSN are available.  */
18167 
18168 static void
mips_sim_wait_units(struct mips_sim * state,rtx_insn * insn)18169 mips_sim_wait_units (struct mips_sim *state, rtx_insn *insn)
18170 {
18171   state_t tmp_state;
18172 
18173   tmp_state = alloca (state_size ());
18174   while (state->insns_left == 0
18175 	 || (memcpy (tmp_state, state->dfa_state, state_size ()),
18176 	     state_transition (tmp_state, insn) >= 0))
18177     mips_sim_next_cycle (state);
18178 }
18179 
18180 /* Advance simulation state STATE until INSN is ready to issue.  */
18181 
18182 static void
mips_sim_wait_insn(struct mips_sim * state,rtx_insn * insn)18183 mips_sim_wait_insn (struct mips_sim *state, rtx_insn *insn)
18184 {
18185   mips_sim_wait_regs (state, insn);
18186   mips_sim_wait_units (state, insn);
18187 }
18188 
18189 /* mips_sim_insn has just set X.  Update the LAST_SET array
18190    in simulation state DATA.  */
18191 
18192 static void
mips_sim_record_set(rtx x,const_rtx pat ATTRIBUTE_UNUSED,void * data)18193 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
18194 {
18195   struct mips_sim *state;
18196 
18197   state = (struct mips_sim *) data;
18198   if (REG_P (x))
18199     {
18200       unsigned int regno, end_regno;
18201 
18202       end_regno = END_REGNO (x);
18203       for (regno = REGNO (x); regno < end_regno; regno++)
18204 	{
18205 	  state->last_set[regno].insn = mips_sim_insn;
18206 	  state->last_set[regno].time = state->time;
18207 	}
18208     }
18209 }
18210 
18211 /* Issue instruction INSN in scheduler state STATE.  Assume that INSN
18212    can issue immediately (i.e., that mips_sim_wait_insn has already
18213    been called).  */
18214 
18215 static void
mips_sim_issue_insn(struct mips_sim * state,rtx_insn * insn)18216 mips_sim_issue_insn (struct mips_sim *state, rtx_insn *insn)
18217 {
18218   curr_state = state->dfa_state;
18219 
18220   state_transition (curr_state, insn);
18221   state->insns_left = targetm.sched.variable_issue (0, false, insn,
18222 						    state->insns_left);
18223 
18224   mips_sim_insn = insn;
18225   note_stores (PATTERN (insn), mips_sim_record_set, state);
18226 }
18227 
18228 /* Simulate issuing a NOP in state STATE.  */
18229 
18230 static void
mips_sim_issue_nop(struct mips_sim * state)18231 mips_sim_issue_nop (struct mips_sim *state)
18232 {
18233   if (state->insns_left == 0)
18234     mips_sim_next_cycle (state);
18235   state->insns_left--;
18236 }
18237 
18238 /* Update simulation state STATE so that it's ready to accept the instruction
18239    after INSN.  INSN should be part of the main rtl chain, not a member of a
18240    SEQUENCE.  */
18241 
18242 static void
mips_sim_finish_insn(struct mips_sim * state,rtx_insn * insn)18243 mips_sim_finish_insn (struct mips_sim *state, rtx_insn *insn)
18244 {
18245   /* If INSN is a jump with an implicit delay slot, simulate a nop.  */
18246   if (JUMP_P (insn))
18247     mips_sim_issue_nop (state);
18248 
18249   switch (GET_CODE (SEQ_BEGIN (insn)))
18250     {
18251     case CODE_LABEL:
18252     case CALL_INSN:
18253       /* We can't predict the processor state after a call or label.  */
18254       mips_sim_reset (state);
18255       break;
18256 
18257     case JUMP_INSN:
18258       /* The delay slots of branch likely instructions are only executed
18259 	 when the branch is taken.  Therefore, if the caller has simulated
18260 	 the delay slot instruction, STATE does not really reflect the state
18261 	 of the pipeline for the instruction after the delay slot.  Also,
18262 	 branch likely instructions tend to incur a penalty when not taken,
18263 	 so there will probably be an extra delay between the branch and
18264 	 the instruction after the delay slot.  */
18265       if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
18266 	mips_sim_reset (state);
18267       break;
18268 
18269     default:
18270       break;
18271     }
18272 }
18273 
18274 /* Use simulator state STATE to calculate the execution time of
18275    instruction sequence SEQ.  */
18276 
18277 static unsigned int
mips_seq_time(struct mips_sim * state,rtx_insn * seq)18278 mips_seq_time (struct mips_sim *state, rtx_insn *seq)
18279 {
18280   mips_sim_reset (state);
18281   for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
18282     {
18283       mips_sim_wait_insn (state, insn);
18284       mips_sim_issue_insn (state, insn);
18285     }
18286   return state->time;
18287 }
18288 
18289 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
18290    setting SETTING, using STATE to simulate instruction sequences.  */
18291 
18292 static unsigned int
mips_mult_zero_zero_cost(struct mips_sim * state,bool setting)18293 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
18294 {
18295   mips_tuning_info.fast_mult_zero_zero_p = setting;
18296   start_sequence ();
18297 
18298   machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
18299   rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
18300   mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
18301 
18302   /* If the target provides mulsidi3_32bit then that's the most likely
18303      consumer of the result.  Test for bypasses.  */
18304   if (dword_mode == DImode && HAVE_maddsidi4)
18305     {
18306       rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
18307       emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
18308     }
18309 
18310   unsigned int time = mips_seq_time (state, get_insns ());
18311   end_sequence ();
18312   return time;
18313 }
18314 
18315 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
18316    and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
18317    Prefer MULT -- which is shorter -- in the event of a tie.  */
18318 
18319 static void
mips_set_fast_mult_zero_zero_p(struct mips_sim * state)18320 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
18321 {
18322   if (TARGET_MIPS16 || !ISA_HAS_HILO)
18323     /* No MTLO or MTHI available for MIPS16. Also, when there are no HI or LO
18324        registers then there is no reason to zero them, arbitrarily choose to
18325        say that "MULT $0,$0" would be faster.  */
18326     mips_tuning_info.fast_mult_zero_zero_p = true;
18327   else
18328     {
18329       unsigned int true_time = mips_mult_zero_zero_cost (state, true);
18330       unsigned int false_time = mips_mult_zero_zero_cost (state, false);
18331       mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
18332     }
18333 }
18334 
18335 /* Set up costs based on the current architecture and tuning settings.  */
18336 
18337 static void
mips_set_tuning_info(void)18338 mips_set_tuning_info (void)
18339 {
18340   if (mips_tuning_info.initialized_p
18341       && mips_tuning_info.arch == mips_arch
18342       && mips_tuning_info.tune == mips_tune
18343       && mips_tuning_info.mips16_p == TARGET_MIPS16)
18344     return;
18345 
18346   mips_tuning_info.arch = mips_arch;
18347   mips_tuning_info.tune = mips_tune;
18348   mips_tuning_info.mips16_p = TARGET_MIPS16;
18349   mips_tuning_info.initialized_p = true;
18350 
18351   dfa_start ();
18352 
18353   struct mips_sim state;
18354   mips_sim_init (&state, alloca (state_size ()));
18355 
18356   mips_set_fast_mult_zero_zero_p (&state);
18357 
18358   dfa_finish ();
18359 }
18360 
18361 /* Implement TARGET_EXPAND_TO_RTL_HOOK.  */
18362 
18363 static void
mips_expand_to_rtl_hook(void)18364 mips_expand_to_rtl_hook (void)
18365 {
18366   /* We need to call this at a point where we can safely create sequences
18367      of instructions, so TARGET_OVERRIDE_OPTIONS is too early.  We also
18368      need to call it at a point where the DFA infrastructure is not
18369      already in use, so we can't just call it lazily on demand.
18370 
18371      At present, mips_tuning_info is only needed during post-expand
18372      RTL passes such as split_insns, so this hook should be early enough.
18373      We may need to move the call elsewhere if mips_tuning_info starts
18374      to be used for other things (such as rtx_costs, or expanders that
18375      could be called during gimple optimization).  */
18376   mips_set_tuning_info ();
18377 }
18378 
18379 /* The VR4130 pipeline issues aligned pairs of instructions together,
18380    but it stalls the second instruction if it depends on the first.
18381    In order to cut down the amount of logic required, this dependence
18382    check is not based on a full instruction decode.  Instead, any non-SPECIAL
18383    instruction is assumed to modify the register specified by bits 20-16
18384    (which is usually the "rt" field).
18385 
18386    In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
18387    input, so we can end up with a false dependence between the branch
18388    and its delay slot.  If this situation occurs in instruction INSN,
18389    try to avoid it by swapping rs and rt.  */
18390 
18391 static void
vr4130_avoid_branch_rt_conflict(rtx_insn * insn)18392 vr4130_avoid_branch_rt_conflict (rtx_insn *insn)
18393 {
18394   rtx_insn *first, *second;
18395 
18396   first = SEQ_BEGIN (insn);
18397   second = SEQ_END (insn);
18398   if (JUMP_P (first)
18399       && NONJUMP_INSN_P (second)
18400       && GET_CODE (PATTERN (first)) == SET
18401       && GET_CODE (SET_DEST (PATTERN (first))) == PC
18402       && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
18403     {
18404       /* Check for the right kind of condition.  */
18405       rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
18406       if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
18407 	  && REG_P (XEXP (cond, 0))
18408 	  && REG_P (XEXP (cond, 1))
18409 	  && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
18410 	  && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
18411 	{
18412 	  /* SECOND mentions the rt register but not the rs register.  */
18413 	  rtx tmp = XEXP (cond, 0);
18414 	  XEXP (cond, 0) = XEXP (cond, 1);
18415 	  XEXP (cond, 1) = tmp;
18416 	}
18417     }
18418 }
18419 
18420 /* Implement -mvr4130-align.  Go through each basic block and simulate the
18421    processor pipeline.  If we find that a pair of instructions could execute
18422    in parallel, and the first of those instructions is not 8-byte aligned,
18423    insert a nop to make it aligned.  */
18424 
18425 static void
vr4130_align_insns(void)18426 vr4130_align_insns (void)
18427 {
18428   struct mips_sim state;
18429   rtx_insn *insn, *subinsn, *last, *last2, *next;
18430   bool aligned_p;
18431 
18432   dfa_start ();
18433 
18434   /* LAST is the last instruction before INSN to have a nonzero length.
18435      LAST2 is the last such instruction before LAST.  */
18436   last = 0;
18437   last2 = 0;
18438 
18439   /* ALIGNED_P is true if INSN is known to be at an aligned address.  */
18440   aligned_p = true;
18441 
18442   mips_sim_init (&state, alloca (state_size ()));
18443   for (insn = get_insns (); insn != 0; insn = next)
18444     {
18445       unsigned int length;
18446 
18447       next = NEXT_INSN (insn);
18448 
18449       /* See the comment above vr4130_avoid_branch_rt_conflict for details.
18450 	 This isn't really related to the alignment pass, but we do it on
18451 	 the fly to avoid a separate instruction walk.  */
18452       vr4130_avoid_branch_rt_conflict (insn);
18453 
18454       length = get_attr_length (insn);
18455       if (length > 0 && USEFUL_INSN_P (insn))
18456 	FOR_EACH_SUBINSN (subinsn, insn)
18457 	  {
18458 	    mips_sim_wait_insn (&state, subinsn);
18459 
18460 	    /* If we want this instruction to issue in parallel with the
18461 	       previous one, make sure that the previous instruction is
18462 	       aligned.  There are several reasons why this isn't worthwhile
18463 	       when the second instruction is a call:
18464 
18465 	          - Calls are less likely to be performance critical,
18466 		  - There's a good chance that the delay slot can execute
18467 		    in parallel with the call.
18468 	          - The return address would then be unaligned.
18469 
18470 	       In general, if we're going to insert a nop between instructions
18471 	       X and Y, it's better to insert it immediately after X.  That
18472 	       way, if the nop makes Y aligned, it will also align any labels
18473 	       between X and Y.  */
18474 	    if (state.insns_left != state.issue_rate
18475 		&& !CALL_P (subinsn))
18476 	      {
18477 		if (subinsn == SEQ_BEGIN (insn) && aligned_p)
18478 		  {
18479 		    /* SUBINSN is the first instruction in INSN and INSN is
18480 		       aligned.  We want to align the previous instruction
18481 		       instead, so insert a nop between LAST2 and LAST.
18482 
18483 		       Note that LAST could be either a single instruction
18484 		       or a branch with a delay slot.  In the latter case,
18485 		       LAST, like INSN, is already aligned, but the delay
18486 		       slot must have some extra delay that stops it from
18487 		       issuing at the same time as the branch.  We therefore
18488 		       insert a nop before the branch in order to align its
18489 		       delay slot.  */
18490 		    gcc_assert (last2);
18491 		    emit_insn_after (gen_nop (), last2);
18492 		    aligned_p = false;
18493 		  }
18494 		else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
18495 		  {
18496 		    /* SUBINSN is the delay slot of INSN, but INSN is
18497 		       currently unaligned.  Insert a nop between
18498 		       LAST and INSN to align it.  */
18499 		    gcc_assert (last);
18500 		    emit_insn_after (gen_nop (), last);
18501 		    aligned_p = true;
18502 		  }
18503 	      }
18504 	    mips_sim_issue_insn (&state, subinsn);
18505 	  }
18506       mips_sim_finish_insn (&state, insn);
18507 
18508       /* Update LAST, LAST2 and ALIGNED_P for the next instruction.  */
18509       length = get_attr_length (insn);
18510       if (length > 0)
18511 	{
18512 	  /* If the instruction is an asm statement or multi-instruction
18513 	     mips.md patern, the length is only an estimate.  Insert an
18514 	     8 byte alignment after it so that the following instructions
18515 	     can be handled correctly.  */
18516 	  if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
18517 	      && (recog_memoized (insn) < 0 || length >= 8))
18518 	    {
18519 	      next = emit_insn_after (gen_align (GEN_INT (3)), insn);
18520 	      next = NEXT_INSN (next);
18521 	      mips_sim_next_cycle (&state);
18522 	      aligned_p = true;
18523 	    }
18524 	  else if (length & 4)
18525 	    aligned_p = !aligned_p;
18526 	  last2 = last;
18527 	  last = insn;
18528 	}
18529 
18530       /* See whether INSN is an aligned label.  */
18531       if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
18532 	aligned_p = true;
18533     }
18534   dfa_finish ();
18535 }
18536 
18537 /* This structure records that the current function has a LO_SUM
18538    involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
18539    the largest offset applied to BASE by all such LO_SUMs.  */
18540 struct mips_lo_sum_offset {
18541   rtx base;
18542   HOST_WIDE_INT offset;
18543 };
18544 
18545 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE.  */
18546 
18547 static hashval_t
mips_hash_base(rtx base)18548 mips_hash_base (rtx base)
18549 {
18550   int do_not_record_p;
18551 
18552   return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
18553 }
18554 
18555 /* Hashtable helpers.  */
18556 
18557 struct mips_lo_sum_offset_hasher : free_ptr_hash <mips_lo_sum_offset>
18558 {
18559   typedef rtx_def *compare_type;
18560   static inline hashval_t hash (const mips_lo_sum_offset *);
18561   static inline bool equal (const mips_lo_sum_offset *, const rtx_def *);
18562 };
18563 
18564 /* Hash-table callbacks for mips_lo_sum_offsets.  */
18565 
18566 inline hashval_t
hash(const mips_lo_sum_offset * entry)18567 mips_lo_sum_offset_hasher::hash (const mips_lo_sum_offset *entry)
18568 {
18569   return mips_hash_base (entry->base);
18570 }
18571 
18572 inline bool
equal(const mips_lo_sum_offset * entry,const rtx_def * value)18573 mips_lo_sum_offset_hasher::equal (const mips_lo_sum_offset *entry,
18574 				  const rtx_def *value)
18575 {
18576   return rtx_equal_p (entry->base, value);
18577 }
18578 
18579 typedef hash_table<mips_lo_sum_offset_hasher> mips_offset_table;
18580 
18581 /* Look up symbolic constant X in HTAB, which is a hash table of
18582    mips_lo_sum_offsets.  If OPTION is NO_INSERT, return true if X can be
18583    paired with a recorded LO_SUM, otherwise record X in the table.  */
18584 
18585 static bool
mips_lo_sum_offset_lookup(mips_offset_table * htab,rtx x,enum insert_option option)18586 mips_lo_sum_offset_lookup (mips_offset_table *htab, rtx x,
18587 			   enum insert_option option)
18588 {
18589   rtx base, offset;
18590   mips_lo_sum_offset **slot;
18591   struct mips_lo_sum_offset *entry;
18592 
18593   /* Split X into a base and offset.  */
18594   split_const (x, &base, &offset);
18595   if (UNSPEC_ADDRESS_P (base))
18596     base = UNSPEC_ADDRESS (base);
18597 
18598   /* Look up the base in the hash table.  */
18599   slot = htab->find_slot_with_hash (base, mips_hash_base (base), option);
18600   if (slot == NULL)
18601     return false;
18602 
18603   entry = (struct mips_lo_sum_offset *) *slot;
18604   if (option == INSERT)
18605     {
18606       if (entry == NULL)
18607 	{
18608 	  entry = XNEW (struct mips_lo_sum_offset);
18609 	  entry->base = base;
18610 	  entry->offset = INTVAL (offset);
18611 	  *slot = entry;
18612 	}
18613       else
18614 	{
18615 	  if (INTVAL (offset) > entry->offset)
18616 	    entry->offset = INTVAL (offset);
18617 	}
18618     }
18619   return INTVAL (offset) <= entry->offset;
18620 }
18621 
18622 /* Search X for LO_SUMs and record them in HTAB.  */
18623 
18624 static void
mips_record_lo_sums(const_rtx x,mips_offset_table * htab)18625 mips_record_lo_sums (const_rtx x, mips_offset_table *htab)
18626 {
18627   subrtx_iterator::array_type array;
18628   FOR_EACH_SUBRTX (iter, array, x, NONCONST)
18629     if (GET_CODE (*iter) == LO_SUM)
18630       mips_lo_sum_offset_lookup (htab, XEXP (*iter, 1), INSERT);
18631 }
18632 
18633 /* Return true if INSN is a SET of an orphaned high-part relocation.
18634    HTAB is a hash table of mips_lo_sum_offsets that describes all the
18635    LO_SUMs in the current function.  */
18636 
18637 static bool
mips_orphaned_high_part_p(mips_offset_table * htab,rtx_insn * insn)18638 mips_orphaned_high_part_p (mips_offset_table *htab, rtx_insn *insn)
18639 {
18640   enum mips_symbol_type type;
18641   rtx x, set;
18642 
18643   set = single_set (insn);
18644   if (set)
18645     {
18646       /* Check for %his.  */
18647       x = SET_SRC (set);
18648       if (GET_CODE (x) == HIGH
18649 	  && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
18650 	return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
18651 
18652       /* Check for local %gots (and %got_pages, which is redundant but OK).  */
18653       if (GET_CODE (x) == UNSPEC
18654 	  && XINT (x, 1) == UNSPEC_LOAD_GOT
18655 	  && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
18656 				       SYMBOL_CONTEXT_LEA, &type)
18657 	  && type == SYMBOL_GOTOFF_PAGE)
18658 	return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
18659     }
18660   return false;
18661 }
18662 
18663 /* Subroutine of mips_reorg_process_insns.  If there is a hazard between
18664    INSN and a previous instruction, avoid it by inserting nops after
18665    instruction AFTER.
18666 
18667    *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
18668    this point.  If *DELAYED_REG is non-null, INSN must wait a cycle
18669    before using the value of that register.  *HILO_DELAY counts the
18670    number of instructions since the last hilo hazard (that is,
18671    the number of instructions since the last MFLO or MFHI).
18672 
18673    After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
18674    for the next instruction.
18675 
18676    LO_REG is an rtx for the LO register, used in dependence checking.  */
18677 
18678 static void
mips_avoid_hazard(rtx_insn * after,rtx_insn * insn,int * hilo_delay,rtx * delayed_reg,rtx lo_reg,bool * fs_delay)18679 mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
18680 		   rtx *delayed_reg, rtx lo_reg, bool *fs_delay)
18681 {
18682   rtx pattern, set;
18683   int nops, ninsns;
18684 
18685   pattern = PATTERN (insn);
18686 
18687   /* Do not put the whole function in .set noreorder if it contains
18688      an asm statement.  We don't know whether there will be hazards
18689      between the asm statement and the gcc-generated code.  */
18690   if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
18691     cfun->machine->all_noreorder_p = false;
18692 
18693   /* Ignore zero-length instructions (barriers and the like).  */
18694   ninsns = get_attr_length (insn) / 4;
18695   if (ninsns == 0)
18696     return;
18697 
18698   /* Work out how many nops are needed.  Note that we only care about
18699      registers that are explicitly mentioned in the instruction's pattern.
18700      It doesn't matter that calls use the argument registers or that they
18701      clobber hi and lo.  */
18702   if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
18703     nops = 2 - *hilo_delay;
18704   else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
18705     nops = 1;
18706   /* If processing a forbidden slot hazard then a NOP is required if the
18707      branch instruction was not in a sequence (as the sequence would
18708      imply it is not actually a compact branch anyway) and the current
18709      insn is not an inline asm, and can't go in a delay slot.  */
18710   else if (*fs_delay && get_attr_can_delay (insn) == CAN_DELAY_NO
18711 	   && GET_CODE (PATTERN (after)) != SEQUENCE
18712 	   && GET_CODE (pattern) != ASM_INPUT
18713 	   && asm_noperands (pattern) < 0)
18714     nops = 1;
18715   else
18716     nops = 0;
18717 
18718   /* Insert the nops between this instruction and the previous one.
18719      Each new nop takes us further from the last hilo hazard.  */
18720   *hilo_delay += nops;
18721   while (nops-- > 0)
18722     emit_insn_after (gen_hazard_nop (), after);
18723 
18724   /* Set up the state for the next instruction.  */
18725   *hilo_delay += ninsns;
18726   *delayed_reg = 0;
18727   *fs_delay = false;
18728   if (INSN_CODE (insn) >= 0)
18729     switch (get_attr_hazard (insn))
18730       {
18731       case HAZARD_NONE:
18732 	break;
18733 
18734       case HAZARD_FORBIDDEN_SLOT:
18735 	if (TARGET_CB_MAYBE)
18736 	  *fs_delay = true;
18737 	break;
18738 
18739       case HAZARD_HILO:
18740 	*hilo_delay = 0;
18741 	break;
18742 
18743       case HAZARD_DELAY:
18744 	set = single_set (insn);
18745 	gcc_assert (set);
18746 	*delayed_reg = SET_DEST (set);
18747 	break;
18748       }
18749 }
18750 
18751 /* A SEQUENCE is breakable iff the branch inside it has a compact form
18752    and the target has compact branches.  */
18753 
18754 static bool
mips_breakable_sequence_p(rtx_insn * insn)18755 mips_breakable_sequence_p (rtx_insn *insn)
18756 {
18757   return (insn && GET_CODE (PATTERN (insn)) == SEQUENCE
18758 	  && TARGET_CB_MAYBE
18759 	  && get_attr_compact_form (SEQ_BEGIN (insn)) != COMPACT_FORM_NEVER);
18760 }
18761 
18762 /* Remove a SEQUENCE and replace it with the delay slot instruction
18763    followed by the branch and return the instruction in the delay slot.
18764    Return the first of the two new instructions.
18765    Subroutine of mips_reorg_process_insns.  */
18766 
18767 static rtx_insn *
mips_break_sequence(rtx_insn * insn)18768 mips_break_sequence (rtx_insn *insn)
18769 {
18770   rtx_insn *before = PREV_INSN (insn);
18771   rtx_insn *branch = SEQ_BEGIN (insn);
18772   rtx_insn *ds = SEQ_END (insn);
18773   remove_insn (insn);
18774   add_insn_after (ds, before, NULL);
18775   add_insn_after (branch, ds, NULL);
18776   return ds;
18777 }
18778 
18779 /* Go through the instruction stream and insert nops where necessary.
18780    Also delete any high-part relocations whose partnering low parts
18781    are now all dead.  See if the whole function can then be put into
18782    .set noreorder and .set nomacro.  */
18783 
18784 static void
mips_reorg_process_insns(void)18785 mips_reorg_process_insns (void)
18786 {
18787   rtx_insn *insn, *last_insn, *subinsn, *next_insn;
18788   rtx lo_reg, delayed_reg;
18789   int hilo_delay;
18790   bool fs_delay;
18791 
18792   /* Force all instructions to be split into their final form.  */
18793   split_all_insns_noflow ();
18794 
18795   /* Recalculate instruction lengths without taking nops into account.  */
18796   cfun->machine->ignore_hazard_length_p = true;
18797   shorten_branches (get_insns ());
18798 
18799   cfun->machine->all_noreorder_p = true;
18800 
18801   /* We don't track MIPS16 PC-relative offsets closely enough to make
18802      a good job of "set .noreorder" code in MIPS16 mode.  */
18803   if (TARGET_MIPS16)
18804     cfun->machine->all_noreorder_p = false;
18805 
18806   /* Code that doesn't use explicit relocs can't be ".set nomacro".  */
18807   if (!TARGET_EXPLICIT_RELOCS)
18808     cfun->machine->all_noreorder_p = false;
18809 
18810   /* Profiled functions can't be all noreorder because the profiler
18811      support uses assembler macros.  */
18812   if (crtl->profile)
18813     cfun->machine->all_noreorder_p = false;
18814 
18815   /* Code compiled with -mfix-vr4120, -mfix-rm7000 or -mfix-24k can't be
18816      all noreorder because we rely on the assembler to work around some
18817      errata.  The R5900 too has several bugs.  */
18818   if (TARGET_FIX_VR4120
18819       || TARGET_FIX_RM7000
18820       || TARGET_FIX_24K
18821       || TARGET_MIPS5900)
18822     cfun->machine->all_noreorder_p = false;
18823 
18824   /* The same is true for -mfix-vr4130 if we might generate MFLO or
18825      MFHI instructions.  Note that we avoid using MFLO and MFHI if
18826      the VR4130 MACC and DMACC instructions are available instead;
18827      see the *mfhilo_{si,di}_macc patterns.  */
18828   if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
18829     cfun->machine->all_noreorder_p = false;
18830 
18831   mips_offset_table htab (37);
18832 
18833   /* Make a first pass over the instructions, recording all the LO_SUMs.  */
18834   for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
18835     FOR_EACH_SUBINSN (subinsn, insn)
18836       if (USEFUL_INSN_P (subinsn))
18837 	{
18838 	  rtx body = PATTERN (insn);
18839 	  int noperands = asm_noperands (body);
18840 	  if (noperands >= 0)
18841 	    {
18842 	      rtx *ops = XALLOCAVEC (rtx, noperands);
18843 	      bool *used = XALLOCAVEC (bool, noperands);
18844 	      const char *string = decode_asm_operands (body, ops, NULL, NULL,
18845 							NULL, NULL);
18846 	      get_referenced_operands (string, used, noperands);
18847 	      for (int i = 0; i < noperands; ++i)
18848 		if (used[i])
18849 		  mips_record_lo_sums (ops[i], &htab);
18850 	    }
18851 	  else
18852 	    mips_record_lo_sums (PATTERN (subinsn), &htab);
18853 	}
18854 
18855   last_insn = 0;
18856   hilo_delay = 2;
18857   delayed_reg = 0;
18858   lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
18859   fs_delay = false;
18860 
18861   /* Make a second pass over the instructions.  Delete orphaned
18862      high-part relocations or turn them into NOPs.  Avoid hazards
18863      by inserting NOPs.  */
18864   for (insn = get_insns (); insn != 0; insn = next_insn)
18865     {
18866       next_insn = NEXT_INSN (insn);
18867       if (USEFUL_INSN_P (insn))
18868 	{
18869 	  if (GET_CODE (PATTERN (insn)) == SEQUENCE)
18870 	    {
18871 	      rtx_insn *next_active = next_active_insn (insn);
18872 	      /* Undo delay slots to avoid bubbles if the next instruction can
18873 		 be placed in a forbidden slot or the cost of adding an
18874 		 explicit NOP in a forbidden slot is OK and if the SEQUENCE is
18875 		 safely breakable.  */
18876 	      if (TARGET_CB_MAYBE
18877 		  && mips_breakable_sequence_p (insn)
18878 		  && INSN_P (SEQ_BEGIN (insn))
18879 		  && INSN_P (SEQ_END (insn))
18880 		  && ((next_active
18881 		       && INSN_P (next_active)
18882 		       && GET_CODE (PATTERN (next_active)) != SEQUENCE
18883 		       && get_attr_can_delay (next_active) == CAN_DELAY_YES)
18884 		      || !optimize_size))
18885 		{
18886 		  /* To hide a potential pipeline bubble, if we scan backwards
18887 		     from the current SEQUENCE and find that there is a load
18888 		     of a value that is used in the CTI and there are no
18889 		     dependencies between the CTI and instruction in the delay
18890 		     slot, break the sequence so the load delay is hidden.  */
18891 		  HARD_REG_SET uses;
18892 		  CLEAR_HARD_REG_SET (uses);
18893 		  note_uses (&PATTERN (SEQ_BEGIN (insn)), record_hard_reg_uses,
18894 			     &uses);
18895 		  HARD_REG_SET delay_sets;
18896 		  CLEAR_HARD_REG_SET (delay_sets);
18897 		  note_stores (PATTERN (SEQ_END (insn)), record_hard_reg_sets,
18898 			       &delay_sets);
18899 
18900 		  rtx_insn *prev = prev_active_insn (insn);
18901 		  if (prev
18902 		      && GET_CODE (PATTERN (prev)) == SET
18903 		      && MEM_P (SET_SRC (PATTERN (prev))))
18904 		    {
18905 		      HARD_REG_SET sets;
18906 		      CLEAR_HARD_REG_SET (sets);
18907 		      note_stores (PATTERN (prev), record_hard_reg_sets,
18908 				   &sets);
18909 
18910 		      /* Re-order if safe.  */
18911 		      if (!hard_reg_set_intersect_p (delay_sets, uses)
18912 			  && hard_reg_set_intersect_p (uses, sets))
18913 			{
18914 			  next_insn = mips_break_sequence (insn);
18915 			  /* Need to process the hazards of the newly
18916 			     introduced instructions.  */
18917 			  continue;
18918 			}
18919 		    }
18920 
18921 		  /* If we find an orphaned high-part relocation in a delay
18922 		     slot then we can convert to a compact branch and get
18923 		     the orphaned high part deleted.  */
18924 		  if (mips_orphaned_high_part_p (&htab, SEQ_END (insn)))
18925 		    {
18926 		      next_insn = mips_break_sequence (insn);
18927 		      /* Need to process the hazards of the newly
18928 			 introduced instructions.  */
18929 		      continue;
18930 		    }
18931 		}
18932 
18933 	      /* If we find an orphaned high-part relocation in a delay
18934 		 slot, it's easier to turn that instruction into a NOP than
18935 		 to delete it.  The delay slot will be a NOP either way.  */
18936 	      FOR_EACH_SUBINSN (subinsn, insn)
18937 		if (INSN_P (subinsn))
18938 		  {
18939 		    if (mips_orphaned_high_part_p (&htab, subinsn))
18940 		      {
18941 			PATTERN (subinsn) = gen_nop ();
18942 			INSN_CODE (subinsn) = CODE_FOR_nop;
18943 		      }
18944 		    mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
18945 				       &delayed_reg, lo_reg, &fs_delay);
18946 		  }
18947 	      last_insn = insn;
18948 	    }
18949 	  else
18950 	    {
18951 	      /* INSN is a single instruction.  Delete it if it's an
18952 		 orphaned high-part relocation.  */
18953 	      if (mips_orphaned_high_part_p (&htab, insn))
18954 		delete_insn (insn);
18955 	      /* Also delete cache barriers if the last instruction
18956 		 was an annulled branch.  INSN will not be speculatively
18957 		 executed.  */
18958 	      else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
18959 		       && last_insn
18960 		       && JUMP_P (SEQ_BEGIN (last_insn))
18961 		       && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
18962 		delete_insn (insn);
18963 	      else
18964 		{
18965 		  mips_avoid_hazard (last_insn, insn, &hilo_delay,
18966 				     &delayed_reg, lo_reg, &fs_delay);
18967 		  /* When a compact branch introduces a forbidden slot hazard
18968 		     and the next useful instruction is a SEQUENCE of a jump
18969 		     and a non-nop instruction in the delay slot, remove the
18970 		     sequence and replace it with the delay slot instruction
18971 		     then the jump to clear the forbidden slot hazard.  */
18972 
18973 		  if (fs_delay)
18974 		    {
18975 		      /* Search onwards from the current position looking for
18976 			 a SEQUENCE.  We are looking for pipeline hazards here
18977 			 and do not need to worry about labels or barriers as
18978 			 the optimization only undoes delay slot filling which
18979 			 only affects the order of the branch and its delay
18980 			 slot.  */
18981 		      rtx_insn *next = next_active_insn (insn);
18982 		      if (next
18983 			  && USEFUL_INSN_P (next)
18984 			  && GET_CODE (PATTERN (next)) == SEQUENCE
18985 			  && mips_breakable_sequence_p (next))
18986 			{
18987 			  last_insn = insn;
18988 			  next_insn = mips_break_sequence (next);
18989 			  /* Need to process the hazards of the newly
18990 			     introduced instructions.  */
18991 			  continue;
18992 			}
18993 		    }
18994 		  last_insn = insn;
18995 		}
18996 	    }
18997 	}
18998     }
18999 }
19000 
19001 /* Return true if the function has a long branch instruction.  */
19002 
19003 static bool
mips_has_long_branch_p(void)19004 mips_has_long_branch_p (void)
19005 {
19006   rtx_insn *insn, *subinsn;
19007   int normal_length;
19008 
19009   /* We need up-to-date instruction lengths.  */
19010   shorten_branches (get_insns ());
19011 
19012   /* Look for a branch that is longer than normal.  The normal length for
19013      non-MIPS16 branches is 8, because the length includes the delay slot.
19014      It is 4 for MIPS16, because MIPS16 branches are extended instructions,
19015      but they have no delay slot.  */
19016   normal_length = (TARGET_MIPS16 ? 4 : 8);
19017   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19018     FOR_EACH_SUBINSN (subinsn, insn)
19019       if (JUMP_P (subinsn)
19020 	  && get_attr_length (subinsn) > normal_length
19021 	  && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
19022 	return true;
19023 
19024   return false;
19025 }
19026 
19027 /* If we are using a GOT, but have not decided to use a global pointer yet,
19028    see whether we need one to implement long branches.  Convert the ghost
19029    global-pointer instructions into real ones if so.  */
19030 
19031 static bool
mips_expand_ghost_gp_insns(void)19032 mips_expand_ghost_gp_insns (void)
19033 {
19034   /* Quick exit if we already know that we will or won't need a
19035      global pointer.  */
19036   if (!TARGET_USE_GOT
19037       || cfun->machine->global_pointer == INVALID_REGNUM
19038       || mips_must_initialize_gp_p ())
19039     return false;
19040 
19041   /* Run a full check for long branches.  */
19042   if (!mips_has_long_branch_p ())
19043     return false;
19044 
19045   /* We've now established that we need $gp.  */
19046   cfun->machine->must_initialize_gp_p = true;
19047   split_all_insns_noflow ();
19048 
19049   return true;
19050 }
19051 
19052 /* Subroutine of mips_reorg to manage passes that require DF.  */
19053 
19054 static void
mips_df_reorg(void)19055 mips_df_reorg (void)
19056 {
19057   /* Create def-use chains.  */
19058   df_set_flags (DF_EQ_NOTES);
19059   df_chain_add_problem (DF_UD_CHAIN);
19060   df_analyze ();
19061 
19062   if (TARGET_RELAX_PIC_CALLS)
19063     mips_annotate_pic_calls ();
19064 
19065   if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
19066     r10k_insert_cache_barriers ();
19067 
19068   df_finish_pass (false);
19069 }
19070 
19071 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST.  This is
19072    called very late in mips_reorg, but the caller is required to run
19073    mips16_lay_out_constants on the result.  */
19074 
19075 static void
mips16_load_branch_target(rtx dest,rtx src)19076 mips16_load_branch_target (rtx dest, rtx src)
19077 {
19078   if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
19079     {
19080       rtx page, low;
19081 
19082       if (mips_cfun_has_cprestore_slot_p ())
19083 	mips_emit_move (dest, mips_cprestore_slot (dest, true));
19084       else
19085 	mips_emit_move (dest, pic_offset_table_rtx);
19086       page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
19087       low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
19088       emit_insn (gen_rtx_SET (dest,
19089 			      PMODE_INSN (gen_unspec_got, (dest, page))));
19090       emit_insn (gen_rtx_SET (dest, gen_rtx_LO_SUM (Pmode, dest, low)));
19091     }
19092   else
19093     {
19094       src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
19095       mips_emit_move (dest, src);
19096     }
19097 }
19098 
19099 /* If we're compiling a MIPS16 function, look for and split any long branches.
19100    This must be called after all other instruction modifications in
19101    mips_reorg.  */
19102 
19103 static void
mips16_split_long_branches(void)19104 mips16_split_long_branches (void)
19105 {
19106   bool something_changed;
19107 
19108   if (!TARGET_MIPS16)
19109     return;
19110 
19111   /* Loop until the alignments for all targets are sufficient.  */
19112   do
19113     {
19114       rtx_insn *insn;
19115       rtx_jump_insn *jump_insn;
19116 
19117       shorten_branches (get_insns ());
19118       something_changed = false;
19119       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19120 	if ((jump_insn = dyn_cast <rtx_jump_insn *> (insn))
19121 	    && get_attr_length (jump_insn) > 4
19122 	    && (any_condjump_p (jump_insn) || any_uncondjump_p (jump_insn)))
19123 	  {
19124 	    rtx old_label, temp, saved_temp;
19125 	    rtx_code_label *new_label;
19126 	    rtx target;
19127 	    rtx_insn *jump, *jump_sequence;
19128 
19129 	    start_sequence ();
19130 
19131 	    /* Free up a MIPS16 register by saving it in $1.  */
19132 	    saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
19133 	    temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
19134 	    emit_move_insn (saved_temp, temp);
19135 
19136 	    /* Load the branch target into TEMP.  */
19137 	    old_label = JUMP_LABEL (jump_insn);
19138 	    target = gen_rtx_LABEL_REF (Pmode, old_label);
19139 	    mips16_load_branch_target (temp, target);
19140 
19141 	    /* Jump to the target and restore the register's
19142 	       original value.  */
19143 	    jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
19144 					       (temp, temp, saved_temp)));
19145 	    JUMP_LABEL (jump) = old_label;
19146 	    LABEL_NUSES (old_label)++;
19147 
19148 	    /* Rewrite any symbolic references that are supposed to use
19149 	       a PC-relative constant pool.  */
19150 	    mips16_lay_out_constants (false);
19151 
19152 	    if (simplejump_p (jump_insn))
19153 	      /* We're going to replace INSN with a longer form.  */
19154 	      new_label = NULL;
19155 	    else
19156 	      {
19157 		/* Create a branch-around label for the original
19158 		   instruction.  */
19159 		new_label = gen_label_rtx ();
19160 		emit_label (new_label);
19161 	      }
19162 
19163 	    jump_sequence = get_insns ();
19164 	    end_sequence ();
19165 
19166 	    emit_insn_after (jump_sequence, jump_insn);
19167 	    if (new_label)
19168 	      invert_jump (jump_insn, new_label, false);
19169 	    else
19170 	      delete_insn (jump_insn);
19171 	    something_changed = true;
19172 	  }
19173     }
19174   while (something_changed);
19175 }
19176 
19177 /* Insert a `.insn' assembly pseudo-op after any labels followed by
19178    a MIPS16 constant pool or no insn at all.  This is needed so that
19179    targets that have been optimized away are still marked as code
19180    and therefore branches that remained and point to them are known
19181    to retain the ISA mode and as such can be successfully assembled.  */
19182 
19183 static void
mips_insert_insn_pseudos(void)19184 mips_insert_insn_pseudos (void)
19185 {
19186   bool insn_pseudo_needed = TRUE;
19187   rtx_insn *insn;
19188 
19189   for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
19190     switch (GET_CODE (insn))
19191       {
19192       case INSN:
19193 	if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
19194 	    && XINT (PATTERN (insn), 1) == UNSPEC_CONSTTABLE)
19195 	  {
19196 	    insn_pseudo_needed = TRUE;
19197 	    break;
19198 	  }
19199 	/* Fall through.  */
19200       case JUMP_INSN:
19201       case CALL_INSN:
19202       case JUMP_TABLE_DATA:
19203 	insn_pseudo_needed = FALSE;
19204 	break;
19205       case CODE_LABEL:
19206 	if (insn_pseudo_needed)
19207 	  {
19208 	    emit_insn_after (gen_insn_pseudo (), insn);
19209 	    insn_pseudo_needed = FALSE;
19210 	  }
19211 	break;
19212       default:
19213 	break;
19214       }
19215 }
19216 
19217 /* Implement TARGET_MACHINE_DEPENDENT_REORG.  */
19218 
19219 static void
mips_reorg(void)19220 mips_reorg (void)
19221 {
19222   /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF.  Also during
19223      insn splitting in mips16_lay_out_constants, DF insn info is only kept up
19224      to date if the CFG is available.  */
19225   if (mips_cfg_in_reorg ())
19226     compute_bb_for_insn ();
19227   mips16_lay_out_constants (true);
19228   if (mips_cfg_in_reorg ())
19229     {
19230       mips_df_reorg ();
19231       free_bb_for_insn ();
19232     }
19233 }
19234 
19235 /* We use a machine specific pass to do a second machine dependent reorg
19236    pass after delay branch scheduling.  */
19237 
19238 static unsigned int
mips_machine_reorg2(void)19239 mips_machine_reorg2 (void)
19240 {
19241   mips_reorg_process_insns ();
19242   if (!TARGET_MIPS16
19243       && TARGET_EXPLICIT_RELOCS
19244       && TUNE_MIPS4130
19245       && TARGET_VR4130_ALIGN)
19246     vr4130_align_insns ();
19247   if (mips_expand_ghost_gp_insns ())
19248     /* The expansion could invalidate some of the VR4130 alignment
19249        optimizations, but this should be an extremely rare case anyhow.  */
19250     mips_reorg_process_insns ();
19251   mips16_split_long_branches ();
19252   mips_insert_insn_pseudos ();
19253   return 0;
19254 }
19255 
19256 namespace {
19257 
19258 const pass_data pass_data_mips_machine_reorg2 =
19259 {
19260   RTL_PASS, /* type */
19261   "mach2", /* name */
19262   OPTGROUP_NONE, /* optinfo_flags */
19263   TV_MACH_DEP, /* tv_id */
19264   0, /* properties_required */
19265   0, /* properties_provided */
19266   0, /* properties_destroyed */
19267   0, /* todo_flags_start */
19268   0, /* todo_flags_finish */
19269 };
19270 
19271 class pass_mips_machine_reorg2 : public rtl_opt_pass
19272 {
19273 public:
pass_mips_machine_reorg2(gcc::context * ctxt)19274   pass_mips_machine_reorg2(gcc::context *ctxt)
19275     : rtl_opt_pass(pass_data_mips_machine_reorg2, ctxt)
19276   {}
19277 
19278   /* opt_pass methods: */
execute(function *)19279   virtual unsigned int execute (function *) { return mips_machine_reorg2 (); }
19280 
19281 }; // class pass_mips_machine_reorg2
19282 
19283 } // anon namespace
19284 
19285 rtl_opt_pass *
make_pass_mips_machine_reorg2(gcc::context * ctxt)19286 make_pass_mips_machine_reorg2 (gcc::context *ctxt)
19287 {
19288   return new pass_mips_machine_reorg2 (ctxt);
19289 }
19290 
19291 
19292 /* Implement TARGET_ASM_OUTPUT_MI_THUNK.  Generate rtl rather than asm text
19293    in order to avoid duplicating too much logic from elsewhere.  */
19294 
19295 static void
mips_output_mi_thunk(FILE * file,tree thunk_fndecl ATTRIBUTE_UNUSED,HOST_WIDE_INT delta,HOST_WIDE_INT vcall_offset,tree function)19296 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
19297 		      HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
19298 		      tree function)
19299 {
19300   rtx this_rtx, temp1, temp2, fnaddr;
19301   rtx_insn *insn;
19302   bool use_sibcall_p;
19303 
19304   /* Pretend to be a post-reload pass while generating rtl.  */
19305   reload_completed = 1;
19306 
19307   /* Mark the end of the (empty) prologue.  */
19308   emit_note (NOTE_INSN_PROLOGUE_END);
19309 
19310   /* Determine if we can use a sibcall to call FUNCTION directly.  */
19311   fnaddr = XEXP (DECL_RTL (function), 0);
19312   use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
19313 		   && const_call_insn_operand (fnaddr, Pmode));
19314 
19315   /* Determine if we need to load FNADDR from the GOT.  */
19316   if (!use_sibcall_p
19317       && (mips_got_symbol_type_p
19318 	  (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
19319     {
19320       /* Pick a global pointer.  Use a call-clobbered register if
19321 	 TARGET_CALL_SAVED_GP.  */
19322       cfun->machine->global_pointer
19323 	= TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
19324       cfun->machine->must_initialize_gp_p = true;
19325       SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
19326 
19327       /* Set up the global pointer for n32 or n64 abicalls.  */
19328       mips_emit_loadgp ();
19329     }
19330 
19331   /* We need two temporary registers in some cases.  */
19332   temp1 = gen_rtx_REG (Pmode, 2);
19333   temp2 = gen_rtx_REG (Pmode, 3);
19334 
19335   /* Find out which register contains the "this" pointer.  */
19336   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
19337     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
19338   else
19339     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
19340 
19341   /* Add DELTA to THIS_RTX.  */
19342   if (delta != 0)
19343     {
19344       rtx offset = GEN_INT (delta);
19345       if (!SMALL_OPERAND (delta))
19346 	{
19347 	  mips_emit_move (temp1, offset);
19348 	  offset = temp1;
19349 	}
19350       emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
19351     }
19352 
19353   /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX.  */
19354   if (vcall_offset != 0)
19355     {
19356       rtx addr;
19357 
19358       /* Set TEMP1 to *THIS_RTX.  */
19359       mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
19360 
19361       /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET.  */
19362       addr = mips_add_offset (temp2, temp1, vcall_offset);
19363 
19364       /* Load the offset and add it to THIS_RTX.  */
19365       mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
19366       emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
19367     }
19368 
19369   /* Jump to the target function.  Use a sibcall if direct jumps are
19370      allowed, otherwise load the address into a register first.  */
19371   if (use_sibcall_p)
19372     {
19373       insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
19374       SIBLING_CALL_P (insn) = 1;
19375     }
19376   else
19377     {
19378       /* This is messy.  GAS treats "la $25,foo" as part of a call
19379 	 sequence and may allow a global "foo" to be lazily bound.
19380 	 The general move patterns therefore reject this combination.
19381 
19382 	 In this context, lazy binding would actually be OK
19383 	 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
19384 	 TARGET_CALL_SAVED_GP; see mips_load_call_address.
19385 	 We must therefore load the address via a temporary
19386 	 register if mips_dangerous_for_la25_p.
19387 
19388 	 If we jump to the temporary register rather than $25,
19389 	 the assembler can use the move insn to fill the jump's
19390 	 delay slot.
19391 
19392 	 We can use the same technique for MIPS16 code, where $25
19393 	 is not a valid JR register.  */
19394       if (TARGET_USE_PIC_FN_ADDR_REG
19395 	  && !TARGET_MIPS16
19396 	  && !mips_dangerous_for_la25_p (fnaddr))
19397 	temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
19398       mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
19399 
19400       if (TARGET_USE_PIC_FN_ADDR_REG
19401 	  && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
19402 	mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
19403       emit_jump_insn (gen_indirect_jump (temp1));
19404     }
19405 
19406   /* Run just enough of rest_of_compilation.  This sequence was
19407      "borrowed" from alpha.c.  */
19408   insn = get_insns ();
19409   split_all_insns_noflow ();
19410   mips16_lay_out_constants (true);
19411   shorten_branches (insn);
19412   final_start_function (insn, file, 1);
19413   final (insn, file, 1);
19414   final_end_function ();
19415 
19416   /* Clean up the vars set above.  Note that final_end_function resets
19417      the global pointer for us.  */
19418   reload_completed = 0;
19419 }
19420 
19421 
19422 /* The last argument passed to mips_set_compression_mode,
19423    or negative if the function hasn't been called yet.  */
19424 static unsigned int old_compression_mode = -1;
19425 
19426 /* Set up the target-dependent global state for ISA mode COMPRESSION_MODE,
19427    which is either MASK_MIPS16 or MASK_MICROMIPS.  */
19428 
19429 static void
mips_set_compression_mode(unsigned int compression_mode)19430 mips_set_compression_mode (unsigned int compression_mode)
19431 {
19432 
19433   if (compression_mode == old_compression_mode)
19434     return;
19435 
19436   /* Restore base settings of various flags.  */
19437   target_flags = mips_base_target_flags;
19438   flag_schedule_insns = mips_base_schedule_insns;
19439   flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
19440   flag_move_loop_invariants = mips_base_move_loop_invariants;
19441   align_loops = mips_base_align_loops;
19442   align_jumps = mips_base_align_jumps;
19443   align_functions = mips_base_align_functions;
19444   target_flags &= ~(MASK_MIPS16 | MASK_MICROMIPS);
19445   target_flags |= compression_mode;
19446 
19447   if (compression_mode & MASK_MIPS16)
19448     {
19449       /* Switch to MIPS16 mode.  */
19450       target_flags |= MASK_MIPS16;
19451 
19452       /* Turn off SYNCI if it was on, MIPS16 doesn't support it.  */
19453       target_flags &= ~MASK_SYNCI;
19454 
19455       /* Don't run the scheduler before reload, since it tends to
19456          increase register pressure.  */
19457       flag_schedule_insns = 0;
19458 
19459       /* Don't do hot/cold partitioning.  mips16_lay_out_constants expects
19460 	 the whole function to be in a single section.  */
19461       flag_reorder_blocks_and_partition = 0;
19462 
19463       /* Don't move loop invariants, because it tends to increase
19464 	 register pressure.  It also introduces an extra move in cases
19465 	 where the constant is the first operand in a two-operand binary
19466 	 instruction, or when it forms a register argument to a functon
19467 	 call.  */
19468       flag_move_loop_invariants = 0;
19469 
19470       target_flags |= MASK_EXPLICIT_RELOCS;
19471 
19472       /* Experiments suggest we get the best overall section-anchor
19473 	 results from using the range of an unextended LW or SW.  Code
19474 	 that makes heavy use of byte or short accesses can do better
19475 	 with ranges of 0...31 and 0...63 respectively, but most code is
19476 	 sensitive to the range of LW and SW instead.  */
19477       targetm.min_anchor_offset = 0;
19478       targetm.max_anchor_offset = 127;
19479 
19480       targetm.const_anchor = 0;
19481 
19482       /* MIPS16 has no BAL instruction.  */
19483       target_flags &= ~MASK_RELAX_PIC_CALLS;
19484 
19485       /* The R4000 errata don't apply to any known MIPS16 cores.
19486 	 It's simpler to make the R4000 fixes and MIPS16 mode
19487 	 mutually exclusive.  */
19488       target_flags &= ~MASK_FIX_R4000;
19489 
19490       if (flag_pic && !TARGET_OLDABI)
19491 	sorry ("MIPS16 PIC for ABIs other than o32 and o64");
19492 
19493       if (TARGET_XGOT)
19494 	sorry ("MIPS16 -mxgot code");
19495 
19496       if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
19497 	sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
19498 
19499       if (TARGET_MSA)
19500 	sorry ("MSA MIPS16 code");
19501     }
19502   else
19503     {
19504       /* Switch to microMIPS or the standard encoding.  */
19505 
19506       if (TARGET_MICROMIPS)
19507 	/* Avoid branch likely.  */
19508 	target_flags &= ~MASK_BRANCHLIKELY;
19509 
19510       /* Provide default values for align_* for 64-bit targets.  */
19511       if (TARGET_64BIT)
19512 	{
19513 	  if (align_loops == 0)
19514 	    align_loops = 8;
19515 	  if (align_jumps == 0)
19516 	    align_jumps = 8;
19517 	  if (align_functions == 0)
19518 	    align_functions = 8;
19519 	}
19520 
19521       targetm.min_anchor_offset = -32768;
19522       targetm.max_anchor_offset = 32767;
19523 
19524       targetm.const_anchor = 0x8000;
19525     }
19526 
19527   /* (Re)initialize MIPS target internals for new ISA.  */
19528   mips_init_relocs ();
19529 
19530   if (compression_mode & MASK_MIPS16)
19531     {
19532       if (!mips16_globals)
19533 	mips16_globals = save_target_globals_default_opts ();
19534       else
19535 	restore_target_globals (mips16_globals);
19536     }
19537   else if (compression_mode & MASK_MICROMIPS)
19538     {
19539       if (!micromips_globals)
19540 	micromips_globals = save_target_globals_default_opts ();
19541       else
19542 	restore_target_globals (micromips_globals);
19543     }
19544   else
19545     restore_target_globals (&default_target_globals);
19546 
19547   old_compression_mode = compression_mode;
19548 }
19549 
19550 /* Implement TARGET_SET_CURRENT_FUNCTION.  Decide whether the current
19551    function should use the MIPS16 or microMIPS ISA and switch modes
19552    accordingly.  */
19553 
19554 static void
mips_set_current_function(tree fndecl)19555 mips_set_current_function (tree fndecl)
19556 {
19557   mips_set_compression_mode (mips_get_compress_mode (fndecl));
19558 }
19559 
19560 /* Allocate a chunk of memory for per-function machine-dependent data.  */
19561 
19562 static struct machine_function *
mips_init_machine_status(void)19563 mips_init_machine_status (void)
19564 {
19565   return ggc_cleared_alloc<machine_function> ();
19566 }
19567 
19568 /* Return the processor associated with the given ISA level, or null
19569    if the ISA isn't valid.  */
19570 
19571 static const struct mips_cpu_info *
mips_cpu_info_from_isa(int isa)19572 mips_cpu_info_from_isa (int isa)
19573 {
19574   unsigned int i;
19575 
19576   for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
19577     if (mips_cpu_info_table[i].isa == isa)
19578       return mips_cpu_info_table + i;
19579 
19580   return NULL;
19581 }
19582 
19583 /* Return a mips_cpu_info entry determined by an option valued
19584    OPT.  */
19585 
19586 static const struct mips_cpu_info *
mips_cpu_info_from_opt(int opt)19587 mips_cpu_info_from_opt (int opt)
19588 {
19589   switch (opt)
19590     {
19591     case MIPS_ARCH_OPTION_FROM_ABI:
19592       /* 'from-abi' selects the most compatible architecture for the
19593 	 given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
19594 	 ABIs.  For the EABIs, we have to decide whether we're using
19595 	 the 32-bit or 64-bit version.  */
19596       return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
19597 				     : ABI_NEEDS_64BIT_REGS ? 3
19598 				     : (TARGET_64BIT ? 3 : 1));
19599 
19600     case MIPS_ARCH_OPTION_NATIVE:
19601       gcc_unreachable ();
19602 
19603     default:
19604       return &mips_cpu_info_table[opt];
19605     }
19606 }
19607 
19608 /* Return a default mips_cpu_info entry, given that no -march= option
19609    was explicitly specified.  */
19610 
19611 static const struct mips_cpu_info *
mips_default_arch(void)19612 mips_default_arch (void)
19613 {
19614 #if defined (MIPS_CPU_STRING_DEFAULT)
19615   unsigned int i;
19616   for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
19617     if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
19618       return mips_cpu_info_table + i;
19619   gcc_unreachable ();
19620 #elif defined (MIPS_ISA_DEFAULT)
19621   return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
19622 #else
19623   /* 'from-abi' makes a good default: you get whatever the ABI
19624      requires.  */
19625   return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
19626 #endif
19627 }
19628 
19629 /* Set up globals to generate code for the ISA or processor
19630    described by INFO.  */
19631 
19632 static void
mips_set_architecture(const struct mips_cpu_info * info)19633 mips_set_architecture (const struct mips_cpu_info *info)
19634 {
19635   if (info != 0)
19636     {
19637       mips_arch_info = info;
19638       mips_arch = info->cpu;
19639       mips_isa = info->isa;
19640       if (mips_isa < 32)
19641 	mips_isa_rev = 0;
19642       else
19643 	mips_isa_rev = (mips_isa & 31) + 1;
19644     }
19645 }
19646 
19647 /* Likewise for tuning.  */
19648 
19649 static void
mips_set_tune(const struct mips_cpu_info * info)19650 mips_set_tune (const struct mips_cpu_info *info)
19651 {
19652   if (info != 0)
19653     {
19654       mips_tune_info = info;
19655       mips_tune = info->cpu;
19656     }
19657 }
19658 
19659 /* Implement TARGET_OPTION_OVERRIDE.  */
19660 
19661 static void
mips_option_override(void)19662 mips_option_override (void)
19663 {
19664   int i, start, regno, mode;
19665 
19666   if (global_options_set.x_mips_isa_option)
19667     mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
19668 
19669 #ifdef SUBTARGET_OVERRIDE_OPTIONS
19670   SUBTARGET_OVERRIDE_OPTIONS;
19671 #endif
19672 
19673   /* MIPS16 and microMIPS cannot coexist.  */
19674   if (TARGET_MICROMIPS && TARGET_MIPS16)
19675     error ("unsupported combination: %s", "-mips16 -mmicromips");
19676 
19677   /* Prohibit Paired-Single and MSA combination.  This is software restriction
19678      rather than architectural.  */
19679   if (ISA_HAS_MSA && TARGET_PAIRED_SINGLE_FLOAT)
19680     error ("unsupported combination: %s", "-mmsa -mpaired-single");
19681 
19682   /* Save the base compression state and process flags as though we
19683      were generating uncompressed code.  */
19684   mips_base_compression_flags = TARGET_COMPRESSION;
19685   target_flags &= ~TARGET_COMPRESSION;
19686 
19687   /* -mno-float overrides -mhard-float and -msoft-float.  */
19688   if (TARGET_NO_FLOAT)
19689     {
19690       target_flags |= MASK_SOFT_FLOAT_ABI;
19691       target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
19692     }
19693 
19694   if (TARGET_FLIP_MIPS16)
19695     TARGET_INTERLINK_COMPRESSED = 1;
19696 
19697   /* Set the small data limit.  */
19698   mips_small_data_threshold = (global_options_set.x_g_switch_value
19699 			       ? g_switch_value
19700 			       : MIPS_DEFAULT_GVALUE);
19701 
19702   /* The following code determines the architecture and register size.
19703      Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
19704      The GAS and GCC code should be kept in sync as much as possible.  */
19705 
19706   if (global_options_set.x_mips_arch_option)
19707     mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
19708 
19709   if (mips_isa_option_info != 0)
19710     {
19711       if (mips_arch_info == 0)
19712 	mips_set_architecture (mips_isa_option_info);
19713       else if (mips_arch_info->isa != mips_isa_option_info->isa)
19714 	error ("%<-%s%> conflicts with the other architecture options, "
19715 	       "which specify a %s processor",
19716 	       mips_isa_option_info->name,
19717 	       mips_cpu_info_from_isa (mips_arch_info->isa)->name);
19718     }
19719 
19720   if (mips_arch_info == 0)
19721     mips_set_architecture (mips_default_arch ());
19722 
19723   if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
19724     error ("%<-march=%s%> is not compatible with the selected ABI",
19725 	   mips_arch_info->name);
19726 
19727   /* Optimize for mips_arch, unless -mtune selects a different processor.  */
19728   if (global_options_set.x_mips_tune_option)
19729     mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
19730 
19731   if (mips_tune_info == 0)
19732     mips_set_tune (mips_arch_info);
19733 
19734   if ((target_flags_explicit & MASK_64BIT) != 0)
19735     {
19736       /* The user specified the size of the integer registers.  Make sure
19737 	 it agrees with the ABI and ISA.  */
19738       if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
19739 	error ("%<-mgp64%> used with a 32-bit processor");
19740       else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
19741 	error ("%<-mgp32%> used with a 64-bit ABI");
19742       else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
19743 	error ("%<-mgp64%> used with a 32-bit ABI");
19744     }
19745   else
19746     {
19747       /* Infer the integer register size from the ABI and processor.
19748 	 Restrict ourselves to 32-bit registers if that's all the
19749 	 processor has, or if the ABI cannot handle 64-bit registers.  */
19750       if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
19751 	target_flags &= ~MASK_64BIT;
19752       else
19753 	target_flags |= MASK_64BIT;
19754     }
19755 
19756   if ((target_flags_explicit & MASK_FLOAT64) != 0)
19757     {
19758       if (mips_isa_rev >= 6 && !TARGET_FLOAT64)
19759 	error ("the %qs architecture does not support %<-mfp32%>",
19760 	       mips_arch_info->name);
19761       else if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
19762 	error ("unsupported combination: %s", "-mfp64 -msingle-float");
19763       else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
19764 	error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
19765       else if (!TARGET_64BIT && TARGET_FLOAT64)
19766 	{
19767 	  if (!ISA_HAS_MXHC1)
19768 	    error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
19769 		   " the target supports the mfhc1 and mthc1 instructions");
19770 	  else if (mips_abi != ABI_32)
19771 	    error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
19772 		   " the o32 ABI");
19773 	}
19774     }
19775   else
19776     {
19777       /* -msingle-float selects 32-bit float registers.  On r6 and later,
19778 	 -mdouble-float selects 64-bit float registers, since the old paired
19779 	 register model is not supported.  In other cases the float registers
19780 	 should be the same size as the integer ones.  */
19781       if (mips_isa_rev >= 6 && TARGET_DOUBLE_FLOAT && !TARGET_FLOATXX)
19782 	target_flags |= MASK_FLOAT64;
19783       else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
19784 	target_flags |= MASK_FLOAT64;
19785       else if (mips_abi == ABI_32 && ISA_HAS_MSA && !TARGET_FLOATXX)
19786 	target_flags |= MASK_FLOAT64;
19787       else
19788 	target_flags &= ~MASK_FLOAT64;
19789     }
19790 
19791   if (mips_abi != ABI_32 && TARGET_FLOATXX)
19792     error ("%<-mfpxx%> can only be used with the o32 ABI");
19793   else if (TARGET_FLOAT64 && TARGET_FLOATXX)
19794     error ("unsupported combination: %s", "-mfp64 -mfpxx");
19795   else if (ISA_MIPS1 && !TARGET_FLOAT32)
19796     error ("%<-march=%s%> requires %<-mfp32%>", mips_arch_info->name);
19797   else if (TARGET_FLOATXX && !mips_lra_flag)
19798     error ("%<-mfpxx%> requires %<-mlra%>");
19799 
19800   /* End of code shared with GAS.  */
19801 
19802   /* The R5900 FPU only supports single precision.  */
19803   if (TARGET_MIPS5900 && TARGET_HARD_FLOAT_ABI && TARGET_DOUBLE_FLOAT)
19804     error ("unsupported combination: %s",
19805 	   "-march=r5900 -mhard-float -mdouble-float");
19806 
19807   /* If a -mlong* option was given, check that it matches the ABI,
19808      otherwise infer the -mlong* setting from the other options.  */
19809   if ((target_flags_explicit & MASK_LONG64) != 0)
19810     {
19811       if (TARGET_LONG64)
19812 	{
19813 	  if (mips_abi == ABI_N32)
19814 	    error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
19815 	  else if (mips_abi == ABI_32)
19816 	    error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
19817 	  else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
19818 	    /* We have traditionally allowed non-abicalls code to use
19819 	       an LP64 form of o64.  However, it would take a bit more
19820 	       effort to support the combination of 32-bit GOT entries
19821 	       and 64-bit pointers, so we treat the abicalls case as
19822 	       an error.  */
19823 	    error ("the combination of %qs and %qs is incompatible with %qs",
19824 		   "-mabi=o64", "-mabicalls", "-mlong64");
19825 	}
19826       else
19827 	{
19828 	  if (mips_abi == ABI_64)
19829 	    error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
19830 	}
19831     }
19832   else
19833     {
19834       if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
19835 	target_flags |= MASK_LONG64;
19836       else
19837 	target_flags &= ~MASK_LONG64;
19838     }
19839 
19840   if (!TARGET_OLDABI)
19841     flag_pcc_struct_return = 0;
19842 
19843   /* Decide which rtx_costs structure to use.  */
19844   if (optimize_size)
19845     mips_cost = &mips_rtx_cost_optimize_size;
19846   else
19847     mips_cost = &mips_rtx_cost_data[mips_tune];
19848 
19849   /* If the user hasn't specified a branch cost, use the processor's
19850      default.  */
19851   if (mips_branch_cost == 0)
19852     mips_branch_cost = mips_cost->branch_cost;
19853 
19854   /* If neither -mbranch-likely nor -mno-branch-likely was given
19855      on the command line, set MASK_BRANCHLIKELY based on the target
19856      architecture and tuning flags.  Annulled delay slots are a
19857      size win, so we only consider the processor-specific tuning
19858      for !optimize_size.  */
19859   if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
19860     {
19861       if (ISA_HAS_BRANCHLIKELY
19862 	  && ((optimize_size
19863 	       && (mips_tune_info->tune_flags
19864 		   & PTF_AVOID_BRANCHLIKELY_SIZE) == 0)
19865 	      || (!optimize_size
19866 		  && optimize > 0
19867 		  && (mips_tune_info->tune_flags
19868 		      & PTF_AVOID_BRANCHLIKELY_SPEED) == 0)
19869 	      || (mips_tune_info->tune_flags
19870 		  & PTF_AVOID_BRANCHLIKELY_ALWAYS) == 0))
19871 	target_flags |= MASK_BRANCHLIKELY;
19872       else
19873 	target_flags &= ~MASK_BRANCHLIKELY;
19874     }
19875   else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
19876     warning (0, "the %qs architecture does not support branch-likely"
19877 	     " instructions", mips_arch_info->name);
19878 
19879   /* If the user hasn't specified -mimadd or -mno-imadd set
19880      MASK_IMADD based on the target architecture and tuning
19881      flags.  */
19882   if ((target_flags_explicit & MASK_IMADD) == 0)
19883     {
19884       if (ISA_HAS_MADD_MSUB &&
19885           (mips_tune_info->tune_flags & PTF_AVOID_IMADD) == 0)
19886 	target_flags |= MASK_IMADD;
19887       else
19888 	target_flags &= ~MASK_IMADD;
19889     }
19890   else if (TARGET_IMADD && !ISA_HAS_MADD_MSUB)
19891     warning (0, "the %qs architecture does not support madd or msub"
19892 	     " instructions", mips_arch_info->name);
19893 
19894   /* If neither -modd-spreg nor -mno-odd-spreg was given on the command
19895      line, set MASK_ODD_SPREG based on the ISA and ABI.  */
19896   if ((target_flags_explicit & MASK_ODD_SPREG) == 0)
19897     {
19898       /* Disable TARGET_ODD_SPREG when using the o32 FPXX ABI.  */
19899       if (!ISA_HAS_ODD_SPREG || TARGET_FLOATXX)
19900 	target_flags &= ~MASK_ODD_SPREG;
19901       else
19902 	target_flags |= MASK_ODD_SPREG;
19903     }
19904   else if (TARGET_ODD_SPREG && !ISA_HAS_ODD_SPREG)
19905     warning (0, "the %qs architecture does not support odd single-precision"
19906 	     " registers", mips_arch_info->name);
19907 
19908   if (!TARGET_ODD_SPREG && TARGET_64BIT)
19909     {
19910       error ("unsupported combination: %s", "-mgp64 -mno-odd-spreg");
19911       /* Allow compilation to continue further even though invalid output
19912          will be produced.  */
19913       target_flags |= MASK_ODD_SPREG;
19914     }
19915 
19916   if (!ISA_HAS_COMPACT_BRANCHES && mips_cb == MIPS_CB_ALWAYS)
19917     {
19918       error ("unsupported combination: %qs%s %s",
19919 	      mips_arch_info->name, TARGET_MICROMIPS ? " -mmicromips" : "",
19920 	      "-mcompact-branches=always");
19921     }
19922   else if (!ISA_HAS_DELAY_SLOTS && mips_cb == MIPS_CB_NEVER)
19923     {
19924       error ("unsupported combination: %qs%s %s",
19925 	      mips_arch_info->name, TARGET_MICROMIPS ? " -mmicromips" : "",
19926 	      "-mcompact-branches=never");
19927     }
19928 
19929   /* Require explicit relocs for MIPS R6 onwards.  This enables simplification
19930      of the compact branch and jump support through the backend.  */
19931   if (!TARGET_EXPLICIT_RELOCS && mips_isa_rev >= 6)
19932     {
19933       error ("unsupported combination: %qs %s",
19934 	     mips_arch_info->name, "-mno-explicit-relocs");
19935     }
19936 
19937   /* The effect of -mabicalls isn't defined for the EABI.  */
19938   if (mips_abi == ABI_EABI && TARGET_ABICALLS)
19939     {
19940       error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
19941       target_flags &= ~MASK_ABICALLS;
19942     }
19943 
19944   /* PIC requires -mabicalls.  */
19945   if (flag_pic)
19946     {
19947       if (mips_abi == ABI_EABI)
19948 	error ("cannot generate position-independent code for %qs",
19949 	       "-mabi=eabi");
19950       else if (!TARGET_ABICALLS)
19951 	error ("position-independent code requires %qs", "-mabicalls");
19952     }
19953 
19954   if (TARGET_ABICALLS_PIC2)
19955     /* We need to set flag_pic for executables as well as DSOs
19956        because we may reference symbols that are not defined in
19957        the final executable.  (MIPS does not use things like
19958        copy relocs, for example.)
19959 
19960        There is a body of code that uses __PIC__ to distinguish
19961        between -mabicalls and -mno-abicalls code.  The non-__PIC__
19962        variant is usually appropriate for TARGET_ABICALLS_PIC0, as
19963        long as any indirect jumps use $25.  */
19964     flag_pic = 1;
19965 
19966   /* -mvr4130-align is a "speed over size" optimization: it usually produces
19967      faster code, but at the expense of more nops.  Enable it at -O3 and
19968      above.  */
19969   if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
19970     target_flags |= MASK_VR4130_ALIGN;
19971 
19972   /* Prefer a call to memcpy over inline code when optimizing for size,
19973      though see MOVE_RATIO in mips.h.  */
19974   if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
19975     target_flags |= MASK_MEMCPY;
19976 
19977   /* If we have a nonzero small-data limit, check that the -mgpopt
19978      setting is consistent with the other target flags.  */
19979   if (mips_small_data_threshold > 0)
19980     {
19981       if (!TARGET_GPOPT)
19982 	{
19983 	  if (!TARGET_EXPLICIT_RELOCS)
19984 	    error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
19985 
19986 	  TARGET_LOCAL_SDATA = false;
19987 	  TARGET_EXTERN_SDATA = false;
19988 	}
19989       else
19990 	{
19991 	  if (TARGET_VXWORKS_RTP)
19992 	    warning (0, "cannot use small-data accesses for %qs", "-mrtp");
19993 
19994 	  if (TARGET_ABICALLS)
19995 	    warning (0, "cannot use small-data accesses for %qs",
19996 		     "-mabicalls");
19997 	}
19998     }
19999 
20000   /* Set NaN and ABS defaults.  */
20001   if (mips_nan == MIPS_IEEE_754_DEFAULT && !ISA_HAS_IEEE_754_LEGACY)
20002     mips_nan = MIPS_IEEE_754_2008;
20003   if (mips_abs == MIPS_IEEE_754_DEFAULT && !ISA_HAS_IEEE_754_LEGACY)
20004     mips_abs = MIPS_IEEE_754_2008;
20005 
20006   /* Check for IEEE 754 legacy/2008 support.  */
20007   if ((mips_nan == MIPS_IEEE_754_LEGACY
20008        || mips_abs == MIPS_IEEE_754_LEGACY)
20009       && !ISA_HAS_IEEE_754_LEGACY)
20010     warning (0, "the %qs architecture does not support %<-m%s=legacy%>",
20011 	     mips_arch_info->name,
20012 	     mips_nan == MIPS_IEEE_754_LEGACY ? "nan" : "abs");
20013 
20014   if ((mips_nan == MIPS_IEEE_754_2008
20015        || mips_abs == MIPS_IEEE_754_2008)
20016       && !ISA_HAS_IEEE_754_2008)
20017     warning (0, "the %qs architecture does not support %<-m%s=2008%>",
20018 	     mips_arch_info->name,
20019 	     mips_nan == MIPS_IEEE_754_2008 ? "nan" : "abs");
20020 
20021   /* Pre-IEEE 754-2008 MIPS hardware has a quirky almost-IEEE format
20022      for all its floating point.  */
20023   if (mips_nan != MIPS_IEEE_754_2008)
20024     {
20025       REAL_MODE_FORMAT (SFmode) = &mips_single_format;
20026       REAL_MODE_FORMAT (DFmode) = &mips_double_format;
20027       REAL_MODE_FORMAT (TFmode) = &mips_quad_format;
20028     }
20029 
20030   /* Make sure that the user didn't turn off paired single support when
20031      MIPS-3D support is requested.  */
20032   if (TARGET_MIPS3D
20033       && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
20034       && !TARGET_PAIRED_SINGLE_FLOAT)
20035     error ("%<-mips3d%> requires %<-mpaired-single%>");
20036 
20037   /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT.  */
20038   if (TARGET_MIPS3D)
20039     target_flags |= MASK_PAIRED_SINGLE_FLOAT;
20040 
20041   /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
20042      and TARGET_HARD_FLOAT_ABI are both true.  */
20043   if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
20044     {
20045       error ("%qs must be used with %qs",
20046 	     TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
20047 	     TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
20048       target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
20049       TARGET_MIPS3D = 0;
20050     }
20051 
20052   /* Make sure that when ISA_HAS_MSA is true, TARGET_FLOAT64 and
20053      TARGET_HARD_FLOAT_ABI and  both true.  */
20054   if (ISA_HAS_MSA && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
20055     error ("%<-mmsa%> must be used with %<-mfp64%> and %<-mhard-float%>");
20056 
20057   /* Make sure that -mpaired-single is only used on ISAs that support it.
20058      We must disable it otherwise since it relies on other ISA properties
20059      like ISA_HAS_8CC having their normal values.  */
20060   if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
20061     {
20062       error ("the %qs architecture does not support paired-single"
20063 	     " instructions", mips_arch_info->name);
20064       target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
20065       TARGET_MIPS3D = 0;
20066     }
20067 
20068   if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
20069       && !TARGET_CACHE_BUILTIN)
20070     {
20071       error ("%qs requires a target that provides the %qs instruction",
20072 	     "-mr10k-cache-barrier", "cache");
20073       mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
20074     }
20075 
20076   /* If TARGET_DSPR2, enable TARGET_DSP.  */
20077   if (TARGET_DSPR2)
20078     TARGET_DSP = true;
20079 
20080   if (TARGET_DSP && mips_isa_rev >= 6)
20081     {
20082       error ("the %qs architecture does not support DSP instructions",
20083 	     mips_arch_info->name);
20084       TARGET_DSP = false;
20085       TARGET_DSPR2 = false;
20086     }
20087 
20088   /* .eh_frame addresses should be the same width as a C pointer.
20089      Most MIPS ABIs support only one pointer size, so the assembler
20090      will usually know exactly how big an .eh_frame address is.
20091 
20092      Unfortunately, this is not true of the 64-bit EABI.  The ABI was
20093      originally defined to use 64-bit pointers (i.e. it is LP64), and
20094      this is still the default mode.  However, we also support an n32-like
20095      ILP32 mode, which is selected by -mlong32.  The problem is that the
20096      assembler has traditionally not had an -mlong option, so it has
20097      traditionally not known whether we're using the ILP32 or LP64 form.
20098 
20099      As it happens, gas versions up to and including 2.19 use _32-bit_
20100      addresses for EABI64 .cfi_* directives.  This is wrong for the
20101      default LP64 mode, so we can't use the directives by default.
20102      Moreover, since gas's current behavior is at odds with gcc's
20103      default behavior, it seems unwise to rely on future versions
20104      of gas behaving the same way.  We therefore avoid using .cfi
20105      directives for -mlong32 as well.  */
20106   if (mips_abi == ABI_EABI && TARGET_64BIT)
20107     flag_dwarf2_cfi_asm = 0;
20108 
20109   /* .cfi_* directives generate a read-only section, so fall back on
20110      manual .eh_frame creation if we need the section to be writable.  */
20111   if (TARGET_WRITABLE_EH_FRAME)
20112     flag_dwarf2_cfi_asm = 0;
20113 
20114   mips_init_print_operand_punct ();
20115 
20116   /* Set up array to map GCC register number to debug register number.
20117      Ignore the special purpose register numbers.  */
20118 
20119   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
20120     {
20121       mips_dbx_regno[i] = IGNORED_DWARF_REGNUM;
20122       if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
20123 	mips_dwarf_regno[i] = i;
20124       else
20125 	mips_dwarf_regno[i] = INVALID_REGNUM;
20126     }
20127 
20128   start = GP_DBX_FIRST - GP_REG_FIRST;
20129   for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
20130     mips_dbx_regno[i] = i + start;
20131 
20132   start = FP_DBX_FIRST - FP_REG_FIRST;
20133   for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
20134     mips_dbx_regno[i] = i + start;
20135 
20136   /* Accumulator debug registers use big-endian ordering.  */
20137   mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
20138   mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
20139   mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
20140   mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
20141   for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
20142     {
20143       mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
20144       mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
20145     }
20146 
20147   /* Set up mips_hard_regno_mode_ok.  */
20148   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
20149     for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
20150       mips_hard_regno_mode_ok_p[mode][regno]
20151 	= mips_hard_regno_mode_ok_uncached (regno, (machine_mode) mode);
20152 
20153   /* Function to allocate machine-dependent function status.  */
20154   init_machine_status = &mips_init_machine_status;
20155 
20156   /* Default to working around R4000 errata only if the processor
20157      was selected explicitly.  */
20158   if ((target_flags_explicit & MASK_FIX_R4000) == 0
20159       && strcmp (mips_arch_info->name, "r4000") == 0)
20160     target_flags |= MASK_FIX_R4000;
20161 
20162   /* Default to working around R4400 errata only if the processor
20163      was selected explicitly.  */
20164   if ((target_flags_explicit & MASK_FIX_R4400) == 0
20165       && strcmp (mips_arch_info->name, "r4400") == 0)
20166     target_flags |= MASK_FIX_R4400;
20167 
20168   /* Default to working around R10000 errata only if the processor
20169      was selected explicitly.  */
20170   if ((target_flags_explicit & MASK_FIX_R10000) == 0
20171       && strcmp (mips_arch_info->name, "r10000") == 0)
20172     target_flags |= MASK_FIX_R10000;
20173 
20174   /* Make sure that branch-likely instructions available when using
20175      -mfix-r10000.  The instructions are not available if either:
20176 
20177 	1. -mno-branch-likely was passed.
20178 	2. The selected ISA does not support branch-likely and
20179 	   the command line does not include -mbranch-likely.  */
20180   if (TARGET_FIX_R10000
20181       && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
20182           ? !ISA_HAS_BRANCHLIKELY
20183           : !TARGET_BRANCHLIKELY))
20184     sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
20185 
20186   if (TARGET_SYNCI && !ISA_HAS_SYNCI)
20187     {
20188       warning (0, "the %qs architecture does not support the synci "
20189 	       "instruction", mips_arch_info->name);
20190       target_flags &= ~MASK_SYNCI;
20191     }
20192 
20193   /* Only optimize PIC indirect calls if they are actually required.  */
20194   if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
20195     target_flags &= ~MASK_RELAX_PIC_CALLS;
20196 
20197   /* Save base state of options.  */
20198   mips_base_target_flags = target_flags;
20199   mips_base_schedule_insns = flag_schedule_insns;
20200   mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
20201   mips_base_move_loop_invariants = flag_move_loop_invariants;
20202   mips_base_align_loops = align_loops;
20203   mips_base_align_jumps = align_jumps;
20204   mips_base_align_functions = align_functions;
20205 
20206   /* Now select the ISA mode.
20207 
20208      Do all CPP-sensitive stuff in uncompressed mode; we'll switch modes
20209      later if required.  */
20210   mips_set_compression_mode (0);
20211 
20212   /* We register a second machine specific reorg pass after delay slot
20213      filling.  Registering the pass must be done at start up.  It's
20214      convenient to do it here.  */
20215   opt_pass *new_pass = make_pass_mips_machine_reorg2 (g);
20216   struct register_pass_info insert_pass_mips_machine_reorg2 =
20217     {
20218       new_pass,		/* pass */
20219       "dbr",			/* reference_pass_name */
20220       1,			/* ref_pass_instance_number */
20221       PASS_POS_INSERT_AFTER	/* po_op */
20222     };
20223   register_pass (&insert_pass_mips_machine_reorg2);
20224 
20225   if (TARGET_HARD_FLOAT_ABI && TARGET_MIPS5900)
20226     REAL_MODE_FORMAT (SFmode) = &spu_single_format;
20227 
20228   mips_register_frame_header_opt ();
20229 }
20230 
20231 /* Swap the register information for registers I and I + 1, which
20232    currently have the wrong endianness.  Note that the registers'
20233    fixedness and call-clobberedness might have been set on the
20234    command line.  */
20235 
20236 static void
mips_swap_registers(unsigned int i)20237 mips_swap_registers (unsigned int i)
20238 {
20239   int tmpi;
20240   const char *tmps;
20241 
20242 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
20243 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
20244 
20245   SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
20246   SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
20247   SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
20248   SWAP_STRING (reg_names[i], reg_names[i + 1]);
20249 
20250 #undef SWAP_STRING
20251 #undef SWAP_INT
20252 }
20253 
20254 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE.  */
20255 
20256 static void
mips_conditional_register_usage(void)20257 mips_conditional_register_usage (void)
20258 {
20259 
20260   if (ISA_HAS_DSP)
20261     {
20262       /* These DSP control register fields are global.  */
20263       global_regs[CCDSP_PO_REGNUM] = 1;
20264       global_regs[CCDSP_SC_REGNUM] = 1;
20265     }
20266   else
20267     AND_COMPL_HARD_REG_SET (accessible_reg_set,
20268 			    reg_class_contents[(int) DSP_ACC_REGS]);
20269 
20270   if (!ISA_HAS_HILO)
20271     AND_COMPL_HARD_REG_SET (accessible_reg_set,
20272 			    reg_class_contents[(int) MD_REGS]);
20273 
20274   if (!TARGET_HARD_FLOAT)
20275     {
20276       AND_COMPL_HARD_REG_SET (accessible_reg_set,
20277 			      reg_class_contents[(int) FP_REGS]);
20278       AND_COMPL_HARD_REG_SET (accessible_reg_set,
20279 			      reg_class_contents[(int) ST_REGS]);
20280     }
20281   else if (!ISA_HAS_8CC)
20282     {
20283       /* We only have a single condition-code register.  We implement
20284 	 this by fixing all the condition-code registers and generating
20285 	 RTL that refers directly to ST_REG_FIRST.  */
20286       AND_COMPL_HARD_REG_SET (accessible_reg_set,
20287 			      reg_class_contents[(int) ST_REGS]);
20288       if (!ISA_HAS_CCF)
20289 	SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
20290       fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
20291     }
20292   if (TARGET_MIPS16)
20293     {
20294       /* In MIPS16 mode, we prohibit the unused $s registers, since they
20295 	 are call-saved, and saving them via a MIPS16 register would
20296 	 probably waste more time than just reloading the value.
20297 
20298 	 We permit the $t temporary registers when optimizing for speed
20299 	 but not when optimizing for space because using them results in
20300 	 code that is larger (but faster) then not using them.  We do
20301 	 allow $24 (t8) because it is used in CMP and CMPI instructions
20302 	 and $25 (t9) because it is used as the function call address in
20303 	 SVR4 PIC code.  */
20304 
20305       fixed_regs[18] = call_used_regs[18] = 1;
20306       fixed_regs[19] = call_used_regs[19] = 1;
20307       fixed_regs[20] = call_used_regs[20] = 1;
20308       fixed_regs[21] = call_used_regs[21] = 1;
20309       fixed_regs[22] = call_used_regs[22] = 1;
20310       fixed_regs[23] = call_used_regs[23] = 1;
20311       fixed_regs[26] = call_used_regs[26] = 1;
20312       fixed_regs[27] = call_used_regs[27] = 1;
20313       fixed_regs[30] = call_used_regs[30] = 1;
20314       if (optimize_size)
20315 	{
20316 	  fixed_regs[8] = call_used_regs[8] = 1;
20317 	  fixed_regs[9] = call_used_regs[9] = 1;
20318 	  fixed_regs[10] = call_used_regs[10] = 1;
20319 	  fixed_regs[11] = call_used_regs[11] = 1;
20320 	  fixed_regs[12] = call_used_regs[12] = 1;
20321 	  fixed_regs[13] = call_used_regs[13] = 1;
20322 	  fixed_regs[14] = call_used_regs[14] = 1;
20323 	  fixed_regs[15] = call_used_regs[15] = 1;
20324 	}
20325 
20326       /* Do not allow HI and LO to be treated as register operands.
20327 	 There are no MTHI or MTLO instructions (or any real need
20328 	 for them) and one-way registers cannot easily be reloaded.  */
20329       AND_COMPL_HARD_REG_SET (operand_reg_set,
20330 			      reg_class_contents[(int) MD_REGS]);
20331     }
20332   /* $f20-$f23 are call-clobbered for n64.  */
20333   if (mips_abi == ABI_64)
20334     {
20335       int regno;
20336       for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
20337 	call_really_used_regs[regno] = call_used_regs[regno] = 1;
20338     }
20339   /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
20340      for n32 and o32 FP64.  */
20341   if (mips_abi == ABI_N32
20342       || (mips_abi == ABI_32
20343           && TARGET_FLOAT64))
20344     {
20345       int regno;
20346       for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
20347 	call_really_used_regs[regno] = call_used_regs[regno] = 1;
20348     }
20349   /* Make sure that double-register accumulator values are correctly
20350      ordered for the current endianness.  */
20351   if (TARGET_LITTLE_ENDIAN)
20352     {
20353       unsigned int regno;
20354 
20355       mips_swap_registers (MD_REG_FIRST);
20356       for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
20357 	mips_swap_registers (regno);
20358     }
20359 }
20360 
20361 /* Implement EH_USES.  */
20362 
20363 bool
mips_eh_uses(unsigned int regno)20364 mips_eh_uses (unsigned int regno)
20365 {
20366   if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
20367     {
20368       /* We need to force certain registers to be live in order to handle
20369 	 PIC long branches correctly.  See mips_must_initialize_gp_p for
20370 	 details.  */
20371       if (mips_cfun_has_cprestore_slot_p ())
20372 	{
20373 	  if (regno == CPRESTORE_SLOT_REGNUM)
20374 	    return true;
20375 	}
20376       else
20377 	{
20378 	  if (cfun->machine->global_pointer == regno)
20379 	    return true;
20380 	}
20381     }
20382 
20383   return false;
20384 }
20385 
20386 /* Implement EPILOGUE_USES.  */
20387 
20388 bool
mips_epilogue_uses(unsigned int regno)20389 mips_epilogue_uses (unsigned int regno)
20390 {
20391   /* Say that the epilogue uses the return address register.  Note that
20392      in the case of sibcalls, the values "used by the epilogue" are
20393      considered live at the start of the called function.  */
20394   if (regno == RETURN_ADDR_REGNUM)
20395     return true;
20396 
20397   /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
20398      See the comment above load_call<mode> for details.  */
20399   if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
20400     return true;
20401 
20402   /* An interrupt handler must preserve some registers that are
20403      ordinarily call-clobbered.  */
20404   if (cfun->machine->interrupt_handler_p
20405       && mips_interrupt_extra_call_saved_reg_p (regno))
20406     return true;
20407 
20408   return false;
20409 }
20410 
20411 /* Return true if INSN needs to be wrapped in ".set noat".
20412    INSN has NOPERANDS operands, stored in OPVEC.  */
20413 
20414 static bool
mips_need_noat_wrapper_p(rtx_insn * insn,rtx * opvec,int noperands)20415 mips_need_noat_wrapper_p (rtx_insn *insn, rtx *opvec, int noperands)
20416 {
20417   if (recog_memoized (insn) >= 0)
20418     {
20419       subrtx_iterator::array_type array;
20420       for (int i = 0; i < noperands; i++)
20421 	FOR_EACH_SUBRTX (iter, array, opvec[i], NONCONST)
20422 	  if (REG_P (*iter) && REGNO (*iter) == AT_REGNUM)
20423 	    return true;
20424     }
20425   return false;
20426 }
20427 
20428 /* Implement FINAL_PRESCAN_INSN.  Mark MIPS16 inline constant pools
20429    as data for the purpose of disassembly.  For simplicity embed the
20430    pool's initial label number in the local symbol produced so that
20431    multiple pools within a single function end up marked with unique
20432    symbols.  The label number is carried by the `consttable' insn
20433    emitted at the beginning of each pool.  */
20434 
20435 void
mips_final_prescan_insn(rtx_insn * insn,rtx * opvec,int noperands)20436 mips_final_prescan_insn (rtx_insn *insn, rtx *opvec, int noperands)
20437 {
20438   if (INSN_P (insn)
20439       && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
20440       && XINT (PATTERN (insn), 1) == UNSPEC_CONSTTABLE)
20441     mips_set_text_contents_type (asm_out_file, "__pool_",
20442 				 INTVAL (XVECEXP (PATTERN (insn), 0, 0)),
20443 				 FALSE);
20444 
20445   if (mips_need_noat_wrapper_p (insn, opvec, noperands))
20446     mips_push_asm_switch (&mips_noat);
20447 }
20448 
20449 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN.  Reset text marking to
20450    code after a MIPS16 inline constant pool.  Like with the beginning
20451    of a pool table use the pool's initial label number to keep symbols
20452    unique.  The label number is carried by the `consttable_end' insn
20453    emitted at the end of each pool.  */
20454 
20455 static void
mips_final_postscan_insn(FILE * file ATTRIBUTE_UNUSED,rtx_insn * insn,rtx * opvec,int noperands)20456 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx_insn *insn,
20457 			  rtx *opvec, int noperands)
20458 {
20459   if (mips_need_noat_wrapper_p (insn, opvec, noperands))
20460     mips_pop_asm_switch (&mips_noat);
20461 
20462   if (INSN_P (insn)
20463       && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
20464       && XINT (PATTERN (insn), 1) == UNSPEC_CONSTTABLE_END)
20465     mips_set_text_contents_type (asm_out_file, "__pend_",
20466 				 INTVAL (XVECEXP (PATTERN (insn), 0, 0)),
20467 				 TRUE);
20468 }
20469 
20470 /* Return the function that is used to expand the <u>mulsidi3 pattern.
20471    EXT_CODE is the code of the extension used.  Return NULL if widening
20472    multiplication shouldn't be used.  */
20473 
20474 mulsidi3_gen_fn
mips_mulsidi3_gen_fn(enum rtx_code ext_code)20475 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
20476 {
20477   bool signed_p;
20478 
20479   signed_p = ext_code == SIGN_EXTEND;
20480   if (TARGET_64BIT)
20481     {
20482       /* Don't use widening multiplication with MULT when we have DMUL.  Even
20483 	 with the extension of its input operands DMUL is faster.  Note that
20484 	 the extension is not needed for signed multiplication.  In order to
20485 	 ensure that we always remove the redundant sign-extension in this
20486 	 case we still expand mulsidi3 for DMUL.  */
20487       if (ISA_HAS_R6DMUL)
20488 	return signed_p ? gen_mulsidi3_64bit_r6dmul : NULL;
20489       if (ISA_HAS_DMUL3)
20490 	return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
20491       if (TARGET_MIPS16)
20492 	return (signed_p
20493 		? gen_mulsidi3_64bit_mips16
20494 		: gen_umulsidi3_64bit_mips16);
20495       if (TARGET_FIX_R4000)
20496 	return NULL;
20497       return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
20498     }
20499   else
20500     {
20501       if (ISA_HAS_R6MUL)
20502 	return (signed_p ? gen_mulsidi3_32bit_r6 : gen_umulsidi3_32bit_r6);
20503       if (TARGET_MIPS16)
20504 	return (signed_p
20505 		? gen_mulsidi3_32bit_mips16
20506 		: gen_umulsidi3_32bit_mips16);
20507       if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
20508 	return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
20509       return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
20510     }
20511 }
20512 
20513 /* Return true if PATTERN matches the kind of instruction generated by
20514    umips_build_save_restore.  SAVE_P is true for store.  */
20515 
20516 bool
umips_save_restore_pattern_p(bool save_p,rtx pattern)20517 umips_save_restore_pattern_p (bool save_p, rtx pattern)
20518 {
20519   int n;
20520   unsigned int i;
20521   HOST_WIDE_INT first_offset = 0;
20522   rtx first_base = 0;
20523   unsigned int regmask = 0;
20524 
20525   for (n = 0; n < XVECLEN (pattern, 0); n++)
20526     {
20527       rtx set, reg, mem, this_base;
20528       HOST_WIDE_INT this_offset;
20529 
20530       /* Check that we have a SET.  */
20531       set = XVECEXP (pattern, 0, n);
20532       if (GET_CODE (set) != SET)
20533 	return false;
20534 
20535       /* Check that the SET is a load (if restoring) or a store
20536 	 (if saving).  */
20537       mem = save_p ? SET_DEST (set) : SET_SRC (set);
20538       if (!MEM_P (mem) || MEM_VOLATILE_P (mem))
20539 	return false;
20540 
20541       /* Check that the address is the sum of base and a possibly-zero
20542 	 constant offset.  Determine if the offset is in range.  */
20543       mips_split_plus (XEXP (mem, 0), &this_base, &this_offset);
20544       if (!REG_P (this_base))
20545 	return false;
20546 
20547       if (n == 0)
20548 	{
20549 	  if (!UMIPS_12BIT_OFFSET_P (this_offset))
20550 	    return false;
20551 	  first_base = this_base;
20552 	  first_offset = this_offset;
20553 	}
20554       else
20555 	{
20556 	  /* Check that the save slots are consecutive.  */
20557 	  if (REGNO (this_base) != REGNO (first_base)
20558 	      || this_offset != first_offset + UNITS_PER_WORD * n)
20559 	    return false;
20560 	}
20561 
20562       /* Check that SET's other operand is a register.  */
20563       reg = save_p ? SET_SRC (set) : SET_DEST (set);
20564       if (!REG_P (reg))
20565 	return false;
20566 
20567       regmask |= 1 << REGNO (reg);
20568     }
20569 
20570   for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
20571     if (regmask == umips_swm_mask[i])
20572       return true;
20573 
20574   return false;
20575 }
20576 
20577 /* Return the assembly instruction for microMIPS LWM or SWM.
20578    SAVE_P and PATTERN are as for umips_save_restore_pattern_p.  */
20579 
20580 const char *
umips_output_save_restore(bool save_p,rtx pattern)20581 umips_output_save_restore (bool save_p, rtx pattern)
20582 {
20583   static char buffer[300];
20584   char *s;
20585   int n;
20586   HOST_WIDE_INT offset;
20587   rtx base, mem, set, last_set, last_reg;
20588 
20589   /* Parse the pattern.  */
20590   gcc_assert (umips_save_restore_pattern_p (save_p, pattern));
20591 
20592   s = strcpy (buffer, save_p ? "swm\t" : "lwm\t");
20593   s += strlen (s);
20594   n = XVECLEN (pattern, 0);
20595 
20596   set = XVECEXP (pattern, 0, 0);
20597   mem = save_p ? SET_DEST (set) : SET_SRC (set);
20598   mips_split_plus (XEXP (mem, 0), &base, &offset);
20599 
20600   last_set = XVECEXP (pattern, 0, n - 1);
20601   last_reg = save_p ? SET_SRC (last_set) : SET_DEST (last_set);
20602 
20603   if (REGNO (last_reg) == 31)
20604     n--;
20605 
20606   gcc_assert (n <= 9);
20607   if (n == 0)
20608     ;
20609   else if (n == 1)
20610     s += sprintf (s, "%s,", reg_names[16]);
20611   else if (n < 9)
20612     s += sprintf (s, "%s-%s,", reg_names[16], reg_names[15 + n]);
20613   else if (n == 9)
20614     s += sprintf (s, "%s-%s,%s,", reg_names[16], reg_names[23],
20615 		  reg_names[30]);
20616 
20617   if (REGNO (last_reg) == 31)
20618     s += sprintf (s, "%s,", reg_names[31]);
20619 
20620   s += sprintf (s, "%d(%s)", (int)offset, reg_names[REGNO (base)]);
20621   return buffer;
20622 }
20623 
20624 /* Return true if MEM1 and MEM2 use the same base register, and the
20625    offset of MEM2 equals the offset of MEM1 plus 4.  FIRST_REG is the
20626    register into (from) which the contents of MEM1 will be loaded
20627    (stored), depending on the value of LOAD_P.
20628    SWAP_P is true when the 1st and 2nd instructions are swapped.  */
20629 
20630 static bool
umips_load_store_pair_p_1(bool load_p,bool swap_p,rtx first_reg,rtx mem1,rtx mem2)20631 umips_load_store_pair_p_1 (bool load_p, bool swap_p,
20632 			   rtx first_reg, rtx mem1, rtx mem2)
20633 {
20634   rtx base1, base2;
20635   HOST_WIDE_INT offset1, offset2;
20636 
20637   if (!MEM_P (mem1) || !MEM_P (mem2))
20638     return false;
20639 
20640   mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
20641   mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
20642 
20643   if (!REG_P (base1) || !rtx_equal_p (base1, base2))
20644     return false;
20645 
20646   /* Avoid invalid load pair instructions.  */
20647   if (load_p && REGNO (first_reg) == REGNO (base1))
20648     return false;
20649 
20650   /* We must avoid this case for anti-dependence.
20651      Ex:  lw $3, 4($3)
20652           lw $2, 0($3)
20653      first_reg is $2, but the base is $3.  */
20654   if (load_p
20655       && swap_p
20656       && REGNO (first_reg) + 1 == REGNO (base1))
20657     return false;
20658 
20659   if (offset2 != offset1 + 4)
20660     return false;
20661 
20662   if (!UMIPS_12BIT_OFFSET_P (offset1))
20663     return false;
20664 
20665   return true;
20666 }
20667 
20668 bool
mips_load_store_bonding_p(rtx * operands,machine_mode mode,bool load_p)20669 mips_load_store_bonding_p (rtx *operands, machine_mode mode, bool load_p)
20670 {
20671   rtx reg1, reg2, mem1, mem2, base1, base2;
20672   enum reg_class rc1, rc2;
20673   HOST_WIDE_INT offset1, offset2;
20674 
20675   if (load_p)
20676     {
20677       reg1 = operands[0];
20678       reg2 = operands[2];
20679       mem1 = operands[1];
20680       mem2 = operands[3];
20681     }
20682   else
20683     {
20684       reg1 = operands[1];
20685       reg2 = operands[3];
20686       mem1 = operands[0];
20687       mem2 = operands[2];
20688     }
20689 
20690   if (mips_address_insns (XEXP (mem1, 0), mode, false) == 0
20691       || mips_address_insns (XEXP (mem2, 0), mode, false) == 0)
20692     return false;
20693 
20694   mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
20695   mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
20696 
20697   /* Base regs do not match.  */
20698   if (!REG_P (base1) || !rtx_equal_p (base1, base2))
20699     return false;
20700 
20701   /* Either of the loads is clobbering base register.  It is legitimate to bond
20702      loads if second load clobbers base register.  However, hardware does not
20703      support such bonding.  */
20704   if (load_p
20705       && (REGNO (reg1) == REGNO (base1)
20706 	  || (REGNO (reg2) == REGNO (base1))))
20707     return false;
20708 
20709   /* Loading in same registers.  */
20710   if (load_p
20711       && REGNO (reg1) == REGNO (reg2))
20712     return false;
20713 
20714   /* The loads/stores are not of same type.  */
20715   rc1 = REGNO_REG_CLASS (REGNO (reg1));
20716   rc2 = REGNO_REG_CLASS (REGNO (reg2));
20717   if (rc1 != rc2
20718       && !reg_class_subset_p (rc1, rc2)
20719       && !reg_class_subset_p (rc2, rc1))
20720     return false;
20721 
20722   if (abs (offset1 - offset2) != GET_MODE_SIZE (mode))
20723     return false;
20724 
20725   return true;
20726 }
20727 
20728 /* OPERANDS describes the operands to a pair of SETs, in the order
20729    dest1, src1, dest2, src2.  Return true if the operands can be used
20730    in an LWP or SWP instruction; LOAD_P says which.  */
20731 
20732 bool
umips_load_store_pair_p(bool load_p,rtx * operands)20733 umips_load_store_pair_p (bool load_p, rtx *operands)
20734 {
20735   rtx reg1, reg2, mem1, mem2;
20736 
20737   if (load_p)
20738     {
20739       reg1 = operands[0];
20740       reg2 = operands[2];
20741       mem1 = operands[1];
20742       mem2 = operands[3];
20743     }
20744   else
20745     {
20746       reg1 = operands[1];
20747       reg2 = operands[3];
20748       mem1 = operands[0];
20749       mem2 = operands[2];
20750     }
20751 
20752   if (REGNO (reg2) == REGNO (reg1) + 1)
20753     return umips_load_store_pair_p_1 (load_p, false, reg1, mem1, mem2);
20754 
20755   if (REGNO (reg1) == REGNO (reg2) + 1)
20756     return umips_load_store_pair_p_1 (load_p, true, reg2, mem2, mem1);
20757 
20758   return false;
20759 }
20760 
20761 /* Return the assembly instruction for a microMIPS LWP or SWP in which
20762    the first register is REG and the first memory slot is MEM.
20763    LOAD_P is true for LWP.  */
20764 
20765 static void
umips_output_load_store_pair_1(bool load_p,rtx reg,rtx mem)20766 umips_output_load_store_pair_1 (bool load_p, rtx reg, rtx mem)
20767 {
20768   rtx ops[] = {reg, mem};
20769 
20770   if (load_p)
20771     output_asm_insn ("lwp\t%0,%1", ops);
20772   else
20773     output_asm_insn ("swp\t%0,%1", ops);
20774 }
20775 
20776 /* Output the assembly instruction for a microMIPS LWP or SWP instruction.
20777    LOAD_P and OPERANDS are as for umips_load_store_pair_p.  */
20778 
20779 void
umips_output_load_store_pair(bool load_p,rtx * operands)20780 umips_output_load_store_pair (bool load_p, rtx *operands)
20781 {
20782   rtx reg1, reg2, mem1, mem2;
20783   if (load_p)
20784     {
20785       reg1 = operands[0];
20786       reg2 = operands[2];
20787       mem1 = operands[1];
20788       mem2 = operands[3];
20789     }
20790   else
20791     {
20792       reg1 = operands[1];
20793       reg2 = operands[3];
20794       mem1 = operands[0];
20795       mem2 = operands[2];
20796     }
20797 
20798   if (REGNO (reg2) == REGNO (reg1) + 1)
20799     {
20800       umips_output_load_store_pair_1 (load_p, reg1, mem1);
20801       return;
20802     }
20803 
20804   gcc_assert (REGNO (reg1) == REGNO (reg2) + 1);
20805   umips_output_load_store_pair_1 (load_p, reg2, mem2);
20806 }
20807 
20808 /* Return true if REG1 and REG2 match the criteria for a movep insn.  */
20809 
20810 bool
umips_movep_target_p(rtx reg1,rtx reg2)20811 umips_movep_target_p (rtx reg1, rtx reg2)
20812 {
20813   int regno1, regno2, pair;
20814   unsigned int i;
20815   static const int match[8] = {
20816     0x00000060, /* 5, 6 */
20817     0x000000a0, /* 5, 7 */
20818     0x000000c0, /* 6, 7 */
20819     0x00200010, /* 4, 21 */
20820     0x00400010, /* 4, 22 */
20821     0x00000030, /* 4, 5 */
20822     0x00000050, /* 4, 6 */
20823     0x00000090  /* 4, 7 */
20824   };
20825 
20826   if (!REG_P (reg1) || !REG_P (reg2))
20827     return false;
20828 
20829   regno1 = REGNO (reg1);
20830   regno2 = REGNO (reg2);
20831 
20832   if (!GP_REG_P (regno1) || !GP_REG_P (regno2))
20833     return false;
20834 
20835   pair = (1 << regno1) | (1 << regno2);
20836 
20837   for (i = 0; i < ARRAY_SIZE (match); i++)
20838     if (pair == match[i])
20839       return true;
20840 
20841   return false;
20842 }
20843 
20844 /* Return the size in bytes of the trampoline code, padded to
20845    TRAMPOLINE_ALIGNMENT bits.  The static chain pointer and target
20846    function address immediately follow.  */
20847 
20848 int
mips_trampoline_code_size(void)20849 mips_trampoline_code_size (void)
20850 {
20851   if (TARGET_USE_PIC_FN_ADDR_REG)
20852     return 4 * 4;
20853   else if (ptr_mode == DImode)
20854     return 8 * 4;
20855   else if (ISA_HAS_LOAD_DELAY)
20856     return 6 * 4;
20857   else
20858     return 4 * 4;
20859 }
20860 
20861 /* Implement TARGET_TRAMPOLINE_INIT.  */
20862 
20863 static void
mips_trampoline_init(rtx m_tramp,tree fndecl,rtx chain_value)20864 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
20865 {
20866   rtx addr, end_addr, high, low, opcode, mem;
20867   rtx trampoline[8];
20868   unsigned int i, j;
20869   HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
20870 
20871   /* Work out the offsets of the pointers from the start of the
20872      trampoline code.  */
20873   end_addr_offset = mips_trampoline_code_size ();
20874   static_chain_offset = end_addr_offset;
20875   target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
20876 
20877   /* Get pointers to the beginning and end of the code block.  */
20878   addr = force_reg (Pmode, XEXP (m_tramp, 0));
20879   end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
20880 
20881 #define OP(X) gen_int_mode (X, SImode)
20882 
20883   /* Build up the code in TRAMPOLINE.  */
20884   i = 0;
20885   if (TARGET_USE_PIC_FN_ADDR_REG)
20886     {
20887       /* $25 contains the address of the trampoline.  Emit code of the form:
20888 
20889 	     l[wd]    $1, target_function_offset($25)
20890 	     l[wd]    $static_chain, static_chain_offset($25)
20891 	     jr       $1
20892 	     move     $25,$1.  */
20893       trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
20894 					   target_function_offset,
20895 					   PIC_FUNCTION_ADDR_REGNUM));
20896       trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
20897 					   static_chain_offset,
20898 					   PIC_FUNCTION_ADDR_REGNUM));
20899       trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
20900       trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
20901     }
20902   else if (ptr_mode == DImode)
20903     {
20904       /* It's too cumbersome to create the full 64-bit address, so let's
20905 	 instead use:
20906 
20907 	     move    $1, $31
20908 	     bal     1f
20909 	     nop
20910 	 1:  l[wd]   $25, target_function_offset - 12($31)
20911 	     l[wd]   $static_chain, static_chain_offset - 12($31)
20912 	     jr      $25
20913 	     move    $31, $1
20914 
20915 	where 12 is the offset of "1:" from the start of the code block.  */
20916       trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
20917       trampoline[i++] = OP (MIPS_BAL (1));
20918       trampoline[i++] = OP (MIPS_NOP);
20919       trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
20920 					   target_function_offset - 12,
20921 					   RETURN_ADDR_REGNUM));
20922       trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
20923 					   static_chain_offset - 12,
20924 					   RETURN_ADDR_REGNUM));
20925       trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
20926       trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
20927     }
20928   else
20929     {
20930       /* If the target has load delays, emit:
20931 
20932 	     lui     $1, %hi(end_addr)
20933 	     lw      $25, %lo(end_addr + ...)($1)
20934 	     lw      $static_chain, %lo(end_addr + ...)($1)
20935 	     jr      $25
20936 	     nop
20937 
20938 	 Otherwise emit:
20939 
20940 	     lui     $1, %hi(end_addr)
20941 	     lw      $25, %lo(end_addr + ...)($1)
20942 	     jr      $25
20943 	     lw      $static_chain, %lo(end_addr + ...)($1).  */
20944 
20945       /* Split END_ADDR into %hi and %lo values.  Trampolines are aligned
20946 	 to 64 bits, so the %lo value will have the bottom 3 bits clear.  */
20947       high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
20948 				  NULL, false, OPTAB_WIDEN);
20949       high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
20950 				  NULL, false, OPTAB_WIDEN);
20951       low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
20952 
20953       /* Emit the LUI.  */
20954       opcode = OP (MIPS_LUI (AT_REGNUM, 0));
20955       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
20956 					     NULL, false, OPTAB_WIDEN);
20957 
20958       /* Emit the load of the target function.  */
20959       opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
20960 				  target_function_offset - end_addr_offset,
20961 				  AT_REGNUM));
20962       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
20963 					     NULL, false, OPTAB_WIDEN);
20964 
20965       /* Emit the JR here, if we can.  */
20966       if (!ISA_HAS_LOAD_DELAY)
20967 	trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
20968 
20969       /* Emit the load of the static chain register.  */
20970       opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
20971 				  static_chain_offset - end_addr_offset,
20972 				  AT_REGNUM));
20973       trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
20974 					     NULL, false, OPTAB_WIDEN);
20975 
20976       /* Emit the JR, if we couldn't above.  */
20977       if (ISA_HAS_LOAD_DELAY)
20978 	{
20979 	  trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
20980 	  trampoline[i++] = OP (MIPS_NOP);
20981 	}
20982     }
20983 
20984 #undef OP
20985 
20986   /* If we are using compact branches we don't have delay slots so
20987      place the instruction that was in the delay slot before the JRC
20988      instruction.  */
20989 
20990   if (TARGET_CB_ALWAYS)
20991     {
20992       rtx temp;
20993       temp = trampoline[i-2];
20994       trampoline[i-2] = trampoline[i-1];
20995       trampoline[i-1] = temp;
20996     }
20997 
20998   /* Copy the trampoline code.  Leave any padding uninitialized.  */
20999   for (j = 0; j < i; j++)
21000     {
21001       mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
21002       mips_emit_move (mem, trampoline[j]);
21003     }
21004 
21005   /* Set up the static chain pointer field.  */
21006   mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
21007   mips_emit_move (mem, chain_value);
21008 
21009   /* Set up the target function field.  */
21010   mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
21011   mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
21012 
21013   /* Flush the code part of the trampoline.  */
21014   emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
21015   emit_insn (gen_clear_cache (addr, end_addr));
21016 }
21017 
21018 /* Implement FUNCTION_PROFILER.  */
21019 
mips_function_profiler(FILE * file)21020 void mips_function_profiler (FILE *file)
21021 {
21022   if (TARGET_MIPS16)
21023     sorry ("mips16 function profiling");
21024   if (TARGET_LONG_CALLS)
21025     {
21026       /* For TARGET_LONG_CALLS use $3 for the address of _mcount.  */
21027       if (Pmode == DImode)
21028 	fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
21029       else
21030 	fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
21031     }
21032   mips_push_asm_switch (&mips_noat);
21033   fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
21034 	   reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
21035   /* _mcount treats $2 as the static chain register.  */
21036   if (cfun->static_chain_decl != NULL)
21037     fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
21038 	     reg_names[STATIC_CHAIN_REGNUM]);
21039   if (TARGET_MCOUNT_RA_ADDRESS)
21040     {
21041       /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
21042 	 ra save location.  */
21043       if (cfun->machine->frame.ra_fp_offset == 0)
21044 	/* ra not saved, pass zero.  */
21045 	fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
21046       else
21047 	fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
21048 		 Pmode == DImode ? "dla" : "la", reg_names[12],
21049 		 cfun->machine->frame.ra_fp_offset,
21050 		 reg_names[STACK_POINTER_REGNUM]);
21051     }
21052   if (!TARGET_NEWABI)
21053     fprintf (file,
21054 	     "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from  stack\n",
21055 	     TARGET_64BIT ? "dsubu" : "subu",
21056 	     reg_names[STACK_POINTER_REGNUM],
21057 	     reg_names[STACK_POINTER_REGNUM],
21058 	     Pmode == DImode ? 16 : 8);
21059 
21060   if (TARGET_LONG_CALLS)
21061     fprintf (file, "\tjalr\t%s\n", reg_names[3]);
21062   else
21063     fprintf (file, "\tjal\t_mcount\n");
21064   mips_pop_asm_switch (&mips_noat);
21065   /* _mcount treats $2 as the static chain register.  */
21066   if (cfun->static_chain_decl != NULL)
21067     fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
21068 	     reg_names[2]);
21069 }
21070 
21071 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  We want to keep the default
21072    behavior of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
21073    when TARGET_LOONGSON_VECTORS is true.  */
21074 
21075 static unsigned HOST_WIDE_INT
mips_shift_truncation_mask(machine_mode mode)21076 mips_shift_truncation_mask (machine_mode mode)
21077 {
21078   if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
21079     return 0;
21080 
21081   return GET_MODE_BITSIZE (mode) - 1;
21082 }
21083 
21084 /* Implement TARGET_PREPARE_PCH_SAVE.  */
21085 
21086 static void
mips_prepare_pch_save(void)21087 mips_prepare_pch_save (void)
21088 {
21089   /* We are called in a context where the current compression vs.
21090      non-compression setting should be irrelevant.  The question then is:
21091      which setting makes most sense at load time?
21092 
21093      The PCH is loaded before the first token is read.  We should never have
21094      switched into a compression mode by that point, and thus should not have
21095      populated mips16_globals or micromips_globals.  Nor can we load the
21096      entire contents of mips16_globals or micromips_globals from the PCH file,
21097      because they contain a combination of GGC and non-GGC data.
21098 
21099      There is therefore no point in trying save the GGC part of
21100      mips16_globals/micromips_globals to the PCH file, or to preserve a
21101      compression setting across the PCH save and load.  The loading compiler
21102      would not have access to the non-GGC parts of mips16_globals or
21103      micromips_globals (either from the PCH file, or from a copy that the
21104      loading compiler generated itself) and would have to call target_reinit
21105      anyway.
21106 
21107      It therefore seems best to switch back to non-MIPS16 mode and
21108      non-microMIPS mode to save time, and to ensure that mips16_globals and
21109      micromips_globals remain null after a PCH load.  */
21110   mips_set_compression_mode (0);
21111   mips16_globals = 0;
21112   micromips_globals = 0;
21113 }
21114 
21115 /* Generate or test for an insn that supports a constant permutation.  */
21116 
21117 #define MAX_VECT_LEN 16
21118 
21119 struct expand_vec_perm_d
21120 {
21121   rtx target, op0, op1;
21122   unsigned char perm[MAX_VECT_LEN];
21123   machine_mode vmode;
21124   unsigned char nelt;
21125   bool one_vector_p;
21126   bool testing_p;
21127 };
21128 
21129 /* Construct (set target (vec_select op0 (parallel perm))) and
21130    return true if that's a valid instruction in the active ISA.  */
21131 
21132 static bool
mips_expand_vselect(rtx target,rtx op0,const unsigned char * perm,unsigned nelt)21133 mips_expand_vselect (rtx target, rtx op0,
21134 		     const unsigned char *perm, unsigned nelt)
21135 {
21136   rtx rperm[MAX_VECT_LEN], x;
21137   rtx_insn *insn;
21138   unsigned i;
21139 
21140   for (i = 0; i < nelt; ++i)
21141     rperm[i] = GEN_INT (perm[i]);
21142 
21143   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
21144   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
21145   x = gen_rtx_SET (target, x);
21146 
21147   insn = emit_insn (x);
21148   if (recog_memoized (insn) < 0)
21149     {
21150       remove_insn (insn);
21151       return false;
21152     }
21153   return true;
21154 }
21155 
21156 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
21157 
21158 static bool
mips_expand_vselect_vconcat(rtx target,rtx op0,rtx op1,const unsigned char * perm,unsigned nelt)21159 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
21160 			     const unsigned char *perm, unsigned nelt)
21161 {
21162   machine_mode v2mode;
21163   rtx x;
21164 
21165   if (!GET_MODE_2XWIDER_MODE (GET_MODE (op0)).exists (&v2mode))
21166     return false;
21167   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
21168   return mips_expand_vselect (target, x, perm, nelt);
21169 }
21170 
21171 /* Recognize patterns for even-odd extraction.  */
21172 
21173 static bool
mips_expand_vpc_loongson_even_odd(struct expand_vec_perm_d * d)21174 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
21175 {
21176   unsigned i, odd, nelt = d->nelt;
21177   rtx t0, t1, t2, t3;
21178 
21179   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
21180     return false;
21181   /* Even-odd for V2SI/V2SFmode is matched by interleave directly.  */
21182   if (nelt < 4)
21183     return false;
21184 
21185   odd = d->perm[0];
21186   if (odd > 1)
21187     return false;
21188   for (i = 1; i < nelt; ++i)
21189     if (d->perm[i] != i * 2 + odd)
21190       return false;
21191 
21192   if (d->testing_p)
21193     return true;
21194 
21195   /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
21196   t0 = gen_reg_rtx (d->vmode);
21197   t1 = gen_reg_rtx (d->vmode);
21198   switch (d->vmode)
21199     {
21200     case E_V4HImode:
21201       emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
21202       emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
21203       if (odd)
21204 	emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
21205       else
21206 	emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
21207       break;
21208 
21209     case E_V8QImode:
21210       t2 = gen_reg_rtx (d->vmode);
21211       t3 = gen_reg_rtx (d->vmode);
21212       emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
21213       emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
21214       emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
21215       emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
21216       if (odd)
21217 	emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
21218       else
21219 	emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
21220       break;
21221 
21222     default:
21223       gcc_unreachable ();
21224     }
21225   return true;
21226 }
21227 
21228 /* Recognize patterns for the Loongson PSHUFH instruction.  */
21229 
21230 static bool
mips_expand_vpc_loongson_pshufh(struct expand_vec_perm_d * d)21231 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
21232 {
21233   unsigned i, mask;
21234   rtx rmask;
21235 
21236   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
21237     return false;
21238   if (d->vmode != V4HImode)
21239     return false;
21240   if (d->testing_p)
21241     return true;
21242 
21243   /* Convert the selector into the packed 8-bit form for pshufh.  */
21244   /* Recall that loongson is little-endian only.  No big-endian
21245      adjustment required.  */
21246   for (i = mask = 0; i < 4; i++)
21247     mask |= (d->perm[i] & 3) << (i * 2);
21248   rmask = force_reg (SImode, GEN_INT (mask));
21249 
21250   if (d->one_vector_p)
21251     emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
21252   else
21253     {
21254       rtx t0, t1, x, merge, rmerge[4];
21255 
21256       t0 = gen_reg_rtx (V4HImode);
21257       t1 = gen_reg_rtx (V4HImode);
21258       emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
21259       emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
21260 
21261       for (i = 0; i < 4; ++i)
21262 	rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
21263       merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
21264       merge = force_reg (V4HImode, merge);
21265 
21266       x = gen_rtx_AND (V4HImode, merge, t1);
21267       emit_insn (gen_rtx_SET (t1, x));
21268 
21269       x = gen_rtx_NOT (V4HImode, merge);
21270       x = gen_rtx_AND (V4HImode, x, t0);
21271       emit_insn (gen_rtx_SET (t0, x));
21272 
21273       x = gen_rtx_IOR (V4HImode, t0, t1);
21274       emit_insn (gen_rtx_SET (d->target, x));
21275     }
21276 
21277   return true;
21278 }
21279 
21280 /* Recognize broadcast patterns for the Loongson.  */
21281 
21282 static bool
mips_expand_vpc_loongson_bcast(struct expand_vec_perm_d * d)21283 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
21284 {
21285   unsigned i, elt;
21286   rtx t0, t1;
21287 
21288   if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
21289     return false;
21290   /* Note that we've already matched V2SI via punpck and V4HI via pshufh.  */
21291   if (d->vmode != V8QImode)
21292     return false;
21293   if (!d->one_vector_p)
21294     return false;
21295 
21296   elt = d->perm[0];
21297   for (i = 1; i < 8; ++i)
21298     if (d->perm[i] != elt)
21299       return false;
21300 
21301   if (d->testing_p)
21302     return true;
21303 
21304   /* With one interleave we put two of the desired element adjacent.  */
21305   t0 = gen_reg_rtx (V8QImode);
21306   if (elt < 4)
21307     emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
21308   else
21309     emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
21310 
21311   /* Shuffle that one HImode element into all locations.  */
21312   elt &= 3;
21313   elt *= 0x55;
21314   t1 = gen_reg_rtx (V4HImode);
21315   emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
21316 				  force_reg (SImode, GEN_INT (elt))));
21317 
21318   emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
21319   return true;
21320 }
21321 
21322 /* Construct (set target (vec_select op0 (parallel selector))) and
21323    return true if that's a valid instruction in the active ISA.  */
21324 
21325 static bool
mips_expand_msa_shuffle(struct expand_vec_perm_d * d)21326 mips_expand_msa_shuffle (struct expand_vec_perm_d *d)
21327 {
21328   rtx x, elts[MAX_VECT_LEN];
21329   rtvec v;
21330   rtx_insn *insn;
21331   unsigned i;
21332 
21333   if (!ISA_HAS_MSA)
21334     return false;
21335 
21336   for (i = 0; i < d->nelt; i++)
21337     elts[i] = GEN_INT (d->perm[i]);
21338 
21339   v = gen_rtvec_v (d->nelt, elts);
21340   x = gen_rtx_PARALLEL (VOIDmode, v);
21341 
21342   if (!mips_const_vector_shuffle_set_p (x, d->vmode))
21343     return false;
21344 
21345   x = gen_rtx_VEC_SELECT (d->vmode, d->op0, x);
21346   x = gen_rtx_SET (d->target, x);
21347 
21348   insn = emit_insn (x);
21349   if (recog_memoized (insn) < 0)
21350     {
21351       remove_insn (insn);
21352       return false;
21353     }
21354   return true;
21355 }
21356 
21357 static bool
mips_expand_vec_perm_const_1(struct expand_vec_perm_d * d)21358 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
21359 {
21360   unsigned int i, nelt = d->nelt;
21361   unsigned char perm2[MAX_VECT_LEN];
21362 
21363   if (d->one_vector_p)
21364     {
21365       /* Try interleave with alternating operands.  */
21366       memcpy (perm2, d->perm, sizeof(perm2));
21367       for (i = 1; i < nelt; i += 2)
21368 	perm2[i] += nelt;
21369       if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
21370 	return true;
21371     }
21372   else
21373     {
21374       if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
21375 				       d->perm, nelt))
21376 	return true;
21377 
21378       /* Try again with swapped operands.  */
21379       for (i = 0; i < nelt; ++i)
21380 	perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
21381       if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
21382 	return true;
21383     }
21384 
21385   if (mips_expand_vpc_loongson_even_odd (d))
21386     return true;
21387   if (mips_expand_vpc_loongson_pshufh (d))
21388     return true;
21389   if (mips_expand_vpc_loongson_bcast (d))
21390     return true;
21391   if (mips_expand_msa_shuffle (d))
21392     return true;
21393   return false;
21394 }
21395 
21396 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST.  */
21397 
21398 static bool
mips_vectorize_vec_perm_const(machine_mode vmode,rtx target,rtx op0,rtx op1,const vec_perm_indices & sel)21399 mips_vectorize_vec_perm_const (machine_mode vmode, rtx target, rtx op0,
21400 			       rtx op1, const vec_perm_indices &sel)
21401 {
21402   struct expand_vec_perm_d d;
21403   int i, nelt, which;
21404   unsigned char orig_perm[MAX_VECT_LEN];
21405   bool ok;
21406 
21407   d.target = target;
21408   d.op0 = op0;
21409   d.op1 = op1;
21410 
21411   d.vmode = vmode;
21412   gcc_assert (VECTOR_MODE_P (vmode));
21413   d.nelt = nelt = GET_MODE_NUNITS (vmode);
21414   d.testing_p = !target;
21415 
21416   /* This is overly conservative, but ensures we don't get an
21417      uninitialized warning on ORIG_PERM.  */
21418   memset (orig_perm, 0, MAX_VECT_LEN);
21419   for (i = which = 0; i < nelt; ++i)
21420     {
21421       int ei = sel[i] & (2 * nelt - 1);
21422       which |= (ei < nelt ? 1 : 2);
21423       orig_perm[i] = ei;
21424     }
21425   memcpy (d.perm, orig_perm, MAX_VECT_LEN);
21426 
21427   switch (which)
21428     {
21429     default:
21430       gcc_unreachable();
21431 
21432     case 3:
21433       d.one_vector_p = false;
21434       if (d.testing_p || !rtx_equal_p (d.op0, d.op1))
21435 	break;
21436       /* FALLTHRU */
21437 
21438     case 2:
21439       for (i = 0; i < nelt; ++i)
21440         d.perm[i] &= nelt - 1;
21441       d.op0 = d.op1;
21442       d.one_vector_p = true;
21443       break;
21444 
21445     case 1:
21446       d.op1 = d.op0;
21447       d.one_vector_p = true;
21448       break;
21449     }
21450 
21451   if (d.testing_p)
21452     {
21453       d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
21454       d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
21455       if (!d.one_vector_p)
21456 	d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
21457 
21458       start_sequence ();
21459       ok = mips_expand_vec_perm_const_1 (&d);
21460       end_sequence ();
21461       return ok;
21462     }
21463 
21464   ok = mips_expand_vec_perm_const_1 (&d);
21465 
21466   /* If we were given a two-vector permutation which just happened to
21467      have both input vectors equal, we folded this into a one-vector
21468      permutation.  There are several loongson patterns that are matched
21469      via direct vec_select+vec_concat expansion, but we do not have
21470      support in mips_expand_vec_perm_const_1 to guess the adjustment
21471      that should be made for a single operand.  Just try again with
21472      the original permutation.  */
21473   if (!ok && which == 3)
21474     {
21475       d.op0 = op0;
21476       d.op1 = op1;
21477       d.one_vector_p = false;
21478       memcpy (d.perm, orig_perm, MAX_VECT_LEN);
21479       ok = mips_expand_vec_perm_const_1 (&d);
21480     }
21481 
21482   return ok;
21483 }
21484 
21485 /* Implement TARGET_SCHED_REASSOCIATION_WIDTH.  */
21486 
21487 static int
mips_sched_reassociation_width(unsigned int opc ATTRIBUTE_UNUSED,machine_mode mode)21488 mips_sched_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
21489 				machine_mode mode)
21490 {
21491   if (MSA_SUPPORTED_MODE_P (mode))
21492     return 2;
21493   return 1;
21494 }
21495 
21496 /* Expand an integral vector unpack operation.  */
21497 
21498 void
mips_expand_vec_unpack(rtx operands[2],bool unsigned_p,bool high_p)21499 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
21500 {
21501   machine_mode imode = GET_MODE (operands[1]);
21502   rtx (*unpack) (rtx, rtx, rtx);
21503   rtx (*cmpFunc) (rtx, rtx, rtx);
21504   rtx tmp, dest, zero;
21505 
21506   if (ISA_HAS_MSA)
21507     {
21508       switch (imode)
21509 	{
21510 	case E_V4SImode:
21511 	  if (BYTES_BIG_ENDIAN != high_p)
21512 	    unpack = gen_msa_ilvl_w;
21513 	  else
21514 	    unpack = gen_msa_ilvr_w;
21515 
21516 	  cmpFunc = gen_msa_clt_s_w;
21517 	  break;
21518 
21519 	case E_V8HImode:
21520 	  if (BYTES_BIG_ENDIAN != high_p)
21521 	    unpack = gen_msa_ilvl_h;
21522 	  else
21523 	    unpack = gen_msa_ilvr_h;
21524 
21525 	  cmpFunc = gen_msa_clt_s_h;
21526 	  break;
21527 
21528 	case E_V16QImode:
21529 	  if (BYTES_BIG_ENDIAN != high_p)
21530 	    unpack = gen_msa_ilvl_b;
21531 	  else
21532 	    unpack = gen_msa_ilvr_b;
21533 
21534 	  cmpFunc = gen_msa_clt_s_b;
21535 	  break;
21536 
21537 	default:
21538 	  gcc_unreachable ();
21539 	  break;
21540 	}
21541 
21542       if (!unsigned_p)
21543 	{
21544 	  /* Extract sign extention for each element comparing each element
21545 	     with immediate zero.  */
21546 	  tmp = gen_reg_rtx (imode);
21547 	  emit_insn (cmpFunc (tmp, operands[1], CONST0_RTX (imode)));
21548 	}
21549       else
21550 	tmp = force_reg (imode, CONST0_RTX (imode));
21551 
21552       dest = gen_reg_rtx (imode);
21553 
21554       emit_insn (unpack (dest, operands[1], tmp));
21555       emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
21556       return;
21557     }
21558 
21559   switch (imode)
21560     {
21561     case E_V8QImode:
21562       if (high_p)
21563 	unpack = gen_loongson_punpckhbh;
21564       else
21565 	unpack = gen_loongson_punpcklbh;
21566       cmpFunc = gen_loongson_pcmpgtb;
21567       break;
21568     case E_V4HImode:
21569       if (high_p)
21570 	unpack = gen_loongson_punpckhhw;
21571       else
21572 	unpack = gen_loongson_punpcklhw;
21573       cmpFunc = gen_loongson_pcmpgth;
21574       break;
21575     default:
21576       gcc_unreachable ();
21577     }
21578 
21579   zero = force_reg (imode, CONST0_RTX (imode));
21580   if (unsigned_p)
21581     tmp = zero;
21582   else
21583     {
21584       tmp = gen_reg_rtx (imode);
21585       emit_insn (cmpFunc (tmp, zero, operands[1]));
21586     }
21587 
21588   dest = gen_reg_rtx (imode);
21589   emit_insn (unpack (dest, operands[1], tmp));
21590 
21591   emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
21592 }
21593 
21594 /* Construct and return PARALLEL RTX with CONST_INTs for HIGH (high_p == TRUE)
21595    or LOW (high_p == FALSE) half of a vector for mode MODE.  */
21596 
21597 rtx
mips_msa_vec_parallel_const_half(machine_mode mode,bool high_p)21598 mips_msa_vec_parallel_const_half (machine_mode mode, bool high_p)
21599 {
21600   int nunits = GET_MODE_NUNITS (mode);
21601   rtvec v = rtvec_alloc (nunits / 2);
21602   int base;
21603   int i;
21604 
21605   if (BYTES_BIG_ENDIAN)
21606     base = high_p ? 0 : nunits / 2;
21607   else
21608     base = high_p ? nunits / 2 : 0;
21609 
21610   for (i = 0; i < nunits / 2; i++)
21611     RTVEC_ELT (v, i) = GEN_INT (base + i);
21612 
21613   return gen_rtx_PARALLEL (VOIDmode, v);
21614 }
21615 
21616 /* A subroutine of mips_expand_vec_init, match constant vector elements.  */
21617 
21618 static inline bool
mips_constant_elt_p(rtx x)21619 mips_constant_elt_p (rtx x)
21620 {
21621   return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
21622 }
21623 
21624 /* A subroutine of mips_expand_vec_init, expand via broadcast.  */
21625 
21626 static void
mips_expand_vi_broadcast(machine_mode vmode,rtx target,rtx elt)21627 mips_expand_vi_broadcast (machine_mode vmode, rtx target, rtx elt)
21628 {
21629   struct expand_vec_perm_d d;
21630   rtx t1;
21631   bool ok;
21632 
21633   if (elt != const0_rtx)
21634     elt = force_reg (GET_MODE_INNER (vmode), elt);
21635   if (REG_P (elt))
21636     elt = gen_lowpart (DImode, elt);
21637 
21638   t1 = gen_reg_rtx (vmode);
21639   switch (vmode)
21640     {
21641     case E_V8QImode:
21642       emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
21643       break;
21644     case E_V4HImode:
21645       emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
21646       break;
21647     default:
21648       gcc_unreachable ();
21649     }
21650 
21651   memset (&d, 0, sizeof (d));
21652   d.target = target;
21653   d.op0 = t1;
21654   d.op1 = t1;
21655   d.vmode = vmode;
21656   d.nelt = GET_MODE_NUNITS (vmode);
21657   d.one_vector_p = true;
21658 
21659   ok = mips_expand_vec_perm_const_1 (&d);
21660   gcc_assert (ok);
21661 }
21662 
21663 /* Return a const_int vector of VAL with mode MODE.  */
21664 
21665 rtx
mips_gen_const_int_vector(machine_mode mode,HOST_WIDE_INT val)21666 mips_gen_const_int_vector (machine_mode mode, HOST_WIDE_INT val)
21667 {
21668   rtx c = gen_int_mode (val, GET_MODE_INNER (mode));
21669   return gen_const_vec_duplicate (mode, c);
21670 }
21671 
21672 /* Return a vector of repeated 4-element sets generated from
21673    immediate VAL in mode MODE.  */
21674 
21675 static rtx
mips_gen_const_int_vector_shuffle(machine_mode mode,int val)21676 mips_gen_const_int_vector_shuffle (machine_mode mode, int val)
21677 {
21678   int nunits = GET_MODE_NUNITS (mode);
21679   int nsets = nunits / 4;
21680   rtx elts[MAX_VECT_LEN];
21681   int set = 0;
21682   int i, j;
21683 
21684   /* Generate a const_int vector replicating the same 4-element set
21685      from an immediate.  */
21686   for (j = 0; j < nsets; j++, set = 4 * j)
21687     for (i = 0; i < 4; i++)
21688       elts[set + i] = GEN_INT (set + ((val >> (2 * i)) & 0x3));
21689 
21690   return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nunits, elts));
21691 }
21692 
21693 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
21694    elements of VALS with zeros, copy the constant vector to TARGET.  */
21695 
21696 static void
mips_expand_vi_constant(machine_mode vmode,unsigned nelt,rtx target,rtx vals)21697 mips_expand_vi_constant (machine_mode vmode, unsigned nelt,
21698 			 rtx target, rtx vals)
21699 {
21700   rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
21701   unsigned i;
21702 
21703   for (i = 0; i < nelt; ++i)
21704     {
21705       rtx elem = RTVEC_ELT (vec, i);
21706       if (!mips_constant_elt_p (elem))
21707 	RTVEC_ELT (vec, i) = CONST0_RTX (GET_MODE (elem));
21708     }
21709 
21710   emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
21711 }
21712 
21713 
21714 /* A subroutine of mips_expand_vec_init, expand via pinsrh.  */
21715 
21716 static void
mips_expand_vi_loongson_one_pinsrh(rtx target,rtx vals,unsigned one_var)21717 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
21718 {
21719   mips_expand_vi_constant (V4HImode, 4, target, vals);
21720 
21721   emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
21722 			      GEN_INT (one_var)));
21723 }
21724 
21725 /* A subroutine of mips_expand_vec_init, expand anything via memory.  */
21726 
21727 static void
mips_expand_vi_general(machine_mode vmode,machine_mode imode,unsigned nelt,unsigned nvar,rtx target,rtx vals)21728 mips_expand_vi_general (machine_mode vmode, machine_mode imode,
21729 			unsigned nelt, unsigned nvar, rtx target, rtx vals)
21730 {
21731   rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
21732   unsigned int i, isize = GET_MODE_SIZE (imode);
21733 
21734   if (nvar < nelt)
21735     mips_expand_vi_constant (vmode, nelt, mem, vals);
21736 
21737   for (i = 0; i < nelt; ++i)
21738     {
21739       rtx x = XVECEXP (vals, 0, i);
21740       if (!mips_constant_elt_p (x))
21741 	emit_move_insn (adjust_address (mem, imode, i * isize), x);
21742     }
21743 
21744   emit_move_insn (target, mem);
21745 }
21746 
21747 /* Expand a vector initialization.  */
21748 
21749 void
mips_expand_vector_init(rtx target,rtx vals)21750 mips_expand_vector_init (rtx target, rtx vals)
21751 {
21752   machine_mode vmode = GET_MODE (target);
21753   machine_mode imode = GET_MODE_INNER (vmode);
21754   unsigned i, nelt = GET_MODE_NUNITS (vmode);
21755   unsigned nvar = 0, one_var = -1u;
21756   bool all_same = true;
21757   rtx x;
21758 
21759   for (i = 0; i < nelt; ++i)
21760     {
21761       x = XVECEXP (vals, 0, i);
21762       if (!mips_constant_elt_p (x))
21763 	nvar++, one_var = i;
21764       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
21765 	all_same = false;
21766     }
21767 
21768   if (ISA_HAS_MSA)
21769     {
21770       if (all_same)
21771 	{
21772 	  rtx same = XVECEXP (vals, 0, 0);
21773 	  rtx temp, temp2;
21774 
21775 	  if (CONST_INT_P (same) && nvar == 0
21776 	      && mips_signed_immediate_p (INTVAL (same), 10, 0))
21777 	    {
21778 	      switch (vmode)
21779 		{
21780 		case E_V16QImode:
21781 		case E_V8HImode:
21782 		case E_V4SImode:
21783 		case E_V2DImode:
21784 		  temp = gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0));
21785 		  emit_move_insn (target, temp);
21786 		  return;
21787 
21788 		default:
21789 		  gcc_unreachable ();
21790 		}
21791 	    }
21792 	  temp = gen_reg_rtx (imode);
21793 	  if (imode == GET_MODE (same))
21794 	    temp2 = same;
21795 	  else if (GET_MODE_SIZE (imode) >= UNITS_PER_WORD)
21796 	    temp2 = simplify_gen_subreg (imode, same, GET_MODE (same), 0);
21797 	  else
21798 	    temp2 = lowpart_subreg (imode, same, GET_MODE (same));
21799 	  emit_move_insn (temp, temp2);
21800 
21801 	  switch (vmode)
21802 	    {
21803 	    case E_V16QImode:
21804 	    case E_V8HImode:
21805 	    case E_V4SImode:
21806 	    case E_V2DImode:
21807 	      mips_emit_move (target, gen_rtx_VEC_DUPLICATE (vmode, temp));
21808 	      break;
21809 
21810 	    case E_V4SFmode:
21811 	      emit_insn (gen_msa_splati_w_f_scalar (target, temp));
21812 	      break;
21813 
21814 	    case E_V2DFmode:
21815 	      emit_insn (gen_msa_splati_d_f_scalar (target, temp));
21816 	      break;
21817 
21818 	    default:
21819 	      gcc_unreachable ();
21820 	    }
21821 	}
21822       else
21823 	{
21824 	  emit_move_insn (target, CONST0_RTX (vmode));
21825 
21826 	  for (i = 0; i < nelt; ++i)
21827 	    {
21828 	      rtx temp = gen_reg_rtx (imode);
21829 	      emit_move_insn (temp, XVECEXP (vals, 0, i));
21830 	      switch (vmode)
21831 		{
21832 		case E_V16QImode:
21833 		  emit_insn (gen_vec_setv16qi (target, temp, GEN_INT (i)));
21834 		  break;
21835 
21836 		case E_V8HImode:
21837 		  emit_insn (gen_vec_setv8hi (target, temp, GEN_INT (i)));
21838 		  break;
21839 
21840 		case E_V4SImode:
21841 		  emit_insn (gen_vec_setv4si (target, temp, GEN_INT (i)));
21842 		  break;
21843 
21844 		case E_V2DImode:
21845 		  emit_insn (gen_vec_setv2di (target, temp, GEN_INT (i)));
21846 		  break;
21847 
21848 		case E_V4SFmode:
21849 		  emit_insn (gen_vec_setv4sf (target, temp, GEN_INT (i)));
21850 		  break;
21851 
21852 		case E_V2DFmode:
21853 		  emit_insn (gen_vec_setv2df (target, temp, GEN_INT (i)));
21854 		  break;
21855 
21856 		default:
21857 		  gcc_unreachable ();
21858 		}
21859 	    }
21860 	}
21861       return;
21862     }
21863 
21864   /* Load constants from the pool, or whatever's handy.  */
21865   if (nvar == 0)
21866     {
21867       emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
21868       return;
21869     }
21870 
21871   /* For two-part initialization, always use CONCAT.  */
21872   if (nelt == 2)
21873     {
21874       rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
21875       rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
21876       x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
21877       emit_insn (gen_rtx_SET (target, x));
21878       return;
21879     }
21880 
21881   /* Loongson is the only cpu with vectors with more elements.  */
21882   gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
21883 
21884   /* If all values are identical, broadcast the value.  */
21885   if (all_same)
21886     {
21887       mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
21888       return;
21889     }
21890 
21891   /* If we've only got one non-variable V4HImode, use PINSRH.  */
21892   if (nvar == 1 && vmode == V4HImode)
21893     {
21894       mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
21895       return;
21896     }
21897 
21898   mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
21899 }
21900 
21901 /* Expand a vector reduction.  */
21902 
21903 void
mips_expand_vec_reduc(rtx target,rtx in,rtx (* gen)(rtx,rtx,rtx))21904 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
21905 {
21906   machine_mode vmode = GET_MODE (in);
21907   unsigned char perm2[2];
21908   rtx last, next, fold, x;
21909   bool ok;
21910 
21911   last = in;
21912   fold = gen_reg_rtx (vmode);
21913   switch (vmode)
21914     {
21915     case E_V2SFmode:
21916       /* Use PUL/PLU to produce { L, H } op { H, L }.
21917 	 By reversing the pair order, rather than a pure interleave high,
21918 	 we avoid erroneous exceptional conditions that we might otherwise
21919 	 produce from the computation of H op H.  */
21920       perm2[0] = 1;
21921       perm2[1] = 2;
21922       ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
21923       gcc_assert (ok);
21924       break;
21925 
21926     case E_V2SImode:
21927       /* Use interleave to produce { H, L } op { H, H }.  */
21928       emit_insn (gen_loongson_punpckhwd (fold, last, last));
21929       break;
21930 
21931     case E_V4HImode:
21932       /* Perform the first reduction with interleave,
21933 	 and subsequent reductions with shifts.  */
21934       emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
21935 
21936       next = gen_reg_rtx (vmode);
21937       emit_insn (gen (next, last, fold));
21938       last = next;
21939 
21940       fold = gen_reg_rtx (vmode);
21941       x = force_reg (SImode, GEN_INT (16));
21942       emit_insn (gen_vec_shr_v4hi (fold, last, x));
21943       break;
21944 
21945     case E_V8QImode:
21946       emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
21947 
21948       next = gen_reg_rtx (vmode);
21949       emit_insn (gen (next, last, fold));
21950       last = next;
21951 
21952       fold = gen_reg_rtx (vmode);
21953       x = force_reg (SImode, GEN_INT (16));
21954       emit_insn (gen_vec_shr_v8qi (fold, last, x));
21955 
21956       next = gen_reg_rtx (vmode);
21957       emit_insn (gen (next, last, fold));
21958       last = next;
21959 
21960       fold = gen_reg_rtx (vmode);
21961       x = force_reg (SImode, GEN_INT (8));
21962       emit_insn (gen_vec_shr_v8qi (fold, last, x));
21963       break;
21964 
21965     default:
21966       gcc_unreachable ();
21967     }
21968 
21969   emit_insn (gen (target, last, fold));
21970 }
21971 
21972 /* Expand a vector minimum/maximum.  */
21973 
21974 void
mips_expand_vec_minmax(rtx target,rtx op0,rtx op1,rtx (* cmp)(rtx,rtx,rtx),bool min_p)21975 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
21976 			rtx (*cmp) (rtx, rtx, rtx), bool min_p)
21977 {
21978   machine_mode vmode = GET_MODE (target);
21979   rtx tc, t0, t1, x;
21980 
21981   tc = gen_reg_rtx (vmode);
21982   t0 = gen_reg_rtx (vmode);
21983   t1 = gen_reg_rtx (vmode);
21984 
21985   /* op0 > op1 */
21986   emit_insn (cmp (tc, op0, op1));
21987 
21988   x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
21989   emit_insn (gen_rtx_SET (t0, x));
21990 
21991   x = gen_rtx_NOT (vmode, tc);
21992   x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
21993   emit_insn (gen_rtx_SET (t1, x));
21994 
21995   x = gen_rtx_IOR (vmode, t0, t1);
21996   emit_insn (gen_rtx_SET (target, x));
21997 }
21998 
21999 /* Implement HARD_REGNO_CALLER_SAVE_MODE.  */
22000 
22001 machine_mode
mips_hard_regno_caller_save_mode(unsigned int regno,unsigned int nregs,machine_mode mode)22002 mips_hard_regno_caller_save_mode (unsigned int regno,
22003 				  unsigned int nregs,
22004 				  machine_mode mode)
22005 {
22006   /* For performance, avoid saving/restoring upper parts of a register
22007      by returning MODE as save mode when the mode is known.  */
22008   if (mode == VOIDmode)
22009     return choose_hard_reg_mode (regno, nregs, false);
22010   else
22011     return mode;
22012 }
22013 
22014 /* Generate RTL for comparing CMP_OP0 and CMP_OP1 using condition COND and
22015    store the result -1 or 0 in DEST.  */
22016 
22017 static void
mips_expand_msa_cmp(rtx dest,enum rtx_code cond,rtx op0,rtx op1)22018 mips_expand_msa_cmp (rtx dest, enum rtx_code cond, rtx op0, rtx op1)
22019 {
22020   machine_mode cmp_mode = GET_MODE (op0);
22021   int unspec = -1;
22022   bool negate = false;
22023 
22024   switch (cmp_mode)
22025     {
22026     case E_V16QImode:
22027     case E_V8HImode:
22028     case E_V4SImode:
22029     case E_V2DImode:
22030       switch (cond)
22031 	{
22032 	case NE:
22033 	  cond = reverse_condition (cond);
22034 	  negate = true;
22035 	  break;
22036 	case EQ:
22037 	case LT:
22038 	case LE:
22039 	case LTU:
22040 	case LEU:
22041 	  break;
22042 	case GE:
22043 	case GT:
22044 	case GEU:
22045 	case GTU:
22046 	  std::swap (op0, op1);
22047 	  cond = swap_condition (cond);
22048 	  break;
22049 	default:
22050 	  gcc_unreachable ();
22051 	}
22052       mips_emit_binary (cond, dest, op0, op1);
22053       if (negate)
22054 	emit_move_insn (dest, gen_rtx_NOT (GET_MODE (dest), dest));
22055       break;
22056 
22057     case E_V4SFmode:
22058     case E_V2DFmode:
22059       switch (cond)
22060 	{
22061 	case UNORDERED:
22062 	case ORDERED:
22063 	case EQ:
22064 	case NE:
22065 	case UNEQ:
22066 	case UNLE:
22067 	case UNLT:
22068 	  break;
22069 	case LTGT: cond = NE; break;
22070 	case UNGE: cond = UNLE; std::swap (op0, op1); break;
22071 	case UNGT: cond = UNLT; std::swap (op0, op1); break;
22072 	case LE: unspec = UNSPEC_MSA_FSLE; break;
22073 	case LT: unspec = UNSPEC_MSA_FSLT; break;
22074 	case GE: unspec = UNSPEC_MSA_FSLE; std::swap (op0, op1); break;
22075 	case GT: unspec = UNSPEC_MSA_FSLT; std::swap (op0, op1); break;
22076 	default:
22077 	  gcc_unreachable ();
22078 	}
22079       if (unspec < 0)
22080 	mips_emit_binary (cond, dest, op0, op1);
22081       else
22082 	{
22083 	  rtx x = gen_rtx_UNSPEC (GET_MODE (dest),
22084 				  gen_rtvec (2, op0, op1), unspec);
22085 	  emit_insn (gen_rtx_SET (dest, x));
22086 	}
22087       break;
22088 
22089     default:
22090       gcc_unreachable ();
22091       break;
22092     }
22093 }
22094 
22095 /* Expand VEC_COND_EXPR, where:
22096    MODE is mode of the result
22097    VIMODE equivalent integer mode
22098    OPERANDS operands of VEC_COND_EXPR.  */
22099 
22100 void
mips_expand_vec_cond_expr(machine_mode mode,machine_mode vimode,rtx * operands)22101 mips_expand_vec_cond_expr (machine_mode mode, machine_mode vimode,
22102 			   rtx *operands)
22103 {
22104   rtx cond = operands[3];
22105   rtx cmp_op0 = operands[4];
22106   rtx cmp_op1 = operands[5];
22107   rtx cmp_res = gen_reg_rtx (vimode);
22108 
22109   mips_expand_msa_cmp (cmp_res, GET_CODE (cond), cmp_op0, cmp_op1);
22110 
22111   /* We handle the following cases:
22112      1) r = a CMP b ? -1 : 0
22113      2) r = a CMP b ? -1 : v
22114      3) r = a CMP b ?  v : 0
22115      4) r = a CMP b ? v1 : v2  */
22116 
22117   /* Case (1) above.  We only move the results.  */
22118   if (operands[1] == CONSTM1_RTX (vimode)
22119       && operands[2] == CONST0_RTX (vimode))
22120     emit_move_insn (operands[0], cmp_res);
22121   else
22122     {
22123       rtx src1 = gen_reg_rtx (vimode);
22124       rtx src2 = gen_reg_rtx (vimode);
22125       rtx mask = gen_reg_rtx (vimode);
22126       rtx bsel;
22127 
22128       /* Move the vector result to use it as a mask.  */
22129       emit_move_insn (mask, cmp_res);
22130 
22131       if (register_operand (operands[1], mode))
22132 	{
22133 	  rtx xop1 = operands[1];
22134 	  if (mode != vimode)
22135 	    {
22136 	      xop1 = gen_reg_rtx (vimode);
22137 	      emit_move_insn (xop1, gen_rtx_SUBREG (vimode, operands[1], 0));
22138 	    }
22139 	  emit_move_insn (src1, xop1);
22140 	}
22141       else
22142 	{
22143 	  gcc_assert (operands[1] == CONSTM1_RTX (vimode));
22144 	  /* Case (2) if the below doesn't move the mask to src2.  */
22145 	  emit_move_insn (src1, mask);
22146 	}
22147 
22148       if (register_operand (operands[2], mode))
22149 	{
22150 	  rtx xop2 = operands[2];
22151 	  if (mode != vimode)
22152 	    {
22153 	      xop2 = gen_reg_rtx (vimode);
22154 	      emit_move_insn (xop2, gen_rtx_SUBREG (vimode, operands[2], 0));
22155 	    }
22156 	  emit_move_insn (src2, xop2);
22157 	}
22158       else
22159 	{
22160 	  gcc_assert (operands[2] == CONST0_RTX (mode));
22161 	  /* Case (3) if the above didn't move the mask to src1.  */
22162 	  emit_move_insn (src2, mask);
22163 	}
22164 
22165       /* We deal with case (4) if the mask wasn't moved to either src1 or src2.
22166 	 In any case, we eventually do vector mask-based copy.  */
22167       bsel = gen_rtx_IOR (vimode,
22168 			  gen_rtx_AND (vimode,
22169 				       gen_rtx_NOT (vimode, mask), src2),
22170 			  gen_rtx_AND (vimode, mask, src1));
22171       /* The result is placed back to a register with the mask.  */
22172       emit_insn (gen_rtx_SET (mask, bsel));
22173       emit_move_insn (operands[0], gen_rtx_SUBREG (mode, mask, 0));
22174     }
22175 }
22176 
22177 /* Implement TARGET_CASE_VALUES_THRESHOLD.  */
22178 
22179 unsigned int
mips_case_values_threshold(void)22180 mips_case_values_threshold (void)
22181 {
22182   /* In MIPS16 mode using a larger case threshold generates smaller code.  */
22183   if (TARGET_MIPS16 && optimize_size)
22184     return 10;
22185   else
22186     return default_case_values_threshold ();
22187 }
22188 
22189 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV.  */
22190 
22191 static void
mips_atomic_assign_expand_fenv(tree * hold,tree * clear,tree * update)22192 mips_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
22193 {
22194   if (!TARGET_HARD_FLOAT_ABI)
22195     return;
22196   tree exceptions_var = create_tmp_var_raw (MIPS_ATYPE_USI);
22197   tree fcsr_orig_var = create_tmp_var_raw (MIPS_ATYPE_USI);
22198   tree fcsr_mod_var = create_tmp_var_raw (MIPS_ATYPE_USI);
22199   tree get_fcsr = mips_builtin_decls[MIPS_GET_FCSR];
22200   tree set_fcsr = mips_builtin_decls[MIPS_SET_FCSR];
22201   tree get_fcsr_hold_call = build_call_expr (get_fcsr, 0);
22202   tree hold_assign_orig = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
22203 				  fcsr_orig_var, get_fcsr_hold_call);
22204   tree hold_mod_val = build2 (BIT_AND_EXPR, MIPS_ATYPE_USI, fcsr_orig_var,
22205 			      build_int_cst (MIPS_ATYPE_USI, 0xfffff003));
22206   tree hold_assign_mod = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
22207 				 fcsr_mod_var, hold_mod_val);
22208   tree set_fcsr_hold_call = build_call_expr (set_fcsr, 1, fcsr_mod_var);
22209   tree hold_all = build2 (COMPOUND_EXPR, MIPS_ATYPE_USI,
22210 			  hold_assign_orig, hold_assign_mod);
22211   *hold = build2 (COMPOUND_EXPR, void_type_node, hold_all,
22212 		  set_fcsr_hold_call);
22213 
22214   *clear = build_call_expr (set_fcsr, 1, fcsr_mod_var);
22215 
22216   tree get_fcsr_update_call = build_call_expr (get_fcsr, 0);
22217   *update = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
22218 		    exceptions_var, get_fcsr_update_call);
22219   tree set_fcsr_update_call = build_call_expr (set_fcsr, 1, fcsr_orig_var);
22220   *update = build2 (COMPOUND_EXPR, void_type_node, *update,
22221 		    set_fcsr_update_call);
22222   tree atomic_feraiseexcept
22223     = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
22224   tree int_exceptions_var = fold_convert (integer_type_node,
22225 					  exceptions_var);
22226   tree atomic_feraiseexcept_call = build_call_expr (atomic_feraiseexcept,
22227 						    1, int_exceptions_var);
22228   *update = build2 (COMPOUND_EXPR, void_type_node, *update,
22229 		    atomic_feraiseexcept_call);
22230 }
22231 
22232 /* Implement TARGET_SPILL_CLASS.  */
22233 
22234 static reg_class_t
mips_spill_class(reg_class_t rclass ATTRIBUTE_UNUSED,machine_mode mode ATTRIBUTE_UNUSED)22235 mips_spill_class (reg_class_t rclass ATTRIBUTE_UNUSED,
22236 		  machine_mode mode ATTRIBUTE_UNUSED)
22237 {
22238   if (TARGET_MIPS16)
22239     return SPILL_REGS;
22240   return NO_REGS;
22241 }
22242 
22243 /* Implement TARGET_LRA_P.  */
22244 
22245 static bool
mips_lra_p(void)22246 mips_lra_p (void)
22247 {
22248   return mips_lra_flag;
22249 }
22250 
22251 /* Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS.  */
22252 
22253 static reg_class_t
mips_ira_change_pseudo_allocno_class(int regno,reg_class_t allocno_class,reg_class_t best_class ATTRIBUTE_UNUSED)22254 mips_ira_change_pseudo_allocno_class (int regno, reg_class_t allocno_class,
22255 				      reg_class_t best_class ATTRIBUTE_UNUSED)
22256 {
22257   /* LRA will allocate an FPR for an integer mode pseudo instead of spilling
22258      to memory if an FPR is present in the allocno class.  It is rare that
22259      we actually need to place an integer mode value in an FPR so where
22260      possible limit the allocation to GR_REGS.  This will slightly pessimize
22261      code that involves integer to/from float conversions as these will have
22262      to reload into FPRs in LRA.  Such reloads are sometimes eliminated and
22263      sometimes only partially eliminated.  We choose to take this penalty
22264      in order to eliminate usage of FPRs in code that does not use floating
22265      point data.
22266 
22267      This change has a similar effect to increasing the cost of FPR->GPR
22268      register moves for integer modes so that they are higher than the cost
22269      of memory but changing the allocno class is more reliable.
22270 
22271      This is also similar to forbidding integer mode values in FPRs entirely
22272      but this would lead to an inconsistency in the integer to/from float
22273      instructions that say integer mode values must be placed in FPRs.  */
22274   if (INTEGRAL_MODE_P (PSEUDO_REGNO_MODE (regno)) && allocno_class == ALL_REGS)
22275     return GR_REGS;
22276   return allocno_class;
22277 }
22278 
22279 /* Implement TARGET_PROMOTE_FUNCTION_MODE */
22280 
22281 /* This function is equivalent to default_promote_function_mode_always_promote
22282    except that it returns a promoted mode even if type is NULL_TREE.  This is
22283    needed by libcalls which have no type (only a mode) such as fixed conversion
22284    routines that take a signed or unsigned char/short argument and convert it
22285    to a fixed type.  */
22286 
22287 static machine_mode
mips_promote_function_mode(const_tree type ATTRIBUTE_UNUSED,machine_mode mode,int * punsignedp ATTRIBUTE_UNUSED,const_tree fntype ATTRIBUTE_UNUSED,int for_return ATTRIBUTE_UNUSED)22288 mips_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
22289                             machine_mode mode,
22290                             int *punsignedp ATTRIBUTE_UNUSED,
22291                             const_tree fntype ATTRIBUTE_UNUSED,
22292                             int for_return ATTRIBUTE_UNUSED)
22293 {
22294   int unsignedp;
22295 
22296   if (type != NULL_TREE)
22297     return promote_mode (type, mode, punsignedp);
22298 
22299   unsignedp = *punsignedp;
22300   PROMOTE_MODE (mode, unsignedp, type);
22301   *punsignedp = unsignedp;
22302   return mode;
22303 }
22304 
22305 /* Implement TARGET_TRULY_NOOP_TRUNCATION.  */
22306 
22307 static bool
mips_truly_noop_truncation(poly_uint64 outprec,poly_uint64 inprec)22308 mips_truly_noop_truncation (poly_uint64 outprec, poly_uint64 inprec)
22309 {
22310   return !TARGET_64BIT || inprec <= 32 || outprec > 32;
22311 }
22312 
22313 /* Implement TARGET_CONSTANT_ALIGNMENT.  */
22314 
22315 static HOST_WIDE_INT
mips_constant_alignment(const_tree exp,HOST_WIDE_INT align)22316 mips_constant_alignment (const_tree exp, HOST_WIDE_INT align)
22317 {
22318   if (TREE_CODE (exp) == STRING_CST || TREE_CODE (exp) == CONSTRUCTOR)
22319     return MAX (align, BITS_PER_WORD);
22320   return align;
22321 }
22322 
22323 /* Implement TARGET_STARTING_FRAME_OFFSET.  See mips_compute_frame_info
22324    for details about the frame layout.  */
22325 
22326 static HOST_WIDE_INT
mips_starting_frame_offset(void)22327 mips_starting_frame_offset (void)
22328 {
22329   if (FRAME_GROWS_DOWNWARD)
22330     return 0;
22331   return crtl->outgoing_args_size + MIPS_GP_SAVE_AREA_SIZE;
22332 }
22333 
22334 /* Initialize the GCC target structure.  */
22335 #undef TARGET_ASM_ALIGNED_HI_OP
22336 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
22337 #undef TARGET_ASM_ALIGNED_SI_OP
22338 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
22339 #undef TARGET_ASM_ALIGNED_DI_OP
22340 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
22341 
22342 #undef TARGET_OPTION_OVERRIDE
22343 #define TARGET_OPTION_OVERRIDE mips_option_override
22344 
22345 #undef TARGET_LEGITIMIZE_ADDRESS
22346 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
22347 
22348 #undef TARGET_ASM_FUNCTION_PROLOGUE
22349 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
22350 #undef TARGET_ASM_FUNCTION_EPILOGUE
22351 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
22352 #undef TARGET_ASM_SELECT_RTX_SECTION
22353 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
22354 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
22355 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
22356 
22357 #undef TARGET_SCHED_INIT
22358 #define TARGET_SCHED_INIT mips_sched_init
22359 #undef TARGET_SCHED_REORDER
22360 #define TARGET_SCHED_REORDER mips_sched_reorder
22361 #undef TARGET_SCHED_REORDER2
22362 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
22363 #undef TARGET_SCHED_VARIABLE_ISSUE
22364 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
22365 #undef TARGET_SCHED_ADJUST_COST
22366 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
22367 #undef TARGET_SCHED_ISSUE_RATE
22368 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
22369 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
22370 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
22371 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
22372 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
22373 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
22374 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
22375   mips_multipass_dfa_lookahead
22376 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
22377 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
22378   mips_small_register_classes_for_mode_p
22379 
22380 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
22381 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
22382 
22383 #undef TARGET_INSERT_ATTRIBUTES
22384 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
22385 #undef TARGET_MERGE_DECL_ATTRIBUTES
22386 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
22387 #undef TARGET_CAN_INLINE_P
22388 #define TARGET_CAN_INLINE_P mips_can_inline_p
22389 #undef TARGET_SET_CURRENT_FUNCTION
22390 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
22391 
22392 #undef TARGET_VALID_POINTER_MODE
22393 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
22394 #undef TARGET_REGISTER_MOVE_COST
22395 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
22396 #undef TARGET_REGISTER_PRIORITY
22397 #define TARGET_REGISTER_PRIORITY mips_register_priority
22398 #undef TARGET_MEMORY_MOVE_COST
22399 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
22400 #undef TARGET_RTX_COSTS
22401 #define TARGET_RTX_COSTS mips_rtx_costs
22402 #undef TARGET_ADDRESS_COST
22403 #define TARGET_ADDRESS_COST mips_address_cost
22404 
22405 #undef TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P
22406 #define TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P mips_no_speculation_in_delay_slots_p
22407 
22408 #undef TARGET_IN_SMALL_DATA_P
22409 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
22410 
22411 #undef TARGET_MACHINE_DEPENDENT_REORG
22412 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
22413 
22414 #undef  TARGET_PREFERRED_RELOAD_CLASS
22415 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
22416 
22417 #undef TARGET_EXPAND_TO_RTL_HOOK
22418 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
22419 #undef TARGET_ASM_FILE_START
22420 #define TARGET_ASM_FILE_START mips_file_start
22421 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
22422 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
22423 #undef TARGET_ASM_CODE_END
22424 #define TARGET_ASM_CODE_END mips_code_end
22425 
22426 #undef TARGET_INIT_LIBFUNCS
22427 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
22428 
22429 #undef TARGET_BUILD_BUILTIN_VA_LIST
22430 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
22431 #undef TARGET_EXPAND_BUILTIN_VA_START
22432 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
22433 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
22434 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
22435 
22436 #undef  TARGET_PROMOTE_FUNCTION_MODE
22437 #define TARGET_PROMOTE_FUNCTION_MODE mips_promote_function_mode
22438 #undef TARGET_FUNCTION_VALUE
22439 #define TARGET_FUNCTION_VALUE mips_function_value
22440 #undef TARGET_LIBCALL_VALUE
22441 #define TARGET_LIBCALL_VALUE mips_libcall_value
22442 #undef TARGET_FUNCTION_VALUE_REGNO_P
22443 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
22444 #undef TARGET_RETURN_IN_MEMORY
22445 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
22446 #undef TARGET_RETURN_IN_MSB
22447 #define TARGET_RETURN_IN_MSB mips_return_in_msb
22448 
22449 #undef TARGET_ASM_OUTPUT_MI_THUNK
22450 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
22451 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
22452 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
22453 
22454 #undef TARGET_PRINT_OPERAND
22455 #define TARGET_PRINT_OPERAND mips_print_operand
22456 #undef TARGET_PRINT_OPERAND_ADDRESS
22457 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
22458 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
22459 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
22460 
22461 #undef TARGET_SETUP_INCOMING_VARARGS
22462 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
22463 #undef TARGET_STRICT_ARGUMENT_NAMING
22464 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
22465 #undef TARGET_MUST_PASS_IN_STACK
22466 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
22467 #undef TARGET_PASS_BY_REFERENCE
22468 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
22469 #undef TARGET_CALLEE_COPIES
22470 #define TARGET_CALLEE_COPIES mips_callee_copies
22471 #undef TARGET_ARG_PARTIAL_BYTES
22472 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
22473 #undef TARGET_FUNCTION_ARG
22474 #define TARGET_FUNCTION_ARG mips_function_arg
22475 #undef TARGET_FUNCTION_ARG_ADVANCE
22476 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
22477 #undef TARGET_FUNCTION_ARG_PADDING
22478 #define TARGET_FUNCTION_ARG_PADDING mips_function_arg_padding
22479 #undef TARGET_FUNCTION_ARG_BOUNDARY
22480 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
22481 #undef TARGET_GET_RAW_RESULT_MODE
22482 #define TARGET_GET_RAW_RESULT_MODE mips_get_reg_raw_mode
22483 #undef TARGET_GET_RAW_ARG_MODE
22484 #define TARGET_GET_RAW_ARG_MODE mips_get_reg_raw_mode
22485 
22486 #undef TARGET_MODE_REP_EXTENDED
22487 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
22488 
22489 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
22490 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
22491   mips_builtin_vectorized_function
22492 #undef TARGET_VECTOR_MODE_SUPPORTED_P
22493 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
22494 
22495 #undef TARGET_SCALAR_MODE_SUPPORTED_P
22496 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
22497 
22498 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
22499 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
22500 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
22501 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
22502   mips_autovectorize_vector_sizes
22503 
22504 #undef TARGET_INIT_BUILTINS
22505 #define TARGET_INIT_BUILTINS mips_init_builtins
22506 #undef TARGET_BUILTIN_DECL
22507 #define TARGET_BUILTIN_DECL mips_builtin_decl
22508 #undef TARGET_EXPAND_BUILTIN
22509 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
22510 
22511 #undef TARGET_HAVE_TLS
22512 #define TARGET_HAVE_TLS HAVE_AS_TLS
22513 
22514 #undef TARGET_CANNOT_FORCE_CONST_MEM
22515 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
22516 
22517 #undef TARGET_LEGITIMATE_CONSTANT_P
22518 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
22519 
22520 #undef TARGET_ENCODE_SECTION_INFO
22521 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
22522 
22523 #undef TARGET_ATTRIBUTE_TABLE
22524 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
22525 /* All our function attributes are related to how out-of-line copies should
22526    be compiled or called.  They don't in themselves prevent inlining.  */
22527 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
22528 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
22529 
22530 #undef TARGET_EXTRA_LIVE_ON_ENTRY
22531 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
22532 
22533 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
22534 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
22535 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
22536 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
22537 
22538 #undef  TARGET_COMP_TYPE_ATTRIBUTES
22539 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
22540 
22541 #ifdef HAVE_AS_DTPRELWORD
22542 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
22543 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
22544 #endif
22545 #undef TARGET_DWARF_REGISTER_SPAN
22546 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
22547 #undef TARGET_DWARF_FRAME_REG_MODE
22548 #define TARGET_DWARF_FRAME_REG_MODE mips_dwarf_frame_reg_mode
22549 
22550 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
22551 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
22552 
22553 #undef TARGET_LEGITIMATE_ADDRESS_P
22554 #define TARGET_LEGITIMATE_ADDRESS_P	mips_legitimate_address_p
22555 
22556 #undef TARGET_FRAME_POINTER_REQUIRED
22557 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
22558 
22559 #undef TARGET_CAN_ELIMINATE
22560 #define TARGET_CAN_ELIMINATE mips_can_eliminate
22561 
22562 #undef TARGET_CONDITIONAL_REGISTER_USAGE
22563 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
22564 
22565 #undef TARGET_TRAMPOLINE_INIT
22566 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
22567 
22568 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
22569 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
22570 
22571 #undef TARGET_SHIFT_TRUNCATION_MASK
22572 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
22573 
22574 #undef TARGET_PREPARE_PCH_SAVE
22575 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
22576 
22577 #undef TARGET_VECTORIZE_VEC_PERM_CONST
22578 #define TARGET_VECTORIZE_VEC_PERM_CONST mips_vectorize_vec_perm_const
22579 
22580 #undef TARGET_SCHED_REASSOCIATION_WIDTH
22581 #define TARGET_SCHED_REASSOCIATION_WIDTH mips_sched_reassociation_width
22582 
22583 #undef TARGET_CASE_VALUES_THRESHOLD
22584 #define TARGET_CASE_VALUES_THRESHOLD mips_case_values_threshold
22585 
22586 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
22587 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV mips_atomic_assign_expand_fenv
22588 
22589 #undef TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS
22590 #define TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS true
22591 
22592 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
22593 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
22594   mips_use_by_pieces_infrastructure_p
22595 
22596 #undef TARGET_SPILL_CLASS
22597 #define TARGET_SPILL_CLASS mips_spill_class
22598 #undef TARGET_LRA_P
22599 #define TARGET_LRA_P mips_lra_p
22600 #undef TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS
22601 #define TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS mips_ira_change_pseudo_allocno_class
22602 
22603 #undef TARGET_HARD_REGNO_SCRATCH_OK
22604 #define TARGET_HARD_REGNO_SCRATCH_OK mips_hard_regno_scratch_ok
22605 
22606 #undef TARGET_HARD_REGNO_NREGS
22607 #define TARGET_HARD_REGNO_NREGS mips_hard_regno_nregs
22608 #undef TARGET_HARD_REGNO_MODE_OK
22609 #define TARGET_HARD_REGNO_MODE_OK mips_hard_regno_mode_ok
22610 
22611 #undef TARGET_MODES_TIEABLE_P
22612 #define TARGET_MODES_TIEABLE_P mips_modes_tieable_p
22613 
22614 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
22615 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
22616   mips_hard_regno_call_part_clobbered
22617 
22618 /* The architecture reserves bit 0 for MIPS16 so use bit 1 for descriptors.  */
22619 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
22620 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 2
22621 
22622 #undef TARGET_SECONDARY_MEMORY_NEEDED
22623 #define TARGET_SECONDARY_MEMORY_NEEDED mips_secondary_memory_needed
22624 
22625 #undef TARGET_CAN_CHANGE_MODE_CLASS
22626 #define TARGET_CAN_CHANGE_MODE_CLASS mips_can_change_mode_class
22627 
22628 #undef TARGET_TRULY_NOOP_TRUNCATION
22629 #define TARGET_TRULY_NOOP_TRUNCATION mips_truly_noop_truncation
22630 
22631 #undef TARGET_CONSTANT_ALIGNMENT
22632 #define TARGET_CONSTANT_ALIGNMENT mips_constant_alignment
22633 
22634 #undef TARGET_STARTING_FRAME_OFFSET
22635 #define TARGET_STARTING_FRAME_OFFSET mips_starting_frame_offset
22636 
22637 struct gcc_target targetm = TARGET_INITIALIZER;
22638 
22639 #include "gt-mips.h"
22640