Debian Codegraph @

gcc/config/mips/mips.cc

raw
1 /* Subroutines used for MIPS code generation.
2 Copyright (C) 1989-2025 Free Software Foundation, Inc.
3 Contributed by A. Lichnewsky, [email protected].
4 Changes by Michael Meissner, [email protected].
5 64-bit r4000 support by Ian Lance Taylor, [email protected], and
6 Brendan Eich, [email protected].
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 #include "flags.h"
69 #include "opts.h"
70
71 /* This file should be included last. */
72 #include "target-def.h"
73
74 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
75 #define UNSPEC_ADDRESS_P(X) \
76 (GET_CODE (X) == UNSPEC \
77 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
78 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
79
80 /* Extract the symbol or label from UNSPEC wrapper X. */
81 #define UNSPEC_ADDRESS(X) \
82 XVECEXP (X, 0, 0)
83
84 /* Extract the symbol type from UNSPEC wrapper X. */
85 #define UNSPEC_ADDRESS_TYPE(X) \
86 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
87
88 /* The maximum distance between the top of the stack frame and the
89 value $sp has when we save and restore registers.
90
91 The value for normal-mode code must be a SMALL_OPERAND and must
92 preserve the maximum stack alignment. We therefore use a value
93 of 0x7ff0 in this case.
94
95 microMIPS LWM and SWM support 12-bit offsets (from -0x800 to 0x7ff),
96 so we use a maximum of 0x7f0 for TARGET_MICROMIPS.
97
98 MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
99 up to 0x7f8 bytes and can usually save or restore all the registers
100 that we need to save or restore. (Note that we can only use these
101 instructions for o32, for which the stack alignment is 8 bytes.)
102
103 We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
104 RESTORE are not available. We can then use unextended instructions
105 to save and restore registers, and to allocate and deallocate the top
106 part of the frame. */
107 #define MIPS_MAX_FIRST_STACK_STEP \
108 (!TARGET_COMPRESSION ? 0x7ff0 \
109 : TARGET_MICROMIPS || GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8 \
110 : TARGET_64BIT ? 0x100 : 0x400)
111
112 /* True if INSN is a mips.md pattern or asm statement. */
113 /* ??? This test exists through the compiler, perhaps it should be
114 moved to rtl.h. */
115 #define USEFUL_INSN_P(INSN) \
116 (NONDEBUG_INSN_P (INSN) \
117 && GET_CODE (PATTERN (INSN)) != USE \
118 && GET_CODE (PATTERN (INSN)) != CLOBBER)
119
120 /* If INSN is a delayed branch sequence, return the first instruction
121 in the sequence, otherwise return INSN itself. */
122 #define SEQ_BEGIN(INSN) \
123 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
124 ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN), 0, 0)) \
125 : (INSN))
126
127 /* Likewise for the last instruction in a delayed branch sequence. */
128 #define SEQ_END(INSN) \
129 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
130 ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN), \
131 0, \
132 XVECLEN (PATTERN (INSN), 0) - 1)) \
133 : (INSN))
134
135 /* Execute the following loop body with SUBINSN set to each instruction
136 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
137 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
138 for ((SUBINSN) = SEQ_BEGIN (INSN); \
139 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
140 (SUBINSN) = NEXT_INSN (SUBINSN))
141
142 /* True if bit BIT is set in VALUE. */
143 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
144
145 /* Return the opcode for a ptr_mode load of the form:
146
147 l[wd] DEST, OFFSET(BASE). */
148 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE) \
149 (((ptr_mode == DImode ? 0x37 : 0x23) << 26) \
150 | ((BASE) << 21) \
151 | ((DEST) << 16) \
152 | (OFFSET))
153
154 /* Return the opcode to move register SRC into register DEST. */
155 #define MIPS_MOVE(DEST, SRC) \
156 ((TARGET_64BIT ? 0x2d : 0x21) \
157 | ((DEST) << 11) \
158 | ((SRC) << 21))
159
160 /* Return the opcode for:
161
162 lui DEST, VALUE. */
163 #define MIPS_LUI(DEST, VALUE) \
164 ((0xf << 26) | ((DEST) << 16) | (VALUE))
165
166 /* Return the opcode to jump to register DEST. When the JR opcode is not
167 available use JALR $0, DEST. */
168 #define MIPS_JR(DEST) \
169 (TARGET_CB_ALWAYS ? ((0x1b << 27) | ((DEST) << 16)) \
170 : (((DEST) << 21) | (ISA_HAS_JR ? 0x8 : 0x9)))
171
172 /* Return the opcode for:
173
174 bal . + (1 + OFFSET) * 4. */
175 #define MIPS_BAL(OFFSET) \
176 ((0x1 << 26) | (0x11 << 16) | (OFFSET))
177
178 /* Return the usual opcode for a nop. */
179 #define MIPS_NOP 0
180
181 /* Classifies an address.
182
183 ADDRESS_REG
184 A natural register + offset address. The register satisfies
185 mips_valid_base_register_p and the offset is a const_arith_operand.
186
187 ADDRESS_LO_SUM
188 A LO_SUM rtx. The first operand is a valid base register and
189 the second operand is a symbolic address.
190
191 ADDRESS_CONST_INT
192 A signed 16-bit constant address.
193
194 ADDRESS_SYMBOLIC:
195 A constant symbolic address. */
196 enum mips_address_type {
197 ADDRESS_REG,
198 ADDRESS_LO_SUM,
199 ADDRESS_CONST_INT,
200 ADDRESS_SYMBOLIC
201 };
202
203 /* Classifies an unconditional branch of interest for the P6600. */
204
205 enum mips_ucbranch_type
206 {
207 /* May not even be a branch. */
208 UC_UNDEFINED,
209 UC_BALC,
210 UC_OTHER
211 };
212
213 /* Macros to create an enumeration identifier for a function prototype. */
214 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
215 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
216 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
217 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
218
219 /* Classifies the prototype of a built-in function. */
220 enum mips_function_type {
221 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
222 #include "config/mips/mips-ftypes.def"
223 #undef DEF_MIPS_FTYPE
224 MIPS_MAX_FTYPE_MAX
225 };
226
227 /* Specifies how a built-in function should be converted into rtl. */
228 enum mips_builtin_type {
229 /* The function corresponds directly to an .md pattern. The return
230 value is mapped to operand 0 and the arguments are mapped to
231 operands 1 and above. */
232 MIPS_BUILTIN_DIRECT,
233
234 /* The function corresponds directly to an .md pattern. There is no return
235 value and the arguments are mapped to operands 0 and above. */
236 MIPS_BUILTIN_DIRECT_NO_TARGET,
237
238 /* The function corresponds to a comparison instruction followed by
239 a mips_cond_move_tf_ps pattern. The first two arguments are the
240 values to compare and the second two arguments are the vector
241 operands for the movt.ps or movf.ps instruction (in assembly order). */
242 MIPS_BUILTIN_MOVF,
243 MIPS_BUILTIN_MOVT,
244
245 /* The function corresponds to a V2SF comparison instruction. Operand 0
246 of this instruction is the result of the comparison, which has mode
247 CCV2 or CCV4. The function arguments are mapped to operands 1 and
248 above. The function's return value is an SImode boolean that is
249 true under the following conditions:
250
251 MIPS_BUILTIN_CMP_ANY: one of the registers is true
252 MIPS_BUILTIN_CMP_ALL: all of the registers are true
253 MIPS_BUILTIN_CMP_LOWER: the first register is true
254 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
255 MIPS_BUILTIN_CMP_ANY,
256 MIPS_BUILTIN_CMP_ALL,
257 MIPS_BUILTIN_CMP_UPPER,
258 MIPS_BUILTIN_CMP_LOWER,
259
260 /* As above, but the instruction only sets a single $fcc register. */
261 MIPS_BUILTIN_CMP_SINGLE,
262
263 /* The function corresponds to an MSA conditional branch instruction
264 combined with a compare instruction. */
265 MIPS_BUILTIN_MSA_TEST_BRANCH,
266
267 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
268 MIPS_BUILTIN_BPOSGE32
269 };
270
271 /* Invoke MACRO (COND) for each C.cond.fmt condition. */
272 #define MIPS_FP_CONDITIONS(MACRO) \
273 MACRO (f), \
274 MACRO (un), \
275 MACRO (eq), \
276 MACRO (ueq), \
277 MACRO (olt), \
278 MACRO (ult), \
279 MACRO (ole), \
280 MACRO (ule), \
281 MACRO (sf), \
282 MACRO (ngle), \
283 MACRO (seq), \
284 MACRO (ngl), \
285 MACRO (lt), \
286 MACRO (nge), \
287 MACRO (le), \
288 MACRO (ngt)
289
290 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
291 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
292 enum mips_fp_condition {
293 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
294 };
295 #undef DECLARE_MIPS_COND
296
297 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
298 #define STRINGIFY(X) #X
299 static const char *const mips_fp_conditions[] = {
300 MIPS_FP_CONDITIONS (STRINGIFY)
301 };
302 #undef STRINGIFY
303
304 /* A class used to control a comdat-style stub that we output in each
305 translation unit that needs it. */
306 class mips_one_only_stub {
307 public:
308 virtual ~mips_one_only_stub () {}
309
310 /* Return the name of the stub. */
311 virtual const char *get_name () = 0;
312
313 /* Output the body of the function to asm_out_file. */
314 virtual void output_body () = 0;
315 };
316
317 /* Tuning information that is automatically derived from other sources
318 (such as the scheduler). */
319 static struct {
320 /* The architecture and tuning settings that this structure describes. */
321 enum processor arch;
322 enum processor tune;
323
324 /* True if this structure describes MIPS16 settings. */
325 bool mips16_p;
326
327 /* True if the structure has been initialized. */
328 bool initialized_p;
329
330 /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
331 when optimizing for speed. */
332 bool fast_mult_zero_zero_p;
333 } mips_tuning_info;
334
335 /* Information about a single argument. */
336 struct mips_arg_info {
337 /* True if the argument is passed in a floating-point register, or
338 would have been if we hadn't run out of registers. */
339 bool fpr_p;
340
341 /* The number of words passed in registers, rounded up. */
342 unsigned int reg_words;
343
344 /* For EABI, the offset of the first register from GP_ARG_FIRST or
345 FP_ARG_FIRST. For other ABIs, the offset of the first register from
346 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
347 comment for details).
348
349 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
350 on the stack. */
351 unsigned int reg_offset;
352
353 /* The number of words that must be passed on the stack, rounded up. */
354 unsigned int stack_words;
355
356 /* The offset from the start of the stack overflow area of the argument's
357 first stack word. Only meaningful when STACK_WORDS is nonzero. */
358 unsigned int stack_offset;
359 };
360
361 /* Information about an address described by mips_address_type.
362
363 ADDRESS_CONST_INT
364 No fields are used.
365
366 ADDRESS_REG
367 REG is the base register and OFFSET is the constant offset.
368
369 ADDRESS_LO_SUM
370 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
371 is the type of symbol it references.
372
373 ADDRESS_SYMBOLIC
374 SYMBOL_TYPE is the type of symbol that the address references. */
375 struct mips_address_info {
376 enum mips_address_type type;
377 rtx reg;
378 rtx offset;
379 enum mips_symbol_type symbol_type;
380 };
381
382 /* One stage in a constant building sequence. These sequences have
383 the form:
384
385 A = VALUE[0]
386 A = A CODE[1] VALUE[1]
387 A = A CODE[2] VALUE[2]
388 ...
389
390 where A is an accumulator, each CODE[i] is a binary rtl operation
391 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
392 struct mips_integer_op {
393 enum rtx_code code;
394 unsigned HOST_WIDE_INT value;
395 };
396
397 /* The largest number of operations needed to load an integer constant.
398 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
399 When the lowest bit is clear, we can try, but reject a sequence with
400 an extra SLL at the end. */
401 #define MIPS_MAX_INTEGER_OPS 7
402
403 /* Information about a MIPS16e SAVE or RESTORE instruction. */
404 struct mips16e_save_restore_info {
405 /* The number of argument registers saved by a SAVE instruction.
406 0 for RESTORE instructions. */
407 unsigned int nargs;
408
409 /* Bit X is set if the instruction saves or restores GPR X. */
410 unsigned int mask;
411
412 /* The total number of bytes to allocate. */
413 HOST_WIDE_INT size;
414 };
415
416 /* Costs of various operations on the different architectures. */
417
418 struct mips_rtx_cost_data
419 {
420 unsigned short fp_add;
421 unsigned short fp_mult_sf;
422 unsigned short fp_mult_df;
423 unsigned short fp_div_sf;
424 unsigned short fp_div_df;
425 unsigned short int_mult_si;
426 unsigned short int_mult_di;
427 unsigned short int_div_si;
428 unsigned short int_div_di;
429 unsigned short branch_cost;
430 unsigned short memory_latency;
431 };
432
433 /* Global variables for machine-dependent things. */
434
435 /* The -G setting, or the configuration's default small-data limit if
436 no -G option is given. */
437 static unsigned int mips_small_data_threshold;
438
439 /* The number of file directives written by mips_output_filename. */
440 int num_source_filenames;
441
442 /* The name that appeared in the last .file directive written by
443 mips_output_filename, or "" if mips_output_filename hasn't
444 written anything yet. */
445 const char *current_function_file = "";
446
447 /* Arrays that map GCC register numbers to debugger register numbers. */
448 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
449
450 /* Information about the current function's epilogue, used only while
451 expanding it. */
452 static struct {
453 /* A list of queued REG_CFA_RESTORE notes. */
454 rtx cfa_restores;
455
456 /* The CFA is currently defined as CFA_REG + CFA_OFFSET. */
457 rtx cfa_reg;
458 HOST_WIDE_INT cfa_offset;
459
460 /* The offset of the CFA from the stack pointer while restoring
461 registers. */
462 HOST_WIDE_INT cfa_restore_sp_offset;
463 } mips_epilogue;
464
465 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs. */
466 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
467 struct mips_asm_switch mips_nomacro = { "macro", 0 };
468 struct mips_asm_switch mips_noat = { "at", 0 };
469
470 /* True if we're writing out a branch-likely instruction rather than a
471 normal branch. */
472 static bool mips_branch_likely;
473
474 /* The current instruction-set architecture. */
475 enum processor mips_arch;
476 const struct mips_cpu_info *mips_arch_info;
477
478 /* The processor that we should tune the code for. */
479 enum processor mips_tune;
480 const struct mips_cpu_info *mips_tune_info;
481
482 /* The ISA level associated with mips_arch. */
483 int mips_isa;
484
485 /* The ISA revision level. This is 0 for MIPS I to V and N for
486 MIPS{32,64}rN. */
487 int mips_isa_rev;
488
489 /* The architecture selected by -mipsN, or null if -mipsN wasn't used. */
490 static const struct mips_cpu_info *mips_isa_option_info;
491
492 /* Which cost information to use. */
493 static const struct mips_rtx_cost_data *mips_cost;
494
495 /* The ambient target flags, excluding MASK_MIPS16. */
496 static int mips_base_target_flags;
497
498 /* The default compression mode. */
499 unsigned int mips_base_compression_flags;
500
501 /* The default code readable setting. */
502 enum mips_code_readable_setting mips_base_code_readable;
503
504 /* The ambient values of other global variables. */
505 static int mips_base_schedule_insns; /* flag_schedule_insns */
506 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
507 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
508 static const char *mips_base_align_loops; /* align_loops */
509 static const char *mips_base_align_jumps; /* align_jumps */
510 static const char *mips_base_align_functions; /* align_functions */
511
512 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
513 static bool mips_hard_regno_mode_ok_p[MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
514
515 /* Index C is true if character C is a valid PRINT_OPERAND punctation
516 character. */
517 static bool mips_print_operand_punct[256];
518
519 static GTY (()) int mips_output_filename_first_time = 1;
520
521 /* mips_split_p[X] is true if symbols of type X can be split by
522 mips_split_symbol. */
523 bool mips_split_p[NUM_SYMBOL_TYPES];
524
525 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
526 can be split by mips_split_symbol. */
527 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
528
529 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
530 forced into a PC-relative constant pool. */
531 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
532
533 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
534 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
535 if they are matched by a special .md file pattern. */
536 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
537
538 /* Likewise for HIGHs. */
539 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
540
541 /* Target state for MIPS16. */
542 struct target_globals *mips16_globals;
543
544 /* Target state for MICROMIPS. */
545 struct target_globals *micromips_globals;
546
547 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
548 and returned from mips_sched_reorder2. */
549 static int cached_can_issue_more;
550
551 /* The stubs for various MIPS16 support functions, if used. */
552 static mips_one_only_stub *mips16_rdhwr_stub;
553 static mips_one_only_stub *mips16_get_fcsr_stub;
554 static mips_one_only_stub *mips16_set_fcsr_stub;
555
556 /* Index R is the smallest register class that contains register R. */
557 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
558 LEA_REGS, LEA_REGS, M16_STORE_REGS, V1_REG,
559 M16_STORE_REGS, M16_STORE_REGS, M16_STORE_REGS, M16_STORE_REGS,
560 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
561 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
562 M16_REGS, M16_STORE_REGS, LEA_REGS, LEA_REGS,
563 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
564 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
565 LEA_REGS, M16_SP_REGS, LEA_REGS, LEA_REGS,
566
567 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
568 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
569 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
570 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
571 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
572 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
573 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
574 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
575 MD0_REG, MD1_REG, NO_REGS, ST_REGS,
576 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
577 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
578 NO_REGS, FRAME_REGS, FRAME_REGS, NO_REGS,
579 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
580 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
581 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
582 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
583 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
584 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
585 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
586 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
587 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
588 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
589 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
590 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
591 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
592 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
593 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
594 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
595 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
596 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
597 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
598 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
599 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
600 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
601 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
602 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
603 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
604 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
605 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
606 };
607
608 static tree mips_handle_code_readable_attr (tree *, tree, tree, int, bool *);
609 static tree mips_handle_interrupt_attr (tree *, tree, tree, int, bool *);
610 static tree mips_handle_use_shadow_register_set_attr (tree *, tree, tree, int,
611 bool *);
612
613 /* The value of TARGET_ATTRIBUTE_TABLE. */
614 TARGET_GNU_ATTRIBUTES (mips_attribute_table, {
615 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
616 affects_type_identity, handler, exclude } */
617 { "long_call", 0, 0, false, true, true, false, NULL, NULL },
618 { "short_call", 0, 0, false, true, true, false, NULL, NULL },
619 { "far", 0, 0, false, true, true, false, NULL, NULL },
620 { "near", 0, 0, false, true, true, false, NULL, NULL },
621 /* We would really like to treat "mips16" and "nomips16" as type
622 attributes, but GCC doesn't provide the hooks we need to support
623 the right conversion rules. As declaration attributes, they affect
624 code generation but don't carry other semantics. */
625 { "mips16", 0, 0, true, false, false, false, NULL, NULL },
626 { "nomips16", 0, 0, true, false, false, false, NULL, NULL },
627 { "micromips", 0, 0, true, false, false, false, NULL, NULL },
628 { "nomicromips", 0, 0, true, false, false, false, NULL, NULL },
629 { "nocompression", 0, 0, true, false, false, false, NULL, NULL },
630 { "code_readable", 0, 1, true, false, false, false,
631 mips_handle_code_readable_attr, NULL },
632 /* Allow functions to be specified as interrupt handlers */
633 { "interrupt", 0, 1, false, true, true, false, mips_handle_interrupt_attr,
634 NULL },
635 { "use_shadow_register_set", 0, 1, false, true, true, false,
636 mips_handle_use_shadow_register_set_attr, NULL },
637 { "keep_interrupts_masked", 0, 0, false, true, true, false, NULL, NULL },
638 { "use_debug_exception_return", 0, 0, false, true, true, false, NULL, NULL },
639 { "use_hazard_barrier_return", 0, 0, true, false, false, false, NULL, NULL }
640 });
641
642 /* A table describing all the processors GCC knows about; see
643 mips-cpus.def for details. */
644 static const struct mips_cpu_info mips_cpu_info_table[] = {
645 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
646 { NAME, CPU, ISA, FLAGS },
647 #include "mips-cpus.def"
648 #undef MIPS_CPU
649 };
650
651 /* Default costs. If these are used for a processor we should look
652 up the actual costs. */
653 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
654 COSTS_N_INSNS (7), /* fp_mult_sf */ \
655 COSTS_N_INSNS (8), /* fp_mult_df */ \
656 COSTS_N_INSNS (23), /* fp_div_sf */ \
657 COSTS_N_INSNS (36), /* fp_div_df */ \
658 COSTS_N_INSNS (10), /* int_mult_si */ \
659 COSTS_N_INSNS (10), /* int_mult_di */ \
660 COSTS_N_INSNS (69), /* int_div_si */ \
661 COSTS_N_INSNS (69), /* int_div_di */ \
662 2, /* branch_cost */ \
663 4 /* memory_latency */
664
665 /* Floating-point costs for processors without an FPU. Just assume that
666 all floating-point libcalls are very expensive. */
667 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
668 COSTS_N_INSNS (256), /* fp_mult_sf */ \
669 COSTS_N_INSNS (256), /* fp_mult_df */ \
670 COSTS_N_INSNS (256), /* fp_div_sf */ \
671 COSTS_N_INSNS (256) /* fp_div_df */
672
673 /* Costs to use when optimizing for size. */
674 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
675 COSTS_N_INSNS (1), /* fp_add */
676 COSTS_N_INSNS (1), /* fp_mult_sf */
677 COSTS_N_INSNS (1), /* fp_mult_df */
678 COSTS_N_INSNS (1), /* fp_div_sf */
679 COSTS_N_INSNS (1), /* fp_div_df */
680 COSTS_N_INSNS (1), /* int_mult_si */
681 COSTS_N_INSNS (1), /* int_mult_di */
682 COSTS_N_INSNS (1), /* int_div_si */
683 COSTS_N_INSNS (1), /* int_div_di */
684 2, /* branch_cost */
685 4 /* memory_latency */
686 };
687
688 /* Costs to use when optimizing for speed, indexed by processor. */
689 static const struct mips_rtx_cost_data
690 mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
691 { /* R3000 */
692 COSTS_N_INSNS (2), /* fp_add */
693 COSTS_N_INSNS (4), /* fp_mult_sf */
694 COSTS_N_INSNS (5), /* fp_mult_df */
695 COSTS_N_INSNS (12), /* fp_div_sf */
696 COSTS_N_INSNS (19), /* fp_div_df */
697 COSTS_N_INSNS (12), /* int_mult_si */
698 COSTS_N_INSNS (12), /* int_mult_di */
699 COSTS_N_INSNS (35), /* int_div_si */
700 COSTS_N_INSNS (35), /* int_div_di */
701 1, /* branch_cost */
702 4 /* memory_latency */
703 },
704 { /* 4KC */
705 SOFT_FP_COSTS,
706 COSTS_N_INSNS (6), /* int_mult_si */
707 COSTS_N_INSNS (6), /* int_mult_di */
708 COSTS_N_INSNS (36), /* int_div_si */
709 COSTS_N_INSNS (36), /* int_div_di */
710 1, /* branch_cost */
711 4 /* memory_latency */
712 },
713 { /* 4KP */
714 SOFT_FP_COSTS,
715 COSTS_N_INSNS (36), /* int_mult_si */
716 COSTS_N_INSNS (36), /* int_mult_di */
717 COSTS_N_INSNS (37), /* int_div_si */
718 COSTS_N_INSNS (37), /* int_div_di */
719 1, /* branch_cost */
720 4 /* memory_latency */
721 },
722 { /* 5KC */
723 SOFT_FP_COSTS,
724 COSTS_N_INSNS (4), /* int_mult_si */
725 COSTS_N_INSNS (11), /* int_mult_di */
726 COSTS_N_INSNS (36), /* int_div_si */
727 COSTS_N_INSNS (68), /* int_div_di */
728 1, /* branch_cost */
729 4 /* memory_latency */
730 },
731 { /* 5KF */
732 COSTS_N_INSNS (4), /* fp_add */
733 COSTS_N_INSNS (4), /* fp_mult_sf */
734 COSTS_N_INSNS (5), /* fp_mult_df */
735 COSTS_N_INSNS (17), /* fp_div_sf */
736 COSTS_N_INSNS (32), /* fp_div_df */
737 COSTS_N_INSNS (4), /* int_mult_si */
738 COSTS_N_INSNS (11), /* int_mult_di */
739 COSTS_N_INSNS (36), /* int_div_si */
740 COSTS_N_INSNS (68), /* int_div_di */
741 1, /* branch_cost */
742 4 /* memory_latency */
743 },
744 { /* 20KC */
745 COSTS_N_INSNS (4), /* fp_add */
746 COSTS_N_INSNS (4), /* fp_mult_sf */
747 COSTS_N_INSNS (5), /* fp_mult_df */
748 COSTS_N_INSNS (17), /* fp_div_sf */
749 COSTS_N_INSNS (32), /* fp_div_df */
750 COSTS_N_INSNS (4), /* int_mult_si */
751 COSTS_N_INSNS (7), /* int_mult_di */
752 COSTS_N_INSNS (42), /* int_div_si */
753 COSTS_N_INSNS (72), /* int_div_di */
754 1, /* branch_cost */
755 4 /* memory_latency */
756 },
757 { /* 24KC */
758 SOFT_FP_COSTS,
759 COSTS_N_INSNS (5), /* int_mult_si */
760 COSTS_N_INSNS (5), /* int_mult_di */
761 COSTS_N_INSNS (41), /* int_div_si */
762 COSTS_N_INSNS (41), /* int_div_di */
763 1, /* branch_cost */
764 4 /* memory_latency */
765 },
766 { /* 24KF2_1 */
767 COSTS_N_INSNS (8), /* fp_add */
768 COSTS_N_INSNS (8), /* fp_mult_sf */
769 COSTS_N_INSNS (10), /* fp_mult_df */
770 COSTS_N_INSNS (34), /* fp_div_sf */
771 COSTS_N_INSNS (64), /* fp_div_df */
772 COSTS_N_INSNS (5), /* int_mult_si */
773 COSTS_N_INSNS (5), /* int_mult_di */
774 COSTS_N_INSNS (41), /* int_div_si */
775 COSTS_N_INSNS (41), /* int_div_di */
776 1, /* branch_cost */
777 4 /* memory_latency */
778 },
779 { /* 24KF1_1 */
780 COSTS_N_INSNS (4), /* fp_add */
781 COSTS_N_INSNS (4), /* fp_mult_sf */
782 COSTS_N_INSNS (5), /* fp_mult_df */
783 COSTS_N_INSNS (17), /* fp_div_sf */
784 COSTS_N_INSNS (32), /* fp_div_df */
785 COSTS_N_INSNS (5), /* int_mult_si */
786 COSTS_N_INSNS (5), /* int_mult_di */
787 COSTS_N_INSNS (41), /* int_div_si */
788 COSTS_N_INSNS (41), /* int_div_di */
789 1, /* branch_cost */
790 4 /* memory_latency */
791 },
792 { /* 74KC */
793 SOFT_FP_COSTS,
794 COSTS_N_INSNS (5), /* int_mult_si */
795 COSTS_N_INSNS (5), /* int_mult_di */
796 COSTS_N_INSNS (41), /* int_div_si */
797 COSTS_N_INSNS (41), /* int_div_di */
798 1, /* branch_cost */
799 4 /* memory_latency */
800 },
801 { /* 74KF2_1 */
802 COSTS_N_INSNS (8), /* fp_add */
803 COSTS_N_INSNS (8), /* fp_mult_sf */
804 COSTS_N_INSNS (10), /* fp_mult_df */
805 COSTS_N_INSNS (34), /* fp_div_sf */
806 COSTS_N_INSNS (64), /* fp_div_df */
807 COSTS_N_INSNS (5), /* int_mult_si */
808 COSTS_N_INSNS (5), /* int_mult_di */
809 COSTS_N_INSNS (41), /* int_div_si */
810 COSTS_N_INSNS (41), /* int_div_di */
811 1, /* branch_cost */
812 4 /* memory_latency */
813 },
814 { /* 74KF1_1 */
815 COSTS_N_INSNS (4), /* fp_add */
816 COSTS_N_INSNS (4), /* fp_mult_sf */
817 COSTS_N_INSNS (5), /* fp_mult_df */
818 COSTS_N_INSNS (17), /* fp_div_sf */
819 COSTS_N_INSNS (32), /* fp_div_df */
820 COSTS_N_INSNS (5), /* int_mult_si */
821 COSTS_N_INSNS (5), /* int_mult_di */
822 COSTS_N_INSNS (41), /* int_div_si */
823 COSTS_N_INSNS (41), /* int_div_di */
824 1, /* branch_cost */
825 4 /* memory_latency */
826 },
827 { /* 74KF3_2 */
828 COSTS_N_INSNS (6), /* fp_add */
829 COSTS_N_INSNS (6), /* fp_mult_sf */
830 COSTS_N_INSNS (7), /* fp_mult_df */
831 COSTS_N_INSNS (25), /* fp_div_sf */
832 COSTS_N_INSNS (48), /* fp_div_df */
833 COSTS_N_INSNS (5), /* int_mult_si */
834 COSTS_N_INSNS (5), /* int_mult_di */
835 COSTS_N_INSNS (41), /* int_div_si */
836 COSTS_N_INSNS (41), /* int_div_di */
837 1, /* branch_cost */
838 4 /* memory_latency */
839 },
840 { /* Loongson-2E */
841 DEFAULT_COSTS
842 },
843 { /* Loongson-2F */
844 DEFAULT_COSTS
845 },
846 { /* Loongson gs464. */
847 DEFAULT_COSTS
848 },
849 { /* Loongson gs464e. */
850 DEFAULT_COSTS
851 },
852 { /* Loongson gs264e. */
853 DEFAULT_COSTS
854 },
855 { /* M4k */
856 DEFAULT_COSTS
857 },
858 /* Octeon */
859 {
860 SOFT_FP_COSTS,
861 COSTS_N_INSNS (5), /* int_mult_si */
862 COSTS_N_INSNS (5), /* int_mult_di */
863 COSTS_N_INSNS (72), /* int_div_si */
864 COSTS_N_INSNS (72), /* int_div_di */
865 1, /* branch_cost */
866 4 /* memory_latency */
867 },
868 /* Octeon II */
869 {
870 SOFT_FP_COSTS,
871 COSTS_N_INSNS (6), /* int_mult_si */
872 COSTS_N_INSNS (6), /* int_mult_di */
873 COSTS_N_INSNS (18), /* int_div_si */
874 COSTS_N_INSNS (35), /* int_div_di */
875 4, /* branch_cost */
876 4 /* memory_latency */
877 },
878 /* Octeon III */
879 {
880 COSTS_N_INSNS (6), /* fp_add */
881 COSTS_N_INSNS (6), /* fp_mult_sf */
882 COSTS_N_INSNS (7), /* fp_mult_df */
883 COSTS_N_INSNS (25), /* fp_div_sf */
884 COSTS_N_INSNS (48), /* fp_div_df */
885 COSTS_N_INSNS (6), /* int_mult_si */
886 COSTS_N_INSNS (6), /* int_mult_di */
887 COSTS_N_INSNS (18), /* int_div_si */
888 COSTS_N_INSNS (35), /* int_div_di */
889 4, /* branch_cost */
890 4 /* memory_latency */
891 },
892 { /* R3900 */
893 COSTS_N_INSNS (2), /* fp_add */
894 COSTS_N_INSNS (4), /* fp_mult_sf */
895 COSTS_N_INSNS (5), /* fp_mult_df */
896 COSTS_N_INSNS (12), /* fp_div_sf */
897 COSTS_N_INSNS (19), /* fp_div_df */
898 COSTS_N_INSNS (2), /* int_mult_si */
899 COSTS_N_INSNS (2), /* int_mult_di */
900 COSTS_N_INSNS (35), /* int_div_si */
901 COSTS_N_INSNS (35), /* int_div_di */
902 1, /* branch_cost */
903 4 /* memory_latency */
904 },
905 { /* R6000 */
906 COSTS_N_INSNS (3), /* fp_add */
907 COSTS_N_INSNS (5), /* fp_mult_sf */
908 COSTS_N_INSNS (6), /* fp_mult_df */
909 COSTS_N_INSNS (15), /* fp_div_sf */
910 COSTS_N_INSNS (16), /* fp_div_df */
911 COSTS_N_INSNS (17), /* int_mult_si */
912 COSTS_N_INSNS (17), /* int_mult_di */
913 COSTS_N_INSNS (38), /* int_div_si */
914 COSTS_N_INSNS (38), /* int_div_di */
915 2, /* branch_cost */
916 6 /* memory_latency */
917 },
918 { /* R4000 */
919 COSTS_N_INSNS (6), /* fp_add */
920 COSTS_N_INSNS (7), /* fp_mult_sf */
921 COSTS_N_INSNS (8), /* fp_mult_df */
922 COSTS_N_INSNS (23), /* fp_div_sf */
923 COSTS_N_INSNS (36), /* fp_div_df */
924 COSTS_N_INSNS (10), /* int_mult_si */
925 COSTS_N_INSNS (10), /* int_mult_di */
926 COSTS_N_INSNS (69), /* int_div_si */
927 COSTS_N_INSNS (69), /* int_div_di */
928 2, /* branch_cost */
929 6 /* memory_latency */
930 },
931 { /* R4100 */
932 DEFAULT_COSTS
933 },
934 { /* R4111 */
935 DEFAULT_COSTS
936 },
937 { /* R4120 */
938 DEFAULT_COSTS
939 },
940 { /* R4130 */
941 /* The only costs that appear to be updated here are
942 integer multiplication. */
943 SOFT_FP_COSTS,
944 COSTS_N_INSNS (4), /* int_mult_si */
945 COSTS_N_INSNS (6), /* int_mult_di */
946 COSTS_N_INSNS (69), /* int_div_si */
947 COSTS_N_INSNS (69), /* int_div_di */
948 1, /* branch_cost */
949 4 /* memory_latency */
950 },
951 { /* R4300 */
952 DEFAULT_COSTS
953 },
954 { /* R4600 */
955 DEFAULT_COSTS
956 },
957 { /* R4650 */
958 DEFAULT_COSTS
959 },
960 { /* R4700 */
961 DEFAULT_COSTS
962 },
963 { /* R5000 */
964 COSTS_N_INSNS (6), /* fp_add */
965 COSTS_N_INSNS (4), /* fp_mult_sf */
966 COSTS_N_INSNS (5), /* fp_mult_df */
967 COSTS_N_INSNS (23), /* fp_div_sf */
968 COSTS_N_INSNS (36), /* fp_div_df */
969 COSTS_N_INSNS (5), /* int_mult_si */
970 COSTS_N_INSNS (5), /* int_mult_di */
971 COSTS_N_INSNS (36), /* int_div_si */
972 COSTS_N_INSNS (36), /* int_div_di */
973 1, /* branch_cost */
974 4 /* memory_latency */
975 },
976 { /* R5400 */
977 COSTS_N_INSNS (6), /* fp_add */
978 COSTS_N_INSNS (5), /* fp_mult_sf */
979 COSTS_N_INSNS (6), /* fp_mult_df */
980 COSTS_N_INSNS (30), /* fp_div_sf */
981 COSTS_N_INSNS (59), /* fp_div_df */
982 COSTS_N_INSNS (3), /* int_mult_si */
983 COSTS_N_INSNS (4), /* int_mult_di */
984 COSTS_N_INSNS (42), /* int_div_si */
985 COSTS_N_INSNS (74), /* int_div_di */
986 1, /* branch_cost */
987 4 /* memory_latency */
988 },
989 { /* R5500 */
990 COSTS_N_INSNS (6), /* fp_add */
991 COSTS_N_INSNS (5), /* fp_mult_sf */
992 COSTS_N_INSNS (6), /* fp_mult_df */
993 COSTS_N_INSNS (30), /* fp_div_sf */
994 COSTS_N_INSNS (59), /* fp_div_df */
995 COSTS_N_INSNS (5), /* int_mult_si */
996 COSTS_N_INSNS (9), /* int_mult_di */
997 COSTS_N_INSNS (42), /* int_div_si */
998 COSTS_N_INSNS (74), /* int_div_di */
999 1, /* branch_cost */
1000 4 /* memory_latency */
1001 },
1002 { /* R5900 */
1003 COSTS_N_INSNS (4), /* fp_add */
1004 COSTS_N_INSNS (4), /* fp_mult_sf */
1005 COSTS_N_INSNS (256), /* fp_mult_df */
1006 COSTS_N_INSNS (8), /* fp_div_sf */
1007 COSTS_N_INSNS (256), /* fp_div_df */
1008 COSTS_N_INSNS (4), /* int_mult_si */
1009 COSTS_N_INSNS (256), /* int_mult_di */
1010 COSTS_N_INSNS (37), /* int_div_si */
1011 COSTS_N_INSNS (256), /* int_div_di */
1012 1, /* branch_cost */
1013 4 /* memory_latency */
1014 },
1015 { /* R7000 */
1016 /* The only costs that are changed here are
1017 integer multiplication. */
1018 COSTS_N_INSNS (6), /* fp_add */
1019 COSTS_N_INSNS (7), /* fp_mult_sf */
1020 COSTS_N_INSNS (8), /* fp_mult_df */
1021 COSTS_N_INSNS (23), /* fp_div_sf */
1022 COSTS_N_INSNS (36), /* fp_div_df */
1023 COSTS_N_INSNS (5), /* int_mult_si */
1024 COSTS_N_INSNS (9), /* int_mult_di */
1025 COSTS_N_INSNS (69), /* int_div_si */
1026 COSTS_N_INSNS (69), /* int_div_di */
1027 1, /* branch_cost */
1028 4 /* memory_latency */
1029 },
1030 { /* R8000 */
1031 DEFAULT_COSTS
1032 },
1033 { /* R9000 */
1034 /* The only costs that are changed here are
1035 integer multiplication. */
1036 COSTS_N_INSNS (6), /* fp_add */
1037 COSTS_N_INSNS (7), /* fp_mult_sf */
1038 COSTS_N_INSNS (8), /* fp_mult_df */
1039 COSTS_N_INSNS (23), /* fp_div_sf */
1040 COSTS_N_INSNS (36), /* fp_div_df */
1041 COSTS_N_INSNS (3), /* int_mult_si */
1042 COSTS_N_INSNS (8), /* int_mult_di */
1043 COSTS_N_INSNS (69), /* int_div_si */
1044 COSTS_N_INSNS (69), /* int_div_di */
1045 1, /* branch_cost */
1046 4 /* memory_latency */
1047 },
1048 { /* R1x000 */
1049 COSTS_N_INSNS (2), /* fp_add */
1050 COSTS_N_INSNS (2), /* fp_mult_sf */
1051 COSTS_N_INSNS (2), /* fp_mult_df */
1052 COSTS_N_INSNS (12), /* fp_div_sf */
1053 COSTS_N_INSNS (19), /* fp_div_df */
1054 COSTS_N_INSNS (5), /* int_mult_si */
1055 COSTS_N_INSNS (9), /* int_mult_di */
1056 COSTS_N_INSNS (34), /* int_div_si */
1057 COSTS_N_INSNS (66), /* int_div_di */
1058 1, /* branch_cost */
1059 4 /* memory_latency */
1060 },
1061 { /* SB1 */
1062 /* These costs are the same as the SB-1A below. */
1063 COSTS_N_INSNS (4), /* fp_add */
1064 COSTS_N_INSNS (4), /* fp_mult_sf */
1065 COSTS_N_INSNS (4), /* fp_mult_df */
1066 COSTS_N_INSNS (24), /* fp_div_sf */
1067 COSTS_N_INSNS (32), /* fp_div_df */
1068 COSTS_N_INSNS (3), /* int_mult_si */
1069 COSTS_N_INSNS (4), /* int_mult_di */
1070 COSTS_N_INSNS (36), /* int_div_si */
1071 COSTS_N_INSNS (68), /* int_div_di */
1072 1, /* branch_cost */
1073 4 /* memory_latency */
1074 },
1075 { /* SB1-A */
1076 /* These costs are the same as the SB-1 above. */
1077 COSTS_N_INSNS (4), /* fp_add */
1078 COSTS_N_INSNS (4), /* fp_mult_sf */
1079 COSTS_N_INSNS (4), /* fp_mult_df */
1080 COSTS_N_INSNS (24), /* fp_div_sf */
1081 COSTS_N_INSNS (32), /* fp_div_df */
1082 COSTS_N_INSNS (3), /* int_mult_si */
1083 COSTS_N_INSNS (4), /* int_mult_di */
1084 COSTS_N_INSNS (36), /* int_div_si */
1085 COSTS_N_INSNS (68), /* int_div_di */
1086 1, /* branch_cost */
1087 4 /* memory_latency */
1088 },
1089 { /* SR71000 */
1090 DEFAULT_COSTS
1091 },
1092 { /* XLR */
1093 SOFT_FP_COSTS,
1094 COSTS_N_INSNS (8), /* int_mult_si */
1095 COSTS_N_INSNS (8), /* int_mult_di */
1096 COSTS_N_INSNS (72), /* int_div_si */
1097 COSTS_N_INSNS (72), /* int_div_di */
1098 1, /* branch_cost */
1099 4 /* memory_latency */
1100 },
1101 { /* XLP */
1102 /* These costs are the same as 5KF above. */
1103 COSTS_N_INSNS (4), /* fp_add */
1104 COSTS_N_INSNS (4), /* fp_mult_sf */
1105 COSTS_N_INSNS (5), /* fp_mult_df */
1106 COSTS_N_INSNS (17), /* fp_div_sf */
1107 COSTS_N_INSNS (32), /* fp_div_df */
1108 COSTS_N_INSNS (4), /* int_mult_si */
1109 COSTS_N_INSNS (11), /* int_mult_di */
1110 COSTS_N_INSNS (36), /* int_div_si */
1111 COSTS_N_INSNS (68), /* int_div_di */
1112 1, /* branch_cost */
1113 4 /* memory_latency */
1114 },
1115 { /* P5600 */
1116 COSTS_N_INSNS (4), /* fp_add */
1117 COSTS_N_INSNS (5), /* fp_mult_sf */
1118 COSTS_N_INSNS (5), /* fp_mult_df */
1119 COSTS_N_INSNS (17), /* fp_div_sf */
1120 COSTS_N_INSNS (17), /* fp_div_df */
1121 COSTS_N_INSNS (5), /* int_mult_si */
1122 COSTS_N_INSNS (5), /* int_mult_di */
1123 COSTS_N_INSNS (8), /* int_div_si */
1124 COSTS_N_INSNS (8), /* int_div_di */
1125 2, /* branch_cost */
1126 4 /* memory_latency */
1127 },
1128 { /* M5100 */
1129 COSTS_N_INSNS (4), /* fp_add */
1130 COSTS_N_INSNS (4), /* fp_mult_sf */
1131 COSTS_N_INSNS (5), /* fp_mult_df */
1132 COSTS_N_INSNS (17), /* fp_div_sf */
1133 COSTS_N_INSNS (32), /* fp_div_df */
1134 COSTS_N_INSNS (5), /* int_mult_si */
1135 COSTS_N_INSNS (5), /* int_mult_di */
1136 COSTS_N_INSNS (34), /* int_div_si */
1137 COSTS_N_INSNS (68), /* int_div_di */
1138 1, /* branch_cost */
1139 4 /* memory_latency */
1140 },
1141 { /* I6400 */
1142 COSTS_N_INSNS (4), /* fp_add */
1143 COSTS_N_INSNS (5), /* fp_mult_sf */
1144 COSTS_N_INSNS (5), /* fp_mult_df */
1145 COSTS_N_INSNS (32), /* fp_div_sf */
1146 COSTS_N_INSNS (32), /* fp_div_df */
1147 COSTS_N_INSNS (5), /* int_mult_si */
1148 COSTS_N_INSNS (5), /* int_mult_di */
1149 COSTS_N_INSNS (36), /* int_div_si */
1150 COSTS_N_INSNS (36), /* int_div_di */
1151 2, /* branch_cost */
1152 4 /* memory_latency */
1153 },
1154 { /* P6600 */
1155 COSTS_N_INSNS (4), /* fp_add */
1156 COSTS_N_INSNS (5), /* fp_mult_sf */
1157 COSTS_N_INSNS (5), /* fp_mult_df */
1158 COSTS_N_INSNS (17), /* fp_div_sf */
1159 COSTS_N_INSNS (17), /* fp_div_df */
1160 COSTS_N_INSNS (5), /* int_mult_si */
1161 COSTS_N_INSNS (5), /* int_mult_di */
1162 COSTS_N_INSNS (8), /* int_div_si */
1163 COSTS_N_INSNS (8), /* int_div_di */
1164 2, /* branch_cost */
1165 4 /* memory_latency */
1166 }
1167 };
1168
1169 static rtx mips_find_pic_call_symbol (rtx_insn *, rtx, bool);
1170 static int mips_register_move_cost (machine_mode, reg_class_t,
1171 reg_class_t);
1172 static unsigned int mips_function_arg_boundary (machine_mode, const_tree);
1173 static rtx mips_gen_const_int_vector_shuffle (machine_mode, int);
1174
1175 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1176 for -mflip_mips16. It maps decl names onto a boolean mode setting. */
1177 static GTY (()) hash_map<nofree_string_hash, bool> *mflip_mips16_htab;
1178
1179 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1180 mode, false if it should next add an attribute for the opposite mode. */
1181 static GTY(()) bool mips16_flipper;
1182
1183 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1184 for -mflip-mips16. Return true if it should use "mips16" and false if
1185 it should use "nomips16". */
1186
1187 static bool
1188 mflip_mips16_use_mips16_p (tree decl)
1189 {
1190 const char *name;
1191 bool base_is_mips16 = (mips_base_compression_flags & MASK_MIPS16) != 0;
1192
1193 /* Use the opposite of the command-line setting for anonymous decls. */
1194 if (!DECL_NAME (decl))
1195 return !base_is_mips16;
1196
1197 if (!mflip_mips16_htab)
1198 mflip_mips16_htab = hash_map<nofree_string_hash, bool>::create_ggc (37);
1199
1200 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1201
1202 bool existed;
1203 bool *slot = &mflip_mips16_htab->get_or_insert (name, &existed);
1204 if (!existed)
1205 {
1206 mips16_flipper = !mips16_flipper;
1207 *slot = mips16_flipper ? !base_is_mips16 : base_is_mips16;
1208 }
1209 return *slot;
1210 }
1211
1212 /* Predicates to test for presence of "near"/"short_call" and "far"/"long_call"
1213 attributes on the given TYPE. */
1214
1215 static bool
1216 mips_near_type_p (const_tree type)
1217 {
1218 return (lookup_attribute ("short_call", TYPE_ATTRIBUTES (type)) != NULL
1219 || lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL);
1220 }
1221
1222 static bool
1223 mips_far_type_p (const_tree type)
1224 {
1225 return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1226 || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1227 }
1228
1229
1230 /* Check if the interrupt attribute is set for a function. */
1231
1232 static bool
1233 mips_interrupt_type_p (tree type)
1234 {
1235 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1236 }
1237
1238 /* Return the mask for the "interrupt" attribute. */
1239
1240 static enum mips_int_mask
1241 mips_interrupt_mask (tree type)
1242 {
1243 tree attr = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type));
1244 tree args, cst;
1245 const char *str;
1246
1247 /* For missing attributes or no arguments then return 'eic' as a safe
1248 fallback. */
1249 if (attr == NULL)
1250 return INT_MASK_EIC;
1251
1252 args = TREE_VALUE (attr);
1253
1254 if (args == NULL)
1255 return INT_MASK_EIC;
1256
1257 cst = TREE_VALUE (args);
1258
1259 if (strcmp (TREE_STRING_POINTER (cst), "eic") == 0)
1260 return INT_MASK_EIC;
1261
1262 /* The validation code in mips_handle_interrupt_attr guarantees that the
1263 argument is now in the form:
1264 vector=(sw0|sw1|hw0|hw1|hw2|hw3|hw4|hw5). */
1265 str = TREE_STRING_POINTER (cst);
1266
1267 gcc_assert (strlen (str) == strlen ("vector=sw0"));
1268
1269 if (str[7] == 's')
1270 return (enum mips_int_mask) (INT_MASK_SW0 + (str[9] - '0'));
1271
1272 return (enum mips_int_mask) (INT_MASK_HW0 + (str[9] - '0'));
1273 }
1274
1275 /* Return the mips_shadow_set if the "use_shadow_register_set" attribute is
1276 set for a function. */
1277
1278 static enum mips_shadow_set
1279 mips_use_shadow_register_set (tree type)
1280 {
1281 tree attr = lookup_attribute ("use_shadow_register_set",
1282 TYPE_ATTRIBUTES (type));
1283 tree args;
1284
1285 /* The validation code in mips_handle_use_shadow_register_set_attr guarantees
1286 that if an argument is present then it means: Assume the shadow register
1287 set has a valid stack pointer in it. */
1288 if (attr == NULL)
1289 return SHADOW_SET_NO;
1290
1291 args = TREE_VALUE (attr);
1292
1293 if (args == NULL)
1294 return SHADOW_SET_YES;
1295
1296 return SHADOW_SET_INTSTACK;
1297 }
1298
1299 /* Check if the attribute to keep interrupts masked is set for a function. */
1300
1301 static bool
1302 mips_keep_interrupts_masked_p (tree type)
1303 {
1304 return lookup_attribute ("keep_interrupts_masked",
1305 TYPE_ATTRIBUTES (type)) != NULL;
1306 }
1307
1308 /* Check if the attribute to use debug exception return is set for
1309 a function. */
1310
1311 static bool
1312 mips_use_debug_exception_return_p (tree type)
1313 {
1314 return lookup_attribute ("use_debug_exception_return",
1315 TYPE_ATTRIBUTES (type)) != NULL;
1316 }
1317
1318
1319 /* Verify the arguments to a code_readable attribute. */
1320
1321 static tree
1322 mips_handle_code_readable_attr (tree *node ATTRIBUTE_UNUSED, tree name,
1323 tree args, int flags ATTRIBUTE_UNUSED,
1324 bool *no_add_attrs)
1325 {
1326 if (!is_attribute_p ("code_readable", name) || args == NULL)
1327 return NULL_TREE;
1328
1329 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
1330 {
1331 warning (OPT_Wattributes,
1332 "%qE attribute requires a string argument", name);
1333 *no_add_attrs = true;
1334 }
1335 else if (strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "no") != 0
1336 && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "pcrel") != 0
1337 && strcmp (TREE_STRING_POINTER (TREE_VALUE (args)), "yes") != 0)
1338 {
1339 warning (OPT_Wattributes,
1340 "argument to %qE attribute is neither no, pcrel nor yes", name);
1341 *no_add_attrs = true;
1342 }
1343
1344 return NULL_TREE;
1345 }
1346
1347 /* Determine the code_readable setting for a function if it has one. Set
1348 *valid to true if we have a properly formed argument and
1349 return the result. If there's no argument, return GCC's default.
1350 Otherwise, leave valid false and return mips_base_code_readable. In
1351 that case the result should be unused anyway. */
1352
1353 static enum mips_code_readable_setting
1354 mips_get_code_readable_attr (tree decl)
1355 {
1356 tree attr;
1357
1358 if (decl == NULL)
1359 return mips_base_code_readable;
1360
1361 attr = lookup_attribute ("code_readable", DECL_ATTRIBUTES (decl));
1362
1363 if (attr != NULL)
1364 {
1365 if (TREE_VALUE (attr) != NULL_TREE)
1366 {
1367 const char * str;
1368
1369 str = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr)));
1370 if (strcmp (str, "no") == 0)
1371 return CODE_READABLE_NO;
1372 else if (strcmp (str, "pcrel") == 0)
1373 return CODE_READABLE_PCREL;
1374 else if (strcmp (str, "yes") == 0)
1375 return CODE_READABLE_YES;
1376
1377 /* mips_handle_code_readable_attr will have verified the
1378 arguments are correct before adding the attribute. */
1379 gcc_unreachable ();
1380 }
1381
1382 /* Just like GCC's default -mcode-readable= setting, the
1383 presence of the code_readable attribute implies that a
1384 function can read data from the instruction stream by
1385 default. */
1386 return CODE_READABLE_YES;
1387 }
1388
1389 return mips_base_code_readable;
1390 }
1391
1392
1393 /* Return the set of compression modes that are explicitly required
1394 by the attributes in ATTRIBUTES. */
1395
1396 static unsigned int
1397 mips_get_compress_on_flags (tree attributes)
1398 {
1399 unsigned int flags = 0;
1400
1401 if (lookup_attribute ("mips16", attributes) != NULL)
1402 flags |= MASK_MIPS16;
1403
1404 if (lookup_attribute ("micromips", attributes) != NULL)
1405 flags |= MASK_MICROMIPS;
1406
1407 return flags;
1408 }
1409
1410 /* Return the set of compression modes that are explicitly forbidden
1411 by the attributes in ATTRIBUTES. */
1412
1413 static unsigned int
1414 mips_get_compress_off_flags (tree attributes)
1415 {
1416 unsigned int flags = 0;
1417
1418 if (lookup_attribute ("nocompression", attributes) != NULL)
1419 flags |= MASK_MIPS16 | MASK_MICROMIPS;
1420
1421 if (lookup_attribute ("nomips16", attributes) != NULL)
1422 flags |= MASK_MIPS16;
1423
1424 if (lookup_attribute ("nomicromips", attributes) != NULL)
1425 flags |= MASK_MICROMIPS;
1426
1427 return flags;
1428 }
1429
1430 /* Return the compression mode that should be used for function DECL.
1431 Return the ambient setting if DECL is null. */
1432
1433 static unsigned int
1434 mips_get_compress_mode (tree decl)
1435 {
1436 unsigned int flags, force_on;
1437
1438 flags = mips_base_compression_flags;
1439 if (decl)
1440 {
1441 /* Nested functions must use the same frame pointer as their
1442 parent and must therefore use the same ISA mode. */
1443 tree parent = decl_function_context (decl);
1444 if (parent)
1445 decl = parent;
1446 force_on = mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1447 if (force_on)
1448 return force_on;
1449 flags &= ~mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1450 }
1451 return flags;
1452 }
1453
1454 /* Return the attribute name associated with MASK_MIPS16 and MASK_MICROMIPS
1455 flags FLAGS. */
1456
1457 static const char *
1458 mips_get_compress_on_name (unsigned int flags)
1459 {
1460 if (flags == MASK_MIPS16)
1461 return "mips16";
1462 return "micromips";
1463 }
1464
1465 /* Return the attribute name that forbids MASK_MIPS16 and MASK_MICROMIPS
1466 flags FLAGS. */
1467
1468 static const char *
1469 mips_get_compress_off_name (unsigned int flags)
1470 {
1471 if (flags == MASK_MIPS16)
1472 return "nomips16";
1473 if (flags == MASK_MICROMIPS)
1474 return "nomicromips";
1475 return "nocompression";
1476 }
1477
1478 /* Implement TARGET_COMP_TYPE_ATTRIBUTES. */
1479
1480 static int
1481 mips_comp_type_attributes (const_tree type1, const_tree type2)
1482 {
1483 /* Disallow mixed near/far attributes. */
1484 if (mips_far_type_p (type1) && mips_near_type_p (type2))
1485 return 0;
1486 if (mips_near_type_p (type1) && mips_far_type_p (type2))
1487 return 0;
1488 return 1;
1489 }
1490
1491 /* Implement TARGET_INSERT_ATTRIBUTES. */
1492
1493 static void
1494 mips_insert_attributes (tree decl, tree *attributes)
1495 {
1496 const char *name;
1497 unsigned int compression_flags, nocompression_flags;
1498
1499 /* Check for "mips16" and "nomips16" attributes. */
1500 compression_flags = mips_get_compress_on_flags (*attributes);
1501 nocompression_flags = mips_get_compress_off_flags (*attributes);
1502
1503 if (TREE_CODE (decl) != FUNCTION_DECL)
1504 {
1505 if (nocompression_flags)
1506 error ("%qs attribute only applies to functions",
1507 mips_get_compress_off_name (nocompression_flags));
1508
1509 if (compression_flags)
1510 error ("%qs attribute only applies to functions",
1511 mips_get_compress_on_name (nocompression_flags));
1512 }
1513 else
1514 {
1515 compression_flags |= mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1516 nocompression_flags |=
1517 mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1518
1519 if (compression_flags && nocompression_flags)
1520 error ("%qE cannot have both %qs and %qs attributes",
1521 DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
1522 mips_get_compress_off_name (nocompression_flags));
1523
1524 if (compression_flags & MASK_MIPS16
1525 && compression_flags & MASK_MICROMIPS)
1526 error ("%qE cannot have both %qs and %qs attributes",
1527 DECL_NAME (decl), "mips16", "micromips");
1528
1529 if (TARGET_FLIP_MIPS16
1530 && !DECL_ARTIFICIAL (decl)
1531 && compression_flags == 0
1532 && nocompression_flags == 0)
1533 {
1534 /* Implement -mflip-mips16. If DECL has neither a "nomips16" nor a
1535 "mips16" attribute, arbitrarily pick one. We must pick the same
1536 setting for duplicate declarations of a function. */
1537 name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1538 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1539 name = "nomicromips";
1540 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1541 }
1542 }
1543 }
1544
1545 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
1546
1547 static tree
1548 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1549 {
1550 unsigned int diff;
1551
1552 diff = (mips_get_compress_on_flags (DECL_ATTRIBUTES (olddecl))
1553 ^ mips_get_compress_on_flags (DECL_ATTRIBUTES (newdecl)));
1554 if (diff)
1555 error ("%qE redeclared with conflicting %qs attributes",
1556 DECL_NAME (newdecl), mips_get_compress_on_name (diff));
1557
1558 diff = (mips_get_compress_off_flags (DECL_ATTRIBUTES (olddecl))
1559 ^ mips_get_compress_off_flags (DECL_ATTRIBUTES (newdecl)));
1560 if (diff)
1561 error ("%qE redeclared with conflicting %qs attributes",
1562 DECL_NAME (newdecl), mips_get_compress_off_name (diff));
1563
1564 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1565 DECL_ATTRIBUTES (newdecl));
1566 }
1567
1568 /* Implement TARGET_CAN_INLINE_P. */
1569
1570 static bool
1571 mips_can_inline_p (tree caller, tree callee)
1572 {
1573 if (mips_get_compress_mode (callee) != mips_get_compress_mode (caller))
1574 return false;
1575 return default_target_can_inline_p (caller, callee);
1576 }
1577
1578 /* Handle an "interrupt" attribute with an optional argument. */
1579
1580 static tree
1581 mips_handle_interrupt_attr (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
1582 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1583 {
1584 /* Check for an argument. */
1585 if (is_attribute_p ("interrupt", name) && args != NULL)
1586 {
1587 tree cst;
1588
1589 cst = TREE_VALUE (args);
1590 if (TREE_CODE (cst) != STRING_CST)
1591 {
1592 warning (OPT_Wattributes,
1593 "%qE attribute requires a string argument",
1594 name);
1595 *no_add_attrs = true;
1596 }
1597 else if (strcmp (TREE_STRING_POINTER (cst), "eic") != 0
1598 && !startswith (TREE_STRING_POINTER (cst), "vector="))
1599 {
1600 warning (OPT_Wattributes,
1601 "argument to %qE attribute is neither eic, nor "
1602 "%<vector=<line>%>", name);
1603 *no_add_attrs = true;
1604 }
1605 else if (startswith (TREE_STRING_POINTER (cst), "vector="))
1606 {
1607 const char *arg = TREE_STRING_POINTER (cst) + 7;
1608
1609 /* Acceptable names are: sw0,sw1,hw0,hw1,hw2,hw3,hw4,hw5. */
1610 if (strlen (arg) != 3
1611 || (arg[0] != 's' && arg[0] != 'h')
1612 || arg[1] != 'w'
1613 || (arg[0] == 's' && arg[2] != '0' && arg[2] != '1')
1614 || (arg[0] == 'h' && (arg[2] < '0' || arg[2] > '5')))
1615 {
1616 warning (OPT_Wattributes,
1617 "interrupt vector to %qE attribute is not "
1618 "%<vector=(sw0|sw1|hw0|hw1|hw2|hw3|hw4|hw5)%>",
1619 name);
1620 *no_add_attrs = true;
1621 }
1622 }
1623
1624 return NULL_TREE;
1625 }
1626
1627 return NULL_TREE;
1628 }
1629
1630 /* Handle a "use_shadow_register_set" attribute with an optional argument. */
1631
1632 static tree
1633 mips_handle_use_shadow_register_set_attr (tree *node ATTRIBUTE_UNUSED,
1634 tree name, tree args,
1635 int flags ATTRIBUTE_UNUSED,
1636 bool *no_add_attrs)
1637 {
1638 /* Check for an argument. */
1639 if (is_attribute_p ("use_shadow_register_set", name) && args != NULL)
1640 {
1641 tree cst;
1642
1643 cst = TREE_VALUE (args);
1644 if (TREE_CODE (cst) != STRING_CST)
1645 {
1646 warning (OPT_Wattributes,
1647 "%qE attribute requires a string argument",
1648 name);
1649 *no_add_attrs = true;
1650 }
1651 else if (strcmp (TREE_STRING_POINTER (cst), "intstack") != 0)
1652 {
1653 warning (OPT_Wattributes,
1654 "argument to %qE attribute is not intstack", name);
1655 *no_add_attrs = true;
1656 }
1657
1658 return NULL_TREE;
1659 }
1660
1661 return NULL_TREE;
1662 }
1663
1664 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1665 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
1666
1667 static void
1668 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1669 {
1670 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1671 {
1672 *base_ptr = XEXP (x, 0);
1673 *offset_ptr = INTVAL (XEXP (x, 1));
1674 }
1675 else
1676 {
1677 *base_ptr = x;
1678 *offset_ptr = 0;
1679 }
1680 }
1681
1682 static unsigned int mips_build_integer (struct mips_integer_op *,
1683 unsigned HOST_WIDE_INT);
1684
1685 /* A subroutine of mips_build_integer, with the same interface.
1686 Assume that the final action in the sequence should be a left shift. */
1687
1688 static unsigned int
1689 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1690 {
1691 unsigned int i, shift;
1692
1693 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1694 since signed numbers are easier to load than unsigned ones. */
1695 shift = 0;
1696 while ((value & 1) == 0)
1697 value /= 2, shift++;
1698
1699 i = mips_build_integer (codes, value);
1700 codes[i].code = ASHIFT;
1701 codes[i].value = shift;
1702 return i + 1;
1703 }
1704
1705 /* As for mips_build_shift, but assume that the final action will be
1706 an IOR or PLUS operation. */
1707
1708 static unsigned int
1709 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1710 {
1711 unsigned HOST_WIDE_INT high;
1712 unsigned int i;
1713
1714 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1715 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1716 {
1717 /* The constant is too complex to load with a simple LUI/ORI pair,
1718 so we want to give the recursive call as many trailing zeros as
1719 possible. In this case, we know bit 16 is set and that the
1720 low 16 bits form a negative number. If we subtract that number
1721 from VALUE, we will clear at least the lowest 17 bits, maybe more. */
1722 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1723 codes[i].code = PLUS;
1724 codes[i].value = CONST_LOW_PART (value);
1725 }
1726 else
1727 {
1728 /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1729 bits gives a value with at least 17 trailing zeros. */
1730 i = mips_build_integer (codes, high);
1731 codes[i].code = IOR;
1732 codes[i].value = value & 0xffff;
1733 }
1734 return i + 1;
1735 }
1736
1737 /* Fill CODES with a sequence of rtl operations to load VALUE.
1738 Return the number of operations needed. */
1739
1740 static unsigned int
1741 mips_build_integer (struct mips_integer_op *codes,
1742 unsigned HOST_WIDE_INT value)
1743 {
1744 if (SMALL_OPERAND (value)
1745 || SMALL_OPERAND_UNSIGNED (value)
1746 || LUI_OPERAND (value))
1747 {
1748 /* The value can be loaded with a single instruction. */
1749 codes[0].code = UNKNOWN;
1750 codes[0].value = value;
1751 return 1;
1752 }
1753 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1754 {
1755 /* Either the constant is a simple LUI/ORI combination or its
1756 lowest bit is set. We don't want to shift in this case. */
1757 return mips_build_lower (codes, value);
1758 }
1759 else if ((value & 0xffff) == 0)
1760 {
1761 /* The constant will need at least three actions. The lowest
1762 16 bits are clear, so the final action will be a shift. */
1763 return mips_build_shift (codes, value);
1764 }
1765 else
1766 {
1767 /* The final action could be a shift, add or inclusive OR.
1768 Rather than use a complex condition to select the best
1769 approach, try both mips_build_shift and mips_build_lower
1770 and pick the one that gives the shortest sequence.
1771 Note that this case is only used once per constant. */
1772 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1773 unsigned int cost, alt_cost;
1774
1775 cost = mips_build_shift (codes, value);
1776 alt_cost = mips_build_lower (alt_codes, value);
1777 if (alt_cost < cost)
1778 {
1779 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1780 cost = alt_cost;
1781 }
1782 return cost;
1783 }
1784 }
1785
1786 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
1787
1788 static bool
1789 mips_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1790 {
1791 return mips_const_insns (x) > 0;
1792 }
1793
1794 /* Return a SYMBOL_REF for a MIPS16 function called NAME. */
1795
1796 static rtx
1797 mips16_stub_function (const char *name)
1798 {
1799 rtx x;
1800
1801 x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1802 SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1803 return x;
1804 }
1805
1806 /* Return a legitimate call address for STUB, given that STUB is a MIPS16
1807 support function. */
1808
1809 static rtx
1810 mips16_stub_call_address (mips_one_only_stub *stub)
1811 {
1812 rtx fn = mips16_stub_function (stub->get_name ());
1813 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
1814 if (!call_insn_operand (fn, VOIDmode))
1815 fn = force_reg (Pmode, fn);
1816 return fn;
1817 }
1818
1819 /* A stub for moving the thread pointer into TLS_GET_TP_REGNUM. */
1820
1821 class mips16_rdhwr_one_only_stub : public mips_one_only_stub
1822 {
1823 virtual const char *get_name ();
1824 virtual void output_body ();
1825 };
1826
1827 const char *
1828 mips16_rdhwr_one_only_stub::get_name ()
1829 {
1830 return "__mips16_rdhwr";
1831 }
1832
1833 void
1834 mips16_rdhwr_one_only_stub::output_body ()
1835 {
1836 fprintf (asm_out_file,
1837 "\t.set\tpush\n"
1838 "\t.set\tmips32r2\n"
1839 "\t.set\tnoreorder\n"
1840 "\trdhwr\t$3,$29\n"
1841 "\t.set\tpop\n"
1842 "\tj\t$31\n");
1843 }
1844
1845 /* A stub for moving the FCSR into GET_FCSR_REGNUM. */
1846 class mips16_get_fcsr_one_only_stub : public mips_one_only_stub
1847 {
1848 virtual const char *get_name ();
1849 virtual void output_body ();
1850 };
1851
1852 const char *
1853 mips16_get_fcsr_one_only_stub::get_name ()
1854 {
1855 return "__mips16_get_fcsr";
1856 }
1857
1858 void
1859 mips16_get_fcsr_one_only_stub::output_body ()
1860 {
1861 fprintf (asm_out_file,
1862 "\tcfc1\t%s,$31\n"
1863 "\tj\t$31\n", reg_names[GET_FCSR_REGNUM]);
1864 }
1865
1866 /* A stub for moving SET_FCSR_REGNUM into the FCSR. */
1867 class mips16_set_fcsr_one_only_stub : public mips_one_only_stub
1868 {
1869 virtual const char *get_name ();
1870 virtual void output_body ();
1871 };
1872
1873 const char *
1874 mips16_set_fcsr_one_only_stub::get_name ()
1875 {
1876 return "__mips16_set_fcsr";
1877 }
1878
1879 void
1880 mips16_set_fcsr_one_only_stub::output_body ()
1881 {
1882 fprintf (asm_out_file,
1883 "\tctc1\t%s,$31\n"
1884 "\tj\t$31\n", reg_names[SET_FCSR_REGNUM]);
1885 }
1886
1887 /* Return true if symbols of type TYPE require a GOT access. */
1888
1889 static bool
1890 mips_got_symbol_type_p (enum mips_symbol_type type)
1891 {
1892 switch (type)
1893 {
1894 case SYMBOL_GOT_PAGE_OFST:
1895 case SYMBOL_GOT_DISP:
1896 return true;
1897
1898 default:
1899 return false;
1900 }
1901 }
1902
1903 /* Return true if X is a thread-local symbol. */
1904
1905 static bool
1906 mips_tls_symbol_p (rtx x)
1907 {
1908 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1909 }
1910
1911 /* Return true if SYMBOL_REF X is associated with a global symbol
1912 (in the STB_GLOBAL sense). */
1913
1914 static bool
1915 mips_global_symbol_p (const_rtx x)
1916 {
1917 const_tree decl = SYMBOL_REF_DECL (x);
1918
1919 if (!decl)
1920 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1921
1922 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1923 or weak symbols. Relocations in the object file will be against
1924 the target symbol, so it's that symbol's binding that matters here. */
1925 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1926 }
1927
1928 /* Return true if function X is a libgcc MIPS16 stub function. */
1929
1930 static bool
1931 mips16_stub_function_p (const_rtx x)
1932 {
1933 return (GET_CODE (x) == SYMBOL_REF
1934 && startswith (XSTR (x, 0), "__mips16_"));
1935 }
1936
1937 /* Return true if function X is a locally-defined and locally-binding
1938 MIPS16 function. */
1939
1940 static bool
1941 mips16_local_function_p (const_rtx x)
1942 {
1943 return (GET_CODE (x) == SYMBOL_REF
1944 && SYMBOL_REF_LOCAL_P (x)
1945 && !SYMBOL_REF_EXTERNAL_P (x)
1946 && (mips_get_compress_mode (SYMBOL_REF_DECL (x)) & MASK_MIPS16));
1947 }
1948
1949 /* Return true if SYMBOL_REF X binds locally. */
1950
1951 static bool
1952 mips_symbol_binds_local_p (const_rtx x)
1953 {
1954 return (SYMBOL_REF_DECL (x)
1955 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1956 : SYMBOL_REF_LOCAL_P (x));
1957 }
1958
1959 /* Return true if OP is a constant vector with the number of units in MODE,
1960 and each unit has the same bit set. */
1961
1962 bool
1963 mips_const_vector_bitimm_set_p (rtx op, machine_mode mode)
1964 {
1965 if (GET_CODE (op) == CONST_VECTOR && op != CONST0_RTX (mode))
1966 {
1967 unsigned HOST_WIDE_INT val = UINTVAL (CONST_VECTOR_ELT (op, 0));
1968 int vlog2 = exact_log2 (val & GET_MODE_MASK (GET_MODE_INNER (mode)));
1969
1970 if (vlog2 != -1)
1971 {
1972 gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
1973 gcc_assert (vlog2 >= 0 && vlog2 <= GET_MODE_UNIT_BITSIZE (mode) - 1);
1974 return mips_const_vector_same_val_p (op, mode);
1975 }
1976 }
1977
1978 return false;
1979 }
1980
1981 /* Return true if OP is a constant vector with the number of units in MODE,
1982 and each unit has the same bit clear. */
1983
1984 bool
1985 mips_const_vector_bitimm_clr_p (rtx op, machine_mode mode)
1986 {
1987 if (GET_CODE (op) == CONST_VECTOR && op != CONSTM1_RTX (mode))
1988 {
1989 unsigned HOST_WIDE_INT val = ~UINTVAL (CONST_VECTOR_ELT (op, 0));
1990 int vlog2 = exact_log2 (val & GET_MODE_MASK (GET_MODE_INNER (mode)));
1991
1992 if (vlog2 != -1)
1993 {
1994 gcc_assert (GET_MODE_CLASS (mode) == MODE_VECTOR_INT);
1995 gcc_assert (vlog2 >= 0 && vlog2 <= GET_MODE_UNIT_BITSIZE (mode) - 1);
1996 return mips_const_vector_same_val_p (op, mode);
1997 }
1998 }
1999
2000 return false;
2001 }
2002
2003 /* Return true if OP is a constant vector with the number of units in MODE,
2004 and each unit has the same value. */
2005
2006 bool
2007 mips_const_vector_same_val_p (rtx op, machine_mode mode)
2008 {
2009 int i, nunits = GET_MODE_NUNITS (mode);
2010 rtx first;
2011
2012 if (GET_CODE (op) != CONST_VECTOR || GET_MODE (op) != mode)
2013 return false;
2014
2015 first = CONST_VECTOR_ELT (op, 0);
2016 for (i = 1; i < nunits; i++)
2017 if (!rtx_equal_p (first, CONST_VECTOR_ELT (op, i)))
2018 return false;
2019
2020 return true;
2021 }
2022
2023 /* Return true if OP is a constant vector with the number of units in MODE,
2024 and each unit has the same value as well as replicated bytes in the value.
2025 */
2026
2027 bool
2028 mips_const_vector_same_bytes_p (rtx op, machine_mode mode)
2029 {
2030 int i, bytes;
2031 HOST_WIDE_INT val, first_byte;
2032 rtx first;
2033
2034 if (!mips_const_vector_same_val_p (op, mode))
2035 return false;
2036
2037 first = CONST_VECTOR_ELT (op, 0);
2038 bytes = GET_MODE_UNIT_SIZE (mode);
2039 val = INTVAL (first);
2040 first_byte = val & 0xff;
2041 for (i = 1; i < bytes; i++)
2042 {
2043 val >>= 8;
2044 if ((val & 0xff) != first_byte)
2045 return false;
2046 }
2047
2048 return true;
2049 }
2050
2051 /* Return true if OP is a constant vector with the number of units in MODE,
2052 and each unit has the same integer value in the range [LOW, HIGH]. */
2053
2054 bool
2055 mips_const_vector_same_int_p (rtx op, machine_mode mode, HOST_WIDE_INT low,
2056 HOST_WIDE_INT high)
2057 {
2058 HOST_WIDE_INT value;
2059 rtx elem0;
2060
2061 if (!mips_const_vector_same_val_p (op, mode))
2062 return false;
2063
2064 elem0 = CONST_VECTOR_ELT (op, 0);
2065 if (!CONST_INT_P (elem0))
2066 return false;
2067
2068 value = INTVAL (elem0);
2069 return (value >= low && value <= high);
2070 }
2071
2072 /* Return true if OP is a constant vector with repeated 4-element sets
2073 in mode MODE. */
2074
2075 bool
2076 mips_const_vector_shuffle_set_p (rtx op, machine_mode mode)
2077 {
2078 int nunits = GET_MODE_NUNITS (mode);
2079 int nsets = nunits / 4;
2080 int set = 0;
2081 int i, j;
2082 int val[4];
2083 bool ok;
2084
2085 /* We support swapping 2 Doubleword part with shf.w. */
2086 if (ISA_HAS_MSA && (mode == V2DFmode || mode == V2DImode))
2087 {
2088 if (!IN_RANGE (INTVAL (XVECEXP (op, 0, 0)), 0, 1)
2089 || !IN_RANGE (INTVAL (XVECEXP (op, 0, 1)), 0, 1))
2090 return false;
2091 }
2092
2093 if (ISA_HAS_MSA && mode == V16QImode)
2094 {
2095 /* We can use shf.w if the elements are in-order inner 32bit. */
2096 ok = true;
2097 for (j = 0; j < 4; j++)
2098 {
2099 val[0] = INTVAL (XVECEXP (op, 0, j * 4));
2100 val[1] = INTVAL (XVECEXP (op, 0, j * 4 + 1));
2101 val[2] = INTVAL (XVECEXP (op, 0, j * 4 + 2));
2102 val[3] = INTVAL (XVECEXP (op, 0, j * 4 + 3));
2103 if (val[0] != val[1] - 1
2104 || val[1] != val[2] - 1
2105 || val[2] != val[3] - 1)
2106 ok = false;
2107 if (val[0] != 0 && val[0] != 4 && val[0] != 8 && val[0] != 12)
2108 ok = false;
2109 }
2110 if (ok)
2111 return ok;
2112
2113 /* We can use shf.h if the elements are in order inner 16bit. */
2114 ok = true;
2115 for (j = 0; j < 4; j++)
2116 {
2117 val[0] = INTVAL (XVECEXP (op, 0, j * 2));
2118 val[1] = INTVAL (XVECEXP (op, 0, j * 2 + 1));
2119 val[2] = INTVAL (XVECEXP (op, 0, j * 2 + 8));
2120 val[3] = INTVAL (XVECEXP (op, 0, j * 2 + 1 + 8));
2121 if (val[0] != val[1] - 1 || val[2] != val[3] - 1)
2122 ok = false;
2123 if (val[0] != val[2] - 8 || val[1] != val[3] - 8)
2124 ok = false;
2125 if (val[0] != 0 && val[0] != 2 && val[0] != 4 && val[0] != 6)
2126 ok = false;
2127 }
2128 if (ok)
2129 return ok;
2130 }
2131
2132 if (ISA_HAS_MSA && mode == V8HImode)
2133 {
2134 /* We can use shf.w if the elements are in-order inner 32bit. */
2135 ok = true;
2136 for (j = 0; j < 4; j++)
2137 {
2138 val[0] = INTVAL (XVECEXP (op, 0, j * 2));
2139 val[1] = INTVAL (XVECEXP (op, 0, j * 2 + 1));
2140 if (val[0] != val[1] - 1)
2141 ok = false;
2142 if (val[0] != 0 && val[0] != 2 && val[0] != 4 && val[0] != 6)
2143 ok = false;
2144 }
2145 if (ok)
2146 return ok;
2147 }
2148
2149 /* Check if we have the same 4-element sets. */
2150 for (j = 0; j < nsets; j++, set = 4 * j)
2151 for (i = 0; i < 4; i++)
2152 if ((INTVAL (XVECEXP (op, 0, i))
2153 != (INTVAL (XVECEXP (op, 0, set + i)) - set))
2154 || !IN_RANGE (INTVAL (XVECEXP (op, 0, set + i)), 0, set + 3))
2155 return false;
2156 return true;
2157 }
2158
2159 /* Return true if rtx constants of mode MODE should be put into a small
2160 data section. */
2161
2162 static bool
2163 mips_rtx_constant_in_small_data_p (machine_mode mode)
2164 {
2165 return (!TARGET_EMBEDDED_DATA
2166 && TARGET_LOCAL_SDATA
2167 && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
2168 }
2169
2170 /* Return true if X should not be moved directly into register $25.
2171 We need this because many versions of GAS will treat "la $25,foo" as
2172 part of a call sequence and so allow a global "foo" to be lazily bound. */
2173
2174 bool
2175 mips_dangerous_for_la25_p (rtx x)
2176 {
2177 return (!TARGET_EXPLICIT_RELOCS
2178 && TARGET_USE_GOT
2179 && GET_CODE (x) == SYMBOL_REF
2180 && mips_global_symbol_p (x));
2181 }
2182
2183 /* Return true if calls to X might need $25 to be valid on entry. */
2184
2185 bool
2186 mips_use_pic_fn_addr_reg_p (const_rtx x)
2187 {
2188 if (!TARGET_USE_PIC_FN_ADDR_REG)
2189 return false;
2190
2191 /* MIPS16 stub functions are guaranteed not to use $25. */
2192 if (mips16_stub_function_p (x))
2193 return false;
2194
2195 if (GET_CODE (x) == SYMBOL_REF)
2196 {
2197 /* If PLTs and copy relocations are available, the static linker
2198 will make sure that $25 is valid on entry to the target function. */
2199 if (TARGET_ABICALLS_PIC0)
2200 return false;
2201
2202 /* Locally-defined functions use absolute accesses to set up
2203 the global pointer. */
2204 if (TARGET_ABSOLUTE_ABICALLS
2205 && mips_symbol_binds_local_p (x)
2206 && !SYMBOL_REF_EXTERNAL_P (x))
2207 return false;
2208 }
2209
2210 return true;
2211 }
2212
2213 /* Return the method that should be used to access SYMBOL_REF or
2214 LABEL_REF X in context CONTEXT. */
2215
2216 static enum mips_symbol_type
2217 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
2218 {
2219 if (TARGET_RTP_PIC)
2220 return SYMBOL_GOT_DISP;
2221
2222 if (GET_CODE (x) == LABEL_REF)
2223 {
2224 /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
2225 code and if we know that the label is in the current function's
2226 text section. LABEL_REFs are used for jump tables as well as
2227 text labels, so we must check whether jump tables live in the
2228 text section. */
2229 if (TARGET_MIPS16_SHORT_JUMP_TABLES
2230 && !LABEL_REF_NONLOCAL_P (x))
2231 return SYMBOL_PC_RELATIVE;
2232
2233 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
2234 return SYMBOL_GOT_PAGE_OFST;
2235
2236 return SYMBOL_ABSOLUTE;
2237 }
2238
2239 gcc_assert (GET_CODE (x) == SYMBOL_REF);
2240
2241 if (SYMBOL_REF_TLS_MODEL (x))
2242 return SYMBOL_TLS;
2243
2244 if (CONSTANT_POOL_ADDRESS_P (x))
2245 {
2246 if (TARGET_MIPS16_TEXT_LOADS)
2247 return SYMBOL_PC_RELATIVE;
2248
2249 if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
2250 return SYMBOL_PC_RELATIVE;
2251
2252 if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
2253 return SYMBOL_GP_RELATIVE;
2254 }
2255
2256 /* Do not use small-data accesses for weak symbols; they may end up
2257 being zero. */
2258 if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
2259 return SYMBOL_GP_RELATIVE;
2260
2261 /* Don't use GOT accesses for locally-binding symbols when -mno-shared
2262 is in effect. */
2263 if (TARGET_ABICALLS_PIC2
2264 && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
2265 {
2266 /* There are three cases to consider:
2267
2268 - o32 PIC (either with or without explicit relocs)
2269 - n32/n64 PIC without explicit relocs
2270 - n32/n64 PIC with explicit relocs
2271
2272 In the first case, both local and global accesses will use an
2273 R_MIPS_GOT16 relocation. We must correctly predict which of
2274 the two semantics (local or global) the assembler and linker
2275 will apply. The choice depends on the symbol's binding rather
2276 than its visibility.
2277
2278 In the second case, the assembler will not use R_MIPS_GOT16
2279 relocations, but it chooses between local and global accesses
2280 in the same way as for o32 PIC.
2281
2282 In the third case we have more freedom since both forms of
2283 access will work for any kind of symbol. However, there seems
2284 little point in doing things differently. */
2285 if (mips_global_symbol_p (x))
2286 return SYMBOL_GOT_DISP;
2287
2288 return SYMBOL_GOT_PAGE_OFST;
2289 }
2290
2291 return SYMBOL_ABSOLUTE;
2292 }
2293
2294 /* Classify the base of symbolic expression X, given that X appears in
2295 context CONTEXT. */
2296
2297 static enum mips_symbol_type
2298 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
2299 {
2300 rtx offset;
2301
2302 split_const (x, &x, &offset);
2303 if (UNSPEC_ADDRESS_P (x))
2304 return UNSPEC_ADDRESS_TYPE (x);
2305
2306 return mips_classify_symbol (x, context);
2307 }
2308
2309 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
2310 is the alignment in bytes of SYMBOL_REF X. */
2311
2312 static bool
2313 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
2314 {
2315 HOST_WIDE_INT align;
2316
2317 align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
2318 return IN_RANGE (offset, 0, align - 1);
2319 }
2320
2321 /* Return true if X is a symbolic constant that can be used in context
2322 CONTEXT. If it is, store the type of the symbol in *SYMBOL_TYPE. */
2323
2324 bool
2325 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
2326 enum mips_symbol_type *symbol_type)
2327 {
2328 rtx offset;
2329
2330 split_const (x, &x, &offset);
2331 if (UNSPEC_ADDRESS_P (x))
2332 {
2333 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
2334 x = UNSPEC_ADDRESS (x);
2335 }
2336 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
2337 {
2338 *symbol_type = mips_classify_symbol (x, context);
2339 if (*symbol_type == SYMBOL_TLS)
2340 return false;
2341 }
2342 else
2343 return false;
2344
2345 if (offset == const0_rtx)
2346 return true;
2347
2348 /* Check whether a nonzero offset is valid for the underlying
2349 relocations. */
2350 switch (*symbol_type)
2351 {
2352 case SYMBOL_ABSOLUTE:
2353 case SYMBOL_64_HIGH:
2354 case SYMBOL_64_MID:
2355 case SYMBOL_64_LOW:
2356 /* If the target has 64-bit pointers and the object file only
2357 supports 32-bit symbols, the values of those symbols will be
2358 sign-extended. In this case we can't allow an arbitrary offset
2359 in case the 32-bit value X + OFFSET has a different sign from X. */
2360 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
2361 return offset_within_block_p (x, INTVAL (offset));
2362
2363 /* In other cases the relocations can handle any offset. */
2364 return true;
2365
2366 case SYMBOL_PC_RELATIVE:
2367 /* Allow constant pool references to be converted to LABEL+CONSTANT.
2368 In this case, we no longer have access to the underlying constant,
2369 but the original symbol-based access was known to be valid. */
2370 if (GET_CODE (x) == LABEL_REF)
2371 return true;
2372
2373 /* Fall through. */
2374
2375 case SYMBOL_GP_RELATIVE:
2376 /* Make sure that the offset refers to something within the
2377 same object block. This should guarantee that the final
2378 PC- or GP-relative offset is within the 16-bit limit. */
2379 return offset_within_block_p (x, INTVAL (offset));
2380
2381 case SYMBOL_GOT_PAGE_OFST:
2382 case SYMBOL_GOTOFF_PAGE:
2383 /* If the symbol is global, the GOT entry will contain the symbol's
2384 address, and we will apply a 16-bit offset after loading it.
2385 If the symbol is local, the linker should provide enough local
2386 GOT entries for a 16-bit offset, but larger offsets may lead
2387 to GOT overflow. */
2388 return SMALL_INT (offset);
2389
2390 case SYMBOL_TPREL:
2391 case SYMBOL_DTPREL:
2392 /* There is no carry between the HI and LO REL relocations, so the
2393 offset is only valid if we know it won't lead to such a carry. */
2394 return mips_offset_within_alignment_p (x, INTVAL (offset));
2395
2396 case SYMBOL_GOT_DISP:
2397 case SYMBOL_GOTOFF_DISP:
2398 case SYMBOL_GOTOFF_CALL:
2399 case SYMBOL_GOTOFF_LOADGP:
2400 case SYMBOL_TLSGD:
2401 case SYMBOL_TLSLDM:
2402 case SYMBOL_GOTTPREL:
2403 case SYMBOL_TLS:
2404 case SYMBOL_HALF:
2405 return false;
2406 }
2407 gcc_unreachable ();
2408 }
2409
2410 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
2411 single instruction. We rely on the fact that, in the worst case,
2412 all instructions involved in a MIPS16 address calculation are usually
2413 extended ones. */
2414
2415 static int
2416 mips_symbol_insns_1 (enum mips_symbol_type type, machine_mode mode)
2417 {
2418 if (mips_use_pcrel_pool_p[(int) type])
2419 {
2420 if (mode == MAX_MACHINE_MODE)
2421 /* LEAs will be converted into constant-pool references by
2422 mips_reorg. */
2423 type = SYMBOL_PC_RELATIVE;
2424 else
2425 /* The constant must be loaded and then dereferenced. */
2426 return 0;
2427 }
2428
2429 switch (type)
2430 {
2431 case SYMBOL_ABSOLUTE:
2432 /* When using 64-bit symbols, we need 5 preparatory instructions,
2433 such as:
2434
2435 lui $at,%highest(symbol)
2436 daddiu $at,$at,%higher(symbol)
2437 dsll $at,$at,16
2438 daddiu $at,$at,%hi(symbol)
2439 dsll $at,$at,16
2440
2441 The final address is then $at + %lo(symbol). With 32-bit
2442 symbols we just need a preparatory LUI for normal mode and
2443 a preparatory LI and SLL for MIPS16. */
2444 return ABI_HAS_64BIT_SYMBOLS
2445 ? 6
2446 : (TARGET_MIPS16 && !ISA_HAS_MIPS16E2) ? 3 : 2;
2447
2448 case SYMBOL_GP_RELATIVE:
2449 /* Treat GP-relative accesses as taking a single instruction on
2450 MIPS16 too; the copy of $gp can often be shared. */
2451 return 1;
2452
2453 case SYMBOL_PC_RELATIVE:
2454 /* PC-relative constants can be only be used with ADDIUPC,
2455 DADDIUPC, LWPC and LDPC. */
2456 if (mode == MAX_MACHINE_MODE
2457 || GET_MODE_SIZE (mode) == 4
2458 || GET_MODE_SIZE (mode) == 8)
2459 return 1;
2460
2461 /* The constant must be loaded using ADDIUPC or DADDIUPC first. */
2462 return 0;
2463
2464 case SYMBOL_GOT_DISP:
2465 /* The constant will have to be loaded from the GOT before it
2466 is used in an address. */
2467 if (mode != MAX_MACHINE_MODE)
2468 return 0;
2469
2470 /* Fall through. */
2471
2472 case SYMBOL_GOT_PAGE_OFST:
2473 /* Unless -funit-at-a-time is in effect, we can't be sure whether the
2474 local/global classification is accurate. The worst cases are:
2475
2476 (1) For local symbols when generating o32 or o64 code. The assembler
2477 will use:
2478
2479 lw $at,%got(symbol)
2480 nop
2481
2482 ...and the final address will be $at + %lo(symbol).
2483
2484 (2) For global symbols when -mxgot. The assembler will use:
2485
2486 lui $at,%got_hi(symbol)
2487 (d)addu $at,$at,$gp
2488
2489 ...and the final address will be $at + %got_lo(symbol). */
2490 return 3;
2491
2492 case SYMBOL_GOTOFF_PAGE:
2493 case SYMBOL_GOTOFF_DISP:
2494 case SYMBOL_GOTOFF_CALL:
2495 case SYMBOL_GOTOFF_LOADGP:
2496 case SYMBOL_64_HIGH:
2497 case SYMBOL_64_MID:
2498 case SYMBOL_64_LOW:
2499 case SYMBOL_TLSGD:
2500 case SYMBOL_TLSLDM:
2501 case SYMBOL_DTPREL:
2502 case SYMBOL_GOTTPREL:
2503 case SYMBOL_TPREL:
2504 case SYMBOL_HALF:
2505 /* A 16-bit constant formed by a single relocation, or a 32-bit
2506 constant formed from a high 16-bit relocation and a low 16-bit
2507 relocation. Use mips_split_p to determine which. 32-bit
2508 constants need an "lui; addiu" sequence for normal mode and
2509 an "li; sll; addiu" sequence for MIPS16 mode. */
2510 return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
2511
2512 case SYMBOL_TLS:
2513 /* We don't treat a bare TLS symbol as a constant. */
2514 return 0;
2515 }
2516 gcc_unreachable ();
2517 }
2518
2519 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
2520 to load symbols of type TYPE into a register. Return 0 if the given
2521 type of symbol cannot be used as an immediate operand.
2522
2523 Otherwise, return the number of instructions needed to load or store
2524 values of mode MODE to or from addresses of type TYPE. Return 0 if
2525 the given type of symbol is not valid in addresses.
2526
2527 In both cases, instruction counts are based off BASE_INSN_LENGTH. */
2528
2529 static int
2530 mips_symbol_insns (enum mips_symbol_type type, machine_mode mode)
2531 {
2532 /* MSA LD.* and ST.* cannot support loading symbols via an immediate
2533 operand. */
2534 if (mode != MAX_MACHINE_MODE && MSA_SUPPORTED_MODE_P (mode))
2535 return 0;
2536
2537 return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
2538 }
2539
2540 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2541
2542 static bool
2543 mips_cannot_force_const_mem (machine_mode mode, rtx x)
2544 {
2545 enum mips_symbol_type type;
2546 rtx base, offset;
2547
2548 /* There is no assembler syntax for expressing an address-sized
2549 high part. */
2550 if (GET_CODE (x) == HIGH)
2551 return true;
2552
2553 /* As an optimization, reject constants that mips_legitimize_move
2554 can expand inline.
2555
2556 Suppose we have a multi-instruction sequence that loads constant C
2557 into register R. If R does not get allocated a hard register, and
2558 R is used in an operand that allows both registers and memory
2559 references, reload will consider forcing C into memory and using
2560 one of the instruction's memory alternatives. Returning false
2561 here will force it to use an input reload instead. */
2562 if ((CONST_INT_P (x) || GET_CODE (x) == CONST_VECTOR)
2563 && mips_legitimate_constant_p (mode, x))
2564 return true;
2565
2566 split_const (x, &base, &offset);
2567 if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
2568 {
2569 /* See whether we explicitly want these symbols in the pool. */
2570 if (mips_use_pcrel_pool_p[(int) type])
2571 return false;
2572
2573 /* The same optimization as for CONST_INT. */
2574 if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2575 return true;
2576
2577 /* If MIPS16 constant pools live in the text section, they should
2578 not refer to anything that might need run-time relocation. */
2579 if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2580 return true;
2581 }
2582
2583 /* TLS symbols must be computed by mips_legitimize_move. */
2584 if (tls_referenced_p (x))
2585 return true;
2586
2587 return false;
2588 }
2589
2590 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. We can't use blocks for
2591 constants when we're using a per-function constant pool. */
2592
2593 static bool
2594 mips_use_blocks_for_constant_p (machine_mode mode ATTRIBUTE_UNUSED,
2595 const_rtx x ATTRIBUTE_UNUSED)
2596 {
2597 return !TARGET_MIPS16_PCREL_LOADS;
2598 }
2599
2600 /* Return true if register REGNO is a valid base register for mode MODE.
2601 STRICT_P is true if REG_OK_STRICT is in effect. */
2602
2603 int
2604 mips_regno_mode_ok_for_base_p (int regno, machine_mode mode,
2605 bool strict_p)
2606 {
2607 if (!HARD_REGISTER_NUM_P (regno))
2608 {
2609 if (!strict_p)
2610 return true;
2611 regno = reg_renumber[regno];
2612 }
2613
2614 /* These fake registers will be eliminated to either the stack or
2615 hard frame pointer, both of which are usually valid base registers.
2616 Reload deals with the cases where the eliminated form isn't valid. */
2617 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2618 return true;
2619
2620 /* In MIPS16 mode, the stack pointer can only address word and doubleword
2621 values, nothing smaller. */
2622 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2623 return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2624
2625 if (MIPS16_GP_LOADS && regno == GLOBAL_POINTER_REGNUM)
2626 return (UNITS_PER_WORD > 4 ? GET_MODE_SIZE (mode) <= 4 : true);
2627
2628 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2629 }
2630
2631 /* Return true if X is a valid base register for mode MODE.
2632 STRICT_P is true if REG_OK_STRICT is in effect. */
2633
2634 static bool
2635 mips_valid_base_register_p (rtx x, machine_mode mode, bool strict_p)
2636 {
2637 if (!strict_p && GET_CODE (x) == SUBREG)
2638 x = SUBREG_REG (x);
2639
2640 return (REG_P (x)
2641 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2642 }
2643
2644 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2645 can address a value of mode MODE. */
2646
2647 static bool
2648 mips_valid_offset_p (rtx x, machine_mode mode)
2649 {
2650 /* Check that X is a signed 16-bit number. */
2651 if (!const_arith_operand (x, Pmode))
2652 return false;
2653
2654 /* We may need to split multiword moves, so make sure that every word
2655 is accessible. */
2656 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2657 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2658 return false;
2659
2660 /* MSA LD.* and ST.* supports 10-bit signed offsets. */
2661 if (MSA_SUPPORTED_MODE_P (mode)
2662 && !mips_signed_immediate_p (INTVAL (x), 10,
2663 mips_ldst_scaled_shift (mode)))
2664 return false;
2665
2666 return true;
2667 }
2668
2669 /* Return true if a LO_SUM can address a value of mode MODE when the
2670 LO_SUM symbol has type SYMBOL_TYPE. */
2671
2672 static bool
2673 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, machine_mode mode)
2674 {
2675 /* Check that symbols of type SYMBOL_TYPE can be used to access values
2676 of mode MODE. */
2677 if (mips_symbol_insns (symbol_type, mode) == 0)
2678 return false;
2679
2680 /* Check that there is a known low-part relocation. */
2681 if (mips_lo_relocs[symbol_type] == NULL)
2682 return false;
2683
2684 /* We may need to split multiword moves, so make sure that each word
2685 can be accessed without inducing a carry. This is mainly needed
2686 for o64, which has historically only guaranteed 64-bit alignment
2687 for 128-bit types. */
2688 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2689 && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2690 return false;
2691
2692 /* MSA LD.* and ST.* cannot support loading symbols via %lo($base). */
2693 if (MSA_SUPPORTED_MODE_P (mode))
2694 return false;
2695
2696 return true;
2697 }
2698
2699 /* Return true if X is a valid address for machine mode MODE. If it is,
2700 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
2701 effect. */
2702
2703 static bool
2704 mips_classify_address (struct mips_address_info *info, rtx x,
2705 machine_mode mode, bool strict_p)
2706 {
2707 switch (GET_CODE (x))
2708 {
2709 case REG:
2710 case SUBREG:
2711 info->type = ADDRESS_REG;
2712 info->reg = x;
2713 info->offset = const0_rtx;
2714 return mips_valid_base_register_p (info->reg, mode, strict_p);
2715
2716 case PLUS:
2717 info->type = ADDRESS_REG;
2718 info->reg = XEXP (x, 0);
2719 info->offset = XEXP (x, 1);
2720 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2721 && mips_valid_offset_p (info->offset, mode));
2722
2723 case LO_SUM:
2724 info->type = ADDRESS_LO_SUM;
2725 info->reg = XEXP (x, 0);
2726 info->offset = XEXP (x, 1);
2727 /* We have to trust the creator of the LO_SUM to do something vaguely
2728 sane. Target-independent code that creates a LO_SUM should also
2729 create and verify the matching HIGH. Target-independent code that
2730 adds an offset to a LO_SUM must prove that the offset will not
2731 induce a carry. Failure to do either of these things would be
2732 a bug, and we are not required to check for it here. The MIPS
2733 backend itself should only create LO_SUMs for valid symbolic
2734 constants, with the high part being either a HIGH or a copy
2735 of _gp. */
2736 info->symbol_type
2737 = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2738 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2739 && mips_valid_lo_sum_p (info->symbol_type, mode));
2740
2741 case CONST_INT:
2742 /* Small-integer addresses don't occur very often, but they
2743 are legitimate if $0 is a valid base register. */
2744 info->type = ADDRESS_CONST_INT;
2745 return !TARGET_MIPS16 && SMALL_INT (x);
2746
2747 case CONST:
2748 case LABEL_REF:
2749 case SYMBOL_REF:
2750 info->type = ADDRESS_SYMBOLIC;
2751 return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2752 &info->symbol_type)
2753 && mips_symbol_insns (info->symbol_type, mode) > 0
2754 && !mips_split_p[info->symbol_type]);
2755
2756 default:
2757 return false;
2758 }
2759 }
2760
2761 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2762
2763 static bool
2764 mips_legitimate_address_p (machine_mode mode, rtx x, bool strict_p,
2765 code_helper = ERROR_MARK)
2766 {
2767 struct mips_address_info addr;
2768
2769 return mips_classify_address (&addr, x, mode, strict_p);
2770 }
2771
2772 /* Return true if X is a legitimate $sp-based address for mode MODE. */
2773
2774 bool
2775 mips_stack_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 && addr.reg == stack_pointer_rtx);
2782 }
2783
2784 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2785 address instruction. Note that such addresses are not considered
2786 legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2787 is so restricted. */
2788
2789 static bool
2790 mips_lwxs_address_p (rtx addr)
2791 {
2792 if (ISA_HAS_LWXS
2793 && GET_CODE (addr) == PLUS
2794 && REG_P (XEXP (addr, 1)))
2795 {
2796 rtx offset = XEXP (addr, 0);
2797 if (GET_CODE (offset) == MULT
2798 && REG_P (XEXP (offset, 0))
2799 && CONST_INT_P (XEXP (offset, 1))
2800 && INTVAL (XEXP (offset, 1)) == 4)
2801 return true;
2802 }
2803 return false;
2804 }
2805
2806 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load
2807 indexed address instruction. Note that such addresses are
2808 not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2809 sense, because their use is so restricted. */
2810
2811 static bool
2812 mips_lx_address_p (rtx addr, machine_mode mode)
2813 {
2814 if (GET_CODE (addr) != PLUS
2815 || !REG_P (XEXP (addr, 0))
2816 || !REG_P (XEXP (addr, 1)))
2817 return false;
2818 if (ISA_HAS_LBX && mode == QImode)
2819 return true;
2820 if (ISA_HAS_LHX && mode == HImode)
2821 return true;
2822 if (ISA_HAS_LWX && mode == SImode)
2823 return true;
2824 if (ISA_HAS_LDX && mode == DImode)
2825 return true;
2826 if (MSA_SUPPORTED_MODE_P (mode))
2827 return true;
2828 return false;
2829 }
2830
2831 /* Return true if a value at OFFSET bytes from base register BASE can be
2832 accessed using an unextended MIPS16 instruction. MODE is the mode of
2833 the value.
2834
2835 Usually the offset in an unextended instruction is a 5-bit field.
2836 The offset is unsigned and shifted left once for LH and SH, twice
2837 for LW and SW, and so on. An exception is LWSP and SWSP, which have
2838 an 8-bit immediate field that's shifted left twice. */
2839
2840 static bool
2841 mips16_unextended_reference_p (machine_mode mode, rtx base,
2842 unsigned HOST_WIDE_INT offset)
2843 {
2844 if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0
2845 && REGNO (base) != GLOBAL_POINTER_REGNUM)
2846 {
2847 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2848 return offset < 256U * GET_MODE_SIZE (mode);
2849 return offset < 32U * GET_MODE_SIZE (mode);
2850 }
2851 return false;
2852 }
2853
2854 /* Return the number of instructions needed to load or store a value
2855 of mode MODE at address X, assuming that BASE_INSN_LENGTH is the
2856 length of one instruction. Return 0 if X isn't valid for MODE.
2857 Assume that multiword moves may need to be split into word moves
2858 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2859 enough. */
2860
2861 int
2862 mips_address_insns (rtx x, machine_mode mode, bool might_split_p)
2863 {
2864 struct mips_address_info addr;
2865 int factor;
2866 bool msa_p = (!might_split_p && MSA_SUPPORTED_MODE_P (mode));
2867
2868 /* BLKmode is used for single unaligned loads and stores and should
2869 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
2870 meaningless, so we have to single it out as a special case one way
2871 or the other.) */
2872 if (mode != BLKmode && might_split_p)
2873 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2874 else
2875 factor = 1;
2876
2877 if (mips_classify_address (&addr, x, mode, false))
2878 switch (addr.type)
2879 {
2880 case ADDRESS_REG:
2881 if (msa_p)
2882 {
2883 /* MSA LD.* and ST.* supports 10-bit signed offsets. */
2884 if (mips_signed_immediate_p (INTVAL (addr.offset), 10,
2885 mips_ldst_scaled_shift (mode)))
2886 return 1;
2887 else
2888 return 0;
2889 }
2890 if (TARGET_MIPS16
2891 && !mips16_unextended_reference_p (mode, addr.reg,
2892 UINTVAL (addr.offset)))
2893 return factor * 2;
2894 return factor;
2895
2896 case ADDRESS_LO_SUM:
2897 return msa_p ? 0 : TARGET_MIPS16 ? factor * 2 : factor;
2898
2899 case ADDRESS_CONST_INT:
2900 return msa_p ? 0 : factor;
2901
2902 case ADDRESS_SYMBOLIC:
2903 return msa_p ? 0 : factor * mips_symbol_insns (addr.symbol_type, mode);
2904 }
2905 return 0;
2906 }
2907
2908 /* Return true if X fits within an unsigned field of BITS bits that is
2909 shifted left SHIFT bits before being used. */
2910
2911 bool
2912 mips_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2913 {
2914 return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2915 }
2916
2917 /* Return true if X fits within a signed field of BITS bits that is
2918 shifted left SHIFT bits before being used. */
2919
2920 bool
2921 mips_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2922 {
2923 x += 1 << (bits + shift - 1);
2924 return mips_unsigned_immediate_p (x, bits, shift);
2925 }
2926
2927 /* Return the scale shift that applied to MSA LD/ST address offset. */
2928
2929 int
2930 mips_ldst_scaled_shift (machine_mode mode)
2931 {
2932 int shift = exact_log2 (GET_MODE_UNIT_SIZE (mode));
2933
2934 if (shift < 0 || shift > 8)
2935 gcc_unreachable ();
2936
2937 return shift;
2938 }
2939
2940 /* Return true if X is legitimate for accessing values of mode MODE,
2941 if it is based on a MIPS16 register, and if the offset satisfies
2942 OFFSET_PREDICATE. */
2943
2944 bool
2945 m16_based_address_p (rtx x, machine_mode mode,
2946 insn_operand_predicate_fn offset_predicate)
2947 {
2948 struct mips_address_info addr;
2949
2950 return (mips_classify_address (&addr, x, mode, false)
2951 && addr.type == ADDRESS_REG
2952 && M16_REG_P (REGNO (addr.reg))
2953 && offset_predicate (addr.offset, mode));
2954 }
2955
2956 /* Return true if X is a legitimate address that conforms to the requirements
2957 for a microMIPS LWSP or SWSP insn. */
2958
2959 bool
2960 lwsp_swsp_address_p (rtx x, machine_mode mode)
2961 {
2962 struct mips_address_info addr;
2963
2964 return (mips_classify_address (&addr, x, mode, false)
2965 && addr.type == ADDRESS_REG
2966 && REGNO (addr.reg) == STACK_POINTER_REGNUM
2967 && uw5_operand (addr.offset, mode));
2968 }
2969
2970 /* Return true if X is a legitimate address with a 12-bit offset.
2971 MODE is the mode of the value being accessed. */
2972
2973 bool
2974 umips_12bit_offset_address_p (rtx x, machine_mode mode)
2975 {
2976 struct mips_address_info addr;
2977
2978 return (mips_classify_address (&addr, x, mode, false)
2979 && addr.type == ADDRESS_REG
2980 && CONST_INT_P (addr.offset)
2981 && UMIPS_12BIT_OFFSET_P (INTVAL (addr.offset)));
2982 }
2983
2984 /* Return true if X is a legitimate address with a 9-bit offset.
2985 MODE is the mode of the value being accessed. */
2986
2987 bool
2988 mips_9bit_offset_address_p (rtx x, machine_mode mode)
2989 {
2990 struct mips_address_info addr;
2991
2992 return (mips_classify_address (&addr, x, mode, false)
2993 && addr.type == ADDRESS_REG
2994 && CONST_INT_P (addr.offset)
2995 && (!TARGET_MIPS16E2
2996 || M16_REG_P (REGNO (addr.reg))
2997 || REGNO (addr.reg) >= FIRST_PSEUDO_REGISTER)
2998 && MIPS_9BIT_OFFSET_P (INTVAL (addr.offset)));
2999 }
3000
3001 /* Return the number of instructions needed to load constant X,
3002 assuming that BASE_INSN_LENGTH is the length of one instruction.
3003 Return 0 if X isn't a valid constant. */
3004
3005 int
3006 mips_const_insns (rtx x)
3007 {
3008 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3009 enum mips_symbol_type symbol_type;
3010 rtx offset;
3011
3012 switch (GET_CODE (x))
3013 {
3014 case HIGH:
3015 if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
3016 &symbol_type)
3017 || !mips_split_p[symbol_type])
3018 return 0;
3019
3020 /* This is simply an LUI for normal mode. It is an extended
3021 LI followed by an extended SLL for MIPS16. */
3022 return TARGET_MIPS16 ? (ISA_HAS_MIPS16E2 ? 2 : 4) : 1;
3023
3024 case CONST_INT:
3025 if (TARGET_MIPS16)
3026 /* Unsigned 8-bit constants can be loaded using an unextended
3027 LI instruction. Unsigned 16-bit constants can be loaded
3028 using an extended LI. Negative constants must be loaded
3029 using LI and then negated. */
3030 return (IN_RANGE (INTVAL (x), 0, 255) ? 1
3031 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
3032 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
3033 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
3034 : ISA_HAS_MIPS16E2
3035 ? (trunc_int_for_mode (INTVAL (x), SImode) == INTVAL (x)
3036 ? 4 : 8)
3037 : 0);
3038
3039 return mips_build_integer (codes, INTVAL (x));
3040
3041 case CONST_VECTOR:
3042 if (MSA_SUPPORTED_MODE_P (GET_MODE (x))
3043 && mips_const_vector_same_int_p (x, GET_MODE (x), -512, 511))
3044 return 1;
3045 /* Fall through. */
3046 case CONST_DOUBLE:
3047 /* Allow zeros for normal mode, where we can use $0. */
3048 return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
3049
3050 case CONST:
3051 if (CONST_GP_P (x))
3052 return 1;
3053
3054 /* See if we can refer to X directly. */
3055 if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
3056 return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
3057
3058 /* Otherwise try splitting the constant into a base and offset.
3059 If the offset is a 16-bit value, we can load the base address
3060 into a register and then use (D)ADDIU to add in the offset.
3061 If the offset is larger, we can load the base and offset
3062 into separate registers and add them together with (D)ADDU.
3063 However, the latter is only possible before reload; during
3064 and after reload, we must have the option of forcing the
3065 constant into the pool instead. */
3066 split_const (x, &x, &offset);
3067 if (offset != 0)
3068 {
3069 int n = mips_const_insns (x);
3070 if (n != 0)
3071 {
3072 if (SMALL_INT (offset))
3073 return n + 1;
3074 else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
3075 return n + 1 + mips_build_integer (codes, INTVAL (offset));
3076 }
3077 }
3078 return 0;
3079
3080 case SYMBOL_REF:
3081 case LABEL_REF:
3082 return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
3083 MAX_MACHINE_MODE);
3084
3085 default:
3086 return 0;
3087 }
3088 }
3089
3090 /* X is a doubleword constant that can be handled by splitting it into
3091 two words and loading each word separately. Return the number of
3092 instructions required to do this, assuming that BASE_INSN_LENGTH
3093 is the length of one instruction. */
3094
3095 int
3096 mips_split_const_insns (rtx x)
3097 {
3098 unsigned int low, high;
3099
3100 low = mips_const_insns (mips_subword (x, false));
3101 high = mips_const_insns (mips_subword (x, true));
3102 gcc_assert (low > 0 && high > 0);
3103 return low + high;
3104 }
3105
3106 /* Return one word of 128-bit value OP, taking into account the fixed
3107 endianness of certain registers. BYTE selects from the byte address. */
3108
3109 rtx
3110 mips_subword_at_byte (rtx op, unsigned int byte)
3111 {
3112 machine_mode mode;
3113
3114 mode = GET_MODE (op);
3115 if (mode == VOIDmode)
3116 mode = TImode;
3117
3118 gcc_assert (!FP_REG_RTX_P (op));
3119
3120 if (MEM_P (op))
3121 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
3122
3123 return simplify_gen_subreg (word_mode, op, mode, byte);
3124 }
3125
3126 /* Return the number of instructions needed to implement INSN,
3127 given that it loads from or stores to MEM. Assume that
3128 BASE_INSN_LENGTH is the length of one instruction. */
3129
3130 int
3131 mips_load_store_insns (rtx mem, rtx_insn *insn)
3132 {
3133 machine_mode mode;
3134 bool might_split_p;
3135 rtx set;
3136
3137 gcc_assert (MEM_P (mem));
3138 mode = GET_MODE (mem);
3139
3140 /* Try to prove that INSN does not need to be split. */
3141 might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
3142 if (might_split_p)
3143 {
3144 set = single_set (insn);
3145 if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
3146 might_split_p = false;
3147 }
3148
3149 return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
3150 }
3151
3152 /* Return the number of instructions needed for an integer division,
3153 assuming that BASE_INSN_LENGTH is the length of one instruction. */
3154
3155 int
3156 mips_idiv_insns (machine_mode mode)
3157 {
3158 int count;
3159
3160 count = 1;
3161 if (TARGET_CHECK_ZERO_DIV)
3162 {
3163 if (GENERATE_DIVIDE_TRAPS && !MSA_SUPPORTED_MODE_P (mode))
3164 count++;
3165 else
3166 count += 2;
3167 }
3168
3169 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
3170 count++;
3171 return count;
3172 }
3173
3174
3175 /* Emit a move from SRC to DEST. Assume that the move expanders can
3176 handle all moves if !can_create_pseudo_p (). The distinction is
3177 important because, unlike emit_move_insn, the move expanders know
3178 how to force Pmode objects into the constant pool even when the
3179 constant pool address is not itself legitimate. */
3180
3181 rtx_insn *
3182 mips_emit_move (rtx dest, rtx src)
3183 {
3184 return (can_create_pseudo_p ()
3185 ? emit_move_insn (dest, src)
3186 : emit_move_insn_1 (dest, src));
3187 }
3188
3189 /* Emit a move from SRC to DEST, splitting compound moves into individual
3190 instructions. SPLIT_TYPE is the type of split to perform. */
3191
3192 static void
3193 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
3194 {
3195 if (mips_split_move_p (dest, src, split_type))
3196 mips_split_move (dest, src, split_type, NULL);
3197 else
3198 mips_emit_move (dest, src);
3199 }
3200
3201 /* Emit an instruction of the form (set TARGET (CODE OP0)). */
3202
3203 static void
3204 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
3205 {
3206 emit_insn (gen_rtx_SET (target, gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
3207 }
3208
3209 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
3210 Return that new register. */
3211
3212 static rtx
3213 mips_force_unary (machine_mode mode, enum rtx_code code, rtx op0)
3214 {
3215 rtx reg;
3216
3217 reg = gen_reg_rtx (mode);
3218 mips_emit_unary (code, reg, op0);
3219 return reg;
3220 }
3221
3222 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
3223
3224 void
3225 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
3226 {
3227 emit_insn (gen_rtx_SET (target, gen_rtx_fmt_ee (code, GET_MODE (target),
3228 op0, op1)));
3229 }
3230
3231 /* Compute (CODE OP0 OP1) and store the result in a new register
3232 of mode MODE. Return that new register. */
3233
3234 static rtx
3235 mips_force_binary (machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
3236 {
3237 rtx reg;
3238
3239 reg = gen_reg_rtx (mode);
3240 mips_emit_binary (code, reg, op0, op1);
3241 return reg;
3242 }
3243
3244 /* Copy VALUE to a register and return that register. If new pseudos
3245 are allowed, copy it into a new register, otherwise use DEST. */
3246
3247 static rtx
3248 mips_force_temporary (rtx dest, rtx value)
3249 {
3250 if (can_create_pseudo_p ())
3251 return force_reg (Pmode, value);
3252 else
3253 {
3254 mips_emit_move (dest, value);
3255 return dest;
3256 }
3257 }
3258
3259 /* Emit a call sequence with call pattern PATTERN and return the call
3260 instruction itself (which is not necessarily the last instruction
3261 emitted). ORIG_ADDR is the original, unlegitimized address,
3262 ADDR is the legitimized form, and LAZY_P is true if the call
3263 address is lazily-bound. */
3264
3265 static rtx_insn *
3266 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
3267 {
3268 rtx_insn *insn;
3269 rtx reg;
3270
3271 insn = emit_call_insn (pattern);
3272
3273 if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
3274 {
3275 /* MIPS16 JALRs only take MIPS16 registers. If the target
3276 function requires $25 to be valid on entry, we must copy it
3277 there separately. The move instruction can be put in the
3278 call's delay slot. */
3279 reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
3280 emit_insn_before (gen_move_insn (reg, addr), insn);
3281 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
3282 }
3283
3284 if (lazy_p)
3285 /* Lazy-binding stubs require $gp to be valid on entry. */
3286 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
3287
3288 if (TARGET_USE_GOT)
3289 {
3290 /* See the comment above load_call<mode> for details. */
3291 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
3292 gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
3293 emit_insn (gen_update_got_version ());
3294 }
3295
3296 if (TARGET_MIPS16
3297 && TARGET_EXPLICIT_RELOCS
3298 && TARGET_CALL_CLOBBERED_GP)
3299 {
3300 rtx post_call_tmp_reg = gen_rtx_REG (word_mode, POST_CALL_TMP_REG);
3301 clobber_reg (&CALL_INSN_FUNCTION_USAGE (insn), post_call_tmp_reg);
3302 clobber_reg (&CALL_INSN_FUNCTION_USAGE (insn), MIPS16_PIC_TEMP);
3303 clobber_reg (&CALL_INSN_FUNCTION_USAGE (insn),
3304 MIPS_PROLOGUE_TEMP (word_mode));
3305 }
3306
3307 return insn;
3308 }
3309
3310 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
3311 then add CONST_INT OFFSET to the result. */
3312
3313 static rtx
3314 mips_unspec_address_offset (rtx base, rtx offset,
3315 enum mips_symbol_type symbol_type)
3316 {
3317 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
3318 UNSPEC_ADDRESS_FIRST + symbol_type);
3319 if (offset != const0_rtx)
3320 base = gen_rtx_PLUS (Pmode, base, offset);
3321 return gen_rtx_CONST (Pmode, base);
3322 }
3323
3324 /* Return an UNSPEC address with underlying address ADDRESS and symbol
3325 type SYMBOL_TYPE. */
3326
3327 rtx
3328 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
3329 {
3330 rtx base, offset;
3331
3332 split_const (address, &base, &offset);
3333 return mips_unspec_address_offset (base, offset, symbol_type);
3334 }
3335
3336 /* If OP is an UNSPEC address, return the address to which it refers,
3337 otherwise return OP itself. */
3338
3339 rtx
3340 mips_strip_unspec_address (rtx op)
3341 {
3342 rtx base, offset;
3343
3344 split_const (op, &base, &offset);
3345 if (UNSPEC_ADDRESS_P (base))
3346 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
3347 return op;
3348 }
3349
3350 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
3351 high part to BASE and return the result. Just return BASE otherwise.
3352 TEMP is as for mips_force_temporary.
3353
3354 The returned expression can be used as the first operand to a LO_SUM. */
3355
3356 static rtx
3357 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
3358 enum mips_symbol_type symbol_type)
3359 {
3360 if (mips_split_p[symbol_type])
3361 {
3362 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
3363 addr = mips_force_temporary (temp, addr);
3364 base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
3365 }
3366 return base;
3367 }
3368
3369 /* Return an instruction that copies $gp into register REG. We want
3370 GCC to treat the register's value as constant, so that its value
3371 can be rematerialized on demand. */
3372
3373 static rtx
3374 gen_load_const_gp (rtx reg)
3375 {
3376 return PMODE_INSN (gen_load_const_gp, (reg));
3377 }
3378
3379 /* Return a pseudo register that contains the value of $gp throughout
3380 the current function. Such registers are needed by MIPS16 functions,
3381 for which $gp itself is not a valid base register or addition operand. */
3382
3383 static rtx
3384 mips16_gp_pseudo_reg (void)
3385 {
3386 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
3387 {
3388 rtx_insn *scan;
3389
3390 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
3391
3392 push_topmost_sequence ();
3393
3394 scan = get_insns ();
3395 while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
3396 scan = NEXT_INSN (scan);
3397
3398 rtx set = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
3399 rtx_insn *insn = emit_insn_after (set, scan);
3400 INSN_LOCATION (insn) = 0;
3401 /* NewABI support hasn't been implement. NewABI should generate RTL
3402 sequence instead of ASM sequence directly. */
3403 if (mips_current_loadgp_style () == LOADGP_OLDABI)
3404 {
3405 emit_clobber (MIPS16_PIC_TEMP);
3406 emit_clobber (MIPS_PROLOGUE_TEMP (Pmode));
3407 }
3408 pop_topmost_sequence ();
3409 }
3410
3411 return cfun->machine->mips16_gp_pseudo_rtx;
3412 }
3413
3414 /* Return a base register that holds pic_offset_table_rtx.
3415 TEMP, if nonnull, is a scratch Pmode base register. */
3416
3417 rtx
3418 mips_pic_base_register (rtx temp)
3419 {
3420 if (MIPS16_GP_LOADS ||!TARGET_MIPS16)
3421 return pic_offset_table_rtx;
3422
3423 if (currently_expanding_to_rtl)
3424 return mips16_gp_pseudo_reg ();
3425
3426 if (can_create_pseudo_p ())
3427 temp = gen_reg_rtx (Pmode);
3428
3429 if (TARGET_USE_GOT)
3430 /* The first post-reload split exposes all references to $gp
3431 (both uses and definitions). All references must remain
3432 explicit after that point.
3433
3434 It is safe to introduce uses of $gp at any time, so for
3435 simplicity, we do that before the split too. */
3436 mips_emit_move (temp, pic_offset_table_rtx);
3437 else
3438 emit_insn (gen_load_const_gp (temp));
3439 return temp;
3440 }
3441
3442 /* Return the RHS of a load_call<mode> insn. */
3443
3444 static rtx
3445 mips_unspec_call (rtx reg, rtx symbol)
3446 {
3447 rtvec vec;
3448
3449 vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
3450 return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
3451 }
3452
3453 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
3454 reference. Return NULL_RTX otherwise. */
3455
3456 static rtx
3457 mips_strip_unspec_call (rtx src)
3458 {
3459 if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
3460 return mips_strip_unspec_address (XVECEXP (src, 0, 1));
3461 return NULL_RTX;
3462 }
3463
3464 /* Create and return a GOT reference of type TYPE for address ADDR.
3465 TEMP, if nonnull, is a scratch Pmode base register. */
3466
3467 rtx
3468 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
3469 {
3470 rtx base, high, lo_sum_symbol;
3471
3472 base = mips_pic_base_register (temp);
3473
3474 /* If we used the temporary register to load $gp, we can't use
3475 it for the high part as well. */
3476 if (temp != NULL && reg_overlap_mentioned_p (base, temp))
3477 temp = NULL;
3478
3479 high = mips_unspec_offset_high (temp, base, addr, type);
3480 lo_sum_symbol = mips_unspec_address (addr, type);
3481
3482 if (type == SYMBOL_GOTOFF_CALL)
3483 return mips_unspec_call (high, lo_sum_symbol);
3484 else
3485 return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
3486 }
3487
3488 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
3489 it appears in a MEM of that mode. Return true if ADDR is a legitimate
3490 constant in that context and can be split into high and low parts.
3491 If so, and if LOW_OUT is nonnull, emit the high part and store the
3492 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
3493
3494 TEMP is as for mips_force_temporary and is used to load the high
3495 part into a register.
3496
3497 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
3498 a legitimize SET_SRC for an .md pattern, otherwise the low part
3499 is guaranteed to be a legitimate address for mode MODE. */
3500
3501 bool
3502 mips_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
3503 {
3504 enum mips_symbol_context context;
3505 enum mips_symbol_type symbol_type;
3506 rtx high;
3507
3508 context = (mode == MAX_MACHINE_MODE
3509 ? SYMBOL_CONTEXT_LEA
3510 : SYMBOL_CONTEXT_MEM);
3511 if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
3512 {
3513 addr = XEXP (addr, 0);
3514 if (mips_symbolic_constant_p (addr, context, &symbol_type)
3515 && mips_symbol_insns (symbol_type, mode) > 0
3516 && mips_split_hi_p[symbol_type])
3517 {
3518 if (low_out)
3519 switch (symbol_type)
3520 {
3521 case SYMBOL_GOT_PAGE_OFST:
3522 /* The high part of a page/ofst pair is loaded from the GOT. */
3523 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
3524 break;
3525
3526 default:
3527 gcc_unreachable ();
3528 }
3529 return true;
3530 }
3531 }
3532 else
3533 {
3534 if (mips_symbolic_constant_p (addr, context, &symbol_type)
3535 && mips_symbol_insns (symbol_type, mode) > 0
3536 && mips_split_p[symbol_type])
3537 {
3538 if (low_out)
3539 switch (symbol_type)
3540 {
3541 case SYMBOL_GOT_DISP:
3542 /* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
3543 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
3544 break;
3545
3546 case SYMBOL_GP_RELATIVE:
3547 high = mips_pic_base_register (temp);
3548 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3549 break;
3550
3551 default:
3552 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
3553 high = mips_force_temporary (temp, high);
3554 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3555 break;
3556 }
3557 return true;
3558 }
3559 }
3560 return false;
3561 }
3562
3563 /* Return a legitimate address for REG + OFFSET. TEMP is as for
3564 mips_force_temporary; it is only needed when OFFSET is not a
3565 SMALL_OPERAND. */
3566
3567 static rtx
3568 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
3569 {
3570 if (!SMALL_OPERAND (offset))
3571 {
3572 rtx high;
3573
3574 if (TARGET_MIPS16)
3575 {
3576 /* Load the full offset into a register so that we can use
3577 an unextended instruction for the address itself. */
3578 high = GEN_INT (offset);
3579 offset = 0;
3580 }
3581 else
3582 {
3583 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
3584 The addition inside the macro CONST_HIGH_PART may cause an
3585 overflow, so we need to force a sign-extension check. */
3586 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
3587 offset = CONST_LOW_PART (offset);
3588 }
3589 high = mips_force_temporary (temp, high);
3590 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
3591 }
3592 return plus_constant (Pmode, reg, offset);
3593 }
3594
3595 /* The __tls_get_attr symbol. */
3596 static GTY(()) rtx mips_tls_symbol;
3597
3598 /* Return an instruction sequence that calls __tls_get_addr. SYM is
3599 the TLS symbol we are referencing and TYPE is the symbol type to use
3600 (either global dynamic or local dynamic). V0 is an RTX for the
3601 return value location. */
3602
3603 static rtx_insn *
3604 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
3605 {
3606 rtx loc, a0;
3607 rtx_insn *insn;
3608
3609 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3610
3611 if (!mips_tls_symbol)
3612 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
3613
3614 loc = mips_unspec_address (sym, type);
3615
3616 start_sequence ();
3617
3618 emit_insn (gen_rtx_SET (a0, gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx,
3619 loc)));
3620 insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
3621 const0_rtx, NULL_RTX, false);
3622 RTL_CONST_CALL_P (insn) = 1;
3623 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
3624 insn = get_insns ();
3625
3626 end_sequence ();
3627
3628 return insn;
3629 }
3630
3631 /* Return a pseudo register that contains the current thread pointer. */
3632
3633 rtx
3634 mips_expand_thread_pointer (rtx tp)
3635 {
3636 rtx fn;
3637
3638 if (TARGET_MIPS16)
3639 {
3640 if (!mips16_rdhwr_stub)
3641 mips16_rdhwr_stub = new mips16_rdhwr_one_only_stub ();
3642 fn = mips16_stub_call_address (mips16_rdhwr_stub);
3643 emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
3644 }
3645 else
3646 emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
3647 return tp;
3648 }
3649
3650 static rtx
3651 mips_get_tp (void)
3652 {
3653 return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
3654 }
3655
3656 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
3657 its address. The return value will be both a valid address and a valid
3658 SET_SRC (either a REG or a LO_SUM). */
3659
3660 static rtx
3661 mips_legitimize_tls_address (rtx loc)
3662 {
3663 rtx dest, v0, tp, tmp1, tmp2, eqv, offset;
3664 enum tls_model model;
3665
3666 model = SYMBOL_REF_TLS_MODEL (loc);
3667 /* Only TARGET_ABICALLS code can have more than one module; other
3668 code must be static and should not use a GOT. All TLS models
3669 reduce to local exec in this situation. */
3670 if (!TARGET_ABICALLS)
3671 model = TLS_MODEL_LOCAL_EXEC;
3672
3673 switch (model)
3674 {
3675 case TLS_MODEL_GLOBAL_DYNAMIC:
3676 {
3677 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3678 rtx_insn *insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
3679 dest = gen_reg_rtx (Pmode);
3680 emit_libcall_block (insn, dest, v0, loc);
3681 break;
3682 }
3683
3684 case TLS_MODEL_LOCAL_DYNAMIC:
3685 {
3686 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3687 rtx_insn *insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
3688 tmp1 = gen_reg_rtx (Pmode);
3689
3690 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3691 share the LDM result with other LD model accesses. */
3692 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3693 UNSPEC_TLS_LDM);
3694 emit_libcall_block (insn, tmp1, v0, eqv);
3695
3696 offset = mips_unspec_address (loc, SYMBOL_DTPREL);
3697 if (mips_split_p[SYMBOL_DTPREL])
3698 {
3699 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
3700 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3701 }
3702 else
3703 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3704 0, 0, OPTAB_DIRECT);
3705 break;
3706 }
3707
3708 case TLS_MODEL_INITIAL_EXEC:
3709 tp = mips_get_tp ();
3710 tmp1 = gen_reg_rtx (Pmode);
3711 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
3712 if (Pmode == DImode)
3713 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
3714 else
3715 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
3716 dest = gen_reg_rtx (Pmode);
3717 emit_insn (gen_add3_insn (dest, tmp1, tp));
3718 break;
3719
3720 case TLS_MODEL_LOCAL_EXEC:
3721 tmp1 = mips_get_tp ();
3722 offset = mips_unspec_address (loc, SYMBOL_TPREL);
3723 if (mips_split_p[SYMBOL_TPREL])
3724 {
3725 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
3726 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3727 }
3728 else
3729 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3730 0, 0, OPTAB_DIRECT);
3731 break;
3732
3733 default:
3734 gcc_unreachable ();
3735 }
3736 return dest;
3737 }
3738
3739 /* Implement "TARGET = __builtin_mips_get_fcsr ()" for MIPS16,
3740 using a stub. */
3741
3742 void
3743 mips16_expand_get_fcsr (rtx target)
3744 {
3745 if (!mips16_get_fcsr_stub)
3746 mips16_get_fcsr_stub = new mips16_get_fcsr_one_only_stub ();
3747 rtx fn = mips16_stub_call_address (mips16_get_fcsr_stub);
3748 emit_insn (PMODE_INSN (gen_mips_get_fcsr_mips16, (fn)));
3749 emit_move_insn (target, gen_rtx_REG (SImode, GET_FCSR_REGNUM));
3750 }
3751
3752 /* Implement __builtin_mips_set_fcsr (TARGET) for MIPS16, using a stub. */
3753
3754 void
3755 mips16_expand_set_fcsr (rtx newval)
3756 {
3757 if (!mips16_set_fcsr_stub)
3758 mips16_set_fcsr_stub = new mips16_set_fcsr_one_only_stub ();
3759 rtx fn = mips16_stub_call_address (mips16_set_fcsr_stub);
3760 emit_move_insn (gen_rtx_REG (SImode, SET_FCSR_REGNUM), newval);
3761 emit_insn (PMODE_INSN (gen_mips_set_fcsr_mips16, (fn)));
3762 }
3763
3764 /* If X is not a valid address for mode MODE, force it into a register. */
3765
3766 static rtx
3767 mips_force_address (rtx x, machine_mode mode)
3768 {
3769 if (!mips_legitimate_address_p (mode, x, false))
3770 x = force_reg (Pmode, x);
3771 return x;
3772 }
3773
3774 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
3775 be legitimized in a way that the generic machinery might not expect,
3776 return a new address, otherwise return NULL. MODE is the mode of
3777 the memory being accessed. */
3778
3779 static rtx
3780 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3781 machine_mode mode)
3782 {
3783 rtx base, addr;
3784 HOST_WIDE_INT offset;
3785
3786 if (mips_tls_symbol_p (x))
3787 return mips_legitimize_tls_address (x);
3788
3789 /* See if the address can split into a high part and a LO_SUM. */
3790 if (mips_split_symbol (NULL, x, mode, &addr))
3791 return mips_force_address (addr, mode);
3792
3793 /* Handle BASE + OFFSET using mips_add_offset. */
3794 mips_split_plus (x, &base, &offset);
3795 if (offset != 0)
3796 {
3797 if (!mips_valid_base_register_p (base, mode, false))
3798 base = copy_to_mode_reg (Pmode, base);
3799 addr = mips_add_offset (NULL, base, offset);
3800 return mips_force_address (addr, mode);
3801 }
3802
3803 return x;
3804 }
3805
3806 /* Load VALUE into DEST. TEMP is as for mips_force_temporary. */
3807
3808 void
3809 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3810 {
3811 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3812 machine_mode mode;
3813 unsigned int i, num_ops;
3814 rtx x;
3815
3816 mode = GET_MODE (dest);
3817 num_ops = mips_build_integer (codes, value);
3818
3819 /* Apply each binary operation to X. Invariant: X is a legitimate
3820 source operand for a SET pattern. */
3821 x = GEN_INT (codes[0].value);
3822 for (i = 1; i < num_ops; i++)
3823 {
3824 if (!can_create_pseudo_p ())
3825 {
3826 emit_insn (gen_rtx_SET (temp, x));
3827 x = temp;
3828 }
3829 else
3830 x = force_reg (mode, x);
3831 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3832 }
3833
3834 emit_insn (gen_rtx_SET (dest, x));
3835 }
3836
3837 /* Subroutine of mips_legitimize_move. Move constant SRC into register
3838 DEST given that SRC satisfies immediate_operand but doesn't satisfy
3839 move_operand. */
3840
3841 static void
3842 mips_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
3843 {
3844 rtx base, offset;
3845
3846 /* Split moves of big integers into smaller pieces. */
3847 if (splittable_const_int_operand (src, mode))
3848 {
3849 mips_move_integer (dest, dest, INTVAL (src));
3850 return;
3851 }
3852
3853 /* Split moves of symbolic constants into high/low pairs. */
3854 if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3855 {
3856 emit_insn (gen_rtx_SET (dest, src));
3857 return;
3858 }
3859
3860 /* Generate the appropriate access sequences for TLS symbols. */
3861 if (mips_tls_symbol_p (src))
3862 {
3863 mips_emit_move (dest, mips_legitimize_tls_address (src));
3864 return;
3865 }
3866
3867 /* If we have (const (plus symbol offset)), and that expression cannot
3868 be forced into memory, load the symbol first and add in the offset.
3869 In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3870 forced into memory, as it usually produces better code. */
3871 split_const (src, &base, &offset);
3872 if (offset != const0_rtx
3873 && (targetm.cannot_force_const_mem (mode, src)
3874 || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3875 {
3876 base = mips_force_temporary (dest, base);
3877 mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3878 return;
3879 }
3880
3881 src = force_const_mem (mode, src);
3882
3883 /* When using explicit relocs, constant pool references are sometimes
3884 not legitimate addresses. */
3885 mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3886 mips_emit_move (dest, src);
3887 }
3888
3889 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3890 sequence that is valid. */
3891
3892 bool
3893 mips_legitimize_move (machine_mode mode, rtx dest, rtx src)
3894 {
3895 /* Both src and dest are non-registers; one special case is supported where
3896 the source is (const_int 0) and the store can source the zero register.
3897 MIPS16 and MSA are never able to source the zero register directly in
3898 memory operations. */
3899 if (!register_operand (dest, mode)
3900 && !register_operand (src, mode)
3901 && (TARGET_MIPS16 || !const_0_operand (src, mode)
3902 || MSA_SUPPORTED_MODE_P (mode)))
3903 {
3904 mips_emit_move (dest, force_reg (mode, src));
3905 return true;
3906 }
3907
3908 /* We need to deal with constants that would be legitimate
3909 immediate_operands but aren't legitimate move_operands. */
3910 if (CONSTANT_P (src) && !move_operand (src, mode))
3911 {
3912 mips_legitimize_const_move (mode, dest, src);
3913 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3914 return true;
3915 }
3916 return false;
3917 }
3918
3919 /* Return true if value X in context CONTEXT is a small-data address
3920 that can be rewritten as a LO_SUM. */
3921
3922 static bool
3923 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3924 {
3925 enum mips_symbol_type symbol_type;
3926
3927 return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3928 && !mips_split_p[SYMBOL_GP_RELATIVE]
3929 && mips_symbolic_constant_p (x, context, &symbol_type)
3930 && symbol_type == SYMBOL_GP_RELATIVE);
3931 }
3932
3933 /* Return true if OP refers to small data symbols directly, not through
3934 a LO_SUM. CONTEXT is the context in which X appears. */
3935
3936 static int
3937 mips_small_data_pattern_1 (rtx x, enum mips_symbol_context context)
3938 {
3939 subrtx_var_iterator::array_type array;
3940 FOR_EACH_SUBRTX_VAR (iter, array, x, ALL)
3941 {
3942 rtx x = *iter;
3943
3944 /* Ignore things like "g" constraints in asms. We make no particular
3945 guarantee about which symbolic constants are acceptable as asm operands
3946 versus which must be forced into a GPR. */
3947 if (GET_CODE (x) == LO_SUM || GET_CODE (x) == ASM_OPERANDS)
3948 iter.skip_subrtxes ();
3949 else if (MEM_P (x))
3950 {
3951 if (mips_small_data_pattern_1 (XEXP (x, 0), SYMBOL_CONTEXT_MEM))
3952 return true;
3953 iter.skip_subrtxes ();
3954 }
3955 else if (mips_rewrite_small_data_p (x, context))
3956 return true;
3957 }
3958 return false;
3959 }
3960
3961 /* Return true if OP refers to small data symbols directly, not through
3962 a LO_SUM. */
3963
3964 bool
3965 mips_small_data_pattern_p (rtx op)
3966 {
3967 return mips_small_data_pattern_1 (op, SYMBOL_CONTEXT_LEA);
3968 }
3969
3970 /* Rewrite *LOC so that it refers to small data using explicit
3971 relocations. CONTEXT is the context in which *LOC appears. */
3972
3973 static void
3974 mips_rewrite_small_data_1 (rtx *loc, enum mips_symbol_context context)
3975 {
3976 subrtx_ptr_iterator::array_type array;
3977 FOR_EACH_SUBRTX_PTR (iter, array, loc, ALL)
3978 {
3979 rtx *loc = *iter;
3980 if (MEM_P (*loc))
3981 {
3982 mips_rewrite_small_data_1 (&XEXP (*loc, 0), SYMBOL_CONTEXT_MEM);
3983 iter.skip_subrtxes ();
3984 }
3985 else if (mips_rewrite_small_data_p (*loc, context))
3986 {
3987 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3988 iter.skip_subrtxes ();
3989 }
3990 else if (GET_CODE (*loc) == LO_SUM)
3991 iter.skip_subrtxes ();
3992 }
3993 }
3994
3995 /* Rewrite instruction pattern PATTERN so that it refers to small data
3996 using explicit relocations. */
3997
3998 rtx
3999 mips_rewrite_small_data (rtx pattern)
4000 {
4001 pattern = copy_insn (pattern);
4002 mips_rewrite_small_data_1 (&pattern, SYMBOL_CONTEXT_LEA);
4003 return pattern;
4004 }
4005
4006 /* The cost of loading values from the constant pool. It should be
4007 larger than the cost of any constant we want to synthesize inline. */
4008 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
4009
4010 /* Return the cost of X when used as an operand to the MIPS16 instruction
4011 that implements CODE. Return -1 if there is no such instruction, or if
4012 X is not a valid immediate operand for it. */
4013
4014 static int
4015 mips16_constant_cost (int code, HOST_WIDE_INT x)
4016 {
4017 switch (code)
4018 {
4019 case ASHIFT:
4020 case ASHIFTRT:
4021 case LSHIFTRT:
4022 /* Shifts by between 1 and 8 bits (inclusive) are unextended,
4023 other shifts are extended. The shift patterns truncate the shift
4024 count to the right size, so there are no out-of-range values. */
4025 if (IN_RANGE (x, 1, 8))
4026 return 0;
4027 return COSTS_N_INSNS (1);
4028
4029 case PLUS:
4030 if (IN_RANGE (x, -128, 127))
4031 return 0;
4032 if (SMALL_OPERAND (x))
4033 return COSTS_N_INSNS (1);
4034 return -1;
4035
4036 case LEU:
4037 /* Like LE, but reject the always-true case. */
4038 if (x == -1)
4039 return -1;
4040 /* FALLTHRU */
4041 case LE:
4042 /* We add 1 to the immediate and use SLT. */
4043 x += 1;
4044 /* FALLTHRU */
4045 case XOR:
4046 /* We can use CMPI for an xor with an unsigned 16-bit X. */
4047 case LT:
4048 case LTU:
4049 if (IN_RANGE (x, 0, 255))
4050 return 0;
4051 if (SMALL_OPERAND_UNSIGNED (x))
4052 return COSTS_N_INSNS (1);
4053 return -1;
4054
4055 case EQ:
4056 case NE:
4057 /* Equality comparisons with 0 are cheap. */
4058 if (x == 0)
4059 return 0;
4060 return -1;
4061
4062 case ZERO_EXTRACT:
4063 /* The bit position and size are immediate operands. */
4064 return ISA_HAS_EXT_INS ? COSTS_N_INSNS (1) : -1;
4065
4066 default:
4067 return -1;
4068 }
4069 }
4070
4071 /* Return true if there is a non-MIPS16 instruction that implements CODE
4072 and if that instruction accepts X as an immediate operand. */
4073
4074 static int
4075 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
4076 {
4077 switch (code)
4078 {
4079 case ASHIFT:
4080 case ASHIFTRT:
4081 case LSHIFTRT:
4082 /* All shift counts are truncated to a valid constant. */
4083 return true;
4084
4085 case ROTATE:
4086 case ROTATERT:
4087 /* Likewise rotates, if the target supports rotates at all. */
4088 return ISA_HAS_ROR;
4089
4090 case AND:
4091 case IOR:
4092 case XOR:
4093 /* These instructions take 16-bit unsigned immediates. */
4094 return SMALL_OPERAND_UNSIGNED (x);
4095
4096 case PLUS:
4097 case LT:
4098 case LTU:
4099 /* These instructions take 16-bit signed immediates. */
4100 return SMALL_OPERAND (x);
4101
4102 case EQ:
4103 case NE:
4104 case GT:
4105 case GTU:
4106 /* The "immediate" forms of these instructions are really
4107 implemented as comparisons with register 0. */
4108 return x == 0;
4109
4110 case GE:
4111 case GEU:
4112 /* Likewise, meaning that the only valid immediate operand is 1. */
4113 return x == 1;
4114
4115 case LE:
4116 /* We add 1 to the immediate and use SLT. */
4117 return SMALL_OPERAND (x + 1);
4118
4119 case LEU:
4120 /* Likewise SLTU, but reject the always-true case. */
4121 return SMALL_OPERAND (x + 1) && x + 1 != 0;
4122
4123 case SIGN_EXTRACT:
4124 case ZERO_EXTRACT:
4125 /* The bit position and size are immediate operands. */
4126 return ISA_HAS_EXT_INS;
4127
4128 default:
4129 /* By default assume that $0 can be used for 0. */
4130 return x == 0;
4131 }
4132 }
4133
4134 /* Return the cost of binary operation X, given that the instruction
4135 sequence for a word-sized or smaller operation has cost SINGLE_COST
4136 and that the sequence of a double-word operation has cost DOUBLE_COST.
4137 If SPEED is true, optimize for speed otherwise optimize for size. */
4138
4139 static int
4140 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
4141 {
4142 int cost;
4143
4144 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
4145 cost = double_cost;
4146 else
4147 cost = single_cost;
4148 return (cost
4149 + set_src_cost (XEXP (x, 0), GET_MODE (x), speed)
4150 + rtx_cost (XEXP (x, 1), GET_MODE (x), GET_CODE (x), 1, speed));
4151 }
4152
4153 /* Return the cost of floating-point multiplications of mode MODE. */
4154
4155 static int
4156 mips_fp_mult_cost (machine_mode mode)
4157 {
4158 return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
4159 }
4160
4161 /* Return the cost of floating-point divisions of mode MODE. */
4162
4163 static int
4164 mips_fp_div_cost (machine_mode mode)
4165 {
4166 return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
4167 }
4168
4169 /* Return the cost of sign-extending OP to mode MODE, not including the
4170 cost of OP itself. */
4171
4172 static int
4173 mips_sign_extend_cost (machine_mode mode, rtx op)
4174 {
4175 if (MEM_P (op))
4176 /* Extended loads are as cheap as unextended ones. */
4177 return 0;
4178
4179 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
4180 /* A sign extension from SImode to DImode in 64-bit mode is free. */
4181 return 0;
4182
4183 if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
4184 /* We can use SEB or SEH. */
4185 return COSTS_N_INSNS (1);
4186
4187 /* We need to use a shift left and a shift right. */
4188 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
4189 }
4190
4191 /* Return the cost of zero-extending OP to mode MODE, not including the
4192 cost of OP itself. */
4193
4194 static int
4195 mips_zero_extend_cost (machine_mode mode, rtx op)
4196 {
4197 if (MEM_P (op))
4198 /* Extended loads are as cheap as unextended ones. */
4199 return 0;
4200
4201 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
4202 /* We need a shift left by 32 bits and a shift right by 32 bits. */
4203 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
4204
4205 if (GENERATE_MIPS16E)
4206 /* We can use ZEB or ZEH. */
4207 return COSTS_N_INSNS (1);
4208
4209 if (TARGET_MIPS16)
4210 /* We need to load 0xff or 0xffff into a register and use AND. */
4211 return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
4212
4213 /* We can use ANDI. */
4214 return COSTS_N_INSNS (1);
4215 }
4216
4217 /* Return the cost of moving between two registers of mode MODE,
4218 assuming that the move will be in pieces of at most UNITS bytes. */
4219
4220 static int
4221 mips_set_reg_reg_piece_cost (machine_mode mode, unsigned int units)
4222 {
4223 return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
4224 }
4225
4226 /* Return the cost of moving between two registers of mode MODE. */
4227
4228 static int
4229 mips_set_reg_reg_cost (machine_mode mode)
4230 {
4231 switch (GET_MODE_CLASS (mode))
4232 {
4233 case MODE_CC:
4234 return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
4235
4236 case MODE_FLOAT:
4237 case MODE_COMPLEX_FLOAT:
4238 case MODE_VECTOR_FLOAT:
4239 if (TARGET_HARD_FLOAT)
4240 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
4241 /* Fall through */
4242
4243 default:
4244 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
4245 }
4246 }
4247
4248 /* Implement TARGET_INSN_COSTS. */
4249
4250 static int
4251 mips_insn_cost (rtx_insn *x, bool speed)
4252 {
4253 int cost;
4254 int count;
4255 int ratio;
4256
4257 if (recog_memoized (x) < 0
4258 && GET_CODE (PATTERN (x)) != ASM_INPUT
4259 && asm_noperands (PATTERN (x)) < 0)
4260 goto pattern_cost;
4261
4262 /* FIXME: return get_attr_length? More tests may be needed. */
4263 if (!speed)
4264 goto pattern_cost;
4265
4266 count = get_attr_insn_count (x);
4267 ratio = get_attr_perf_ratio (x);
4268 cost = COSTS_N_INSNS (count) * ratio;
4269 if (cost > 0)
4270 return cost;
4271
4272 pattern_cost:
4273 cost = pattern_cost (PATTERN (x), speed);
4274 /* If the cost is zero, then it's likely a complex insn.
4275 FIXME: Return COSTS_N_INSNS (2)? More tests are needed. */
4276 return cost;
4277 }
4278
4279 /* Implement TARGET_RTX_COSTS. */
4280
4281 static bool
4282 mips_rtx_costs (rtx x, machine_mode mode, int outer_code,
4283 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
4284 {
4285 int code = GET_CODE (x);
4286 bool float_mode_p = FLOAT_MODE_P (mode);
4287 int cost;
4288 rtx addr;
4289
4290 /* The cost of a COMPARE is hard to define for MIPS. COMPAREs don't
4291 appear in the instruction stream, and the cost of a comparison is
4292 really the cost of the branch or scc condition. At the time of
4293 writing, GCC only uses an explicit outer COMPARE code when optabs
4294 is testing whether a constant is expensive enough to force into a
4295 register. We want optabs to pass such constants through the MIPS
4296 expanders instead, so make all constants very cheap here. */
4297 if (outer_code == COMPARE)
4298 {
4299 gcc_assert (CONSTANT_P (x));
4300 *total = 0;
4301 return true;
4302 }
4303
4304 switch (code)
4305 {
4306 case CONST_INT:
4307 /* Treat *clear_upper32-style ANDs as having zero cost in the
4308 second operand. The cost is entirely in the first operand.
4309
4310 ??? This is needed because we would otherwise try to CSE
4311 the constant operand. Although that's the right thing for
4312 instructions that continue to be a register operation throughout
4313 compilation, it is disastrous for instructions that could
4314 later be converted into a memory operation. */
4315 if (TARGET_64BIT
4316 && outer_code == AND
4317 && UINTVAL (x) == 0xffffffff)
4318 {
4319 *total = 0;
4320 return true;
4321 }
4322
4323 if (TARGET_MIPS16)
4324 {
4325 cost = mips16_constant_cost (outer_code, INTVAL (x));
4326 if (cost >= 0)
4327 {
4328 *total = cost;
4329 return true;
4330 }
4331 }
4332 else
4333 {
4334 /* When not optimizing for size, we care more about the cost
4335 of hot code, and hot code is often in a loop. If a constant
4336 operand needs to be forced into a register, we will often be
4337 able to hoist the constant load out of the loop, so the load
4338 should not contribute to the cost. */
4339 if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
4340 {
4341 *total = 0;
4342 return true;
4343 }
4344 }
4345 /* Fall through. */
4346
4347 case CONST:
4348 case SYMBOL_REF:
4349 case LABEL_REF:
4350 case CONST_DOUBLE:
4351 if (force_to_mem_operand (x, VOIDmode))
4352 {
4353 *total = COSTS_N_INSNS (1);
4354 return true;
4355 }
4356 cost = mips_const_insns (x);
4357 if (cost > 0)
4358 {
4359 /* If the constant is likely to be stored in a GPR, SETs of
4360 single-insn constants are as cheap as register sets; we
4361 never want to CSE them.
4362
4363 Don't reduce the cost of storing a floating-point zero in
4364 FPRs. If we have a zero in an FPR for other reasons, we
4365 can get better cfg-cleanup and delayed-branch results by
4366 using it consistently, rather than using $0 sometimes and
4367 an FPR at other times. Also, moves between floating-point
4368 registers are sometimes cheaper than (D)MTC1 $0. */
4369 if (cost == 1
4370 && outer_code == SET
4371 && !(float_mode_p && TARGET_HARD_FLOAT))
4372 cost = 0;
4373 /* When non-MIPS16 code loads a constant N>1 times, we rarely
4374 want to CSE the constant itself. It is usually better to
4375 have N copies of the last operation in the sequence and one
4376 shared copy of the other operations. (Note that this is
4377 not true for MIPS16 code, where the final operation in the
4378 sequence is often an extended instruction.)
4379
4380 Also, if we have a CONST_INT, we don't know whether it is
4381 for a word or doubleword operation, so we cannot rely on
4382 the result of mips_build_integer. */
4383 else if (!TARGET_MIPS16
4384 && (outer_code == SET || GET_MODE (x) == VOIDmode))
4385 cost = 1;
4386 *total = COSTS_N_INSNS (cost);
4387 return true;
4388 }
4389 /* The value will need to be fetched from the constant pool. */
4390 *total = CONSTANT_POOL_COST;
4391 return true;
4392
4393 case MEM:
4394 /* If the address is legitimate, return the number of
4395 instructions it needs. */
4396 addr = XEXP (x, 0);
4397 cost = mips_address_insns (addr, mode, true);
4398 if (cost > 0)
4399 {
4400 *total = COSTS_N_INSNS (cost + 1);
4401 return true;
4402 }
4403 /* Check for a scaled indexed address. */
4404 if (mips_lwxs_address_p (addr)
4405 || mips_lx_address_p (addr, mode))
4406 {
4407 *total = COSTS_N_INSNS (2);
4408 return true;
4409 }
4410 /* Otherwise use the default handling. */
4411 return false;
4412
4413 case FFS:
4414 *total = COSTS_N_INSNS (6);
4415 return false;
4416
4417 case NOT:
4418 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
4419 return false;
4420
4421 case AND:
4422 /* Check for a *clear_upper32 pattern and treat it like a zero
4423 extension. See the pattern's comment for details. */
4424 if (TARGET_64BIT
4425 && mode == DImode
4426 && CONST_INT_P (XEXP (x, 1))
4427 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
4428 {
4429 *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
4430 + set_src_cost (XEXP (x, 0), mode, speed));
4431 return true;
4432 }
4433 if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
4434 {
4435 rtx op = XEXP (x, 0);
4436 if (GET_CODE (op) == ASHIFT
4437 && CONST_INT_P (XEXP (op, 1))
4438 && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
4439 {
4440 *total = COSTS_N_INSNS (1);
4441 *total += set_src_cost (XEXP (op, 0), mode, speed);
4442 return true;
4443 }
4444 }
4445 /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
4446 a single instruction. */
4447 if (!TARGET_MIPS16
4448 && GET_CODE (XEXP (x, 0)) == NOT
4449 && GET_CODE (XEXP (x, 1)) == NOT)
4450 {
4451 cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
4452 *total = (COSTS_N_INSNS (cost)
4453 + set_src_cost (XEXP (XEXP (x, 0), 0), mode, speed)
4454 + set_src_cost (XEXP (XEXP (x, 1), 0), mode, speed));
4455 return true;
4456 }
4457
4458 /* Fall through. */
4459
4460 case IOR:
4461 case XOR:
4462 /* Double-word operations use two single-word operations. */
4463 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
4464 speed);
4465 return true;
4466
4467 case ASHIFT:
4468 case ASHIFTRT:
4469 case LSHIFTRT:
4470 case ROTATE:
4471 case ROTATERT:
4472 if (CONSTANT_P (XEXP (x, 1)))
4473 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
4474 speed);
4475 else
4476 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
4477 speed);
4478 return true;
4479
4480 case ABS:
4481 if (float_mode_p)
4482 *total = mips_cost->fp_add;
4483 else
4484 *total = COSTS_N_INSNS (4);
4485 return false;
4486
4487 case LO_SUM:
4488 /* Low-part immediates need an extended MIPS16 instruction. */
4489 *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
4490 + set_src_cost (XEXP (x, 0), mode, speed));
4491 return true;
4492
4493 case LT:
4494 case LTU:
4495 case LE:
4496 case LEU:
4497 case GT:
4498 case GTU:
4499 case GE:
4500 case GEU:
4501 case EQ:
4502 case NE:
4503 case UNORDERED:
4504 case LTGT:
4505 case UNGE:
4506 case UNGT:
4507 case UNLE:
4508 case UNLT:
4509 /* Branch comparisons have VOIDmode, so use the first operand's
4510 mode instead. */
4511 mode = GET_MODE (XEXP (x, 0));
4512 if (FLOAT_MODE_P (mode))
4513 {
4514 *total = mips_cost->fp_add;
4515 return false;
4516 }
4517 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
4518 speed);
4519 return true;
4520
4521 case MINUS:
4522 if (float_mode_p && ISA_HAS_UNFUSED_MADD4 && !HONOR_SIGNED_ZEROS (mode))
4523 {
4524 /* See if we can use NMADD or NMSUB via the *nmadd4<mode>_fastmath
4525 or *nmsub4<mode>_fastmath patterns. These patterns check for
4526 HONOR_SIGNED_ZEROS so we check here too. */
4527 rtx op0 = XEXP (x, 0);
4528 rtx op1 = XEXP (x, 1);
4529 if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
4530 {
4531 *total = (mips_fp_mult_cost (mode)
4532 + set_src_cost (XEXP (XEXP (op0, 0), 0), mode, speed)
4533 + set_src_cost (XEXP (op0, 1), mode, speed)
4534 + set_src_cost (op1, mode, speed));
4535 return true;
4536 }
4537 if (GET_CODE (op1) == MULT)
4538 {
4539 *total = (mips_fp_mult_cost (mode)
4540 + set_src_cost (op0, mode, speed)
4541 + set_src_cost (XEXP (op1, 0), mode, speed)
4542 + set_src_cost (XEXP (op1, 1), mode, speed));
4543 return true;
4544 }
4545 }
4546 /* Fall through. */
4547
4548 case PLUS:
4549 if (float_mode_p)
4550 {
4551 /* If this is part of a MADD or MSUB, treat the PLUS as
4552 being free. */
4553 if (ISA_HAS_UNFUSED_MADD4 && GET_CODE (XEXP (x, 0)) == MULT)
4554 *total = 0;
4555 else
4556 *total = mips_cost->fp_add;
4557 return false;
4558 }
4559
4560 /* If it's an add + mult (which is equivalent to shift left) and
4561 it's immediate operand satisfies const_immlsa_operand predicate. */
4562 if (((ISA_HAS_LSA && mode == SImode)
4563 || (ISA_HAS_DLSA && mode == DImode))
4564 && GET_CODE (XEXP (x, 0)) == MULT)
4565 {
4566 rtx op2 = XEXP (XEXP (x, 0), 1);
4567 if (const_immlsa_operand (op2, mode))
4568 {
4569 *total = (COSTS_N_INSNS (1)
4570 + set_src_cost (XEXP (XEXP (x, 0), 0), mode, speed)
4571 + set_src_cost (XEXP (x, 1), mode, speed));
4572 return true;
4573 }
4574 }
4575
4576 /* Double-word operations require three single-word operations and
4577 an SLTU. The MIPS16 version then needs to move the result of
4578 the SLTU from $24 to a MIPS16 register. */
4579 *total = mips_binary_cost (x, COSTS_N_INSNS (1),
4580 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
4581 speed);
4582 return true;
4583
4584 case NEG:
4585 if (float_mode_p && ISA_HAS_UNFUSED_MADD4)
4586 {
4587 /* See if we can use NMADD or NMSUB via the *nmadd4<mode> or
4588 *nmsub4<mode> patterns. */
4589 rtx op = XEXP (x, 0);
4590 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
4591 && GET_CODE (XEXP (op, 0)) == MULT)
4592 {
4593 *total = (mips_fp_mult_cost (mode)
4594 + set_src_cost (XEXP (XEXP (op, 0), 0), mode, speed)
4595 + set_src_cost (XEXP (XEXP (op, 0), 1), mode, speed)
4596 + set_src_cost (XEXP (op, 1), mode, speed));
4597 return true;
4598 }
4599 }
4600
4601 if (float_mode_p)
4602 *total = mips_cost->fp_add;
4603 else
4604 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
4605 return false;
4606
4607 case FMA:
4608 *total = mips_fp_mult_cost (mode);
4609 return false;
4610
4611 case MULT:
4612 if (float_mode_p)
4613 *total = mips_fp_mult_cost (mode);
4614 else if (mode == DImode && !TARGET_64BIT)
4615 /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
4616 where the mulsidi3 always includes an MFHI and an MFLO. */
4617 *total = (speed
4618 ? mips_cost->int_mult_si * 3 + 6
4619 : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
4620 else if (!speed)
4621 *total = COSTS_N_INSNS ((ISA_HAS_MUL3 || ISA_HAS_R6MUL) ? 1 : 2) + 1;
4622 else if (mode == DImode)
4623 *total = mips_cost->int_mult_di;
4624 else
4625 *total = mips_cost->int_mult_si;
4626 return false;
4627
4628 case DIV:
4629 /* Check for a reciprocal. */
4630 if (float_mode_p
4631 && ISA_HAS_FP_RECIP_RSQRT (mode)
4632 && flag_unsafe_math_optimizations
4633 && XEXP (x, 0) == CONST1_RTX (mode))
4634 {
4635 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
4636 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
4637 division as being free. */
4638 *total = set_src_cost (XEXP (x, 1), mode, speed);
4639 else
4640 *total = (mips_fp_div_cost (mode)
4641 + set_src_cost (XEXP (x, 1), mode, speed));
4642 return true;
4643 }
4644 /* Fall through. */
4645
4646 case SQRT:
4647 case MOD:
4648 if (float_mode_p)
4649 {
4650 *total = mips_fp_div_cost (mode);
4651 return false;
4652 }
4653 /* Fall through. */
4654
4655 case UDIV:
4656 case UMOD:
4657 if (!speed)
4658 {
4659 /* It is our responsibility to make division by a power of 2
4660 as cheap as 2 register additions if we want the division
4661 expanders to be used for such operations; see the setting
4662 of sdiv_pow2_cheap in optabs.cc. Using (D)DIV for MIPS16
4663 should always produce shorter code than using
4664 expand_sdiv2_pow2. */
4665 if (TARGET_MIPS16
4666 && CONST_INT_P (XEXP (x, 1))
4667 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
4668 {
4669 *total = COSTS_N_INSNS (2);
4670 *total += set_src_cost (XEXP (x, 0), mode, speed);
4671 return true;
4672 }
4673 *total = COSTS_N_INSNS (mips_idiv_insns (mode));
4674 }
4675 else if (mode == DImode)
4676 *total = mips_cost->int_div_di;
4677 else
4678 *total = mips_cost->int_div_si;
4679 return false;
4680
4681 case SIGN_EXTEND:
4682 *total = mips_sign_extend_cost (mode, XEXP (x, 0));
4683 return false;
4684
4685 case ZERO_EXTEND:
4686 if (outer_code == SET
4687 && ISA_HAS_BADDU
4688 && (GET_CODE (XEXP (x, 0)) == TRUNCATE
4689 || GET_CODE (XEXP (x, 0)) == SUBREG)
4690 && GET_MODE (XEXP (x, 0)) == QImode
4691 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
4692 {
4693 *total = set_src_cost (XEXP (XEXP (x, 0), 0), VOIDmode, speed);
4694 return true;
4695 }
4696 *total = mips_zero_extend_cost (mode, XEXP (x, 0));
4697 return false;
4698 case TRUNCATE:
4699 /* Costings for highpart multiplies. Matching patterns of the form:
4700
4701 (lshiftrt:DI (mult:DI (sign_extend:DI (...)
4702 (sign_extend:DI (...))
4703 (const_int 32)
4704 */
4705 if (ISA_HAS_R6MUL
4706 && (GET_CODE (XEXP (x, 0)) == ASHIFTRT
4707 || GET_CODE (XEXP (x, 0)) == LSHIFTRT)
4708 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4709 && ((INTVAL (XEXP (XEXP (x, 0), 1)) == 32
4710 && GET_MODE (XEXP (x, 0)) == DImode)
4711 || (ISA_HAS_R6DMUL
4712 && INTVAL (XEXP (XEXP (x, 0), 1)) == 64
4713 && GET_MODE (XEXP (x, 0)) == TImode))
4714 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4715 && ((GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND
4716 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SIGN_EXTEND)
4717 || (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4718 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1))
4719 == ZERO_EXTEND))))
4720 {
4721 if (!speed)
4722 *total = COSTS_N_INSNS (1) + 1;
4723 else if (mode == DImode)
4724 *total = mips_cost->int_mult_di;
4725 else
4726 *total = mips_cost->int_mult_si;
4727
4728 /* Sign extension is free, zero extension costs for DImode when
4729 on a 64bit core / when DMUL is present. */
4730 for (int i = 0; i < 2; ++i)
4731 {
4732 rtx op = XEXP (XEXP (XEXP (x, 0), 0), i);
4733 if (ISA_HAS_R6DMUL
4734 && GET_CODE (op) == ZERO_EXTEND
4735 && GET_MODE (op) == DImode)
4736 *total += rtx_cost (op, DImode, MULT, i, speed);
4737 else
4738 *total += rtx_cost (XEXP (op, 0), VOIDmode, GET_CODE (op),
4739 0, speed);
4740 }
4741
4742 return true;
4743 }
4744 return false;
4745
4746 case FLOAT:
4747 case UNSIGNED_FLOAT:
4748 case FIX:
4749 case FLOAT_EXTEND:
4750 case FLOAT_TRUNCATE:
4751 *total = mips_cost->fp_add;
4752 return false;
4753
4754 case SET:
4755 if (register_operand (SET_DEST (x), VOIDmode)
4756 && reg_or_0_operand (SET_SRC (x), VOIDmode))
4757 {
4758 *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4759 return true;
4760 }
4761 int insn_code;
4762 if (register_operand (SET_DEST (x), VOIDmode)
4763 && GET_CODE (SET_SRC (x)) == IF_THEN_ELSE)
4764 insn_code = recog_memoized (make_insn_raw (x));
4765 else
4766 insn_code = -1;
4767 switch (insn_code)
4768 {
4769 /* MIPS16e2 ones may be listed here, while the only known CPU core
4770 that implements MIPS16e2 is interAptiv. The Dependency delays
4771 of MOVN/MOVZ on interAptiv is 3. */
4772 case CODE_FOR_movsi_on_si:
4773 case CODE_FOR_movdi_on_si:
4774 case CODE_FOR_movsi_on_di:
4775 case CODE_FOR_movdi_on_di:
4776 case CODE_FOR_movsi_on_si_ne:
4777 case CODE_FOR_movdi_on_si_ne:
4778 case CODE_FOR_movsi_on_di_ne:
4779 case CODE_FOR_movdi_on_di_ne:
4780 *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4781 return true;
4782 default:
4783 break;
4784 }
4785 return false;
4786
4787 default:
4788 return false;
4789 }
4790 }
4791
4792 /* Implement TARGET_ADDRESS_COST. */
4793
4794 static int
4795 mips_address_cost (rtx addr, machine_mode mode,
4796 addr_space_t as ATTRIBUTE_UNUSED,
4797 bool speed ATTRIBUTE_UNUSED)
4798 {
4799 return mips_address_insns (addr, mode, false);
4800 }
4801
4802 /* Implement TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P. */
4803
4804 static bool
4805 mips_no_speculation_in_delay_slots_p ()
4806 {
4807 return TARGET_CB_MAYBE;
4808 }
4809
4810 /* Information about a single instruction in a multi-instruction
4811 asm sequence. */
4812 struct mips_multi_member {
4813 /* True if this is a label, false if it is code. */
4814 bool is_label_p;
4815
4816 /* The output_asm_insn format of the instruction. */
4817 const char *format;
4818
4819 /* The operands to the instruction. */
4820 rtx operands[MAX_RECOG_OPERANDS];
4821 };
4822 typedef struct mips_multi_member mips_multi_member;
4823
4824 /* The instructions that make up the current multi-insn sequence. */
4825 static vec<mips_multi_member> mips_multi_members;
4826
4827 /* How many instructions (as opposed to labels) are in the current
4828 multi-insn sequence. */
4829 static unsigned int mips_multi_num_insns;
4830
4831 /* Start a new multi-insn sequence. */
4832
4833 static void
4834 mips_multi_start (void)
4835 {
4836 mips_multi_members.truncate (0);
4837 mips_multi_num_insns = 0;
4838 }
4839
4840 /* Add a new, zero initialized member to the current multi-insn sequence. */
4841
4842 static struct mips_multi_member *
4843 mips_multi_add (void)
4844 {
4845 mips_multi_member empty;
4846 memset (&empty, 0, sizeof (empty));
4847 return mips_multi_members.safe_push (empty);
4848 }
4849
4850 /* Add a normal insn with the given asm format to the current multi-insn
4851 sequence. The other arguments are a null-terminated list of operands. */
4852
4853 static void
4854 mips_multi_add_insn (const char *format, ...)
4855 {
4856 struct mips_multi_member *member;
4857 va_list ap;
4858 unsigned int i;
4859 rtx op;
4860
4861 member = mips_multi_add ();
4862 member->is_label_p = false;
4863 member->format = format;
4864 va_start (ap, format);
4865 i = 0;
4866 while ((op = va_arg (ap, rtx)))
4867 member->operands[i++] = op;
4868 va_end (ap);
4869 mips_multi_num_insns++;
4870 }
4871
4872 /* Add the given label definition to the current multi-insn sequence.
4873 The definition should include the colon. */
4874
4875 static void
4876 mips_multi_add_label (const char *label)
4877 {
4878 struct mips_multi_member *member;
4879
4880 member = mips_multi_add ();
4881 member->is_label_p = true;
4882 member->format = label;
4883 }
4884
4885 /* Return the index of the last member of the current multi-insn sequence. */
4886
4887 static unsigned int
4888 mips_multi_last_index (void)
4889 {
4890 return mips_multi_members.length () - 1;
4891 }
4892
4893 /* Add a copy of an existing instruction to the current multi-insn
4894 sequence. I is the index of the instruction that should be copied. */
4895
4896 static void
4897 mips_multi_copy_insn (unsigned int i)
4898 {
4899 struct mips_multi_member *member;
4900
4901 member = mips_multi_add ();
4902 memcpy (member, &mips_multi_members[i], sizeof (*member));
4903 gcc_assert (!member->is_label_p);
4904 }
4905
4906 /* Change the operand of an existing instruction in the current
4907 multi-insn sequence. I is the index of the instruction,
4908 OP is the index of the operand, and X is the new value. */
4909
4910 static void
4911 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4912 {
4913 mips_multi_members[i].operands[op] = x;
4914 }
4915
4916 /* Write out the asm code for the current multi-insn sequence. */
4917
4918 static void
4919 mips_multi_write (void)
4920 {
4921 struct mips_multi_member *member;
4922 unsigned int i;
4923
4924 FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4925 if (member->is_label_p)
4926 fprintf (asm_out_file, "%s\n", member->format);
4927 else
4928 output_asm_insn (member->format, member->operands);
4929 }
4930
4931 /* Return one word of double-word value OP, taking into account the fixed
4932 endianness of certain registers. HIGH_P is true to select the high part,
4933 false to select the low part. */
4934
4935 rtx
4936 mips_subword (rtx op, bool high_p)
4937 {
4938 unsigned int byte, offset;
4939 machine_mode mode;
4940
4941 mode = GET_MODE (op);
4942 if (mode == VOIDmode)
4943 mode = TARGET_64BIT ? TImode : DImode;
4944
4945 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4946 byte = UNITS_PER_WORD;
4947 else
4948 byte = 0;
4949
4950 if (FP_REG_RTX_P (op))
4951 {
4952 /* Paired FPRs are always ordered little-endian. */
4953 offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4954 return gen_rtx_REG (word_mode, REGNO (op) + offset);
4955 }
4956
4957 if (MEM_P (op))
4958 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4959
4960 return simplify_gen_subreg (word_mode, op, mode, byte);
4961 }
4962
4963 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4964 SPLIT_TYPE is the condition under which moves should be split. */
4965
4966 static bool
4967 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4968 {
4969 return ((split_type != SPLIT_FOR_SPEED
4970 || mips_tuning_info.fast_mult_zero_zero_p)
4971 && src == const0_rtx
4972 && REG_P (dest)
4973 && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4974 && (ISA_HAS_DSP_MULT
4975 ? ACC_REG_P (REGNO (dest))
4976 : MD_REG_P (REGNO (dest))));
4977 }
4978
4979 /* Return true if a move from SRC to DEST should be split into two.
4980 SPLIT_TYPE describes the split condition. */
4981
4982 bool
4983 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4984 {
4985 /* Check whether the move can be done using some variant of MULT $0,$0. */
4986 if (mips_mult_move_p (dest, src, split_type))
4987 return false;
4988
4989 /* FPR-to-FPR moves can be done in a single instruction, if they're
4990 allowed at all. */
4991 unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4992 if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4993 return false;
4994
4995 /* Check for floating-point loads and stores. */
4996 if (size == 8 && ISA_HAS_LDC1_SDC1)
4997 {
4998 if (FP_REG_RTX_P (dest) && MEM_P (src))
4999 return false;
5000 if (FP_REG_RTX_P (src) && MEM_P (dest))
5001 return false;
5002 }
5003
5004 /* Check if MSA moves need splitting. */
5005 if (MSA_SUPPORTED_MODE_P (GET_MODE (dest)))
5006 return mips_split_128bit_move_p (dest, src);
5007
5008 /* Otherwise split all multiword moves. */
5009 return size > UNITS_PER_WORD;
5010 }
5011
5012 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
5013 SPLIT_TYPE describes the split condition. INSN is the insn being
5014 split, if we know it, NULL otherwise. */
5015
5016 void
5017 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type, rtx insn_)
5018 {
5019 rtx low_dest;
5020
5021 gcc_checking_assert (mips_split_move_p (dest, src, split_type));
5022 if (MSA_SUPPORTED_MODE_P (GET_MODE (dest)))
5023 mips_split_128bit_move (dest, src);
5024 else if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
5025 {
5026 if (!TARGET_64BIT && GET_MODE (dest) == DImode)
5027 emit_insn (gen_move_doubleword_fprdi (dest, src));
5028 else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
5029 emit_insn (gen_move_doubleword_fprdf (dest, src));
5030 else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
5031 emit_insn (gen_move_doubleword_fprv2sf (dest, src));
5032 else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
5033 emit_insn (gen_move_doubleword_fprv2si (dest, src));
5034 else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
5035 emit_insn (gen_move_doubleword_fprv4hi (dest, src));
5036 else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
5037 emit_insn (gen_move_doubleword_fprv8qi (dest, src));
5038 else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
5039 emit_insn (gen_move_doubleword_fprtf (dest, src));
5040 else
5041 gcc_unreachable ();
5042 }
5043 else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
5044 {
5045 low_dest = mips_subword (dest, false);
5046 mips_emit_move (low_dest, mips_subword (src, false));
5047 if (TARGET_64BIT)
5048 emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
5049 else
5050 emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
5051 }
5052 else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
5053 {
5054 mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
5055 if (TARGET_64BIT)
5056 emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
5057 else
5058 emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
5059 }
5060 else
5061 {
5062 /* The operation can be split into two normal moves. Decide in
5063 which order to do them. */
5064 low_dest = mips_subword (dest, false);
5065 if (REG_P (low_dest)
5066 && reg_overlap_mentioned_p (low_dest, src))
5067 {
5068 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
5069 mips_emit_move (low_dest, mips_subword (src, false));
5070 }
5071 else
5072 {
5073 mips_emit_move (low_dest, mips_subword (src, false));
5074 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
5075 }
5076 }
5077
5078 /* This is a hack. See if the next insn uses DEST and if so, see if we
5079 can forward SRC for DEST. This is most useful if the next insn is a
5080 simple store. */
5081 rtx_insn *insn = (rtx_insn *)insn_;
5082 struct mips_address_info addr = {};
5083 if (insn)
5084 {
5085 rtx_insn *next = next_nonnote_nondebug_insn_bb (insn);
5086 if (next)
5087 {
5088 rtx set = single_set (next);
5089 if (set && SET_SRC (set) == dest)
5090 {
5091 if (MEM_P (src))
5092 {
5093 rtx tmp = XEXP (src, 0);
5094 mips_classify_address (&addr, tmp, GET_MODE (tmp), true);
5095 if (addr.reg && !reg_overlap_mentioned_p (dest, addr.reg))
5096 validate_change (next, &SET_SRC (set), src, false);
5097 }
5098 else
5099 validate_change (next, &SET_SRC (set), src, false);
5100 }
5101 }
5102 }
5103 }
5104
5105 /* Return the split type for instruction INSN. */
5106
5107 static enum mips_split_type
5108 mips_insn_split_type (rtx insn)
5109 {
5110 basic_block bb = BLOCK_FOR_INSN (insn);
5111 if (bb)
5112 {
5113 if (optimize_bb_for_speed_p (bb))
5114 return SPLIT_FOR_SPEED;
5115 else
5116 return SPLIT_FOR_SIZE;
5117 }
5118 /* Once CFG information has been removed, we should trust the optimization
5119 decisions made by previous passes and only split where necessary. */
5120 return SPLIT_IF_NECESSARY;
5121 }
5122
5123 /* Return true if a 128-bit move from SRC to DEST should be split. */
5124
5125 bool
5126 mips_split_128bit_move_p (rtx dest, rtx src)
5127 {
5128 /* MSA-to-MSA moves can be done in a single instruction. */
5129 if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
5130 return false;
5131
5132 /* Check for MSA loads and stores. */
5133 if (FP_REG_RTX_P (dest) && MEM_P (src))
5134 return false;
5135 if (FP_REG_RTX_P (src) && MEM_P (dest))
5136 return false;
5137
5138 /* Check for MSA set to an immediate const vector with valid replicated
5139 element. */
5140 if (FP_REG_RTX_P (dest)
5141 && mips_const_vector_same_int_p (src, GET_MODE (src), -512, 511))
5142 return false;
5143
5144 /* Check for MSA load zero immediate. */
5145 if (FP_REG_RTX_P (dest) && src == CONST0_RTX (GET_MODE (src)))
5146 return false;
5147
5148 return true;
5149 }
5150
5151 /* Split a 128-bit move from SRC to DEST. */
5152
5153 void
5154 mips_split_128bit_move (rtx dest, rtx src)
5155 {
5156 int byte, index;
5157 rtx low_dest, low_src, d, s;
5158
5159 if (FP_REG_RTX_P (dest))
5160 {
5161 gcc_assert (!MEM_P (src));
5162
5163 rtx new_dest = dest;
5164 if (!TARGET_64BIT)
5165 {
5166 if (GET_MODE (dest) != V4SImode)
5167 new_dest = simplify_gen_subreg (V4SImode, dest, GET_MODE (dest), 0);
5168 }
5169 else
5170 {
5171 if (GET_MODE (dest) != V2DImode)
5172 new_dest = simplify_gen_subreg (V2DImode, dest, GET_MODE (dest), 0);
5173 }
5174
5175 for (byte = 0, index = 0; byte < GET_MODE_SIZE (TImode);
5176 byte += UNITS_PER_WORD, index++)
5177 {
5178 s = mips_subword_at_byte (src, byte);
5179 if (!TARGET_64BIT)
5180 emit_insn (gen_msa_insert_w (new_dest, s, new_dest,
5181 GEN_INT (1 << index)));
5182 else
5183 emit_insn (gen_msa_insert_d (new_dest, s, new_dest,
5184 GEN_INT (1 << index)));
5185 }
5186 }
5187 else if (FP_REG_RTX_P (src))
5188 {
5189 gcc_assert (!MEM_P (dest));
5190
5191 rtx new_src = src;
5192 if (!TARGET_64BIT)
5193 {
5194 if (GET_MODE (src) != V4SImode)
5195 new_src = simplify_gen_subreg (V4SImode, src, GET_MODE (src), 0);
5196 }
5197 else
5198 {
5199 if (GET_MODE (src) != V2DImode)
5200 new_src = simplify_gen_subreg (V2DImode, src, GET_MODE (src), 0);
5201 }
5202
5203 for (byte = 0, index = 0; byte < GET_MODE_SIZE (TImode);
5204 byte += UNITS_PER_WORD, index++)
5205 {
5206 d = mips_subword_at_byte (dest, byte);
5207 if (!TARGET_64BIT)
5208 emit_insn (gen_msa_copy_s_w (d, new_src, GEN_INT (index)));
5209 else
5210 emit_insn (gen_msa_copy_s_d (d, new_src, GEN_INT (index)));
5211 }
5212 }
5213 else
5214 {
5215 low_dest = mips_subword_at_byte (dest, 0);
5216 low_src = mips_subword_at_byte (src, 0);
5217 gcc_assert (REG_P (low_dest) && REG_P (low_src));
5218 /* Make sure the source register is not written before reading. */
5219 if (REGNO (low_dest) <= REGNO (low_src))
5220 {
5221 for (byte = 0; byte < GET_MODE_SIZE (TImode);
5222 byte += UNITS_PER_WORD)
5223 {
5224 d = mips_subword_at_byte (dest, byte);
5225 s = mips_subword_at_byte (src, byte);
5226 mips_emit_move (d, s);
5227 }
5228 }
5229 else
5230 {
5231 for (byte = GET_MODE_SIZE (TImode) - UNITS_PER_WORD; byte >= 0;
5232 byte -= UNITS_PER_WORD)
5233 {
5234 d = mips_subword_at_byte (dest, byte);
5235 s = mips_subword_at_byte (src, byte);
5236 mips_emit_move (d, s);
5237 }
5238 }
5239 }
5240 }
5241
5242 /* Split a COPY_S.D with operands DEST, SRC and INDEX. GEN is a function
5243 used to generate subregs. */
5244
5245 void
5246 mips_split_msa_copy_d (rtx dest, rtx src, rtx index,
5247 rtx (*gen_fn)(rtx, rtx, rtx))
5248 {
5249 gcc_assert ((GET_MODE (src) == V2DImode && GET_MODE (dest) == DImode)
5250 || (GET_MODE (src) == V2DFmode && GET_MODE (dest) == DFmode));
5251
5252 /* Note that low is always from the lower index, and high is always
5253 from the higher index. */
5254 rtx low = mips_subword (dest, false);
5255 rtx high = mips_subword (dest, true);
5256 rtx new_src = simplify_gen_subreg (V4SImode, src, GET_MODE (src), 0);
5257
5258 emit_insn (gen_fn (low, new_src, GEN_INT (INTVAL (index) * 2)));
5259 emit_insn (gen_fn (high, new_src, GEN_INT (INTVAL (index) * 2 + 1)));
5260 }
5261
5262 /* Split a INSERT.D with operand DEST, SRC1.INDEX and SRC2. */
5263
5264 void
5265 mips_split_msa_insert_d (rtx dest, rtx src1, rtx index, rtx src2)
5266 {
5267 int i;
5268 gcc_assert (GET_MODE (dest) == GET_MODE (src1));
5269 gcc_assert ((GET_MODE (dest) == V2DImode
5270 && (GET_MODE (src2) == DImode || src2 == const0_rtx))
5271 || (GET_MODE (dest) == V2DFmode && GET_MODE (src2) == DFmode));
5272
5273 /* Note that low is always from the lower index, and high is always
5274 from the higher index. */
5275 rtx low = mips_subword (src2, false);
5276 rtx high = mips_subword (src2, true);
5277 rtx new_dest = simplify_gen_subreg (V4SImode, dest, GET_MODE (dest), 0);
5278 rtx new_src1 = simplify_gen_subreg (V4SImode, src1, GET_MODE (src1), 0);
5279 i = exact_log2 (INTVAL (index));
5280 gcc_assert (i != -1);
5281
5282 emit_insn (gen_msa_insert_w (new_dest, low, new_src1,
5283 GEN_INT (1 << (i * 2))));
5284 emit_insn (gen_msa_insert_w (new_dest, high, new_dest,
5285 GEN_INT (1 << (i * 2 + 1))));
5286 }
5287
5288 /* Split FILL.D. */
5289
5290 void
5291 mips_split_msa_fill_d (rtx dest, rtx src)
5292 {
5293 gcc_assert ((GET_MODE (dest) == V2DImode
5294 && (GET_MODE (src) == DImode || src == const0_rtx))
5295 || (GET_MODE (dest) == V2DFmode && GET_MODE (src) == DFmode));
5296
5297 /* Note that low is always from the lower index, and high is always
5298 from the higher index. */
5299 rtx low, high;
5300 if (src == const0_rtx)
5301 {
5302 low = src;
5303 high = src;
5304 }
5305 else
5306 {
5307 low = mips_subword (src, false);
5308 high = mips_subword (src, true);
5309 }
5310 rtx new_dest = simplify_gen_subreg (V4SImode, dest, GET_MODE (dest), 0);
5311 emit_insn (gen_msa_fill_w (new_dest, low));
5312 emit_insn (gen_msa_insert_w (new_dest, high, new_dest, GEN_INT (1 << 1)));
5313 emit_insn (gen_msa_insert_w (new_dest, high, new_dest, GEN_INT (1 << 3)));
5314 }
5315
5316 /* Return true if a move from SRC to DEST in INSN should be split. */
5317
5318 bool
5319 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
5320 {
5321 return mips_split_move_p (dest, src, mips_insn_split_type (insn));
5322 }
5323
5324 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
5325 holds. */
5326
5327 void
5328 mips_split_move_insn (rtx dest, rtx src, rtx insn)
5329 {
5330 mips_split_move (dest, src, mips_insn_split_type (insn), insn);
5331 }
5332
5333 /* Return the appropriate instructions to move SRC into DEST. Assume
5334 that SRC is operand 1 and DEST is operand 0. */
5335
5336 const char *
5337 mips_output_move (rtx dest, rtx src)
5338 {
5339 enum rtx_code dest_code = GET_CODE (dest);
5340 enum rtx_code src_code = GET_CODE (src);
5341 machine_mode mode = GET_MODE (dest);
5342 bool dbl_p = (GET_MODE_SIZE (mode) == 8);
5343 bool msa_p = MSA_SUPPORTED_MODE_P (mode);
5344 enum mips_symbol_type symbol_type;
5345
5346 if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
5347 return "#";
5348
5349 if (msa_p
5350 && dest_code == REG && FP_REG_P (REGNO (dest))
5351 && src_code == CONST_VECTOR
5352 && CONST_INT_P (CONST_VECTOR_ELT (src, 0)))
5353 {
5354 gcc_assert (mips_const_vector_same_int_p (src, mode, -512, 511));
5355 return "ldi.%v0\t%w0,%E1";
5356 }
5357
5358 if ((src_code == REG && GP_REG_P (REGNO (src)))
5359 || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
5360 {
5361 if (dest_code == REG)
5362 {
5363 if (GP_REG_P (REGNO (dest)))
5364 return "move\t%0,%z1";
5365
5366 if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
5367 {
5368 if (ISA_HAS_DSP_MULT)
5369 return "mult\t%q0,%.,%.";
5370 else
5371 return "mult\t%.,%.";
5372 }
5373
5374 /* Moves to HI are handled by special .md insns. */
5375 if (REGNO (dest) == LO_REGNUM)
5376 return "mtlo\t%z1";
5377
5378 if (DSP_ACC_REG_P (REGNO (dest)))
5379 {
5380 static char retval[] = "mt__\t%z1,%q0";
5381
5382 retval[2] = reg_names[REGNO (dest)][4];
5383 retval[3] = reg_names[REGNO (dest)][5];
5384 return retval;
5385 }
5386
5387 if (FP_REG_P (REGNO (dest)))
5388 {
5389 if (msa_p)
5390 {
5391 gcc_assert (src == CONST0_RTX (GET_MODE (src)));
5392 return "ldi.%v0\t%w0,0";
5393 }
5394
5395 return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
5396 }
5397
5398 if (ALL_COP_REG_P (REGNO (dest)))
5399 {
5400 static char retval[] = "dmtc_\t%z1,%0";
5401
5402 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
5403 return dbl_p ? retval : retval + 1;
5404 }
5405 }
5406 if (dest_code == MEM)
5407 switch (GET_MODE_SIZE (mode))
5408 {
5409 case 1: return "sb\t%z1,%0";
5410 case 2: return "sh\t%z1,%0";
5411 case 4: return "sw\t%z1,%0";
5412 case 8: return "sd\t%z1,%0";
5413 default: gcc_unreachable ();
5414 }
5415 }
5416 if (dest_code == REG && GP_REG_P (REGNO (dest)))
5417 {
5418 if (src_code == REG)
5419 {
5420 /* Moves from HI are handled by special .md insns. */
5421 if (REGNO (src) == LO_REGNUM)
5422 {
5423 /* When generating VR4120 or VR4130 code, we use MACC and
5424 DMACC instead of MFLO. This avoids both the normal
5425 MIPS III HI/LO hazards and the errata related to
5426 -mfix-vr4130. */
5427 if (ISA_HAS_MACCHI)
5428 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
5429 return "mflo\t%0";
5430 }
5431
5432 if (DSP_ACC_REG_P (REGNO (src)))
5433 {
5434 static char retval[] = "mf__\t%0,%q1";
5435
5436 retval[2] = reg_names[REGNO (src)][4];
5437 retval[3] = reg_names[REGNO (src)][5];
5438 return retval;
5439 }
5440
5441 if (FP_REG_P (REGNO (src)))
5442 {
5443 gcc_assert (!msa_p);
5444 return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
5445 }
5446
5447 if (ALL_COP_REG_P (REGNO (src)))
5448 {
5449 static char retval[] = "dmfc_\t%0,%1";
5450
5451 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
5452 return dbl_p ? retval : retval + 1;
5453 }
5454 }
5455
5456 if (src_code == MEM)
5457 switch (GET_MODE_SIZE (mode))
5458 {
5459 case 1: return "lbu\t%0,%1";
5460 case 2: return "lhu\t%0,%1";
5461 case 4: return "lw\t%0,%1";
5462 case 8: return "ld\t%0,%1";
5463 default: gcc_unreachable ();
5464 }
5465
5466 if (src_code == CONST_INT)
5467 {
5468 /* Don't use the X format for the operand itself, because that
5469 will give out-of-range numbers for 64-bit hosts and 32-bit
5470 targets. */
5471 if (!TARGET_MIPS16)
5472 return "li\t%0,%1\t\t\t# %X1";
5473
5474 if (ISA_HAS_MIPS16E2
5475 && LUI_INT (src)
5476 && !SMALL_OPERAND_UNSIGNED (INTVAL (src)))
5477 return "lui\t%0,%%hi(%1)\t\t\t# %X1";
5478
5479 if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
5480 return "li\t%0,%1";
5481
5482 if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
5483 return "#";
5484 }
5485
5486 if (src_code == HIGH)
5487 return (TARGET_MIPS16 && !ISA_HAS_MIPS16E2) ? "#" : "lui\t%0,%h1";
5488
5489 if (CONST_GP_P (src))
5490 return "move\t%0,%1";
5491
5492 if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
5493 && mips_lo_relocs[symbol_type] != 0)
5494 {
5495 /* A signed 16-bit constant formed by applying a relocation
5496 operator to a symbolic address. */
5497 gcc_assert (!mips_split_p[symbol_type]);
5498 return "li\t%0,%R1";
5499 }
5500
5501 if (symbolic_operand (src, VOIDmode))
5502 {
5503 gcc_assert (TARGET_MIPS16
5504 ? TARGET_MIPS16_TEXT_LOADS
5505 : !TARGET_EXPLICIT_RELOCS);
5506 return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
5507 }
5508 }
5509 if (src_code == REG && FP_REG_P (REGNO (src)))
5510 {
5511 if (dest_code == REG && FP_REG_P (REGNO (dest)))
5512 {
5513 if (GET_MODE (dest) == V2SFmode)
5514 return "mov.ps\t%0,%1";
5515 else if (msa_p)
5516 return "move.v\t%w0,%w1";
5517 else
5518 return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
5519 }
5520
5521 if (dest_code == MEM)
5522 {
5523 if (msa_p)
5524 return "st.%v1\t%w1,%0";
5525
5526 return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
5527 }
5528 }
5529 if (dest_code == REG && FP_REG_P (REGNO (dest)))
5530 {
5531 if (src_code == MEM)
5532 {
5533 if (msa_p)
5534 return "ld.%v0\t%w0,%1";
5535
5536 return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
5537 }
5538 }
5539 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
5540 {
5541 static char retval[] = "l_c_\t%0,%1";
5542
5543 retval[1] = (dbl_p ? 'd' : 'w');
5544 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
5545 return retval;
5546 }
5547 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
5548 {
5549 static char retval[] = "s_c_\t%1,%0";
5550
5551 retval[1] = (dbl_p ? 'd' : 'w');
5552 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
5553 return retval;
5554 }
5555 gcc_unreachable ();
5556 }
5557
5558 /* Return true if CMP1 is a suitable second operand for integer ordering
5559 test CODE. See also the *sCC patterns in mips.md. */
5560
5561 static bool
5562 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
5563 {
5564 switch (code)
5565 {
5566 case GT:
5567 case GTU:
5568 return reg_or_0_operand (cmp1, VOIDmode);
5569
5570 case GE:
5571 case GEU:
5572 return !TARGET_MIPS16 && cmp1 == const1_rtx;
5573
5574 case LT:
5575 case LTU:
5576 return arith_operand (cmp1, VOIDmode);
5577
5578 case LE:
5579 return sle_operand (cmp1, VOIDmode);
5580
5581 case LEU:
5582 return sleu_operand (cmp1, VOIDmode);
5583
5584 default:
5585 gcc_unreachable ();
5586 }
5587 }
5588
5589 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
5590 integer ordering test *CODE, or if an equivalent combination can
5591 be formed by adjusting *CODE and *CMP1. When returning true, update
5592 *CODE and *CMP1 with the chosen code and operand, otherwise leave
5593 them alone. */
5594
5595 static bool
5596 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
5597 machine_mode mode)
5598 {
5599 HOST_WIDE_INT plus_one;
5600
5601 if (mips_int_order_operand_ok_p (*code, *cmp1))
5602 return true;
5603
5604 if (CONST_INT_P (*cmp1))
5605 switch (*code)
5606 {
5607 case LE:
5608 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
5609 if (INTVAL (*cmp1) < plus_one)
5610 {
5611 *code = LT;
5612 *cmp1 = force_reg (mode, GEN_INT (plus_one));
5613 return true;
5614 }
5615 break;
5616
5617 case LEU:
5618 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
5619 if (plus_one != 0)
5620 {
5621 *code = LTU;
5622 *cmp1 = force_reg (mode, GEN_INT (plus_one));
5623 return true;
5624 }
5625 break;
5626
5627 default:
5628 break;
5629 }
5630 return false;
5631 }
5632
5633 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
5634 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
5635 is nonnull, it's OK to set TARGET to the inverse of the result and
5636 flip *INVERT_PTR instead. */
5637
5638 static void
5639 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
5640 rtx target, rtx cmp0, rtx cmp1)
5641 {
5642 machine_mode mode;
5643
5644 /* First see if there is a MIPS instruction that can do this operation.
5645 If not, try doing the same for the inverse operation. If that also
5646 fails, force CMP1 into a register and try again. */
5647 mode = GET_MODE (cmp0);
5648 if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
5649 mips_emit_binary (code, target, cmp0, cmp1);
5650 else
5651 {
5652 enum rtx_code inv_code = reverse_condition (code);
5653 if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
5654 {
5655 cmp1 = force_reg (mode, cmp1);
5656 mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
5657 }
5658 else if (invert_ptr == 0)
5659 {
5660 rtx inv_target;
5661
5662 inv_target = mips_force_binary (GET_MODE (target),
5663 inv_code, cmp0, cmp1);
5664 mips_emit_binary (XOR, target, inv_target, const1_rtx);
5665 }
5666 else
5667 {
5668 *invert_ptr = !*invert_ptr;
5669 mips_emit_binary (inv_code, target, cmp0, cmp1);
5670 }
5671 }
5672 }
5673
5674 /* Return a register that is zero iff CMP0 and CMP1 are equal.
5675 The register will have the same mode as CMP0. */
5676
5677 static rtx
5678 mips_zero_if_equal (rtx cmp0, rtx cmp1)
5679 {
5680 if (cmp1 == const0_rtx)
5681 return cmp0;
5682
5683 if (uns_arith_operand (cmp1, VOIDmode))
5684 return expand_binop (GET_MODE (cmp0), xor_optab,
5685 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
5686
5687 return expand_binop (GET_MODE (cmp0), sub_optab,
5688 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
5689 }
5690
5691 /* Convert *CODE into a code that can be used in a floating-point
5692 scc instruction (C.cond.fmt). Return true if the values of
5693 the condition code registers will be inverted, with 0 indicating
5694 that the condition holds. */
5695
5696 static bool
5697 mips_reversed_fp_cond (enum rtx_code *code)
5698 {
5699 switch (*code)
5700 {
5701 case NE:
5702 case LTGT:
5703 case ORDERED:
5704 *code = reverse_condition_maybe_unordered (*code);
5705 return true;
5706
5707 default:
5708 return false;
5709 }
5710 }
5711
5712 /* Allocate a floating-point condition-code register of mode MODE.
5713
5714 These condition code registers are used for certain kinds
5715 of compound operation, such as compare and branches, vconds,
5716 and built-in functions. At expand time, their use is entirely
5717 controlled by MIPS-specific code and is entirely internal
5718 to these compound operations.
5719
5720 We could (and did in the past) expose condition-code values
5721 as pseudo registers and leave the register allocator to pick
5722 appropriate registers. The problem is that it is not practically
5723 possible for the rtl optimizers to guarantee that no spills will
5724 be needed, even when AVOID_CCMODE_COPIES is defined. We would
5725 therefore need spill and reload sequences to handle the worst case.
5726
5727 Although such sequences do exist, they are very expensive and are
5728 not something we'd want to use. This is especially true of CCV2 and
5729 CCV4, where all the shuffling would greatly outweigh whatever benefit
5730 the vectorization itself provides.
5731
5732 The main benefit of having more than one condition-code register
5733 is to allow the pipelining of operations, especially those involving
5734 comparisons and conditional moves. We don't really expect the
5735 registers to be live for long periods, and certainly never want
5736 them to be live across calls.
5737
5738 Also, there should be no penalty attached to using all the available
5739 registers. They are simply bits in the same underlying FPU control
5740 register.
5741
5742 We therefore expose the hardware registers from the outset and use
5743 a simple round-robin allocation scheme. */
5744
5745 static rtx
5746 mips_allocate_fcc (machine_mode mode)
5747 {
5748 unsigned int regno, count;
5749
5750 gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
5751
5752 if (mode == CCmode || mode == CCEmode)
5753 count = 1;
5754 else if (mode == CCV2mode)
5755 count = 2;
5756 else if (mode == CCV4mode)
5757 count = 4;
5758 else
5759 gcc_unreachable ();
5760
5761 cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
5762 if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
5763 cfun->machine->next_fcc = 0;
5764 regno = ST_REG_FIRST + cfun->machine->next_fcc;
5765 cfun->machine->next_fcc += count;
5766 return gen_rtx_REG (mode, regno);
5767 }
5768
5769 /* Convert a comparison into something that can be used in a branch or
5770 conditional move. On entry, *OP0 and *OP1 are the values being
5771 compared and *CODE is the code used to compare them.
5772
5773 Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
5774 If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
5775 otherwise any standard branch condition can be used. The standard branch
5776 conditions are:
5777
5778 - EQ or NE between two registers.
5779 - any comparison between a register and zero.
5780 - if compact branches are available then any condition is valid. */
5781
5782 static void
5783 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
5784 {
5785 rtx cmp_op0 = *op0;
5786 rtx cmp_op1 = *op1;
5787
5788 if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
5789 {
5790 if (!need_eq_ne_p && *op1 == const0_rtx)
5791 ;
5792 else if (*code == EQ || *code == NE)
5793 {
5794 if (need_eq_ne_p)
5795 {
5796 *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
5797 *op1 = const0_rtx;
5798 }
5799 else
5800 *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
5801 }
5802 else if (!need_eq_ne_p && TARGET_CB_MAYBE)
5803 {
5804 bool swap = false;
5805 switch (*code)
5806 {
5807 case LE:
5808 swap = true;
5809 *code = GE;
5810 break;
5811 case GT:
5812 swap = true;
5813 *code = LT;
5814 break;
5815 case LEU:
5816 swap = true;
5817 *code = GEU;
5818 break;
5819 case GTU:
5820 swap = true;
5821 *code = LTU;
5822 break;
5823 case GE:
5824 case LT:
5825 case GEU:
5826 case LTU:
5827 /* Do nothing. */
5828 break;
5829 default:
5830 gcc_unreachable ();
5831 }
5832 *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
5833 if (swap)
5834 {
5835 rtx tmp = *op1;
5836 *op1 = *op0;
5837 *op0 = tmp;
5838 }
5839 }
5840 else
5841 {
5842 /* The comparison needs a separate scc instruction. Store the
5843 result of the scc in *OP0 and compare it against zero. */
5844 bool invert = false;
5845 *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
5846 mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
5847 *code = (invert ? EQ : NE);
5848 *op1 = const0_rtx;
5849 }
5850 }
5851 else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
5852 {
5853 *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
5854 mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
5855 *code = NE;
5856 *op1 = const0_rtx;
5857 }
5858 else
5859 {
5860 enum rtx_code cmp_code;
5861
5862 /* Floating-point tests use a separate C.cond.fmt or CMP.cond.fmt
5863 comparison to set a register. The branch or conditional move will
5864 then compare that register against zero.
5865
5866 Set CMP_CODE to the code of the comparison instruction and
5867 *CODE to the code that the branch or move should use. */
5868 cmp_code = *code;
5869 if (ISA_HAS_CCF)
5870 {
5871 /* All FP conditions can be implemented directly with CMP.cond.fmt
5872 or by reversing the operands. */
5873 *code = NE;
5874 *op0 = gen_reg_rtx (CCFmode);
5875 }
5876 else
5877 {
5878 /* Three FP conditions cannot be implemented by reversing the
5879 operands for C.cond.fmt, instead a reversed condition code is
5880 required and a test for false. */
5881 machine_mode ccmode = CCmode;
5882 switch (*code)
5883 {
5884 case LTGT:
5885 case LT:
5886 case LE:
5887 ccmode = CCEmode;
5888 break;
5889 default:
5890 break;
5891 }
5892 *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
5893 if (ISA_HAS_8CC)
5894 *op0 = mips_allocate_fcc (ccmode);
5895 else
5896 *op0 = gen_rtx_REG (ccmode, FPSW_REGNUM);
5897 }
5898
5899 *op1 = const0_rtx;
5900 mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
5901 }
5902 }
5903
5904
5905 const char *
5906 mips_output_compare (const char *fpcmp, const char *fcond,
5907 const char *fmt, const char *fpcc_mode, bool swap)
5908 {
5909 const char *fc = fcond;
5910
5911 if (ISA_HAS_CCF)
5912 {
5913 /* c.lt.fmt is signaling, while cmp.lt.fmt is quiet. */
5914 if (strcmp (fcond, "lt") == 0)
5915 fc = "slt";
5916 else if (strcmp (fcond, "le") == 0)
5917 fc = "sle";
5918 }
5919 else if (strcmp (fpcc_mode, "cce") == 0)
5920 {
5921 /* It was LTGT, while we have only inverse one. It was then converted
5922 to UNEQ by mips_reversed_fp_cond, and we used CCEmode to mark it.
5923 Lets convert it back to ngl now. */
5924 if (strcmp (fcond, "ueq") == 0)
5925 fc = "ngl";
5926 }
5927 if (swap)
5928 return concat(fpcmp, ".", fc, ".", fmt, "\t%Z0%2,%1", NULL);
5929 return concat(fpcmp, ".", fc, ".", fmt, "\t%Z0%1,%2", NULL);
5930 }
5931
5932
5933 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
5934 and OPERAND[3]. Store the result in OPERANDS[0].
5935
5936 On 64-bit targets, the mode of the comparison and target will always be
5937 SImode, thus possibly narrower than that of the comparison's operands. */
5938
5939 void
5940 mips_expand_scc (rtx operands[])
5941 {
5942 rtx target = operands[0];
5943 enum rtx_code code = GET_CODE (operands[1]);
5944 rtx op0 = operands[2];
5945 rtx op1 = operands[3];
5946
5947 gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
5948
5949 if (code == EQ || code == NE)
5950 {
5951 if (ISA_HAS_SEQ_SNE
5952 && reg_imm10_operand (op1, GET_MODE (op1)))
5953 mips_emit_binary (code, target, op0, op1);
5954 else
5955 {
5956 rtx zie = mips_zero_if_equal (op0, op1);
5957 mips_emit_binary (code, target, zie, const0_rtx);
5958 }
5959 }
5960 else
5961 mips_emit_int_order_test (code, 0, target, op0, op1);
5962 }
5963
5964 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
5965 CODE and jump to OPERANDS[3] if the condition holds. */
5966
5967 void
5968 mips_expand_conditional_branch (rtx *operands)
5969 {
5970 enum rtx_code code = GET_CODE (operands[0]);
5971 rtx op0 = operands[1];
5972 rtx op1 = operands[2];
5973 rtx condition;
5974
5975 mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
5976 condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
5977 emit_jump_insn (gen_condjump (condition, operands[3]));
5978 }
5979
5980 /* Implement:
5981
5982 (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
5983 (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS)) */
5984
5985 void
5986 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
5987 enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
5988 {
5989 rtx cmp_result;
5990 bool reversed_p;
5991
5992 reversed_p = mips_reversed_fp_cond (&cond);
5993 cmp_result = mips_allocate_fcc (CCV2mode);
5994 emit_insn (gen_scc_ps (cmp_result,
5995 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
5996 if (reversed_p)
5997 emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
5998 cmp_result));
5999 else
6000 emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
6001 cmp_result));
6002 }
6003
6004 /* Perform the comparison in OPERANDS[1]. Move OPERANDS[2] into OPERANDS[0]
6005 if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0]. */
6006
6007 void
6008 mips_expand_conditional_move (rtx *operands)
6009 {
6010 rtx cond;
6011 enum rtx_code code = GET_CODE (operands[1]);
6012 rtx op0 = XEXP (operands[1], 0);
6013 rtx op1 = XEXP (operands[1], 1);
6014
6015 mips_emit_compare (&code, &op0, &op1, true);
6016 cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
6017
6018 /* There is no direct support for general conditional GP move involving
6019 two registers using SEL. */
6020 if (ISA_HAS_SEL
6021 && INTEGRAL_MODE_P (GET_MODE (operands[2]))
6022 && register_operand (operands[2], VOIDmode)
6023 && register_operand (operands[3], VOIDmode))
6024 {
6025 machine_mode mode = GET_MODE (operands[0]);
6026 rtx temp = gen_reg_rtx (mode);
6027 rtx temp2 = gen_reg_rtx (mode);
6028
6029 emit_insn (gen_rtx_SET (temp,
6030 gen_rtx_IF_THEN_ELSE (mode, cond,
6031 operands[2], const0_rtx)));
6032
6033 /* Flip the test for the second operand. */
6034 cond = gen_rtx_fmt_ee ((code == EQ) ? NE : EQ, GET_MODE (op0), op0, op1);
6035
6036 emit_insn (gen_rtx_SET (temp2,
6037 gen_rtx_IF_THEN_ELSE (mode, cond,
6038 operands[3], const0_rtx)));
6039
6040 /* Merge the two results, at least one is guaranteed to be zero. */
6041 emit_insn (gen_rtx_SET (operands[0], gen_rtx_IOR (mode, temp, temp2)));
6042 }
6043 else
6044 {
6045 if (FLOAT_MODE_P (GET_MODE (operands[2])) && !ISA_HAS_SEL)
6046 {
6047 operands[2] = force_reg (GET_MODE (operands[0]), operands[2]);
6048 operands[3] = force_reg (GET_MODE (operands[0]), operands[3]);
6049 }
6050
6051 emit_insn (gen_rtx_SET (operands[0],
6052 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
6053 operands[2], operands[3])));
6054 }
6055 }
6056
6057 /* Perform the comparison in COMPARISON, then trap if the condition holds. */
6058
6059 void
6060 mips_expand_conditional_trap (rtx comparison)
6061 {
6062 rtx op0, op1;
6063 machine_mode mode;
6064 enum rtx_code code;
6065
6066 /* MIPS conditional trap instructions don't have GT or LE flavors,
6067 so we must swap the operands and convert to LT and GE respectively. */
6068 code = GET_CODE (comparison);
6069 switch (code)
6070 {
6071 case GT:
6072 case LE:
6073 case GTU:
6074 case LEU:
6075 code = swap_condition (code);
6076 op0 = XEXP (comparison, 1);
6077 op1 = XEXP (comparison, 0);
6078 break;
6079
6080 default:
6081 op0 = XEXP (comparison, 0);
6082 op1 = XEXP (comparison, 1);
6083 break;
6084 }
6085
6086 mode = GET_MODE (XEXP (comparison, 0));
6087 op0 = force_reg (mode, op0);
6088 if (!(ISA_HAS_COND_TRAPI
6089 ? arith_operand (op1, mode)
6090 : reg_or_0_operand (op1, mode)))
6091 op1 = force_reg (mode, op1);
6092
6093 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
6094 gen_rtx_fmt_ee (code, mode, op0, op1),
6095 const0_rtx));
6096 }
6097
6098 /* Initialize *CUM for a call to a function of type FNTYPE. */
6099
6100 void
6101 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
6102 {
6103 memset (cum, 0, sizeof (*cum));
6104 cum->prototype = (fntype && prototype_p (fntype));
6105 cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
6106 }
6107
6108 /* Fill INFO with information about a single argument. CUM is the
6109 cumulative state for earlier arguments. MODE is the mode of this
6110 argument and TYPE is its type (if known). NAMED is true if this
6111 is a named (fixed) argument rather than a variable one. */
6112
6113 static void
6114 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
6115 machine_mode mode, const_tree type, bool named)
6116 {
6117 bool doubleword_aligned_p;
6118 unsigned int num_bytes, num_words, max_regs;
6119
6120 /* Work out the size of the argument. */
6121 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
6122 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6123
6124 /* Decide whether it should go in a floating-point register, assuming
6125 one is free. Later code checks for availability.
6126
6127 The checks against UNITS_PER_FPVALUE handle the soft-float and
6128 single-float cases. */
6129 switch (mips_abi)
6130 {
6131 case ABI_EABI:
6132 /* The EABI conventions have traditionally been defined in terms
6133 of TYPE_MODE, regardless of the actual type. */
6134 info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
6135 || mode == V2SFmode)
6136 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
6137 break;
6138
6139 case ABI_32:
6140 case ABI_O64:
6141 /* Only leading floating-point scalars are passed in
6142 floating-point registers. We also handle vector floats the same
6143 say, which is OK because they are not covered by the standard ABI. */
6144 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
6145 info->fpr_p = (!cum->gp_reg_found
6146 && cum->arg_number < 2
6147 && (type == 0
6148 || SCALAR_FLOAT_TYPE_P (type)
6149 || VECTOR_FLOAT_TYPE_P (type))
6150 && (GET_MODE_CLASS (mode) == MODE_FLOAT
6151 || mode == V2SFmode)
6152 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
6153 break;
6154
6155 case ABI_N32:
6156 case ABI_64:
6157 /* Scalar, complex and vector floating-point types are passed in
6158 floating-point registers, as long as this is a named rather
6159 than a variable argument. */
6160 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
6161 info->fpr_p = (named
6162 && (type == 0 || FLOAT_TYPE_P (type))
6163 && (GET_MODE_CLASS (mode) == MODE_FLOAT
6164 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
6165 || mode == V2SFmode)
6166 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
6167
6168 /* ??? According to the ABI documentation, the real and imaginary
6169 parts of complex floats should be passed in individual registers.
6170 The real and imaginary parts of stack arguments are supposed
6171 to be contiguous and there should be an extra word of padding
6172 at the end.
6173
6174 This has two problems. First, it makes it impossible to use a
6175 single "void *" va_list type, since register and stack arguments
6176 are passed differently. (At the time of writing, MIPSpro cannot
6177 handle complex float varargs correctly.) Second, it's unclear
6178 what should happen when there is only one register free.
6179
6180 For now, we assume that named complex floats should go into FPRs
6181 if there are two FPRs free, otherwise they should be passed in the
6182 same way as a struct containing two floats. */
6183 if (info->fpr_p
6184 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
6185 && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
6186 {
6187 if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
6188 info->fpr_p = false;
6189 else
6190 num_words = 2;
6191 }
6192 break;
6193
6194 default:
6195 gcc_unreachable ();
6196 }
6197
6198 /* See whether the argument has doubleword alignment. */
6199 doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
6200 > BITS_PER_WORD);
6201
6202 /* Set REG_OFFSET to the register count we're interested in.
6203 The EABI allocates the floating-point registers separately,
6204 but the other ABIs allocate them like integer registers. */
6205 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
6206 ? cum->num_fprs
6207 : cum->num_gprs);
6208
6209 /* Advance to an even register if the argument is doubleword-aligned. */
6210 if (doubleword_aligned_p)
6211 info->reg_offset += info->reg_offset & 1;
6212
6213 /* Work out the offset of a stack argument. */
6214 info->stack_offset = cum->stack_words;
6215 if (doubleword_aligned_p)
6216 info->stack_offset += info->stack_offset & 1;
6217
6218 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
6219
6220 /* Partition the argument between registers and stack. */
6221 info->reg_words = MIN (num_words, max_regs);
6222 info->stack_words = num_words - info->reg_words;
6223 }
6224
6225 /* INFO describes a register argument that has the normal format for the
6226 argument's mode. Return the register it uses, assuming that FPRs are
6227 available if HARD_FLOAT_P. */
6228
6229 static unsigned int
6230 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
6231 {
6232 if (!info->fpr_p || !hard_float_p)
6233 return GP_ARG_FIRST + info->reg_offset;
6234 else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
6235 /* In o32, the second argument is always passed in $f14
6236 for TARGET_DOUBLE_FLOAT, regardless of whether the
6237 first argument was a word or doubleword. */
6238 return FP_ARG_FIRST + 2;
6239 else
6240 return FP_ARG_FIRST + info->reg_offset;
6241 }
6242
6243 /* Implement TARGET_STRICT_ARGUMENT_NAMING. */
6244
6245 static bool
6246 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
6247 {
6248 return !TARGET_OLDABI;
6249 }
6250
6251 /* Implement TARGET_FUNCTION_ARG. */
6252
6253 static rtx
6254 mips_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
6255 {
6256 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6257 struct mips_arg_info info;
6258
6259 /* We will be called with an end marker after the last argument
6260 has been seen. Whatever we return will be passed to the call expander.
6261 If we need a MIPS16 fp_code, return a REG with the code stored as
6262 the mode. */
6263 if (arg.end_marker_p ())
6264 {
6265 if (TARGET_MIPS16 && cum->fp_code != 0)
6266 return gen_rtx_REG ((machine_mode) cum->fp_code, 0);
6267 else
6268 return NULL;
6269 }
6270
6271 mips_get_arg_info (&info, cum, arg.mode, arg.type, arg.named);
6272
6273 /* Return straight away if the whole argument is passed on the stack. */
6274 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
6275 return NULL;
6276
6277 /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
6278 contains a double in its entirety, then that 64-bit chunk is passed
6279 in a floating-point register. */
6280 if (TARGET_NEWABI
6281 && TARGET_HARD_FLOAT
6282 && arg.named
6283 && arg.type != 0
6284 && TREE_CODE (arg.type) == RECORD_TYPE
6285 && TYPE_SIZE_UNIT (arg.type)
6286 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (arg.type)))
6287 {
6288 tree field;
6289
6290 /* First check to see if there is any such field. */
6291 for (field = TYPE_FIELDS (arg.type); field; field = DECL_CHAIN (field))
6292 if (TREE_CODE (field) == FIELD_DECL
6293 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
6294 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
6295 && tree_fits_shwi_p (bit_position (field))
6296 && int_bit_position (field) % BITS_PER_WORD == 0)
6297 break;
6298
6299 if (field != 0)
6300 {
6301 /* Now handle the special case by returning a PARALLEL
6302 indicating where each 64-bit chunk goes. INFO.REG_WORDS
6303 chunks are passed in registers. */
6304 unsigned int i;
6305 HOST_WIDE_INT bitpos;
6306 rtx ret;
6307
6308 /* assign_parms checks the mode of ENTRY_PARM, so we must
6309 use the actual mode here. */
6310 ret = gen_rtx_PARALLEL (arg.mode, rtvec_alloc (info.reg_words));
6311
6312 bitpos = 0;
6313 field = TYPE_FIELDS (arg.type);
6314 for (i = 0; i < info.reg_words; i++)
6315 {
6316 rtx reg;
6317 bool zero_width_field_abi_change = false;
6318
6319 for (; field; field = DECL_CHAIN (field))
6320 {
6321 if (TREE_CODE (field) != FIELD_DECL)
6322 continue;
6323
6324 /* Ignore zero-width fields. And, if the ignored
6325 field is not a C++ zero-width bit-field, it may be
6326 an ABI change. */
6327 if (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field))
6328 continue;
6329 if (DECL_SIZE (field)
6330 && integer_zerop (DECL_SIZE (field)))
6331 {
6332 zero_width_field_abi_change = true;
6333 continue;
6334 }
6335
6336 if (int_bit_position (field) >= bitpos)
6337 break;
6338 }
6339
6340 if (field
6341 && int_bit_position (field) == bitpos
6342 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
6343 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
6344 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
6345 else
6346 {
6347 reg = gen_rtx_REG (DImode,
6348 GP_ARG_FIRST + info.reg_offset + i);
6349 zero_width_field_abi_change = false;
6350 }
6351
6352 if (zero_width_field_abi_change && warn_psabi)
6353 {
6354 static unsigned last_reported_type_uid;
6355 unsigned uid = TYPE_UID (TYPE_MAIN_VARIANT (arg.type));
6356 if (uid != last_reported_type_uid)
6357 {
6358 static const char *url
6359 = CHANGES_ROOT_URL
6360 "gcc-12/changes.html#mips_zero_width_fields";
6361 inform (input_location,
6362 "the ABI for passing a value containing "
6363 "zero-width fields before an adjacent "
6364 "64-bit floating-point field was changed "
6365 "in GCC %{12.1%}", url);
6366 last_reported_type_uid = uid;
6367 }
6368 }
6369
6370 XVECEXP (ret, 0, i)
6371 = gen_rtx_EXPR_LIST (VOIDmode, reg,
6372 GEN_INT (bitpos / BITS_PER_UNIT));
6373
6374 bitpos += BITS_PER_WORD;
6375 }
6376 return ret;
6377 }
6378 }
6379
6380 /* Handle the n32/n64 conventions for passing complex floating-point
6381 arguments in FPR pairs. The real part goes in the lower register
6382 and the imaginary part goes in the upper register. */
6383 if (TARGET_NEWABI
6384 && info.fpr_p
6385 && GET_MODE_CLASS (arg.mode) == MODE_COMPLEX_FLOAT)
6386 {
6387 rtx real, imag;
6388 machine_mode inner;
6389 unsigned int regno;
6390
6391 inner = GET_MODE_INNER (arg.mode);
6392 regno = FP_ARG_FIRST + info.reg_offset;
6393 if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
6394 {
6395 /* Real part in registers, imaginary part on stack. */
6396 gcc_assert (info.stack_words == info.reg_words);
6397 return gen_rtx_REG (inner, regno);
6398 }
6399 else
6400 {
6401 gcc_assert (info.stack_words == 0);
6402 real = gen_rtx_EXPR_LIST (VOIDmode,
6403 gen_rtx_REG (inner, regno),
6404 const0_rtx);
6405 imag = gen_rtx_EXPR_LIST (VOIDmode,
6406 gen_rtx_REG (inner,
6407 regno + info.reg_words / 2),
6408 GEN_INT (GET_MODE_SIZE (inner)));
6409 return gen_rtx_PARALLEL (arg.mode, gen_rtvec (2, real, imag));
6410 }
6411 }
6412
6413 return gen_rtx_REG (arg.mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
6414 }
6415
6416 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
6417
6418 static void
6419 mips_function_arg_advance (cumulative_args_t cum_v,
6420 const function_arg_info &arg)
6421 {
6422 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6423 struct mips_arg_info info;
6424
6425 mips_get_arg_info (&info, cum, arg.mode, arg.type, arg.named);
6426
6427 if (!info.fpr_p)
6428 cum->gp_reg_found = true;
6429
6430 /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
6431 an explanation of what this code does. It assumes that we're using
6432 either the o32 or the o64 ABI, both of which pass at most 2 arguments
6433 in FPRs. */
6434 if (cum->arg_number < 2 && info.fpr_p)
6435 cum->fp_code += (arg.mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
6436
6437 /* Advance the register count. This has the effect of setting
6438 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
6439 argument required us to skip the final GPR and pass the whole
6440 argument on the stack. */
6441 if (mips_abi != ABI_EABI || !info.fpr_p)
6442 cum->num_gprs = info.reg_offset + info.reg_words;
6443 else if (info.reg_words > 0)
6444 cum->num_fprs += MAX_FPRS_PER_FMT;
6445
6446 /* Advance the stack word count. */
6447 if (info.stack_words > 0)
6448 cum->stack_words = info.stack_offset + info.stack_words;
6449
6450 cum->arg_number++;
6451 }
6452
6453 /* Implement TARGET_ARG_PARTIAL_BYTES. */
6454
6455 static int
6456 mips_arg_partial_bytes (cumulative_args_t cum, const function_arg_info &arg)
6457 {
6458 struct mips_arg_info info;
6459
6460 mips_get_arg_info (&info, get_cumulative_args (cum),
6461 arg.mode, arg.type, arg.named);
6462 return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
6463 }
6464
6465 /* Given MODE and TYPE of a function argument, return the alignment in
6466 bits.
6467 In case of typedef, alignment of its original type is
6468 used. */
6469
6470 static unsigned int
6471 mips_function_arg_alignment (machine_mode mode, const_tree type)
6472 {
6473 if (!type)
6474 return GET_MODE_ALIGNMENT (mode);
6475
6476 if (is_typedef_decl (TYPE_NAME (type)))
6477 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
6478
6479 return TYPE_ALIGN (type);
6480 }
6481
6482 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
6483 least PARM_BOUNDARY bits of alignment, but will be given anything up
6484 to STACK_BOUNDARY bits if the type requires it. */
6485
6486 static unsigned int
6487 mips_function_arg_boundary (machine_mode mode, const_tree type)
6488 {
6489 unsigned int alignment;
6490 alignment = mips_function_arg_alignment (mode, type);
6491
6492 if (alignment < PARM_BOUNDARY)
6493 alignment = PARM_BOUNDARY;
6494 if (alignment > STACK_BOUNDARY)
6495 alignment = STACK_BOUNDARY;
6496 return alignment;
6497 }
6498
6499 /* Implement TARGET_GET_RAW_RESULT_MODE and TARGET_GET_RAW_ARG_MODE. */
6500
6501 static fixed_size_mode
6502 mips_get_reg_raw_mode (int regno)
6503 {
6504 if (TARGET_FLOATXX && FP_REG_P (regno))
6505 return DFmode;
6506 return default_get_reg_raw_mode (regno);
6507 }
6508
6509 /* Implement TARGET_FUNCTION_ARG_PADDING; return PAD_UPWARD if the first
6510 byte of the stack slot has useful data, PAD_DOWNWARD if the last byte
6511 does. */
6512
6513 static pad_direction
6514 mips_function_arg_padding (machine_mode mode, const_tree type)
6515 {
6516 /* On little-endian targets, the first byte of every stack argument
6517 is passed in the first byte of the stack slot. */
6518 if (!BYTES_BIG_ENDIAN)
6519 return PAD_UPWARD;
6520
6521 /* Otherwise, integral types are padded downward: the last byte of a
6522 stack argument is passed in the last byte of the stack slot. */
6523 if (type != 0
6524 ? (INTEGRAL_TYPE_P (type)
6525 || POINTER_TYPE_P (type)
6526 || FIXED_POINT_TYPE_P (type))
6527 : (SCALAR_INT_MODE_P (mode)
6528 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
6529 return PAD_DOWNWARD;
6530
6531 /* Big-endian o64 pads floating-point arguments downward. */
6532 if (mips_abi == ABI_O64)
6533 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
6534 return PAD_DOWNWARD;
6535
6536 /* Other types are padded upward for o32, o64, n32 and n64. */
6537 if (mips_abi != ABI_EABI)
6538 return PAD_UPWARD;
6539
6540 /* Arguments smaller than a stack slot are padded downward. */
6541 if (mode != BLKmode
6542 ? GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY
6543 : int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT))
6544 return PAD_UPWARD;
6545
6546 return PAD_DOWNWARD;
6547 }
6548
6549 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
6550 if the least significant byte of the register has useful data. Return
6551 the opposite if the most significant byte does. */
6552
6553 bool
6554 mips_pad_reg_upward (machine_mode mode, tree type)
6555 {
6556 /* No shifting is required for floating-point arguments. */
6557 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
6558 return !BYTES_BIG_ENDIAN;
6559
6560 /* Otherwise, apply the same padding to register arguments as we do
6561 to stack arguments. */
6562 return mips_function_arg_padding (mode, type) == PAD_UPWARD;
6563 }
6564
6565 /* Return nonzero when an argument must be passed by reference. */
6566
6567 static bool
6568 mips_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
6569 {
6570 if (mips_abi == ABI_EABI)
6571 {
6572 int size;
6573
6574 /* ??? How should SCmode be handled? */
6575 if (arg.mode == DImode || arg.mode == DFmode
6576 || arg.mode == DQmode || arg.mode == UDQmode
6577 || arg.mode == DAmode || arg.mode == UDAmode)
6578 return 0;
6579
6580 size = arg.type_size_in_bytes ();
6581 return size == -1 || size > UNITS_PER_WORD;
6582 }
6583 else
6584 {
6585 /* If we have a variable-sized parameter, we have no choice. */
6586 return targetm.calls.must_pass_in_stack (arg);
6587 }
6588 }
6589
6590 /* Implement TARGET_CALLEE_COPIES. */
6591
6592 static bool
6593 mips_callee_copies (cumulative_args_t, const function_arg_info &arg)
6594 {
6595 return mips_abi == ABI_EABI && arg.named;
6596 }
6597
6598 /* See whether VALTYPE is a record whose fields should be returned in
6599 floating-point registers. If so, return the number of fields and
6600 list them in FIELDS (which should have two elements). Return 0
6601 otherwise.
6602
6603 For n32 & n64, a structure with one or two fields is returned in
6604 floating-point registers as long as every field has a floating-point
6605 type.
6606
6607 The C++ FE used to remove zero-width bit-fields in GCC 11 and earlier.
6608 To make a proper diagnostic, this function will set
6609 HAS_CXX_ZERO_WIDTH_BF to true once a C++ zero-width bit-field shows up,
6610 and then ignore it.
6611
6612 We had failed to ignore C++17 empty bases in GCC 7, 8, 9, 10, and 11.
6613 This caused an ABI incompatibility between C++14 and C++17. This is
6614 fixed now and to make a proper diagnostic, this function will set
6615 HAS_CXX17_EMPTY_BASE to true once a C++17 empty base shows up, and
6616 then ignore it.
6617
6618 The caller should use the value of HAS_CXX17_EMPTY_BASE and/or
6619 HAS_CXX_ZERO_WIDTH_BF to emit a proper -Wpsabi inform. */
6620
6621 static int
6622 mips_fpr_return_fields (const_tree valtype, tree *fields,
6623 bool *has_cxx_zero_width_bf,
6624 bool *has_cxx17_empty_base)
6625 {
6626 tree field;
6627 int i;
6628
6629 if (!TARGET_NEWABI)
6630 return 0;
6631
6632 if (TREE_CODE (valtype) != RECORD_TYPE)
6633 return 0;
6634
6635 i = 0;
6636 for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
6637 {
6638 if (TREE_CODE (field) != FIELD_DECL)
6639 continue;
6640
6641 if (cxx17_empty_base_field_p (field))
6642 {
6643 *has_cxx17_empty_base = true;
6644 continue;
6645 }
6646
6647 if (DECL_FIELD_CXX_ZERO_WIDTH_BIT_FIELD (field))
6648 {
6649 *has_cxx_zero_width_bf = true;
6650 continue;
6651 }
6652
6653 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
6654 return 0;
6655
6656 if (i == 2)
6657 return 0;
6658
6659 fields[i++] = field;
6660 }
6661 return i;
6662 }
6663
6664 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
6665 a value in the most significant part of $2/$3 if:
6666
6667 - the target is big-endian;
6668
6669 - the value has a structure or union type (we generalize this to
6670 cover aggregates from other languages too); and
6671
6672 - the structure is not returned in floating-point registers. */
6673
6674 static bool
6675 mips_return_in_msb (const_tree valtype)
6676 {
6677 if (!TARGET_NEWABI || !TARGET_BIG_ENDIAN || !AGGREGATE_TYPE_P (valtype))
6678 return false;
6679
6680 tree fields[2];
6681 bool has_cxx_zero_width_bf = false;
6682
6683 /* Its value is not used. */
6684 bool has_cxx17_empty_base = false;
6685
6686 return (mips_fpr_return_fields (valtype, fields,
6687 &has_cxx_zero_width_bf,
6688 &has_cxx17_empty_base) == 0
6689 || has_cxx_zero_width_bf);
6690 }
6691
6692 /* Return true if the function return value MODE will get returned in a
6693 floating-point register. */
6694
6695 static bool
6696 mips_return_mode_in_fpr_p (machine_mode mode)
6697 {
6698 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
6699 return ((GET_MODE_CLASS (mode) == MODE_FLOAT
6700 || mode == V2SFmode
6701 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
6702 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
6703 }
6704
6705 /* Return the representation of an FPR return register when the
6706 value being returned in FP_RETURN has mode VALUE_MODE and the
6707 return type itself has mode TYPE_MODE. On NewABI targets,
6708 the two modes may be different for structures like:
6709
6710 struct __attribute__((packed)) foo { float f; }
6711
6712 where we return the SFmode value of "f" in FP_RETURN, but where
6713 the structure itself has mode BLKmode. */
6714
6715 static rtx
6716 mips_return_fpr_single (machine_mode type_mode,
6717 machine_mode value_mode)
6718 {
6719 rtx x;
6720
6721 x = gen_rtx_REG (value_mode, FP_RETURN);
6722 if (type_mode != value_mode)
6723 {
6724 x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
6725 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
6726 }
6727 return x;
6728 }
6729
6730 /* Return a composite value in a pair of floating-point registers.
6731 MODE1 and OFFSET1 are the mode and byte offset for the first value,
6732 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
6733 complete value.
6734
6735 For n32 & n64, $f0 always holds the first value and $f2 the second.
6736 Otherwise the values are packed together as closely as possible. */
6737
6738 static rtx
6739 mips_return_fpr_pair (machine_mode mode,
6740 machine_mode mode1, HOST_WIDE_INT offset1,
6741 machine_mode mode2, HOST_WIDE_INT offset2)
6742 {
6743 int inc;
6744
6745 inc = (TARGET_NEWABI || mips_abi == ABI_32 ? 2 : MAX_FPRS_PER_FMT);
6746 return gen_rtx_PARALLEL
6747 (mode,
6748 gen_rtvec (2,
6749 gen_rtx_EXPR_LIST (VOIDmode,
6750 gen_rtx_REG (mode1, FP_RETURN),
6751 GEN_INT (offset1)),
6752 gen_rtx_EXPR_LIST (VOIDmode,
6753 gen_rtx_REG (mode2, FP_RETURN + inc),
6754 GEN_INT (offset2))));
6755
6756 }
6757
6758 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
6759 For normal calls, VALTYPE is the return type and MODE is VOIDmode.
6760 For libcalls, VALTYPE is null and MODE is the mode of the return value. */
6761
6762 static rtx
6763 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
6764 machine_mode mode)
6765 {
6766 if (valtype)
6767 {
6768 tree fields[2];
6769 int unsigned_p;
6770 const_tree func;
6771
6772 if (fn_decl_or_type && DECL_P (fn_decl_or_type))
6773 func = fn_decl_or_type;
6774 else
6775 func = NULL;
6776
6777 mode = TYPE_MODE (valtype);
6778 unsigned_p = TYPE_UNSIGNED (valtype);
6779
6780 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
6781 return values, promote the mode here too. */
6782 mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
6783
6784 bool has_cxx_zero_width_bf = false;
6785 bool has_cxx17_empty_base = false;
6786 int use_fpr = mips_fpr_return_fields (valtype, fields,
6787 &has_cxx_zero_width_bf,
6788 &has_cxx17_empty_base);
6789
6790 /* If has_cxx_zero_width_bf and has_cxx17_empty_base are both
6791 true, it *happens* that there is no ABI change. So we won't
6792 inform in this case. */
6793 if (TARGET_HARD_FLOAT
6794 && warn_psabi
6795 && has_cxx_zero_width_bf
6796 && !has_cxx17_empty_base
6797 && use_fpr != 0)
6798 {
6799 static unsigned last_reported_type_uid;
6800 unsigned uid = TYPE_UID (TYPE_MAIN_VARIANT (valtype));
6801 if (uid != last_reported_type_uid)
6802 {
6803 static const char *url
6804 = CHANGES_ROOT_URL
6805 "gcc-12/changes.html#zero_width_bitfields";
6806 inform (input_location,
6807 "the ABI for returning a value containing "
6808 "zero-width bit-fields but otherwise an aggregate "
6809 "with only one or two floating-point fields was "
6810 "changed in GCC %{12.1%}", url);
6811 last_reported_type_uid = uid;
6812 }
6813 }
6814
6815 if (has_cxx_zero_width_bf)
6816 use_fpr = 0;
6817
6818 if (TARGET_HARD_FLOAT
6819 && warn_psabi
6820 && use_fpr != 0
6821 && has_cxx17_empty_base)
6822 {
6823 static unsigned last_reported_type_uid;
6824 unsigned uid = TYPE_UID (TYPE_MAIN_VARIANT (valtype));
6825 if (uid != last_reported_type_uid)
6826 {
6827 static const char *url
6828 = CHANGES_ROOT_URL
6829 "gcc-12/changes.html#mips_cxx17_empty_bases";
6830 inform (input_location,
6831 "the ABI for returning a value with C++17 empty "
6832 "bases but otherwise an aggregate with only one or "
6833 "two floating-point fields was changed in GCC "
6834 "%{12.1%}", url);
6835 last_reported_type_uid = uid;
6836 }
6837 }
6838
6839 /* Handle structures whose fields are returned in $f0/$f2. */
6840 switch (use_fpr)
6841 {
6842 case 1:
6843 return mips_return_fpr_single (mode,
6844 TYPE_MODE (TREE_TYPE (fields[0])));
6845
6846 case 2:
6847 return mips_return_fpr_pair (mode,
6848 TYPE_MODE (TREE_TYPE (fields[0])),
6849 int_byte_position (fields[0]),
6850 TYPE_MODE (TREE_TYPE (fields[1])),
6851 int_byte_position (fields[1]));
6852 }
6853
6854 /* If a value is passed in the most significant part of a register, see
6855 whether we have to round the mode up to a whole number of words. */
6856 if (mips_return_in_msb (valtype))
6857 {
6858 HOST_WIDE_INT size = int_size_in_bytes (valtype);
6859 if (size % UNITS_PER_WORD != 0)
6860 {
6861 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
6862 mode = int_mode_for_size (size * BITS_PER_UNIT, 0).require ();
6863 }
6864 }
6865
6866 /* For EABI, the class of return register depends entirely on MODE.
6867 For example, "struct { some_type x; }" and "union { some_type x; }"
6868 are returned in the same way as a bare "some_type" would be.
6869 Other ABIs only use FPRs for scalar, complex or vector types. */
6870 if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
6871 return gen_rtx_REG (mode, GP_RETURN);
6872 }
6873
6874 if (!TARGET_MIPS16)
6875 {
6876 /* Handle long doubles for n32 & n64. */
6877 if (mode == TFmode)
6878 return mips_return_fpr_pair (mode,
6879 DImode, 0,
6880 DImode, GET_MODE_SIZE (mode) / 2);
6881
6882 if (mips_return_mode_in_fpr_p (mode))
6883 {
6884 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
6885 return mips_return_fpr_pair (mode,
6886 GET_MODE_INNER (mode), 0,
6887 GET_MODE_INNER (mode),
6888 GET_MODE_SIZE (mode) / 2);
6889 else
6890 return gen_rtx_REG (mode, FP_RETURN);
6891 }
6892 }
6893
6894 return gen_rtx_REG (mode, GP_RETURN);
6895 }
6896
6897 /* Implement TARGET_FUNCTION_VALUE. */
6898
6899 static rtx
6900 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
6901 bool outgoing ATTRIBUTE_UNUSED)
6902 {
6903 return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
6904 }
6905
6906 /* Implement TARGET_LIBCALL_VALUE. */
6907
6908 static rtx
6909 mips_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
6910 {
6911 return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
6912 }
6913
6914 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
6915
6916 On the MIPS, R2 R3 and F0 F2 are the only register thus used. */
6917
6918 static bool
6919 mips_function_value_regno_p (const unsigned int regno)
6920 {
6921 /* Most types only require one GPR or one FPR for return values but for
6922 hard-float two FPRs can be used for _Complex types (for all ABIs)
6923 and long doubles (for n64). */
6924 if (regno == GP_RETURN
6925 || regno == FP_RETURN
6926 || (FP_RETURN != GP_RETURN
6927 && regno == FP_RETURN + 2))
6928 return true;
6929
6930 /* For o32 FP32, _Complex double will be returned in four 32-bit registers.
6931 This does not apply to o32 FPXX as floating-point function argument and
6932 return registers are described as 64-bit even though floating-point
6933 registers are primarily described as 32-bit internally.
6934 See: mips_get_reg_raw_mode. */
6935 if ((mips_abi == ABI_32 && TARGET_FLOAT32)
6936 && FP_RETURN != GP_RETURN
6937 && (regno == FP_RETURN + 1
6938 || regno == FP_RETURN + 3))
6939 return true;
6940
6941 return false;
6942 }
6943
6944 /* Implement TARGET_RETURN_IN_MEMORY. Under the o32 and o64 ABIs,
6945 all BLKmode objects are returned in memory. Under the n32, n64
6946 and embedded ABIs, small structures are returned in a register.
6947 Objects with varying size must still be returned in memory, of
6948 course. */
6949
6950 static bool
6951 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
6952 {
6953 if (TARGET_OLDABI)
6954 /* Ensure that any floating point vector types are returned via memory
6955 even if they are supported through a vector mode with some ASEs. */
6956 return (VECTOR_FLOAT_TYPE_P (type)
6957 || TYPE_MODE (type) == BLKmode);
6958
6959 return (!IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
6960 }
6961
6962 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
6963
6964 static void
6965 mips_setup_incoming_varargs (cumulative_args_t cum,
6966 const function_arg_info &arg,
6967 int *pretend_size ATTRIBUTE_UNUSED, int no_rtl)
6968 {
6969 CUMULATIVE_ARGS local_cum;
6970 int gp_saved, fp_saved;
6971
6972 /* The caller has advanced CUM up to, but not beyond, the last named
6973 argument. Advance a local copy of CUM past the last "real" named
6974 argument, to find out how many registers are left over. */
6975 local_cum = *get_cumulative_args (cum);
6976
6977 /* For a C23 variadic function w/o any named argument, and w/o an
6978 artifical argument for large return value, skip advancing args.
6979 There is such an artifical argument iff. arg.type is non-NULL
6980 (PR 114175). */
6981 if (!TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (current_function_decl))
6982 || arg.type != NULL_TREE)
6983 mips_function_arg_advance (pack_cumulative_args (&local_cum), arg);
6984
6985 /* Found out how many registers we need to save. */
6986 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
6987 fp_saved = (EABI_FLOAT_VARARGS_P
6988 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
6989 : 0);
6990
6991 if (!no_rtl)
6992 {
6993 if (gp_saved > 0)
6994 {
6995 rtx ptr, mem;
6996
6997 ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
6998 REG_PARM_STACK_SPACE (cfun->decl)
6999 - gp_saved * UNITS_PER_WORD);
7000 mem = gen_frame_mem (BLKmode, ptr);
7001 set_mem_alias_set (mem, get_varargs_alias_set ());
7002
7003 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
7004 mem, gp_saved);
7005 }
7006 if (fp_saved > 0)
7007 {
7008 /* We can't use move_block_from_reg, because it will use
7009 the wrong mode. */
7010 machine_mode mode;
7011 int off, i;
7012
7013 /* Set OFF to the offset from virtual_incoming_args_rtx of
7014 the first float register. The FP save area lies below
7015 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
7016 off = ROUND_DOWN (-gp_saved * UNITS_PER_WORD, UNITS_PER_FPVALUE);
7017 off -= fp_saved * UNITS_PER_FPREG;
7018
7019 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
7020
7021 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
7022 i += MAX_FPRS_PER_FMT)
7023 {
7024 rtx ptr, mem;
7025
7026 ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
7027 mem = gen_frame_mem (mode, ptr);
7028 set_mem_alias_set (mem, get_varargs_alias_set ());
7029 mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
7030 off += UNITS_PER_HWFPVALUE;
7031 }
7032 }
7033 }
7034 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
7035 cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
7036 + fp_saved * UNITS_PER_FPREG);
7037 }
7038
7039 /* Implement TARGET_BUILTIN_VA_LIST. */
7040
7041 static tree
7042 mips_build_builtin_va_list (void)
7043 {
7044 if (EABI_FLOAT_VARARGS_P)
7045 {
7046 /* We keep 3 pointers, and two offsets.
7047
7048 Two pointers are to the overflow area, which starts at the CFA.
7049 One of these is constant, for addressing into the GPR save area
7050 below it. The other is advanced up the stack through the
7051 overflow region.
7052
7053 The third pointer is to the bottom of the GPR save area.
7054 Since the FPR save area is just below it, we can address
7055 FPR slots off this pointer.
7056
7057 We also keep two one-byte offsets, which are to be subtracted
7058 from the constant pointers to yield addresses in the GPR and
7059 FPR save areas. These are downcounted as float or non-float
7060 arguments are used, and when they get to zero, the argument
7061 must be obtained from the overflow region. */
7062 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
7063 tree array, index;
7064
7065 record = lang_hooks.types.make_type (RECORD_TYPE);
7066
7067 f_ovfl = build_decl (BUILTINS_LOCATION,
7068 FIELD_DECL, get_identifier ("__overflow_argptr"),
7069 ptr_type_node);
7070 f_gtop = build_decl (BUILTINS_LOCATION,
7071 FIELD_DECL, get_identifier ("__gpr_top"),
7072 ptr_type_node);
7073 f_ftop = build_decl (BUILTINS_LOCATION,
7074 FIELD_DECL, get_identifier ("__fpr_top"),
7075 ptr_type_node);
7076 f_goff = build_decl (BUILTINS_LOCATION,
7077 FIELD_DECL, get_identifier ("__gpr_offset"),
7078 unsigned_char_type_node);
7079 f_foff = build_decl (BUILTINS_LOCATION,
7080 FIELD_DECL, get_identifier ("__fpr_offset"),
7081 unsigned_char_type_node);
7082 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
7083 warn on every user file. */
7084 index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
7085 array = build_array_type (unsigned_char_type_node,
7086 build_index_type (index));
7087 f_res = build_decl (BUILTINS_LOCATION,
7088 FIELD_DECL, get_identifier ("__reserved"), array);
7089
7090 DECL_FIELD_CONTEXT (f_ovfl) = record;
7091 DECL_FIELD_CONTEXT (f_gtop) = record;
7092 DECL_FIELD_CONTEXT (f_ftop) = record;
7093 DECL_FIELD_CONTEXT (f_goff) = record;
7094 DECL_FIELD_CONTEXT (f_foff) = record;
7095 DECL_FIELD_CONTEXT (f_res) = record;
7096
7097 TYPE_FIELDS (record) = f_ovfl;
7098 DECL_CHAIN (f_ovfl) = f_gtop;
7099 DECL_CHAIN (f_gtop) = f_ftop;
7100 DECL_CHAIN (f_ftop) = f_goff;
7101 DECL_CHAIN (f_goff) = f_foff;
7102 DECL_CHAIN (f_foff) = f_res;
7103
7104 layout_type (record);
7105 return record;
7106 }
7107 else
7108 /* Otherwise, we use 'void *'. */
7109 return ptr_type_node;
7110 }
7111
7112 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
7113
7114 static void
7115 mips_va_start (tree valist, rtx nextarg)
7116 {
7117 if (EABI_FLOAT_VARARGS_P)
7118 {
7119 const CUMULATIVE_ARGS *cum;
7120 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
7121 tree ovfl, gtop, ftop, goff, foff;
7122 tree t;
7123 int gpr_save_area_size;
7124 int fpr_save_area_size;
7125 int fpr_offset;
7126
7127 cum = &crtl->args.info;
7128 gpr_save_area_size
7129 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
7130 fpr_save_area_size
7131 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
7132
7133 f_ovfl = TYPE_FIELDS (va_list_type_node);
7134 f_gtop = DECL_CHAIN (f_ovfl);
7135 f_ftop = DECL_CHAIN (f_gtop);
7136 f_goff = DECL_CHAIN (f_ftop);
7137 f_foff = DECL_CHAIN (f_goff);
7138
7139 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
7140 NULL_TREE);
7141 gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
7142 NULL_TREE);
7143 ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
7144 NULL_TREE);
7145 goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
7146 NULL_TREE);
7147 foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
7148 NULL_TREE);
7149
7150 /* Emit code to initialize OVFL, which points to the next varargs
7151 stack argument. CUM->STACK_WORDS gives the number of stack
7152 words used by named arguments. */
7153 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
7154 if (cum->stack_words > 0)
7155 t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
7156 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
7157 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7158
7159 /* Emit code to initialize GTOP, the top of the GPR save area. */
7160 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
7161 t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
7162 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7163
7164 /* Emit code to initialize FTOP, the top of the FPR save area.
7165 This address is gpr_save_area_bytes below GTOP, rounded
7166 down to the next fp-aligned boundary. */
7167 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
7168 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
7169 fpr_offset &= -UNITS_PER_FPVALUE;
7170 if (fpr_offset)
7171 t = fold_build_pointer_plus_hwi (t, -fpr_offset);
7172 t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
7173 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7174
7175 /* Emit code to initialize GOFF, the offset from GTOP of the
7176 next GPR argument. */
7177 t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
7178 build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
7179 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7180
7181 /* Likewise emit code to initialize FOFF, the offset from FTOP
7182 of the next FPR argument. */
7183 t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
7184 build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
7185 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7186 }
7187 else
7188 {
7189 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
7190 std_expand_builtin_va_start (valist, nextarg);
7191 }
7192 }
7193
7194 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
7195 types as well. */
7196
7197 static tree
7198 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
7199 gimple_seq *post_p)
7200 {
7201 tree addr, t, type_size, rounded_size, valist_tmp;
7202 unsigned HOST_WIDE_INT align, boundary;
7203 bool indirect;
7204
7205 indirect = pass_va_arg_by_reference (type);
7206 if (indirect)
7207 type = build_pointer_type (type);
7208
7209 align = PARM_BOUNDARY / BITS_PER_UNIT;
7210 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
7211
7212 /* When we align parameter on stack for caller, if the parameter
7213 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
7214 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
7215 here with caller. */
7216 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
7217 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
7218
7219 boundary /= BITS_PER_UNIT;
7220
7221 /* Hoist the valist value into a temporary for the moment. */
7222 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
7223
7224 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
7225 requires greater alignment, we must perform dynamic alignment. */
7226 if (boundary > align)
7227 {
7228 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
7229 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
7230 gimplify_and_add (t, pre_p);
7231
7232 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
7233 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
7234 valist_tmp,
7235 build_int_cst (TREE_TYPE (valist), -boundary)));
7236 gimplify_and_add (t, pre_p);
7237 }
7238 else
7239 boundary = align;
7240
7241 /* If the actual alignment is less than the alignment of the type,
7242 adjust the type accordingly so that we don't assume strict alignment
7243 when dereferencing the pointer. */
7244 boundary *= BITS_PER_UNIT;
7245 if (boundary < TYPE_ALIGN (type))
7246 {
7247 type = build_variant_type_copy (type);
7248 SET_TYPE_ALIGN (type, boundary);
7249 }
7250
7251 /* Compute the rounded size of the type. */
7252 type_size = size_in_bytes (type);
7253 rounded_size = round_up (type_size, align);
7254
7255 /* Reduce rounded_size so it's sharable with the postqueue. */
7256 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
7257
7258 /* Get AP. */
7259 addr = valist_tmp;
7260 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
7261 {
7262 /* Small args are padded downward. */
7263 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
7264 rounded_size, size_int (align));
7265 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
7266 size_binop (MINUS_EXPR, rounded_size, type_size));
7267 addr = fold_build_pointer_plus (addr, t);
7268 }
7269
7270 /* Compute new value for AP. */
7271 t = fold_build_pointer_plus (valist_tmp, rounded_size);
7272 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
7273 gimplify_and_add (t, pre_p);
7274
7275 addr = fold_convert (build_pointer_type (type), addr);
7276
7277 if (indirect)
7278 addr = build_va_arg_indirect_ref (addr);
7279
7280 return build_va_arg_indirect_ref (addr);
7281 }
7282
7283 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
7284
7285 static tree
7286 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
7287 gimple_seq *post_p)
7288 {
7289 tree addr;
7290 bool indirect_p;
7291
7292 indirect_p = pass_va_arg_by_reference (type);
7293 if (indirect_p)
7294 type = build_pointer_type (type);
7295
7296 if (!EABI_FLOAT_VARARGS_P)
7297 addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
7298 else
7299 {
7300 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
7301 tree ovfl, top, off, align;
7302 HOST_WIDE_INT size, rsize, osize;
7303 tree t, u;
7304
7305 f_ovfl = TYPE_FIELDS (va_list_type_node);
7306 f_gtop = DECL_CHAIN (f_ovfl);
7307 f_ftop = DECL_CHAIN (f_gtop);
7308 f_goff = DECL_CHAIN (f_ftop);
7309 f_foff = DECL_CHAIN (f_goff);
7310
7311 /* Let:
7312
7313 TOP be the top of the GPR or FPR save area;
7314 OFF be the offset from TOP of the next register;
7315 ADDR_RTX be the address of the argument;
7316 SIZE be the number of bytes in the argument type;
7317 RSIZE be the number of bytes used to store the argument
7318 when it's in the register save area; and
7319 OSIZE be the number of bytes used to store it when it's
7320 in the stack overflow area.
7321
7322 The code we want is:
7323
7324 1: off &= -rsize; // round down
7325 2: if (off != 0)
7326 3: {
7327 4: addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
7328 5: off -= rsize;
7329 6: }
7330 7: else
7331 8: {
7332 9: ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
7333 10: addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
7334 11: ovfl += osize;
7335 14: }
7336
7337 [1] and [9] can sometimes be optimized away. */
7338
7339 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
7340 NULL_TREE);
7341 size = int_size_in_bytes (type);
7342
7343 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
7344 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
7345 {
7346 top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
7347 unshare_expr (valist), f_ftop, NULL_TREE);
7348 off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
7349 unshare_expr (valist), f_foff, NULL_TREE);
7350
7351 /* When va_start saves FPR arguments to the stack, each slot
7352 takes up UNITS_PER_HWFPVALUE bytes, regardless of the
7353 argument's precision. */
7354 rsize = UNITS_PER_HWFPVALUE;
7355
7356 /* Overflow arguments are padded to UNITS_PER_WORD bytes
7357 (= PARM_BOUNDARY bits). This can be different from RSIZE
7358 in two cases:
7359
7360 (1) On 32-bit targets when TYPE is a structure such as:
7361
7362 struct s { float f; };
7363
7364 Such structures are passed in paired FPRs, so RSIZE
7365 will be 8 bytes. However, the structure only takes
7366 up 4 bytes of memory, so OSIZE will only be 4.
7367
7368 (2) In combinations such as -mgp64 -msingle-float
7369 -fshort-double. Doubles passed in registers will then take
7370 up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
7371 stack take up UNITS_PER_WORD bytes. */
7372 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
7373 }
7374 else
7375 {
7376 top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
7377 unshare_expr (valist), f_gtop, NULL_TREE);
7378 off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
7379 unshare_expr (valist), f_goff, NULL_TREE);
7380 rsize = ROUND_UP (size, UNITS_PER_WORD);
7381 if (rsize > UNITS_PER_WORD)
7382 {
7383 /* [1] Emit code for: off &= -rsize. */
7384 t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
7385 build_int_cst (TREE_TYPE (off), -rsize));
7386 gimplify_assign (unshare_expr (off), t, pre_p);
7387 }
7388 osize = rsize;
7389 }
7390
7391 /* [2] Emit code to branch if off == 0. */
7392 t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
7393 build_int_cst (TREE_TYPE (off), 0));
7394 addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
7395
7396 /* [5] Emit code for: off -= rsize. We do this as a form of
7397 post-decrement not available to C. */
7398 t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
7399 t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
7400
7401 /* [4] Emit code for:
7402 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0). */
7403 t = fold_convert (sizetype, t);
7404 t = fold_build1 (NEGATE_EXPR, sizetype, t);
7405 t = fold_build_pointer_plus (top, t);
7406 if (BYTES_BIG_ENDIAN && rsize > size)
7407 t = fold_build_pointer_plus_hwi (t, rsize - size);
7408 COND_EXPR_THEN (addr) = t;
7409
7410 if (osize > UNITS_PER_WORD)
7411 {
7412 /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize. */
7413 t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
7414 u = build_int_cst (TREE_TYPE (t), -osize);
7415 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
7416 align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
7417 unshare_expr (ovfl), t);
7418 }
7419 else
7420 align = NULL;
7421
7422 /* [10, 11] Emit code for:
7423 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
7424 ovfl += osize. */
7425 u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
7426 t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
7427 if (BYTES_BIG_ENDIAN && osize > size)
7428 t = fold_build_pointer_plus_hwi (t, osize - size);
7429
7430 /* String [9] and [10, 11] together. */
7431 if (align)
7432 t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
7433 COND_EXPR_ELSE (addr) = t;
7434
7435 addr = fold_convert (build_pointer_type (type), addr);
7436 addr = build_va_arg_indirect_ref (addr);
7437 }
7438
7439 if (indirect_p)
7440 addr = build_va_arg_indirect_ref (addr);
7441
7442 return addr;
7443 }
7444
7445 /* Declare a unique, locally-binding function called NAME, then start
7446 its definition. */
7447
7448 static tree
7449 mips_start_unique_function (const char *name)
7450 {
7451 tree decl;
7452
7453 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
7454 get_identifier (name),
7455 build_function_type_list (void_type_node, NULL_TREE));
7456 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
7457 NULL_TREE, void_type_node);
7458 TREE_PUBLIC (decl) = 1;
7459 TREE_STATIC (decl) = 1;
7460
7461 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
7462
7463 targetm.asm_out.unique_section (decl, 0);
7464 switch_to_section (get_named_section (decl, NULL, 0));
7465
7466 targetm.asm_out.globalize_label (asm_out_file, name);
7467 fputs ("\t.hidden\t", asm_out_file);
7468 assemble_name (asm_out_file, name);
7469 putc ('\n', asm_out_file);
7470
7471 return decl;
7472 }
7473
7474 /* Start a definition of function NAME. MIPS16_P indicates whether the
7475 function contains MIPS16 code. */
7476
7477 static void
7478 mips_start_function_definition (const char *name, bool mips16_p,
7479 tree decl ATTRIBUTE_UNUSED)
7480 {
7481 if (mips16_p)
7482 fprintf (asm_out_file, "\t.set\tmips16\n");
7483 else
7484 fprintf (asm_out_file, "\t.set\tnomips16\n");
7485
7486 if (TARGET_MICROMIPS)
7487 fprintf (asm_out_file, "\t.set\tmicromips\n");
7488 #ifdef HAVE_GAS_MICROMIPS
7489 else
7490 fprintf (asm_out_file, "\t.set\tnomicromips\n");
7491 #endif
7492
7493 if (!flag_inhibit_size_directive)
7494 {
7495 fputs ("\t.ent\t", asm_out_file);
7496 assemble_name (asm_out_file, name);
7497 fputs ("\n", asm_out_file);
7498 }
7499
7500 ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
7501
7502 /* Start the definition proper. */
7503 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, name, decl);
7504 }
7505
7506 /* End a function definition started by mips_start_function_definition. */
7507
7508 static void
7509 mips_end_function_definition (const char *name)
7510 {
7511 if (!flag_inhibit_size_directive)
7512 {
7513 fputs ("\t.end\t", asm_out_file);
7514 assemble_name (asm_out_file, name);
7515 fputs ("\n", asm_out_file);
7516 }
7517 }
7518
7519 /* If *STUB_PTR points to a stub, output a comdat-style definition for it,
7520 then free *STUB_PTR. */
7521
7522 static void
7523 mips_finish_stub (mips_one_only_stub **stub_ptr)
7524 {
7525 mips_one_only_stub *stub = *stub_ptr;
7526 if (!stub)
7527 return;
7528
7529 const char *name = stub->get_name ();
7530 tree decl = mips_start_unique_function (name);
7531 mips_start_function_definition (name, false, decl);
7532 stub->output_body ();
7533 mips_end_function_definition (name);
7534 delete stub;
7535 *stub_ptr = 0;
7536 }
7537
7538 /* Return true if calls to X can use R_MIPS_CALL* relocations. */
7539
7540 static bool
7541 mips_ok_for_lazy_binding_p (rtx x)
7542 {
7543 return (TARGET_USE_GOT
7544 && GET_CODE (x) == SYMBOL_REF
7545 && !SYMBOL_REF_BIND_NOW_P (x)
7546 && !mips_symbol_binds_local_p (x));
7547 }
7548
7549 /* Load function address ADDR into register DEST. TYPE is as for
7550 mips_expand_call. Return true if we used an explicit lazy-binding
7551 sequence. */
7552
7553 static bool
7554 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
7555 {
7556 /* If we're generating PIC, and this call is to a global function,
7557 try to allow its address to be resolved lazily. This isn't
7558 possible for sibcalls when $gp is call-saved because the value
7559 of $gp on entry to the stub would be our caller's gp, not ours. */
7560 if (TARGET_EXPLICIT_RELOCS
7561 && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
7562 && mips_ok_for_lazy_binding_p (addr))
7563 {
7564 addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
7565 emit_insn (gen_rtx_SET (dest, addr));
7566 return true;
7567 }
7568 else
7569 {
7570 mips_emit_move (dest, addr);
7571 return false;
7572 }
7573 }
7574
7575 /* Each locally-defined hard-float MIPS16 function has a local symbol
7576 associated with it. This hash table maps the function symbol (FUNC)
7577 to the local symbol (LOCAL). */
7578 static GTY (()) hash_map<nofree_string_hash, rtx> *mips16_local_aliases;
7579
7580 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
7581 Return a local alias for it, creating a new one if necessary. */
7582
7583 static rtx
7584 mips16_local_alias (rtx func)
7585 {
7586 /* Create the hash table if this is the first call. */
7587 if (mips16_local_aliases == NULL)
7588 mips16_local_aliases = hash_map<nofree_string_hash, rtx>::create_ggc (37);
7589
7590 /* Look up the function symbol, creating a new entry if need be. */
7591 bool existed;
7592 const char *func_name = XSTR (func, 0);
7593 rtx *slot = &mips16_local_aliases->get_or_insert (func_name, &existed);
7594 gcc_assert (slot != NULL);
7595
7596 if (!existed)
7597 {
7598 rtx local;
7599
7600 /* Create a new SYMBOL_REF for the local symbol. The choice of
7601 __fn_local_* is based on the __fn_stub_* names that we've
7602 traditionally used for the non-MIPS16 stub. */
7603 func_name = targetm.strip_name_encoding (XSTR (func, 0));
7604 const char *local_name = ACONCAT (("__fn_local_", func_name, NULL));
7605 local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
7606 SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
7607
7608 /* Create a new structure to represent the mapping. */
7609 *slot = local;
7610 }
7611 return *slot;
7612 }
7613
7614 /* A chained list of functions for which mips16_build_call_stub has already
7615 generated a stub. NAME is the name of the function and FP_RET_P is true
7616 if the function returns a value in floating-point registers. */
7617 struct mips16_stub {
7618 struct mips16_stub *next;
7619 char *name;
7620 bool fp_ret_p;
7621 };
7622 static struct mips16_stub *mips16_stubs;
7623
7624 /* Return the two-character string that identifies floating-point
7625 return mode MODE in the name of a MIPS16 function stub. */
7626
7627 static const char *
7628 mips16_call_stub_mode_suffix (machine_mode mode)
7629 {
7630 if (mode == SFmode)
7631 return "sf";
7632 else if (mode == DFmode)
7633 return "df";
7634 else if (mode == SCmode)
7635 return "sc";
7636 else if (mode == DCmode)
7637 return "dc";
7638 else if (mode == V2SFmode)
7639 {
7640 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT);
7641 return "df";
7642 }
7643 else
7644 gcc_unreachable ();
7645 }
7646
7647 /* Write instructions to move a 32-bit value between general register
7648 GPREG and floating-point register FPREG. DIRECTION is 't' to move
7649 from GPREG to FPREG and 'f' to move in the opposite direction. */
7650
7651 static void
7652 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
7653 {
7654 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
7655 reg_names[gpreg], reg_names[fpreg]);
7656 }
7657
7658 /* Likewise for 64-bit values. */
7659
7660 static void
7661 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
7662 {
7663 if (TARGET_64BIT)
7664 fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
7665 reg_names[gpreg], reg_names[fpreg]);
7666 else if (ISA_HAS_MXHC1)
7667 {
7668 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
7669 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
7670 fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
7671 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
7672 }
7673 else if (TARGET_FLOATXX && direction == 't')
7674 {
7675 /* Use the argument save area to move via memory. */
7676 fprintf (asm_out_file, "\tsw\t%s,0($sp)\n", reg_names[gpreg]);
7677 fprintf (asm_out_file, "\tsw\t%s,4($sp)\n", reg_names[gpreg + 1]);
7678 fprintf (asm_out_file, "\tldc1\t%s,0($sp)\n", reg_names[fpreg]);
7679 }
7680 else if (TARGET_FLOATXX && direction == 'f')
7681 {
7682 /* Use the argument save area to move via memory. */
7683 fprintf (asm_out_file, "\tsdc1\t%s,0($sp)\n", reg_names[fpreg]);
7684 fprintf (asm_out_file, "\tlw\t%s,0($sp)\n", reg_names[gpreg]);
7685 fprintf (asm_out_file, "\tlw\t%s,4($sp)\n", reg_names[gpreg + 1]);
7686 }
7687 else
7688 {
7689 /* Move the least-significant word. */
7690 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
7691 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
7692 /* ...then the most significant word. */
7693 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
7694 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
7695 }
7696 }
7697
7698 /* Write out code to move floating-point arguments into or out of
7699 general registers. FP_CODE is the code describing which arguments
7700 are present (see the comment above the definition of CUMULATIVE_ARGS
7701 in mips.h). DIRECTION is as for mips_output_32bit_xfer. */
7702
7703 static void
7704 mips_output_args_xfer (int fp_code, char direction)
7705 {
7706 unsigned int gparg, fparg, f;
7707 CUMULATIVE_ARGS cum;
7708
7709 /* This code only works for o32 and o64. */
7710 gcc_assert (TARGET_OLDABI);
7711
7712 mips_init_cumulative_args (&cum, NULL);
7713
7714 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7715 {
7716 machine_mode mode;
7717 struct mips_arg_info info;
7718
7719 if ((f & 3) == 1)
7720 mode = SFmode;
7721 else if ((f & 3) == 2)
7722 mode = DFmode;
7723 else
7724 gcc_unreachable ();
7725
7726 mips_get_arg_info (&info, &cum, mode, NULL, true);
7727 gparg = mips_arg_regno (&info, false);
7728 fparg = mips_arg_regno (&info, true);
7729
7730 if (mode == SFmode)
7731 mips_output_32bit_xfer (direction, gparg, fparg);
7732 else
7733 mips_output_64bit_xfer (direction, gparg, fparg);
7734
7735 function_arg_info arg (mode, /*named=*/true);
7736 mips_function_arg_advance (pack_cumulative_args (&cum), arg);
7737 }
7738 }
7739
7740 /* Write a MIPS16 stub for the current function. This stub is used
7741 for functions which take arguments in the floating-point registers.
7742 It is normal-mode code that moves the floating-point arguments
7743 into the general registers and then jumps to the MIPS16 code. */
7744
7745 static void
7746 mips16_build_function_stub (void)
7747 {
7748 const char *fnname, *alias_name, *separator;
7749 char *secname, *stubname;
7750 tree stubdecl;
7751 unsigned int f;
7752 rtx symbol, alias;
7753
7754 /* Create the name of the stub, and its unique section. */
7755 symbol = XEXP (DECL_RTL (current_function_decl), 0);
7756 alias = mips16_local_alias (symbol);
7757
7758 fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
7759 alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
7760 secname = ACONCAT ((".mips16.fn.", fnname, NULL));
7761 stubname = ACONCAT (("__fn_stub_", fnname, NULL));
7762
7763 /* Build a decl for the stub. */
7764 stubdecl = build_decl (BUILTINS_LOCATION,
7765 FUNCTION_DECL, get_identifier (stubname),
7766 build_function_type_list (void_type_node, NULL_TREE));
7767 set_decl_section_name (stubdecl, secname);
7768 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
7769 RESULT_DECL, NULL_TREE, void_type_node);
7770
7771 /* Output a comment. */
7772 fprintf (asm_out_file, "\t# Stub function for %s (",
7773 current_function_name ());
7774 separator = "";
7775 for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
7776 {
7777 fprintf (asm_out_file, "%s%s", separator,
7778 (f & 3) == 1 ? "float" : "double");
7779 separator = ", ";
7780 }
7781 fprintf (asm_out_file, ")\n");
7782
7783 /* Start the function definition. */
7784 assemble_start_function (stubdecl, stubname);
7785 mips_start_function_definition (stubname, false, stubdecl);
7786
7787 /* If generating pic2 code, either set up the global pointer or
7788 switch to pic0. */
7789 if (TARGET_ABICALLS_PIC2)
7790 {
7791 if (TARGET_ABSOLUTE_ABICALLS)
7792 fprintf (asm_out_file, "\t.option\tpic0\n");
7793 else
7794 {
7795 output_asm_insn ("%(.cpload\t%^%)", NULL);
7796 /* Emit an R_MIPS_NONE relocation to tell the linker what the
7797 target function is. Use a local GOT access when loading the
7798 symbol, to cut down on the number of unnecessary GOT entries
7799 for stubs that aren't needed. */
7800 output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
7801 symbol = alias;
7802 }
7803 }
7804
7805 /* Load the address of the MIPS16 function into $25. Do this first so
7806 that targets with coprocessor interlocks can use an MFC1 to fill the
7807 delay slot. */
7808 output_asm_insn ("la\t%^,%0", &symbol);
7809
7810 /* Move the arguments from floating-point registers to general registers. */
7811 mips_output_args_xfer (crtl->args.info.fp_code, 'f');
7812
7813 /* Jump to the MIPS16 function. */
7814 output_asm_insn ("jr\t%^", NULL);
7815
7816 if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
7817 fprintf (asm_out_file, "\t.option\tpic2\n");
7818
7819 mips_end_function_definition (stubname);
7820
7821 /* If the linker needs to create a dynamic symbol for the target
7822 function, it will associate the symbol with the stub (which,
7823 unlike the target function, follows the proper calling conventions).
7824 It is therefore useful to have a local alias for the target function,
7825 so that it can still be identified as MIPS16 code. As an optimization,
7826 this symbol can also be used for indirect MIPS16 references from
7827 within this file. */
7828 ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
7829
7830 switch_to_section (function_section (current_function_decl));
7831 }
7832
7833 /* The current function is a MIPS16 function that returns a value in an FPR.
7834 Copy the return value from its soft-float to its hard-float location.
7835 libgcc2 has special non-MIPS16 helper functions for each case. */
7836
7837 static void
7838 mips16_copy_fpr_return_value (void)
7839 {
7840 rtx fn, insn, retval;
7841 tree return_type;
7842 machine_mode return_mode;
7843 const char *name;
7844
7845 return_type = DECL_RESULT (current_function_decl);
7846 return_mode = DECL_MODE (return_type);
7847
7848 name = ACONCAT (("__mips16_ret_",
7849 mips16_call_stub_mode_suffix (return_mode),
7850 NULL));
7851 fn = mips16_stub_function (name);
7852
7853 /* The function takes arguments in $2 (and possibly $3), so calls
7854 to it cannot be lazily bound. */
7855 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
7856
7857 /* Model the call as something that takes the GPR return value as
7858 argument and returns an "updated" value. */
7859 retval = gen_rtx_REG (return_mode, GP_RETURN);
7860 insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
7861 const0_rtx, NULL_RTX, false);
7862 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
7863 }
7864
7865 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
7866 RETVAL is the location of the return value, or null if this is
7867 a "call" rather than a "call_value". ARGS_SIZE is the size of the
7868 arguments and FP_CODE is the code built by mips_function_arg;
7869 see the comment before the fp_code field in CUMULATIVE_ARGS for details.
7870
7871 There are three alternatives:
7872
7873 - If a stub was needed, emit the call and return the call insn itself.
7874
7875 - If we can avoid using a stub by redirecting the call, set *FN_PTR
7876 to the new target and return null.
7877
7878 - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
7879 unmodified.
7880
7881 A stub is needed for calls to functions that, in normal mode,
7882 receive arguments in FPRs or return values in FPRs. The stub
7883 copies the arguments from their soft-float positions to their
7884 hard-float positions, calls the real function, then copies the
7885 return value from its hard-float position to its soft-float
7886 position.
7887
7888 We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
7889 If *FN_PTR turns out to be to a non-MIPS16 function, the linker
7890 automatically redirects the JAL to the stub, otherwise the JAL
7891 continues to call FN directly. */
7892
7893 static rtx_insn *
7894 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
7895 {
7896 const char *fnname;
7897 bool fp_ret_p;
7898 struct mips16_stub *l;
7899 rtx_insn *insn;
7900 rtx pattern, fn;
7901
7902 /* We don't need to do anything if we aren't in MIPS16 mode, or if
7903 we were invoked with the -msoft-float option. */
7904 if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
7905 return NULL;
7906
7907 /* Figure out whether the value might come back in a floating-point
7908 register. */
7909 fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
7910
7911 /* We don't need to do anything if there were no floating-point
7912 arguments and the value will not be returned in a floating-point
7913 register. */
7914 if (fp_code == 0 && !fp_ret_p)
7915 return NULL;
7916
7917 /* We don't need to do anything if this is a call to a special
7918 MIPS16 support function. */
7919 fn = *fn_ptr;
7920 if (mips16_stub_function_p (fn))
7921 return NULL;
7922
7923 /* If we're calling a locally-defined MIPS16 function, we know that
7924 it will return values in both the "soft-float" and "hard-float"
7925 registers. There is no need to use a stub to move the latter
7926 to the former. */
7927 if (fp_code == 0 && mips16_local_function_p (fn))
7928 return NULL;
7929
7930 /* This code will only work for o32 and o64 abis. The other ABI's
7931 require more sophisticated support. */
7932 gcc_assert (TARGET_OLDABI);
7933
7934 /* If we're calling via a function pointer, use one of the magic
7935 libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
7936 Each stub expects the function address to arrive in register $2. */
7937 if (GET_CODE (fn) != SYMBOL_REF
7938 || !call_insn_operand (fn, VOIDmode))
7939 {
7940 char buf[32];
7941 rtx stub_fn, addr;
7942 rtx_insn *insn;
7943 bool lazy_p;
7944
7945 /* If this is a locally-defined and locally-binding function,
7946 avoid the stub by calling the local alias directly. */
7947 if (mips16_local_function_p (fn))
7948 {
7949 *fn_ptr = mips16_local_alias (fn);
7950 return NULL;
7951 }
7952
7953 /* Create a SYMBOL_REF for the libgcc.a function. */
7954 if (fp_ret_p)
7955 sprintf (buf, "__mips16_call_stub_%s_%d",
7956 mips16_call_stub_mode_suffix (GET_MODE (retval)),
7957 fp_code);
7958 else
7959 sprintf (buf, "__mips16_call_stub_%d", fp_code);
7960 stub_fn = mips16_stub_function (buf);
7961
7962 /* The function uses $2 as an argument, so calls to it
7963 cannot be lazily bound. */
7964 SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
7965
7966 /* Load the target function into $2. */
7967 addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
7968 lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
7969
7970 /* Emit the call. */
7971 insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
7972 args_size, NULL_RTX, lazy_p);
7973
7974 /* Tell GCC that this call does indeed use the value of $2. */
7975 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
7976
7977 /* If we are handling a floating-point return value, we need to
7978 save $18 in the function prologue. Putting a note on the
7979 call will mean that df_regs_ever_live_p ($18) will be true if the
7980 call is not eliminated, and we can check that in the prologue
7981 code. */
7982 if (fp_ret_p)
7983 CALL_INSN_FUNCTION_USAGE (insn) =
7984 gen_rtx_EXPR_LIST (VOIDmode,
7985 gen_rtx_CLOBBER (VOIDmode,
7986 gen_rtx_REG (word_mode, 18)),
7987 CALL_INSN_FUNCTION_USAGE (insn));
7988
7989 return insn;
7990 }
7991
7992 /* We know the function we are going to call. If we have already
7993 built a stub, we don't need to do anything further. */
7994 fnname = targetm.strip_name_encoding (XSTR (fn, 0));
7995 for (l = mips16_stubs; l != NULL; l = l->next)
7996 if (strcmp (l->name, fnname) == 0)
7997 break;
7998
7999 if (l == NULL)
8000 {
8001 const char *separator;
8002 char *secname, *stubname;
8003 tree stubid, stubdecl;
8004 unsigned int f;
8005
8006 /* If the function does not return in FPRs, the special stub
8007 section is named
8008 .mips16.call.FNNAME
8009
8010 If the function does return in FPRs, the stub section is named
8011 .mips16.call.fp.FNNAME
8012
8013 Build a decl for the stub. */
8014 secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
8015 fnname, NULL));
8016 stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
8017 fnname, NULL));
8018 stubid = get_identifier (stubname);
8019 stubdecl = build_decl (BUILTINS_LOCATION,
8020 FUNCTION_DECL, stubid,
8021 build_function_type_list (void_type_node,
8022 NULL_TREE));
8023 set_decl_section_name (stubdecl, secname);
8024 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
8025 RESULT_DECL, NULL_TREE,
8026 void_type_node);
8027
8028 /* Output a comment. */
8029 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
8030 (fp_ret_p
8031 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
8032 : ""),
8033 fnname);
8034 separator = "";
8035 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
8036 {
8037 fprintf (asm_out_file, "%s%s", separator,
8038 (f & 3) == 1 ? "float" : "double");
8039 separator = ", ";
8040 }
8041 fprintf (asm_out_file, ")\n");
8042
8043 /* Start the function definition. */
8044 assemble_start_function (stubdecl, stubname);
8045 mips_start_function_definition (stubname, false, stubdecl);
8046
8047 if (fp_ret_p)
8048 {
8049 fprintf (asm_out_file, "\t.cfi_startproc\n");
8050
8051 /* Create a fake CFA 4 bytes below the stack pointer.
8052 This works around unwinders (like libgcc's) that expect
8053 the CFA for non-signal frames to be unique. */
8054 fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
8055
8056 /* "Save" $sp in itself so we don't use the fake CFA.
8057 This is: DW_CFA_val_expression r29, { DW_OP_reg29 }. */
8058 fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
8059
8060 /* Save the return address in $18. The stub's caller knows
8061 that $18 might be clobbered, even though $18 is usually
8062 a call-saved register.
8063
8064 Do it early on in case the last move to a floating-point
8065 register can be scheduled into the delay slot of the
8066 call we are about to make. */
8067 fprintf (asm_out_file, "\tmove\t%s,%s\n",
8068 reg_names[GP_REG_FIRST + 18],
8069 reg_names[RETURN_ADDR_REGNUM]);
8070 }
8071 else
8072 {
8073 /* Load the address of the MIPS16 function into $25. Do this
8074 first so that targets with coprocessor interlocks can use
8075 an MFC1 to fill the delay slot. */
8076 if (TARGET_EXPLICIT_RELOCS)
8077 {
8078 output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
8079 output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
8080 }
8081 else
8082 output_asm_insn ("la\t%^,%0", &fn);
8083 }
8084
8085 /* Move the arguments from general registers to floating-point
8086 registers. */
8087 mips_output_args_xfer (fp_code, 't');
8088
8089 if (fp_ret_p)
8090 {
8091 /* Now call the non-MIPS16 function. */
8092 output_asm_insn (mips_output_jump (&fn, 0, -1, true), &fn);
8093 fprintf (asm_out_file, "\t.cfi_register 31,18\n");
8094
8095 /* Move the result from floating-point registers to
8096 general registers. */
8097 switch (GET_MODE (retval))
8098 {
8099 case E_SCmode:
8100 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
8101 TARGET_BIG_ENDIAN
8102 ? FP_REG_FIRST + 2
8103 : FP_REG_FIRST);
8104 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
8105 TARGET_LITTLE_ENDIAN
8106 ? FP_REG_FIRST + 2
8107 : FP_REG_FIRST);
8108 if (GET_MODE (retval) == SCmode && TARGET_64BIT)
8109 {
8110 /* On 64-bit targets, complex floats are returned in
8111 a single GPR, such that "sd" on a suitably-aligned
8112 target would store the value correctly. */
8113 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
8114 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
8115 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
8116 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
8117 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
8118 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
8119 fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
8120 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
8121 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
8122 fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
8123 reg_names[GP_RETURN],
8124 reg_names[GP_RETURN],
8125 reg_names[GP_RETURN + 1]);
8126 }
8127 break;
8128
8129 case E_SFmode:
8130 mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
8131 break;
8132
8133 case E_DCmode:
8134 mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
8135 FP_REG_FIRST + 2);
8136 /* FALLTHRU */
8137 case E_DFmode:
8138 case E_V2SFmode:
8139 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT
8140 || GET_MODE (retval) != V2SFmode);
8141 mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
8142 break;
8143
8144 default:
8145 gcc_unreachable ();
8146 }
8147 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
8148 fprintf (asm_out_file, "\t.cfi_endproc\n");
8149 }
8150 else
8151 {
8152 /* Jump to the previously-loaded address. */
8153 output_asm_insn ("jr\t%^", NULL);
8154 }
8155
8156 #ifdef ASM_DECLARE_FUNCTION_SIZE
8157 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
8158 #endif
8159
8160 mips_end_function_definition (stubname);
8161
8162 /* Record this stub. */
8163 l = XNEW (struct mips16_stub);
8164 l->name = xstrdup (fnname);
8165 l->fp_ret_p = fp_ret_p;
8166 l->next = mips16_stubs;
8167 mips16_stubs = l;
8168 }
8169
8170 /* If we expect a floating-point return value, but we've built a
8171 stub which does not expect one, then we're in trouble. We can't
8172 use the existing stub, because it won't handle the floating-point
8173 value. We can't build a new stub, because the linker won't know
8174 which stub to use for the various calls in this object file.
8175 Fortunately, this case is illegal, since it means that a function
8176 was declared in two different ways in a single compilation. */
8177 if (fp_ret_p && !l->fp_ret_p)
8178 error ("cannot handle inconsistent calls to %qs", fnname);
8179
8180 if (retval == NULL_RTX)
8181 pattern = gen_call_internal_direct (fn, args_size);
8182 else
8183 pattern = gen_call_value_internal_direct (retval, fn, args_size);
8184 insn = mips_emit_call_insn (pattern, fn, fn, false);
8185
8186 /* If we are calling a stub which handles a floating-point return
8187 value, we need to arrange to save $18 in the prologue. We do this
8188 by marking the function call as using the register. The prologue
8189 will later see that it is used, and emit code to save it. */
8190 if (fp_ret_p)
8191 CALL_INSN_FUNCTION_USAGE (insn) =
8192 gen_rtx_EXPR_LIST (VOIDmode,
8193 gen_rtx_CLOBBER (VOIDmode,
8194 gen_rtx_REG (word_mode, 18)),
8195 CALL_INSN_FUNCTION_USAGE (insn));
8196
8197 return insn;
8198 }
8199
8200 /* Expand a call of type TYPE. RESULT is where the result will go (null
8201 for "call"s and "sibcall"s), ADDR is the address of the function,
8202 ARGS_SIZE is the size of the arguments and AUX is the value passed
8203 to us by mips_function_arg. LAZY_P is true if this call already
8204 involves a lazily-bound function address (such as when calling
8205 functions through a MIPS16 hard-float stub).
8206
8207 Return the call itself. */
8208
8209 rtx_insn *
8210 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
8211 rtx args_size, rtx aux, bool lazy_p)
8212 {
8213 rtx orig_addr, pattern;
8214 rtx_insn *insn;
8215 int fp_code;
8216
8217 fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
8218 insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
8219 if (insn)
8220 {
8221 gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
8222 return insn;
8223 }
8224
8225 orig_addr = addr;
8226 if (!call_insn_operand (addr, VOIDmode))
8227 {
8228 if (type == MIPS_CALL_EPILOGUE)
8229 addr = MIPS_EPILOGUE_TEMP (Pmode);
8230 else
8231 addr = gen_reg_rtx (Pmode);
8232 lazy_p |= mips_load_call_address (type, addr, orig_addr);
8233 }
8234
8235 if (result == 0)
8236 {
8237 rtx (*fn) (rtx, rtx);
8238
8239 if (type == MIPS_CALL_SIBCALL)
8240 fn = gen_sibcall_internal;
8241 else
8242 fn = gen_call_internal;
8243
8244 pattern = fn (addr, args_size);
8245 }
8246 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
8247 {
8248 /* Handle return values created by mips_return_fpr_pair. */
8249 rtx (*fn) (rtx, rtx, rtx, rtx);
8250 rtx reg1, reg2;
8251
8252 if (type == MIPS_CALL_SIBCALL)
8253 fn = gen_sibcall_value_multiple_internal;
8254 else
8255 fn = gen_call_value_multiple_internal;
8256
8257 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
8258 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
8259 pattern = fn (reg1, addr, args_size, reg2);
8260 }
8261 else
8262 {
8263 rtx (*fn) (rtx, rtx, rtx);
8264
8265 if (type == MIPS_CALL_SIBCALL)
8266 fn = gen_sibcall_value_internal;
8267 else
8268 fn = gen_call_value_internal;
8269
8270 /* Handle return values created by mips_return_fpr_single. */
8271 if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
8272 result = XEXP (XVECEXP (result, 0, 0), 0);
8273 pattern = fn (result, addr, args_size);
8274 }
8275
8276 return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
8277 }
8278
8279 /* Split call instruction INSN into a $gp-clobbering call and
8280 (where necessary) an instruction to restore $gp from its save slot.
8281 CALL_PATTERN is the pattern of the new call. */
8282
8283 void
8284 mips_split_call (rtx insn, rtx call_pattern)
8285 {
8286 emit_call_insn (call_pattern);
8287 if (!find_reg_note (insn, REG_NORETURN, 0))
8288 mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode,
8289 POST_CALL_TMP_REG));
8290 }
8291
8292 /* Return true if a call to DECL may need to use JALX. */
8293
8294 static bool
8295 mips_call_may_need_jalx_p (tree decl)
8296 {
8297 /* If the current translation unit would use a different mode for DECL,
8298 assume that the call needs JALX. */
8299 if (mips_get_compress_mode (decl) != TARGET_COMPRESSION)
8300 return true;
8301
8302 /* mips_get_compress_mode is always accurate for locally-binding
8303 functions in the current translation unit. */
8304 if (!DECL_EXTERNAL (decl) && targetm.binds_local_p (decl))
8305 return false;
8306
8307 /* When -minterlink-compressed is in effect, assume that functions
8308 could use a different encoding mode unless an attribute explicitly
8309 tells us otherwise. */
8310 if (TARGET_INTERLINK_COMPRESSED)
8311 {
8312 if (!TARGET_COMPRESSION
8313 && mips_get_compress_off_flags (DECL_ATTRIBUTES (decl)) ==0)
8314 return true;
8315 if (TARGET_COMPRESSION
8316 && mips_get_compress_on_flags (DECL_ATTRIBUTES (decl)) == 0)
8317 return true;
8318 }
8319
8320 return false;
8321 }
8322
8323 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
8324
8325 static bool
8326 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
8327 {
8328 if (!TARGET_SIBCALLS)
8329 return false;
8330
8331 /* Interrupt handlers need special epilogue code and therefore can't
8332 use sibcalls. */
8333 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
8334 return false;
8335
8336 /* Direct Js are only possible to functions that use the same ISA encoding.
8337 There is no JX counterpoart of JALX. */
8338 if (decl
8339 && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode)
8340 && mips_call_may_need_jalx_p (decl))
8341 return false;
8342
8343 /* Sibling calls should not prevent lazy binding. Lazy-binding stubs
8344 require $gp to be valid on entry, so sibcalls can only use stubs
8345 if $gp is call-clobbered. */
8346 if (decl
8347 && TARGET_CALL_SAVED_GP
8348 && !TARGET_ABICALLS_PIC0
8349 && !targetm.binds_local_p (decl))
8350 return false;
8351
8352 /* Otherwise OK. */
8353 return true;
8354 }
8355
8356 /* Implement TARGET_USE_MOVE_BY_PIECES_INFRASTRUCTURE_P. */
8357
8358 bool
8359 mips_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
8360 unsigned int align,
8361 enum by_pieces_operation op,
8362 bool speed_p)
8363 {
8364 if (op == STORE_BY_PIECES)
8365 return mips_store_by_pieces_p (size, align);
8366 if (op == MOVE_BY_PIECES && HAVE_cpymemsi)
8367 {
8368 /* cpymemsi is meant to generate code that is at least as good as
8369 move_by_pieces. However, cpymemsi effectively uses a by-pieces
8370 implementation both for moves smaller than a word and for
8371 word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
8372 bytes. We should allow the tree-level optimisers to do such
8373 moves by pieces, as it often exposes other optimization
8374 opportunities. We might as well continue to use cpymemsi at
8375 the rtl level though, as it produces better code when
8376 scheduling is disabled (such as at -O). */
8377 if (currently_expanding_to_rtl)
8378 return false;
8379 if (align < BITS_PER_WORD)
8380 return size < UNITS_PER_WORD;
8381 return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
8382 }
8383
8384 return default_use_by_pieces_infrastructure_p (size, align, op, speed_p);
8385 }
8386
8387 /* Implement a handler for STORE_BY_PIECES operations
8388 for TARGET_USE_MOVE_BY_PIECES_INFRASTRUCTURE_P. */
8389
8390 bool
8391 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
8392 {
8393 /* Storing by pieces involves moving constants into registers
8394 of size MIN (ALIGN, BITS_PER_WORD), then storing them.
8395 We need to decide whether it is cheaper to load the address of
8396 constant data into a register and use a block move instead. */
8397
8398 /* If the data is only byte aligned, then:
8399
8400 (a1) A block move of less than 4 bytes would involve three 3 LBs and
8401 3 SBs. We might as well use 3 single-instruction LIs and 3 SBs
8402 instead.
8403
8404 (a2) A block move of 4 bytes from aligned source data can use an
8405 LW/SWL/SWR sequence. This is often better than the 4 LIs and
8406 4 SBs that we would generate when storing by pieces. */
8407 if (align <= BITS_PER_UNIT)
8408 return size < 4;
8409
8410 /* If the data is 2-byte aligned, then:
8411
8412 (b1) A block move of less than 4 bytes would use a combination of LBs,
8413 LHs, SBs and SHs. We get better code by using single-instruction
8414 LIs, SBs and SHs instead.
8415
8416 (b2) A block move of 4 bytes from aligned source data would again use
8417 an LW/SWL/SWR sequence. In most cases, loading the address of
8418 the source data would require at least one extra instruction.
8419 It is often more efficient to use 2 single-instruction LIs and
8420 2 SHs instead.
8421
8422 (b3) A block move of up to 3 additional bytes would be like (b1).
8423
8424 (b4) A block move of 8 bytes from aligned source data can use two
8425 LW/SWL/SWR sequences or a single LD/SDL/SDR sequence. Both
8426 sequences are better than the 4 LIs and 4 SHs that we'd generate
8427 when storing by pieces.
8428
8429 The reasoning for higher alignments is similar:
8430
8431 (c1) A block move of less than 4 bytes would be the same as (b1).
8432
8433 (c2) A block move of 4 bytes would use an LW/SW sequence. Again,
8434 loading the address of the source data would typically require
8435 at least one extra instruction. It is generally better to use
8436 LUI/ORI/SW instead.
8437
8438 (c3) A block move of up to 3 additional bytes would be like (b1).
8439
8440 (c4) A block move of 8 bytes can use two LW/SW sequences or a single
8441 LD/SD sequence, and in these cases we've traditionally preferred
8442 the memory copy over the more bulky constant moves. */
8443 return size < 8;
8444 }
8445
8446 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
8447 Assume that the areas do not overlap. */
8448
8449 static void
8450 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
8451 {
8452 HOST_WIDE_INT offset, delta;
8453 unsigned HOST_WIDE_INT bits;
8454 int i;
8455 machine_mode mode;
8456 rtx *regs;
8457
8458 /* Work out how many bits to move at a time. If both operands have
8459 half-word alignment, it is usually better to move in half words.
8460 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
8461 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
8462 Otherwise move word-sized chunks.
8463
8464 For ISA_HAS_LWL_LWR we rely on the lwl/lwr & swl/swr load. Otherwise
8465 picking the minimum of alignment or BITS_PER_WORD gets us the
8466 desired size for bits. */
8467 if (ISA_HAS_UNALIGNED_ACCESS)
8468 bits = BITS_PER_WORD;
8469 else if (!ISA_HAS_LWL_LWR)
8470 bits = MIN (BITS_PER_WORD, MIN (MEM_ALIGN (src), MEM_ALIGN (dest)));
8471 else
8472 {
8473 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
8474 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
8475 bits = BITS_PER_WORD / 2;
8476 else
8477 bits = BITS_PER_WORD;
8478 }
8479
8480 mode = int_mode_for_size (bits, 0).require ();
8481 delta = bits / BITS_PER_UNIT;
8482
8483 /* Allocate a buffer for the temporary registers. */
8484 regs = XALLOCAVEC (rtx, length / delta);
8485
8486 /* Load as many BITS-sized chunks as possible. Use a normal load if
8487 the source has enough alignment, otherwise use left/right pairs. */
8488 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
8489 {
8490 regs[i] = gen_reg_rtx (mode);
8491 if (ISA_HAS_UNALIGNED_ACCESS || MEM_ALIGN (src) >= bits)
8492 mips_emit_move (regs[i], adjust_address (src, mode, offset));
8493 else
8494 {
8495 rtx part = adjust_address (src, BLKmode, offset);
8496 set_mem_size (part, delta);
8497 if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
8498 gcc_unreachable ();
8499 }
8500 }
8501
8502 /* Copy the chunks to the destination. */
8503 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
8504 if (ISA_HAS_UNALIGNED_ACCESS || MEM_ALIGN (dest) >= bits)
8505 mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
8506 else
8507 {
8508 rtx part = adjust_address (dest, BLKmode, offset);
8509 set_mem_size (part, delta);
8510 if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
8511 gcc_unreachable ();
8512 }
8513
8514 /* Mop up any left-over bytes. */
8515 if (offset < length)
8516 {
8517 src = adjust_address (src, BLKmode, offset);
8518 dest = adjust_address (dest, BLKmode, offset);
8519 move_by_pieces (dest, src, length - offset,
8520 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), RETURN_BEGIN);
8521 }
8522 }
8523
8524 /* Helper function for doing a loop-based block operation on memory
8525 reference MEM. Each iteration of the loop will operate on LENGTH
8526 bytes of MEM.
8527
8528 Create a new base register for use within the loop and point it to
8529 the start of MEM. Create a new memory reference that uses this
8530 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
8531
8532 static void
8533 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
8534 rtx *loop_reg, rtx *loop_mem)
8535 {
8536 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
8537
8538 /* Although the new mem does not refer to a known location,
8539 it does keep up to LENGTH bytes of alignment. */
8540 *loop_mem = change_address (mem, BLKmode, *loop_reg);
8541 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
8542 }
8543
8544 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
8545 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
8546 the memory regions do not overlap. */
8547
8548 static void
8549 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
8550 HOST_WIDE_INT bytes_per_iter)
8551 {
8552 rtx_code_label *label;
8553 rtx src_reg, dest_reg, final_src, test;
8554 HOST_WIDE_INT leftover;
8555
8556 leftover = length % bytes_per_iter;
8557 length -= leftover;
8558
8559 /* Create registers and memory references for use within the loop. */
8560 mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
8561 mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
8562
8563 /* Calculate the value that SRC_REG should have after the last iteration
8564 of the loop. */
8565 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
8566 0, 0, OPTAB_WIDEN);
8567
8568 /* Emit the start of the loop. */
8569 label = gen_label_rtx ();
8570 emit_label (label);
8571
8572 /* Emit the loop body. */
8573 mips_block_move_straight (dest, src, bytes_per_iter);
8574
8575 /* Move on to the next block. */
8576 mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
8577 mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
8578
8579 /* Emit the loop condition. */
8580 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
8581 if (Pmode == DImode)
8582 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
8583 else
8584 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
8585
8586 /* Mop up any left-over bytes. */
8587 if (leftover)
8588 mips_block_move_straight (dest, src, leftover);
8589 else
8590 /* Temporary fix for PR79150. */
8591 emit_insn (gen_nop ());
8592 }
8593
8594 /* Expand a cpymemsi instruction, which copies LENGTH bytes from
8595 memory reference SRC to memory reference DEST. */
8596
8597 bool
8598 mips_expand_block_move (rtx dest, rtx src, rtx length)
8599 {
8600 if (!CONST_INT_P (length))
8601 return false;
8602
8603 if (mips_isa_rev >= 6 && !ISA_HAS_UNALIGNED_ACCESS
8604 && (MEM_ALIGN (src) < MIPS_MIN_MOVE_MEM_ALIGN
8605 || MEM_ALIGN (dest) < MIPS_MIN_MOVE_MEM_ALIGN))
8606 return false;
8607
8608 if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER)
8609 {
8610 mips_block_move_straight (dest, src, INTVAL (length));
8611 return true;
8612 }
8613 else if (optimize)
8614 {
8615 mips_block_move_loop (dest, src, INTVAL (length),
8616 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
8617 return true;
8618 }
8619
8620 return false;
8621 }
8622
8623 /* Expand a loop of synci insns for the address range [BEGIN, END). */
8624
8625 void
8626 mips_expand_synci_loop (rtx begin, rtx end)
8627 {
8628 rtx inc, cmp_result, mask, length;
8629 rtx_code_label *label, *end_label;
8630
8631 /* Create end_label. */
8632 end_label = gen_label_rtx ();
8633
8634 /* Check if begin equals end. */
8635 cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
8636 emit_jump_insn (gen_condjump (cmp_result, end_label));
8637
8638 /* Load INC with the cache line size (rdhwr INC,$1). */
8639 inc = gen_reg_rtx (Pmode);
8640 emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
8641
8642 /* Check if inc is 0. */
8643 cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
8644 emit_jump_insn (gen_condjump (cmp_result, end_label));
8645
8646 /* Calculate mask. */
8647 mask = mips_force_unary (Pmode, NEG, inc);
8648
8649 /* Mask out begin by mask. */
8650 begin = mips_force_binary (Pmode, AND, begin, mask);
8651
8652 /* Calculate length. */
8653 length = mips_force_binary (Pmode, MINUS, end, begin);
8654
8655 /* Loop back to here. */
8656 label = gen_label_rtx ();
8657 emit_label (label);
8658
8659 emit_insn (gen_synci (begin));
8660
8661 /* Update length. */
8662 mips_emit_binary (MINUS, length, length, inc);
8663
8664 /* Update begin. */
8665 mips_emit_binary (PLUS, begin, begin, inc);
8666
8667 /* Check if length is greater than 0. */
8668 cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
8669 emit_jump_insn (gen_condjump (cmp_result, label));
8670
8671 emit_label (end_label);
8672 }
8673
8674 /* Expand a QI or HI mode atomic memory operation.
8675
8676 GENERATOR contains a pointer to the gen_* function that generates
8677 the SI mode underlying atomic operation using masks that we
8678 calculate.
8679
8680 RESULT is the return register for the operation. Its value is NULL
8681 if unused.
8682
8683 MEM is the location of the atomic access.
8684
8685 OLDVAL is the first operand for the operation.
8686
8687 NEWVAL is the optional second operand for the operation. Its value
8688 is NULL if unused. */
8689
8690 void
8691 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
8692 rtx result, rtx mem, rtx oldval, rtx newval)
8693 {
8694 rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
8695 rtx unshifted_mask_reg, mask, inverted_mask, si_op;
8696 rtx res = NULL;
8697 machine_mode mode;
8698
8699 mode = GET_MODE (mem);
8700
8701 /* Compute the address of the containing SImode value. */
8702 orig_addr = force_reg (Pmode, XEXP (mem, 0));
8703 memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
8704 force_reg (Pmode, GEN_INT (-4)));
8705
8706 /* Create a memory reference for it. */
8707 memsi = gen_rtx_MEM (SImode, memsi_addr);
8708 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
8709 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
8710
8711 /* Work out the byte offset of the QImode or HImode value,
8712 counting from the least significant byte. */
8713 shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
8714 if (TARGET_BIG_ENDIAN)
8715 mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
8716
8717 /* Multiply by eight to convert the shift value from bytes to bits. */
8718 mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
8719
8720 /* Make the final shift an SImode value, so that it can be used in
8721 SImode operations. */
8722 shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
8723
8724 /* Set MASK to an inclusive mask of the QImode or HImode value. */
8725 unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
8726 unshifted_mask_reg = force_reg (SImode, unshifted_mask);
8727 mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
8728
8729 /* Compute the equivalent exclusive mask. */
8730 inverted_mask = gen_reg_rtx (SImode);
8731 emit_insn (gen_rtx_SET (inverted_mask, gen_rtx_NOT (SImode, mask)));
8732
8733 /* Shift the old value into place. */
8734 if (oldval != const0_rtx)
8735 {
8736 oldval = convert_modes (SImode, mode, oldval, true);
8737 oldval = force_reg (SImode, oldval);
8738 oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
8739 }
8740
8741 /* Do the same for the new value. */
8742 if (newval && newval != const0_rtx)
8743 {
8744 newval = convert_modes (SImode, mode, newval, true);
8745 newval = force_reg (SImode, newval);
8746 newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
8747 }
8748
8749 /* Do the SImode atomic access. */
8750 if (result)
8751 res = gen_reg_rtx (SImode);
8752 if (newval)
8753 si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
8754 else if (result)
8755 si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
8756 else
8757 si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
8758
8759 emit_insn (si_op);
8760
8761 if (result)
8762 {
8763 /* Shift and convert the result. */
8764 mips_emit_binary (AND, res, res, mask);
8765 mips_emit_binary (LSHIFTRT, res, res, shiftsi);
8766 mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
8767 }
8768 }
8769
8770 /* Return true if it is possible to use left/right accesses for a
8771 bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
8772 When returning true, update *LEFT and *RIGHT as follows:
8773
8774 *LEFT is a QImode reference to the first byte if big endian or
8775 the last byte if little endian. This address can be used in the
8776 left-side instructions (LWL, SWL, LDL, SDL).
8777
8778 *RIGHT is a QImode reference to the opposite end of the field and
8779 can be used in the patterning right-side instruction. */
8780
8781 static bool
8782 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
8783 rtx *left, rtx *right)
8784 {
8785 rtx first, last;
8786
8787 /* Check that the size is valid. */
8788 if (width != 32 && (!TARGET_64BIT || width != 64))
8789 return false;
8790
8791 /* We can only access byte-aligned values. Since we are always passed
8792 a reference to the first byte of the field, it is not necessary to
8793 do anything with BITPOS after this check. */
8794 if (bitpos % BITS_PER_UNIT != 0)
8795 return false;
8796
8797 /* Reject aligned bitfields: we want to use a normal load or store
8798 instead of a left/right pair. */
8799 if (MEM_ALIGN (op) >= width)
8800 return false;
8801
8802 /* Get references to both ends of the field. */
8803 first = adjust_address (op, QImode, 0);
8804 last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
8805
8806 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
8807 correspond to the MSB and RIGHT to the LSB. */
8808 if (TARGET_BIG_ENDIAN)
8809 *left = first, *right = last;
8810 else
8811 *left = last, *right = first;
8812
8813 return true;
8814 }
8815
8816 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
8817 DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
8818 the operation is the equivalent of:
8819
8820 (set DEST (*_extract SRC WIDTH BITPOS))
8821
8822 Return true on success. */
8823
8824 bool
8825 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
8826 HOST_WIDE_INT bitpos, bool unsigned_p)
8827 {
8828 rtx left, right, temp;
8829 rtx dest1 = NULL_RTX;
8830
8831 /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
8832 be a DImode, create a new temp and emit a zero extend at the end. */
8833 if (GET_MODE (dest) == DImode
8834 && (REG_P (dest) || (SUBREG_P (dest) && !MEM_P (SUBREG_REG (dest))))
8835 && GET_MODE_BITSIZE (SImode) == width)
8836 {
8837 dest1 = dest;
8838 dest = gen_reg_rtx (SImode);
8839 }
8840
8841 if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
8842 return false;
8843
8844 temp = gen_reg_rtx (GET_MODE (dest));
8845 if (GET_MODE (dest) == DImode)
8846 {
8847 emit_insn (gen_mov_ldl (temp, src, left));
8848 emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
8849 }
8850 else
8851 {
8852 emit_insn (gen_mov_lwl (temp, src, left));
8853 emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
8854 }
8855
8856 /* If we were loading 32bits and the original register was DI then
8857 sign/zero extend into the orignal dest. */
8858 if (dest1)
8859 {
8860 if (unsigned_p)
8861 emit_insn (gen_zero_extendsidi2 (dest1, dest));
8862 else
8863 emit_insn (gen_extendsidi2 (dest1, dest));
8864 }
8865 return true;
8866 }
8867
8868 /* Try to use left/right stores to expand an "ins" pattern. DEST, WIDTH,
8869 BITPOS and SRC are the operands passed to the expander; the operation
8870 is the equivalent of:
8871
8872 (set (zero_extract DEST WIDTH BITPOS) SRC)
8873
8874 Return true on success. */
8875
8876 bool
8877 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
8878 HOST_WIDE_INT bitpos)
8879 {
8880 rtx left, right;
8881 machine_mode mode;
8882
8883 if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
8884 return false;
8885
8886 mode = int_mode_for_size (width, 0).require ();
8887 if (TARGET_MIPS16
8888 && src == const0_rtx)
8889 src = force_reg (mode, src);
8890 else
8891 src = gen_lowpart (mode, src);
8892
8893 if (mode == DImode)
8894 {
8895 if (TARGET_MIPS16)
8896 gcc_unreachable ();
8897 emit_insn (gen_mov_sdl (dest, src, left));
8898 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
8899 }
8900 else if (TARGET_MIPS16)
8901 {
8902 emit_insn (gen_mov_swl_mips16e2 (dest, src, left));
8903 emit_insn (gen_mov_swr_mips16e2 (copy_rtx (dest), copy_rtx (src),
8904 right));
8905 }
8906 else
8907 {
8908 emit_insn (gen_mov_swl (dest, src, left));
8909 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
8910 }
8911 return true;
8912 }
8913
8914 /* Return true if X is a MEM with the same size as MODE. */
8915
8916 bool
8917 mips_mem_fits_mode_p (machine_mode mode, rtx x)
8918 {
8919 return (MEM_P (x)
8920 && MEM_SIZE_KNOWN_P (x)
8921 && MEM_SIZE (x) == GET_MODE_SIZE (mode));
8922 }
8923
8924 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
8925 source of an "ext" instruction or the destination of an "ins"
8926 instruction. OP must be a register operand and the following
8927 conditions must hold:
8928
8929 0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
8930 0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
8931 0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
8932
8933 Also reject lengths equal to a word as they are better handled
8934 by the move patterns. */
8935
8936 bool
8937 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
8938 {
8939 if (!ISA_HAS_EXT_INS
8940 || !register_operand (op, VOIDmode)
8941 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
8942 return false;
8943
8944 if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
8945 return false;
8946
8947 if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
8948 return false;
8949
8950 return true;
8951 }
8952
8953 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
8954 operation if MAXLEN is the maxium length of consecutive bits that
8955 can make up MASK. MODE is the mode of the operation. See
8956 mask_low_and_shift_len for the actual definition. */
8957
8958 bool
8959 mask_low_and_shift_p (machine_mode mode, rtx mask, rtx shift, int maxlen)
8960 {
8961 return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
8962 }
8963
8964 /* Return true iff OP1 and OP2 are valid operands together for the
8965 *and<MODE>3 and *and<MODE>3_mips16 patterns. For the cases to consider,
8966 see the table in the comment before the pattern. */
8967
8968 bool
8969 and_operands_ok (machine_mode mode, rtx op1, rtx op2)
8970 {
8971
8972 if (memory_operand (op1, mode))
8973 {
8974 if (TARGET_MIPS16) {
8975 struct mips_address_info addr;
8976 if (!mips_classify_address (&addr, op1, mode, false))
8977 return false;
8978 }
8979 return and_load_operand (op2, mode);
8980 }
8981 else
8982 return and_reg_operand (op2, mode);
8983 }
8984
8985 /* The canonical form of a mask-low-and-shift-left operation is
8986 (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
8987 cleared. Thus we need to shift MASK to the right before checking if it
8988 is a valid mask value. MODE is the mode of the operation. If true
8989 return the length of the mask, otherwise return -1. */
8990
8991 int
8992 mask_low_and_shift_len (machine_mode mode, rtx mask, rtx shift)
8993 {
8994 HOST_WIDE_INT shval;
8995
8996 shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
8997 return exact_log2 ((UINTVAL (mask) >> shval) + 1);
8998 }
8999
9000 /* Return true if -msplit-addresses is selected and should be honored.
9001
9002 -msplit-addresses is a half-way house between explicit relocations
9003 and the traditional assembler macros. It can split absolute 32-bit
9004 symbolic constants into a high/lo_sum pair but uses macros for other
9005 sorts of access.
9006
9007 Like explicit relocation support for REL targets, it relies
9008 on GNU extensions in the assembler and the linker.
9009
9010 Although this code should work for -O0, it has traditionally
9011 been treated as an optimization. */
9012
9013 static bool
9014 mips_split_addresses_p (void)
9015 {
9016 return (TARGET_SPLIT_ADDRESSES
9017 && optimize
9018 && !TARGET_MIPS16
9019 && !flag_pic
9020 && !ABI_HAS_64BIT_SYMBOLS);
9021 }
9022
9023 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs. */
9024
9025 static void
9026 mips_init_relocs (void)
9027 {
9028 memset (mips_split_p, '\0', sizeof (mips_split_p));
9029 memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
9030 memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
9031 memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
9032 memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
9033
9034 if (TARGET_MIPS16_PCREL_LOADS)
9035 mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
9036 else
9037 {
9038 if (ABI_HAS_64BIT_SYMBOLS)
9039 {
9040 if (TARGET_EXPLICIT_RELOCS)
9041 {
9042 mips_split_p[SYMBOL_64_HIGH] = true;
9043 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
9044 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
9045
9046 mips_split_p[SYMBOL_64_MID] = true;
9047 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
9048 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
9049
9050 mips_split_p[SYMBOL_64_LOW] = true;
9051 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
9052 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
9053
9054 mips_split_p[SYMBOL_ABSOLUTE] = true;
9055 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
9056 }
9057 }
9058 else
9059 {
9060 if (TARGET_EXPLICIT_RELOCS
9061 || mips_split_addresses_p ()
9062 || TARGET_MIPS16)
9063 {
9064 mips_split_p[SYMBOL_ABSOLUTE] = true;
9065 mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
9066 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
9067 }
9068 }
9069 }
9070
9071 if (!MIPS16_GP_LOADS && TARGET_MIPS16)
9072 {
9073 /* The high part is provided by a pseudo copy of $gp. */
9074 mips_split_p[SYMBOL_GP_RELATIVE] = true;
9075 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
9076 }
9077 else if (TARGET_EXPLICIT_RELOCS)
9078 /* Small data constants are kept whole until after reload,
9079 then lowered by mips_rewrite_small_data. */
9080 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
9081
9082 if (TARGET_EXPLICIT_RELOCS)
9083 {
9084 mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
9085 if (TARGET_NEWABI)
9086 {
9087 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
9088 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
9089 }
9090 else
9091 {
9092 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
9093 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
9094 }
9095 if (TARGET_MIPS16)
9096 /* Expose the use of $28 as soon as possible. */
9097 mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
9098
9099 if (TARGET_XGOT)
9100 {
9101 /* The HIGH and LO_SUM are matched by special .md patterns. */
9102 mips_split_p[SYMBOL_GOT_DISP] = true;
9103
9104 mips_split_p[SYMBOL_GOTOFF_DISP] = true;
9105 mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
9106 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
9107
9108 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
9109 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
9110 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
9111 }
9112 else
9113 {
9114 if (TARGET_NEWABI)
9115 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
9116 else
9117 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
9118 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
9119 if (TARGET_MIPS16)
9120 /* Expose the use of $28 as soon as possible. */
9121 mips_split_p[SYMBOL_GOT_DISP] = true;
9122 }
9123 }
9124
9125 if (TARGET_NEWABI)
9126 {
9127 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
9128 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
9129 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
9130 }
9131
9132 mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
9133 mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
9134
9135 if (TARGET_MIPS16_PCREL_LOADS)
9136 {
9137 mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
9138 mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
9139 }
9140 else
9141 {
9142 mips_split_p[SYMBOL_DTPREL] = true;
9143 mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
9144 mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
9145
9146 mips_split_p[SYMBOL_TPREL] = true;
9147 mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
9148 mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
9149 }
9150
9151 mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
9152 mips_lo_relocs[SYMBOL_HALF] = "%half(";
9153 }
9154
9155 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
9156 in context CONTEXT. RELOCS is the array of relocations to use. */
9157
9158 static void
9159 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
9160 const char **relocs)
9161 {
9162 enum mips_symbol_type symbol_type;
9163 const char *p;
9164
9165 symbol_type = mips_classify_symbolic_expression (op, context);
9166 gcc_assert (relocs[symbol_type]);
9167
9168 fputs (relocs[symbol_type], file);
9169 output_addr_const (file, mips_strip_unspec_address (op));
9170 for (p = relocs[symbol_type]; *p != 0; p++)
9171 if (*p == '(')
9172 fputc (')', file);
9173 }
9174
9175 /* Start a new block with the given asm switch enabled. If we need
9176 to print a directive, emit PREFIX before it and SUFFIX after it. */
9177
9178 static void
9179 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
9180 const char *prefix, const char *suffix)
9181 {
9182 if (asm_switch->nesting_level == 0)
9183 fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
9184 asm_switch->nesting_level++;
9185 }
9186
9187 /* Likewise, but end a block. */
9188
9189 static void
9190 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
9191 const char *prefix, const char *suffix)
9192 {
9193 gcc_assert (asm_switch->nesting_level);
9194 asm_switch->nesting_level--;
9195 if (asm_switch->nesting_level == 0)
9196 fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
9197 }
9198
9199 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
9200 that either print a complete line or print nothing. */
9201
9202 void
9203 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
9204 {
9205 mips_push_asm_switch_1 (asm_switch, "\t", "\n");
9206 }
9207
9208 void
9209 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
9210 {
9211 mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
9212 }
9213
9214 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
9215 The punctuation characters are:
9216
9217 '(' Start a nested ".set noreorder" block.
9218 ')' End a nested ".set noreorder" block.
9219 '[' Start a nested ".set noat" block.
9220 ']' End a nested ".set noat" block.
9221 '<' Start a nested ".set nomacro" block.
9222 '>' End a nested ".set nomacro" block.
9223 '*' Behave like %(%< if generating a delayed-branch sequence.
9224 '#' Print a nop if in a ".set noreorder" block.
9225 '/' Like '#', but do nothing within a delayed-branch sequence.
9226 '?' Print "l" if mips_branch_likely is true
9227 '~' Print a nop if mips_branch_likely is true
9228 '.' Print the name of the register with a hard-wired zero (zero or $0).
9229 '@' Print the name of the assembler temporary register (at or $1).
9230 '^' Print the name of the pic call-through register (t9 or $25).
9231 '+' Print the name of the gp register (usually gp or $28).
9232 '$' Print the name of the stack pointer register (sp or $29).
9233 ':' Print "c" to use the compact version if the delay slot is a nop.
9234 '!' Print "s" to use the short version if the delay slot contains a
9235 16-bit instruction.
9236
9237 See also mips_init_print_operand_punct. */
9238
9239 static void
9240 mips_print_operand_punctuation (FILE *file, int ch)
9241 {
9242 switch (ch)
9243 {
9244 case '(':
9245 mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
9246 break;
9247
9248 case ')':
9249 mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
9250 break;
9251
9252 case '[':
9253 mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
9254 break;
9255
9256 case ']':
9257 mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
9258 break;
9259
9260 case '<':
9261 mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
9262 break;
9263
9264 case '>':
9265 mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
9266 break;
9267
9268 case '*':
9269 if (final_sequence != 0)
9270 {
9271 mips_print_operand_punctuation (file, '(');
9272 mips_print_operand_punctuation (file, '<');
9273 }
9274 break;
9275
9276 case '#':
9277 if (mips_noreorder.nesting_level > 0)
9278 fputs ("\n\tnop", file);
9279 break;
9280
9281 case '/':
9282 /* Print an extra newline so that the delayed insn is separated
9283 from the following ones. This looks neater and is consistent
9284 with non-nop delayed sequences. */
9285 if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
9286 fputs ("\n\tnop\n", file);
9287 break;
9288
9289 case '?':
9290 if (mips_branch_likely)
9291 putc ('l', file);
9292 break;
9293
9294 case '~':
9295 if (mips_branch_likely)
9296 fputs ("\n\tnop", file);
9297 break;
9298
9299 case '.':
9300 fputs (reg_names[GP_REG_FIRST + 0], file);
9301 break;
9302
9303 case '@':
9304 fputs (reg_names[AT_REGNUM], file);
9305 break;
9306
9307 case '^':
9308 fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
9309 break;
9310
9311 case '+':
9312 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
9313 break;
9314
9315 case '$':
9316 fputs (reg_names[STACK_POINTER_REGNUM], file);
9317 break;
9318
9319 case ':':
9320 /* When final_sequence is 0, the delay slot will be a nop. We can
9321 use the compact version where available. The %: formatter will
9322 only be present if a compact form of the branch is available. */
9323 if (final_sequence == 0)
9324 putc ('c', file);
9325 break;
9326
9327 case '!':
9328 /* If the delay slot instruction is short, then use the
9329 compact version. */
9330 if (TARGET_MICROMIPS && !TARGET_INTERLINK_COMPRESSED && mips_isa_rev <= 5
9331 && (final_sequence == 0
9332 || get_attr_length (final_sequence->insn (1)) == 2))
9333 putc ('s', file);
9334 break;
9335
9336 default:
9337 gcc_unreachable ();
9338 break;
9339 }
9340 }
9341
9342 /* Initialize mips_print_operand_punct. */
9343
9344 static void
9345 mips_init_print_operand_punct (void)
9346 {
9347 const char *p;
9348
9349 for (p = "()[]<>*#/?~.@^+$:!"; *p; p++)
9350 mips_print_operand_punct[(unsigned char) *p] = true;
9351 }
9352
9353 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
9354 associated with condition CODE. Print the condition part of the
9355 opcode to FILE. */
9356
9357 static void
9358 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
9359 {
9360 switch (code)
9361 {
9362 case EQ:
9363 case NE:
9364 case GT:
9365 case GE:
9366 case LT:
9367 case LE:
9368 case GTU:
9369 case GEU:
9370 case LTU:
9371 case LEU:
9372 /* Conveniently, the MIPS names for these conditions are the same
9373 as their RTL equivalents. */
9374 fputs (GET_RTX_NAME (code), file);
9375 break;
9376
9377 default:
9378 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
9379 break;
9380 }
9381 }
9382
9383 /* Likewise floating-point branches. */
9384
9385 static void
9386 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
9387 {
9388 switch (code)
9389 {
9390 case EQ:
9391 if (ISA_HAS_CCF)
9392 fputs ("c1eqz", file);
9393 else
9394 fputs ("c1f", file);
9395 break;
9396
9397 case NE:
9398 if (ISA_HAS_CCF)
9399 fputs ("c1nez", file);
9400 else
9401 fputs ("c1t", file);
9402 break;
9403
9404 default:
9405 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
9406 break;
9407 }
9408 }
9409
9410 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
9411
9412 static bool
9413 mips_print_operand_punct_valid_p (unsigned char code)
9414 {
9415 return mips_print_operand_punct[code];
9416 }
9417
9418 /* Implement TARGET_PRINT_OPERAND. The MIPS-specific operand codes are:
9419
9420 'E' Print CONST_INT OP element 0 of a replicated CONST_VECTOR in decimal.
9421 'X' Print CONST_INT OP in hexadecimal format.
9422 'x' Print the low 16 bits of CONST_INT OP in hexadecimal format.
9423 'd' Print CONST_INT OP in decimal.
9424 'B' Print CONST_INT OP element 0 of a replicated CONST_VECTOR
9425 as an unsigned byte [0..255].
9426 'm' Print one less than CONST_INT OP in decimal.
9427 'y' Print exact log2 of CONST_INT OP in decimal.
9428 'h' Print the high-part relocation associated with OP, after stripping
9429 any outermost HIGH.
9430 'R' Print the low-part relocation associated with OP.
9431 'C' Print the integer branch condition for comparison OP.
9432 'N' Print the inverse of the integer branch condition for comparison OP.
9433 'F' Print the FPU branch condition for comparison OP.
9434 'W' Print the inverse of the FPU branch condition for comparison OP.
9435 'w' Print a MSA register.
9436 'T' Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
9437 'z' for (eq:?I ...), 'n' for (ne:?I ...).
9438 't' Like 'T', but with the EQ/NE cases reversed
9439 'Y' Print mips_fp_conditions[INTVAL (OP)]
9440 'Z' Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
9441 'q' Print a DSP accumulator register.
9442 'D' Print the second part of a double-word register or memory operand.
9443 'L' Print the low-order register in a double-word register operand.
9444 'M' Print high-order register in a double-word register operand.
9445 'z' Print $0 if OP is zero, otherwise print OP normally.
9446 'b' Print the address of a memory operand, without offset.
9447 'v' Print the insn size suffix b, h, w or d for vector modes V16QI, V8HI,
9448 V4SI, V2SI, and w, d for vector modes V4SF, V2DF respectively.
9449 'V' Print exact log2 of CONST_INT OP element 0 of a replicated
9450 CONST_VECTOR in decimal. */
9451
9452 static void
9453 mips_print_operand (FILE *file, rtx op, int letter)
9454 {
9455 enum rtx_code code;
9456
9457 if (mips_print_operand_punct_valid_p (letter))
9458 {
9459 mips_print_operand_punctuation (file, letter);
9460 return;
9461 }
9462
9463 gcc_assert (op);
9464 code = GET_CODE (op);
9465
9466 switch (letter)
9467 {
9468 case 'E':
9469 if (GET_CODE (op) == CONST_VECTOR)
9470 {
9471 gcc_assert (mips_const_vector_same_val_p (op, GET_MODE (op)));
9472 op = CONST_VECTOR_ELT (op, 0);
9473 gcc_assert (CONST_INT_P (op));
9474 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
9475 }
9476 else
9477 output_operand_lossage ("invalid use of '%%%c'", letter);
9478 break;
9479
9480 case 'X':
9481 if (CONST_INT_P (op))
9482 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
9483 else
9484 output_operand_lossage ("invalid use of '%%%c'", letter);
9485 break;
9486
9487 case 'x':
9488 if (CONST_INT_P (op))
9489 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
9490 else
9491 output_operand_lossage ("invalid use of '%%%c'", letter);
9492 break;
9493
9494 case 'd':
9495 if (CONST_INT_P (op))
9496 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
9497 else
9498 output_operand_lossage ("invalid use of '%%%c'", letter);
9499 break;
9500
9501 case 'B':
9502 if (GET_CODE (op) == CONST_VECTOR)
9503 {
9504 gcc_assert (mips_const_vector_same_val_p (op, GET_MODE (op)));
9505 op = CONST_VECTOR_ELT (op, 0);
9506 gcc_assert (CONST_INT_P (op));
9507 unsigned HOST_WIDE_INT val8 = UINTVAL (op) & GET_MODE_MASK (QImode);
9508 fprintf (file, HOST_WIDE_INT_PRINT_UNSIGNED, val8);
9509 }
9510 else
9511 output_operand_lossage ("invalid use of '%%%c'", letter);
9512 break;
9513
9514 case 'm':
9515 if (CONST_INT_P (op))
9516 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
9517 else
9518 output_operand_lossage ("invalid use of '%%%c'", letter);
9519 break;
9520
9521 case 'y':
9522 if (CONST_INT_P (op))
9523 {
9524 int val = exact_log2 (INTVAL (op));
9525 if (val != -1)
9526 fprintf (file, "%d", val);
9527 else
9528 output_operand_lossage ("invalid use of '%%%c'", letter);
9529 }
9530 else
9531 output_operand_lossage ("invalid use of '%%%c'", letter);
9532 break;
9533
9534 case 'V':
9535 if (GET_CODE (op) == CONST_VECTOR)
9536 {
9537 machine_mode mode = GET_MODE_INNER (GET_MODE (op));
9538 unsigned HOST_WIDE_INT val = UINTVAL (CONST_VECTOR_ELT (op, 0));
9539 int vlog2 = exact_log2 (val & GET_MODE_MASK (mode));
9540 if (vlog2 != -1)
9541 fprintf (file, "%d", vlog2);
9542 else
9543 output_operand_lossage ("invalid use of '%%%c'", letter);
9544 }
9545 else
9546 output_operand_lossage ("invalid use of '%%%c'", letter);
9547 break;
9548
9549 case 'h':
9550 if (code == HIGH)
9551 op = XEXP (op, 0);
9552 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
9553 break;
9554
9555 case 'R':
9556 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
9557 break;
9558
9559 case 'C':
9560 mips_print_int_branch_condition (file, code, letter);
9561 break;
9562
9563 case 'N':
9564 mips_print_int_branch_condition (file, reverse_condition (code), letter);
9565 break;
9566
9567 case 'F':
9568 mips_print_float_branch_condition (file, code, letter);
9569 break;
9570
9571 case 'W':
9572 mips_print_float_branch_condition (file, reverse_condition (code),
9573 letter);
9574 break;
9575
9576 case 'T':
9577 case 't':
9578 {
9579 int truth = (code == NE) == (letter == 'T');
9580 fputc ("zfnt"[truth * 2 + ST_REG_P (REGNO (XEXP (op, 0)))], file);
9581 }
9582 break;
9583
9584 case 'Y':
9585 if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
9586 fputs (mips_fp_conditions[UINTVAL (op)], file);
9587 else
9588 output_operand_lossage ("'%%%c' is not a valid operand prefix",
9589 letter);
9590 break;
9591
9592 case 'Z':
9593 if (ISA_HAS_8CC || ISA_HAS_CCF)
9594 {
9595 mips_print_operand (file, op, 0);
9596 fputc (',', file);
9597 }
9598 break;
9599
9600 case 'q':
9601 if (code == REG && MD_REG_P (REGNO (op)))
9602 fprintf (file, "$ac0");
9603 else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
9604 fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
9605 else
9606 output_operand_lossage ("invalid use of '%%%c'", letter);
9607 break;
9608
9609 case 'w':
9610 if (code == REG && MSA_REG_P (REGNO (op)))
9611 fprintf (file, "$w%s", &reg_names[REGNO (op)][2]);
9612 else
9613 output_operand_lossage ("invalid use of '%%%c'", letter);
9614 break;
9615
9616 case 'v':
9617 switch (GET_MODE (op))
9618 {
9619 case E_V16QImode:
9620 fprintf (file, "b");
9621 break;
9622 case E_V8HImode:
9623 fprintf (file, "h");
9624 break;
9625 case E_V4SImode:
9626 case E_V4SFmode:
9627 fprintf (file, "w");
9628 break;
9629 case E_V2DImode:
9630 case E_V2DFmode:
9631 fprintf (file, "d");
9632 break;
9633 default:
9634 output_operand_lossage ("invalid use of '%%%c'", letter);
9635 }
9636 break;
9637
9638 default:
9639 switch (code)
9640 {
9641 case REG:
9642 {
9643 unsigned int regno = REGNO (op);
9644 if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
9645 || (letter == 'L' && TARGET_BIG_ENDIAN)
9646 || letter == 'D')
9647 regno++;
9648 else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
9649 output_operand_lossage ("invalid use of '%%%c'", letter);
9650 /* We need to print $0 .. $31 for COP0 registers. */
9651 if (COP0_REG_P (regno))
9652 fprintf (file, "$%s", &reg_names[regno][4]);
9653 else
9654 fprintf (file, "%s", reg_names[regno]);
9655 }
9656 break;
9657
9658 case MEM:
9659 if (letter == 'D')
9660 output_address (GET_MODE (op), plus_constant (Pmode,
9661 XEXP (op, 0), 4));
9662 else if (letter == 'b')
9663 {
9664 gcc_assert (REG_P (XEXP (op, 0)));
9665 mips_print_operand (file, XEXP (op, 0), 0);
9666 }
9667 else if (letter && letter != 'z')
9668 output_operand_lossage ("invalid use of '%%%c'", letter);
9669 else
9670 output_address (GET_MODE (op), XEXP (op, 0));
9671 break;
9672
9673 default:
9674 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
9675 fputs (reg_names[GP_REG_FIRST], file);
9676 else if (letter && letter != 'z')
9677 output_operand_lossage ("invalid use of '%%%c'", letter);
9678 else if (CONST_GP_P (op))
9679 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
9680 else
9681 output_addr_const (file, mips_strip_unspec_address (op));
9682 break;
9683 }
9684 }
9685 }
9686
9687 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
9688
9689 static void
9690 mips_print_operand_address (FILE *file, machine_mode /*mode*/, rtx x)
9691 {
9692 struct mips_address_info addr;
9693
9694 if (mips_classify_address (&addr, x, word_mode, true))
9695 switch (addr.type)
9696 {
9697 case ADDRESS_REG:
9698 mips_print_operand (file, addr.offset, 0);
9699 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
9700 return;
9701
9702 case ADDRESS_LO_SUM:
9703 mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
9704 mips_lo_relocs);
9705 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
9706 return;
9707
9708 case ADDRESS_CONST_INT:
9709 output_addr_const (file, x);
9710 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
9711 return;
9712
9713 case ADDRESS_SYMBOLIC:
9714 output_addr_const (file, mips_strip_unspec_address (x));
9715 return;
9716 }
9717 gcc_unreachable ();
9718 }
9719
9720 /* Implement TARGET_ENCODE_SECTION_INFO. */
9721
9722 static void
9723 mips_encode_section_info (tree decl, rtx rtl, int first)
9724 {
9725 default_encode_section_info (decl, rtl, first);
9726
9727 if (TREE_CODE (decl) == FUNCTION_DECL)
9728 {
9729 rtx symbol = XEXP (rtl, 0);
9730 tree type = TREE_TYPE (decl);
9731
9732 /* Encode whether the symbol is short or long. */
9733 if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
9734 || mips_far_type_p (type))
9735 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
9736 }
9737 }
9738
9739 /* Implement TARGET_SELECT_RTX_SECTION. */
9740
9741 static section *
9742 mips_select_rtx_section (machine_mode mode, rtx x,
9743 unsigned HOST_WIDE_INT align)
9744 {
9745 /* ??? Consider using mergeable small data sections. */
9746 if (mips_rtx_constant_in_small_data_p (mode))
9747 return get_named_section (NULL, ".sdata", 0);
9748
9749 return default_elf_select_rtx_section (mode, x, align);
9750 }
9751
9752 /* Implement TARGET_IN_SMALL_DATA_P. */
9753
9754 static bool
9755 mips_in_small_data_p (const_tree decl)
9756 {
9757 unsigned HOST_WIDE_INT size;
9758
9759 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
9760 return false;
9761
9762 /* We don't yet generate small-data references for -mabicalls
9763 or VxWorks RTP code. See the related -G handling in
9764 mips_option_override. */
9765 if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
9766 return false;
9767
9768 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
9769 {
9770 const char *name;
9771
9772 /* Reject anything that isn't in a known small-data section. */
9773 name = DECL_SECTION_NAME (decl);
9774 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
9775 return false;
9776
9777 /* If a symbol is defined externally, the assembler will use the
9778 usual -G rules when deciding how to implement macros. */
9779 if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
9780 return true;
9781 }
9782 else if (TARGET_EMBEDDED_DATA)
9783 {
9784 /* Don't put constants into the small data section: we want them
9785 to be in ROM rather than RAM. */
9786 if (TREE_CODE (decl) != VAR_DECL)
9787 return false;
9788
9789 if (TREE_READONLY (decl)
9790 && !TREE_SIDE_EFFECTS (decl)
9791 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
9792 return false;
9793 }
9794
9795 /* Enforce -mlocal-sdata. */
9796 if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
9797 return false;
9798
9799 /* Enforce -mextern-sdata. */
9800 if (!TARGET_EXTERN_SDATA && DECL_P (decl))
9801 {
9802 if (DECL_EXTERNAL (decl))
9803 return false;
9804 if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
9805 return false;
9806 }
9807
9808 /* We have traditionally not treated zero-sized objects as small data,
9809 so this is now effectively part of the ABI. */
9810 size = int_size_in_bytes (TREE_TYPE (decl));
9811 return size > 0 && size <= mips_small_data_threshold;
9812 }
9813
9814 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P. We don't want to use
9815 anchors for small data: the GP register acts as an anchor in that
9816 case. We also don't want to use them for PC-relative accesses,
9817 where the PC acts as an anchor. */
9818
9819 static bool
9820 mips_use_anchors_for_symbol_p (const_rtx symbol)
9821 {
9822 switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
9823 {
9824 case SYMBOL_PC_RELATIVE:
9825 case SYMBOL_GP_RELATIVE:
9826 return false;
9827
9828 default:
9829 return default_use_anchors_for_symbol_p (symbol);
9830 }
9831 }
9832
9833 /* The MIPS debug format wants all automatic variables and arguments
9834 to be in terms of the virtual frame pointer (stack pointer before
9835 any adjustment in the function), while the MIPS 3.0 linker wants
9836 the frame pointer to be the stack pointer after the initial
9837 adjustment. So, we do the adjustment here. The arg pointer (which
9838 is eliminated) points to the virtual frame pointer, while the frame
9839 pointer (which may be eliminated) points to the stack pointer after
9840 the initial adjustments. */
9841
9842 HOST_WIDE_INT
9843 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
9844 {
9845 rtx offset2 = const0_rtx;
9846 rtx reg = eliminate_constant_term (addr, &offset2);
9847
9848 if (offset == 0)
9849 offset = INTVAL (offset2);
9850
9851 if (reg == stack_pointer_rtx
9852 || reg == frame_pointer_rtx
9853 || reg == hard_frame_pointer_rtx)
9854 {
9855 offset -= cfun->machine->frame.total_size;
9856 if (reg == hard_frame_pointer_rtx)
9857 offset += cfun->machine->frame.hard_frame_pointer_offset;
9858 }
9859
9860 return offset;
9861 }
9862
9863 /* Implement ASM_OUTPUT_EXTERNAL. */
9864
9865 void
9866 mips_output_external (FILE *file, tree decl, const char *name)
9867 {
9868 default_elf_asm_output_external (file, decl, name);
9869
9870 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
9871 set in order to avoid putting out names that are never really
9872 used. */
9873 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
9874 {
9875 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
9876 {
9877 /* When using assembler macros, emit .extern directives for
9878 all small-data externs so that the assembler knows how
9879 big they are.
9880
9881 In most cases it would be safe (though pointless) to emit
9882 .externs for other symbols too. One exception is when an
9883 object is within the -G limit but declared by the user to
9884 be in a section other than .sbss or .sdata. */
9885 fputs ("\t.extern\t", file);
9886 assemble_name (file, name);
9887 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
9888 int_size_in_bytes (TREE_TYPE (decl)));
9889 }
9890 }
9891 }
9892
9893 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
9894
9895 static void
9896 mips_output_filename (FILE *stream, const char *name)
9897 {
9898 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
9899 directives. */
9900 if (dwarf_debuginfo_p ())
9901 return;
9902 else if (mips_output_filename_first_time)
9903 {
9904 mips_output_filename_first_time = 0;
9905 num_source_filenames += 1;
9906 current_function_file = name;
9907 fprintf (stream, "\t.file\t%d ", num_source_filenames);
9908 output_quoted_string (stream, name);
9909 putc ('\n', stream);
9910 }
9911 else if (name != current_function_file
9912 && strcmp (name, current_function_file) != 0)
9913 {
9914 num_source_filenames += 1;
9915 current_function_file = name;
9916 fprintf (stream, "\t.file\t%d ", num_source_filenames);
9917 output_quoted_string (stream, name);
9918 putc ('\n', stream);
9919 }
9920 }
9921
9922 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL. */
9923
9924 static void ATTRIBUTE_UNUSED
9925 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
9926 {
9927 switch (size)
9928 {
9929 case 4:
9930 fputs ("\t.dtprelword\t", file);
9931 break;
9932
9933 case 8:
9934 fputs ("\t.dtpreldword\t", file);
9935 break;
9936
9937 default:
9938 gcc_unreachable ();
9939 }
9940 output_addr_const (file, x);
9941 fputs ("+0x8000", file);
9942 }
9943
9944 /* Implement TARGET_DWARF_REGISTER_SPAN. */
9945
9946 static rtx
9947 mips_dwarf_register_span (rtx reg)
9948 {
9949 rtx high, low;
9950 machine_mode mode;
9951
9952 /* TARGET_FLOATXX is implemented as 32-bit floating-point registers but
9953 ensures that double-precision registers are treated as if they were
9954 64-bit physical registers. The code will run correctly with 32-bit or
9955 64-bit registers which means that dwarf information cannot be precise
9956 for all scenarios. We choose to state that the 64-bit values are stored
9957 in a single 64-bit 'piece'. This slightly unusual construct can then be
9958 interpreted as either a pair of registers if the registers are 32-bit or
9959 a single 64-bit register depending on hardware. */
9960 mode = GET_MODE (reg);
9961 if (FP_REG_P (REGNO (reg))
9962 && TARGET_FLOATXX
9963 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
9964 {
9965 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, reg));
9966 }
9967 /* By default, GCC maps increasing register numbers to increasing
9968 memory locations, but paired FPRs are always little-endian,
9969 regardless of the prevailing endianness. */
9970 else if (FP_REG_P (REGNO (reg))
9971 && TARGET_BIG_ENDIAN
9972 && MAX_FPRS_PER_FMT > 1
9973 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
9974 {
9975 gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
9976 high = mips_subword (reg, true);
9977 low = mips_subword (reg, false);
9978 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
9979 }
9980
9981 return NULL_RTX;
9982 }
9983
9984 /* Implement TARGET_DWARF_FRAME_REG_MODE. */
9985
9986 static machine_mode
9987 mips_dwarf_frame_reg_mode (int regno)
9988 {
9989 machine_mode mode = default_dwarf_frame_reg_mode (regno);
9990
9991 if (FP_REG_P (regno) && mips_abi == ABI_32 && !TARGET_FLOAT32)
9992 mode = SImode;
9993
9994 return mode;
9995 }
9996
9997 /* DSP ALU can bypass data with no delays for the following pairs. */
9998 enum insn_code dspalu_bypass_table[][2] =
9999 {
10000 {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
10001 {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
10002 {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
10003 {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
10004 {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
10005 {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
10006 {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
10007 {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
10008 };
10009
10010 int
10011 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
10012 {
10013 int i;
10014 int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
10015 enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
10016 enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
10017
10018 for (i = 0; i < num_bypass; i++)
10019 {
10020 if (out_icode == dspalu_bypass_table[i][0]
10021 && in_icode == dspalu_bypass_table[i][1])
10022 return true;
10023 }
10024
10025 return false;
10026 }
10027 /* Implement ASM_OUTPUT_ASCII. */
10028
10029 void
10030 mips_output_ascii (FILE *stream, const char *string, size_t len)
10031 {
10032 size_t i;
10033 int cur_pos;
10034
10035 cur_pos = 17;
10036 fprintf (stream, "\t.ascii\t\"");
10037 for (i = 0; i < len; i++)
10038 {
10039 int c;
10040
10041 c = (unsigned char) string[i];
10042 if (ISPRINT (c))
10043 {
10044 if (c == '\\' || c == '\"')
10045 {
10046 putc ('\\', stream);
10047 cur_pos++;
10048 }
10049 putc (c, stream);
10050 cur_pos++;
10051 }
10052 else
10053 {
10054 fprintf (stream, "\\%03o", c);
10055 cur_pos += 4;
10056 }
10057
10058 if (cur_pos > 72 && i+1 < len)
10059 {
10060 cur_pos = 17;
10061 fprintf (stream, "\"\n\t.ascii\t\"");
10062 }
10063 }
10064 fprintf (stream, "\"\n");
10065 }
10066
10067 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
10068 Update *ADDR with the operand that should be printed. */
10069
10070 const char *
10071 mips_output_tls_reloc_directive (rtx *addr)
10072 {
10073 enum mips_symbol_type type;
10074
10075 type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
10076 *addr = mips_strip_unspec_address (*addr);
10077 switch (type)
10078 {
10079 case SYMBOL_DTPREL:
10080 return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
10081
10082 case SYMBOL_TPREL:
10083 return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
10084
10085 default:
10086 gcc_unreachable ();
10087 }
10088 }
10089
10090 /* Emit either a label, .comm, or .lcomm directive. When using assembler
10091 macros, mark the symbol as written so that mips_asm_output_external
10092 won't emit an .extern for it. STREAM is the output file, NAME is the
10093 name of the symbol, INIT_STRING is the string that should be written
10094 before the symbol and FINAL_STRING is the string that should be
10095 written after it. FINAL_STRING is a printf format that consumes the
10096 remaining arguments. */
10097
10098 void
10099 mips_declare_object (FILE *stream, const char *name, const char *init_string,
10100 const char *final_string, ...)
10101 {
10102 va_list ap;
10103
10104 fputs (init_string, stream);
10105 assemble_name (stream, name);
10106 va_start (ap, final_string);
10107 vfprintf (stream, final_string, ap);
10108 va_end (ap);
10109
10110 if (!TARGET_EXPLICIT_RELOCS)
10111 {
10112 tree name_tree = get_identifier (name);
10113 TREE_ASM_WRITTEN (name_tree) = 1;
10114 }
10115 }
10116
10117 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
10118 NAME is the name of the object and ALIGN is the required alignment
10119 in bytes. TAKES_ALIGNMENT_P is true if the directive takes a third
10120 alignment argument. */
10121
10122 void
10123 mips_declare_common_object (FILE *stream, const char *name,
10124 const char *init_string,
10125 unsigned HOST_WIDE_INT size,
10126 unsigned int align, bool takes_alignment_p)
10127 {
10128 if (!takes_alignment_p)
10129 {
10130 size += (align / BITS_PER_UNIT) - 1;
10131 size -= size % (align / BITS_PER_UNIT);
10132 mips_declare_object (stream, name, init_string,
10133 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
10134 }
10135 else
10136 mips_declare_object (stream, name, init_string,
10137 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
10138 size, align / BITS_PER_UNIT);
10139 }
10140
10141 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as the
10142 elfos.h version, but we also need to handle -muninit-const-in-rodata. */
10143
10144 void
10145 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
10146 unsigned HOST_WIDE_INT size,
10147 unsigned int align)
10148 {
10149 /* If the target wants uninitialized const declarations in
10150 .rdata then don't put them in .comm. */
10151 if (TARGET_EMBEDDED_DATA
10152 && TARGET_UNINIT_CONST_IN_RODATA
10153 && VAR_P (decl)
10154 && TREE_READONLY (decl)
10155 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
10156 {
10157 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
10158 targetm.asm_out.globalize_label (stream, name);
10159
10160 switch_to_section (readonly_data_section);
10161 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
10162 mips_declare_object (stream, name, "",
10163 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
10164 size);
10165 }
10166 else
10167 mips_declare_common_object (stream, name, "\n\t.comm\t",
10168 size, align, true);
10169 }
10170
10171 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
10172 extern int size_directive_output;
10173
10174 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
10175 definitions except that it uses mips_declare_object to emit the label. */
10176
10177 void
10178 mips_declare_object_name (FILE *stream, const char *name,
10179 tree decl ATTRIBUTE_UNUSED)
10180 {
10181 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
10182 #ifdef USE_GNU_UNIQUE_OBJECT
10183 /* As in elfos.h. */
10184 if (USE_GNU_UNIQUE_OBJECT && DECL_ONE_ONLY (decl)
10185 && (!DECL_ARTIFICIAL (decl) || !TREE_READONLY (decl)))
10186 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "gnu_unique_object");
10187 else
10188 #endif
10189 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
10190 #endif
10191
10192 size_directive_output = 0;
10193 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
10194 {
10195 HOST_WIDE_INT size;
10196
10197 size_directive_output = 1;
10198 size = int_size_in_bytes (TREE_TYPE (decl));
10199 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
10200 }
10201
10202 mips_declare_object (stream, name, "", ":\n");
10203 }
10204
10205 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
10206
10207 void
10208 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
10209 {
10210 const char *name;
10211
10212 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
10213 if (!flag_inhibit_size_directive
10214 && DECL_SIZE (decl) != 0
10215 && !at_end
10216 && top_level
10217 && DECL_INITIAL (decl) == error_mark_node
10218 && !size_directive_output)
10219 {
10220 HOST_WIDE_INT size;
10221
10222 size_directive_output = 1;
10223 size = int_size_in_bytes (TREE_TYPE (decl));
10224 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
10225 }
10226 }
10227 #endif
10228
10229 /* Mark text contents as code or data, mainly for the purpose of correct
10230 disassembly. Emit a local symbol and set its type appropriately for
10231 that purpose. Also emit `.insn' if marking contents as code so that
10232 the ISA mode is recorded and any padding that follows is disassembled
10233 as correct instructions. */
10234
10235 void
10236 mips_set_text_contents_type (FILE *file ATTRIBUTE_UNUSED,
10237 const char *prefix ATTRIBUTE_UNUSED,
10238 unsigned HOST_WIDE_INT num ATTRIBUTE_UNUSED,
10239 bool function_p ATTRIBUTE_UNUSED)
10240 {
10241 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
10242 char buf[(sizeof (num) * 10) / 4 + 2];
10243 const char *fnname;
10244 char *sname;
10245 rtx symbol;
10246
10247 sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, num);
10248 symbol = XEXP (DECL_RTL (current_function_decl), 0);
10249 fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
10250 sname = ACONCAT ((prefix, fnname, "_", buf, NULL));
10251
10252 ASM_OUTPUT_TYPE_DIRECTIVE (file, sname, function_p ? "function" : "object");
10253 assemble_name (file, sname);
10254 fputs (":\n", file);
10255 if (function_p)
10256 fputs ("\t.insn\n", file);
10257 #endif
10258 }
10259
10260 /* Return the FOO in the name of the ".mdebug.FOO" section associated
10261 with the current ABI. */
10262
10263 static const char *
10264 mips_mdebug_abi_name (void)
10265 {
10266 switch (mips_abi)
10267 {
10268 case ABI_32:
10269 return "abi32";
10270 case ABI_O64:
10271 return "abiO64";
10272 case ABI_N32:
10273 return "abiN32";
10274 case ABI_64:
10275 return "abi64";
10276 case ABI_EABI:
10277 return TARGET_64BIT ? "eabi64" : "eabi32";
10278 default:
10279 gcc_unreachable ();
10280 }
10281 }
10282
10283 /* Implement TARGET_ASM_FILE_START. */
10284
10285 static void
10286 mips_file_start (void)
10287 {
10288 default_file_start ();
10289
10290 /* Generate a special section to describe the ABI switches used to
10291 produce the resultant binary. */
10292
10293 /* Record the ABI itself. Modern versions of binutils encode
10294 this information in the ELF header flags, but GDB needs the
10295 information in order to correctly debug binaries produced by
10296 older binutils. See the function mips_gdbarch_init in
10297 gdb/mips-tdep.c. */
10298 fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
10299 mips_mdebug_abi_name ());
10300
10301 /* There is no ELF header flag to distinguish long32 forms of the
10302 EABI from long64 forms. Emit a special section to help tools
10303 such as GDB. Do the same for o64, which is sometimes used with
10304 -mlong64. */
10305 if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
10306 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
10307 "\t.previous\n", TARGET_LONG64 ? 64 : 32);
10308
10309 /* Record the NaN encoding. */
10310 if (HAVE_AS_NAN || mips_nan != MIPS_IEEE_754_DEFAULT)
10311 fprintf (asm_out_file, "\t.nan\t%s\n",
10312 mips_nan == MIPS_IEEE_754_2008 ? "2008" : "legacy");
10313
10314 #ifdef HAVE_AS_DOT_MODULE
10315 /* Record the FP ABI. See below for comments. */
10316 if (TARGET_NO_FLOAT)
10317 #ifdef HAVE_AS_GNU_ATTRIBUTE
10318 fputs ("\t.gnu_attribute 4, 0\n", asm_out_file);
10319 #else
10320 ;
10321 #endif
10322 else if (!TARGET_HARD_FLOAT_ABI)
10323 fputs ("\t.module\tsoftfloat\n", asm_out_file);
10324 else if (!TARGET_DOUBLE_FLOAT)
10325 fputs ("\t.module\tsinglefloat\n", asm_out_file);
10326 else if (TARGET_FLOATXX)
10327 fputs ("\t.module\tfp=xx\n", asm_out_file);
10328 else if (TARGET_FLOAT64)
10329 fputs ("\t.module\tfp=64\n", asm_out_file);
10330 else
10331 fputs ("\t.module\tfp=32\n", asm_out_file);
10332
10333 if (TARGET_ODD_SPREG)
10334 fputs ("\t.module\toddspreg\n", asm_out_file);
10335 else
10336 fputs ("\t.module\tnooddspreg\n", asm_out_file);
10337
10338 fprintf (asm_out_file, "\t.module\tarch=%s\n", mips_arch_info->name);
10339 /* FIXME: DSPR3 is not supported by GCC? gas does support it */
10340 if (TARGET_DSPR2)
10341 fputs ("\t.module\tdspr2\n", asm_out_file);
10342 else if (TARGET_DSP)
10343 fputs ("\t.module\tdsp\n", asm_out_file);
10344 if (TARGET_EVA)
10345 fputs ("\t.module\teva\n", asm_out_file);
10346 if (TARGET_MCU)
10347 fputs ("\t.module\tmcu\n", asm_out_file);
10348 if (TARGET_MDMX)
10349 fputs ("\t.module\tmdmx\n", asm_out_file);
10350 if (TARGET_MIPS3D)
10351 fputs ("\t.module\tmips3d\n", asm_out_file);
10352 if (TARGET_MT)
10353 fputs ("\t.module\tmt\n", asm_out_file);
10354 if (TARGET_SMARTMIPS)
10355 fputs ("\t.module\tsmartmips\n", asm_out_file);
10356 if (TARGET_VIRT)
10357 fputs ("\t.module\tvirt\n", asm_out_file);
10358 if (TARGET_MSA)
10359 fputs ("\t.module\tmsa\n", asm_out_file);
10360 if (TARGET_XPA)
10361 fputs ("\t.module\txpa\n", asm_out_file);
10362 if (TARGET_MIPS16E2)
10363 fputs ("\t.module\tmips16e2\n", asm_out_file);
10364 if (TARGET_CRC)
10365 fputs ("\t.module\tcrc\n", asm_out_file);
10366 if (TARGET_GINV)
10367 fputs ("\t.module\tginv\n", asm_out_file);
10368 if (TARGET_LOONGSON_MMI)
10369 fputs ("\t.module\tloongson-mmi\n", asm_out_file);
10370 /* FIXME: LOONGSON-CAM is not supported by GCC? gas does support it */
10371 if (TARGET_LOONGSON_EXT2)
10372 fputs ("\t.module\tloongson-ext2\n", asm_out_file);
10373 else if (TARGET_LOONGSON_EXT)
10374 fputs ("\t.module\tloongson-ext\n", asm_out_file);
10375
10376 #else
10377 #ifdef HAVE_AS_GNU_ATTRIBUTE
10378 {
10379 int attr;
10380
10381 /* No floating-point operations, -mno-float. */
10382 if (TARGET_NO_FLOAT)
10383 attr = 0;
10384 /* Soft-float code, -msoft-float. */
10385 else if (!TARGET_HARD_FLOAT_ABI)
10386 attr = 3;
10387 /* Single-float code, -msingle-float. */
10388 else if (!TARGET_DOUBLE_FLOAT)
10389 attr = 2;
10390 /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64.
10391 Reserved attr=4.
10392 This case used 12 callee-saved double-precision registers
10393 and is deprecated. */
10394 /* 64-bit or 32-bit FP registers on a 32-bit target, -mfpxx. */
10395 else if (TARGET_FLOATXX)
10396 attr = 5;
10397 /* 64-bit FP registers on a 32-bit target, -mfp64 -modd-spreg. */
10398 else if (mips_abi == ABI_32 && TARGET_FLOAT64 && TARGET_ODD_SPREG)
10399 attr = 6;
10400 /* 64-bit FP registers on a 32-bit target, -mfp64 -mno-odd-spreg. */
10401 else if (mips_abi == ABI_32 && TARGET_FLOAT64)
10402 attr = 7;
10403 /* Regular FP code, FP regs same size as GP regs, -mdouble-float. */
10404 else
10405 attr = 1;
10406
10407 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
10408
10409 /* 128-bit MSA. */
10410 if (ISA_HAS_MSA)
10411 fprintf (asm_out_file, "\t.gnu_attribute 8, 1\n");
10412 }
10413 #endif
10414 #endif
10415
10416 /* If TARGET_ABICALLS, tell GAS to generate -KPIC code. */
10417 if (TARGET_ABICALLS)
10418 {
10419 fprintf (asm_out_file, "\t.abicalls\n");
10420 if (TARGET_ABICALLS_PIC0)
10421 fprintf (asm_out_file, "\t.option\tpic0\n");
10422 }
10423
10424 if (flag_verbose_asm)
10425 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
10426 ASM_COMMENT_START,
10427 mips_small_data_threshold, mips_arch_info->name, mips_isa);
10428 }
10429
10430 /* Implement TARGET_ASM_CODE_END. */
10431
10432 static void
10433 mips_code_end (void)
10434 {
10435 mips_finish_stub (&mips16_rdhwr_stub);
10436 mips_finish_stub (&mips16_get_fcsr_stub);
10437 mips_finish_stub (&mips16_set_fcsr_stub);
10438 }
10439
10440 /* Make the last instruction frame-related and note that it performs
10441 the operation described by FRAME_PATTERN. */
10442
10443 static void
10444 mips_set_frame_expr (rtx frame_pattern)
10445 {
10446 rtx_insn *insn;
10447
10448 insn = get_last_insn ();
10449 RTX_FRAME_RELATED_P (insn) = 1;
10450 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10451 frame_pattern,
10452 REG_NOTES (insn));
10453 }
10454
10455 /* Return a frame-related rtx that stores REG at MEM.
10456 REG must be a single register. */
10457
10458 static rtx
10459 mips_frame_set (rtx mem, rtx reg)
10460 {
10461 rtx set;
10462
10463 set = gen_rtx_SET (mem, reg);
10464 RTX_FRAME_RELATED_P (set) = 1;
10465
10466 return set;
10467 }
10468
10469 /* Record that the epilogue has restored call-saved register REG. */
10470
10471 static void
10472 mips_add_cfa_restore (rtx reg)
10473 {
10474 mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
10475 mips_epilogue.cfa_restores);
10476 }
10477
10478 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
10479 mips16e_s2_s8_regs[X], it must also save the registers in indexes
10480 X + 1 onwards. Likewise mips16e_a0_a3_regs. */
10481 static const unsigned char mips16e_s2_s8_regs[] = {
10482 30, 23, 22, 21, 20, 19, 18
10483 };
10484 static const unsigned char mips16e_a0_a3_regs[] = {
10485 4, 5, 6, 7
10486 };
10487
10488 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
10489 ordered from the uppermost in memory to the lowest in memory. */
10490 static const unsigned char mips16e_save_restore_regs[] = {
10491 31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
10492 };
10493
10494 /* Return the index of the lowest X in the range [0, SIZE) for which
10495 bit REGS[X] is set in MASK. Return SIZE if there is no such X. */
10496
10497 static unsigned int
10498 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
10499 unsigned int size)
10500 {
10501 unsigned int i;
10502
10503 for (i = 0; i < size; i++)
10504 if (BITSET_P (mask, regs[i]))
10505 break;
10506
10507 return i;
10508 }
10509
10510 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
10511 is the number of set bits. If *MASK_PTR contains REGS[X] for some X
10512 in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
10513 is true for all indexes (X, SIZE). */
10514
10515 static void
10516 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
10517 unsigned int size, unsigned int *num_regs_ptr)
10518 {
10519 unsigned int i;
10520
10521 i = mips16e_find_first_register (*mask_ptr, regs, size);
10522 for (i++; i < size; i++)
10523 if (!BITSET_P (*mask_ptr, regs[i]))
10524 {
10525 *num_regs_ptr += 1;
10526 *mask_ptr |= 1 << regs[i];
10527 }
10528 }
10529
10530 /* Return a simplified form of X using the register values in REG_VALUES.
10531 REG_VALUES[R] is the last value assigned to hard register R, or null
10532 if R has not been modified.
10533
10534 This function is rather limited, but is good enough for our purposes. */
10535
10536 static rtx
10537 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
10538 {
10539 x = avoid_constant_pool_reference (x);
10540
10541 if (UNARY_P (x))
10542 {
10543 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
10544 return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
10545 x0, GET_MODE (XEXP (x, 0)));
10546 }
10547
10548 if (ARITHMETIC_P (x))
10549 {
10550 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
10551 rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
10552 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
10553 }
10554
10555 if (REG_P (x)
10556 && reg_values[REGNO (x)]
10557 && !rtx_unstable_p (reg_values[REGNO (x)]))
10558 return reg_values[REGNO (x)];
10559
10560 return x;
10561 }
10562
10563 /* Return true if (set DEST SRC) stores an argument register into its
10564 caller-allocated save slot, storing the number of that argument
10565 register in *REGNO_PTR if so. REG_VALUES is as for
10566 mips16e_collect_propagate_value. */
10567
10568 static bool
10569 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
10570 unsigned int *regno_ptr)
10571 {
10572 unsigned int argno, regno;
10573 HOST_WIDE_INT offset, required_offset;
10574 rtx addr, base;
10575
10576 /* Check that this is a word-mode store. */
10577 if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
10578 return false;
10579
10580 /* Check that the register being saved is an unmodified argument
10581 register. */
10582 regno = REGNO (src);
10583 if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
10584 return false;
10585 argno = regno - GP_ARG_FIRST;
10586
10587 /* Check whether the address is an appropriate stack-pointer or
10588 frame-pointer access. */
10589 addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
10590 mips_split_plus (addr, &base, &offset);
10591 required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
10592 if (base == hard_frame_pointer_rtx)
10593 required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
10594 else if (base != stack_pointer_rtx)
10595 return false;
10596 if (offset != required_offset)
10597 return false;
10598
10599 *regno_ptr = regno;
10600 return true;
10601 }
10602
10603 /* A subroutine of mips_expand_prologue, called only when generating
10604 MIPS16e SAVE instructions. Search the start of the function for any
10605 instructions that save argument registers into their caller-allocated
10606 save slots. Delete such instructions and return a value N such that
10607 saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
10608 instructions redundant. */
10609
10610 static unsigned int
10611 mips16e_collect_argument_saves (void)
10612 {
10613 rtx reg_values[FIRST_PSEUDO_REGISTER];
10614 rtx_insn *insn, *next;
10615 rtx set, dest, src;
10616 unsigned int nargs, regno;
10617
10618 push_topmost_sequence ();
10619 nargs = 0;
10620 memset (reg_values, 0, sizeof (reg_values));
10621 for (insn = get_insns (); insn; insn = next)
10622 {
10623 next = NEXT_INSN (insn);
10624 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
10625 continue;
10626
10627 if (!INSN_P (insn))
10628 break;
10629
10630 set = PATTERN (insn);
10631 if (GET_CODE (set) != SET)
10632 break;
10633
10634 dest = SET_DEST (set);
10635 src = SET_SRC (set);
10636 if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
10637 {
10638 if (!BITSET_P (cfun->machine->frame.mask, regno))
10639 {
10640 delete_insn (insn);
10641 nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
10642 }
10643 }
10644 else if (REG_P (dest) && GET_MODE (dest) == word_mode)
10645 reg_values[REGNO (dest)]
10646 = mips16e_collect_propagate_value (src, reg_values);
10647 else
10648 break;
10649 }
10650 pop_topmost_sequence ();
10651
10652 return nargs;
10653 }
10654
10655 /* Return a move between register REGNO and memory location SP + OFFSET.
10656 REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
10657 Make the move a load if RESTORE_P, otherwise make it a store. */
10658
10659 static rtx
10660 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
10661 HOST_WIDE_INT offset, unsigned int regno)
10662 {
10663 rtx reg, mem;
10664
10665 mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
10666 offset));
10667 reg = gen_rtx_REG (SImode, regno);
10668 if (restore_p)
10669 {
10670 mips_add_cfa_restore (reg);
10671 return gen_rtx_SET (reg, mem);
10672 }
10673 if (reg_parm_p)
10674 return gen_rtx_SET (mem, reg);
10675 return mips_frame_set (mem, reg);
10676 }
10677
10678 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
10679 The instruction must:
10680
10681 - Allocate or deallocate SIZE bytes in total; SIZE is known
10682 to be nonzero.
10683
10684 - Save or restore as many registers in *MASK_PTR as possible.
10685 The instruction saves the first registers at the top of the
10686 allocated area, with the other registers below it.
10687
10688 - Save NARGS argument registers above the allocated area.
10689
10690 (NARGS is always zero if RESTORE_P.)
10691
10692 The SAVE and RESTORE instructions cannot save and restore all general
10693 registers, so there may be some registers left over for the caller to
10694 handle. Destructively modify *MASK_PTR so that it contains the registers
10695 that still need to be saved or restored. The caller can save these
10696 registers in the memory immediately below *OFFSET_PTR, which is a
10697 byte offset from the bottom of the allocated stack area. */
10698
10699 static rtx
10700 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
10701 HOST_WIDE_INT *offset_ptr, unsigned int nargs,
10702 HOST_WIDE_INT size)
10703 {
10704 rtx pattern, set;
10705 HOST_WIDE_INT offset, top_offset;
10706 unsigned int i, regno;
10707 int n;
10708
10709 gcc_assert (cfun->machine->frame.num_fp == 0);
10710
10711 /* Calculate the number of elements in the PARALLEL. We need one element
10712 for the stack adjustment, one for each argument register save, and one
10713 for each additional register move. */
10714 n = 1 + nargs;
10715 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
10716 if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
10717 n++;
10718
10719 /* Create the final PARALLEL. */
10720 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
10721 n = 0;
10722
10723 /* Add the stack pointer adjustment. */
10724 set = gen_rtx_SET (stack_pointer_rtx,
10725 plus_constant (Pmode, stack_pointer_rtx,
10726 restore_p ? size : -size));
10727 RTX_FRAME_RELATED_P (set) = 1;
10728 XVECEXP (pattern, 0, n++) = set;
10729
10730 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
10731 top_offset = restore_p ? size : 0;
10732
10733 /* Save the arguments. */
10734 for (i = 0; i < nargs; i++)
10735 {
10736 offset = top_offset + i * UNITS_PER_WORD;
10737 set = mips16e_save_restore_reg (restore_p, true, offset,
10738 GP_ARG_FIRST + i);
10739 XVECEXP (pattern, 0, n++) = set;
10740 }
10741
10742 /* Then fill in the other register moves. */
10743 offset = top_offset;
10744 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
10745 {
10746 regno = mips16e_save_restore_regs[i];
10747 if (BITSET_P (*mask_ptr, regno))
10748 {
10749 offset -= UNITS_PER_WORD;
10750 set = mips16e_save_restore_reg (restore_p, false, offset, regno);
10751 XVECEXP (pattern, 0, n++) = set;
10752 *mask_ptr &= ~(1 << regno);
10753 }
10754 }
10755
10756 /* Tell the caller what offset it should use for the remaining registers. */
10757 *offset_ptr = size + (offset - top_offset);
10758
10759 gcc_assert (n == XVECLEN (pattern, 0));
10760
10761 return pattern;
10762 }
10763
10764 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
10765 pointer. Return true if PATTERN matches the kind of instruction
10766 generated by mips16e_build_save_restore. If INFO is nonnull,
10767 initialize it when returning true. */
10768
10769 bool
10770 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
10771 struct mips16e_save_restore_info *info)
10772 {
10773 unsigned int i, nargs, mask, extra;
10774 HOST_WIDE_INT top_offset, save_offset, offset;
10775 rtx set, reg, mem, base;
10776 int n;
10777
10778 if (!GENERATE_MIPS16E_SAVE_RESTORE)
10779 return false;
10780
10781 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
10782 top_offset = adjust > 0 ? adjust : 0;
10783
10784 /* Interpret all other members of the PARALLEL. */
10785 save_offset = top_offset - UNITS_PER_WORD;
10786 mask = 0;
10787 nargs = 0;
10788 i = 0;
10789 for (n = 1; n < XVECLEN (pattern, 0); n++)
10790 {
10791 /* Check that we have a SET. */
10792 set = XVECEXP (pattern, 0, n);
10793 if (GET_CODE (set) != SET)
10794 return false;
10795
10796 /* Check that the SET is a load (if restoring) or a store
10797 (if saving). */
10798 mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
10799 if (!MEM_P (mem))
10800 return false;
10801
10802 /* Check that the address is the sum of the stack pointer and a
10803 possibly-zero constant offset. */
10804 mips_split_plus (XEXP (mem, 0), &base, &offset);
10805 if (base != stack_pointer_rtx)
10806 return false;
10807
10808 /* Check that SET's other operand is a register. */
10809 reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
10810 if (!REG_P (reg))
10811 return false;
10812
10813 /* Check for argument saves. */
10814 if (offset == top_offset + nargs * UNITS_PER_WORD
10815 && REGNO (reg) == GP_ARG_FIRST + nargs)
10816 nargs++;
10817 else if (offset == save_offset)
10818 {
10819 while (mips16e_save_restore_regs[i++] != REGNO (reg))
10820 if (i == ARRAY_SIZE (mips16e_save_restore_regs))
10821 return false;
10822
10823 mask |= 1 << REGNO (reg);
10824 save_offset -= UNITS_PER_WORD;
10825 }
10826 else
10827 return false;
10828 }
10829
10830 /* Check that the restrictions on register ranges are met. */
10831 extra = 0;
10832 mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
10833 ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
10834 mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
10835 ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
10836 if (extra != 0)
10837 return false;
10838
10839 /* Make sure that the topmost argument register is not saved twice.
10840 The checks above ensure that the same is then true for the other
10841 argument registers. */
10842 if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
10843 return false;
10844
10845 /* Pass back information, if requested. */
10846 if (info)
10847 {
10848 info->nargs = nargs;
10849 info->mask = mask;
10850 info->size = (adjust > 0 ? adjust : -adjust);
10851 }
10852
10853 return true;
10854 }
10855
10856 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
10857 for the register range [MIN_REG, MAX_REG]. Return a pointer to
10858 the null terminator. */
10859
10860 static char *
10861 mips16e_add_register_range (char *s, unsigned int min_reg,
10862 unsigned int max_reg)
10863 {
10864 if (min_reg != max_reg)
10865 s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
10866 else
10867 s += sprintf (s, ",%s", reg_names[min_reg]);
10868 return s;
10869 }
10870
10871 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
10872 PATTERN and ADJUST are as for mips16e_save_restore_pattern_p. */
10873
10874 const char *
10875 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
10876 {
10877 static char buffer[300];
10878
10879 struct mips16e_save_restore_info info;
10880 unsigned int i, end;
10881 char *s;
10882
10883 /* Parse the pattern. */
10884 if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
10885 gcc_unreachable ();
10886
10887 /* Add the mnemonic. */
10888 s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
10889 s += strlen (s);
10890
10891 /* Save the arguments. */
10892 if (info.nargs > 1)
10893 s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
10894 reg_names[GP_ARG_FIRST + info.nargs - 1]);
10895 else if (info.nargs == 1)
10896 s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
10897
10898 /* Emit the amount of stack space to allocate or deallocate. */
10899 s += sprintf (s, "%d", (int) info.size);
10900
10901 /* Save or restore $16. */
10902 if (BITSET_P (info.mask, 16))
10903 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
10904
10905 /* Save or restore $17. */
10906 if (BITSET_P (info.mask, 17))
10907 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
10908
10909 /* Save or restore registers in the range $s2...$s8, which
10910 mips16e_s2_s8_regs lists in decreasing order. Note that this
10911 is a software register range; the hardware registers are not
10912 numbered consecutively. */
10913 end = ARRAY_SIZE (mips16e_s2_s8_regs);
10914 i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
10915 if (i < end)
10916 s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
10917 mips16e_s2_s8_regs[i]);
10918
10919 /* Save or restore registers in the range $a0...$a3. */
10920 end = ARRAY_SIZE (mips16e_a0_a3_regs);
10921 i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
10922 if (i < end)
10923 s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
10924 mips16e_a0_a3_regs[end - 1]);
10925
10926 /* Save or restore $31. */
10927 if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
10928 s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
10929
10930 return buffer;
10931 }
10932
10933 /* Return true if the current function returns its value in a floating-point
10934 register in MIPS16 mode. */
10935
10936 static bool
10937 mips16_cfun_returns_in_fpr_p (void)
10938 {
10939 tree return_type = DECL_RESULT (current_function_decl);
10940 return (TARGET_MIPS16
10941 && TARGET_HARD_FLOAT_ABI
10942 && !aggregate_value_p (return_type, current_function_decl)
10943 && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
10944 }
10945
10946 /* Return true if predicate PRED is true for at least one instruction.
10947 Cache the result in *CACHE, and assume that the result is true
10948 if *CACHE is already true. */
10949
10950 static bool
10951 mips_find_gp_ref (bool *cache, bool (*pred) (rtx_insn *))
10952 {
10953 rtx_insn *insn, *subinsn;
10954
10955 if (!*cache)
10956 {
10957 push_topmost_sequence ();
10958 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
10959 FOR_EACH_SUBINSN (subinsn, insn)
10960 if (USEFUL_INSN_P (subinsn) && pred (subinsn))
10961 {
10962 *cache = true;
10963 break;
10964 }
10965 pop_topmost_sequence ();
10966 }
10967 return *cache;
10968 }
10969
10970 /* Return true if INSN refers to the global pointer in an "inflexible" way.
10971 See mips_cfun_has_inflexible_gp_ref_p for details. */
10972
10973 static bool
10974 mips_insn_has_inflexible_gp_ref_p (rtx_insn *insn)
10975 {
10976 /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
10977 indicate that the target could be a traditional MIPS
10978 lazily-binding stub. */
10979 return find_reg_fusage (insn, USE, pic_offset_table_rtx);
10980 }
10981
10982 /* Return true if the current function refers to the global pointer
10983 in a way that forces $28 to be valid. This means that we can't
10984 change the choice of global pointer, even for NewABI code.
10985
10986 One example of this (and one which needs several checks) is that
10987 $28 must be valid when calling traditional MIPS lazy-binding stubs.
10988 (This restriction does not apply to PLTs.) */
10989
10990 static bool
10991 mips_cfun_has_inflexible_gp_ref_p (void)
10992 {
10993 /* If the function has a nonlocal goto, $28 must hold the correct
10994 global pointer for the target function. That is, the target
10995 of the goto implicitly uses $28. */
10996 if (crtl->has_nonlocal_goto)
10997 return true;
10998
10999 if (TARGET_ABICALLS_PIC2)
11000 {
11001 /* Symbolic accesses implicitly use the global pointer unless
11002 -mexplicit-relocs is in effect. JAL macros to symbolic addresses
11003 might go to traditional MIPS lazy-binding stubs. */
11004 if (!TARGET_EXPLICIT_RELOCS)
11005 return true;
11006
11007 /* FUNCTION_PROFILER includes a JAL to _mcount, which again
11008 can be lazily-bound. */
11009 if (crtl->profile)
11010 return true;
11011
11012 /* MIPS16 functions that return in FPRs need to call an
11013 external libgcc routine. This call is only made explict
11014 during mips_expand_epilogue, and it too might be lazily bound. */
11015 if (mips16_cfun_returns_in_fpr_p ())
11016 return true;
11017 }
11018
11019 return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
11020 mips_insn_has_inflexible_gp_ref_p);
11021 }
11022
11023 /* Return true if INSN refers to the global pointer in a "flexible" way.
11024 See mips_cfun_has_flexible_gp_ref_p for details. */
11025
11026 static bool
11027 mips_insn_has_flexible_gp_ref_p (rtx_insn *insn)
11028 {
11029 return (get_attr_got (insn) != GOT_UNSET
11030 || mips_small_data_pattern_p (PATTERN (insn))
11031 || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
11032 }
11033
11034 /* Return true if the current function references the global pointer,
11035 but if those references do not inherently require the global pointer
11036 to be $28. Assume !mips_cfun_has_inflexible_gp_ref_p (). */
11037
11038 static bool
11039 mips_cfun_has_flexible_gp_ref_p (void)
11040 {
11041 /* Reload can sometimes introduce constant pool references
11042 into a function that otherwise didn't need them. For example,
11043 suppose we have an instruction like:
11044
11045 (set (reg:DF R1) (float:DF (reg:SI R2)))
11046
11047 If R2 turns out to be a constant such as 1, the instruction may
11048 have a REG_EQUAL note saying that R1 == 1.0. Reload then has
11049 the option of using this constant if R2 doesn't get allocated
11050 to a register.
11051
11052 In cases like these, reload will have added the constant to the
11053 pool but no instruction will yet refer to it. */
11054 if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
11055 return true;
11056
11057 return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
11058 mips_insn_has_flexible_gp_ref_p);
11059 }
11060
11061 /* Return the register that should be used as the global pointer
11062 within this function. Return INVALID_REGNUM if the function
11063 doesn't need a global pointer. */
11064
11065 static unsigned int
11066 mips_global_pointer (void)
11067 {
11068 unsigned int regno;
11069
11070 /* $gp is always available unless we're using a GOT. */
11071 if (!TARGET_USE_GOT)
11072 return GLOBAL_POINTER_REGNUM;
11073
11074 /* If there are inflexible references to $gp, we must use the
11075 standard register. */
11076 if (mips_cfun_has_inflexible_gp_ref_p ())
11077 return GLOBAL_POINTER_REGNUM;
11078
11079 /* If there are no current references to $gp, then the only uses
11080 we can introduce later are those involved in long branches. */
11081 if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
11082 return INVALID_REGNUM;
11083
11084 /* If the global pointer is call-saved, try to use a call-clobbered
11085 alternative. */
11086 if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
11087 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
11088 if (!df_regs_ever_live_p (regno)
11089 && call_used_regs[regno]
11090 && !fixed_regs[regno]
11091 && regno != PIC_FUNCTION_ADDR_REGNUM)
11092 return regno;
11093
11094 return GLOBAL_POINTER_REGNUM;
11095 }
11096
11097 /* Return true if the current function's prologue must load the global
11098 pointer value into pic_offset_table_rtx and store the same value in
11099 the function's cprestore slot (if any).
11100
11101 One problem we have to deal with is that, when emitting GOT-based
11102 position independent code, long-branch sequences will need to load
11103 the address of the branch target from the GOT. We don't know until
11104 the very end of compilation whether (and where) the function needs
11105 long branches, so we must ensure that _any_ branch can access the
11106 global pointer in some form. However, we do not want to pessimize
11107 the usual case in which all branches are short.
11108
11109 We handle this as follows:
11110
11111 (1) During reload, we set cfun->machine->global_pointer to
11112 INVALID_REGNUM if we _know_ that the current function
11113 doesn't need a global pointer. This is only valid if
11114 long branches don't need the GOT.
11115
11116 Otherwise, we assume that we might need a global pointer
11117 and pick an appropriate register.
11118
11119 (2) If cfun->machine->global_pointer != INVALID_REGNUM,
11120 we ensure that the global pointer is available at every
11121 block boundary bar entry and exit. We do this in one of two ways:
11122
11123 - If the function has a cprestore slot, we ensure that this
11124 slot is valid at every branch. However, as explained in
11125 point (6) below, there is no guarantee that pic_offset_table_rtx
11126 itself is valid if new uses of the global pointer are introduced
11127 after the first post-epilogue split.
11128
11129 We guarantee that the cprestore slot is valid by loading it
11130 into a fake register, CPRESTORE_SLOT_REGNUM. We then make
11131 this register live at every block boundary bar function entry
11132 and exit. It is then invalid to move the load (and thus the
11133 preceding store) across a block boundary.
11134
11135 - If the function has no cprestore slot, we guarantee that
11136 pic_offset_table_rtx itself is valid at every branch.
11137
11138 See mips_eh_uses for the handling of the register liveness.
11139
11140 (3) During prologue and epilogue generation, we emit "ghost"
11141 placeholder instructions to manipulate the global pointer.
11142
11143 (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
11144 and cfun->machine->must_restore_gp_when_clobbered_p if we already know
11145 that the function needs a global pointer. (There is no need to set
11146 them earlier than this, and doing it as late as possible leads to
11147 fewer false positives.)
11148
11149 (5) If cfun->machine->must_initialize_gp_p is true during a
11150 split_insns pass, we split the ghost instructions into real
11151 instructions. These split instructions can then be optimized in
11152 the usual way. Otherwise, we keep the ghost instructions intact,
11153 and optimize for the case where they aren't needed. We still
11154 have the option of splitting them later, if we need to introduce
11155 new uses of the global pointer.
11156
11157 For example, the scheduler ignores a ghost instruction that
11158 stores $28 to the stack, but it handles the split form of
11159 the ghost instruction as an ordinary store.
11160
11161 (6) [OldABI only.] If cfun->machine->must_restore_gp_when_clobbered_p
11162 is true during the first post-epilogue split_insns pass, we split
11163 calls and restore_gp patterns into instructions that explicitly
11164 load pic_offset_table_rtx from the cprestore slot. Otherwise,
11165 we split these patterns into instructions that _don't_ load from
11166 the cprestore slot.
11167
11168 If cfun->machine->must_restore_gp_when_clobbered_p is true at the
11169 time of the split, then any instructions that exist at that time
11170 can make free use of pic_offset_table_rtx. However, if we want
11171 to introduce new uses of the global pointer after the split,
11172 we must explicitly load the value from the cprestore slot, since
11173 pic_offset_table_rtx itself might not be valid at a given point
11174 in the function.
11175
11176 The idea is that we want to be able to delete redundant
11177 loads from the cprestore slot in the usual case where no
11178 long branches are needed.
11179
11180 (7) If cfun->machine->must_initialize_gp_p is still false at the end
11181 of md_reorg, we decide whether the global pointer is needed for
11182 long branches. If so, we set cfun->machine->must_initialize_gp_p
11183 to true and split the ghost instructions into real instructions
11184 at that stage.
11185
11186 Note that the ghost instructions must have a zero length for three reasons:
11187
11188 - Giving the length of the underlying $gp sequence might cause
11189 us to use long branches in cases where they aren't really needed.
11190
11191 - They would perturb things like alignment calculations.
11192
11193 - More importantly, the hazard detection in md_reorg relies on
11194 empty instructions having a zero length.
11195
11196 If we find a long branch and split the ghost instructions at the
11197 end of md_reorg, the split could introduce more long branches.
11198 That isn't a problem though, because we still do the split before
11199 the final shorten_branches pass.
11200
11201 This is extremely ugly, but it seems like the best compromise between
11202 correctness and efficiency. */
11203
11204 bool
11205 mips_must_initialize_gp_p (void)
11206 {
11207 return cfun->machine->must_initialize_gp_p;
11208 }
11209
11210 /* Return true if REGNO is a register that is ordinarily call-clobbered
11211 but must nevertheless be preserved by an interrupt handler. */
11212
11213 static bool
11214 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
11215 {
11216 if ((ISA_HAS_HILO || TARGET_DSP)
11217 && MD_REG_P (regno))
11218 return true;
11219
11220 if (TARGET_DSP && DSP_ACC_REG_P (regno))
11221 return true;
11222
11223 if (GP_REG_P (regno)
11224 && cfun->machine->use_shadow_register_set == SHADOW_SET_NO)
11225 {
11226 /* $0 is hard-wired. */
11227 if (regno == GP_REG_FIRST)
11228 return false;
11229
11230 /* The interrupt handler can treat kernel registers as
11231 scratch registers. */
11232 if (KERNEL_REG_P (regno))
11233 return false;
11234
11235 /* The function will return the stack pointer to its original value
11236 anyway. */
11237 if (regno == STACK_POINTER_REGNUM)
11238 return false;
11239
11240 /* Otherwise, return true for registers that aren't ordinarily
11241 call-clobbered. */
11242 return call_used_regs[regno];
11243 }
11244
11245 return false;
11246 }
11247
11248 /* Return true if the current function should treat register REGNO
11249 as call-saved. */
11250
11251 static bool
11252 mips_cfun_call_saved_reg_p (unsigned int regno)
11253 {
11254 /* If the user makes an ordinarily-call-saved register global,
11255 that register is no longer call-saved. */
11256 if (global_regs[regno])
11257 return false;
11258
11259 /* Interrupt handlers need to save extra registers. */
11260 if (cfun->machine->interrupt_handler_p
11261 && mips_interrupt_extra_call_saved_reg_p (regno))
11262 return true;
11263
11264 /* call_insns preserve $28 unless they explicitly say otherwise,
11265 so call_used_regs[] treats $28 as call-saved. However,
11266 we want the ABI property rather than the default call_insn
11267 property here. */
11268 return (regno == GLOBAL_POINTER_REGNUM
11269 ? TARGET_CALL_SAVED_GP
11270 : !call_used_regs[regno]);
11271 }
11272
11273 /* Return true if the function body might clobber register REGNO.
11274 We know that REGNO is call-saved. */
11275
11276 static bool
11277 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
11278 {
11279 /* Some functions should be treated as clobbering all call-saved
11280 registers. */
11281 if (crtl->saves_all_registers)
11282 return true;
11283
11284 /* DF handles cases where a register is explicitly referenced in
11285 the rtl. Incoming values are passed in call-clobbered registers,
11286 so we can assume that any live call-saved register is set within
11287 the function. */
11288 if (df_regs_ever_live_p (regno))
11289 return true;
11290
11291 /* Check for registers that are clobbered by FUNCTION_PROFILER.
11292 These clobbers are not explicit in the rtl. */
11293 if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
11294 return true;
11295
11296 /* If we're using a call-saved global pointer, the function's
11297 prologue will need to set it up. */
11298 if (cfun->machine->global_pointer == regno)
11299 return true;
11300
11301 /* The function's prologue will need to set the frame pointer if
11302 frame_pointer_needed. */
11303 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
11304 return true;
11305
11306 /* If a MIPS16 function returns a value in FPRs, its epilogue
11307 will need to call an external libgcc routine. This yet-to-be
11308 generated call_insn will clobber $31. */
11309 if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
11310 return true;
11311
11312 /* If REGNO is ordinarily call-clobbered, we must assume that any
11313 called function could modify it. */
11314 if (cfun->machine->interrupt_handler_p
11315 && !crtl->is_leaf
11316 && mips_interrupt_extra_call_saved_reg_p (regno))
11317 return true;
11318
11319 return false;
11320 }
11321
11322 /* Return true if the current function must save register REGNO. */
11323
11324 static bool
11325 mips_save_reg_p (unsigned int regno)
11326 {
11327 if (mips_cfun_call_saved_reg_p (regno))
11328 {
11329 if (mips_cfun_might_clobber_call_saved_reg_p (regno))
11330 return true;
11331
11332 /* Save both registers in an FPR pair if either one is used. This is
11333 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
11334 register to be used without the even register. */
11335 if (FP_REG_P (regno)
11336 && MAX_FPRS_PER_FMT == 2
11337 && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
11338 return true;
11339 }
11340
11341 /* We need to save the incoming return address if __builtin_eh_return
11342 is being used to set a different return address. */
11343 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
11344 return true;
11345
11346 return false;
11347 }
11348
11349 /* Populate the current function's mips_frame_info structure.
11350
11351 MIPS stack frames look like:
11352
11353 +-------------------------------+
11354 | |
11355 | incoming stack arguments |
11356 | |
11357 +-------------------------------+
11358 | |
11359 | caller-allocated save area |
11360 A | for register arguments |
11361 | |
11362 +-------------------------------+ <-- incoming stack pointer
11363 | |
11364 | callee-allocated save area |
11365 B | for arguments that are |
11366 | split between registers and |
11367 | the stack |
11368 | |
11369 +-------------------------------+ <-- arg_pointer_rtx
11370 | |
11371 C | callee-allocated save area |
11372 | for register varargs |
11373 | |
11374 +-------------------------------+ <-- frame_pointer_rtx
11375 | | + cop0_sp_offset
11376 | COP0 reg save area | + UNITS_PER_WORD
11377 | |
11378 +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
11379 | | + UNITS_PER_WORD
11380 | accumulator save area |
11381 | |
11382 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
11383 | | + UNITS_PER_HWFPVALUE
11384 | FPR save area |
11385 | |
11386 +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
11387 | | + UNITS_PER_WORD
11388 | GPR save area |
11389 | |
11390 +-------------------------------+ <-- frame_pointer_rtx with
11391 | | \ -fstack-protector
11392 | local variables | | var_size
11393 | | /
11394 +-------------------------------+
11395 | | \
11396 | $gp save area | | cprestore_size
11397 | | /
11398 P +-------------------------------+ <-- hard_frame_pointer_rtx for
11399 | | \ MIPS16 code
11400 | outgoing stack arguments | |
11401 | | |
11402 +-------------------------------+ | args_size
11403 | | |
11404 | caller-allocated save area | |
11405 | for register arguments | |
11406 | | /
11407 +-------------------------------+ <-- stack_pointer_rtx
11408 frame_pointer_rtx without
11409 -fstack-protector
11410 hard_frame_pointer_rtx for
11411 non-MIPS16 code.
11412
11413 At least two of A, B and C will be empty.
11414
11415 Dynamic stack allocations such as alloca insert data at point P.
11416 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
11417 hard_frame_pointer_rtx unchanged. */
11418
11419 static void
11420 mips_compute_frame_info (void)
11421 {
11422 struct mips_frame_info *frame;
11423 HOST_WIDE_INT offset, size;
11424 unsigned int regno, i;
11425
11426 /* Skip re-computing the frame info after reload completed. */
11427 if (reload_completed)
11428 return;
11429
11430 /* Set this function's interrupt properties. */
11431 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
11432 {
11433 if (mips_isa_rev < 2)
11434 error ("the %<interrupt%> attribute requires a MIPS32r2 processor or greater");
11435 else if (TARGET_MIPS16)
11436 error ("interrupt handlers cannot be MIPS16 functions");
11437 else
11438 {
11439 cfun->machine->interrupt_handler_p = true;
11440 cfun->machine->int_mask =
11441 mips_interrupt_mask (TREE_TYPE (current_function_decl));
11442 cfun->machine->use_shadow_register_set =
11443 mips_use_shadow_register_set (TREE_TYPE (current_function_decl));
11444 cfun->machine->keep_interrupts_masked_p =
11445 mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
11446 cfun->machine->use_debug_exception_return_p =
11447 mips_use_debug_exception_return_p (TREE_TYPE
11448 (current_function_decl));
11449 }
11450 }
11451
11452 frame = &cfun->machine->frame;
11453 memset (frame, 0, sizeof (*frame));
11454 size = get_frame_size ();
11455
11456 /* The first two blocks contain the outgoing argument area and the $gp save
11457 slot. This area isn't needed in leaf functions. We can also skip it
11458 if we know that none of the called functions will use this space.
11459
11460 But if the target-independent frame size is nonzero, we have already
11461 committed to allocating these in TARGET_STARTING_FRAME_OFFSET for
11462 !FRAME_GROWS_DOWNWARD. */
11463
11464 if ((size == 0 || FRAME_GROWS_DOWNWARD)
11465 && (crtl->is_leaf || (cfun->machine->optimize_call_stack && !flag_pic)))
11466 {
11467 /* The MIPS 3.0 linker does not like functions that dynamically
11468 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
11469 looks like we are trying to create a second frame pointer to the
11470 function, so allocate some stack space to make it happy. */
11471 if (cfun->calls_alloca)
11472 frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
11473 else
11474 frame->args_size = 0;
11475 frame->cprestore_size = 0;
11476 }
11477 else
11478 {
11479 frame->args_size = crtl->outgoing_args_size;
11480 frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
11481 }
11482
11483 /* MIPS16 code offsets the frame pointer by the size of the outgoing
11484 arguments. This tends to increase the chances of using unextended
11485 instructions for local variables and incoming arguments. */
11486 if (TARGET_MIPS16)
11487 frame->hard_frame_pointer_offset = frame->args_size;
11488
11489 /* PR 69129 / 69012: Beware of a possible race condition. mips_global_pointer
11490 might call mips_cfun_has_inflexible_gp_ref_p which in turn can call
11491 mips_find_gp_ref which will iterate over the current insn sequence.
11492 If any of these insns use the cprestore_save_slot_operand or
11493 cprestore_load_slot_operand predicates in order to be recognised then
11494 they will call mips_cprestore_address_p which calls
11495 mips_get_cprestore_base_and_offset which expects the frame information
11496 to be filled in... In fact mips_get_cprestore_base_and_offset only
11497 needs the args_size and hard_frame_pointer_offset fields to be filled
11498 in, which is why the global_pointer field is initialised here and not
11499 earlier. */
11500 cfun->machine->global_pointer = mips_global_pointer ();
11501 if (cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
11502 df_set_regs_ever_live (GLOBAL_POINTER_REGNUM, false);
11503
11504 offset = frame->args_size + frame->cprestore_size;
11505
11506 /* Move above the local variables. */
11507 frame->var_size = MIPS_STACK_ALIGN (size);
11508 offset += frame->var_size;
11509
11510 /* Find out which GPRs we need to save. */
11511 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
11512 if (mips_save_reg_p (regno))
11513 {
11514 frame->num_gp++;
11515 frame->mask |= 1 << (regno - GP_REG_FIRST);
11516 }
11517
11518 /* If this function calls eh_return, we must also save and restore the
11519 EH data registers. */
11520 if (crtl->calls_eh_return)
11521 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
11522 {
11523 frame->num_gp++;
11524 frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
11525 }
11526
11527 /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
11528 $a3-$a0 and $s2-$s8. If we save one register in the range, we must
11529 save all later registers too. */
11530 if (GENERATE_MIPS16E_SAVE_RESTORE)
11531 {
11532 mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
11533 ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
11534 mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
11535 ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
11536 }
11537
11538 /* Move above the GPR save area. */
11539 if (frame->num_gp > 0)
11540 {
11541 offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
11542 frame->gp_sp_offset = offset - UNITS_PER_WORD;
11543 }
11544
11545 /* Find out which FPRs we need to save. This loop must iterate over
11546 the same space as its companion in mips_for_each_saved_gpr_and_fpr. */
11547 if (TARGET_HARD_FLOAT)
11548 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
11549 if (mips_save_reg_p (regno))
11550 {
11551 frame->num_fp += MAX_FPRS_PER_FMT;
11552 frame->fmask |= ~(~0U << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
11553 }
11554
11555 /* Move above the FPR save area. */
11556 if (frame->num_fp > 0)
11557 {
11558 offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
11559 frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
11560 }
11561
11562 /* Add in space for the interrupt context information. */
11563 if (cfun->machine->interrupt_handler_p)
11564 {
11565 /* Check HI/LO. */
11566 if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
11567 {
11568 frame->num_acc++;
11569 frame->acc_mask |= (1 << 0);
11570 }
11571
11572 /* Check accumulators 1, 2, 3. */
11573 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
11574 if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
11575 {
11576 frame->num_acc++;
11577 frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
11578 }
11579
11580 /* All interrupt context functions need space to preserve STATUS. */
11581 frame->num_cop0_regs++;
11582
11583 /* We need to save EPC regardless of whether interrupts remain masked
11584 as exceptions will corrupt EPC. */
11585 frame->num_cop0_regs++;
11586 }
11587
11588 /* Move above the accumulator save area. */
11589 if (frame->num_acc > 0)
11590 {
11591 /* Each accumulator needs 2 words. */
11592 offset += frame->num_acc * 2 * UNITS_PER_WORD;
11593 frame->acc_sp_offset = offset - UNITS_PER_WORD;
11594 }
11595
11596 /* Move above the COP0 register save area. */
11597 if (frame->num_cop0_regs > 0)
11598 {
11599 offset += frame->num_cop0_regs * UNITS_PER_WORD;
11600 frame->cop0_sp_offset = offset - UNITS_PER_WORD;
11601 }
11602
11603 /* Determine if we can save the callee-saved registers in the frame
11604 header. Restrict this to functions where there is no other reason
11605 to allocate stack space so that we can eliminate the instructions
11606 that modify the stack pointer. */
11607
11608 if (TARGET_OLDABI
11609 && optimize > 0
11610 && flag_frame_header_optimization
11611 && !MAIN_NAME_P (DECL_NAME (current_function_decl))
11612 && cfun->machine->varargs_size == 0
11613 && crtl->args.pretend_args_size == 0
11614 && frame->var_size == 0
11615 && frame->num_acc == 0
11616 && frame->num_cop0_regs == 0
11617 && frame->num_fp == 0
11618 && frame->num_gp > 0
11619 && frame->num_gp <= MAX_ARGS_IN_REGISTERS
11620 && !GENERATE_MIPS16E_SAVE_RESTORE
11621 && !cfun->machine->interrupt_handler_p
11622 && cfun->machine->does_not_use_frame_header
11623 && cfun->machine->optimize_call_stack
11624 && !cfun->machine->callers_may_not_allocate_frame
11625 && !mips_cfun_has_cprestore_slot_p ())
11626 {
11627 offset = 0;
11628 frame->gp_sp_offset = REG_PARM_STACK_SPACE(cfun) - UNITS_PER_WORD;
11629 cfun->machine->use_frame_header_for_callee_saved_regs = true;
11630 }
11631
11632 /* Move above the callee-allocated varargs save area. */
11633 offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
11634 frame->arg_pointer_offset = offset;
11635
11636 /* Move above the callee-allocated area for pretend stack arguments. */
11637 offset += crtl->args.pretend_args_size;
11638 frame->total_size = offset;
11639
11640 /* Work out the offsets of the save areas from the top of the frame. */
11641 if (frame->gp_sp_offset > 0)
11642 frame->gp_save_offset = frame->gp_sp_offset - offset;
11643 if (frame->fp_sp_offset > 0)
11644 frame->fp_save_offset = frame->fp_sp_offset - offset;
11645 if (frame->acc_sp_offset > 0)
11646 frame->acc_save_offset = frame->acc_sp_offset - offset;
11647 if (frame->num_cop0_regs > 0)
11648 frame->cop0_save_offset = frame->cop0_sp_offset - offset;
11649 }
11650
11651 /* Return the style of GP load sequence that is being used for the
11652 current function. */
11653
11654 enum mips_loadgp_style
11655 mips_current_loadgp_style (void)
11656 {
11657 if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
11658 return LOADGP_NONE;
11659
11660 if (TARGET_RTP_PIC)
11661 return LOADGP_RTP;
11662
11663 if (TARGET_ABSOLUTE_ABICALLS)
11664 return LOADGP_ABSOLUTE;
11665
11666 return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
11667 }
11668
11669 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
11670
11671 static bool
11672 mips_frame_pointer_required (void)
11673 {
11674 /* If the function contains dynamic stack allocations, we need to
11675 use the frame pointer to access the static parts of the frame. */
11676 if (cfun->calls_alloca)
11677 return true;
11678
11679 /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
11680 reload may be unable to compute the address of a local variable,
11681 since there is no way to add a large constant to the stack pointer
11682 without using a second temporary register. */
11683 if (TARGET_MIPS16)
11684 {
11685 mips_compute_frame_info ();
11686 if (!SMALL_OPERAND (cfun->machine->frame.total_size))
11687 return true;
11688 }
11689
11690 return false;
11691 }
11692
11693 /* Make sure that we're not trying to eliminate to the wrong hard frame
11694 pointer. */
11695
11696 static bool
11697 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
11698 {
11699 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
11700 }
11701
11702 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
11703 or argument pointer. TO is either the stack pointer or hard frame
11704 pointer. */
11705
11706 HOST_WIDE_INT
11707 mips_initial_elimination_offset (int from, int to)
11708 {
11709 HOST_WIDE_INT offset;
11710
11711 mips_compute_frame_info ();
11712
11713 /* Set OFFSET to the offset from the end-of-prologue stack pointer. */
11714 switch (from)
11715 {
11716 case FRAME_POINTER_REGNUM:
11717 if (FRAME_GROWS_DOWNWARD)
11718 offset = (cfun->machine->frame.args_size
11719 + cfun->machine->frame.cprestore_size
11720 + cfun->machine->frame.var_size);
11721 else
11722 offset = 0;
11723 break;
11724
11725 case ARG_POINTER_REGNUM:
11726 offset = cfun->machine->frame.arg_pointer_offset;
11727 break;
11728
11729 default:
11730 gcc_unreachable ();
11731 }
11732
11733 if (to == HARD_FRAME_POINTER_REGNUM)
11734 offset -= cfun->machine->frame.hard_frame_pointer_offset;
11735
11736 return offset;
11737 }
11738
11739 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY. */
11740
11741 static void
11742 mips_extra_live_on_entry (bitmap regs)
11743 {
11744 if (TARGET_USE_GOT)
11745 {
11746 /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
11747 the global pointer. */
11748 if (!TARGET_ABSOLUTE_ABICALLS)
11749 bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
11750
11751 /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
11752 the global pointer. */
11753 if (TARGET_MIPS16)
11754 bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
11755
11756 /* See the comment above load_call<mode> for details. */
11757 bitmap_set_bit (regs, GOT_VERSION_REGNUM);
11758 }
11759 }
11760
11761 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
11762 previous frame. */
11763
11764 rtx
11765 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
11766 {
11767 if (count != 0)
11768 return const0_rtx;
11769
11770 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
11771 }
11772
11773 /* Emit code to change the current function's return address to
11774 ADDRESS. SCRATCH is available as a scratch register, if needed.
11775 ADDRESS and SCRATCH are both word-mode GPRs. */
11776
11777 void
11778 mips_set_return_address (rtx address, rtx scratch)
11779 {
11780 rtx slot_address;
11781
11782 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
11783 slot_address = mips_add_offset (scratch, stack_pointer_rtx,
11784 cfun->machine->frame.gp_sp_offset);
11785 mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
11786 }
11787
11788 /* Return true if the current function has a cprestore slot. */
11789
11790 bool
11791 mips_cfun_has_cprestore_slot_p (void)
11792 {
11793 return (cfun->machine->global_pointer != INVALID_REGNUM
11794 && cfun->machine->frame.cprestore_size > 0);
11795 }
11796
11797 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
11798 cprestore slot. LOAD_P is true if the caller wants to load from
11799 the cprestore slot; it is false if the caller wants to store to
11800 the slot. */
11801
11802 static void
11803 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
11804 bool load_p)
11805 {
11806 const struct mips_frame_info *frame;
11807
11808 frame = &cfun->machine->frame;
11809 /* .cprestore always uses the stack pointer instead of the frame pointer.
11810 We have a free choice for direct stores for non-MIPS16 functions,
11811 and for MIPS16 functions whose cprestore slot is in range of the
11812 stack pointer. Using the stack pointer would sometimes give more
11813 (early) scheduling freedom, but using the frame pointer would
11814 sometimes give more (late) scheduling freedom. It's hard to
11815 predict which applies to a given function, so let's keep things
11816 simple.
11817
11818 Loads must always use the frame pointer in functions that call
11819 alloca, and there's little benefit to using the stack pointer
11820 otherwise. */
11821 if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
11822 {
11823 *base = hard_frame_pointer_rtx;
11824 *offset = frame->args_size - frame->hard_frame_pointer_offset;
11825 }
11826 else
11827 {
11828 *base = stack_pointer_rtx;
11829 *offset = frame->args_size;
11830 }
11831 }
11832
11833 /* Return true if X is the load or store address of the cprestore slot;
11834 LOAD_P says which. */
11835
11836 bool
11837 mips_cprestore_address_p (rtx x, bool load_p)
11838 {
11839 rtx given_base, required_base;
11840 HOST_WIDE_INT given_offset, required_offset;
11841
11842 mips_split_plus (x, &given_base, &given_offset);
11843 mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
11844 return given_base == required_base && given_offset == required_offset;
11845 }
11846
11847 /* Return a MEM rtx for the cprestore slot. LOAD_P is true if we are
11848 going to load from it, false if we are going to store to it.
11849 Use TEMP as a temporary register if need be. */
11850
11851 static rtx
11852 mips_cprestore_slot (rtx temp, bool load_p)
11853 {
11854 rtx base;
11855 HOST_WIDE_INT offset;
11856
11857 mips_get_cprestore_base_and_offset (&base, &offset, load_p);
11858 return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
11859 }
11860
11861 /* Emit instructions to save global pointer value GP into cprestore
11862 slot MEM. OFFSET is the offset that MEM applies to the base register.
11863
11864 MEM may not be a legitimate address. If it isn't, TEMP is a
11865 temporary register that can be used, otherwise it is a SCRATCH. */
11866
11867 void
11868 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
11869 {
11870 if (TARGET_CPRESTORE_DIRECTIVE)
11871 {
11872 gcc_assert (gp == pic_offset_table_rtx);
11873 emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
11874 }
11875 else
11876 mips_emit_move (mips_cprestore_slot (temp, false), gp);
11877 }
11878
11879 /* Restore $gp from its save slot, using TEMP as a temporary base register
11880 if need be. This function is for o32 and o64 abicalls only.
11881
11882 See mips_must_initialize_gp_p for details about how we manage the
11883 global pointer. */
11884
11885 void
11886 mips_restore_gp_from_cprestore_slot (rtx temp)
11887 {
11888 gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
11889
11890 if (!cfun->machine->must_restore_gp_when_clobbered_p)
11891 {
11892 emit_note (NOTE_INSN_DELETED);
11893 return;
11894 }
11895
11896 if (TARGET_MIPS16)
11897 {
11898 mips_emit_move (temp, mips_cprestore_slot (temp, true));
11899 mips_emit_move (pic_offset_table_rtx, temp);
11900 }
11901 else
11902 mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
11903 if (!TARGET_EXPLICIT_RELOCS)
11904 emit_insn (gen_blockage ());
11905 }
11906
11907 /* A function to save or store a register. The first argument is the
11908 register and the second is the stack slot. */
11909 typedef void (*mips_save_restore_fn) (rtx, rtx);
11910
11911 /* Use FN to save or restore register REGNO. MODE is the register's
11912 mode and OFFSET is the offset of its save slot from the current
11913 stack pointer. */
11914
11915 static void
11916 mips_save_restore_reg (machine_mode mode, int regno,
11917 HOST_WIDE_INT offset, mips_save_restore_fn fn)
11918 {
11919 rtx mem;
11920
11921 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
11922 offset));
11923 fn (gen_rtx_REG (mode, regno), mem);
11924 }
11925
11926 /* Call FN for each accumulator that is saved by the current function.
11927 SP_OFFSET is the offset of the current stack pointer from the start
11928 of the frame. */
11929
11930 static void
11931 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
11932 {
11933 HOST_WIDE_INT offset;
11934 int regno;
11935
11936 offset = cfun->machine->frame.acc_sp_offset - sp_offset;
11937 if (BITSET_P (cfun->machine->frame.acc_mask, 0))
11938 {
11939 mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
11940 offset -= UNITS_PER_WORD;
11941 mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
11942 offset -= UNITS_PER_WORD;
11943 }
11944
11945 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
11946 if (BITSET_P (cfun->machine->frame.acc_mask,
11947 ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
11948 {
11949 mips_save_restore_reg (word_mode, regno, offset, fn);
11950 offset -= UNITS_PER_WORD;
11951 }
11952 }
11953
11954 /* Save register REG to MEM. Make the instruction frame-related. */
11955
11956 static void
11957 mips_save_reg (rtx reg, rtx mem)
11958 {
11959 if (GET_MODE (reg) == DFmode
11960 && (!TARGET_FLOAT64
11961 || mips_abi == ABI_32))
11962 {
11963 rtx x1, x2;
11964
11965 mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
11966
11967 x1 = mips_frame_set (mips_subword (mem, false),
11968 mips_subword (reg, false));
11969 x2 = mips_frame_set (mips_subword (mem, true),
11970 mips_subword (reg, true));
11971 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
11972 }
11973 else
11974 mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
11975 }
11976
11977 /* Capture the register combinations that are allowed in a SWM or LWM
11978 instruction. The entries are ordered by number of registers set in
11979 the mask. We also ignore the single register encodings because a
11980 normal SW/LW is preferred. */
11981
11982 static const unsigned int umips_swm_mask[17] = {
11983 0xc0ff0000, 0x80ff0000, 0x40ff0000, 0x807f0000,
11984 0x00ff0000, 0x803f0000, 0x007f0000, 0x801f0000,
11985 0x003f0000, 0x800f0000, 0x001f0000, 0x80070000,
11986 0x000f0000, 0x80030000, 0x00070000, 0x80010000,
11987 0x00030000
11988 };
11989
11990 static const unsigned int umips_swm_encoding[17] = {
11991 25, 24, 9, 23, 8, 22, 7, 21, 6, 20, 5, 19, 4, 18, 3, 17, 2
11992 };
11993
11994 /* Try to use a microMIPS LWM or SWM instruction to save or restore
11995 as many GPRs in *MASK as possible. *OFFSET is the offset from the
11996 stack pointer of the topmost save slot.
11997
11998 Remove from *MASK all registers that were handled using LWM and SWM.
11999 Update *OFFSET so that it points to the first unused save slot. */
12000
12001 static bool
12002 umips_build_save_restore (mips_save_restore_fn fn,
12003 unsigned *mask, HOST_WIDE_INT *offset)
12004 {
12005 int nregs;
12006 unsigned int i, j;
12007 rtx pattern, set, reg, mem;
12008 HOST_WIDE_INT this_offset;
12009 rtx this_base;
12010
12011 /* Try matching $16 to $31 (s0 to ra). */
12012 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
12013 if ((*mask & 0xffff0000) == umips_swm_mask[i])
12014 break;
12015
12016 if (i == ARRAY_SIZE (umips_swm_mask))
12017 return false;
12018
12019 /* Get the offset of the lowest save slot. */
12020 nregs = (umips_swm_encoding[i] & 0xf) + (umips_swm_encoding[i] >> 4);
12021 this_offset = *offset - UNITS_PER_WORD * (nregs - 1);
12022
12023 /* LWM/SWM can only support offsets from -2048 to 2047. */
12024 if (!UMIPS_12BIT_OFFSET_P (this_offset))
12025 return false;
12026
12027 /* Create the final PARALLEL. */
12028 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
12029 this_base = stack_pointer_rtx;
12030
12031 /* For registers $16-$23 and $30. */
12032 for (j = 0; j < (umips_swm_encoding[i] & 0xf); j++)
12033 {
12034 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
12035 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
12036 unsigned int regno = (j != 8) ? 16 + j : 30;
12037 *mask &= ~(1 << regno);
12038 reg = gen_rtx_REG (SImode, regno);
12039 if (fn == mips_save_reg)
12040 set = mips_frame_set (mem, reg);
12041 else
12042 {
12043 set = gen_rtx_SET (reg, mem);
12044 mips_add_cfa_restore (reg);
12045 }
12046 XVECEXP (pattern, 0, j) = set;
12047 }
12048
12049 /* For register $31. */
12050 if (umips_swm_encoding[i] >> 4)
12051 {
12052 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
12053 *mask &= ~(1 << 31);
12054 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
12055 reg = gen_rtx_REG (SImode, 31);
12056 if (fn == mips_save_reg)
12057 set = mips_frame_set (mem, reg);
12058 else
12059 {
12060 set = gen_rtx_SET (reg, mem);
12061 mips_add_cfa_restore (reg);
12062 }
12063 XVECEXP (pattern, 0, j) = set;
12064 }
12065
12066 pattern = emit_insn (pattern);
12067 if (fn == mips_save_reg)
12068 RTX_FRAME_RELATED_P (pattern) = 1;
12069
12070 /* Adjust the last offset. */
12071 *offset -= UNITS_PER_WORD * nregs;
12072
12073 return true;
12074 }
12075
12076 /* Call FN for each register that is saved by the current function.
12077 SP_OFFSET is the offset of the current stack pointer from the start
12078 of the frame. */
12079
12080 static void
12081 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
12082 mips_save_restore_fn fn)
12083 {
12084 machine_mode fpr_mode;
12085 int regno;
12086 const struct mips_frame_info *frame = &cfun->machine->frame;
12087 HOST_WIDE_INT offset;
12088 unsigned int mask;
12089
12090 /* Save registers starting from high to low. The debuggers prefer at least
12091 the return register be stored at func+4, and also it allows us not to
12092 need a nop in the epilogue if at least one register is reloaded in
12093 addition to return address. */
12094 offset = frame->gp_sp_offset - sp_offset;
12095 mask = frame->mask;
12096
12097 if (TARGET_MICROMIPS)
12098 umips_build_save_restore (fn, &mask, &offset);
12099
12100 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
12101 if (BITSET_P (mask, regno - GP_REG_FIRST))
12102 {
12103 /* Record the ra offset for use by mips_function_profiler. */
12104 if (regno == RETURN_ADDR_REGNUM)
12105 cfun->machine->frame.ra_fp_offset = offset + sp_offset;
12106 mips_save_restore_reg (word_mode, regno, offset, fn);
12107 offset -= UNITS_PER_WORD;
12108 }
12109
12110 /* This loop must iterate over the same space as its companion in
12111 mips_compute_frame_info. */
12112 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
12113 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
12114 for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
12115 regno >= FP_REG_FIRST;
12116 regno -= MAX_FPRS_PER_FMT)
12117 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
12118 {
12119 if (!TARGET_FLOAT64 && TARGET_DOUBLE_FLOAT
12120 && (fixed_regs[regno] || fixed_regs[regno + 1]))
12121 {
12122 if (fixed_regs[regno])
12123 mips_save_restore_reg (SFmode, regno + 1, offset, fn);
12124 else
12125 mips_save_restore_reg (SFmode, regno, offset, fn);
12126 }
12127 else
12128 mips_save_restore_reg (fpr_mode, regno, offset, fn);
12129 offset -= GET_MODE_SIZE (fpr_mode);
12130 }
12131 }
12132
12133 /* Return true if a move between register REGNO and its save slot (MEM)
12134 can be done in a single move. LOAD_P is true if we are loading
12135 from the slot, false if we are storing to it. */
12136
12137 static bool
12138 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
12139 {
12140 /* There is a specific MIPS16 instruction for saving $31 to the stack. */
12141 if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
12142 return false;
12143
12144 return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
12145 GET_MODE (mem), mem, load_p) == NO_REGS;
12146 }
12147
12148 /* Emit a move from SRC to DEST, given that one of them is a register
12149 save slot and that the other is a register. TEMP is a temporary
12150 GPR of the same mode that is available if need be. */
12151
12152 void
12153 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
12154 {
12155 unsigned int regno;
12156 rtx mem;
12157
12158 if (REG_P (src))
12159 {
12160 regno = REGNO (src);
12161 mem = dest;
12162 }
12163 else
12164 {
12165 regno = REGNO (dest);
12166 mem = src;
12167 }
12168
12169 if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
12170 {
12171 /* We don't yet know whether we'll need this instruction or not.
12172 Postpone the decision by emitting a ghost move. This move
12173 is specifically not frame-related; only the split version is. */
12174 if (TARGET_64BIT)
12175 emit_insn (gen_move_gpdi (dest, src));
12176 else
12177 emit_insn (gen_move_gpsi (dest, src));
12178 return;
12179 }
12180
12181 if (regno == HI_REGNUM)
12182 {
12183 if (REG_P (dest))
12184 {
12185 mips_emit_move (temp, src);
12186 if (TARGET_64BIT)
12187 emit_insn (gen_mthidi_ti (gen_rtx_REG (TImode, MD_REG_FIRST),
12188 temp, gen_rtx_REG (DImode, LO_REGNUM)));
12189 else
12190 emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
12191 temp, gen_rtx_REG (SImode, LO_REGNUM)));
12192 }
12193 else
12194 {
12195 if (TARGET_64BIT)
12196 emit_insn (gen_mfhidi_ti (temp,
12197 gen_rtx_REG (TImode, MD_REG_FIRST)));
12198 else
12199 emit_insn (gen_mfhisi_di (temp,
12200 gen_rtx_REG (DImode, MD_REG_FIRST)));
12201 mips_emit_move (dest, temp);
12202 }
12203 }
12204 else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
12205 mips_emit_move (dest, src);
12206 else
12207 {
12208 gcc_assert (!reg_overlap_mentioned_p (dest, temp));
12209 mips_emit_move (temp, src);
12210 mips_emit_move (dest, temp);
12211 }
12212 if (MEM_P (dest))
12213 mips_set_frame_expr (mips_frame_set (dest, src));
12214 }
12215
12216 /* If we're generating n32 or n64 abicalls, and the current function
12217 does not use $28 as its global pointer, emit a cplocal directive.
12218 Use pic_offset_table_rtx as the argument to the directive. */
12219
12220 static void
12221 mips_output_cplocal (void)
12222 {
12223 if (!TARGET_EXPLICIT_RELOCS
12224 && mips_must_initialize_gp_p ()
12225 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
12226 output_asm_insn (".cplocal %+", 0);
12227 }
12228
12229 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE. */
12230
12231 static void
12232 mips_output_function_prologue (FILE *file)
12233 {
12234 const char *fnname;
12235
12236 /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
12237 floating-point arguments. */
12238 if (TARGET_MIPS16
12239 && TARGET_HARD_FLOAT_ABI
12240 && crtl->args.info.fp_code != 0)
12241 mips16_build_function_stub ();
12242
12243 /* Get the function name the same way that toplev.cc does before calling
12244 assemble_start_function. This is needed so that the name used here
12245 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
12246 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
12247 mips_start_function_definition (fnname, TARGET_MIPS16, current_function_decl);
12248
12249 /* Output MIPS-specific frame information. */
12250 if (!flag_inhibit_size_directive)
12251 {
12252 const struct mips_frame_info *frame;
12253
12254 frame = &cfun->machine->frame;
12255
12256 /* .frame FRAMEREG, FRAMESIZE, RETREG. */
12257 fprintf (file,
12258 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
12259 "# vars= " HOST_WIDE_INT_PRINT_DEC
12260 ", regs= %d/%d"
12261 ", args= " HOST_WIDE_INT_PRINT_DEC
12262 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
12263 reg_names[frame_pointer_needed
12264 ? HARD_FRAME_POINTER_REGNUM
12265 : STACK_POINTER_REGNUM],
12266 (frame_pointer_needed
12267 ? frame->total_size - frame->hard_frame_pointer_offset
12268 : frame->total_size),
12269 reg_names[RETURN_ADDR_REGNUM],
12270 frame->var_size,
12271 frame->num_gp, frame->num_fp,
12272 frame->args_size,
12273 frame->cprestore_size);
12274
12275 /* .mask MASK, OFFSET. */
12276 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
12277 frame->mask, frame->gp_save_offset);
12278
12279 /* .fmask MASK, OFFSET. */
12280 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
12281 frame->fmask, frame->fp_save_offset);
12282 }
12283
12284 /* Handle the initialization of $gp for SVR4 PIC, if applicable.
12285 Also emit the ".set noreorder; .set nomacro" sequence for functions
12286 that need it. */
12287 if (mips_must_initialize_gp_p ()
12288 && mips_current_loadgp_style () == LOADGP_OLDABI)
12289 {
12290 if (TARGET_MIPS16)
12291 {
12292 if (ISA_HAS_MIPS16E2)
12293 {
12294 /* This is a fixed-form sequence. The position of the
12295 first two instructions is important because of the
12296 way _gp_disp is defined. */
12297 output_asm_insn ("lui\t$2,%%hi(_gp_disp)", 0);
12298 output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
12299 output_asm_insn ("addu\t$2,$3", 0);
12300 }
12301 else
12302 {
12303 /* This is a fixed-form sequence. The position of the
12304 first two instructions is important because of the
12305 way _gp_disp is defined. */
12306 output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
12307 output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
12308 output_asm_insn ("sll\t$2,16", 0);
12309 output_asm_insn ("addu\t$2,$3", 0);
12310 }
12311 }
12312 else
12313 {
12314 /* .cpload must be in a .set noreorder but not a
12315 .set nomacro block. */
12316 mips_push_asm_switch (&mips_noreorder);
12317 output_asm_insn (".cpload\t%^", 0);
12318 if (!cfun->machine->all_noreorder_p)
12319 mips_pop_asm_switch (&mips_noreorder);
12320 else
12321 mips_push_asm_switch (&mips_nomacro);
12322 }
12323 }
12324 else if (cfun->machine->all_noreorder_p)
12325 {
12326 mips_push_asm_switch (&mips_noreorder);
12327 mips_push_asm_switch (&mips_nomacro);
12328 }
12329
12330 /* Tell the assembler which register we're using as the global
12331 pointer. This is needed for thunks, since they can use either
12332 explicit relocs or assembler macros. */
12333 mips_output_cplocal ();
12334 }
12335
12336 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE. */
12337
12338 static void
12339 mips_output_function_epilogue (FILE *)
12340 {
12341 const char *fnname;
12342
12343 /* Reinstate the normal $gp. */
12344 SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
12345 mips_output_cplocal ();
12346
12347 if (cfun->machine->all_noreorder_p)
12348 {
12349 mips_pop_asm_switch (&mips_nomacro);
12350 mips_pop_asm_switch (&mips_noreorder);
12351 }
12352
12353 /* Get the function name the same way that toplev.cc does before calling
12354 assemble_start_function. This is needed so that the name used here
12355 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
12356 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
12357 mips_end_function_definition (fnname);
12358 }
12359
12360 /* Emit an optimisation barrier for accesses to the current frame. */
12361
12362 static void
12363 mips_frame_barrier (void)
12364 {
12365 emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
12366 }
12367
12368
12369 /* The __gnu_local_gp symbol. */
12370
12371 static GTY(()) rtx mips_gnu_local_gp;
12372
12373 /* If we're generating n32 or n64 abicalls, emit instructions
12374 to set up the global pointer. */
12375
12376 static void
12377 mips_emit_loadgp (void)
12378 {
12379 rtx addr, offset, incoming_address, base, index, pic_reg;
12380
12381 pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
12382 switch (mips_current_loadgp_style ())
12383 {
12384 case LOADGP_ABSOLUTE:
12385 if (mips_gnu_local_gp == NULL)
12386 {
12387 mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
12388 SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
12389 }
12390 emit_insn (PMODE_INSN (gen_loadgp_absolute,
12391 (pic_reg, mips_gnu_local_gp)));
12392 break;
12393
12394 case LOADGP_OLDABI:
12395 /* Added by mips_output_function_prologue. */
12396 break;
12397
12398 case LOADGP_NEWABI:
12399 addr = XEXP (DECL_RTL (current_function_decl), 0);
12400 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
12401 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
12402 emit_insn (PMODE_INSN (gen_loadgp_newabi,
12403 (pic_reg, offset, incoming_address)));
12404 break;
12405
12406 case LOADGP_RTP:
12407 base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
12408 index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
12409 emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
12410 break;
12411
12412 default:
12413 return;
12414 }
12415
12416 if (TARGET_MIPS16)
12417 emit_insn (PMODE_INSN (gen_copygp_mips16,
12418 (pic_offset_table_rtx, pic_reg)));
12419
12420 /* Emit a blockage if there are implicit uses of the GP register.
12421 This includes profiled functions, because FUNCTION_PROFILE uses
12422 a jal macro. */
12423 if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
12424 emit_insn (gen_loadgp_blockage ());
12425 }
12426
12427 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
12428
12429 #if PROBE_INTERVAL > 32768
12430 #error Cannot use indexed addressing mode for stack probing
12431 #endif
12432
12433 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
12434 inclusive. These are offsets from the current stack pointer. */
12435
12436 static void
12437 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
12438 {
12439 if (TARGET_MIPS16)
12440 sorry ("%<-fstack-check=specific%> not implemented for MIPS16");
12441
12442 /* See if we have a constant small number of probes to generate. If so,
12443 that's the easy case. */
12444 if (first + size <= 32768)
12445 {
12446 HOST_WIDE_INT i;
12447
12448 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
12449 it exceeds SIZE. If only one probe is needed, this will not
12450 generate any code. Then probe at FIRST + SIZE. */
12451 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
12452 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
12453 -(first + i)));
12454
12455 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
12456 -(first + size)));
12457 }
12458
12459 /* Otherwise, do the same as above, but in a loop. Note that we must be
12460 extra careful with variables wrapping around because we might be at
12461 the very top (or the very bottom) of the address space and we have
12462 to be able to handle this case properly; in particular, we use an
12463 equality test for the loop condition. */
12464 else
12465 {
12466 HOST_WIDE_INT rounded_size;
12467 rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
12468 rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
12469
12470 /* Sanity check for the addressing mode we're going to use. */
12471 gcc_assert (first <= 32768);
12472
12473
12474 /* Step 1: round SIZE to the previous multiple of the interval. */
12475
12476 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
12477
12478
12479 /* Step 2: compute initial and final value of the loop counter. */
12480
12481 /* TEST_ADDR = SP + FIRST. */
12482 emit_insn (gen_rtx_SET (r3, plus_constant (Pmode, stack_pointer_rtx,
12483 -first)));
12484
12485 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
12486 if (rounded_size > 32768)
12487 {
12488 emit_move_insn (r12, GEN_INT (rounded_size));
12489 emit_insn (gen_rtx_SET (r12, gen_rtx_MINUS (Pmode, r3, r12)));
12490 }
12491 else
12492 emit_insn (gen_rtx_SET (r12, plus_constant (Pmode, r3,
12493 -rounded_size)));
12494
12495
12496 /* Step 3: the loop
12497
12498 do
12499 {
12500 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
12501 probe at TEST_ADDR
12502 }
12503 while (TEST_ADDR != LAST_ADDR)
12504
12505 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
12506 until it is equal to ROUNDED_SIZE. */
12507
12508 emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
12509
12510
12511 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
12512 that SIZE is equal to ROUNDED_SIZE. */
12513
12514 if (size != rounded_size)
12515 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
12516 }
12517
12518 /* Make sure nothing is scheduled before we are done. */
12519 emit_insn (gen_blockage ());
12520 }
12521
12522 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
12523 absolute addresses. */
12524
12525 const char *
12526 mips_output_probe_stack_range (rtx reg1, rtx reg2)
12527 {
12528 static int labelno = 0;
12529 char loop_lab[32], tmp[64];
12530 rtx xops[2];
12531
12532 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
12533
12534 /* Loop. */
12535 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
12536
12537 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
12538 xops[0] = reg1;
12539 xops[1] = GEN_INT (-PROBE_INTERVAL);
12540 if (TARGET_64BIT && TARGET_LONG64)
12541 output_asm_insn ("daddiu\t%0,%0,%1", xops);
12542 else
12543 output_asm_insn ("addiu\t%0,%0,%1", xops);
12544
12545 /* Probe at TEST_ADDR, test if TEST_ADDR == LAST_ADDR and branch. */
12546 xops[1] = reg2;
12547 strcpy (tmp, "%(%<bne\t%0,%1,");
12548 output_asm_insn (strcat (tmp, &loop_lab[1]), xops);
12549 if (TARGET_64BIT)
12550 output_asm_insn ("sd\t$0,0(%0)%)", xops);
12551 else
12552 output_asm_insn ("sw\t$0,0(%0)%)", xops);
12553
12554 return "";
12555 }
12556
12557 /* Return true if X contains a kernel register. */
12558
12559 static bool
12560 mips_refers_to_kernel_reg_p (const_rtx x)
12561 {
12562 subrtx_iterator::array_type array;
12563 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
12564 if (REG_P (*iter) && KERNEL_REG_P (REGNO (*iter)))
12565 return true;
12566 return false;
12567 }
12568
12569 /* Expand the "prologue" pattern. */
12570
12571 void
12572 mips_expand_prologue (void)
12573 {
12574 const struct mips_frame_info *frame;
12575 HOST_WIDE_INT size;
12576 unsigned int nargs;
12577
12578 if (cfun->machine->global_pointer != INVALID_REGNUM)
12579 {
12580 /* Check whether an insn uses pic_offset_table_rtx, either explicitly
12581 or implicitly. If so, we can commit to using a global pointer
12582 straight away, otherwise we need to defer the decision. */
12583 if (mips_cfun_has_inflexible_gp_ref_p ()
12584 || mips_cfun_has_flexible_gp_ref_p ())
12585 {
12586 cfun->machine->must_initialize_gp_p = true;
12587 cfun->machine->must_restore_gp_when_clobbered_p = true;
12588 }
12589
12590 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
12591 }
12592
12593 frame = &cfun->machine->frame;
12594 size = frame->total_size;
12595
12596 if (flag_stack_usage_info)
12597 current_function_static_stack_size = size;
12598
12599 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK
12600 || flag_stack_clash_protection)
12601 {
12602 if (crtl->is_leaf && !cfun->calls_alloca)
12603 {
12604 if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
12605 mips_emit_probe_stack_range (get_stack_check_protect (),
12606 size - get_stack_check_protect ());
12607 }
12608 else if (size > 0)
12609 mips_emit_probe_stack_range (get_stack_check_protect (), size);
12610 }
12611
12612 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
12613 bytes beforehand; this is enough to cover the register save area
12614 without going out of range. */
12615 if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
12616 || frame->num_cop0_regs > 0)
12617 {
12618 HOST_WIDE_INT step1;
12619
12620 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
12621 if (GENERATE_MIPS16E_SAVE_RESTORE)
12622 {
12623 HOST_WIDE_INT offset;
12624 unsigned int mask, regno;
12625
12626 /* Try to merge argument stores into the save instruction. */
12627 nargs = mips16e_collect_argument_saves ();
12628
12629 /* Build the save instruction. */
12630 mask = frame->mask;
12631 rtx insn = mips16e_build_save_restore (false, &mask, &offset,
12632 nargs, step1);
12633 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
12634 mips_frame_barrier ();
12635 size -= step1;
12636
12637 /* Check if we need to save other registers. */
12638 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
12639 if (BITSET_P (mask, regno - GP_REG_FIRST))
12640 {
12641 offset -= UNITS_PER_WORD;
12642 mips_save_restore_reg (word_mode, regno,
12643 offset, mips_save_reg);
12644 }
12645 }
12646 else
12647 {
12648 if (cfun->machine->interrupt_handler_p)
12649 {
12650 HOST_WIDE_INT offset;
12651 rtx mem;
12652
12653 /* If this interrupt is using a shadow register set, we need to
12654 get the stack pointer from the previous register set. */
12655 if (cfun->machine->use_shadow_register_set == SHADOW_SET_YES)
12656 emit_insn (PMODE_INSN (gen_mips_rdpgpr, (stack_pointer_rtx,
12657 stack_pointer_rtx)));
12658
12659 if (!cfun->machine->keep_interrupts_masked_p)
12660 {
12661 if (cfun->machine->int_mask == INT_MASK_EIC)
12662 /* Move from COP0 Cause to K0. */
12663 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
12664 gen_rtx_REG (SImode, COP0_CAUSE_REG_NUM)));
12665 }
12666 /* Move from COP0 EPC to K1. */
12667 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
12668 gen_rtx_REG (SImode,
12669 COP0_EPC_REG_NUM)));
12670
12671 /* Allocate the first part of the frame. */
12672 rtx insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
12673 GEN_INT (-step1));
12674 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
12675 mips_frame_barrier ();
12676 size -= step1;
12677
12678 /* Start at the uppermost location for saving. */
12679 offset = frame->cop0_sp_offset - size;
12680
12681 /* Push EPC into its stack slot. */
12682 mem = gen_frame_mem (word_mode,
12683 plus_constant (Pmode, stack_pointer_rtx,
12684 offset));
12685 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
12686 offset -= UNITS_PER_WORD;
12687
12688 /* Move from COP0 Status to K1. */
12689 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
12690 gen_rtx_REG (SImode,
12691 COP0_STATUS_REG_NUM)));
12692
12693 /* Right justify the RIPL in k0. */
12694 if (!cfun->machine->keep_interrupts_masked_p
12695 && cfun->machine->int_mask == INT_MASK_EIC)
12696 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
12697 gen_rtx_REG (SImode, K0_REG_NUM),
12698 GEN_INT (CAUSE_IPL)));
12699
12700 /* Push Status into its stack slot. */
12701 mem = gen_frame_mem (word_mode,
12702 plus_constant (Pmode, stack_pointer_rtx,
12703 offset));
12704 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
12705 offset -= UNITS_PER_WORD;
12706
12707 /* Insert the RIPL into our copy of SR (k1) as the new IPL. */
12708 if (!cfun->machine->keep_interrupts_masked_p
12709 && cfun->machine->int_mask == INT_MASK_EIC)
12710 {
12711 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12712 TARGET_MCU ? GEN_INT (7) : GEN_INT (6),
12713 GEN_INT (SR_IPL),
12714 gen_rtx_REG (SImode, K0_REG_NUM)));
12715 if (TARGET_MCU)
12716 {
12717 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
12718 gen_rtx_REG (SImode, K0_REG_NUM),
12719 GEN_INT (7)));
12720 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12721 GEN_INT (1),
12722 GEN_INT (SR_IPL+8),
12723 gen_rtx_REG (SImode, K0_REG_NUM)));
12724 }
12725 }
12726
12727 /* Clear all interrupt mask bits up to and including the
12728 handler's interrupt line. */
12729 if (!cfun->machine->keep_interrupts_masked_p
12730 && cfun->machine->int_mask != INT_MASK_EIC)
12731 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12732 GEN_INT (cfun->machine->int_mask + 1),
12733 GEN_INT (SR_IM0),
12734 gen_rtx_REG (SImode, GP_REG_FIRST)));
12735
12736 if (!cfun->machine->keep_interrupts_masked_p)
12737 /* Enable interrupts by clearing the KSU ERL and EXL bits.
12738 IE is already the correct value, so we don't have to do
12739 anything explicit. */
12740 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12741 GEN_INT (4),
12742 GEN_INT (SR_EXL),
12743 gen_rtx_REG (SImode, GP_REG_FIRST)));
12744 else
12745 /* Disable interrupts by clearing the KSU, ERL, EXL,
12746 and IE bits. */
12747 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12748 GEN_INT (5),
12749 GEN_INT (SR_IE),
12750 gen_rtx_REG (SImode, GP_REG_FIRST)));
12751
12752 if (TARGET_HARD_FLOAT)
12753 /* Disable COP1 for hard-float. This will lead to an exception
12754 if floating-point code is executed in an ISR. */
12755 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
12756 GEN_INT (1),
12757 GEN_INT (SR_COP1),
12758 gen_rtx_REG (SImode, GP_REG_FIRST)));
12759 }
12760 else
12761 {
12762 if (step1 != 0)
12763 {
12764 rtx insn = gen_add3_insn (stack_pointer_rtx,
12765 stack_pointer_rtx,
12766 GEN_INT (-step1));
12767 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
12768 mips_frame_barrier ();
12769 size -= step1;
12770 }
12771 }
12772 mips_for_each_saved_acc (size, mips_save_reg);
12773 mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
12774 }
12775 }
12776
12777 /* Allocate the rest of the frame. */
12778 if (size > 0)
12779 {
12780 if (SMALL_OPERAND (-size))
12781 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
12782 stack_pointer_rtx,
12783 GEN_INT (-size)))) = 1;
12784 else
12785 {
12786 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
12787 if (TARGET_MIPS16)
12788 {
12789 /* There are no instructions to add or subtract registers
12790 from the stack pointer, so use the frame pointer as a
12791 temporary. We should always be using a frame pointer
12792 in this case anyway. */
12793 gcc_assert (frame_pointer_needed);
12794 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
12795 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
12796 hard_frame_pointer_rtx,
12797 MIPS_PROLOGUE_TEMP (Pmode)));
12798 mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
12799 }
12800 else
12801 emit_insn (gen_sub3_insn (stack_pointer_rtx,
12802 stack_pointer_rtx,
12803 MIPS_PROLOGUE_TEMP (Pmode)));
12804
12805 /* Describe the combined effect of the previous instructions. */
12806 mips_set_frame_expr
12807 (gen_rtx_SET (stack_pointer_rtx,
12808 plus_constant (Pmode, stack_pointer_rtx, -size)));
12809 }
12810 mips_frame_barrier ();
12811 }
12812
12813 /* Set up the frame pointer, if we're using one. */
12814 if (frame_pointer_needed)
12815 {
12816 HOST_WIDE_INT offset;
12817
12818 offset = frame->hard_frame_pointer_offset;
12819 if (offset == 0)
12820 {
12821 rtx insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
12822 RTX_FRAME_RELATED_P (insn) = 1;
12823 }
12824 else if (SMALL_OPERAND (offset))
12825 {
12826 rtx insn = gen_add3_insn (hard_frame_pointer_rtx,
12827 stack_pointer_rtx, GEN_INT (offset));
12828 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
12829 }
12830 else
12831 {
12832 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
12833 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
12834 emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
12835 hard_frame_pointer_rtx,
12836 MIPS_PROLOGUE_TEMP (Pmode)));
12837 mips_set_frame_expr
12838 (gen_rtx_SET (hard_frame_pointer_rtx,
12839 plus_constant (Pmode, stack_pointer_rtx, offset)));
12840 }
12841 }
12842
12843 mips_emit_loadgp ();
12844
12845 /* Initialize the $gp save slot. */
12846 if (mips_cfun_has_cprestore_slot_p ())
12847 {
12848 rtx base, mem, gp, temp;
12849 HOST_WIDE_INT offset;
12850
12851 mips_get_cprestore_base_and_offset (&base, &offset, false);
12852 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
12853 gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
12854 temp = (SMALL_OPERAND (offset)
12855 ? gen_rtx_SCRATCH (Pmode)
12856 : MIPS_PROLOGUE_TEMP (Pmode));
12857 emit_insn (PMODE_INSN (gen_potential_cprestore,
12858 (mem, GEN_INT (offset), gp, temp)));
12859
12860 mips_get_cprestore_base_and_offset (&base, &offset, true);
12861 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
12862 emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
12863 }
12864
12865 /* We need to search back to the last use of K0 or K1. */
12866 if (cfun->machine->interrupt_handler_p)
12867 {
12868 rtx_insn *insn;
12869 for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
12870 if (INSN_P (insn)
12871 && mips_refers_to_kernel_reg_p (PATTERN (insn)))
12872 break;
12873 /* Emit a move from K1 to COP0 Status after insn. */
12874 gcc_assert (insn != NULL_RTX);
12875 emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
12876 gen_rtx_REG (SImode, K1_REG_NUM)),
12877 insn);
12878 }
12879
12880 /* If we are profiling, make sure no instructions are scheduled before
12881 the call to mcount. */
12882 if (crtl->profile)
12883 emit_insn (gen_blockage ());
12884 }
12885
12886 /* Attach all pending register saves to the previous instruction.
12887 Return that instruction. */
12888
12889 static rtx_insn *
12890 mips_epilogue_emit_cfa_restores (void)
12891 {
12892 rtx_insn *insn;
12893
12894 insn = get_last_insn ();
12895 if (mips_epilogue.cfa_restores)
12896 {
12897 gcc_assert (insn && !REG_NOTES (insn));
12898 RTX_FRAME_RELATED_P (insn) = 1;
12899 REG_NOTES (insn) = mips_epilogue.cfa_restores;
12900 mips_epilogue.cfa_restores = 0;
12901 }
12902 return insn;
12903 }
12904
12905 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
12906 now at REG + OFFSET. */
12907
12908 static void
12909 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
12910 {
12911 rtx_insn *insn;
12912
12913 insn = mips_epilogue_emit_cfa_restores ();
12914 if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
12915 {
12916 RTX_FRAME_RELATED_P (insn) = 1;
12917 REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
12918 plus_constant (Pmode, reg, offset),
12919 REG_NOTES (insn));
12920 mips_epilogue.cfa_reg = reg;
12921 mips_epilogue.cfa_offset = offset;
12922 }
12923 }
12924
12925 /* Emit instructions to restore register REG from slot MEM. Also update
12926 the cfa_restores list. */
12927
12928 static void
12929 mips_restore_reg (rtx reg, rtx mem)
12930 {
12931 /* There's no MIPS16 instruction to load $31 directly. Load into
12932 $7 instead and adjust the return insn appropriately. */
12933 if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
12934 reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
12935 else if (GET_MODE (reg) == DFmode
12936 && (!TARGET_FLOAT64
12937 || mips_abi == ABI_32))
12938 {
12939 mips_add_cfa_restore (mips_subword (reg, true));
12940 mips_add_cfa_restore (mips_subword (reg, false));
12941 }
12942 else
12943 mips_add_cfa_restore (reg);
12944
12945 mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
12946 if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
12947 /* The CFA is currently defined in terms of the register whose
12948 value we have just restored. Redefine the CFA in terms of
12949 the stack pointer. */
12950 mips_epilogue_set_cfa (stack_pointer_rtx,
12951 mips_epilogue.cfa_restore_sp_offset);
12952 }
12953
12954 /* Emit code to set the stack pointer to BASE + OFFSET, given that
12955 BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
12956 BASE, if not the stack pointer, is available as a temporary. */
12957
12958 static void
12959 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
12960 {
12961 if (base == stack_pointer_rtx && offset == const0_rtx)
12962 return;
12963
12964 mips_frame_barrier ();
12965 if (offset == const0_rtx)
12966 {
12967 emit_move_insn (stack_pointer_rtx, base);
12968 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
12969 }
12970 else if (TARGET_MIPS16 && base != stack_pointer_rtx)
12971 {
12972 emit_insn (gen_add3_insn (base, base, offset));
12973 mips_epilogue_set_cfa (base, new_frame_size);
12974 emit_move_insn (stack_pointer_rtx, base);
12975 }
12976 else
12977 {
12978 emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
12979 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
12980 }
12981 }
12982
12983 /* Emit any instructions needed before a return. */
12984
12985 void
12986 mips_expand_before_return (void)
12987 {
12988 /* When using a call-clobbered gp, we start out with unified call
12989 insns that include instructions to restore the gp. We then split
12990 these unified calls after reload. These split calls explicitly
12991 clobber gp, so there is no need to define
12992 PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
12993
12994 For consistency, we should also insert an explicit clobber of $28
12995 before return insns, so that the post-reload optimizers know that
12996 the register is not live on exit. */
12997 if (TARGET_CALL_CLOBBERED_GP)
12998 emit_clobber (pic_offset_table_rtx);
12999 }
13000
13001 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
13002 says which. */
13003
13004 void
13005 mips_expand_epilogue (bool sibcall_p)
13006 {
13007 const struct mips_frame_info *frame;
13008 HOST_WIDE_INT step1, step2;
13009 rtx base, adjust;
13010 rtx_insn *insn;
13011 bool use_jraddiusp_p = false;
13012
13013 if (!sibcall_p && mips_can_use_return_insn ())
13014 {
13015 emit_jump_insn (gen_return ());
13016 return;
13017 }
13018
13019 /* In MIPS16 mode, if the return value should go into a floating-point
13020 register, we need to call a helper routine to copy it over. */
13021 if (mips16_cfun_returns_in_fpr_p ())
13022 mips16_copy_fpr_return_value ();
13023
13024 /* Split the frame into two. STEP1 is the amount of stack we should
13025 deallocate before restoring the registers. STEP2 is the amount we
13026 should deallocate afterwards.
13027
13028 Start off by assuming that no registers need to be restored. */
13029 frame = &cfun->machine->frame;
13030 step1 = frame->total_size;
13031 step2 = 0;
13032
13033 /* Work out which register holds the frame address. */
13034 if (!frame_pointer_needed)
13035 base = stack_pointer_rtx;
13036 else
13037 {
13038 base = hard_frame_pointer_rtx;
13039 step1 -= frame->hard_frame_pointer_offset;
13040 }
13041 mips_epilogue.cfa_reg = base;
13042 mips_epilogue.cfa_offset = step1;
13043 mips_epilogue.cfa_restores = NULL_RTX;
13044
13045 /* If we need to restore registers, deallocate as much stack as
13046 possible in the second step without going out of range. */
13047 if ((frame->mask | frame->fmask | frame->acc_mask) != 0
13048 || frame->num_cop0_regs > 0)
13049 {
13050 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
13051 step1 -= step2;
13052 }
13053
13054 /* Get an rtx for STEP1 that we can add to BASE. */
13055 adjust = GEN_INT (step1);
13056 if (!SMALL_OPERAND (step1))
13057 {
13058 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
13059 adjust = MIPS_EPILOGUE_TEMP (Pmode);
13060 }
13061 mips_deallocate_stack (base, adjust, step2);
13062
13063 /* If we're using addressing macros, $gp is implicitly used by all
13064 SYMBOL_REFs. We must emit a blockage insn before restoring $gp
13065 from the stack. */
13066 if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
13067 emit_insn (gen_blockage ());
13068
13069 mips_epilogue.cfa_restore_sp_offset = step2;
13070 if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
13071 {
13072 unsigned int regno, mask;
13073 HOST_WIDE_INT offset;
13074 rtx restore;
13075
13076 /* Generate the restore instruction. */
13077 mask = frame->mask;
13078 restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
13079
13080 /* Restore any other registers manually. */
13081 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
13082 if (BITSET_P (mask, regno - GP_REG_FIRST))
13083 {
13084 offset -= UNITS_PER_WORD;
13085 mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
13086 }
13087
13088 /* Restore the remaining registers and deallocate the final bit
13089 of the frame. */
13090 mips_frame_barrier ();
13091 emit_insn (restore);
13092 mips_epilogue_set_cfa (stack_pointer_rtx, 0);
13093 }
13094 else
13095 {
13096 /* Restore the registers. */
13097 mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
13098 mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
13099 mips_restore_reg);
13100
13101 if (cfun->machine->interrupt_handler_p)
13102 {
13103 HOST_WIDE_INT offset;
13104 rtx mem;
13105
13106 offset = frame->cop0_sp_offset - (frame->total_size - step2);
13107
13108 /* Restore the original EPC. */
13109 mem = gen_frame_mem (word_mode,
13110 plus_constant (Pmode, stack_pointer_rtx,
13111 offset));
13112 mips_emit_move (gen_rtx_REG (word_mode, K1_REG_NUM), mem);
13113 offset -= UNITS_PER_WORD;
13114
13115 /* Move to COP0 EPC. */
13116 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
13117 gen_rtx_REG (SImode, K1_REG_NUM)));
13118
13119 /* Restore the original Status. */
13120 mem = gen_frame_mem (word_mode,
13121 plus_constant (Pmode, stack_pointer_rtx,
13122 offset));
13123 mips_emit_move (gen_rtx_REG (word_mode, K1_REG_NUM), mem);
13124 offset -= UNITS_PER_WORD;
13125
13126 /* If we don't use shadow register set, we need to update SP. */
13127 if (cfun->machine->use_shadow_register_set == SHADOW_SET_NO)
13128 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
13129 else
13130 /* The choice of position is somewhat arbitrary in this case. */
13131 mips_epilogue_emit_cfa_restores ();
13132
13133 /* Move to COP0 Status. */
13134 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
13135 gen_rtx_REG (SImode, K1_REG_NUM)));
13136 }
13137 else if (TARGET_MICROMIPS
13138 && !crtl->calls_eh_return
13139 && !sibcall_p
13140 && step2 > 0
13141 && mips_unsigned_immediate_p (step2, 5, 2))
13142 use_jraddiusp_p = true;
13143 else
13144 /* Deallocate the final bit of the frame. */
13145 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
13146 }
13147
13148 if (cfun->machine->use_frame_header_for_callee_saved_regs)
13149 mips_epilogue_emit_cfa_restores ();
13150 else if (!use_jraddiusp_p)
13151 gcc_assert (!mips_epilogue.cfa_restores);
13152
13153 /* Add in the __builtin_eh_return stack adjustment. We need to
13154 use a temporary in MIPS16 code. */
13155 if (crtl->calls_eh_return)
13156 {
13157 if (TARGET_MIPS16)
13158 {
13159 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
13160 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
13161 MIPS_EPILOGUE_TEMP (Pmode),
13162 EH_RETURN_STACKADJ_RTX));
13163 mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
13164 }
13165 else
13166 emit_insn (gen_add3_insn (stack_pointer_rtx,
13167 stack_pointer_rtx,
13168 EH_RETURN_STACKADJ_RTX));
13169 }
13170
13171 if (!sibcall_p)
13172 {
13173 mips_expand_before_return ();
13174 if (cfun->machine->interrupt_handler_p)
13175 {
13176 /* Interrupt handlers generate eret or deret. */
13177 if (cfun->machine->use_debug_exception_return_p)
13178 emit_jump_insn (gen_mips_deret ());
13179 else
13180 emit_jump_insn (gen_mips_eret ());
13181 }
13182 else
13183 {
13184 rtx pat;
13185
13186 /* When generating MIPS16 code, the normal
13187 mips_for_each_saved_gpr_and_fpr path will restore the return
13188 address into $7 rather than $31. */
13189 if (TARGET_MIPS16
13190 && !GENERATE_MIPS16E_SAVE_RESTORE
13191 && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
13192 {
13193 /* simple_returns cannot rely on values that are only available
13194 on paths through the epilogue (because return paths that do
13195 not pass through the epilogue may nevertheless reuse a
13196 simple_return that occurs at the end of the epilogue).
13197 Use a normal return here instead. */
13198 rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
13199 pat = gen_return_internal (reg);
13200 }
13201 else if (use_jraddiusp_p)
13202 pat = gen_jraddiusp (GEN_INT (step2));
13203 else
13204 {
13205 rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
13206 pat = gen_simple_return_internal (reg);
13207 }
13208 emit_jump_insn (pat);
13209 if (use_jraddiusp_p)
13210 mips_epilogue_set_cfa (stack_pointer_rtx, step2);
13211 }
13212 }
13213
13214 /* Search from the beginning to the first use of K0 or K1. */
13215 if (cfun->machine->interrupt_handler_p
13216 && !cfun->machine->keep_interrupts_masked_p)
13217 {
13218 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
13219 if (INSN_P (insn)
13220 && mips_refers_to_kernel_reg_p (PATTERN (insn)))
13221 break;
13222 gcc_assert (insn != NULL_RTX);
13223 /* Insert disable interrupts before the first use of K0 or K1. */
13224 emit_insn_before (gen_mips_di (), insn);
13225 emit_insn_before (gen_mips_ehb (), insn);
13226 }
13227 }
13228
13229 /* Return nonzero if this function is known to have a null epilogue.
13230 This allows the optimizer to omit jumps to jumps if no stack
13231 was created. */
13232
13233 bool
13234 mips_can_use_return_insn (void)
13235 {
13236 /* Interrupt handlers need to go through the epilogue. */
13237 if (cfun->machine->interrupt_handler_p)
13238 return false;
13239
13240 if (!reload_completed)
13241 return false;
13242
13243 if (crtl->profile)
13244 return false;
13245
13246 /* In MIPS16 mode, a function that returns a floating-point value
13247 needs to arrange to copy the return value into the floating-point
13248 registers. */
13249 if (mips16_cfun_returns_in_fpr_p ())
13250 return false;
13251
13252 return (cfun->machine->frame.total_size == 0
13253 && !cfun->machine->use_frame_header_for_callee_saved_regs);
13254 }
13255
13256 /* Return true if register REGNO can store a value of mode MODE.
13257 The result of this function is cached in mips_hard_regno_mode_ok. */
13258
13259 static bool
13260 mips_hard_regno_mode_ok_uncached (unsigned int regno, machine_mode mode)
13261 {
13262 unsigned int size;
13263 enum mode_class mclass;
13264
13265 if (mode == CCV2mode)
13266 return (ISA_HAS_8CC
13267 && ST_REG_P (regno)
13268 && (regno - ST_REG_FIRST) % 2 == 0);
13269
13270 if (mode == CCV4mode)
13271 return (ISA_HAS_8CC
13272 && ST_REG_P (regno)
13273 && (regno - ST_REG_FIRST) % 4 == 0);
13274
13275 if (mode == CCmode || mode == CCEmode)
13276 return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
13277
13278 size = GET_MODE_SIZE (mode);
13279 mclass = GET_MODE_CLASS (mode);
13280
13281 if (GP_REG_P (regno) && mode != CCFmode && !MSA_SUPPORTED_MODE_P (mode))
13282 return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
13283
13284 /* For MSA, allow TImode and 128-bit vector modes in all FPR. */
13285 if (FP_REG_P (regno) && MSA_SUPPORTED_MODE_P (mode))
13286 return true;
13287
13288 if (FP_REG_P (regno)
13289 && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
13290 || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
13291 {
13292 /* Deny use of odd-numbered registers for 32-bit data for
13293 the o32 FP64A ABI. */
13294 if (TARGET_O32_FP64A_ABI && size <= 4 && (regno & 1) != 0)
13295 return false;
13296
13297 /* The FPXX ABI requires double-precision values to be placed in
13298 even-numbered registers. Disallow odd-numbered registers with
13299 CCFmode because CCFmode double-precision compares will write a
13300 64-bit value to a register. */
13301 if (mode == CCFmode)
13302 return !(TARGET_FLOATXX && (regno & 1) != 0);
13303
13304 /* Allow 64-bit vector modes for Loongson MultiMedia extensions
13305 Instructions (MMI). */
13306 if (TARGET_LOONGSON_MMI
13307 && (mode == V2SImode
13308 || mode == V4HImode
13309 || mode == V8QImode
13310 || mode == DImode))
13311 return true;
13312
13313 if (mclass == MODE_FLOAT
13314 || mclass == MODE_COMPLEX_FLOAT
13315 || mclass == MODE_VECTOR_FLOAT)
13316 return size <= UNITS_PER_FPVALUE;
13317
13318 /* Allow integer modes that fit into a single register. We need
13319 to put integers into FPRs when using instructions like CVT
13320 and TRUNC. There's no point allowing sizes smaller than a word,
13321 because the FPU has no appropriate load/store instructions. */
13322 if (mclass == MODE_INT)
13323 return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
13324 }
13325
13326 /* Don't allow vector modes in accumulators. */
13327 if (ACC_REG_P (regno)
13328 && !VECTOR_MODE_P (mode)
13329 && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
13330 {
13331 if (MD_REG_P (regno))
13332 {
13333 /* After a multiplication or division, clobbering HI makes
13334 the value of LO unpredictable, and vice versa. This means
13335 that, for all interesting cases, HI and LO are effectively
13336 a single register.
13337
13338 We model this by requiring that any value that uses HI
13339 also uses LO. */
13340 if (size <= UNITS_PER_WORD * 2)
13341 return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
13342 }
13343 else
13344 {
13345 /* DSP accumulators do not have the same restrictions as
13346 HI and LO, so we can treat them as normal doubleword
13347 registers. */
13348 if (size <= UNITS_PER_WORD)
13349 return true;
13350
13351 if (size <= UNITS_PER_WORD * 2
13352 && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
13353 return true;
13354 }
13355 }
13356
13357 if (ALL_COP_REG_P (regno))
13358 return mclass == MODE_INT && size <= UNITS_PER_WORD;
13359
13360 if (regno == GOT_VERSION_REGNUM)
13361 return mode == SImode;
13362
13363 return false;
13364 }
13365
13366 /* Implement TARGET_HARD_REGNO_MODE_OK. */
13367
13368 static bool
13369 mips_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
13370 {
13371 return mips_hard_regno_mode_ok_p[mode][regno];
13372 }
13373
13374 /* Return nonzero if register OLD_REG can be renamed to register NEW_REG. */
13375
13376 bool
13377 mips_hard_regno_rename_ok (unsigned int old_reg ATTRIBUTE_UNUSED,
13378 unsigned int new_reg)
13379 {
13380 /* Interrupt functions can only use registers that have already been
13381 saved by the prologue, even if they would normally be call-clobbered. */
13382 if (cfun->machine->interrupt_handler_p && !df_regs_ever_live_p (new_reg))
13383 return false;
13384
13385 return true;
13386 }
13387
13388 /* Return nonzero if register REGNO can be used as a scratch register
13389 in peephole2. */
13390
13391 bool
13392 mips_hard_regno_scratch_ok (unsigned int regno)
13393 {
13394 /* See mips_hard_regno_rename_ok. */
13395 if (cfun->machine->interrupt_handler_p && !df_regs_ever_live_p (regno))
13396 return false;
13397
13398 return true;
13399 }
13400
13401 /* Implement TARGET_HARD_REGNO_CALL_PART_CLOBBERED. Odd-numbered
13402 single-precision registers are not considered callee-saved for o32
13403 FPXX as they will be clobbered when run on an FR=1 FPU. MSA vector
13404 registers with MODE > 64 bits are part clobbered too. */
13405
13406 static bool
13407 mips_hard_regno_call_part_clobbered (unsigned int, unsigned int regno,
13408 machine_mode mode)
13409 {
13410 if (TARGET_FLOATXX
13411 && hard_regno_nregs (regno, mode) == 1
13412 && FP_REG_P (regno)
13413 && (regno & 1) != 0)
13414 return true;
13415
13416 if (ISA_HAS_MSA && FP_REG_P (regno) && GET_MODE_SIZE (mode) > 8)
13417 return true;
13418
13419 return false;
13420 }
13421
13422 /* Implement TARGET_HARD_REGNO_NREGS. */
13423
13424 static unsigned int
13425 mips_hard_regno_nregs (unsigned int regno, machine_mode mode)
13426 {
13427 if (ST_REG_P (regno))
13428 /* The size of FP status registers is always 4, because they only hold
13429 CCmode values, and CCmode is always considered to be 4 bytes wide. */
13430 return (GET_MODE_SIZE (mode) + 3) / 4;
13431
13432 if (FP_REG_P (regno))
13433 {
13434 if (MSA_SUPPORTED_MODE_P (mode))
13435 return 1;
13436
13437 return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
13438 }
13439
13440 /* All other registers are word-sized. */
13441 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
13442 }
13443
13444 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
13445 in mips_hard_regno_nregs. */
13446
13447 int
13448 mips_class_max_nregs (enum reg_class rclass, machine_mode mode)
13449 {
13450 int size;
13451 HARD_REG_SET left;
13452
13453 size = 0x8000;
13454 left = reg_class_contents[rclass];
13455 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
13456 {
13457 if (mips_hard_regno_mode_ok (ST_REG_FIRST, mode))
13458 size = MIN (size, 4);
13459
13460 left &= ~reg_class_contents[ST_REGS];
13461 }
13462 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
13463 {
13464 if (mips_hard_regno_mode_ok (FP_REG_FIRST, mode))
13465 {
13466 if (MSA_SUPPORTED_MODE_P (mode))
13467 size = MIN (size, UNITS_PER_MSA_REG);
13468 else
13469 size = MIN (size, UNITS_PER_FPREG);
13470 }
13471
13472 left &= ~reg_class_contents[FP_REGS];
13473 }
13474 if (!hard_reg_set_empty_p (left))
13475 size = MIN (size, UNITS_PER_WORD);
13476 return (GET_MODE_SIZE (mode) + size - 1) / size;
13477 }
13478
13479 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. */
13480
13481 static bool
13482 mips_can_change_mode_class (machine_mode from,
13483 machine_mode to, reg_class_t rclass)
13484 {
13485 /* Allow conversions between different Loongson integer vectors,
13486 and between those vectors and DImode. */
13487 if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
13488 && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
13489 return true;
13490
13491 /* Allow conversions between different MSA vector modes. */
13492 if (MSA_SUPPORTED_MODE_P (from) && MSA_SUPPORTED_MODE_P (to))
13493 return true;
13494
13495 /* Otherwise, there are several problems with changing the modes of
13496 values in floating-point registers:
13497
13498 - When a multi-word value is stored in paired floating-point
13499 registers, the first register always holds the low word. We
13500 therefore can't allow FPRs to change between single-word and
13501 multi-word modes on big-endian targets.
13502
13503 - GCC assumes that each word of a multiword register can be
13504 accessed individually using SUBREGs. This is not true for
13505 floating-point registers if they are bigger than a word.
13506
13507 - Loading a 32-bit value into a 64-bit floating-point register
13508 will not sign-extend the value, despite what LOAD_EXTEND_OP
13509 says. We can't allow FPRs to change from SImode to a wider
13510 mode on 64-bit targets.
13511
13512 - If the FPU has already interpreted a value in one format, we
13513 must not ask it to treat the value as having a different
13514 format.
13515
13516 We therefore disallow all mode changes involving FPRs. */
13517
13518 return !reg_classes_intersect_p (FP_REGS, rclass);
13519 }
13520
13521 /* Implement target hook small_register_classes_for_mode_p. */
13522
13523 static bool
13524 mips_small_register_classes_for_mode_p (machine_mode mode
13525 ATTRIBUTE_UNUSED)
13526 {
13527 return TARGET_MIPS16;
13528 }
13529
13530 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction,
13531 or use the MSA's move.v instruction. */
13532
13533 static bool
13534 mips_mode_ok_for_mov_fmt_p (machine_mode mode)
13535 {
13536 switch (mode)
13537 {
13538 case E_CCFmode:
13539 case E_SFmode:
13540 return TARGET_HARD_FLOAT;
13541
13542 case E_DFmode:
13543 return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
13544
13545 case E_V2SFmode:
13546 return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
13547
13548 default:
13549 return MSA_SUPPORTED_MODE_P (mode);
13550 }
13551 }
13552
13553 /* Implement TARGET_MODES_TIEABLE_P. */
13554
13555 static bool
13556 mips_modes_tieable_p (machine_mode mode1, machine_mode mode2)
13557 {
13558 /* FPRs allow no mode punning, so it's not worth tying modes if we'd
13559 prefer to put one of them in FPRs. */
13560 return (mode1 == mode2
13561 || (!mips_mode_ok_for_mov_fmt_p (mode1)
13562 && !mips_mode_ok_for_mov_fmt_p (mode2)));
13563 }
13564
13565 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
13566
13567 static reg_class_t
13568 mips_preferred_reload_class (rtx x, reg_class_t rclass)
13569 {
13570 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
13571 return LEA_REGS;
13572
13573 if (reg_class_subset_p (FP_REGS, rclass)
13574 && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
13575 return FP_REGS;
13576
13577 if (reg_class_subset_p (GR_REGS, rclass))
13578 rclass = GR_REGS;
13579
13580 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
13581 rclass = M16_REGS;
13582
13583 return rclass;
13584 }
13585
13586 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
13587 Return a "canonical" class to represent it in later calculations. */
13588
13589 static reg_class_t
13590 mips_canonicalize_move_class (reg_class_t rclass)
13591 {
13592 /* All moves involving accumulator registers have the same cost. */
13593 if (reg_class_subset_p (rclass, ACC_REGS))
13594 rclass = ACC_REGS;
13595
13596 /* Likewise promote subclasses of general registers to the most
13597 interesting containing class. */
13598 if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
13599 rclass = M16_REGS;
13600 else if (reg_class_subset_p (rclass, GENERAL_REGS))
13601 rclass = GENERAL_REGS;
13602
13603 return rclass;
13604 }
13605
13606 /* Return the cost of moving a value from a register of class FROM to a GPR.
13607 Return 0 for classes that are unions of other classes handled by this
13608 function. */
13609
13610 static int
13611 mips_move_to_gpr_cost (reg_class_t from)
13612 {
13613 switch (from)
13614 {
13615 case M16_REGS:
13616 case GENERAL_REGS:
13617 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
13618 return 2;
13619
13620 case ACC_REGS:
13621 /* MFLO and MFHI. */
13622 return 6;
13623
13624 case FP_REGS:
13625 /* MFC1, etc. */
13626 return 4;
13627
13628 case COP0_REGS:
13629 case COP2_REGS:
13630 case COP3_REGS:
13631 /* This choice of value is historical. */
13632 return 5;
13633
13634 default:
13635 return 0;
13636 }
13637 }
13638
13639 /* Return the cost of moving a value from a GPR to a register of class TO.
13640 Return 0 for classes that are unions of other classes handled by this
13641 function. */
13642
13643 static int
13644 mips_move_from_gpr_cost (reg_class_t to)
13645 {
13646 switch (to)
13647 {
13648 case M16_REGS:
13649 case GENERAL_REGS:
13650 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
13651 return 2;
13652
13653 case ACC_REGS:
13654 /* MTLO and MTHI. */
13655 return 6;
13656
13657 case FP_REGS:
13658 /* MTC1, etc. */
13659 return 4;
13660
13661 case COP0_REGS:
13662 case COP2_REGS:
13663 case COP3_REGS:
13664 /* This choice of value is historical. */
13665 return 5;
13666
13667 default:
13668 return 0;
13669 }
13670 }
13671
13672 /* Implement TARGET_REGISTER_MOVE_COST. Return 0 for classes that are the
13673 maximum of the move costs for subclasses; regclass will work out
13674 the maximum for us. */
13675
13676 static int
13677 mips_register_move_cost (machine_mode mode,
13678 reg_class_t from, reg_class_t to)
13679 {
13680 reg_class_t dregs;
13681 int cost1, cost2;
13682
13683 from = mips_canonicalize_move_class (from);
13684 to = mips_canonicalize_move_class (to);
13685
13686 /* Handle moves that can be done without using general-purpose registers. */
13687 if (from == FP_REGS)
13688 {
13689 if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
13690 /* MOV.FMT. */
13691 return 4;
13692 }
13693
13694 /* Handle cases in which only one class deviates from the ideal. */
13695 dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
13696 if (from == dregs)
13697 return mips_move_from_gpr_cost (to);
13698 if (to == dregs)
13699 return mips_move_to_gpr_cost (from);
13700
13701 /* Handles cases that require a GPR temporary. */
13702 cost1 = mips_move_to_gpr_cost (from);
13703 if (cost1 != 0)
13704 {
13705 cost2 = mips_move_from_gpr_cost (to);
13706 if (cost2 != 0)
13707 return cost1 + cost2;
13708 }
13709
13710 return 0;
13711 }
13712
13713 /* Implement TARGET_REGISTER_PRIORITY. */
13714
13715 static int
13716 mips_register_priority (int hard_regno)
13717 {
13718 /* Treat MIPS16 registers with higher priority than other regs. */
13719 if (TARGET_MIPS16
13720 && TEST_HARD_REG_BIT (reg_class_contents[M16_REGS], hard_regno))
13721 return 1;
13722 return 0;
13723 }
13724
13725 /* Implement TARGET_MEMORY_MOVE_COST. */
13726
13727 static int
13728 mips_memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
13729 {
13730 return (mips_cost->memory_latency
13731 + memory_move_secondary_cost (mode, rclass, in));
13732 }
13733
13734 /* Implement TARGET_SECONDARY_MEMORY_NEEDED.
13735
13736 When targeting the o32 FPXX ABI, all moves with a length of doubleword
13737 or greater must be performed by FR-mode-aware instructions.
13738 This can be achieved using MFHC1/MTHC1 when these instructions are
13739 available but otherwise moves must go via memory.
13740 For the o32 FP64A ABI, all odd-numbered moves with a length of
13741 doubleword or greater are required to use memory. Using MTC1/MFC1
13742 to access the lower-half of these registers would require a forbidden
13743 single-precision access. We require all double-word moves to use
13744 memory because adding even and odd floating-point registers classes
13745 would have a significant impact on the backend. */
13746
13747 static bool
13748 mips_secondary_memory_needed (machine_mode mode, reg_class_t class1,
13749 reg_class_t class2)
13750 {
13751 /* Ignore spilled pseudos. */
13752 if (lra_in_progress && (class1 == NO_REGS || class2 == NO_REGS))
13753 return false;
13754
13755 if (((class1 == FP_REGS) != (class2 == FP_REGS))
13756 && ((TARGET_FLOATXX && !ISA_HAS_MXHC1)
13757 || TARGET_O32_FP64A_ABI)
13758 && GET_MODE_SIZE (mode) >= 8)
13759 return true;
13760
13761 return false;
13762 }
13763
13764 /* Return the register class required for a secondary register when
13765 copying between one of the registers in RCLASS and value X, which
13766 has mode MODE. X is the source of the move if IN_P, otherwise it
13767 is the destination. Return NO_REGS if no secondary register is
13768 needed. */
13769
13770 enum reg_class
13771 mips_secondary_reload_class (enum reg_class rclass,
13772 machine_mode mode, rtx x, bool)
13773 {
13774 int regno;
13775
13776 /* If X is a constant that cannot be loaded into $25, it must be loaded
13777 into some other GPR. No other register class allows a direct move. */
13778 if (mips_dangerous_for_la25_p (x))
13779 return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
13780
13781 regno = true_regnum (x);
13782 if (TARGET_MIPS16)
13783 {
13784 /* In MIPS16 mode, every move must involve a member of M16_REGS. */
13785 if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
13786 return M16_REGS;
13787
13788 return NO_REGS;
13789 }
13790
13791 /* Copying from accumulator registers to anywhere other than a general
13792 register requires a temporary general register. */
13793 if (reg_class_subset_p (rclass, ACC_REGS))
13794 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
13795 if (ACC_REG_P (regno))
13796 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
13797
13798 if (reg_class_subset_p (rclass, FP_REGS))
13799 {
13800 if (regno < 0
13801 || (MEM_P (x)
13802 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)))
13803 /* In this case we can use lwc1, swc1, ldc1 or sdc1. We'll use
13804 pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported. */
13805 return NO_REGS;
13806
13807 if (MEM_P (x) && MSA_SUPPORTED_MODE_P (mode))
13808 /* In this case we can use MSA LD.* and ST.*. */
13809 return NO_REGS;
13810
13811 if (GP_REG_P (regno) || x == CONST0_RTX (mode))
13812 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
13813 return NO_REGS;
13814
13815 if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
13816 /* We can force the constant to memory and use lwc1
13817 and ldc1. As above, we will use pairs of lwc1s if
13818 ldc1 is not supported. */
13819 return NO_REGS;
13820
13821 if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
13822 /* In this case we can use mov.fmt. */
13823 return NO_REGS;
13824
13825 /* Otherwise, we need to reload through an integer register. */
13826 return GR_REGS;
13827 }
13828 if (FP_REG_P (regno))
13829 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
13830
13831 return NO_REGS;
13832 }
13833
13834 /* Implement TARGET_MODE_REP_EXTENDED. */
13835
13836 static int
13837 mips_mode_rep_extended (scalar_int_mode mode, scalar_int_mode mode_rep)
13838 {
13839 /* On 64-bit targets, SImode register values are sign-extended to DImode. */
13840 if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
13841 return SIGN_EXTEND;
13842
13843 return UNKNOWN;
13844 }
13845
13846 /* Implement TARGET_VALID_POINTER_MODE. */
13847
13848 static bool
13849 mips_valid_pointer_mode (scalar_int_mode mode)
13850 {
13851 return mode == SImode || (TARGET_64BIT && mode == DImode);
13852 }
13853
13854 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P. */
13855
13856 static bool
13857 mips_vector_mode_supported_p (machine_mode mode)
13858 {
13859 switch (mode)
13860 {
13861 case E_V2SFmode:
13862 return TARGET_PAIRED_SINGLE_FLOAT;
13863
13864 case E_V2HImode:
13865 case E_V4QImode:
13866 case E_V2HQmode:
13867 case E_V2UHQmode:
13868 case E_V2HAmode:
13869 case E_V2UHAmode:
13870 case E_V4QQmode:
13871 case E_V4UQQmode:
13872 return TARGET_DSP;
13873
13874 case E_V2SImode:
13875 case E_V4HImode:
13876 case E_V8QImode:
13877 return TARGET_LOONGSON_MMI;
13878
13879 default:
13880 return MSA_SUPPORTED_MODE_P (mode);
13881 }
13882 }
13883
13884 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
13885
13886 static bool
13887 mips_scalar_mode_supported_p (scalar_mode mode)
13888 {
13889 if (ALL_FIXED_POINT_MODE_P (mode)
13890 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
13891 return true;
13892
13893 return default_scalar_mode_supported_p (mode);
13894 }
13895
13896 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
13897
13898 static machine_mode
13899 mips_preferred_simd_mode (scalar_mode mode)
13900 {
13901 if (TARGET_PAIRED_SINGLE_FLOAT
13902 && mode == SFmode)
13903 return V2SFmode;
13904
13905 if (!ISA_HAS_MSA)
13906 return word_mode;
13907
13908 switch (mode)
13909 {
13910 case E_QImode:
13911 return V16QImode;
13912 case E_HImode:
13913 return V8HImode;
13914 case E_SImode:
13915 return V4SImode;
13916 case E_DImode:
13917 return V2DImode;
13918
13919 case E_SFmode:
13920 return V4SFmode;
13921
13922 case E_DFmode:
13923 return V2DFmode;
13924
13925 default:
13926 break;
13927 }
13928 return word_mode;
13929 }
13930
13931 /* Implement TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES. */
13932
13933 static unsigned int
13934 mips_autovectorize_vector_modes (vector_modes *modes, bool)
13935 {
13936 if (ISA_HAS_MSA)
13937 modes->safe_push (V16QImode);
13938 return 0;
13939 }
13940
13941
13942 static GTY (()) rtx speculation_barrier_libfunc;
13943
13944 /* Implement TARGET_INIT_LIBFUNCS. */
13945
13946 static void
13947 mips_init_libfuncs (void)
13948 {
13949 if (TARGET_FIX_VR4120)
13950 {
13951 /* Register the special divsi3 and modsi3 functions needed to work
13952 around VR4120 division errata. */
13953 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
13954 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
13955 }
13956
13957 if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
13958 {
13959 /* Register the MIPS16 -mhard-float stubs. */
13960 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
13961 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
13962 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
13963 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
13964
13965 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
13966 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
13967 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
13968 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
13969 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
13970 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
13971 set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
13972
13973 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
13974 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
13975 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
13976
13977 if (TARGET_DOUBLE_FLOAT)
13978 {
13979 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
13980 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
13981 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
13982 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
13983
13984 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
13985 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
13986 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
13987 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
13988 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
13989 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
13990 set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
13991
13992 set_conv_libfunc (sext_optab, DFmode, SFmode,
13993 "__mips16_extendsfdf2");
13994 set_conv_libfunc (trunc_optab, SFmode, DFmode,
13995 "__mips16_truncdfsf2");
13996 set_conv_libfunc (sfix_optab, SImode, DFmode,
13997 "__mips16_fix_truncdfsi");
13998 set_conv_libfunc (sfloat_optab, DFmode, SImode,
13999 "__mips16_floatsidf");
14000 set_conv_libfunc (ufloat_optab, DFmode, SImode,
14001 "__mips16_floatunsidf");
14002 }
14003 }
14004
14005 /* The MIPS16 ISA does not have an encoding for "sync", so we rely
14006 on an external non-MIPS16 routine to implement __sync_synchronize.
14007 Similarly for the rest of the ll/sc libfuncs. */
14008 if (TARGET_MIPS16)
14009 {
14010 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
14011 init_sync_libfuncs (UNITS_PER_WORD);
14012 }
14013 speculation_barrier_libfunc = init_one_libfunc ("__speculation_barrier");
14014 }
14015
14016 /* Build up a multi-insn sequence that loads label TARGET into $AT. */
14017
14018 static void
14019 mips_process_load_label (rtx target)
14020 {
14021 rtx base, gp, intop;
14022 HOST_WIDE_INT offset;
14023
14024 mips_multi_start ();
14025 switch (mips_abi)
14026 {
14027 case ABI_N32:
14028 mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
14029 mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
14030 break;
14031
14032 case ABI_64:
14033 mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
14034 mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
14035 break;
14036
14037 default:
14038 gp = pic_offset_table_rtx;
14039 if (mips_cfun_has_cprestore_slot_p ())
14040 {
14041 gp = gen_rtx_REG (Pmode, AT_REGNUM);
14042 mips_get_cprestore_base_and_offset (&base, &offset, true);
14043 if (!SMALL_OPERAND (offset))
14044 {
14045 intop = GEN_INT (CONST_HIGH_PART (offset));
14046 mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
14047 mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
14048
14049 base = gp;
14050 offset = CONST_LOW_PART (offset);
14051 }
14052 intop = GEN_INT (offset);
14053 if (ISA_HAS_LOAD_DELAY)
14054 mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
14055 else
14056 mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
14057 }
14058 if (ISA_HAS_LOAD_DELAY)
14059 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
14060 else
14061 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
14062 mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
14063 break;
14064 }
14065 }
14066
14067 /* Return the number of instructions needed to load a label into $AT. */
14068
14069 static unsigned int
14070 mips_load_label_num_insns (void)
14071 {
14072 if (cfun->machine->load_label_num_insns == 0)
14073 {
14074 mips_process_load_label (pc_rtx);
14075 cfun->machine->load_label_num_insns = mips_multi_num_insns;
14076 }
14077 return cfun->machine->load_label_num_insns;
14078 }
14079
14080 /* Emit an asm sequence to start a noat block and load the address
14081 of a label into $1. */
14082
14083 void
14084 mips_output_load_label (rtx target)
14085 {
14086 mips_push_asm_switch (&mips_noat);
14087 if (TARGET_EXPLICIT_RELOCS)
14088 {
14089 mips_process_load_label (target);
14090 mips_multi_write ();
14091 }
14092 else
14093 {
14094 if (Pmode == DImode)
14095 output_asm_insn ("dla\t%@,%0", &target);
14096 else
14097 output_asm_insn ("la\t%@,%0", &target);
14098 }
14099 }
14100
14101 /* Return the length of INSN. LENGTH is the initial length computed by
14102 attributes in the machine-description file. */
14103
14104 int
14105 mips_adjust_insn_length (rtx_insn *insn, int length)
14106 {
14107 /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
14108 of a PIC long-branch sequence. Substitute the correct value. */
14109 if (length == MAX_PIC_BRANCH_LENGTH
14110 && JUMP_P (insn)
14111 && INSN_CODE (insn) >= 0
14112 && get_attr_type (insn) == TYPE_BRANCH)
14113 {
14114 /* Add the branch-over instruction and its delay slot, if this
14115 is a conditional branch. */
14116 length = simplejump_p (insn) ? 0 : 8;
14117
14118 /* Add the size of a load into $AT. */
14119 length += BASE_INSN_LENGTH * mips_load_label_num_insns ();
14120
14121 /* Add the length of an indirect jump, ignoring the delay slot. */
14122 length += TARGET_COMPRESSION ? 2 : 4;
14123 }
14124
14125 /* A unconditional jump has an unfilled delay slot if it is not part
14126 of a sequence. A conditional jump normally has a delay slot, but
14127 does not on MIPS16. */
14128 if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
14129 length += TARGET_MIPS16 ? 2 : 4;
14130
14131 /* See how many nops might be needed to avoid hardware hazards. */
14132 if (!cfun->machine->ignore_hazard_length_p
14133 && INSN_P (insn)
14134 && INSN_CODE (insn) >= 0)
14135 switch (get_attr_hazard (insn))
14136 {
14137 case HAZARD_NONE:
14138 break;
14139
14140 case HAZARD_DELAY:
14141 case HAZARD_FORBIDDEN_SLOT:
14142 length += NOP_INSN_LENGTH;
14143 break;
14144
14145 case HAZARD_HILO:
14146 length += NOP_INSN_LENGTH * 2;
14147 break;
14148 }
14149
14150 return length;
14151 }
14152
14153 /* Return the asm template for a call. OPERANDS are the operands, TARGET_OPNO
14154 is the operand number of the target. SIZE_OPNO is the operand number of
14155 the argument size operand that can optionally hold the call attributes. If
14156 SIZE_OPNO is not -1 and the call is indirect, use the function symbol from
14157 the call attributes to attach a R_MIPS_JALR relocation to the call. LINK_P
14158 indicates whether the jump is a call and needs to set the link register.
14159
14160 When generating GOT code without explicit relocation operators, all calls
14161 should use assembly macros. Otherwise, all indirect calls should use "jr"
14162 or "jalr"; we will arrange to restore $gp afterwards if necessary. Finally,
14163 we can only generate direct calls for -mabicalls by temporarily switching
14164 to non-PIC mode.
14165
14166 For microMIPS jal(r), we try to generate jal(r)s when a 16-bit
14167 instruction is in the delay slot of jal(r).
14168
14169 Where compact branches are available, we try to use them if the delay slot
14170 has a NOP (or equivalently delay slots were not enabled for the instruction
14171 anyway). */
14172
14173 const char *
14174 mips_output_jump (rtx *operands, int target_opno, int size_opno, bool link_p)
14175 {
14176 static char buffer[300];
14177 char *s = buffer;
14178 bool reg_p = REG_P (operands[target_opno]);
14179
14180 const char *and_link = link_p ? "al" : "";
14181 const char *reg = reg_p ? "r" : "";
14182 const char *compact = "";
14183 const char *nop = "%/";
14184 const char *short_delay = link_p ? "%!" : "";
14185 const char *insn_name = TARGET_CB_NEVER || reg_p ? "j" : "b";
14186
14187 /* Compact branches can only be described when the ISA has support for them
14188 as both the compact formatter '%:' and the delay slot NOP formatter '%/'
14189 work as a mutually exclusive pair. I.e. a NOP is never required if a
14190 compact form is available. */
14191 if (!final_sequence
14192 && (TARGET_CB_MAYBE
14193 || (ISA_HAS_JRC && !link_p && reg_p)))
14194 {
14195 compact = "c";
14196 nop = "";
14197 }
14198
14199 if (TARGET_USE_GOT && !TARGET_EXPLICIT_RELOCS)
14200 sprintf (s, "%%*%s%s\t%%%d%%/", insn_name, and_link, target_opno);
14201 else
14202 {
14203 if (!reg_p && TARGET_ABICALLS_PIC2)
14204 s += sprintf (s, ".option\tpic0\n\t");
14205
14206 if (reg_p && mips_get_pic_call_symbol (operands, size_opno))
14207 s += sprintf (s, "%%*.reloc\t1f,%s,%%%d\n1:\t",
14208 TARGET_MICROMIPS ? "R_MICROMIPS_JALR" : "R_MIPS_JALR",
14209 size_opno);
14210 else
14211 s += sprintf (s, "%%*");
14212
14213 s += sprintf (s, "%s%s%s%s%s\t%%%d%s",
14214 insn_name, and_link, reg, compact, short_delay,
14215 target_opno, nop);
14216
14217 if (!reg_p && TARGET_ABICALLS_PIC2)
14218 s += sprintf (s, "\n\t.option\tpic2");
14219 }
14220 return buffer;
14221 }
14222
14223 /* Return the assembly code for INSN, which has the operands given by
14224 OPERANDS, and which branches to OPERANDS[0] if some condition is true.
14225 BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
14226 is in range of a direct branch. BRANCH_IF_FALSE is an inverted
14227 version of BRANCH_IF_TRUE. */
14228
14229 const char *
14230 mips_output_conditional_branch (rtx_insn *insn, rtx *operands,
14231 const char *branch_if_true,
14232 const char *branch_if_false)
14233 {
14234 unsigned int length;
14235 rtx taken;
14236
14237 gcc_assert (LABEL_P (operands[0]));
14238
14239 length = get_attr_length (insn);
14240 if (length <= 8)
14241 {
14242 /* Just a simple conditional branch. */
14243 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
14244 return branch_if_true;
14245 }
14246
14247 /* Generate a reversed branch around a direct jump. This fallback does
14248 not use branch-likely instructions. */
14249 mips_branch_likely = false;
14250 rtx_code_label *not_taken = gen_label_rtx ();
14251 taken = operands[0];
14252
14253 /* Generate the reversed branch to NOT_TAKEN. */
14254 operands[0] = not_taken;
14255 output_asm_insn (branch_if_false, operands);
14256
14257 /* If INSN has a delay slot, we must provide delay slots for both the
14258 branch to NOT_TAKEN and the conditional jump. We must also ensure
14259 that INSN's delay slot is executed in the appropriate cases. */
14260 if (final_sequence)
14261 {
14262 /* This first delay slot will always be executed, so use INSN's
14263 delay slot if is not annulled. */
14264 if (!INSN_ANNULLED_BRANCH_P (insn))
14265 {
14266 final_scan_insn (final_sequence->insn (1),
14267 asm_out_file, optimize, 1, NULL);
14268 final_sequence->insn (1)->set_deleted ();
14269 }
14270 else
14271 output_asm_insn ("nop", 0);
14272 fprintf (asm_out_file, "\n");
14273 }
14274
14275 /* Output the unconditional branch to TAKEN. */
14276 if (TARGET_ABSOLUTE_JUMPS && TARGET_CB_MAYBE)
14277 {
14278 /* Add a hazard nop. */
14279 if (!final_sequence)
14280 {
14281 output_asm_insn ("nop\t\t# hazard nop", 0);
14282 fprintf (asm_out_file, "\n");
14283 }
14284 output_asm_insn (MIPS_ABSOLUTE_JUMP ("bc\t%0"), &taken);
14285 }
14286 else if (TARGET_ABSOLUTE_JUMPS)
14287 output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
14288 else
14289 {
14290 mips_output_load_label (taken);
14291 if (TARGET_CB_MAYBE)
14292 output_asm_insn ("jrc\t%@%]", 0);
14293 else
14294 output_asm_insn ("jr\t%@%]%/", 0);
14295 }
14296
14297 /* Now deal with its delay slot; see above. */
14298 if (final_sequence)
14299 {
14300 /* This delay slot will only be executed if the branch is taken.
14301 Use INSN's delay slot if is annulled. */
14302 if (INSN_ANNULLED_BRANCH_P (insn))
14303 {
14304 final_scan_insn (final_sequence->insn (1),
14305 asm_out_file, optimize, 1, NULL);
14306 final_sequence->insn (1)->set_deleted ();
14307 }
14308 else if (TARGET_CB_NEVER)
14309 output_asm_insn ("nop", 0);
14310 fprintf (asm_out_file, "\n");
14311 }
14312
14313 /* Output NOT_TAKEN. */
14314 targetm.asm_out.internal_label (asm_out_file, "L",
14315 CODE_LABEL_NUMBER (not_taken));
14316 return "";
14317 }
14318
14319 /* Return the assembly code for INSN, which branches to OPERANDS[0]
14320 if some equality condition is true. The condition is given by
14321 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
14322 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
14323 OPERANDS[3] is the second operand and may be zero or a register. */
14324
14325 const char *
14326 mips_output_equal_conditional_branch (rtx_insn* insn, rtx *operands,
14327 bool inverted_p)
14328 {
14329 const char *branch[2];
14330 /* For a simple BNEZ or BEQZ microMIPSr3 branch. */
14331 if (TARGET_MICROMIPS
14332 && mips_isa_rev <= 5
14333 && operands[3] == const0_rtx
14334 && get_attr_length (insn) <= 8)
14335 {
14336 if (mips_cb == MIPS_CB_OPTIMAL)
14337 {
14338 branch[!inverted_p] = "%*b%C1z%:\t%2,%0";
14339 branch[inverted_p] = "%*b%N1z%:\t%2,%0";
14340 }
14341 else
14342 {
14343 branch[!inverted_p] = "%*b%C1z\t%2,%0%/";
14344 branch[inverted_p] = "%*b%N1z\t%2,%0%/";
14345 }
14346 }
14347 else if (TARGET_CB_MAYBE)
14348 {
14349 if (operands[3] == const0_rtx)
14350 {
14351 branch[!inverted_p] = MIPS_BRANCH_C ("b%C1z", "%2,%0");
14352 branch[inverted_p] = MIPS_BRANCH_C ("b%N1z", "%2,%0");
14353 }
14354 else if (REGNO (operands[2]) != REGNO (operands[3]))
14355 {
14356 branch[!inverted_p] = MIPS_BRANCH_C ("b%C1", "%2,%3,%0");
14357 branch[inverted_p] = MIPS_BRANCH_C ("b%N1", "%2,%3,%0");
14358 }
14359 else
14360 {
14361 /* This case is degenerate. It should not happen, but does. */
14362 if (GET_CODE (operands[1]) == NE)
14363 inverted_p = !inverted_p;
14364
14365 branch[!inverted_p] = MIPS_BRANCH_C ("b", "%0");
14366 branch[inverted_p] = "%*\t\t# branch never";
14367 }
14368 }
14369 else
14370 {
14371 branch[!inverted_p] = MIPS_BRANCH ("b%C1", "%2,%z3,%0");
14372 branch[inverted_p] = MIPS_BRANCH ("b%N1", "%2,%z3,%0");
14373 }
14374
14375 return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
14376 }
14377
14378 /* Return the assembly code for INSN, which branches to OPERANDS[0]
14379 if some ordering condition is true. The condition is given by
14380 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
14381 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
14382 OPERANDS[3] is the second operand and may be zero or a register. */
14383
14384 const char *
14385 mips_output_order_conditional_branch (rtx_insn *insn, rtx *operands,
14386 bool inverted_p)
14387 {
14388 const char *branch[2];
14389
14390 /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
14391 Make BRANCH[0] branch on the inverse condition. */
14392 if (operands[3] != const0_rtx)
14393 {
14394 /* Handle degenerate cases that should not, but do, occur. */
14395 if (REGNO (operands[2]) == REGNO (operands[3]))
14396 {
14397 switch (GET_CODE (operands[1]))
14398 {
14399 case LT:
14400 case LTU:
14401 inverted_p = !inverted_p;
14402 /* Fall through. */
14403 case GE:
14404 case GEU:
14405 branch[!inverted_p] = MIPS_BRANCH_C ("b", "%0");
14406 branch[inverted_p] = "%*\t\t# branch never";
14407 break;
14408 default:
14409 gcc_unreachable ();
14410 }
14411 }
14412 else
14413 {
14414 branch[!inverted_p] = MIPS_BRANCH_C ("b%C1", "%2,%3,%0");
14415 branch[inverted_p] = MIPS_BRANCH_C ("b%N1", "%2,%3,%0");
14416 }
14417 }
14418 else
14419 {
14420 switch (GET_CODE (operands[1]))
14421 {
14422 /* These cases are equivalent to comparisons against zero. */
14423 case LEU:
14424 inverted_p = !inverted_p;
14425 /* Fall through. */
14426 case GTU:
14427 if (TARGET_CB_MAYBE)
14428 {
14429 branch[!inverted_p] = MIPS_BRANCH_C ("bnez", "%2,%0");
14430 branch[inverted_p] = MIPS_BRANCH_C ("beqz", "%2,%0");
14431 }
14432 else
14433 {
14434 branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
14435 branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
14436 }
14437 break;
14438
14439 /* These cases are always true or always false. */
14440 case LTU:
14441 inverted_p = !inverted_p;
14442 /* Fall through. */
14443 case GEU:
14444 if (TARGET_CB_MAYBE)
14445 {
14446 branch[!inverted_p] = MIPS_BRANCH_C ("b", "%0");
14447 branch[inverted_p] = "%*\t\t# branch never";
14448 }
14449 else
14450 {
14451 branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
14452 branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
14453 }
14454 break;
14455
14456 default:
14457 if (TARGET_CB_MAYBE)
14458 {
14459 branch[!inverted_p] = MIPS_BRANCH_C ("b%C1z", "%2,%0");
14460 branch[inverted_p] = MIPS_BRANCH_C ("b%N1z", "%2,%0");
14461 }
14462 else
14463 {
14464 branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
14465 branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
14466 }
14467 break;
14468 }
14469 }
14470 return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
14471 }
14472
14473 /* Start a block of code that needs access to the LL, SC and SYNC
14474 instructions. */
14475
14476 static void
14477 mips_start_ll_sc_sync_block (void)
14478 {
14479 if (!ISA_HAS_LL_SC)
14480 {
14481 output_asm_insn (".set\tpush", 0);
14482 if (TARGET_64BIT)
14483 output_asm_insn (".set\tmips3", 0);
14484 else
14485 output_asm_insn (".set\tmips2", 0);
14486 }
14487 }
14488
14489 /* End a block started by mips_start_ll_sc_sync_block. */
14490
14491 static void
14492 mips_end_ll_sc_sync_block (void)
14493 {
14494 if (!ISA_HAS_LL_SC)
14495 output_asm_insn (".set\tpop", 0);
14496 }
14497
14498 /* Output and/or return the asm template for a sync instruction. */
14499
14500 const char *
14501 mips_output_sync (void)
14502 {
14503 mips_start_ll_sc_sync_block ();
14504 output_asm_insn ("sync", 0);
14505 mips_end_ll_sc_sync_block ();
14506 return "";
14507 }
14508
14509 /* Return the asm template associated with sync_insn1 value TYPE.
14510 IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation. */
14511
14512 static const char *
14513 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
14514 {
14515 switch (type)
14516 {
14517 case SYNC_INSN1_MOVE:
14518 return "move\t%0,%z2";
14519 case SYNC_INSN1_LI:
14520 return "li\t%0,%2";
14521 case SYNC_INSN1_ADDU:
14522 return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
14523 case SYNC_INSN1_ADDIU:
14524 return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
14525 case SYNC_INSN1_SUBU:
14526 return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
14527 case SYNC_INSN1_AND:
14528 return "and\t%0,%1,%z2";
14529 case SYNC_INSN1_ANDI:
14530 return "andi\t%0,%1,%2";
14531 case SYNC_INSN1_OR:
14532 return "or\t%0,%1,%z2";
14533 case SYNC_INSN1_ORI:
14534 return "ori\t%0,%1,%2";
14535 case SYNC_INSN1_XOR:
14536 return "xor\t%0,%1,%z2";
14537 case SYNC_INSN1_XORI:
14538 return "xori\t%0,%1,%2";
14539 }
14540 gcc_unreachable ();
14541 }
14542
14543 /* Return the asm template associated with sync_insn2 value TYPE. */
14544
14545 static const char *
14546 mips_sync_insn2_template (enum attr_sync_insn2 type)
14547 {
14548 switch (type)
14549 {
14550 case SYNC_INSN2_NOP:
14551 gcc_unreachable ();
14552 case SYNC_INSN2_AND:
14553 return "and\t%0,%1,%z2";
14554 case SYNC_INSN2_XOR:
14555 return "xor\t%0,%1,%z2";
14556 case SYNC_INSN2_NOT:
14557 return "nor\t%0,%1,%.";
14558 }
14559 gcc_unreachable ();
14560 }
14561
14562 /* OPERANDS are the operands to a sync loop instruction and INDEX is
14563 the value of the one of the sync_* attributes. Return the operand
14564 referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
14565 have the associated attribute. */
14566
14567 static rtx
14568 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
14569 {
14570 if (index > 0)
14571 default_value = operands[index - 1];
14572 return default_value;
14573 }
14574
14575 /* INSN is a sync loop with operands OPERANDS. Build up a multi-insn
14576 sequence for it. */
14577
14578 static void
14579 mips_process_sync_loop (rtx_insn *insn, rtx *operands)
14580 {
14581 rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
14582 rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
14583 unsigned int tmp3_insn;
14584 enum attr_sync_insn1 insn1;
14585 enum attr_sync_insn2 insn2;
14586 bool is_64bit_p;
14587 int memmodel_attr;
14588 enum memmodel model;
14589
14590 /* Read an operand from the sync_WHAT attribute and store it in
14591 variable WHAT. DEFAULT is the default value if no attribute
14592 is specified. */
14593 #define READ_OPERAND(WHAT, DEFAULT) \
14594 WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
14595 DEFAULT)
14596
14597 /* Read the memory. */
14598 READ_OPERAND (mem, 0);
14599 gcc_assert (mem);
14600 is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
14601
14602 /* Read the other attributes. */
14603 at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
14604 READ_OPERAND (oldval, at);
14605 READ_OPERAND (cmp, 0);
14606 READ_OPERAND (newval, at);
14607 READ_OPERAND (inclusive_mask, 0);
14608 READ_OPERAND (exclusive_mask, 0);
14609 READ_OPERAND (required_oldval, 0);
14610 READ_OPERAND (insn1_op2, 0);
14611 insn1 = get_attr_sync_insn1 (insn);
14612 insn2 = get_attr_sync_insn2 (insn);
14613
14614 /* Don't bother setting CMP result that is never used. */
14615 if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
14616 cmp = 0;
14617
14618 memmodel_attr = get_attr_sync_memmodel (insn);
14619 switch (memmodel_attr)
14620 {
14621 case 10:
14622 model = MEMMODEL_ACQ_REL;
14623 break;
14624 case 11:
14625 model = MEMMODEL_ACQUIRE;
14626 break;
14627 default:
14628 model = memmodel_from_int (INTVAL (operands[memmodel_attr]));
14629 }
14630
14631 mips_multi_start ();
14632
14633 /* Output the release side of the memory barrier. */
14634 if (need_atomic_barrier_p (model, true))
14635 {
14636 if (required_oldval == 0 && TARGET_OCTEON)
14637 {
14638 /* Octeon doesn't reorder reads, so a full barrier can be
14639 created by using SYNCW to order writes combined with the
14640 write from the following SC. When the SC successfully
14641 completes, we know that all preceding writes are also
14642 committed to the coherent memory system. It is possible
14643 for a single SYNCW to fail, but a pair of them will never
14644 fail, so we use two. */
14645 mips_multi_add_insn ("syncw", NULL);
14646 mips_multi_add_insn ("syncw", NULL);
14647 }
14648 else
14649 mips_multi_add_insn ("sync", NULL);
14650 }
14651
14652 /* Output the branch-back label. */
14653 mips_multi_add_label ("1:");
14654
14655 /* OLDVAL = *MEM. */
14656 mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
14657 oldval, mem, NULL);
14658
14659 /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2. */
14660 if (required_oldval)
14661 {
14662 if (inclusive_mask == 0)
14663 tmp1 = oldval;
14664 else
14665 {
14666 gcc_assert (oldval != at);
14667 mips_multi_add_insn ("and\t%0,%1,%2",
14668 at, oldval, inclusive_mask, NULL);
14669 tmp1 = at;
14670 }
14671 if (TARGET_CB_NEVER)
14672 mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
14673
14674 /* CMP = 0 [delay slot]. */
14675 if (cmp)
14676 mips_multi_add_insn ("li\t%0,0", cmp, NULL);
14677
14678 if (TARGET_CB_MAYBE && required_oldval == const0_rtx)
14679 mips_multi_add_insn ("bnezc\t%0,2f", tmp1, NULL);
14680 else if (TARGET_CB_MAYBE)
14681 mips_multi_add_insn ("bnec\t%0,%1,2f", tmp1, required_oldval, NULL);
14682
14683 }
14684
14685 /* $TMP1 = OLDVAL & EXCLUSIVE_MASK. */
14686 if (exclusive_mask == 0)
14687 tmp1 = const0_rtx;
14688 else
14689 {
14690 gcc_assert (oldval != at);
14691 mips_multi_add_insn ("and\t%0,%1,%z2",
14692 at, oldval, exclusive_mask, NULL);
14693 tmp1 = at;
14694 }
14695
14696 /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
14697
14698 We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
14699 at least one instruction in that case. */
14700 if (insn1 == SYNC_INSN1_MOVE
14701 && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
14702 tmp2 = insn1_op2;
14703 else
14704 {
14705 mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
14706 newval, oldval, insn1_op2, NULL);
14707 tmp2 = newval;
14708 }
14709
14710 /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK). */
14711 if (insn2 == SYNC_INSN2_NOP)
14712 tmp3 = tmp2;
14713 else
14714 {
14715 mips_multi_add_insn (mips_sync_insn2_template (insn2),
14716 newval, tmp2, inclusive_mask, NULL);
14717 tmp3 = newval;
14718 }
14719 tmp3_insn = mips_multi_last_index ();
14720
14721 /* $AT = $TMP1 | $TMP3. */
14722 if (tmp1 == const0_rtx || tmp3 == const0_rtx)
14723 {
14724 mips_multi_set_operand (tmp3_insn, 0, at);
14725 tmp3 = at;
14726 }
14727 else
14728 {
14729 gcc_assert (tmp1 != tmp3);
14730 mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
14731 }
14732
14733 /* if (!commit (*MEM = $AT)) goto 1.
14734
14735 This will sometimes be a delayed branch; see the write code below
14736 for details. */
14737 mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
14738
14739 /* When using branch likely (-mfix-r10000), the delay slot instruction
14740 will be annulled on false. The normal delay slot instructions
14741 calculate the overall result of the atomic operation and must not
14742 be annulled. To ensure this behavior unconditionally use a NOP
14743 in the delay slot for the branch likely case. */
14744
14745 if (TARGET_CB_MAYBE)
14746 mips_multi_add_insn ("beqzc\t%0,1b", at, NULL);
14747 else
14748 mips_multi_add_insn ("beq%?\t%0,%.,1b%~", at, NULL);
14749
14750 /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot]. */
14751 if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
14752 {
14753 mips_multi_copy_insn (tmp3_insn);
14754 mips_multi_set_operand (mips_multi_last_index (), 0, newval);
14755 }
14756 else if (!(required_oldval && cmp) && !mips_branch_likely)
14757 mips_multi_add_insn ("nop", NULL);
14758
14759 /* CMP = 1 -- either standalone or in a delay slot. */
14760 if (required_oldval && cmp)
14761 mips_multi_add_insn ("li\t%0,1", cmp, NULL);
14762
14763 /* Output the acquire side of the memory barrier. */
14764 if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
14765 mips_multi_add_insn ("sync", NULL);
14766
14767 /* Output the exit label, if needed. */
14768 if (required_oldval)
14769 mips_multi_add_label ("2:");
14770
14771 #undef READ_OPERAND
14772 }
14773
14774 /* Output and/or return the asm template for sync loop INSN, which has
14775 the operands given by OPERANDS. */
14776
14777 const char *
14778 mips_output_sync_loop (rtx_insn *insn, rtx *operands)
14779 {
14780 /* Use branch-likely instructions to work around the LL/SC R10000
14781 errata. */
14782 mips_branch_likely = TARGET_FIX_R10000;
14783
14784 mips_process_sync_loop (insn, operands);
14785
14786 mips_push_asm_switch (&mips_noreorder);
14787 mips_push_asm_switch (&mips_nomacro);
14788 mips_push_asm_switch (&mips_noat);
14789 mips_start_ll_sc_sync_block ();
14790
14791 mips_multi_write ();
14792
14793 mips_end_ll_sc_sync_block ();
14794 mips_pop_asm_switch (&mips_noat);
14795 mips_pop_asm_switch (&mips_nomacro);
14796 mips_pop_asm_switch (&mips_noreorder);
14797
14798 return "";
14799 }
14800
14801 /* Return the number of individual instructions in sync loop INSN,
14802 which has the operands given by OPERANDS. */
14803
14804 unsigned int
14805 mips_sync_loop_insns (rtx_insn *insn, rtx *operands)
14806 {
14807 /* Use branch-likely instructions to work around the LL/SC R10000
14808 errata. */
14809 mips_branch_likely = TARGET_FIX_R10000;
14810 mips_process_sync_loop (insn, operands);
14811 return mips_multi_num_insns;
14812 }
14813
14814 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
14815 the operands given by OPERANDS. Add in a divide-by-zero check if needed.
14816
14817 When working around R4000 and R4400 errata, we need to make sure that
14818 the division is not immediately followed by a shift[1][2]. We also
14819 need to stop the division from being put into a branch delay slot[3].
14820 The easiest way to avoid both problems is to add a nop after the
14821 division. When a divide-by-zero check is needed, this nop can be
14822 used to fill the branch delay slot.
14823
14824 [1] If a double-word or a variable shift executes immediately
14825 after starting an integer division, the shift may give an
14826 incorrect result. See quotations of errata #16 and #28 from
14827 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
14828 in mips.md for details.
14829
14830 [2] A similar bug to [1] exists for all revisions of the
14831 R4000 and the R4400 when run in an MC configuration.
14832 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
14833
14834 "19. In this following sequence:
14835
14836 ddiv (or ddivu or div or divu)
14837 dsll32 (or dsrl32, dsra32)
14838
14839 if an MPT stall occurs, while the divide is slipping the cpu
14840 pipeline, then the following double shift would end up with an
14841 incorrect result.
14842
14843 Workaround: The compiler needs to avoid generating any
14844 sequence with divide followed by extended double shift."
14845
14846 This erratum is also present in "MIPS R4400MC Errata, Processor
14847 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
14848 & 3.0" as errata #10 and #4, respectively.
14849
14850 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
14851 (also valid for MIPS R4000MC processors):
14852
14853 "52. R4000SC: This bug does not apply for the R4000PC.
14854
14855 There are two flavors of this bug:
14856
14857 1) If the instruction just after divide takes an RF exception
14858 (tlb-refill, tlb-invalid) and gets an instruction cache
14859 miss (both primary and secondary) and the line which is
14860 currently in secondary cache at this index had the first
14861 data word, where the bits 5..2 are set, then R4000 would
14862 get a wrong result for the div.
14863
14864 ##1
14865 nop
14866 div r8, r9
14867 ------------------- # end-of page. -tlb-refill
14868 nop
14869 ##2
14870 nop
14871 div r8, r9
14872 ------------------- # end-of page. -tlb-invalid
14873 nop
14874
14875 2) If the divide is in the taken branch delay slot, where the
14876 target takes RF exception and gets an I-cache miss for the
14877 exception vector or where I-cache miss occurs for the
14878 target address, under the above mentioned scenarios, the
14879 div would get wrong results.
14880
14881 ##1
14882 j r2 # to next page mapped or unmapped
14883 div r8,r9 # this bug would be there as long
14884 # as there is an ICache miss and
14885 nop # the "data pattern" is present
14886
14887 ##2
14888 beq r0, r0, NextPage # to Next page
14889 div r8,r9
14890 nop
14891
14892 This bug is present for div, divu, ddiv, and ddivu
14893 instructions.
14894
14895 Workaround: For item 1), OS could make sure that the next page
14896 after the divide instruction is also mapped. For item 2), the
14897 compiler could make sure that the divide instruction is not in
14898 the branch delay slot."
14899
14900 These processors have PRId values of 0x00004220 and 0x00004300 for
14901 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
14902
14903 const char *
14904 mips_output_division (const char *division, rtx *operands)
14905 {
14906 const char *s;
14907
14908 s = division;
14909 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
14910 {
14911 output_asm_insn (s, operands);
14912 s = "nop";
14913 }
14914 if (TARGET_CHECK_ZERO_DIV)
14915 {
14916 if (TARGET_MIPS16)
14917 {
14918 output_asm_insn (s, operands);
14919 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
14920 }
14921 else if (GENERATE_DIVIDE_TRAPS)
14922 {
14923 /* Avoid long replay penalty on load miss by putting the trap before
14924 the divide. */
14925 if (TUNE_74K)
14926 output_asm_insn ("teq\t%2,%.,7", operands);
14927 else
14928 {
14929 output_asm_insn (s, operands);
14930 s = "teq\t%2,%.,7";
14931 }
14932 }
14933 else
14934 {
14935 if (flag_delayed_branch)
14936 {
14937 output_asm_insn ("%(bne\t%2,%.,1f", operands);
14938 output_asm_insn (s, operands);
14939 s = "break\t7%)\n1:";
14940 }
14941 else
14942 {
14943 output_asm_insn (s, operands);
14944 s = "bne\t%2,%.,1f\n\tnop\n\tbreak\t7\n1:";
14945 }
14946 }
14947 }
14948 return s;
14949 }
14950
14951 /* Return the assembly code for MSA DIV_{S,U}.DF or MOD_{S,U}.DF instructions,
14952 which has the operands given by OPERANDS. Add in a divide-by-zero check
14953 if needed. */
14954
14955 const char *
14956 mips_msa_output_division (const char *division, rtx *operands)
14957 {
14958 const char *s;
14959
14960 s = division;
14961 if (TARGET_CHECK_ZERO_DIV)
14962 {
14963 output_asm_insn ("%(bnz.%v0\t%w2,1f", operands);
14964 output_asm_insn (s, operands);
14965 s = "break\t7%)\n1:";
14966 }
14967 return s;
14968 }
14969
14970 /* Return the assembly code for MSA immediate shift instructions,
14971 which has the operands given by OPERANDS. Truncate the shift amount
14972 to make GAS happy. */
14973
14974 const char *
14975 mips_msa_output_shift_immediate (const char *shift, rtx *operands)
14976 {
14977 rtx amount = operands[2];
14978 machine_mode mode = amount->mode;
14979
14980 unsigned val = UINTVAL (CONST_VECTOR_ELT (amount, 0));
14981 val &= GET_MODE_UNIT_BITSIZE (mode) - 1;
14982 if (!val)
14983 return "";
14984
14985 rtx c = gen_int_mode (val, GET_MODE_INNER (mode));
14986 operands[2] = gen_const_vec_duplicate (mode, c);
14987
14988 return shift;
14989 }
14990
14991 /* Return true if destination of IN_INSN is used as add source in
14992 OUT_INSN. Both IN_INSN and OUT_INSN are of type fmadd. Example:
14993 madd.s dst, x, y, z
14994 madd.s a, dst, b, c */
14995
14996 bool
14997 mips_fmadd_bypass (rtx_insn *out_insn, rtx_insn *in_insn)
14998 {
14999 int dst_reg, src_reg;
15000
15001 gcc_assert (get_attr_type (in_insn) == TYPE_FMADD);
15002 gcc_assert (get_attr_type (out_insn) == TYPE_FMADD);
15003
15004 extract_insn (in_insn);
15005 dst_reg = REG_P (recog_data.operand[0]);
15006
15007 extract_insn (out_insn);
15008 src_reg = REG_P (recog_data.operand[1]);
15009
15010 if (dst_reg == src_reg)
15011 return true;
15012
15013 return false;
15014 }
15015
15016 /* Return true if IN_INSN is a multiply-add or multiply-subtract
15017 instruction and if OUT_INSN assigns to the accumulator operand. */
15018
15019 bool
15020 mips_linked_madd_p (rtx_insn *out_insn, rtx_insn *in_insn)
15021 {
15022 enum attr_accum_in accum_in;
15023 int accum_in_opnum;
15024 rtx accum_in_op;
15025
15026 if (recog_memoized (in_insn) < 0)
15027 return false;
15028
15029 accum_in = get_attr_accum_in (in_insn);
15030 if (accum_in == ACCUM_IN_NONE)
15031 return false;
15032
15033 accum_in_opnum = accum_in - ACCUM_IN_0;
15034
15035 extract_insn (in_insn);
15036 gcc_assert (accum_in_opnum < recog_data.n_operands);
15037 accum_in_op = recog_data.operand[accum_in_opnum];
15038
15039 return reg_set_p (accum_in_op, out_insn);
15040 }
15041
15042 /* True if the dependency between OUT_INSN and IN_INSN is on the store
15043 data rather than the address. We need this because the cprestore
15044 pattern is type "store", but is defined using an UNSPEC_VOLATILE,
15045 which causes the default routine to abort. We just return false
15046 for that case. */
15047
15048 bool
15049 mips_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
15050 {
15051 if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
15052 return false;
15053
15054 return store_data_bypass_p (out_insn, in_insn);
15055 }
15056
15057
15058 /* Variables and flags used in scheduler hooks when tuning for
15059 Loongson 2E/2F. */
15060 static struct
15061 {
15062 /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
15063 strategy. */
15064
15065 /* If true, then next ALU1/2 instruction will go to ALU1. */
15066 bool alu1_turn_p;
15067
15068 /* If true, then next FALU1/2 unstruction will go to FALU1. */
15069 bool falu1_turn_p;
15070
15071 /* Codes to query if [f]alu{1,2}_core units are subscribed or not. */
15072 int alu1_core_unit_code;
15073 int alu2_core_unit_code;
15074 int falu1_core_unit_code;
15075 int falu2_core_unit_code;
15076
15077 /* True if current cycle has a multi instruction.
15078 This flag is used in mips_ls2_dfa_post_advance_cycle. */
15079 bool cycle_has_multi_p;
15080
15081 /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
15082 These are used in mips_ls2_dfa_post_advance_cycle to initialize
15083 DFA state.
15084 E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
15085 instruction to go ALU1. */
15086 rtx_insn *alu1_turn_enabled_insn;
15087 rtx_insn *alu2_turn_enabled_insn;
15088 rtx_insn *falu1_turn_enabled_insn;
15089 rtx_insn *falu2_turn_enabled_insn;
15090 } mips_ls2;
15091
15092 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
15093 dependencies have no cost, except on the 20Kc where output-dependence
15094 is treated like input-dependence. */
15095
15096 static int
15097 mips_adjust_cost (rtx_insn *, int dep_type, rtx_insn *, int cost, unsigned int)
15098 {
15099 if (dep_type != 0 && (dep_type != REG_DEP_OUTPUT || !TUNE_20KC))
15100 return 0;
15101 return cost;
15102 }
15103
15104 /* Return the number of instructions that can be issued per cycle. */
15105
15106 static int
15107 mips_issue_rate (void)
15108 {
15109 switch (mips_tune)
15110 {
15111 case PROCESSOR_74KC:
15112 case PROCESSOR_74KF2_1:
15113 case PROCESSOR_74KF1_1:
15114 case PROCESSOR_74KF3_2:
15115 /* The 74k is not strictly quad-issue cpu, but can be seen as one
15116 by the scheduler. It can issue 1 ALU, 1 AGEN and 2 FPU insns,
15117 but in reality only a maximum of 3 insns can be issued as
15118 floating-point loads and stores also require a slot in the
15119 AGEN pipe. */
15120 case PROCESSOR_R10000:
15121 /* All R10K Processors are quad-issue (being the first MIPS
15122 processors to support this feature). */
15123 return 4;
15124
15125 case PROCESSOR_20KC:
15126 case PROCESSOR_R4130:
15127 case PROCESSOR_R5400:
15128 case PROCESSOR_R5500:
15129 case PROCESSOR_R5900:
15130 case PROCESSOR_R7000:
15131 case PROCESSOR_R9000:
15132 case PROCESSOR_OCTEON:
15133 case PROCESSOR_OCTEON2:
15134 case PROCESSOR_OCTEON3:
15135 case PROCESSOR_I6400:
15136 case PROCESSOR_GS264E:
15137 return 2;
15138
15139 case PROCESSOR_SB1:
15140 case PROCESSOR_SB1A:
15141 /* This is actually 4, but we get better performance if we claim 3.
15142 This is partly because of unwanted speculative code motion with the
15143 larger number, and partly because in most common cases we can't
15144 reach the theoretical max of 4. */
15145 return 3;
15146
15147 case PROCESSOR_LOONGSON_2E:
15148 case PROCESSOR_LOONGSON_2F:
15149 case PROCESSOR_GS464:
15150 case PROCESSOR_GS464E:
15151 case PROCESSOR_P5600:
15152 case PROCESSOR_P6600:
15153 return 4;
15154
15155 case PROCESSOR_XLP:
15156 return (reload_completed ? 4 : 3);
15157
15158 default:
15159 return 1;
15160 }
15161 }
15162
15163 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2. */
15164
15165 static void
15166 mips_ls2_init_dfa_post_cycle_insn (void)
15167 {
15168 start_sequence ();
15169 emit_insn (gen_ls2_alu1_turn_enabled_insn ());
15170 mips_ls2.alu1_turn_enabled_insn = get_insns ();
15171 end_sequence ();
15172
15173 start_sequence ();
15174 emit_insn (gen_ls2_alu2_turn_enabled_insn ());
15175 mips_ls2.alu2_turn_enabled_insn = get_insns ();
15176 end_sequence ();
15177
15178 start_sequence ();
15179 emit_insn (gen_ls2_falu1_turn_enabled_insn ());
15180 mips_ls2.falu1_turn_enabled_insn = get_insns ();
15181 end_sequence ();
15182
15183 start_sequence ();
15184 emit_insn (gen_ls2_falu2_turn_enabled_insn ());
15185 mips_ls2.falu2_turn_enabled_insn = get_insns ();
15186 end_sequence ();
15187
15188 mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
15189 mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
15190 mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
15191 mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
15192 }
15193
15194 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
15195 Init data used in mips_dfa_post_advance_cycle. */
15196
15197 static void
15198 mips_init_dfa_post_cycle_insn (void)
15199 {
15200 if (TUNE_LOONGSON_2EF)
15201 mips_ls2_init_dfa_post_cycle_insn ();
15202 }
15203
15204 /* Initialize STATE when scheduling for Loongson 2E/2F.
15205 Support round-robin dispatch scheme by enabling only one of
15206 ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
15207 respectively. */
15208
15209 static void
15210 mips_ls2_dfa_post_advance_cycle (state_t state)
15211 {
15212 if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
15213 {
15214 /* Though there are no non-pipelined ALU1 insns,
15215 we can get an instruction of type 'multi' before reload. */
15216 gcc_assert (mips_ls2.cycle_has_multi_p);
15217 mips_ls2.alu1_turn_p = false;
15218 }
15219
15220 mips_ls2.cycle_has_multi_p = false;
15221
15222 if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
15223 /* We have a non-pipelined alu instruction in the core,
15224 adjust round-robin counter. */
15225 mips_ls2.alu1_turn_p = true;
15226
15227 if (mips_ls2.alu1_turn_p)
15228 {
15229 if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
15230 gcc_unreachable ();
15231 }
15232 else
15233 {
15234 if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
15235 gcc_unreachable ();
15236 }
15237
15238 if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
15239 {
15240 /* There are no non-pipelined FALU1 insns. */
15241 gcc_unreachable ();
15242 mips_ls2.falu1_turn_p = false;
15243 }
15244
15245 if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
15246 /* We have a non-pipelined falu instruction in the core,
15247 adjust round-robin counter. */
15248 mips_ls2.falu1_turn_p = true;
15249
15250 if (mips_ls2.falu1_turn_p)
15251 {
15252 if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
15253 gcc_unreachable ();
15254 }
15255 else
15256 {
15257 if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
15258 gcc_unreachable ();
15259 }
15260 }
15261
15262 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
15263 This hook is being called at the start of each cycle. */
15264
15265 static void
15266 mips_dfa_post_advance_cycle (void)
15267 {
15268 if (TUNE_LOONGSON_2EF)
15269 mips_ls2_dfa_post_advance_cycle (curr_state);
15270 }
15271
15272 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
15273 be as wide as the scheduling freedom in the DFA. */
15274
15275 static int
15276 mips_multipass_dfa_lookahead (void)
15277 {
15278 /* Can schedule up to 4 of the 6 function units in any one cycle. */
15279 if (TUNE_SB1)
15280 return 4;
15281
15282 if (TUNE_LOONGSON_2EF || TUNE_GS464 || TUNE_GS464E)
15283 return 4;
15284
15285 if (TUNE_OCTEON || TUNE_GS264E)
15286 return 2;
15287
15288 if (TUNE_P5600 || TUNE_P6600 || TUNE_I6400)
15289 return 4;
15290
15291 return 0;
15292 }
15293
15294 /* Remove the instruction at index LOWER from ready queue READY and
15295 reinsert it in front of the instruction at index HIGHER. LOWER must
15296 be <= HIGHER. */
15297
15298 static void
15299 mips_promote_ready (rtx_insn **ready, int lower, int higher)
15300 {
15301 rtx_insn *new_head;
15302 int i;
15303
15304 new_head = ready[lower];
15305 for (i = lower; i < higher; i++)
15306 ready[i] = ready[i + 1];
15307 ready[i] = new_head;
15308 }
15309
15310 /* If the priority of the instruction at POS2 in the ready queue READY
15311 is within LIMIT units of that of the instruction at POS1, swap the
15312 instructions if POS2 is not already less than POS1. */
15313
15314 static void
15315 mips_maybe_swap_ready (rtx_insn **ready, int pos1, int pos2, int limit)
15316 {
15317 if (pos1 < pos2
15318 && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
15319 {
15320 rtx_insn *temp;
15321
15322 temp = ready[pos1];
15323 ready[pos1] = ready[pos2];
15324 ready[pos2] = temp;
15325 }
15326 }
15327
15328 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
15329 that may clobber hi or lo. */
15330 static rtx_insn *mips_macc_chains_last_hilo;
15331
15332 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
15333 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
15334
15335 static void
15336 mips_macc_chains_record (rtx_insn *insn)
15337 {
15338 if (get_attr_may_clobber_hilo (insn))
15339 mips_macc_chains_last_hilo = insn;
15340 }
15341
15342 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
15343 has NREADY elements, looking for a multiply-add or multiply-subtract
15344 instruction that is cumulative with mips_macc_chains_last_hilo.
15345 If there is one, promote it ahead of anything else that might
15346 clobber hi or lo. */
15347
15348 static void
15349 mips_macc_chains_reorder (rtx_insn **ready, int nready)
15350 {
15351 int i, j;
15352
15353 if (mips_macc_chains_last_hilo != 0)
15354 for (i = nready - 1; i >= 0; i--)
15355 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
15356 {
15357 for (j = nready - 1; j > i; j--)
15358 if (recog_memoized (ready[j]) >= 0
15359 && get_attr_may_clobber_hilo (ready[j]))
15360 {
15361 mips_promote_ready (ready, i, j);
15362 break;
15363 }
15364 break;
15365 }
15366 }
15367
15368 /* The last instruction to be scheduled. */
15369 static rtx_insn *vr4130_last_insn;
15370
15371 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
15372 points to an rtx that is initially an instruction. Nullify the rtx
15373 if the instruction uses the value of register X. */
15374
15375 static void
15376 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
15377 void *data)
15378 {
15379 rtx *insn_ptr;
15380
15381 insn_ptr = (rtx *) data;
15382 if (REG_P (x)
15383 && *insn_ptr != 0
15384 && reg_referenced_p (x, PATTERN (*insn_ptr)))
15385 *insn_ptr = 0;
15386 }
15387
15388 /* Return true if there is true register dependence between vr4130_last_insn
15389 and INSN. */
15390
15391 static bool
15392 vr4130_true_reg_dependence_p (rtx insn)
15393 {
15394 note_stores (vr4130_last_insn, vr4130_true_reg_dependence_p_1, &insn);
15395 return insn == 0;
15396 }
15397
15398 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
15399 the ready queue and that INSN2 is the instruction after it, return
15400 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
15401 in which INSN1 and INSN2 can probably issue in parallel, but for
15402 which (INSN2, INSN1) should be less sensitive to instruction
15403 alignment than (INSN1, INSN2). See 4130.md for more details. */
15404
15405 static bool
15406 vr4130_swap_insns_p (rtx_insn *insn1, rtx_insn *insn2)
15407 {
15408 sd_iterator_def sd_it;
15409 dep_t dep;
15410
15411 /* Check for the following case:
15412
15413 1) there is some other instruction X with an anti dependence on INSN1;
15414 2) X has a higher priority than INSN2; and
15415 3) X is an arithmetic instruction (and thus has no unit restrictions).
15416
15417 If INSN1 is the last instruction blocking X, it would better to
15418 choose (INSN1, X) over (INSN2, INSN1). */
15419 FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
15420 if (DEP_TYPE (dep) == REG_DEP_ANTI
15421 && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
15422 && recog_memoized (DEP_CON (dep)) >= 0
15423 && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
15424 return false;
15425
15426 if (vr4130_last_insn != 0
15427 && recog_memoized (insn1) >= 0
15428 && recog_memoized (insn2) >= 0)
15429 {
15430 /* See whether INSN1 and INSN2 use different execution units,
15431 or if they are both ALU-type instructions. If so, they can
15432 probably execute in parallel. */
15433 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
15434 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
15435 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
15436 {
15437 /* If only one of the instructions has a dependence on
15438 vr4130_last_insn, prefer to schedule the other one first. */
15439 bool dep1_p = vr4130_true_reg_dependence_p (insn1);
15440 bool dep2_p = vr4130_true_reg_dependence_p (insn2);
15441 if (dep1_p != dep2_p)
15442 return dep1_p;
15443
15444 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
15445 is not an ALU-type instruction and if INSN1 uses the same
15446 execution unit. (Note that if this condition holds, we already
15447 know that INSN2 uses a different execution unit.) */
15448 if (class1 != VR4130_CLASS_ALU
15449 && recog_memoized (vr4130_last_insn) >= 0
15450 && class1 == get_attr_vr4130_class (vr4130_last_insn))
15451 return true;
15452 }
15453 }
15454 return false;
15455 }
15456
15457 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
15458 queue with at least two instructions. Swap the first two if
15459 vr4130_swap_insns_p says that it could be worthwhile. */
15460
15461 static void
15462 vr4130_reorder (rtx_insn **ready, int nready)
15463 {
15464 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
15465 mips_promote_ready (ready, nready - 2, nready - 1);
15466 }
15467
15468 /* Record whether last 74k AGEN instruction was a load or store. */
15469 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
15470
15471 /* Initialize mips_last_74k_agen_insn from INSN. A null argument
15472 resets to TYPE_UNKNOWN state. */
15473
15474 static void
15475 mips_74k_agen_init (rtx_insn *insn)
15476 {
15477 if (!insn || CALL_P (insn) || JUMP_P (insn))
15478 mips_last_74k_agen_insn = TYPE_UNKNOWN;
15479 else
15480 {
15481 enum attr_type type = get_attr_type (insn);
15482 if (type == TYPE_LOAD || type == TYPE_STORE)
15483 mips_last_74k_agen_insn = type;
15484 }
15485 }
15486
15487 /* A TUNE_74K helper function. The 74K AGEN pipeline likes multiple
15488 loads to be grouped together, and multiple stores to be grouped
15489 together. Swap things around in the ready queue to make this happen. */
15490
15491 static void
15492 mips_74k_agen_reorder (rtx_insn **ready, int nready)
15493 {
15494 int i;
15495 int store_pos, load_pos;
15496
15497 store_pos = -1;
15498 load_pos = -1;
15499
15500 for (i = nready - 1; i >= 0; i--)
15501 {
15502 rtx_insn *insn = ready[i];
15503 if (USEFUL_INSN_P (insn))
15504 switch (get_attr_type (insn))
15505 {
15506 case TYPE_STORE:
15507 if (store_pos == -1)
15508 store_pos = i;
15509 break;
15510
15511 case TYPE_LOAD:
15512 if (load_pos == -1)
15513 load_pos = i;
15514 break;
15515
15516 default:
15517 break;
15518 }
15519 }
15520
15521 if (load_pos == -1 || store_pos == -1)
15522 return;
15523
15524 switch (mips_last_74k_agen_insn)
15525 {
15526 case TYPE_UNKNOWN:
15527 /* Prefer to schedule loads since they have a higher latency. */
15528 case TYPE_LOAD:
15529 /* Swap loads to the front of the queue. */
15530 mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
15531 break;
15532 case TYPE_STORE:
15533 /* Swap stores to the front of the queue. */
15534 mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
15535 break;
15536 default:
15537 break;
15538 }
15539 }
15540
15541 /* Implement TARGET_SCHED_INIT. */
15542
15543 static void
15544 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15545 int max_ready ATTRIBUTE_UNUSED)
15546 {
15547 mips_macc_chains_last_hilo = 0;
15548 vr4130_last_insn = 0;
15549 mips_74k_agen_init (NULL);
15550
15551 /* When scheduling for Loongson2, branch instructions go to ALU1,
15552 therefore basic block is most likely to start with round-robin counter
15553 pointed to ALU2. */
15554 mips_ls2.alu1_turn_p = false;
15555 mips_ls2.falu1_turn_p = true;
15556 }
15557
15558 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2. */
15559
15560 static void
15561 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15562 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
15563 {
15564 if (!reload_completed
15565 && TUNE_MACC_CHAINS
15566 && *nreadyp > 0)
15567 mips_macc_chains_reorder (ready, *nreadyp);
15568
15569 if (reload_completed
15570 && TUNE_MIPS4130
15571 && !TARGET_VR4130_ALIGN
15572 && *nreadyp > 1)
15573 vr4130_reorder (ready, *nreadyp);
15574
15575 if (TUNE_74K)
15576 mips_74k_agen_reorder (ready, *nreadyp);
15577 }
15578
15579 /* Implement TARGET_SCHED_REORDER. */
15580
15581 static int
15582 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15583 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
15584 {
15585 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
15586 return mips_issue_rate ();
15587 }
15588
15589 /* Implement TARGET_SCHED_REORDER2. */
15590
15591 static int
15592 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15593 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
15594 {
15595 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
15596 return cached_can_issue_more;
15597 }
15598
15599 /* Update round-robin counters for ALU1/2 and FALU1/2. */
15600
15601 static void
15602 mips_ls2_variable_issue (rtx_insn *insn)
15603 {
15604 if (mips_ls2.alu1_turn_p)
15605 {
15606 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
15607 mips_ls2.alu1_turn_p = false;
15608 }
15609 else
15610 {
15611 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
15612 mips_ls2.alu1_turn_p = true;
15613 }
15614
15615 if (mips_ls2.falu1_turn_p)
15616 {
15617 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
15618 mips_ls2.falu1_turn_p = false;
15619 }
15620 else
15621 {
15622 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
15623 mips_ls2.falu1_turn_p = true;
15624 }
15625
15626 if (recog_memoized (insn) >= 0)
15627 mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
15628 }
15629
15630 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
15631
15632 static int
15633 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
15634 rtx_insn *insn, int more)
15635 {
15636 /* Ignore USEs and CLOBBERs; don't count them against the issue rate. */
15637 if (USEFUL_INSN_P (insn))
15638 {
15639 if (get_attr_type (insn) != TYPE_GHOST)
15640 more--;
15641 if (!reload_completed && TUNE_MACC_CHAINS)
15642 mips_macc_chains_record (insn);
15643 vr4130_last_insn = insn;
15644 if (TUNE_74K)
15645 mips_74k_agen_init (insn);
15646 else if (TUNE_LOONGSON_2EF)
15647 mips_ls2_variable_issue (insn);
15648 }
15649
15650 /* Instructions of type 'multi' should all be split before
15651 the second scheduling pass. */
15652 gcc_assert (!reload_completed
15653 || recog_memoized (insn) < 0
15654 || get_attr_type (insn) != TYPE_MULTI);
15655
15656 cached_can_issue_more = more;
15657 return more;
15658 }
15659
15660 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
15661 return the first operand of the associated PREF or PREFX insn. */
15662
15663 rtx
15664 mips_prefetch_cookie (rtx write, rtx locality)
15665 {
15666 /* store_streamed / load_streamed. */
15667 if (INTVAL (locality) <= 0)
15668 return GEN_INT (INTVAL (write) + 4);
15669
15670 /* store / load. */
15671 if (INTVAL (locality) <= 2)
15672 return write;
15673
15674 /* store_retained / load_retained. */
15675 return GEN_INT (INTVAL (write) + 6);
15676 }
15677
15678 /* Loongson EXT2 only implements pref hint=0 (prefetch for load) and hint=1
15679 (prefetch for store), other hint just scale to hint = 0 and hint = 1. */
15680
15681 rtx
15682 mips_loongson_ext2_prefetch_cookie (rtx write, rtx)
15683 {
15684 /* store. */
15685 if (INTVAL (write) == 1)
15686 return GEN_INT (INTVAL (write));
15687
15688 /* load. */
15689 if (INTVAL (write) == 0)
15690 return GEN_INT (INTVAL (write));
15691
15692 gcc_unreachable ();
15693 }
15694
15695
15696 /* Flags that indicate when a built-in function is available.
15697
15698 BUILTIN_AVAIL_NON_MIPS16
15699 The function is available on the current target if !TARGET_MIPS16.
15700
15701 BUILTIN_AVAIL_MIPS16
15702 The function is available on the current target if TARGET_MIPS16.
15703
15704 BUILTIN_AVAIL_MIPS16E2
15705 The function is available on the current target if TARGET_MIPS16E2. */
15706 #define BUILTIN_AVAIL_NON_MIPS16 1
15707 #define BUILTIN_AVAIL_MIPS16 2
15708 #define BUILTIN_AVAIL_MIPS16E2 4
15709
15710 /* Declare an availability predicate for built-in functions that
15711 require non-MIPS16 mode and also require COND to be true.
15712 NAME is the main part of the predicate's name. */
15713 #define AVAIL_NON_MIPS16(NAME, COND) \
15714 static unsigned int \
15715 mips_builtin_avail_##NAME (void) \
15716 { \
15717 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0; \
15718 }
15719
15720 /* Declare an availability predicate for built-in functions that
15721 require non-MIPS16 mode or MIPS16E2 and also require COND to be true.
15722 NAME is the main part of the predicate's name. */
15723 #define AVAIL_MIPS16E2_OR_NON_MIPS16(NAME, COND) \
15724 static unsigned int \
15725 mips_builtin_avail_##NAME (void) \
15726 { \
15727 return ((COND) ? BUILTIN_AVAIL_NON_MIPS16 | BUILTIN_AVAIL_MIPS16E2 \
15728 : 0); \
15729 }
15730
15731 /* Declare an availability predicate for built-in functions that
15732 support both MIPS16 and non-MIPS16 code and also require COND
15733 to be true. NAME is the main part of the predicate's name. */
15734 #define AVAIL_ALL(NAME, COND) \
15735 static unsigned int \
15736 mips_builtin_avail_##NAME (void) \
15737 { \
15738 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 | BUILTIN_AVAIL_MIPS16 : 0; \
15739 }
15740
15741 /* This structure describes a single built-in function. */
15742 struct mips_builtin_description {
15743 /* The code of the main .md file instruction. See mips_builtin_type
15744 for more information. */
15745 enum insn_code icode;
15746
15747 /* The floating-point comparison code to use with ICODE, if any. */
15748 enum mips_fp_condition cond;
15749
15750 /* The name of the built-in function. */
15751 const char *name;
15752
15753 /* Specifies how the function should be expanded. */
15754 enum mips_builtin_type builtin_type;
15755
15756 /* The function's prototype. */
15757 enum mips_function_type function_type;
15758
15759 /* Whether the function is available. */
15760 unsigned int (*avail) (void);
15761
15762 /* Whether the function is pure. */
15763 bool is_pure;
15764 };
15765
15766 AVAIL_ALL (hard_float, TARGET_HARD_FLOAT_ABI)
15767 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
15768 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
15769 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
15770 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
15771 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
15772 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
15773 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
15774 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
15775 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_MMI)
15776 AVAIL_MIPS16E2_OR_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
15777 AVAIL_NON_MIPS16 (msa, TARGET_MSA)
15778 AVAIL_NON_MIPS16 (r6, mips_isa_rev >= 6)
15779
15780 /* Construct a mips_builtin_description from the given arguments.
15781
15782 INSN is the name of the associated instruction pattern, without the
15783 leading CODE_FOR_mips_.
15784
15785 CODE is the floating-point condition code associated with the
15786 function. It can be 'f' if the field is not applicable.
15787
15788 NAME is the name of the function itself, without the leading
15789 "__builtin_mips_".
15790
15791 BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
15792
15793 AVAIL is the name of the availability predicate, without the leading
15794 mips_builtin_avail_. */
15795 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE, \
15796 FUNCTION_TYPE, AVAIL, PURE) \
15797 { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND, \
15798 "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE, \
15799 mips_builtin_avail_ ## AVAIL, PURE }
15800
15801 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
15802 mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE and AVAIL
15803 are as for MIPS_BUILTIN. */
15804 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
15805 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, \
15806 AVAIL, false)
15807
15808 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT pure function
15809 mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE and AVAIL
15810 are as for MIPS_BUILTIN. */
15811 #define DIRECT_BUILTIN_PURE(INSN, FUNCTION_TYPE, AVAIL) \
15812 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, \
15813 AVAIL, true)
15814
15815 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
15816 are subject to mips_builtin_avail_<AVAIL>. */
15817 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL) \
15818 MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s", \
15819 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL, \
15820 false), \
15821 MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d", \
15822 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL, false)
15823
15824 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
15825 The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
15826 while the any and all forms are subject to mips_builtin_avail_mips3d. */
15827 #define CMP_PS_BUILTINS(INSN, COND, AVAIL) \
15828 MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps", \
15829 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF, \
15830 mips3d, false), \
15831 MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps", \
15832 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF, \
15833 mips3d, false), \
15834 MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
15835 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF, \
15836 AVAIL, false), \
15837 MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
15838 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF, \
15839 AVAIL, false)
15840
15841 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s. The functions
15842 are subject to mips_builtin_avail_mips3d. */
15843 #define CMP_4S_BUILTINS(INSN, COND) \
15844 MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s", \
15845 MIPS_BUILTIN_CMP_ANY, \
15846 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d, false), \
15847 MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s", \
15848 MIPS_BUILTIN_CMP_ALL, \
15849 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d, false)
15850
15851 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps. The comparison
15852 instruction requires mips_builtin_avail_<AVAIL>. */
15853 #define MOVTF_BUILTINS(INSN, COND, AVAIL) \
15854 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps", \
15855 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
15856 AVAIL, false), \
15857 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps", \
15858 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
15859 AVAIL, false)
15860
15861 /* Define all the built-in functions related to C.cond.fmt condition COND. */
15862 #define CMP_BUILTINS(COND) \
15863 MOVTF_BUILTINS (c, COND, paired_single), \
15864 MOVTF_BUILTINS (cabs, COND, mips3d), \
15865 CMP_SCALAR_BUILTINS (cabs, COND, mips3d), \
15866 CMP_PS_BUILTINS (c, COND, paired_single), \
15867 CMP_PS_BUILTINS (cabs, COND, mips3d), \
15868 CMP_4S_BUILTINS (c, COND), \
15869 CMP_4S_BUILTINS (cabs, COND)
15870
15871 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
15872 function mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE
15873 and AVAIL are as for MIPS_BUILTIN. */
15874 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
15875 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET, \
15876 FUNCTION_TYPE, AVAIL, false)
15877
15878 /* Define __builtin_mips_bposge<VALUE>. <VALUE> is 32 for the MIPS32 DSP
15879 branch instruction. AVAIL is as for MIPS_BUILTIN. */
15880 #define BPOSGE_BUILTIN(VALUE, AVAIL) \
15881 MIPS_BUILTIN (bposge, f, "bposge" #VALUE, \
15882 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL, false)
15883
15884 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
15885 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
15886 builtin_description field. */
15887 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE) \
15888 { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f, \
15889 "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT, \
15890 FUNCTION_TYPE, mips_builtin_avail_loongson, false }
15891
15892 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
15893 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
15894 builtin_description field. */
15895 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE) \
15896 LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
15897
15898 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
15899 We use functions of this form when the same insn can be usefully applied
15900 to more than one datatype. */
15901 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE) \
15902 LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
15903
15904 /* Define an MSA MIPS_BUILTIN_DIRECT pure function __builtin_msa_<INSN>
15905 for instruction CODE_FOR_msa_<INSN>. FUNCTION_TYPE is a builtin_description
15906 field. */
15907 #define MSA_BUILTIN_PURE(INSN, FUNCTION_TYPE) \
15908 { CODE_FOR_msa_ ## INSN, MIPS_FP_COND_f, \
15909 "__builtin_msa_" #INSN, MIPS_BUILTIN_DIRECT, \
15910 FUNCTION_TYPE, mips_builtin_avail_msa, true }
15911
15912 /* Define an MSA MIPS_BUILTIN_DIRECT non-pure function __builtin_msa_<INSN>
15913 for instruction CODE_FOR_msa_<INSN>. FUNCTION_TYPE is a builtin_description
15914 field. */
15915 #define MSA_BUILTIN(INSN, FUNCTION_TYPE) \
15916 { CODE_FOR_msa_ ## INSN, MIPS_FP_COND_f, \
15917 "__builtin_msa_" #INSN, MIPS_BUILTIN_DIRECT, \
15918 FUNCTION_TYPE, mips_builtin_avail_msa, false }
15919
15920 /* Define a remapped MSA MIPS_BUILTIN_DIRECT function __builtin_msa_<INSN>
15921 for instruction CODE_FOR_msa_<INSN2>. FUNCTION_TYPE is
15922 a builtin_description field. */
15923 #define MSA_BUILTIN_REMAP(INSN, INSN2, FUNCTION_TYPE) \
15924 { CODE_FOR_msa_ ## INSN2, MIPS_FP_COND_f, \
15925 "__builtin_msa_" #INSN, MIPS_BUILTIN_DIRECT, \
15926 FUNCTION_TYPE, mips_builtin_avail_msa, false }
15927
15928 /* Define an MSA MIPS_BUILTIN_MSA_TEST_BRANCH function __builtin_msa_<INSN>
15929 for instruction CODE_FOR_msa_<INSN>. FUNCTION_TYPE is a builtin_description
15930 field. */
15931 #define MSA_BUILTIN_TEST_BRANCH(INSN, FUNCTION_TYPE) \
15932 { CODE_FOR_msa_ ## INSN, MIPS_FP_COND_f, \
15933 "__builtin_msa_" #INSN, MIPS_BUILTIN_MSA_TEST_BRANCH, \
15934 FUNCTION_TYPE, mips_builtin_avail_msa, false }
15935
15936 /* Define an MSA MIPS_BUILTIN_DIRECT_NO_TARGET function __builtin_msa_<INSN>
15937 for instruction CODE_FOR_msa_<INSN>. FUNCTION_TYPE is a builtin_description
15938 field. */
15939 #define MSA_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE) \
15940 { CODE_FOR_msa_ ## INSN, MIPS_FP_COND_f, \
15941 "__builtin_msa_" #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET, \
15942 FUNCTION_TYPE, mips_builtin_avail_msa, false }
15943
15944 /* Define a MIPSr6 MIPS_BUILTIN_DIRECT pure function __builtin_mipsr6_<INSN>
15945 for instruction CODE_FOR_mipsr6_<INSN>. FUNCTION_TYPE is a builtin_description
15946 field. */
15947 #define MIPSR6_BUILTIN_PURE(INSN, FUNCTION_TYPE) \
15948 { CODE_FOR_mipsr6_ ## INSN, MIPS_FP_COND_f, \
15949 "__builtin_mipsr6_" #INSN, MIPS_BUILTIN_DIRECT, \
15950 FUNCTION_TYPE, mips_builtin_avail_r6, true }
15951
15952 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
15953 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
15954 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
15955 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
15956 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
15957 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
15958 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
15959 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
15960
15961 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
15962 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
15963 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
15964 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
15965 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
15966 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
15967 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
15968 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
15969 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
15970 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
15971 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
15972 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
15973 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
15974 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
15975 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
15976 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
15977 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
15978 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
15979 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
15980 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
15981 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
15982 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
15983 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
15984 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
15985 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
15986 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
15987 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
15988 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
15989 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
15990 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
15991
15992 #define CODE_FOR_msa_adds_s_b CODE_FOR_ssaddv16qi3
15993 #define CODE_FOR_msa_adds_s_h CODE_FOR_ssaddv8hi3
15994 #define CODE_FOR_msa_adds_s_w CODE_FOR_ssaddv4si3
15995 #define CODE_FOR_msa_adds_s_d CODE_FOR_ssaddv2di3
15996 #define CODE_FOR_msa_adds_u_b CODE_FOR_usaddv16qi3
15997 #define CODE_FOR_msa_adds_u_h CODE_FOR_usaddv8hi3
15998 #define CODE_FOR_msa_adds_u_w CODE_FOR_usaddv4si3
15999 #define CODE_FOR_msa_adds_u_d CODE_FOR_usaddv2di3
16000 #define CODE_FOR_msa_addv_b CODE_FOR_addv16qi3
16001 #define CODE_FOR_msa_addv_h CODE_FOR_addv8hi3
16002 #define CODE_FOR_msa_addv_w CODE_FOR_addv4si3
16003 #define CODE_FOR_msa_addv_d CODE_FOR_addv2di3
16004 #define CODE_FOR_msa_addvi_b CODE_FOR_addv16qi3
16005 #define CODE_FOR_msa_addvi_h CODE_FOR_addv8hi3
16006 #define CODE_FOR_msa_addvi_w CODE_FOR_addv4si3
16007 #define CODE_FOR_msa_addvi_d CODE_FOR_addv2di3
16008 #define CODE_FOR_msa_and_v CODE_FOR_andv16qi3
16009 #define CODE_FOR_msa_andi_b CODE_FOR_andv16qi3
16010 #define CODE_FOR_msa_bmnz_v CODE_FOR_msa_bmnz_b
16011 #define CODE_FOR_msa_bmnzi_b CODE_FOR_msa_bmnz_b
16012 #define CODE_FOR_msa_bmz_v CODE_FOR_msa_bmz_b
16013 #define CODE_FOR_msa_bmzi_b CODE_FOR_msa_bmz_b
16014 #define CODE_FOR_msa_bnz_v CODE_FOR_msa_bnz_v_b
16015 #define CODE_FOR_msa_bz_v CODE_FOR_msa_bz_v_b
16016 #define CODE_FOR_msa_bsel_v CODE_FOR_msa_bsel_b
16017 #define CODE_FOR_msa_bseli_b CODE_FOR_msa_bsel_b
16018 #define CODE_FOR_msa_ceqi_b CODE_FOR_msa_ceq_b
16019 #define CODE_FOR_msa_ceqi_h CODE_FOR_msa_ceq_h
16020 #define CODE_FOR_msa_ceqi_w CODE_FOR_msa_ceq_w
16021 #define CODE_FOR_msa_ceqi_d CODE_FOR_msa_ceq_d
16022 #define CODE_FOR_msa_clti_s_b CODE_FOR_msa_clt_s_b
16023 #define CODE_FOR_msa_clti_s_h CODE_FOR_msa_clt_s_h
16024 #define CODE_FOR_msa_clti_s_w CODE_FOR_msa_clt_s_w
16025 #define CODE_FOR_msa_clti_s_d CODE_FOR_msa_clt_s_d
16026 #define CODE_FOR_msa_clti_u_b CODE_FOR_msa_clt_u_b
16027 #define CODE_FOR_msa_clti_u_h CODE_FOR_msa_clt_u_h
16028 #define CODE_FOR_msa_clti_u_w CODE_FOR_msa_clt_u_w
16029 #define CODE_FOR_msa_clti_u_d CODE_FOR_msa_clt_u_d
16030 #define CODE_FOR_msa_clei_s_b CODE_FOR_msa_cle_s_b
16031 #define CODE_FOR_msa_clei_s_h CODE_FOR_msa_cle_s_h
16032 #define CODE_FOR_msa_clei_s_w CODE_FOR_msa_cle_s_w
16033 #define CODE_FOR_msa_clei_s_d CODE_FOR_msa_cle_s_d
16034 #define CODE_FOR_msa_clei_u_b CODE_FOR_msa_cle_u_b
16035 #define CODE_FOR_msa_clei_u_h CODE_FOR_msa_cle_u_h
16036 #define CODE_FOR_msa_clei_u_w CODE_FOR_msa_cle_u_w
16037 #define CODE_FOR_msa_clei_u_d CODE_FOR_msa_cle_u_d
16038 #define CODE_FOR_msa_div_s_b CODE_FOR_divv16qi3
16039 #define CODE_FOR_msa_div_s_h CODE_FOR_divv8hi3
16040 #define CODE_FOR_msa_div_s_w CODE_FOR_divv4si3
16041 #define CODE_FOR_msa_div_s_d CODE_FOR_divv2di3
16042 #define CODE_FOR_msa_div_u_b CODE_FOR_udivv16qi3
16043 #define CODE_FOR_msa_div_u_h CODE_FOR_udivv8hi3
16044 #define CODE_FOR_msa_div_u_w CODE_FOR_udivv4si3
16045 #define CODE_FOR_msa_div_u_d CODE_FOR_udivv2di3
16046 #define CODE_FOR_msa_fadd_w CODE_FOR_addv4sf3
16047 #define CODE_FOR_msa_fadd_d CODE_FOR_addv2df3
16048 #define CODE_FOR_msa_fexdo_w CODE_FOR_vec_pack_trunc_v2df
16049 #define CODE_FOR_msa_ftrunc_s_w CODE_FOR_fix_truncv4sfv4si2
16050 #define CODE_FOR_msa_ftrunc_s_d CODE_FOR_fix_truncv2dfv2di2
16051 #define CODE_FOR_msa_ftrunc_u_w CODE_FOR_fixuns_truncv4sfv4si2
16052 #define CODE_FOR_msa_ftrunc_u_d CODE_FOR_fixuns_truncv2dfv2di2
16053 #define CODE_FOR_msa_ffint_s_w CODE_FOR_floatv4siv4sf2
16054 #define CODE_FOR_msa_ffint_s_d CODE_FOR_floatv2div2df2
16055 #define CODE_FOR_msa_ffint_u_w CODE_FOR_floatunsv4siv4sf2
16056 #define CODE_FOR_msa_ffint_u_d CODE_FOR_floatunsv2div2df2
16057 #define CODE_FOR_msa_fsub_w CODE_FOR_subv4sf3
16058 #define CODE_FOR_msa_fsub_d CODE_FOR_subv2df3
16059 #define CODE_FOR_msa_fmadd_w CODE_FOR_fmav4sf4
16060 #define CODE_FOR_msa_fmadd_d CODE_FOR_fmav2df4
16061 #define CODE_FOR_msa_fmsub_w CODE_FOR_fnmav4sf4
16062 #define CODE_FOR_msa_fmsub_d CODE_FOR_fnmav2df4
16063 #define CODE_FOR_msa_fmul_w CODE_FOR_mulv4sf3
16064 #define CODE_FOR_msa_fmul_d CODE_FOR_mulv2df3
16065 #define CODE_FOR_msa_fdiv_w CODE_FOR_divv4sf3
16066 #define CODE_FOR_msa_fdiv_d CODE_FOR_divv2df3
16067 #define CODE_FOR_msa_fmax_w CODE_FOR_smaxv4sf3
16068 #define CODE_FOR_msa_fmax_d CODE_FOR_smaxv2df3
16069 #define CODE_FOR_msa_fmin_w CODE_FOR_sminv4sf3
16070 #define CODE_FOR_msa_fmin_d CODE_FOR_sminv2df3
16071 #define CODE_FOR_msa_fsqrt_w CODE_FOR_sqrtv4sf2
16072 #define CODE_FOR_msa_fsqrt_d CODE_FOR_sqrtv2df2
16073 #define CODE_FOR_msa_max_s_b CODE_FOR_smaxv16qi3
16074 #define CODE_FOR_msa_max_s_h CODE_FOR_smaxv8hi3
16075 #define CODE_FOR_msa_max_s_w CODE_FOR_smaxv4si3
16076 #define CODE_FOR_msa_max_s_d CODE_FOR_smaxv2di3
16077 #define CODE_FOR_msa_maxi_s_b CODE_FOR_smaxv16qi3
16078 #define CODE_FOR_msa_maxi_s_h CODE_FOR_smaxv8hi3
16079 #define CODE_FOR_msa_maxi_s_w CODE_FOR_smaxv4si3
16080 #define CODE_FOR_msa_maxi_s_d CODE_FOR_smaxv2di3
16081 #define CODE_FOR_msa_max_u_b CODE_FOR_umaxv16qi3
16082 #define CODE_FOR_msa_max_u_h CODE_FOR_umaxv8hi3
16083 #define CODE_FOR_msa_max_u_w CODE_FOR_umaxv4si3
16084 #define CODE_FOR_msa_max_u_d CODE_FOR_umaxv2di3
16085 #define CODE_FOR_msa_maxi_u_b CODE_FOR_umaxv16qi3
16086 #define CODE_FOR_msa_maxi_u_h CODE_FOR_umaxv8hi3
16087 #define CODE_FOR_msa_maxi_u_w CODE_FOR_umaxv4si3
16088 #define CODE_FOR_msa_maxi_u_d CODE_FOR_umaxv2di3
16089 #define CODE_FOR_msa_min_s_b CODE_FOR_sminv16qi3
16090 #define CODE_FOR_msa_min_s_h CODE_FOR_sminv8hi3
16091 #define CODE_FOR_msa_min_s_w CODE_FOR_sminv4si3
16092 #define CODE_FOR_msa_min_s_d CODE_FOR_sminv2di3
16093 #define CODE_FOR_msa_mini_s_b CODE_FOR_sminv16qi3
16094 #define CODE_FOR_msa_mini_s_h CODE_FOR_sminv8hi3
16095 #define CODE_FOR_msa_mini_s_w CODE_FOR_sminv4si3
16096 #define CODE_FOR_msa_mini_s_d CODE_FOR_sminv2di3
16097 #define CODE_FOR_msa_min_u_b CODE_FOR_uminv16qi3
16098 #define CODE_FOR_msa_min_u_h CODE_FOR_uminv8hi3
16099 #define CODE_FOR_msa_min_u_w CODE_FOR_uminv4si3
16100 #define CODE_FOR_msa_min_u_d CODE_FOR_uminv2di3
16101 #define CODE_FOR_msa_mini_u_b CODE_FOR_uminv16qi3
16102 #define CODE_FOR_msa_mini_u_h CODE_FOR_uminv8hi3
16103 #define CODE_FOR_msa_mini_u_w CODE_FOR_uminv4si3
16104 #define CODE_FOR_msa_mini_u_d CODE_FOR_uminv2di3
16105 #define CODE_FOR_msa_mod_s_b CODE_FOR_modv16qi3
16106 #define CODE_FOR_msa_mod_s_h CODE_FOR_modv8hi3
16107 #define CODE_FOR_msa_mod_s_w CODE_FOR_modv4si3
16108 #define CODE_FOR_msa_mod_s_d CODE_FOR_modv2di3
16109 #define CODE_FOR_msa_mod_u_b CODE_FOR_umodv16qi3
16110 #define CODE_FOR_msa_mod_u_h CODE_FOR_umodv8hi3
16111 #define CODE_FOR_msa_mod_u_w CODE_FOR_umodv4si3
16112 #define CODE_FOR_msa_mod_u_d CODE_FOR_umodv2di3
16113 #define CODE_FOR_msa_mod_s_b CODE_FOR_modv16qi3
16114 #define CODE_FOR_msa_mod_s_h CODE_FOR_modv8hi3
16115 #define CODE_FOR_msa_mod_s_w CODE_FOR_modv4si3
16116 #define CODE_FOR_msa_mod_s_d CODE_FOR_modv2di3
16117 #define CODE_FOR_msa_mod_u_b CODE_FOR_umodv16qi3
16118 #define CODE_FOR_msa_mod_u_h CODE_FOR_umodv8hi3
16119 #define CODE_FOR_msa_mod_u_w CODE_FOR_umodv4si3
16120 #define CODE_FOR_msa_mod_u_d CODE_FOR_umodv2di3
16121 #define CODE_FOR_msa_mulv_b CODE_FOR_mulv16qi3
16122 #define CODE_FOR_msa_mulv_h CODE_FOR_mulv8hi3
16123 #define CODE_FOR_msa_mulv_w CODE_FOR_mulv4si3
16124 #define CODE_FOR_msa_mulv_d CODE_FOR_mulv2di3
16125 #define CODE_FOR_msa_nlzc_b CODE_FOR_clzv16qi2
16126 #define CODE_FOR_msa_nlzc_h CODE_FOR_clzv8hi2
16127 #define CODE_FOR_msa_nlzc_w CODE_FOR_clzv4si2
16128 #define CODE_FOR_msa_nlzc_d CODE_FOR_clzv2di2
16129 #define CODE_FOR_msa_nor_v CODE_FOR_msa_nor_b
16130 #define CODE_FOR_msa_or_v CODE_FOR_iorv16qi3
16131 #define CODE_FOR_msa_ori_b CODE_FOR_iorv16qi3
16132 #define CODE_FOR_msa_nori_b CODE_FOR_msa_nor_b
16133 #define CODE_FOR_msa_pcnt_b CODE_FOR_popcountv16qi2
16134 #define CODE_FOR_msa_pcnt_h CODE_FOR_popcountv8hi2
16135 #define CODE_FOR_msa_pcnt_w CODE_FOR_popcountv4si2
16136 #define CODE_FOR_msa_pcnt_d CODE_FOR_popcountv2di2
16137 #define CODE_FOR_msa_xor_v CODE_FOR_xorv16qi3
16138 #define CODE_FOR_msa_xori_b CODE_FOR_xorv16qi3
16139 #define CODE_FOR_msa_sll_b CODE_FOR_vashlv16qi3
16140 #define CODE_FOR_msa_sll_h CODE_FOR_vashlv8hi3
16141 #define CODE_FOR_msa_sll_w CODE_FOR_vashlv4si3
16142 #define CODE_FOR_msa_sll_d CODE_FOR_vashlv2di3
16143 #define CODE_FOR_msa_slli_b CODE_FOR_vashlv16qi3
16144 #define CODE_FOR_msa_slli_h CODE_FOR_vashlv8hi3
16145 #define CODE_FOR_msa_slli_w CODE_FOR_vashlv4si3
16146 #define CODE_FOR_msa_slli_d CODE_FOR_vashlv2di3
16147 #define CODE_FOR_msa_sra_b CODE_FOR_vashrv16qi3
16148 #define CODE_FOR_msa_sra_h CODE_FOR_vashrv8hi3
16149 #define CODE_FOR_msa_sra_w CODE_FOR_vashrv4si3
16150 #define CODE_FOR_msa_sra_d CODE_FOR_vashrv2di3
16151 #define CODE_FOR_msa_srai_b CODE_FOR_vashrv16qi3
16152 #define CODE_FOR_msa_srai_h CODE_FOR_vashrv8hi3
16153 #define CODE_FOR_msa_srai_w CODE_FOR_vashrv4si3
16154 #define CODE_FOR_msa_srai_d CODE_FOR_vashrv2di3
16155 #define CODE_FOR_msa_srl_b CODE_FOR_vlshrv16qi3
16156 #define CODE_FOR_msa_srl_h CODE_FOR_vlshrv8hi3
16157 #define CODE_FOR_msa_srl_w CODE_FOR_vlshrv4si3
16158 #define CODE_FOR_msa_srl_d CODE_FOR_vlshrv2di3
16159 #define CODE_FOR_msa_srli_b CODE_FOR_vlshrv16qi3
16160 #define CODE_FOR_msa_srli_h CODE_FOR_vlshrv8hi3
16161 #define CODE_FOR_msa_srli_w CODE_FOR_vlshrv4si3
16162 #define CODE_FOR_msa_srli_d CODE_FOR_vlshrv2di3
16163 #define CODE_FOR_msa_subv_b CODE_FOR_subv16qi3
16164 #define CODE_FOR_msa_subv_h CODE_FOR_subv8hi3
16165 #define CODE_FOR_msa_subv_w CODE_FOR_subv4si3
16166 #define CODE_FOR_msa_subv_d CODE_FOR_subv2di3
16167 #define CODE_FOR_msa_subvi_b CODE_FOR_subv16qi3
16168 #define CODE_FOR_msa_subvi_h CODE_FOR_subv8hi3
16169 #define CODE_FOR_msa_subvi_w CODE_FOR_subv4si3
16170 #define CODE_FOR_msa_subvi_d CODE_FOR_subv2di3
16171
16172 #define CODE_FOR_msa_move_v CODE_FOR_movv16qi
16173
16174 #define CODE_FOR_msa_vshf_b CODE_FOR_vec_permv16qi
16175 #define CODE_FOR_msa_vshf_h CODE_FOR_vec_permv8hi
16176 #define CODE_FOR_msa_vshf_w CODE_FOR_vec_permv4si
16177 #define CODE_FOR_msa_vshf_d CODE_FOR_vec_permv2di
16178
16179 #define CODE_FOR_msa_ilvod_d CODE_FOR_msa_ilvl_d
16180 #define CODE_FOR_msa_ilvev_d CODE_FOR_msa_ilvr_d
16181 #define CODE_FOR_msa_pckod_d CODE_FOR_msa_ilvl_d
16182 #define CODE_FOR_msa_pckev_d CODE_FOR_msa_ilvr_d
16183
16184 #define CODE_FOR_msa_ldi_b CODE_FOR_msa_ldiv16qi
16185 #define CODE_FOR_msa_ldi_h CODE_FOR_msa_ldiv8hi
16186 #define CODE_FOR_msa_ldi_w CODE_FOR_msa_ldiv4si
16187 #define CODE_FOR_msa_ldi_d CODE_FOR_msa_ldiv2di
16188
16189 #define CODE_FOR_mipsr6_min_a_s CODE_FOR_fmin_a_sf
16190 #define CODE_FOR_mipsr6_min_a_d CODE_FOR_fmin_a_df
16191 #define CODE_FOR_mipsr6_max_a_s CODE_FOR_fmax_a_sf
16192 #define CODE_FOR_mipsr6_max_a_d CODE_FOR_fmax_a_df
16193 #define CODE_FOR_mipsr6_class_s CODE_FOR_fclass_sf
16194 #define CODE_FOR_mipsr6_class_d CODE_FOR_fclass_df
16195
16196 static const struct mips_builtin_description mips_builtins[] = {
16197 #define MIPS_GET_FCSR 0
16198 DIRECT_BUILTIN (get_fcsr, MIPS_USI_FTYPE_VOID, hard_float),
16199 #define MIPS_SET_FCSR 1
16200 DIRECT_NO_TARGET_BUILTIN (set_fcsr, MIPS_VOID_FTYPE_USI, hard_float),
16201
16202 DIRECT_BUILTIN_PURE (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
16203 DIRECT_BUILTIN_PURE (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
16204 DIRECT_BUILTIN_PURE (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
16205 DIRECT_BUILTIN_PURE (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
16206 DIRECT_BUILTIN_PURE (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
16207 DIRECT_BUILTIN_PURE (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
16208 DIRECT_BUILTIN_PURE (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
16209 DIRECT_BUILTIN_PURE (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
16210
16211 DIRECT_BUILTIN_PURE (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
16212 DIRECT_BUILTIN_PURE (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
16213 DIRECT_BUILTIN_PURE (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
16214 DIRECT_BUILTIN_PURE (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
16215 DIRECT_BUILTIN_PURE (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
16216
16217 DIRECT_BUILTIN_PURE (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
16218 DIRECT_BUILTIN_PURE (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
16219 DIRECT_BUILTIN_PURE (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
16220 DIRECT_BUILTIN_PURE (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
16221 DIRECT_BUILTIN_PURE (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
16222 DIRECT_BUILTIN_PURE (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
16223
16224 DIRECT_BUILTIN_PURE (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
16225 DIRECT_BUILTIN_PURE (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
16226 DIRECT_BUILTIN_PURE (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
16227 DIRECT_BUILTIN_PURE (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
16228 DIRECT_BUILTIN_PURE (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
16229 DIRECT_BUILTIN_PURE (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
16230
16231 MIPS_FP_CONDITIONS (CMP_BUILTINS),
16232
16233 /* Built-in functions for the SB-1 processor. */
16234 DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
16235
16236 /* Built-in functions for the DSP ASE (32-bit and 64-bit). */
16237 DIRECT_BUILTIN_PURE (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
16238 DIRECT_BUILTIN_PURE (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
16239 DIRECT_BUILTIN_PURE (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
16240 DIRECT_BUILTIN_PURE (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
16241 DIRECT_BUILTIN_PURE (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
16242 DIRECT_BUILTIN_PURE (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
16243 DIRECT_BUILTIN_PURE (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
16244 DIRECT_BUILTIN_PURE (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
16245 DIRECT_BUILTIN_PURE (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
16246 DIRECT_BUILTIN_PURE (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
16247 DIRECT_BUILTIN_PURE (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
16248 DIRECT_BUILTIN_PURE (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
16249 DIRECT_BUILTIN_PURE (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
16250 DIRECT_BUILTIN_PURE (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
16251 DIRECT_BUILTIN_PURE (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
16252 DIRECT_BUILTIN_PURE (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
16253 DIRECT_BUILTIN_PURE (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
16254 DIRECT_BUILTIN_PURE (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
16255 DIRECT_BUILTIN_PURE (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
16256 DIRECT_BUILTIN_PURE (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
16257 DIRECT_BUILTIN_PURE (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
16258 DIRECT_BUILTIN_PURE (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
16259 DIRECT_BUILTIN_PURE (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
16260 DIRECT_BUILTIN_PURE (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
16261 DIRECT_BUILTIN_PURE (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
16262 DIRECT_BUILTIN_PURE (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
16263 DIRECT_BUILTIN_PURE (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
16264 DIRECT_BUILTIN_PURE (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
16265 DIRECT_BUILTIN_PURE (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
16266 DIRECT_BUILTIN_PURE (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
16267 DIRECT_BUILTIN_PURE (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
16268 DIRECT_BUILTIN_PURE (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
16269 DIRECT_BUILTIN_PURE (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
16270 DIRECT_BUILTIN_PURE (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
16271 DIRECT_BUILTIN_PURE (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
16272 DIRECT_BUILTIN_PURE (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
16273 DIRECT_BUILTIN_PURE (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
16274 DIRECT_BUILTIN_PURE (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
16275 DIRECT_BUILTIN_PURE (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
16276 DIRECT_BUILTIN_PURE (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
16277 DIRECT_BUILTIN_PURE (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
16278 DIRECT_BUILTIN_PURE (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
16279 DIRECT_BUILTIN_PURE (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
16280 DIRECT_BUILTIN_PURE (bitrev, MIPS_SI_FTYPE_SI, dsp),
16281 DIRECT_BUILTIN_PURE (insv, MIPS_SI_FTYPE_SI_SI, dsp),
16282 DIRECT_BUILTIN_PURE (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
16283 DIRECT_BUILTIN_PURE (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
16284 DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
16285 DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
16286 DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
16287 DIRECT_BUILTIN_PURE (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
16288 DIRECT_BUILTIN_PURE (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
16289 DIRECT_BUILTIN_PURE (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
16290 DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
16291 DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
16292 DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
16293 DIRECT_BUILTIN_PURE (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
16294 DIRECT_BUILTIN_PURE (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
16295 DIRECT_BUILTIN_PURE (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
16296 DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
16297 DIRECT_BUILTIN_PURE (rddsp, MIPS_SI_FTYPE_SI, dsp),
16298 DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
16299 DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
16300 DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
16301 BPOSGE_BUILTIN (32, dsp),
16302
16303 /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit). */
16304 DIRECT_BUILTIN_PURE (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
16305 DIRECT_BUILTIN_PURE (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16306 DIRECT_BUILTIN_PURE (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16307 DIRECT_BUILTIN_PURE (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
16308 DIRECT_BUILTIN_PURE (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
16309 DIRECT_BUILTIN_PURE (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
16310 DIRECT_BUILTIN_PURE (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
16311 DIRECT_BUILTIN_PURE (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
16312 DIRECT_BUILTIN_PURE (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
16313 DIRECT_BUILTIN_PURE (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
16314 DIRECT_BUILTIN_PURE (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16315 DIRECT_BUILTIN_PURE (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16316 DIRECT_BUILTIN_PURE (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
16317 DIRECT_BUILTIN_PURE (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16318 DIRECT_BUILTIN_PURE (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
16319 DIRECT_BUILTIN_PURE (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
16320 DIRECT_BUILTIN_PURE (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
16321 DIRECT_BUILTIN_PURE (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
16322 DIRECT_BUILTIN_PURE (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
16323 DIRECT_BUILTIN_PURE (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
16324 DIRECT_BUILTIN_PURE (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
16325 DIRECT_BUILTIN_PURE (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
16326 DIRECT_BUILTIN_PURE (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16327 DIRECT_BUILTIN_PURE (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16328 DIRECT_BUILTIN_PURE (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
16329 DIRECT_BUILTIN_PURE (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
16330 DIRECT_BUILTIN_PURE (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16331 DIRECT_BUILTIN_PURE (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16332 DIRECT_BUILTIN_PURE (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
16333 DIRECT_BUILTIN_PURE (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
16334 DIRECT_BUILTIN_PURE (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16335 DIRECT_BUILTIN_PURE (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
16336 DIRECT_BUILTIN_PURE (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
16337 DIRECT_BUILTIN_PURE (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
16338
16339 /* Built-in functions for the DSP ASE (32-bit only). */
16340 DIRECT_BUILTIN_PURE (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
16341 DIRECT_BUILTIN_PURE (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
16342 DIRECT_BUILTIN_PURE (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
16343 DIRECT_BUILTIN_PURE (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
16344 DIRECT_BUILTIN_PURE (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
16345 DIRECT_BUILTIN_PURE (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
16346 DIRECT_BUILTIN_PURE (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
16347 DIRECT_BUILTIN_PURE (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
16348 DIRECT_BUILTIN_PURE (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
16349 DIRECT_BUILTIN_PURE (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
16350 DIRECT_BUILTIN_PURE (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
16351 DIRECT_BUILTIN_PURE (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
16352 DIRECT_BUILTIN_PURE (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
16353 DIRECT_BUILTIN_PURE (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
16354 DIRECT_BUILTIN_PURE (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
16355 DIRECT_BUILTIN_PURE (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
16356 DIRECT_BUILTIN_PURE (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
16357 DIRECT_BUILTIN_PURE (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
16358 DIRECT_BUILTIN_PURE (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
16359 DIRECT_BUILTIN_PURE (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
16360 DIRECT_BUILTIN_PURE (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
16361 DIRECT_BUILTIN_PURE (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
16362 DIRECT_BUILTIN_PURE (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
16363 DIRECT_BUILTIN_PURE (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
16364 DIRECT_BUILTIN_PURE (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
16365 DIRECT_BUILTIN_PURE (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
16366 DIRECT_BUILTIN_PURE (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
16367
16368 /* Built-in functions for the DSP ASE (64-bit only). */
16369 DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
16370
16371 /* The following are for the MIPS DSP ASE REV 2 (32-bit only). */
16372 DIRECT_BUILTIN_PURE (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16373 DIRECT_BUILTIN_PURE (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16374 DIRECT_BUILTIN_PURE (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16375 DIRECT_BUILTIN_PURE (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16376 DIRECT_BUILTIN_PURE (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16377 DIRECT_BUILTIN_PURE (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16378 DIRECT_BUILTIN_PURE (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16379 DIRECT_BUILTIN_PURE (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16380 DIRECT_BUILTIN_PURE (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
16381
16382 /* Builtin functions for ST Microelectronics Loongson-2E/2F cores. */
16383 LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
16384 LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
16385 LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
16386 LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
16387 LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16388 LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16389 LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
16390 LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16391 LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
16392 LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
16393 LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
16394 LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
16395 LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
16396 LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16397 LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16398 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
16399 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
16400 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16401 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16402 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
16403 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
16404 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
16405 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
16406 LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16407 LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16408 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
16409 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16410 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16411 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
16412 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16413 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
16414 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
16415 LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16416 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16417 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
16418 LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16419 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
16420 LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
16421 LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
16422 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16423 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16424 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16425 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16426 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16427 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16428 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16429 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16430 LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
16431 LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
16432 LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16433 LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
16434 LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16435 LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
16436 LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
16437 LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16438 LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
16439 LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
16440 LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
16441 LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16442 LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
16443 LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
16444 LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
16445 LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
16446 LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
16447 LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
16448 LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
16449 LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
16450 LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
16451 LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
16452 LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
16453 LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
16454 LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
16455 LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
16456 LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
16457 LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
16458 LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
16459 LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16460 LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16461 LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
16462 LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16463 LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
16464 LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
16465 LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
16466 LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
16467 LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
16468 LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16469 LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16470 LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16471 LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16472 LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
16473 LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
16474 LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16475 LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
16476 LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
16477 LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
16478 LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
16479 LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
16480 LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
16481 LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
16482
16483 /* Sundry other built-in functions. */
16484 DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache),
16485
16486 /* Built-in functions for MSA. */
16487 MSA_BUILTIN_PURE (sll_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16488 MSA_BUILTIN_PURE (sll_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16489 MSA_BUILTIN_PURE (sll_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16490 MSA_BUILTIN_PURE (sll_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16491 MSA_BUILTIN_PURE (slli_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16492 MSA_BUILTIN_PURE (slli_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16493 MSA_BUILTIN_PURE (slli_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16494 MSA_BUILTIN_PURE (slli_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16495 MSA_BUILTIN_PURE (sra_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16496 MSA_BUILTIN_PURE (sra_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16497 MSA_BUILTIN_PURE (sra_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16498 MSA_BUILTIN_PURE (sra_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16499 MSA_BUILTIN_PURE (srai_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16500 MSA_BUILTIN_PURE (srai_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16501 MSA_BUILTIN_PURE (srai_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16502 MSA_BUILTIN_PURE (srai_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16503 MSA_BUILTIN_PURE (srar_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16504 MSA_BUILTIN_PURE (srar_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16505 MSA_BUILTIN_PURE (srar_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16506 MSA_BUILTIN_PURE (srar_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16507 MSA_BUILTIN_PURE (srari_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16508 MSA_BUILTIN_PURE (srari_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16509 MSA_BUILTIN_PURE (srari_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16510 MSA_BUILTIN_PURE (srari_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16511 MSA_BUILTIN_PURE (srl_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16512 MSA_BUILTIN_PURE (srl_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16513 MSA_BUILTIN_PURE (srl_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16514 MSA_BUILTIN_PURE (srl_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16515 MSA_BUILTIN_PURE (srli_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16516 MSA_BUILTIN_PURE (srli_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16517 MSA_BUILTIN_PURE (srli_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16518 MSA_BUILTIN_PURE (srli_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16519 MSA_BUILTIN_PURE (srlr_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16520 MSA_BUILTIN_PURE (srlr_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16521 MSA_BUILTIN_PURE (srlr_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16522 MSA_BUILTIN_PURE (srlr_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16523 MSA_BUILTIN_PURE (srlri_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16524 MSA_BUILTIN_PURE (srlri_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16525 MSA_BUILTIN_PURE (srlri_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16526 MSA_BUILTIN_PURE (srlri_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16527 MSA_BUILTIN_PURE (bclr_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16528 MSA_BUILTIN_PURE (bclr_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16529 MSA_BUILTIN_PURE (bclr_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16530 MSA_BUILTIN_PURE (bclr_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16531 MSA_BUILTIN_PURE (bclri_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16532 MSA_BUILTIN_PURE (bclri_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
16533 MSA_BUILTIN_PURE (bclri_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
16534 MSA_BUILTIN_PURE (bclri_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
16535 MSA_BUILTIN_PURE (bset_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16536 MSA_BUILTIN_PURE (bset_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16537 MSA_BUILTIN_PURE (bset_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16538 MSA_BUILTIN_PURE (bset_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16539 MSA_BUILTIN_PURE (bseti_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16540 MSA_BUILTIN_PURE (bseti_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
16541 MSA_BUILTIN_PURE (bseti_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
16542 MSA_BUILTIN_PURE (bseti_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
16543 MSA_BUILTIN_PURE (bneg_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16544 MSA_BUILTIN_PURE (bneg_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16545 MSA_BUILTIN_PURE (bneg_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16546 MSA_BUILTIN_PURE (bneg_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16547 MSA_BUILTIN_PURE (bnegi_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16548 MSA_BUILTIN_PURE (bnegi_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
16549 MSA_BUILTIN_PURE (bnegi_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
16550 MSA_BUILTIN_PURE (bnegi_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
16551 MSA_BUILTIN_PURE (binsl_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
16552 MSA_BUILTIN_PURE (binsl_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI_UV8HI),
16553 MSA_BUILTIN_PURE (binsl_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI_UV4SI),
16554 MSA_BUILTIN_PURE (binsl_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI_UV2DI),
16555 MSA_BUILTIN_PURE (binsli_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
16556 MSA_BUILTIN_PURE (binsli_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI_UQI),
16557 MSA_BUILTIN_PURE (binsli_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI_UQI),
16558 MSA_BUILTIN_PURE (binsli_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI_UQI),
16559 MSA_BUILTIN_PURE (binsr_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
16560 MSA_BUILTIN_PURE (binsr_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI_UV8HI),
16561 MSA_BUILTIN_PURE (binsr_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI_UV4SI),
16562 MSA_BUILTIN_PURE (binsr_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI_UV2DI),
16563 MSA_BUILTIN_PURE (binsri_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
16564 MSA_BUILTIN_PURE (binsri_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI_UQI),
16565 MSA_BUILTIN_PURE (binsri_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI_UQI),
16566 MSA_BUILTIN_PURE (binsri_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI_UQI),
16567 MSA_BUILTIN_PURE (addv_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16568 MSA_BUILTIN_PURE (addv_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16569 MSA_BUILTIN_PURE (addv_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16570 MSA_BUILTIN_PURE (addv_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16571 MSA_BUILTIN_PURE (addvi_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16572 MSA_BUILTIN_PURE (addvi_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16573 MSA_BUILTIN_PURE (addvi_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16574 MSA_BUILTIN_PURE (addvi_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16575 MSA_BUILTIN_PURE (subv_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16576 MSA_BUILTIN_PURE (subv_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16577 MSA_BUILTIN_PURE (subv_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16578 MSA_BUILTIN_PURE (subv_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16579 MSA_BUILTIN_PURE (subvi_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16580 MSA_BUILTIN_PURE (subvi_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16581 MSA_BUILTIN_PURE (subvi_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16582 MSA_BUILTIN_PURE (subvi_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16583 MSA_BUILTIN_PURE (max_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16584 MSA_BUILTIN_PURE (max_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16585 MSA_BUILTIN_PURE (max_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16586 MSA_BUILTIN_PURE (max_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16587 MSA_BUILTIN_PURE (maxi_s_b, MIPS_V16QI_FTYPE_V16QI_QI),
16588 MSA_BUILTIN_PURE (maxi_s_h, MIPS_V8HI_FTYPE_V8HI_QI),
16589 MSA_BUILTIN_PURE (maxi_s_w, MIPS_V4SI_FTYPE_V4SI_QI),
16590 MSA_BUILTIN_PURE (maxi_s_d, MIPS_V2DI_FTYPE_V2DI_QI),
16591 MSA_BUILTIN_PURE (max_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16592 MSA_BUILTIN_PURE (max_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16593 MSA_BUILTIN_PURE (max_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16594 MSA_BUILTIN_PURE (max_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16595 MSA_BUILTIN_PURE (maxi_u_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16596 MSA_BUILTIN_PURE (maxi_u_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
16597 MSA_BUILTIN_PURE (maxi_u_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
16598 MSA_BUILTIN_PURE (maxi_u_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
16599 MSA_BUILTIN_PURE (min_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16600 MSA_BUILTIN_PURE (min_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16601 MSA_BUILTIN_PURE (min_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16602 MSA_BUILTIN_PURE (min_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16603 MSA_BUILTIN_PURE (mini_s_b, MIPS_V16QI_FTYPE_V16QI_QI),
16604 MSA_BUILTIN_PURE (mini_s_h, MIPS_V8HI_FTYPE_V8HI_QI),
16605 MSA_BUILTIN_PURE (mini_s_w, MIPS_V4SI_FTYPE_V4SI_QI),
16606 MSA_BUILTIN_PURE (mini_s_d, MIPS_V2DI_FTYPE_V2DI_QI),
16607 MSA_BUILTIN_PURE (min_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16608 MSA_BUILTIN_PURE (min_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16609 MSA_BUILTIN_PURE (min_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16610 MSA_BUILTIN_PURE (min_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16611 MSA_BUILTIN_PURE (mini_u_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16612 MSA_BUILTIN_PURE (mini_u_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
16613 MSA_BUILTIN_PURE (mini_u_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
16614 MSA_BUILTIN_PURE (mini_u_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
16615 MSA_BUILTIN_PURE (max_a_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16616 MSA_BUILTIN_PURE (max_a_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16617 MSA_BUILTIN_PURE (max_a_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16618 MSA_BUILTIN_PURE (max_a_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16619 MSA_BUILTIN_PURE (min_a_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16620 MSA_BUILTIN_PURE (min_a_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16621 MSA_BUILTIN_PURE (min_a_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16622 MSA_BUILTIN_PURE (min_a_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16623 MSA_BUILTIN (ceq_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16624 MSA_BUILTIN (ceq_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16625 MSA_BUILTIN (ceq_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16626 MSA_BUILTIN (ceq_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16627 MSA_BUILTIN (ceqi_b, MIPS_V16QI_FTYPE_V16QI_QI),
16628 MSA_BUILTIN (ceqi_h, MIPS_V8HI_FTYPE_V8HI_QI),
16629 MSA_BUILTIN (ceqi_w, MIPS_V4SI_FTYPE_V4SI_QI),
16630 MSA_BUILTIN (ceqi_d, MIPS_V2DI_FTYPE_V2DI_QI),
16631 MSA_BUILTIN (clt_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16632 MSA_BUILTIN (clt_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16633 MSA_BUILTIN (clt_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16634 MSA_BUILTIN (clt_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16635 MSA_BUILTIN (clti_s_b, MIPS_V16QI_FTYPE_V16QI_QI),
16636 MSA_BUILTIN (clti_s_h, MIPS_V8HI_FTYPE_V8HI_QI),
16637 MSA_BUILTIN (clti_s_w, MIPS_V4SI_FTYPE_V4SI_QI),
16638 MSA_BUILTIN (clti_s_d, MIPS_V2DI_FTYPE_V2DI_QI),
16639 MSA_BUILTIN (clt_u_b, MIPS_V16QI_FTYPE_UV16QI_UV16QI),
16640 MSA_BUILTIN (clt_u_h, MIPS_V8HI_FTYPE_UV8HI_UV8HI),
16641 MSA_BUILTIN (clt_u_w, MIPS_V4SI_FTYPE_UV4SI_UV4SI),
16642 MSA_BUILTIN (clt_u_d, MIPS_V2DI_FTYPE_UV2DI_UV2DI),
16643 MSA_BUILTIN (clti_u_b, MIPS_V16QI_FTYPE_UV16QI_UQI),
16644 MSA_BUILTIN (clti_u_h, MIPS_V8HI_FTYPE_UV8HI_UQI),
16645 MSA_BUILTIN (clti_u_w, MIPS_V4SI_FTYPE_UV4SI_UQI),
16646 MSA_BUILTIN (clti_u_d, MIPS_V2DI_FTYPE_UV2DI_UQI),
16647 MSA_BUILTIN (cle_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16648 MSA_BUILTIN (cle_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16649 MSA_BUILTIN (cle_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16650 MSA_BUILTIN (cle_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16651 MSA_BUILTIN (clei_s_b, MIPS_V16QI_FTYPE_V16QI_QI),
16652 MSA_BUILTIN (clei_s_h, MIPS_V8HI_FTYPE_V8HI_QI),
16653 MSA_BUILTIN (clei_s_w, MIPS_V4SI_FTYPE_V4SI_QI),
16654 MSA_BUILTIN (clei_s_d, MIPS_V2DI_FTYPE_V2DI_QI),
16655 MSA_BUILTIN (cle_u_b, MIPS_V16QI_FTYPE_UV16QI_UV16QI),
16656 MSA_BUILTIN (cle_u_h, MIPS_V8HI_FTYPE_UV8HI_UV8HI),
16657 MSA_BUILTIN (cle_u_w, MIPS_V4SI_FTYPE_UV4SI_UV4SI),
16658 MSA_BUILTIN (cle_u_d, MIPS_V2DI_FTYPE_UV2DI_UV2DI),
16659 MSA_BUILTIN (clei_u_b, MIPS_V16QI_FTYPE_UV16QI_UQI),
16660 MSA_BUILTIN (clei_u_h, MIPS_V8HI_FTYPE_UV8HI_UQI),
16661 MSA_BUILTIN (clei_u_w, MIPS_V4SI_FTYPE_UV4SI_UQI),
16662 MSA_BUILTIN (clei_u_d, MIPS_V2DI_FTYPE_UV2DI_UQI),
16663 MSA_BUILTIN (ld_b, MIPS_V16QI_FTYPE_CVPOINTER_SI),
16664 MSA_BUILTIN (ld_h, MIPS_V8HI_FTYPE_CVPOINTER_SI),
16665 MSA_BUILTIN (ld_w, MIPS_V4SI_FTYPE_CVPOINTER_SI),
16666 MSA_BUILTIN (ld_d, MIPS_V2DI_FTYPE_CVPOINTER_SI),
16667 MSA_NO_TARGET_BUILTIN (st_b, MIPS_VOID_FTYPE_V16QI_CVPOINTER_SI),
16668 MSA_NO_TARGET_BUILTIN (st_h, MIPS_VOID_FTYPE_V8HI_CVPOINTER_SI),
16669 MSA_NO_TARGET_BUILTIN (st_w, MIPS_VOID_FTYPE_V4SI_CVPOINTER_SI),
16670 MSA_NO_TARGET_BUILTIN (st_d, MIPS_VOID_FTYPE_V2DI_CVPOINTER_SI),
16671 MSA_BUILTIN_PURE (sat_s_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16672 MSA_BUILTIN_PURE (sat_s_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16673 MSA_BUILTIN_PURE (sat_s_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16674 MSA_BUILTIN_PURE (sat_s_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16675 MSA_BUILTIN_PURE (sat_u_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16676 MSA_BUILTIN_PURE (sat_u_h, MIPS_UV8HI_FTYPE_UV8HI_UQI),
16677 MSA_BUILTIN_PURE (sat_u_w, MIPS_UV4SI_FTYPE_UV4SI_UQI),
16678 MSA_BUILTIN_PURE (sat_u_d, MIPS_UV2DI_FTYPE_UV2DI_UQI),
16679 MSA_BUILTIN_PURE (add_a_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16680 MSA_BUILTIN_PURE (add_a_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16681 MSA_BUILTIN_PURE (add_a_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16682 MSA_BUILTIN_PURE (add_a_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16683 MSA_BUILTIN_PURE (adds_a_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16684 MSA_BUILTIN_PURE (adds_a_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16685 MSA_BUILTIN_PURE (adds_a_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16686 MSA_BUILTIN_PURE (adds_a_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16687 MSA_BUILTIN_PURE (adds_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16688 MSA_BUILTIN_PURE (adds_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16689 MSA_BUILTIN_PURE (adds_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16690 MSA_BUILTIN_PURE (adds_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16691 MSA_BUILTIN_PURE (adds_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16692 MSA_BUILTIN_PURE (adds_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16693 MSA_BUILTIN_PURE (adds_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16694 MSA_BUILTIN_PURE (adds_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16695 MSA_BUILTIN_PURE (ave_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16696 MSA_BUILTIN_PURE (ave_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16697 MSA_BUILTIN_PURE (ave_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16698 MSA_BUILTIN_PURE (ave_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16699 MSA_BUILTIN_PURE (ave_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16700 MSA_BUILTIN_PURE (ave_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16701 MSA_BUILTIN_PURE (ave_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16702 MSA_BUILTIN_PURE (ave_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16703 MSA_BUILTIN_PURE (aver_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16704 MSA_BUILTIN_PURE (aver_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16705 MSA_BUILTIN_PURE (aver_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16706 MSA_BUILTIN_PURE (aver_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16707 MSA_BUILTIN_PURE (aver_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16708 MSA_BUILTIN_PURE (aver_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16709 MSA_BUILTIN_PURE (aver_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16710 MSA_BUILTIN_PURE (aver_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16711 MSA_BUILTIN_PURE (subs_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16712 MSA_BUILTIN_PURE (subs_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16713 MSA_BUILTIN_PURE (subs_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16714 MSA_BUILTIN_PURE (subs_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16715 MSA_BUILTIN_PURE (subs_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16716 MSA_BUILTIN_PURE (subs_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16717 MSA_BUILTIN_PURE (subs_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16718 MSA_BUILTIN_PURE (subs_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16719 MSA_BUILTIN_PURE (subsuu_s_b, MIPS_V16QI_FTYPE_UV16QI_UV16QI),
16720 MSA_BUILTIN_PURE (subsuu_s_h, MIPS_V8HI_FTYPE_UV8HI_UV8HI),
16721 MSA_BUILTIN_PURE (subsuu_s_w, MIPS_V4SI_FTYPE_UV4SI_UV4SI),
16722 MSA_BUILTIN_PURE (subsuu_s_d, MIPS_V2DI_FTYPE_UV2DI_UV2DI),
16723 MSA_BUILTIN_PURE (subsus_u_b, MIPS_UV16QI_FTYPE_UV16QI_V16QI),
16724 MSA_BUILTIN_PURE (subsus_u_h, MIPS_UV8HI_FTYPE_UV8HI_V8HI),
16725 MSA_BUILTIN_PURE (subsus_u_w, MIPS_UV4SI_FTYPE_UV4SI_V4SI),
16726 MSA_BUILTIN_PURE (subsus_u_d, MIPS_UV2DI_FTYPE_UV2DI_V2DI),
16727 MSA_BUILTIN_PURE (asub_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16728 MSA_BUILTIN_PURE (asub_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16729 MSA_BUILTIN_PURE (asub_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16730 MSA_BUILTIN_PURE (asub_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16731 MSA_BUILTIN_PURE (asub_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16732 MSA_BUILTIN_PURE (asub_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16733 MSA_BUILTIN_PURE (asub_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16734 MSA_BUILTIN_PURE (asub_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16735 MSA_BUILTIN_PURE (mulv_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16736 MSA_BUILTIN_PURE (mulv_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16737 MSA_BUILTIN_PURE (mulv_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16738 MSA_BUILTIN_PURE (mulv_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16739 MSA_BUILTIN_PURE (maddv_b, MIPS_V16QI_FTYPE_V16QI_V16QI_V16QI),
16740 MSA_BUILTIN_PURE (maddv_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16741 MSA_BUILTIN_PURE (maddv_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16742 MSA_BUILTIN_PURE (maddv_d, MIPS_V2DI_FTYPE_V2DI_V2DI_V2DI),
16743 MSA_BUILTIN_PURE (msubv_b, MIPS_V16QI_FTYPE_V16QI_V16QI_V16QI),
16744 MSA_BUILTIN_PURE (msubv_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16745 MSA_BUILTIN_PURE (msubv_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16746 MSA_BUILTIN_PURE (msubv_d, MIPS_V2DI_FTYPE_V2DI_V2DI_V2DI),
16747 MSA_BUILTIN_PURE (div_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16748 MSA_BUILTIN_PURE (div_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16749 MSA_BUILTIN_PURE (div_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16750 MSA_BUILTIN_PURE (div_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16751 MSA_BUILTIN_PURE (div_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16752 MSA_BUILTIN_PURE (div_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16753 MSA_BUILTIN_PURE (div_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16754 MSA_BUILTIN_PURE (div_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16755 MSA_BUILTIN_PURE (hadd_s_h, MIPS_V8HI_FTYPE_V16QI_V16QI),
16756 MSA_BUILTIN_PURE (hadd_s_w, MIPS_V4SI_FTYPE_V8HI_V8HI),
16757 MSA_BUILTIN_PURE (hadd_s_d, MIPS_V2DI_FTYPE_V4SI_V4SI),
16758 MSA_BUILTIN_PURE (hadd_u_h, MIPS_UV8HI_FTYPE_UV16QI_UV16QI),
16759 MSA_BUILTIN_PURE (hadd_u_w, MIPS_UV4SI_FTYPE_UV8HI_UV8HI),
16760 MSA_BUILTIN_PURE (hadd_u_d, MIPS_UV2DI_FTYPE_UV4SI_UV4SI),
16761 MSA_BUILTIN_PURE (hsub_s_h, MIPS_V8HI_FTYPE_V16QI_V16QI),
16762 MSA_BUILTIN_PURE (hsub_s_w, MIPS_V4SI_FTYPE_V8HI_V8HI),
16763 MSA_BUILTIN_PURE (hsub_s_d, MIPS_V2DI_FTYPE_V4SI_V4SI),
16764 MSA_BUILTIN_PURE (hsub_u_h, MIPS_V8HI_FTYPE_UV16QI_UV16QI),
16765 MSA_BUILTIN_PURE (hsub_u_w, MIPS_V4SI_FTYPE_UV8HI_UV8HI),
16766 MSA_BUILTIN_PURE (hsub_u_d, MIPS_V2DI_FTYPE_UV4SI_UV4SI),
16767 MSA_BUILTIN_PURE (mod_s_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16768 MSA_BUILTIN_PURE (mod_s_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16769 MSA_BUILTIN_PURE (mod_s_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16770 MSA_BUILTIN_PURE (mod_s_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16771 MSA_BUILTIN_PURE (mod_u_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16772 MSA_BUILTIN_PURE (mod_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV8HI),
16773 MSA_BUILTIN_PURE (mod_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV4SI),
16774 MSA_BUILTIN_PURE (mod_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV2DI),
16775 MSA_BUILTIN_PURE (dotp_s_h, MIPS_V8HI_FTYPE_V16QI_V16QI),
16776 MSA_BUILTIN_PURE (dotp_s_w, MIPS_V4SI_FTYPE_V8HI_V8HI),
16777 MSA_BUILTIN_PURE (dotp_s_d, MIPS_V2DI_FTYPE_V4SI_V4SI),
16778 MSA_BUILTIN_PURE (dotp_u_h, MIPS_UV8HI_FTYPE_UV16QI_UV16QI),
16779 MSA_BUILTIN_PURE (dotp_u_w, MIPS_UV4SI_FTYPE_UV8HI_UV8HI),
16780 MSA_BUILTIN_PURE (dotp_u_d, MIPS_UV2DI_FTYPE_UV4SI_UV4SI),
16781 MSA_BUILTIN_PURE (dpadd_s_h, MIPS_V8HI_FTYPE_V8HI_V16QI_V16QI),
16782 MSA_BUILTIN_PURE (dpadd_s_w, MIPS_V4SI_FTYPE_V4SI_V8HI_V8HI),
16783 MSA_BUILTIN_PURE (dpadd_s_d, MIPS_V2DI_FTYPE_V2DI_V4SI_V4SI),
16784 MSA_BUILTIN_PURE (dpadd_u_h, MIPS_UV8HI_FTYPE_UV8HI_UV16QI_UV16QI),
16785 MSA_BUILTIN_PURE (dpadd_u_w, MIPS_UV4SI_FTYPE_UV4SI_UV8HI_UV8HI),
16786 MSA_BUILTIN_PURE (dpadd_u_d, MIPS_UV2DI_FTYPE_UV2DI_UV4SI_UV4SI),
16787 MSA_BUILTIN_PURE (dpsub_s_h, MIPS_V8HI_FTYPE_V8HI_V16QI_V16QI),
16788 MSA_BUILTIN_PURE (dpsub_s_w, MIPS_V4SI_FTYPE_V4SI_V8HI_V8HI),
16789 MSA_BUILTIN_PURE (dpsub_s_d, MIPS_V2DI_FTYPE_V2DI_V4SI_V4SI),
16790 MSA_BUILTIN_PURE (dpsub_u_h, MIPS_V8HI_FTYPE_V8HI_UV16QI_UV16QI),
16791 MSA_BUILTIN_PURE (dpsub_u_w, MIPS_V4SI_FTYPE_V4SI_UV8HI_UV8HI),
16792 MSA_BUILTIN_PURE (dpsub_u_d, MIPS_V2DI_FTYPE_V2DI_UV4SI_UV4SI),
16793 MSA_BUILTIN_PURE (sld_b, MIPS_V16QI_FTYPE_V16QI_V16QI_SI),
16794 MSA_BUILTIN_PURE (sld_h, MIPS_V8HI_FTYPE_V8HI_V8HI_SI),
16795 MSA_BUILTIN_PURE (sld_w, MIPS_V4SI_FTYPE_V4SI_V4SI_SI),
16796 MSA_BUILTIN_PURE (sld_d, MIPS_V2DI_FTYPE_V2DI_V2DI_SI),
16797 MSA_BUILTIN_PURE (sldi_b, MIPS_V16QI_FTYPE_V16QI_V16QI_UQI),
16798 MSA_BUILTIN_PURE (sldi_h, MIPS_V8HI_FTYPE_V8HI_V8HI_UQI),
16799 MSA_BUILTIN_PURE (sldi_w, MIPS_V4SI_FTYPE_V4SI_V4SI_UQI),
16800 MSA_BUILTIN_PURE (sldi_d, MIPS_V2DI_FTYPE_V2DI_V2DI_UQI),
16801 MSA_BUILTIN_PURE (splat_b, MIPS_V16QI_FTYPE_V16QI_SI),
16802 MSA_BUILTIN_PURE (splat_h, MIPS_V8HI_FTYPE_V8HI_SI),
16803 MSA_BUILTIN_PURE (splat_w, MIPS_V4SI_FTYPE_V4SI_SI),
16804 MSA_BUILTIN_PURE (splat_d, MIPS_V2DI_FTYPE_V2DI_SI),
16805 MSA_BUILTIN_PURE (splati_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16806 MSA_BUILTIN_PURE (splati_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16807 MSA_BUILTIN_PURE (splati_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16808 MSA_BUILTIN_PURE (splati_d, MIPS_V2DI_FTYPE_V2DI_UQI),
16809 MSA_BUILTIN_PURE (pckev_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16810 MSA_BUILTIN_PURE (pckev_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16811 MSA_BUILTIN_PURE (pckev_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16812 MSA_BUILTIN_PURE (pckev_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16813 MSA_BUILTIN_PURE (pckod_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16814 MSA_BUILTIN_PURE (pckod_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16815 MSA_BUILTIN_PURE (pckod_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16816 MSA_BUILTIN_PURE (pckod_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16817 MSA_BUILTIN_PURE (ilvl_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16818 MSA_BUILTIN_PURE (ilvl_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16819 MSA_BUILTIN_PURE (ilvl_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16820 MSA_BUILTIN_PURE (ilvl_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16821 MSA_BUILTIN_PURE (ilvr_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16822 MSA_BUILTIN_PURE (ilvr_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16823 MSA_BUILTIN_PURE (ilvr_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16824 MSA_BUILTIN_PURE (ilvr_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16825 MSA_BUILTIN_PURE (ilvev_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16826 MSA_BUILTIN_PURE (ilvev_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16827 MSA_BUILTIN_PURE (ilvev_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16828 MSA_BUILTIN_PURE (ilvev_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16829 MSA_BUILTIN_PURE (ilvod_b, MIPS_V16QI_FTYPE_V16QI_V16QI),
16830 MSA_BUILTIN_PURE (ilvod_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16831 MSA_BUILTIN_PURE (ilvod_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16832 MSA_BUILTIN_PURE (ilvod_d, MIPS_V2DI_FTYPE_V2DI_V2DI),
16833 MSA_BUILTIN_PURE (vshf_b, MIPS_V16QI_FTYPE_V16QI_V16QI_V16QI),
16834 MSA_BUILTIN_PURE (vshf_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16835 MSA_BUILTIN_PURE (vshf_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16836 MSA_BUILTIN_PURE (vshf_d, MIPS_V2DI_FTYPE_V2DI_V2DI_V2DI),
16837 MSA_BUILTIN_PURE (and_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16838 MSA_BUILTIN_PURE (andi_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16839 MSA_BUILTIN_PURE (or_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16840 MSA_BUILTIN_PURE (ori_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16841 MSA_BUILTIN_PURE (nor_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16842 MSA_BUILTIN_PURE (nori_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16843 MSA_BUILTIN_PURE (xor_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI),
16844 MSA_BUILTIN_PURE (xori_b, MIPS_UV16QI_FTYPE_UV16QI_UQI),
16845 MSA_BUILTIN_PURE (bmnz_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
16846 MSA_BUILTIN_PURE (bmnzi_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
16847 MSA_BUILTIN_PURE (bmz_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
16848 MSA_BUILTIN_PURE (bmzi_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
16849 MSA_BUILTIN_PURE (bsel_v, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UV16QI),
16850 MSA_BUILTIN_PURE (bseli_b, MIPS_UV16QI_FTYPE_UV16QI_UV16QI_UQI),
16851 MSA_BUILTIN_PURE (shf_b, MIPS_V16QI_FTYPE_V16QI_UQI),
16852 MSA_BUILTIN_PURE (shf_h, MIPS_V8HI_FTYPE_V8HI_UQI),
16853 MSA_BUILTIN_PURE (shf_w, MIPS_V4SI_FTYPE_V4SI_UQI),
16854 MSA_BUILTIN_TEST_BRANCH (bnz_v, MIPS_SI_FTYPE_UV16QI),
16855 MSA_BUILTIN_TEST_BRANCH (bz_v, MIPS_SI_FTYPE_UV16QI),
16856 MSA_BUILTIN_PURE (fill_b, MIPS_V16QI_FTYPE_SI),
16857 MSA_BUILTIN_PURE (fill_h, MIPS_V8HI_FTYPE_SI),
16858 MSA_BUILTIN_PURE (fill_w, MIPS_V4SI_FTYPE_SI),
16859 MSA_BUILTIN_PURE (fill_d, MIPS_V2DI_FTYPE_DI),
16860 MSA_BUILTIN_PURE (pcnt_b, MIPS_V16QI_FTYPE_V16QI),
16861 MSA_BUILTIN_PURE (pcnt_h, MIPS_V8HI_FTYPE_V8HI),
16862 MSA_BUILTIN_PURE (pcnt_w, MIPS_V4SI_FTYPE_V4SI),
16863 MSA_BUILTIN_PURE (pcnt_d, MIPS_V2DI_FTYPE_V2DI),
16864 MSA_BUILTIN_PURE (nloc_b, MIPS_V16QI_FTYPE_V16QI),
16865 MSA_BUILTIN_PURE (nloc_h, MIPS_V8HI_FTYPE_V8HI),
16866 MSA_BUILTIN_PURE (nloc_w, MIPS_V4SI_FTYPE_V4SI),
16867 MSA_BUILTIN_PURE (nloc_d, MIPS_V2DI_FTYPE_V2DI),
16868 MSA_BUILTIN_PURE (nlzc_b, MIPS_V16QI_FTYPE_V16QI),
16869 MSA_BUILTIN_PURE (nlzc_h, MIPS_V8HI_FTYPE_V8HI),
16870 MSA_BUILTIN_PURE (nlzc_w, MIPS_V4SI_FTYPE_V4SI),
16871 MSA_BUILTIN_PURE (nlzc_d, MIPS_V2DI_FTYPE_V2DI),
16872 MSA_BUILTIN_PURE (copy_s_b, MIPS_SI_FTYPE_V16QI_UQI),
16873 MSA_BUILTIN_PURE (copy_s_h, MIPS_SI_FTYPE_V8HI_UQI),
16874 MSA_BUILTIN_PURE (copy_s_w, MIPS_SI_FTYPE_V4SI_UQI),
16875 MSA_BUILTIN_PURE (copy_s_d, MIPS_DI_FTYPE_V2DI_UQI),
16876 MSA_BUILTIN_PURE (copy_u_b, MIPS_USI_FTYPE_V16QI_UQI),
16877 MSA_BUILTIN_PURE (copy_u_h, MIPS_USI_FTYPE_V8HI_UQI),
16878 MSA_BUILTIN_REMAP (copy_u_w, copy_s_w, MIPS_USI_FTYPE_V4SI_UQI),
16879 MSA_BUILTIN_REMAP (copy_u_d, copy_s_d, MIPS_UDI_FTYPE_V2DI_UQI),
16880 MSA_BUILTIN_PURE (insert_b, MIPS_V16QI_FTYPE_V16QI_UQI_SI),
16881 MSA_BUILTIN_PURE (insert_h, MIPS_V8HI_FTYPE_V8HI_UQI_SI),
16882 MSA_BUILTIN_PURE (insert_w, MIPS_V4SI_FTYPE_V4SI_UQI_SI),
16883 MSA_BUILTIN_PURE (insert_d, MIPS_V2DI_FTYPE_V2DI_UQI_DI),
16884 MSA_BUILTIN_PURE (insve_b, MIPS_V16QI_FTYPE_V16QI_UQI_V16QI),
16885 MSA_BUILTIN_PURE (insve_h, MIPS_V8HI_FTYPE_V8HI_UQI_V8HI),
16886 MSA_BUILTIN_PURE (insve_w, MIPS_V4SI_FTYPE_V4SI_UQI_V4SI),
16887 MSA_BUILTIN_PURE (insve_d, MIPS_V2DI_FTYPE_V2DI_UQI_V2DI),
16888 MSA_BUILTIN_TEST_BRANCH (bnz_b, MIPS_SI_FTYPE_UV16QI),
16889 MSA_BUILTIN_TEST_BRANCH (bnz_h, MIPS_SI_FTYPE_UV8HI),
16890 MSA_BUILTIN_TEST_BRANCH (bnz_w, MIPS_SI_FTYPE_UV4SI),
16891 MSA_BUILTIN_TEST_BRANCH (bnz_d, MIPS_SI_FTYPE_UV2DI),
16892 MSA_BUILTIN_TEST_BRANCH (bz_b, MIPS_SI_FTYPE_UV16QI),
16893 MSA_BUILTIN_TEST_BRANCH (bz_h, MIPS_SI_FTYPE_UV8HI),
16894 MSA_BUILTIN_TEST_BRANCH (bz_w, MIPS_SI_FTYPE_UV4SI),
16895 MSA_BUILTIN_TEST_BRANCH (bz_d, MIPS_SI_FTYPE_UV2DI),
16896 MSA_BUILTIN_PURE (ldi_b, MIPS_V16QI_FTYPE_HI),
16897 MSA_BUILTIN_PURE (ldi_h, MIPS_V8HI_FTYPE_HI),
16898 MSA_BUILTIN_PURE (ldi_w, MIPS_V4SI_FTYPE_HI),
16899 MSA_BUILTIN_PURE (ldi_d, MIPS_V2DI_FTYPE_HI),
16900 MSA_BUILTIN_PURE (fcaf_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16901 MSA_BUILTIN_PURE (fcaf_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16902 MSA_BUILTIN_PURE (fcor_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16903 MSA_BUILTIN_PURE (fcor_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16904 MSA_BUILTIN_PURE (fcun_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16905 MSA_BUILTIN_PURE (fcun_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16906 MSA_BUILTIN_PURE (fcune_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16907 MSA_BUILTIN_PURE (fcune_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16908 MSA_BUILTIN_PURE (fcueq_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16909 MSA_BUILTIN_PURE (fcueq_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16910 MSA_BUILTIN_PURE (fceq_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16911 MSA_BUILTIN_PURE (fceq_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16912 MSA_BUILTIN_PURE (fcne_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16913 MSA_BUILTIN_PURE (fcne_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16914 MSA_BUILTIN_PURE (fclt_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16915 MSA_BUILTIN_PURE (fclt_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16916 MSA_BUILTIN_PURE (fcult_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16917 MSA_BUILTIN_PURE (fcult_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16918 MSA_BUILTIN_PURE (fcle_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16919 MSA_BUILTIN_PURE (fcle_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16920 MSA_BUILTIN_PURE (fcule_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16921 MSA_BUILTIN_PURE (fcule_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16922 MSA_BUILTIN_PURE (fsaf_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16923 MSA_BUILTIN_PURE (fsaf_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16924 MSA_BUILTIN_PURE (fsor_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16925 MSA_BUILTIN_PURE (fsor_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16926 MSA_BUILTIN_PURE (fsun_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16927 MSA_BUILTIN_PURE (fsun_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16928 MSA_BUILTIN_PURE (fsune_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16929 MSA_BUILTIN_PURE (fsune_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16930 MSA_BUILTIN_PURE (fsueq_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16931 MSA_BUILTIN_PURE (fsueq_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16932 MSA_BUILTIN_PURE (fseq_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16933 MSA_BUILTIN_PURE (fseq_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16934 MSA_BUILTIN_PURE (fsne_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16935 MSA_BUILTIN_PURE (fsne_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16936 MSA_BUILTIN_PURE (fslt_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16937 MSA_BUILTIN_PURE (fslt_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16938 MSA_BUILTIN_PURE (fsult_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16939 MSA_BUILTIN_PURE (fsult_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16940 MSA_BUILTIN_PURE (fsle_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16941 MSA_BUILTIN_PURE (fsle_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16942 MSA_BUILTIN_PURE (fsule_w, MIPS_V4SI_FTYPE_V4SF_V4SF),
16943 MSA_BUILTIN_PURE (fsule_d, MIPS_V2DI_FTYPE_V2DF_V2DF),
16944 MSA_BUILTIN_PURE (fadd_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16945 MSA_BUILTIN_PURE (fadd_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16946 MSA_BUILTIN_PURE (fsub_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16947 MSA_BUILTIN_PURE (fsub_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16948 MSA_BUILTIN_PURE (fmul_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16949 MSA_BUILTIN_PURE (fmul_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16950 MSA_BUILTIN_PURE (fdiv_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16951 MSA_BUILTIN_PURE (fdiv_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16952 MSA_BUILTIN_PURE (fmadd_w, MIPS_V4SF_FTYPE_V4SF_V4SF_V4SF),
16953 MSA_BUILTIN_PURE (fmadd_d, MIPS_V2DF_FTYPE_V2DF_V2DF_V2DF),
16954 MSA_BUILTIN_PURE (fmsub_w, MIPS_V4SF_FTYPE_V4SF_V4SF_V4SF),
16955 MSA_BUILTIN_PURE (fmsub_d, MIPS_V2DF_FTYPE_V2DF_V2DF_V2DF),
16956 MSA_BUILTIN_PURE (fexp2_w, MIPS_V4SF_FTYPE_V4SF_V4SI),
16957 MSA_BUILTIN_PURE (fexp2_d, MIPS_V2DF_FTYPE_V2DF_V2DI),
16958 MSA_BUILTIN_PURE (fexdo_h, MIPS_V8HI_FTYPE_V4SF_V4SF),
16959 MSA_BUILTIN_PURE (fexdo_w, MIPS_V4SF_FTYPE_V2DF_V2DF),
16960 MSA_BUILTIN_PURE (ftq_h, MIPS_V8HI_FTYPE_V4SF_V4SF),
16961 MSA_BUILTIN_PURE (ftq_w, MIPS_V4SI_FTYPE_V2DF_V2DF),
16962 MSA_BUILTIN_PURE (fmin_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16963 MSA_BUILTIN_PURE (fmin_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16964 MSA_BUILTIN_PURE (fmin_a_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16965 MSA_BUILTIN_PURE (fmin_a_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16966 MSA_BUILTIN_PURE (fmax_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16967 MSA_BUILTIN_PURE (fmax_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16968 MSA_BUILTIN_PURE (fmax_a_w, MIPS_V4SF_FTYPE_V4SF_V4SF),
16969 MSA_BUILTIN_PURE (fmax_a_d, MIPS_V2DF_FTYPE_V2DF_V2DF),
16970 MSA_BUILTIN_PURE (mul_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16971 MSA_BUILTIN_PURE (mul_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16972 MSA_BUILTIN_PURE (mulr_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI),
16973 MSA_BUILTIN_PURE (mulr_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI),
16974 MSA_BUILTIN_PURE (madd_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16975 MSA_BUILTIN_PURE (madd_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16976 MSA_BUILTIN_PURE (maddr_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16977 MSA_BUILTIN_PURE (maddr_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16978 MSA_BUILTIN_PURE (msub_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16979 MSA_BUILTIN_PURE (msub_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16980 MSA_BUILTIN_PURE (msubr_q_h, MIPS_V8HI_FTYPE_V8HI_V8HI_V8HI),
16981 MSA_BUILTIN_PURE (msubr_q_w, MIPS_V4SI_FTYPE_V4SI_V4SI_V4SI),
16982 MSA_BUILTIN_PURE (fclass_w, MIPS_V4SI_FTYPE_V4SF),
16983 MSA_BUILTIN_PURE (fclass_d, MIPS_V2DI_FTYPE_V2DF),
16984 MSA_BUILTIN_PURE (fsqrt_w, MIPS_V4SF_FTYPE_V4SF),
16985 MSA_BUILTIN_PURE (fsqrt_d, MIPS_V2DF_FTYPE_V2DF),
16986 MSA_BUILTIN_PURE (frcp_w, MIPS_V4SF_FTYPE_V4SF),
16987 MSA_BUILTIN_PURE (frcp_d, MIPS_V2DF_FTYPE_V2DF),
16988 MSA_BUILTIN_PURE (frint_w, MIPS_V4SF_FTYPE_V4SF),
16989 MSA_BUILTIN_PURE (frint_d, MIPS_V2DF_FTYPE_V2DF),
16990 MSA_BUILTIN_PURE (frsqrt_w, MIPS_V4SF_FTYPE_V4SF),
16991 MSA_BUILTIN_PURE (frsqrt_d, MIPS_V2DF_FTYPE_V2DF),
16992 MSA_BUILTIN_PURE (flog2_w, MIPS_V4SF_FTYPE_V4SF),
16993 MSA_BUILTIN_PURE (flog2_d, MIPS_V2DF_FTYPE_V2DF),
16994 MSA_BUILTIN_PURE (fexupl_w, MIPS_V4SF_FTYPE_V8HI),
16995 MSA_BUILTIN_PURE (fexupl_d, MIPS_V2DF_FTYPE_V4SF),
16996 MSA_BUILTIN_PURE (fexupr_w, MIPS_V4SF_FTYPE_V8HI),
16997 MSA_BUILTIN_PURE (fexupr_d, MIPS_V2DF_FTYPE_V4SF),
16998 MSA_BUILTIN_PURE (ffql_w, MIPS_V4SF_FTYPE_V8HI),
16999 MSA_BUILTIN_PURE (ffql_d, MIPS_V2DF_FTYPE_V4SI),
17000 MSA_BUILTIN_PURE (ffqr_w, MIPS_V4SF_FTYPE_V8HI),
17001 MSA_BUILTIN_PURE (ffqr_d, MIPS_V2DF_FTYPE_V4SI),
17002 MSA_BUILTIN_PURE (ftint_s_w, MIPS_V4SI_FTYPE_V4SF),
17003 MSA_BUILTIN_PURE (ftint_s_d, MIPS_V2DI_FTYPE_V2DF),
17004 MSA_BUILTIN_PURE (ftint_u_w, MIPS_UV4SI_FTYPE_V4SF),
17005 MSA_BUILTIN_PURE (ftint_u_d, MIPS_UV2DI_FTYPE_V2DF),
17006 MSA_BUILTIN_PURE (ftrunc_s_w, MIPS_V4SI_FTYPE_V4SF),
17007 MSA_BUILTIN_PURE (ftrunc_s_d, MIPS_V2DI_FTYPE_V2DF),
17008 MSA_BUILTIN_PURE (ftrunc_u_w, MIPS_UV4SI_FTYPE_V4SF),
17009 MSA_BUILTIN_PURE (ftrunc_u_d, MIPS_UV2DI_FTYPE_V2DF),
17010 MSA_BUILTIN_PURE (ffint_s_w, MIPS_V4SF_FTYPE_V4SI),
17011 MSA_BUILTIN_PURE (ffint_s_d, MIPS_V2DF_FTYPE_V2DI),
17012 MSA_BUILTIN_PURE (ffint_u_w, MIPS_V4SF_FTYPE_UV4SI),
17013 MSA_BUILTIN_PURE (ffint_u_d, MIPS_V2DF_FTYPE_UV2DI),
17014 MSA_NO_TARGET_BUILTIN (ctcmsa, MIPS_VOID_FTYPE_UQI_SI),
17015 MSA_BUILTIN_PURE (cfcmsa, MIPS_SI_FTYPE_UQI),
17016 MSA_BUILTIN_PURE (move_v, MIPS_V16QI_FTYPE_V16QI),
17017
17018 /* Built-in functions for MIPSr6. */
17019 MIPSR6_BUILTIN_PURE (min_a_s, MIPS_SF_FTYPE_SF_SF),
17020 MIPSR6_BUILTIN_PURE (min_a_d, MIPS_DF_FTYPE_DF_DF),
17021 MIPSR6_BUILTIN_PURE (max_a_s, MIPS_SF_FTYPE_SF_SF),
17022 MIPSR6_BUILTIN_PURE (max_a_d, MIPS_DF_FTYPE_DF_DF),
17023 MIPSR6_BUILTIN_PURE (class_s, MIPS_SF_FTYPE_SF),
17024 MIPSR6_BUILTIN_PURE (class_d, MIPS_DF_FTYPE_DF),
17025 };
17026
17027 /* Index I is the function declaration for mips_builtins[I], or null if the
17028 function isn't defined on this target. */
17029 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
17030 /* Get the index I of the function declaration for mips_builtin_decls[I]
17031 using the instruction code or return null if not defined for the target. */
17032 static GTY(()) int mips_get_builtin_decl_index[NUM_INSN_CODES];
17033
17034 /* MODE is a vector mode whose elements have type TYPE. Return the type
17035 of the vector itself. */
17036
17037 static tree
17038 mips_builtin_vector_type (tree type, machine_mode mode)
17039 {
17040 static tree types[2 * (int) MAX_MACHINE_MODE];
17041 int mode_index;
17042
17043 mode_index = (int) mode;
17044
17045 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
17046 mode_index += MAX_MACHINE_MODE;
17047
17048 if (types[mode_index] == NULL_TREE)
17049 types[mode_index] = build_vector_type_for_mode (type, mode);
17050 return types[mode_index];
17051 }
17052
17053 /* Return a type for 'const volatile void *'. */
17054
17055 static tree
17056 mips_build_cvpointer_type (void)
17057 {
17058 static tree cache;
17059
17060 if (cache == NULL_TREE)
17061 cache = build_pointer_type (build_qualified_type
17062 (void_type_node,
17063 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
17064 return cache;
17065 }
17066
17067 /* Source-level argument types. */
17068 #define MIPS_ATYPE_VOID void_type_node
17069 #define MIPS_ATYPE_INT integer_type_node
17070 #define MIPS_ATYPE_POINTER ptr_type_node
17071 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
17072
17073 /* Standard mode-based argument types. */
17074 #define MIPS_ATYPE_QI intQI_type_node
17075 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
17076 #define MIPS_ATYPE_HI intHI_type_node
17077 #define MIPS_ATYPE_SI intSI_type_node
17078 #define MIPS_ATYPE_USI unsigned_intSI_type_node
17079 #define MIPS_ATYPE_DI intDI_type_node
17080 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
17081 #define MIPS_ATYPE_SF float_type_node
17082 #define MIPS_ATYPE_DF double_type_node
17083
17084 /* Vector argument types. */
17085 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
17086 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
17087 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
17088 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
17089 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
17090 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
17091
17092 #define MIPS_ATYPE_V2DI \
17093 mips_builtin_vector_type (long_long_integer_type_node, V2DImode)
17094 #define MIPS_ATYPE_V4SI mips_builtin_vector_type (intSI_type_node, V4SImode)
17095 #define MIPS_ATYPE_V8HI mips_builtin_vector_type (intHI_type_node, V8HImode)
17096 #define MIPS_ATYPE_V16QI mips_builtin_vector_type (intQI_type_node, V16QImode)
17097 #define MIPS_ATYPE_V2DF mips_builtin_vector_type (double_type_node, V2DFmode)
17098 #define MIPS_ATYPE_V4SF mips_builtin_vector_type (float_type_node, V4SFmode)
17099
17100 #define MIPS_ATYPE_UV2DI \
17101 mips_builtin_vector_type (long_long_unsigned_type_node, V2DImode)
17102 #define MIPS_ATYPE_UV4SI \
17103 mips_builtin_vector_type (unsigned_intSI_type_node, V4SImode)
17104 #define MIPS_ATYPE_UV8HI \
17105 mips_builtin_vector_type (unsigned_intHI_type_node, V8HImode)
17106 #define MIPS_ATYPE_UV16QI \
17107 mips_builtin_vector_type (unsigned_intQI_type_node, V16QImode)
17108
17109 #define MIPS_ATYPE_UV2SI \
17110 mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
17111 #define MIPS_ATYPE_UV4HI \
17112 mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
17113 #define MIPS_ATYPE_UV8QI \
17114 mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
17115
17116 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
17117 their associated MIPS_ATYPEs. */
17118 #define MIPS_FTYPE_ATYPES1(A, B) \
17119 MIPS_ATYPE_##A, MIPS_ATYPE_##B
17120
17121 #define MIPS_FTYPE_ATYPES2(A, B, C) \
17122 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
17123
17124 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
17125 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
17126
17127 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
17128 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
17129 MIPS_ATYPE_##E
17130
17131 /* Return the function type associated with function prototype TYPE. */
17132
17133 static tree
17134 mips_build_function_type (enum mips_function_type type)
17135 {
17136 static tree types[(int) MIPS_MAX_FTYPE_MAX];
17137
17138 if (types[(int) type] == NULL_TREE)
17139 switch (type)
17140 {
17141 #define DEF_MIPS_FTYPE(NUM, ARGS) \
17142 case MIPS_FTYPE_NAME##NUM ARGS: \
17143 types[(int) type] \
17144 = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS, \
17145 NULL_TREE); \
17146 break;
17147 #include "config/mips/mips-ftypes.def"
17148 #undef DEF_MIPS_FTYPE
17149 default:
17150 gcc_unreachable ();
17151 }
17152
17153 return types[(int) type];
17154 }
17155
17156 /* Implement TARGET_INIT_BUILTINS. */
17157
17158 static void
17159 mips_init_builtins (void)
17160 {
17161 const struct mips_builtin_description *d;
17162 unsigned int i;
17163
17164 /* Iterate through all of the bdesc arrays, initializing all of the
17165 builtin functions. */
17166 for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
17167 {
17168 d = &mips_builtins[i];
17169 if (d->avail ())
17170 {
17171 mips_builtin_decls[i]
17172 = add_builtin_function (d->name,
17173 mips_build_function_type (d->function_type),
17174 i, BUILT_IN_MD, NULL, NULL);
17175 if (mips_builtin_decls[i] && d->is_pure)
17176 DECL_PURE_P (mips_builtin_decls[i]) = 1;
17177 mips_get_builtin_decl_index[d->icode] = i;
17178 }
17179 }
17180 }
17181
17182 /* Implement TARGET_BUILTIN_DECL. */
17183
17184 static tree
17185 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
17186 {
17187 if (code >= ARRAY_SIZE (mips_builtins))
17188 return error_mark_node;
17189 return mips_builtin_decls[code];
17190 }
17191
17192 /* Implement TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION. */
17193
17194 static tree
17195 mips_builtin_vectorized_function (unsigned int fn, tree type_out, tree type_in)
17196 {
17197 machine_mode in_mode, out_mode;
17198 int in_n, out_n;
17199
17200 if (TREE_CODE (type_out) != VECTOR_TYPE
17201 || TREE_CODE (type_in) != VECTOR_TYPE
17202 || !ISA_HAS_MSA)
17203 return NULL_TREE;
17204
17205 out_mode = TYPE_MODE (TREE_TYPE (type_out));
17206 out_n = TYPE_VECTOR_SUBPARTS (type_out);
17207 in_mode = TYPE_MODE (TREE_TYPE (type_in));
17208 in_n = TYPE_VECTOR_SUBPARTS (type_in);
17209
17210 /* INSN is the name of the associated instruction pattern, without
17211 the leading CODE_FOR_. */
17212 #define MIPS_GET_BUILTIN(INSN) \
17213 mips_builtin_decls[mips_get_builtin_decl_index[CODE_FOR_##INSN]]
17214
17215 switch (fn)
17216 {
17217 case BUILT_IN_SQRT:
17218 if (out_mode == DFmode && out_n == 2
17219 && in_mode == DFmode && in_n == 2)
17220 return MIPS_GET_BUILTIN (msa_fsqrt_d);
17221 break;
17222 case BUILT_IN_SQRTF:
17223 if (out_mode == SFmode && out_n == 4
17224 && in_mode == SFmode && in_n == 4)
17225 return MIPS_GET_BUILTIN (msa_fsqrt_w);
17226 break;
17227 default:
17228 break;
17229 }
17230
17231 return NULL_TREE;
17232 }
17233
17234 /* Take argument ARGNO from EXP's argument list and convert it into
17235 an expand operand. Store the operand in *OP. */
17236
17237 static void
17238 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
17239 unsigned int argno)
17240 {
17241 tree arg;
17242 rtx value;
17243
17244 arg = CALL_EXPR_ARG (exp, argno);
17245 value = expand_normal (arg);
17246 create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
17247 }
17248
17249 /* Expand instruction ICODE as part of a built-in function sequence.
17250 Use the first NOPS elements of OPS as the instruction's operands.
17251 HAS_TARGET_P is true if operand 0 is a target; it is false if the
17252 instruction has no target.
17253
17254 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
17255
17256 static rtx
17257 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
17258 struct expand_operand *ops, bool has_target_p)
17259 {
17260 machine_mode imode;
17261 int rangelo = 0, rangehi = 0, error_opno = 0;
17262 rtx sireg;
17263
17264 switch (icode)
17265 {
17266 /* The third operand of these instructions is in SImode, so we need to
17267 bring the corresponding builtin argument from QImode into SImode. */
17268 case CODE_FOR_loongson_pshufh:
17269 case CODE_FOR_loongson_psllh:
17270 case CODE_FOR_loongson_psllw:
17271 case CODE_FOR_loongson_psrah:
17272 case CODE_FOR_loongson_psraw:
17273 case CODE_FOR_loongson_psrlh:
17274 case CODE_FOR_loongson_psrlw:
17275 gcc_assert (has_target_p && nops == 3 && ops[2].mode == QImode);
17276 sireg = gen_reg_rtx (SImode);
17277 emit_insn (gen_zero_extendqisi2 (sireg,
17278 force_reg (QImode, ops[2].value)));
17279 ops[2].value = sireg;
17280 ops[2].mode = SImode;
17281 break;
17282
17283 case CODE_FOR_msa_addvi_b:
17284 case CODE_FOR_msa_addvi_h:
17285 case CODE_FOR_msa_addvi_w:
17286 case CODE_FOR_msa_addvi_d:
17287 case CODE_FOR_msa_clti_u_b:
17288 case CODE_FOR_msa_clti_u_h:
17289 case CODE_FOR_msa_clti_u_w:
17290 case CODE_FOR_msa_clti_u_d:
17291 case CODE_FOR_msa_clei_u_b:
17292 case CODE_FOR_msa_clei_u_h:
17293 case CODE_FOR_msa_clei_u_w:
17294 case CODE_FOR_msa_clei_u_d:
17295 case CODE_FOR_msa_maxi_u_b:
17296 case CODE_FOR_msa_maxi_u_h:
17297 case CODE_FOR_msa_maxi_u_w:
17298 case CODE_FOR_msa_maxi_u_d:
17299 case CODE_FOR_msa_mini_u_b:
17300 case CODE_FOR_msa_mini_u_h:
17301 case CODE_FOR_msa_mini_u_w:
17302 case CODE_FOR_msa_mini_u_d:
17303 case CODE_FOR_msa_subvi_b:
17304 case CODE_FOR_msa_subvi_h:
17305 case CODE_FOR_msa_subvi_w:
17306 case CODE_FOR_msa_subvi_d:
17307 gcc_assert (has_target_p && nops == 3);
17308 /* We only generate a vector of constants iff the second argument
17309 is an immediate. We also validate the range of the immediate. */
17310 if (CONST_INT_P (ops[2].value))
17311 {
17312 rangelo = 0;
17313 rangehi = 31;
17314 if (IN_RANGE (INTVAL (ops[2].value), rangelo, rangehi))
17315 {
17316 ops[2].mode = ops[0].mode;
17317 ops[2].value = mips_gen_const_int_vector (ops[2].mode,
17318 INTVAL (ops[2].value));
17319 }
17320 else
17321 error_opno = 2;
17322 }
17323 break;
17324
17325 case CODE_FOR_msa_ceqi_b:
17326 case CODE_FOR_msa_ceqi_h:
17327 case CODE_FOR_msa_ceqi_w:
17328 case CODE_FOR_msa_ceqi_d:
17329 case CODE_FOR_msa_clti_s_b:
17330 case CODE_FOR_msa_clti_s_h:
17331 case CODE_FOR_msa_clti_s_w:
17332 case CODE_FOR_msa_clti_s_d:
17333 case CODE_FOR_msa_clei_s_b:
17334 case CODE_FOR_msa_clei_s_h:
17335 case CODE_FOR_msa_clei_s_w:
17336 case CODE_FOR_msa_clei_s_d:
17337 case CODE_FOR_msa_maxi_s_b:
17338 case CODE_FOR_msa_maxi_s_h:
17339 case CODE_FOR_msa_maxi_s_w:
17340 case CODE_FOR_msa_maxi_s_d:
17341 case CODE_FOR_msa_mini_s_b:
17342 case CODE_FOR_msa_mini_s_h:
17343 case CODE_FOR_msa_mini_s_w:
17344 case CODE_FOR_msa_mini_s_d:
17345 gcc_assert (has_target_p && nops == 3);
17346 /* We only generate a vector of constants iff the second argument
17347 is an immediate. We also validate the range of the immediate. */
17348 if (CONST_INT_P (ops[2].value))
17349 {
17350 rangelo = -16;
17351 rangehi = 15;
17352 if (IN_RANGE (INTVAL (ops[2].value), rangelo, rangehi))
17353 {
17354 ops[2].mode = ops[0].mode;
17355 ops[2].value = mips_gen_const_int_vector (ops[2].mode,
17356 INTVAL (ops[2].value));
17357 }
17358 else
17359 error_opno = 2;
17360 }
17361 break;
17362
17363 case CODE_FOR_msa_andi_b:
17364 case CODE_FOR_msa_ori_b:
17365 case CODE_FOR_msa_nori_b:
17366 case CODE_FOR_msa_xori_b:
17367 gcc_assert (has_target_p && nops == 3);
17368 if (!CONST_INT_P (ops[2].value))
17369 break;
17370 ops[2].mode = ops[0].mode;
17371 ops[2].value = mips_gen_const_int_vector (ops[2].mode,
17372 INTVAL (ops[2].value));
17373 break;
17374
17375 case CODE_FOR_msa_bmzi_b:
17376 case CODE_FOR_msa_bmnzi_b:
17377 case CODE_FOR_msa_bseli_b:
17378 gcc_assert (has_target_p && nops == 4);
17379 if (!CONST_INT_P (ops[3].value))
17380 break;
17381 ops[3].mode = ops[0].mode;
17382 ops[3].value = mips_gen_const_int_vector (ops[3].mode,
17383 INTVAL (ops[3].value));
17384 break;
17385
17386 case CODE_FOR_msa_fill_b:
17387 case CODE_FOR_msa_fill_h:
17388 case CODE_FOR_msa_fill_w:
17389 case CODE_FOR_msa_fill_d:
17390 /* Map the built-ins to vector fill operations. We need fix up the mode
17391 for the element being inserted. */
17392 gcc_assert (has_target_p && nops == 2);
17393 imode = GET_MODE_INNER (ops[0].mode);
17394 ops[1].value = lowpart_subreg (imode, ops[1].value, ops[1].mode);
17395 ops[1].mode = imode;
17396 break;
17397
17398 case CODE_FOR_msa_ilvl_b:
17399 case CODE_FOR_msa_ilvl_h:
17400 case CODE_FOR_msa_ilvl_w:
17401 case CODE_FOR_msa_ilvl_d:
17402 case CODE_FOR_msa_ilvr_b:
17403 case CODE_FOR_msa_ilvr_h:
17404 case CODE_FOR_msa_ilvr_w:
17405 case CODE_FOR_msa_ilvr_d:
17406 case CODE_FOR_msa_ilvev_b:
17407 case CODE_FOR_msa_ilvev_h:
17408 case CODE_FOR_msa_ilvev_w:
17409 case CODE_FOR_msa_ilvod_b:
17410 case CODE_FOR_msa_ilvod_h:
17411 case CODE_FOR_msa_ilvod_w:
17412 case CODE_FOR_msa_pckev_b:
17413 case CODE_FOR_msa_pckev_h:
17414 case CODE_FOR_msa_pckev_w:
17415 case CODE_FOR_msa_pckod_b:
17416 case CODE_FOR_msa_pckod_h:
17417 case CODE_FOR_msa_pckod_w:
17418 /* Swap the operands 1 and 2 for interleave operations. Built-ins follow
17419 convention of ISA, which have op1 as higher component and op2 as lower
17420 component. However, the VEC_PERM op in tree and vec_concat in RTL
17421 expects first operand to be lower component, because of which this
17422 swap is needed for builtins. */
17423 gcc_assert (has_target_p && nops == 3);
17424 std::swap (ops[1], ops[2]);
17425 break;
17426
17427 case CODE_FOR_msa_maddv_b:
17428 case CODE_FOR_msa_maddv_h:
17429 case CODE_FOR_msa_maddv_w:
17430 case CODE_FOR_msa_maddv_d:
17431 case CODE_FOR_msa_fmadd_w:
17432 case CODE_FOR_msa_fmadd_d:
17433 case CODE_FOR_msa_fmsub_w:
17434 case CODE_FOR_msa_fmsub_d:
17435 /* fma(a, b, c) results into (a * b + c), however builtin_msa_fmadd expects
17436 it to be (a + b * c). Swap the 1st and 3rd operands. */
17437 std::swap (ops[1], ops[3]);
17438 break;
17439
17440 case CODE_FOR_msa_slli_b:
17441 case CODE_FOR_msa_slli_h:
17442 case CODE_FOR_msa_slli_w:
17443 case CODE_FOR_msa_slli_d:
17444 case CODE_FOR_msa_srai_b:
17445 case CODE_FOR_msa_srai_h:
17446 case CODE_FOR_msa_srai_w:
17447 case CODE_FOR_msa_srai_d:
17448 case CODE_FOR_msa_srli_b:
17449 case CODE_FOR_msa_srli_h:
17450 case CODE_FOR_msa_srli_w:
17451 case CODE_FOR_msa_srli_d:
17452 gcc_assert (has_target_p && nops == 3);
17453 if (CONST_INT_P (ops[2].value))
17454 {
17455 rangelo = 0;
17456 rangehi = GET_MODE_UNIT_BITSIZE (ops[0].mode) - 1;
17457 if (IN_RANGE (INTVAL (ops[2].value), rangelo, rangehi))
17458 {
17459 ops[2].mode = ops[0].mode;
17460 ops[2].value = mips_gen_const_int_vector (ops[2].mode,
17461 INTVAL (ops[2].value));
17462 }
17463 else
17464 error_opno = 2;
17465 }
17466 break;
17467
17468 case CODE_FOR_msa_insert_b:
17469 case CODE_FOR_msa_insert_h:
17470 case CODE_FOR_msa_insert_w:
17471 case CODE_FOR_msa_insert_d:
17472 /* Map the built-ins to insert operations. We need to swap operands,
17473 fix up the mode for the element being inserted, and generate
17474 a bit mask for vec_merge. */
17475 gcc_assert (has_target_p && nops == 4);
17476 std::swap (ops[1], ops[2]);
17477 std::swap (ops[1], ops[3]);
17478 imode = GET_MODE_INNER (ops[0].mode);
17479 ops[1].value = lowpart_subreg (imode, ops[1].value, ops[1].mode);
17480 ops[1].mode = imode;
17481 rangelo = 0;
17482 rangehi = GET_MODE_NUNITS (ops[0].mode) - 1;
17483 if (CONST_INT_P (ops[3].value)
17484 && IN_RANGE (INTVAL (ops[3].value), rangelo, rangehi))
17485 ops[3].value = GEN_INT (1 << INTVAL (ops[3].value));
17486 else
17487 error_opno = 2;
17488 break;
17489
17490 case CODE_FOR_msa_insve_b:
17491 case CODE_FOR_msa_insve_h:
17492 case CODE_FOR_msa_insve_w:
17493 case CODE_FOR_msa_insve_d:
17494 /* Map the built-ins to element insert operations. We need to swap
17495 operands and generate a bit mask. */
17496 gcc_assert (has_target_p && nops == 4);
17497 std::swap (ops[1], ops[2]);
17498 std::swap (ops[1], ops[3]);
17499 rangelo = 0;
17500 rangehi = GET_MODE_NUNITS (ops[0].mode) - 1;
17501 if (CONST_INT_P (ops[3].value)
17502 && IN_RANGE (INTVAL (ops[3].value), rangelo, rangehi))
17503 ops[3].value = GEN_INT (1 << INTVAL (ops[3].value));
17504 else
17505 error_opno = 2;
17506 break;
17507
17508 case CODE_FOR_msa_shf_b:
17509 case CODE_FOR_msa_shf_h:
17510 case CODE_FOR_msa_shf_w:
17511 case CODE_FOR_msa_shf_w_f:
17512 gcc_assert (has_target_p && nops == 3);
17513 ops[2].value = mips_gen_const_int_vector_shuffle (ops[0].mode,
17514 INTVAL (ops[2].value));
17515 break;
17516
17517 case CODE_FOR_msa_vshf_b:
17518 case CODE_FOR_msa_vshf_h:
17519 case CODE_FOR_msa_vshf_w:
17520 case CODE_FOR_msa_vshf_d:
17521 gcc_assert (has_target_p && nops == 4);
17522 std::swap (ops[1], ops[3]);
17523 break;
17524
17525 case CODE_FOR_msa_dpadd_s_w:
17526 case CODE_FOR_msa_dpadd_s_h:
17527 case CODE_FOR_msa_dpadd_s_d:
17528 case CODE_FOR_msa_dpadd_u_w:
17529 case CODE_FOR_msa_dpadd_u_h:
17530 case CODE_FOR_msa_dpadd_u_d:
17531 case CODE_FOR_msa_dpsub_s_w:
17532 case CODE_FOR_msa_dpsub_s_h:
17533 case CODE_FOR_msa_dpsub_s_d:
17534 case CODE_FOR_msa_dpsub_u_w:
17535 case CODE_FOR_msa_dpsub_u_h:
17536 case CODE_FOR_msa_dpsub_u_d:
17537 /* Force the operands which correspond to the same in-out register
17538 to have the same pseudo assigned to them. If the input operand
17539 is not REG, create one for it. */
17540 if (!REG_P (ops[1].value))
17541 ops[1].value = copy_to_mode_reg (ops[1].mode, ops[1].value);
17542 create_output_operand (&ops[0], ops[1].value, ops[1].mode);
17543 break;
17544
17545 default:
17546 break;
17547 }
17548
17549 if (error_opno != 0)
17550 {
17551 error ("argument %d to the built-in must be a constant"
17552 " in range %d to %d", error_opno, rangelo, rangehi);
17553 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
17554 }
17555 else if (!maybe_expand_insn (icode, nops, ops))
17556 {
17557 error ("invalid argument to built-in function");
17558 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
17559 }
17560 return has_target_p ? ops[0].value : const0_rtx;
17561 }
17562
17563 /* Expand a floating-point comparison for built-in function call EXP.
17564 The first NARGS arguments are the values to be compared. ICODE is
17565 the .md pattern that does the comparison and COND is the condition
17566 that is being tested. Return an rtx for the result. */
17567
17568 static rtx
17569 mips_expand_builtin_compare_1 (enum insn_code icode,
17570 enum mips_fp_condition cond,
17571 tree exp, int nargs)
17572 {
17573 struct expand_operand ops[MAX_RECOG_OPERANDS];
17574 rtx output;
17575 int opno, argno;
17576
17577 /* The instruction should have a target operand, an operand for each
17578 argument, and an operand for COND. */
17579 gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
17580
17581 output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
17582 opno = 0;
17583 create_fixed_operand (&ops[opno++], output);
17584 for (argno = 0; argno < nargs; argno++)
17585 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
17586 create_integer_operand (&ops[opno++], (int) cond);
17587 return mips_expand_builtin_insn (icode, opno, ops, true);
17588 }
17589
17590 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
17591 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
17592 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
17593 suggests a good place to put the result. */
17594
17595 static rtx
17596 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
17597 bool has_target_p)
17598 {
17599 struct expand_operand ops[MAX_RECOG_OPERANDS];
17600 int opno, argno;
17601
17602 /* Map any target to operand 0. */
17603 opno = 0;
17604 if (has_target_p)
17605 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
17606
17607 /* Map the arguments to the other operands. */
17608 gcc_assert (opno + call_expr_nargs (exp)
17609 == insn_data[icode].n_generator_args);
17610 for (argno = 0; argno < call_expr_nargs (exp); argno++)
17611 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
17612
17613 return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
17614 }
17615
17616 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
17617 function; TYPE says which. EXP is the CALL_EXPR that calls the
17618 function, ICODE is the instruction that should be used to compare
17619 the first two arguments, and COND is the condition it should test.
17620 TARGET, if nonnull, suggests a good place to put the result. */
17621
17622 static rtx
17623 mips_expand_builtin_movtf (enum mips_builtin_type type,
17624 enum insn_code icode, enum mips_fp_condition cond,
17625 rtx target, tree exp)
17626 {
17627 struct expand_operand ops[4];
17628 rtx cmp_result;
17629
17630 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
17631 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
17632 if (type == MIPS_BUILTIN_MOVT)
17633 {
17634 mips_prepare_builtin_arg (&ops[2], exp, 2);
17635 mips_prepare_builtin_arg (&ops[1], exp, 3);
17636 }
17637 else
17638 {
17639 mips_prepare_builtin_arg (&ops[1], exp, 2);
17640 mips_prepare_builtin_arg (&ops[2], exp, 3);
17641 }
17642 create_fixed_operand (&ops[3], cmp_result);
17643 return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
17644 4, ops, true);
17645 }
17646
17647 /* Expand an MSA built-in for a compare and branch instruction specified by
17648 ICODE, set a general-purpose register to 1 if the branch was taken,
17649 0 otherwise. */
17650
17651 static rtx
17652 mips_expand_builtin_msa_test_branch (enum insn_code icode, tree exp)
17653 {
17654 struct expand_operand ops[3];
17655 rtx_insn *cbranch;
17656 rtx_code_label *true_label, *done_label;
17657 rtx cmp_result;
17658
17659 true_label = gen_label_rtx ();
17660 done_label = gen_label_rtx ();
17661
17662 create_input_operand (&ops[0], true_label, TYPE_MODE (TREE_TYPE (exp)));
17663 mips_prepare_builtin_arg (&ops[1], exp, 0);
17664 create_fixed_operand (&ops[2], const0_rtx);
17665
17666 /* Make sure that the operand 1 is a REG. */
17667 if (GET_CODE (ops[1].value) != REG)
17668 ops[1].value = force_reg (ops[1].mode, ops[1].value);
17669
17670 if ((cbranch = maybe_gen_insn (icode, 3, ops)) == NULL_RTX)
17671 error ("failed to expand built-in function");
17672
17673 cmp_result = gen_reg_rtx (SImode);
17674
17675 /* First assume that CMP_RESULT is false. */
17676 mips_emit_move (cmp_result, const0_rtx);
17677
17678 /* Branch to TRUE_LABEL if CBRANCH is taken and DONE_LABEL otherwise. */
17679 emit_jump_insn (cbranch);
17680 emit_jump_insn (gen_jump (done_label));
17681 emit_barrier ();
17682
17683 /* Set CMP_RESULT to true if the branch was taken. */
17684 emit_label (true_label);
17685 mips_emit_move (cmp_result, const1_rtx);
17686
17687 emit_label (done_label);
17688 return cmp_result;
17689 }
17690
17691 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
17692 into TARGET otherwise. Return TARGET. */
17693
17694 static rtx
17695 mips_builtin_branch_and_move (rtx condition, rtx target,
17696 rtx value_if_true, rtx value_if_false)
17697 {
17698 rtx_code_label *true_label, *done_label;
17699
17700 true_label = gen_label_rtx ();
17701 done_label = gen_label_rtx ();
17702
17703 /* First assume that CONDITION is false. */
17704 mips_emit_move (target, value_if_false);
17705
17706 /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise. */
17707 emit_jump_insn (gen_condjump (condition, true_label));
17708 emit_jump_insn (gen_jump (done_label));
17709 emit_barrier ();
17710
17711 /* Fix TARGET if CONDITION is true. */
17712 emit_label (true_label);
17713 mips_emit_move (target, value_if_true);
17714
17715 emit_label (done_label);
17716 return target;
17717 }
17718
17719 /* Expand a comparison built-in function of type BUILTIN_TYPE. EXP is
17720 the CALL_EXPR that calls the function, ICODE is the code of the
17721 comparison instruction, and COND is the condition it should test.
17722 TARGET, if nonnull, suggests a good place to put the boolean result. */
17723
17724 static rtx
17725 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
17726 enum insn_code icode, enum mips_fp_condition cond,
17727 rtx target, tree exp)
17728 {
17729 rtx offset, condition, cmp_result;
17730
17731 if (target == 0 || GET_MODE (target) != SImode)
17732 target = gen_reg_rtx (SImode);
17733 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
17734 call_expr_nargs (exp));
17735
17736 /* If the comparison sets more than one register, we define the result
17737 to be 0 if all registers are false and -1 if all registers are true.
17738 The value of the complete result is indeterminate otherwise. */
17739 switch (builtin_type)
17740 {
17741 case MIPS_BUILTIN_CMP_ALL:
17742 condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
17743 return mips_builtin_branch_and_move (condition, target,
17744 const0_rtx, const1_rtx);
17745
17746 case MIPS_BUILTIN_CMP_UPPER:
17747 case MIPS_BUILTIN_CMP_LOWER:
17748 offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
17749 condition = gen_single_cc (cmp_result, offset);
17750 return mips_builtin_branch_and_move (condition, target,
17751 const1_rtx, const0_rtx);
17752
17753 default:
17754 condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
17755 return mips_builtin_branch_and_move (condition, target,
17756 const1_rtx, const0_rtx);
17757 }
17758 }
17759
17760 /* Expand a bposge built-in function of type BUILTIN_TYPE. TARGET,
17761 if nonnull, suggests a good place to put the boolean result. */
17762
17763 static rtx
17764 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
17765 {
17766 rtx condition, cmp_result;
17767 int cmp_value;
17768
17769 if (target == 0 || GET_MODE (target) != SImode)
17770 target = gen_reg_rtx (SImode);
17771
17772 cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
17773
17774 if (builtin_type == MIPS_BUILTIN_BPOSGE32)
17775 cmp_value = 32;
17776 else
17777 gcc_assert (0);
17778
17779 condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
17780 return mips_builtin_branch_and_move (condition, target,
17781 const1_rtx, const0_rtx);
17782 }
17783
17784 /* Implement TARGET_EXPAND_BUILTIN. */
17785
17786 static rtx
17787 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
17788 machine_mode mode, int ignore)
17789 {
17790 tree fndecl;
17791 unsigned int fcode, avail;
17792 const struct mips_builtin_description *d;
17793
17794 fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
17795 fcode = DECL_MD_FUNCTION_CODE (fndecl);
17796 gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
17797 d = &mips_builtins[fcode];
17798 avail = d->avail ();
17799 gcc_assert (avail != 0);
17800 if (TARGET_MIPS16 && !(avail & BUILTIN_AVAIL_MIPS16)
17801 && (!TARGET_MIPS16E2 || !(avail & BUILTIN_AVAIL_MIPS16E2)))
17802 {
17803 error ("built-in function %qE not supported for MIPS16",
17804 DECL_NAME (fndecl));
17805 return ignore ? const0_rtx : CONST0_RTX (mode);
17806 }
17807 switch (d->builtin_type)
17808 {
17809 case MIPS_BUILTIN_DIRECT:
17810 return mips_expand_builtin_direct (d->icode, target, exp, true);
17811
17812 case MIPS_BUILTIN_DIRECT_NO_TARGET:
17813 return mips_expand_builtin_direct (d->icode, target, exp, false);
17814
17815 case MIPS_BUILTIN_MOVT:
17816 case MIPS_BUILTIN_MOVF:
17817 return mips_expand_builtin_movtf (d->builtin_type, d->icode,
17818 d->cond, target, exp);
17819
17820 case MIPS_BUILTIN_CMP_ANY:
17821 case MIPS_BUILTIN_CMP_ALL:
17822 case MIPS_BUILTIN_CMP_UPPER:
17823 case MIPS_BUILTIN_CMP_LOWER:
17824 case MIPS_BUILTIN_CMP_SINGLE:
17825 return mips_expand_builtin_compare (d->builtin_type, d->icode,
17826 d->cond, target, exp);
17827
17828 case MIPS_BUILTIN_MSA_TEST_BRANCH:
17829 return mips_expand_builtin_msa_test_branch (d->icode, exp);
17830
17831 case MIPS_BUILTIN_BPOSGE32:
17832 return mips_expand_builtin_bposge (d->builtin_type, target);
17833 }
17834 gcc_unreachable ();
17835 }
17836
17837 /* An entry in the MIPS16 constant pool. VALUE is the pool constant,
17838 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
17839 struct mips16_constant {
17840 struct mips16_constant *next;
17841 rtx value;
17842 rtx_code_label *label;
17843 machine_mode mode;
17844 };
17845
17846 /* Information about an incomplete MIPS16 constant pool. FIRST is the
17847 first constant, HIGHEST_ADDRESS is the highest address that the first
17848 byte of the pool can have, and INSN_ADDRESS is the current instruction
17849 address. */
17850 struct mips16_constant_pool {
17851 struct mips16_constant *first;
17852 int highest_address;
17853 int insn_address;
17854 };
17855
17856 /* Add constant VALUE to POOL and return its label. MODE is the
17857 value's mode (used for CONST_INTs, etc.). */
17858
17859 static rtx_code_label *
17860 mips16_add_constant (struct mips16_constant_pool *pool,
17861 rtx value, machine_mode mode)
17862 {
17863 struct mips16_constant **p, *c;
17864 bool first_of_size_p;
17865
17866 /* See whether the constant is already in the pool. If so, return the
17867 existing label, otherwise leave P pointing to the place where the
17868 constant should be added.
17869
17870 Keep the pool sorted in increasing order of mode size so that we can
17871 reduce the number of alignments needed. */
17872 first_of_size_p = true;
17873 for (p = &pool->first; *p != 0; p = &(*p)->next)
17874 {
17875 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
17876 return (*p)->label;
17877 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
17878 break;
17879 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
17880 first_of_size_p = false;
17881 }
17882
17883 /* In the worst case, the constant needed by the earliest instruction
17884 will end up at the end of the pool. The entire pool must then be
17885 accessible from that instruction.
17886
17887 When adding the first constant, set the pool's highest address to
17888 the address of the first out-of-range byte. Adjust this address
17889 downwards each time a new constant is added. */
17890 if (pool->first == 0)
17891 /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
17892 of the instruction with the lowest two bits clear. The base PC
17893 value for LDPC has the lowest three bits clear. Assume the worst
17894 case here; namely that the PC-relative instruction occupies the
17895 last 2 bytes in an aligned word. */
17896 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
17897 pool->highest_address -= GET_MODE_SIZE (mode);
17898 if (first_of_size_p)
17899 /* Take into account the worst possible padding due to alignment. */
17900 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
17901
17902 /* Create a new entry. */
17903 c = XNEW (struct mips16_constant);
17904 c->value = value;
17905 c->mode = mode;
17906 c->label = gen_label_rtx ();
17907 c->next = *p;
17908 *p = c;
17909
17910 return c->label;
17911 }
17912
17913 /* Output constant VALUE after instruction INSN and return the last
17914 instruction emitted. MODE is the mode of the constant. */
17915
17916 static rtx_insn *
17917 mips16_emit_constants_1 (machine_mode mode, rtx value, rtx_insn *insn)
17918 {
17919 if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
17920 {
17921 rtx size = GEN_INT (GET_MODE_SIZE (mode));
17922 return emit_insn_after (gen_consttable_int (value, size), insn);
17923 }
17924
17925 if (SCALAR_FLOAT_MODE_P (mode))
17926 return emit_insn_after (gen_consttable_float (value), insn);
17927
17928 if (VECTOR_MODE_P (mode))
17929 {
17930 int i;
17931
17932 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
17933 insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
17934 CONST_VECTOR_ELT (value, i), insn);
17935 return insn;
17936 }
17937
17938 gcc_unreachable ();
17939 }
17940
17941 /* Dump out the constants in CONSTANTS after INSN. Record the initial
17942 label number in the `consttable' and `consttable_end' insns emitted
17943 at the beginning and the end of the constant pool respectively, so
17944 that individual pools can be uniquely marked as data for the purpose
17945 of disassembly. */
17946
17947 static void
17948 mips16_emit_constants (struct mips16_constant *constants, rtx_insn *insn)
17949 {
17950 int label_num = constants ? CODE_LABEL_NUMBER (constants->label) : 0;
17951 struct mips16_constant *c, *next;
17952 int align;
17953
17954 align = 0;
17955 if (constants)
17956 insn = emit_insn_after (gen_consttable (GEN_INT (label_num)), insn);
17957 for (c = constants; c != NULL; c = next)
17958 {
17959 /* If necessary, increase the alignment of PC. */
17960 if (align < GET_MODE_SIZE (c->mode))
17961 {
17962 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
17963 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
17964 }
17965 align = GET_MODE_SIZE (c->mode);
17966
17967 insn = emit_label_after (c->label, insn);
17968 insn = mips16_emit_constants_1 (c->mode, c->value, insn);
17969
17970 next = c->next;
17971 free (c);
17972 }
17973 if (constants)
17974 insn = emit_insn_after (gen_consttable_end (GEN_INT (label_num)), insn);
17975
17976 emit_barrier_after (insn);
17977 }
17978
17979 /* Return the length of instruction INSN. */
17980
17981 static int
17982 mips16_insn_length (rtx_insn *insn)
17983 {
17984 if (JUMP_TABLE_DATA_P (insn))
17985 {
17986 rtx body = PATTERN (insn);
17987 if (GET_CODE (body) == ADDR_VEC)
17988 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
17989 else if (GET_CODE (body) == ADDR_DIFF_VEC)
17990 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
17991 else
17992 gcc_unreachable ();
17993 }
17994 return get_attr_length (insn);
17995 }
17996
17997 /* If *X is a symbolic constant that refers to the constant pool, add
17998 the constant to POOL and rewrite *X to use the constant's label. */
17999
18000 static void
18001 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
18002 {
18003 rtx base, offset;
18004 rtx_code_label *label;
18005
18006 split_const (*x, &base, &offset);
18007 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
18008 {
18009 label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
18010 get_pool_mode (base));
18011 base = gen_rtx_LABEL_REF (Pmode, label);
18012 *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
18013 }
18014 }
18015
18016 /* Rewrite INSN so that constant pool references refer to the constant's
18017 label instead. */
18018
18019 static void
18020 mips16_rewrite_pool_refs (rtx_insn *insn, struct mips16_constant_pool *pool)
18021 {
18022 subrtx_ptr_iterator::array_type array;
18023 FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), ALL)
18024 {
18025 rtx *loc = *iter;
18026
18027 if (force_to_mem_operand (*loc, Pmode))
18028 {
18029 rtx mem = force_const_mem (GET_MODE (*loc), *loc);
18030 validate_change (insn, loc, mem, false);
18031 }
18032
18033 if (MEM_P (*loc))
18034 {
18035 mips16_rewrite_pool_constant (pool, &XEXP (*loc, 0));
18036 iter.skip_subrtxes ();
18037 }
18038 else
18039 {
18040 if (TARGET_MIPS16_TEXT_LOADS)
18041 mips16_rewrite_pool_constant (pool, loc);
18042 if (GET_CODE (*loc) == CONST
18043 /* Don't rewrite the __mips16_rdwr symbol. */
18044 || (GET_CODE (*loc) == UNSPEC
18045 && XINT (*loc, 1) == UNSPEC_TLS_GET_TP))
18046 iter.skip_subrtxes ();
18047 }
18048 }
18049 }
18050
18051 /* Return whether CFG is used in mips_reorg. */
18052
18053 static bool
18054 mips_cfg_in_reorg (void)
18055 {
18056 return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
18057 || TARGET_RELAX_PIC_CALLS);
18058 }
18059
18060 /* Build MIPS16 constant pools. Split the instructions if SPLIT_P,
18061 otherwise assume that they are already split. */
18062
18063 static void
18064 mips16_lay_out_constants (bool split_p)
18065 {
18066 struct mips16_constant_pool pool;
18067 rtx_insn *insn, *barrier;
18068
18069 if (!TARGET_MIPS16_PCREL_LOADS)
18070 return;
18071
18072 if (split_p)
18073 {
18074 if (mips_cfg_in_reorg ())
18075 split_all_insns ();
18076 else
18077 split_all_insns_noflow ();
18078 }
18079 barrier = 0;
18080 memset (&pool, 0, sizeof (pool));
18081 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
18082 {
18083 /* Rewrite constant pool references in INSN. */
18084 if (USEFUL_INSN_P (insn))
18085 mips16_rewrite_pool_refs (insn, &pool);
18086
18087 pool.insn_address += mips16_insn_length (insn);
18088
18089 if (pool.first != NULL)
18090 {
18091 /* If there are no natural barriers between the first user of
18092 the pool and the highest acceptable address, we'll need to
18093 create a new instruction to jump around the constant pool.
18094 In the worst case, this instruction will be 4 bytes long.
18095
18096 If it's too late to do this transformation after INSN,
18097 do it immediately before INSN. */
18098 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
18099 {
18100 rtx_code_label *label;
18101 rtx_insn *jump;
18102
18103 label = gen_label_rtx ();
18104
18105 jump = emit_jump_insn_before (gen_jump (label), insn);
18106 JUMP_LABEL (jump) = label;
18107 LABEL_NUSES (label) = 1;
18108 barrier = emit_barrier_after (jump);
18109
18110 emit_label_after (label, barrier);
18111 pool.insn_address += 4;
18112 }
18113
18114 /* See whether the constant pool is now out of range of the first
18115 user. If so, output the constants after the previous barrier.
18116 Note that any instructions between BARRIER and INSN (inclusive)
18117 will use negative offsets to refer to the pool. */
18118 if (pool.insn_address > pool.highest_address)
18119 {
18120 mips16_emit_constants (pool.first, barrier);
18121 pool.first = NULL;
18122 barrier = 0;
18123 }
18124 else if (BARRIER_P (insn))
18125 barrier = insn;
18126 }
18127 }
18128 mips16_emit_constants (pool.first, get_last_insn ());
18129 }
18130
18131 /* Return true if it is worth r10k_simplify_address's while replacing
18132 an address with X. We are looking for constants, and for addresses
18133 at a known offset from the incoming stack pointer. */
18134
18135 static bool
18136 r10k_simplified_address_p (rtx x)
18137 {
18138 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
18139 x = XEXP (x, 0);
18140 return x == virtual_incoming_args_rtx || CONSTANT_P (x);
18141 }
18142
18143 /* X is an expression that appears in INSN. Try to use the UD chains
18144 to simplify it, returning the simplified form on success and the
18145 original form otherwise. Replace the incoming value of $sp with
18146 virtual_incoming_args_rtx (which should never occur in X otherwise). */
18147
18148 static rtx
18149 r10k_simplify_address (rtx x, rtx_insn *insn)
18150 {
18151 rtx newx, op0, op1, set, note;
18152 rtx_insn *def_insn;
18153 df_ref use, def;
18154 struct df_link *defs;
18155
18156 newx = NULL_RTX;
18157 if (UNARY_P (x))
18158 {
18159 op0 = r10k_simplify_address (XEXP (x, 0), insn);
18160 if (op0 != XEXP (x, 0))
18161 newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
18162 op0, GET_MODE (XEXP (x, 0)));
18163 }
18164 else if (BINARY_P (x))
18165 {
18166 op0 = r10k_simplify_address (XEXP (x, 0), insn);
18167 op1 = r10k_simplify_address (XEXP (x, 1), insn);
18168 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
18169 newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
18170 }
18171 else if (GET_CODE (x) == LO_SUM)
18172 {
18173 /* LO_SUMs can be offset from HIGHs, if we know they won't
18174 overflow. See mips_classify_address for the rationale behind
18175 the lax check. */
18176 op0 = r10k_simplify_address (XEXP (x, 0), insn);
18177 if (GET_CODE (op0) == HIGH)
18178 newx = XEXP (x, 1);
18179 }
18180 else if (REG_P (x))
18181 {
18182 /* Uses are recorded by regno_reg_rtx, not X itself. */
18183 use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
18184 gcc_assert (use);
18185 defs = DF_REF_CHAIN (use);
18186
18187 /* Require a single definition. */
18188 if (defs && defs->next == NULL)
18189 {
18190 def = defs->ref;
18191 if (DF_REF_IS_ARTIFICIAL (def))
18192 {
18193 /* Replace the incoming value of $sp with
18194 virtual_incoming_args_rtx. */
18195 if (x == stack_pointer_rtx
18196 && DF_REF_BB (def) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
18197 newx = virtual_incoming_args_rtx;
18198 }
18199 else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
18200 DF_REF_BB (def)))
18201 {
18202 /* Make sure that DEF_INSN is a single set of REG. */
18203 def_insn = DF_REF_INSN (def);
18204 if (NONJUMP_INSN_P (def_insn))
18205 {
18206 set = single_set (def_insn);
18207 if (set && rtx_equal_p (SET_DEST (set), x))
18208 {
18209 /* Prefer to use notes, since the def-use chains
18210 are often shorter. */
18211 note = find_reg_equal_equiv_note (def_insn);
18212 if (note)
18213 newx = XEXP (note, 0);
18214 else
18215 newx = SET_SRC (set);
18216 newx = r10k_simplify_address (newx, def_insn);
18217 }
18218 }
18219 }
18220 }
18221 }
18222 if (newx && r10k_simplified_address_p (newx))
18223 return newx;
18224 return x;
18225 }
18226
18227 /* Return true if ADDRESS is known to be an uncached address
18228 on R10K systems. */
18229
18230 static bool
18231 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
18232 {
18233 unsigned HOST_WIDE_INT upper;
18234
18235 /* Check for KSEG1. */
18236 if (address + 0x60000000 < 0x20000000)
18237 return true;
18238
18239 /* Check for uncached XKPHYS addresses. */
18240 if (Pmode == DImode)
18241 {
18242 upper = (address >> 40) & 0xf9ffff;
18243 if (upper == 0x900000 || upper == 0xb80000)
18244 return true;
18245 }
18246 return false;
18247 }
18248
18249 /* Return true if we can prove that an access to address X in instruction
18250 INSN would be safe from R10K speculation. This X is a general
18251 expression; it might not be a legitimate address. */
18252
18253 static bool
18254 r10k_safe_address_p (rtx x, rtx_insn *insn)
18255 {
18256 rtx base, offset;
18257 HOST_WIDE_INT offset_val;
18258
18259 x = r10k_simplify_address (x, insn);
18260
18261 /* Check for references to the stack frame. It doesn't really matter
18262 how much of the frame has been allocated at INSN; -mr10k-cache-barrier
18263 allows us to assume that accesses to any part of the eventual frame
18264 is safe from speculation at any point in the function. */
18265 mips_split_plus (x, &base, &offset_val);
18266 if (base == virtual_incoming_args_rtx
18267 && offset_val >= -cfun->machine->frame.total_size
18268 && offset_val < cfun->machine->frame.args_size)
18269 return true;
18270
18271 /* Check for uncached addresses. */
18272 if (CONST_INT_P (x))
18273 return r10k_uncached_address_p (INTVAL (x));
18274
18275 /* Check for accesses to a static object. */
18276 split_const (x, &base, &offset);
18277 return offset_within_block_p (base, INTVAL (offset));
18278 }
18279
18280 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
18281 an in-range access to an automatic variable, or to an object with
18282 a link-time-constant address. */
18283
18284 static bool
18285 r10k_safe_mem_expr_p (tree expr, unsigned HOST_WIDE_INT offset)
18286 {
18287 poly_int64 bitoffset, bitsize;
18288 tree inner, var_offset;
18289 machine_mode mode;
18290 int unsigned_p, reverse_p, volatile_p;
18291
18292 inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
18293 &unsigned_p, &reverse_p, &volatile_p);
18294 if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
18295 return false;
18296
18297 offset += bitoffset / BITS_PER_UNIT;
18298 return offset < tree_to_uhwi (DECL_SIZE_UNIT (inner));
18299 }
18300
18301 /* Return true if X contains a MEM that is not safe from R10K speculation.
18302 INSN is the instruction that contains X. */
18303
18304 static bool
18305 r10k_needs_protection_p_1 (rtx x, rtx_insn *insn)
18306 {
18307 subrtx_var_iterator::array_type array;
18308 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
18309 {
18310 rtx mem = *iter;
18311 if (MEM_P (mem))
18312 {
18313 if ((MEM_EXPR (mem)
18314 && MEM_OFFSET_KNOWN_P (mem)
18315 && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
18316 || r10k_safe_address_p (XEXP (mem, 0), insn))
18317 iter.skip_subrtxes ();
18318 else
18319 return true;
18320 }
18321 }
18322 return false;
18323 }
18324
18325 /* A note_stores callback for which DATA points to an instruction pointer.
18326 If *DATA is nonnull, make it null if it X contains a MEM that is not
18327 safe from R10K speculation. */
18328
18329 static void
18330 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
18331 void *data)
18332 {
18333 rtx_insn **insn_ptr;
18334
18335 insn_ptr = (rtx_insn **) data;
18336 if (*insn_ptr && r10k_needs_protection_p_1 (x, *insn_ptr))
18337 *insn_ptr = NULL;
18338 }
18339
18340 /* X is the pattern of a call instruction. Return true if the call is
18341 not to a declared function. */
18342
18343 static bool
18344 r10k_needs_protection_p_call (const_rtx x)
18345 {
18346 subrtx_iterator::array_type array;
18347 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
18348 {
18349 const_rtx mem = *iter;
18350 if (MEM_P (mem))
18351 {
18352 const_rtx addr = XEXP (mem, 0);
18353 if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DECL (addr))
18354 iter.skip_subrtxes ();
18355 else
18356 return true;
18357 }
18358 }
18359 return false;
18360 }
18361
18362 /* Return true if instruction INSN needs to be protected by an R10K
18363 cache barrier. */
18364
18365 static bool
18366 r10k_needs_protection_p (rtx_insn *insn)
18367 {
18368 if (CALL_P (insn))
18369 return r10k_needs_protection_p_call (PATTERN (insn));
18370
18371 if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
18372 {
18373 note_stores (insn, r10k_needs_protection_p_store, &insn);
18374 return insn == NULL_RTX;
18375 }
18376
18377 return r10k_needs_protection_p_1 (PATTERN (insn), insn);
18378 }
18379
18380 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
18381 edge is unconditional. */
18382
18383 static bool
18384 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
18385 {
18386 edge_iterator ei;
18387 edge e;
18388
18389 FOR_EACH_EDGE (e, ei, bb->preds)
18390 if (!single_succ_p (e->src)
18391 || !bitmap_bit_p (protected_bbs, e->src->index)
18392 || (e->flags & EDGE_COMPLEX) != 0)
18393 return false;
18394 return true;
18395 }
18396
18397 /* Implement -mr10k-cache-barrier= for the current function. */
18398
18399 static void
18400 r10k_insert_cache_barriers (void)
18401 {
18402 int *rev_post_order;
18403 unsigned int i, n;
18404 basic_block bb;
18405 sbitmap protected_bbs;
18406 rtx_insn *insn, *end;
18407 rtx unprotected_region;
18408
18409 if (TARGET_MIPS16)
18410 {
18411 sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
18412 return;
18413 }
18414
18415 /* Calculate dominators. */
18416 calculate_dominance_info (CDI_DOMINATORS);
18417
18418 /* Bit X of PROTECTED_BBS is set if the last operation in basic block
18419 X is protected by a cache barrier. */
18420 protected_bbs = sbitmap_alloc (last_basic_block_for_fn (cfun));
18421 bitmap_clear (protected_bbs);
18422
18423 /* Iterate over the basic blocks in reverse post-order. */
18424 rev_post_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
18425 n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
18426 for (i = 0; i < n; i++)
18427 {
18428 bb = BASIC_BLOCK_FOR_FN (cfun, rev_post_order[i]);
18429
18430 /* If this block is only reached by unconditional edges, and if the
18431 source of every edge is protected, the beginning of the block is
18432 also protected. */
18433 if (r10k_protected_bb_p (bb, protected_bbs))
18434 unprotected_region = NULL_RTX;
18435 else
18436 unprotected_region = pc_rtx;
18437 end = NEXT_INSN (BB_END (bb));
18438
18439 /* UNPROTECTED_REGION is:
18440
18441 - null if we are processing a protected region,
18442 - pc_rtx if we are processing an unprotected region but have
18443 not yet found the first instruction in it
18444 - the first instruction in an unprotected region otherwise. */
18445 for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
18446 {
18447 if (unprotected_region && USEFUL_INSN_P (insn))
18448 {
18449 if (recog_memoized (insn) == CODE_FOR_mips_cache)
18450 /* This CACHE instruction protects the following code. */
18451 unprotected_region = NULL_RTX;
18452 else
18453 {
18454 /* See if INSN is the first instruction in this
18455 unprotected region. */
18456 if (unprotected_region == pc_rtx)
18457 unprotected_region = insn;
18458
18459 /* See if INSN needs to be protected. If so,
18460 we must insert a cache barrier somewhere between
18461 PREV_INSN (UNPROTECTED_REGION) and INSN. It isn't
18462 clear which position is better performance-wise,
18463 but as a tie-breaker, we assume that it is better
18464 to allow delay slots to be back-filled where
18465 possible, and that it is better not to insert
18466 barriers in the middle of already-scheduled code.
18467 We therefore insert the barrier at the beginning
18468 of the region. */
18469 if (r10k_needs_protection_p (insn))
18470 {
18471 emit_insn_before (gen_r10k_cache_barrier (),
18472 as_a <rtx_insn *> (unprotected_region));
18473 unprotected_region = NULL_RTX;
18474 }
18475 }
18476 }
18477
18478 if (CALL_P (insn))
18479 /* The called function is not required to protect the exit path.
18480 The code that follows a call is therefore unprotected. */
18481 unprotected_region = pc_rtx;
18482 }
18483
18484 /* Record whether the end of this block is protected. */
18485 if (unprotected_region == NULL_RTX)
18486 bitmap_set_bit (protected_bbs, bb->index);
18487 }
18488 XDELETEVEC (rev_post_order);
18489
18490 sbitmap_free (protected_bbs);
18491
18492 free_dominance_info (CDI_DOMINATORS);
18493 }
18494
18495 /* If INSN is a call, return the underlying CALL expr. Return NULL_RTX
18496 otherwise. If INSN has two call rtx, then store the second one in
18497 SECOND_CALL. */
18498
18499 static rtx
18500 mips_call_expr_from_insn (rtx_insn *insn, rtx *second_call)
18501 {
18502 rtx x;
18503 rtx x2;
18504
18505 if (!CALL_P (insn))
18506 return NULL_RTX;
18507
18508 x = PATTERN (insn);
18509 if (GET_CODE (x) == PARALLEL)
18510 {
18511 /* Calls returning complex values have two CALL rtx. Look for the second
18512 one here, and return it via the SECOND_CALL arg. */
18513 x2 = XVECEXP (x, 0, 1);
18514 if (GET_CODE (x2) == SET)
18515 x2 = XEXP (x2, 1);
18516 if (GET_CODE (x2) == CALL)
18517 *second_call = x2;
18518
18519 x = XVECEXP (x, 0, 0);
18520 }
18521 if (GET_CODE (x) == SET)
18522 x = XEXP (x, 1);
18523 gcc_assert (GET_CODE (x) == CALL);
18524
18525 return x;
18526 }
18527
18528 /* REG is set in DEF. See if the definition is one of the ways we load a
18529 register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
18530 If it is, return the symbol reference of the function, otherwise return
18531 NULL_RTX.
18532
18533 If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
18534 the values of source registers, otherwise treat such registers as
18535 having an unknown value. */
18536
18537 static rtx
18538 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
18539 {
18540 rtx_insn *def_insn;
18541 rtx set;
18542
18543 if (DF_REF_IS_ARTIFICIAL (def))
18544 return NULL_RTX;
18545
18546 def_insn = DF_REF_INSN (def);
18547 set = single_set (def_insn);
18548 if (set && rtx_equal_p (SET_DEST (set), reg))
18549 {
18550 rtx note, src, symbol;
18551
18552 /* First see whether the source is a plain symbol. This is used
18553 when calling symbols that are not lazily bound. */
18554 src = SET_SRC (set);
18555 if (GET_CODE (src) == SYMBOL_REF)
18556 return src;
18557
18558 /* Handle %call16 references. */
18559 symbol = mips_strip_unspec_call (src);
18560 if (symbol)
18561 {
18562 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
18563 return symbol;
18564 }
18565
18566 /* If we have something more complicated, look for a
18567 REG_EQUAL or REG_EQUIV note. */
18568 note = find_reg_equal_equiv_note (def_insn);
18569 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
18570 return XEXP (note, 0);
18571
18572 /* Follow at most one simple register copy. Such copies are
18573 interesting in cases like:
18574
18575 for (...)
18576 {
18577 locally_binding_fn (...);
18578 }
18579
18580 and:
18581
18582 locally_binding_fn (...);
18583 ...
18584 locally_binding_fn (...);
18585
18586 where the load of locally_binding_fn can legitimately be
18587 hoisted or shared. However, we do not expect to see complex
18588 chains of copies, so a full worklist solution to the problem
18589 would probably be overkill. */
18590 if (recurse_p && REG_P (src))
18591 return mips_find_pic_call_symbol (def_insn, src, false);
18592 }
18593
18594 return NULL_RTX;
18595 }
18596
18597 /* Find the definition of the use of REG in INSN. See if the definition
18598 is one of the ways we load a register with a symbol address for a
18599 mips_use_pic_fn_addr_reg_p call. If it is return the symbol reference
18600 of the function, otherwise return NULL_RTX. RECURSE_P is as for
18601 mips_pic_call_symbol_from_set. */
18602
18603 static rtx
18604 mips_find_pic_call_symbol (rtx_insn *insn, rtx reg, bool recurse_p)
18605 {
18606 df_ref use;
18607 struct df_link *defs;
18608 rtx symbol;
18609
18610 use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
18611 if (!use)
18612 return NULL_RTX;
18613 defs = DF_REF_CHAIN (use);
18614 if (!defs)
18615 return NULL_RTX;
18616 symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
18617 if (!symbol)
18618 return NULL_RTX;
18619
18620 /* If we have more than one definition, they need to be identical. */
18621 for (defs = defs->next; defs; defs = defs->next)
18622 {
18623 rtx other;
18624
18625 other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
18626 if (!rtx_equal_p (symbol, other))
18627 return NULL_RTX;
18628 }
18629
18630 return symbol;
18631 }
18632
18633 /* Replace the args_size operand of the call expression CALL with the
18634 call-attribute UNSPEC and fill in SYMBOL as the function symbol. */
18635
18636 static void
18637 mips_annotate_pic_call_expr (rtx call, rtx symbol)
18638 {
18639 rtx args_size;
18640
18641 args_size = XEXP (call, 1);
18642 XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
18643 gen_rtvec (2, args_size, symbol),
18644 UNSPEC_CALL_ATTR);
18645 }
18646
18647 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression. See
18648 if instead of the arg_size argument it contains the call attributes. If
18649 yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
18650 symbol from the call attributes. Also return false if ARGS_SIZE_OPNO is
18651 -1. */
18652
18653 bool
18654 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
18655 {
18656 rtx args_size, symbol;
18657
18658 if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
18659 return false;
18660
18661 args_size = operands[args_size_opno];
18662 if (GET_CODE (args_size) != UNSPEC)
18663 return false;
18664 gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
18665
18666 symbol = XVECEXP (args_size, 0, 1);
18667 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
18668
18669 operands[args_size_opno] = symbol;
18670 return true;
18671 }
18672
18673 /* Use DF to annotate PIC indirect calls with the function symbol they
18674 dispatch to. */
18675
18676 static void
18677 mips_annotate_pic_calls (void)
18678 {
18679 basic_block bb;
18680 rtx_insn *insn;
18681
18682 FOR_EACH_BB_FN (bb, cfun)
18683 FOR_BB_INSNS (bb, insn)
18684 {
18685 rtx call, reg, symbol, second_call;
18686
18687 second_call = 0;
18688 call = mips_call_expr_from_insn (insn, &second_call);
18689 if (!call)
18690 continue;
18691 gcc_assert (MEM_P (XEXP (call, 0)));
18692 reg = XEXP (XEXP (call, 0), 0);
18693 if (!REG_P (reg))
18694 continue;
18695
18696 symbol = mips_find_pic_call_symbol (insn, reg, true);
18697 if (symbol)
18698 {
18699 mips_annotate_pic_call_expr (call, symbol);
18700 if (second_call)
18701 mips_annotate_pic_call_expr (second_call, symbol);
18702 }
18703 }
18704 }
18705
18706 /* A temporary variable used by note_uses callbacks, etc. */
18707 static rtx_insn *mips_sim_insn;
18708
18709 /* A structure representing the state of the processor pipeline.
18710 Used by the mips_sim_* family of functions. */
18711 struct mips_sim {
18712 /* The maximum number of instructions that can be issued in a cycle.
18713 (Caches mips_issue_rate.) */
18714 unsigned int issue_rate;
18715
18716 /* The current simulation time. */
18717 unsigned int time;
18718
18719 /* How many more instructions can be issued in the current cycle. */
18720 unsigned int insns_left;
18721
18722 /* LAST_SET[X].INSN is the last instruction to set register X.
18723 LAST_SET[X].TIME is the time at which that instruction was issued.
18724 INSN is null if no instruction has yet set register X. */
18725 struct {
18726 rtx_insn *insn;
18727 unsigned int time;
18728 } last_set[FIRST_PSEUDO_REGISTER];
18729
18730 /* The pipeline's current DFA state. */
18731 state_t dfa_state;
18732 };
18733
18734 /* Reset STATE to the initial simulation state. */
18735
18736 static void
18737 mips_sim_reset (struct mips_sim *state)
18738 {
18739 curr_state = state->dfa_state;
18740
18741 state->time = 0;
18742 state->insns_left = state->issue_rate;
18743 memset (&state->last_set, 0, sizeof (state->last_set));
18744 state_reset (curr_state);
18745
18746 targetm.sched.init (0, false, 0);
18747 advance_state (curr_state);
18748 }
18749
18750 /* Initialize STATE before its first use. DFA_STATE points to an
18751 allocated but uninitialized DFA state. */
18752
18753 static void
18754 mips_sim_init (struct mips_sim *state, state_t dfa_state)
18755 {
18756 if (targetm.sched.init_dfa_pre_cycle_insn)
18757 targetm.sched.init_dfa_pre_cycle_insn ();
18758
18759 if (targetm.sched.init_dfa_post_cycle_insn)
18760 targetm.sched.init_dfa_post_cycle_insn ();
18761
18762 state->issue_rate = mips_issue_rate ();
18763 state->dfa_state = dfa_state;
18764 mips_sim_reset (state);
18765 }
18766
18767 /* Advance STATE by one clock cycle. */
18768
18769 static void
18770 mips_sim_next_cycle (struct mips_sim *state)
18771 {
18772 curr_state = state->dfa_state;
18773
18774 state->time++;
18775 state->insns_left = state->issue_rate;
18776 advance_state (curr_state);
18777 }
18778
18779 /* Advance simulation state STATE until instruction INSN can read
18780 register REG. */
18781
18782 static void
18783 mips_sim_wait_reg (struct mips_sim *state, rtx_insn *insn, rtx reg)
18784 {
18785 unsigned int regno, end_regno;
18786
18787 end_regno = END_REGNO (reg);
18788 for (regno = REGNO (reg); regno < end_regno; regno++)
18789 if (state->last_set[regno].insn != 0)
18790 {
18791 unsigned int t;
18792
18793 t = (state->last_set[regno].time
18794 + insn_latency (state->last_set[regno].insn, insn));
18795 while (state->time < t)
18796 mips_sim_next_cycle (state);
18797 }
18798 }
18799
18800 /* A note_uses callback. For each register in *X, advance simulation
18801 state DATA until mips_sim_insn can read the register's value. */
18802
18803 static void
18804 mips_sim_wait_regs_1 (rtx *x, void *data)
18805 {
18806 subrtx_var_iterator::array_type array;
18807 FOR_EACH_SUBRTX_VAR (iter, array, *x, NONCONST)
18808 if (REG_P (*iter))
18809 mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *iter);
18810 }
18811
18812 /* Advance simulation state STATE until all of INSN's register
18813 dependencies are satisfied. */
18814
18815 static void
18816 mips_sim_wait_regs (struct mips_sim *state, rtx_insn *insn)
18817 {
18818 mips_sim_insn = insn;
18819 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
18820 }
18821
18822 /* Advance simulation state STATE until the units required by
18823 instruction INSN are available. */
18824
18825 static void
18826 mips_sim_wait_units (struct mips_sim *state, rtx_insn *insn)
18827 {
18828 state_t tmp_state;
18829
18830 tmp_state = alloca (state_size ());
18831 while (state->insns_left == 0
18832 || (memcpy (tmp_state, state->dfa_state, state_size ()),
18833 state_transition (tmp_state, insn) >= 0))
18834 mips_sim_next_cycle (state);
18835 }
18836
18837 /* Advance simulation state STATE until INSN is ready to issue. */
18838
18839 static void
18840 mips_sim_wait_insn (struct mips_sim *state, rtx_insn *insn)
18841 {
18842 mips_sim_wait_regs (state, insn);
18843 mips_sim_wait_units (state, insn);
18844 }
18845
18846 /* mips_sim_insn has just set X. Update the LAST_SET array
18847 in simulation state DATA. */
18848
18849 static void
18850 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
18851 {
18852 struct mips_sim *state;
18853
18854 state = (struct mips_sim *) data;
18855 if (REG_P (x))
18856 {
18857 unsigned int regno, end_regno;
18858
18859 end_regno = END_REGNO (x);
18860 for (regno = REGNO (x); regno < end_regno; regno++)
18861 {
18862 state->last_set[regno].insn = mips_sim_insn;
18863 state->last_set[regno].time = state->time;
18864 }
18865 }
18866 }
18867
18868 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
18869 can issue immediately (i.e., that mips_sim_wait_insn has already
18870 been called). */
18871
18872 static void
18873 mips_sim_issue_insn (struct mips_sim *state, rtx_insn *insn)
18874 {
18875 curr_state = state->dfa_state;
18876
18877 state_transition (curr_state, insn);
18878 state->insns_left = targetm.sched.variable_issue (0, false, insn,
18879 state->insns_left);
18880
18881 mips_sim_insn = insn;
18882 note_stores (insn, mips_sim_record_set, state);
18883 }
18884
18885 /* Simulate issuing a NOP in state STATE. */
18886
18887 static void
18888 mips_sim_issue_nop (struct mips_sim *state)
18889 {
18890 if (state->insns_left == 0)
18891 mips_sim_next_cycle (state);
18892 state->insns_left--;
18893 }
18894
18895 /* Update simulation state STATE so that it's ready to accept the instruction
18896 after INSN. INSN should be part of the main rtl chain, not a member of a
18897 SEQUENCE. */
18898
18899 static void
18900 mips_sim_finish_insn (struct mips_sim *state, rtx_insn *insn)
18901 {
18902 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
18903 if (JUMP_P (insn))
18904 mips_sim_issue_nop (state);
18905
18906 switch (GET_CODE (SEQ_BEGIN (insn)))
18907 {
18908 case CODE_LABEL:
18909 case CALL_INSN:
18910 /* We can't predict the processor state after a call or label. */
18911 mips_sim_reset (state);
18912 break;
18913
18914 case JUMP_INSN:
18915 /* The delay slots of branch likely instructions are only executed
18916 when the branch is taken. Therefore, if the caller has simulated
18917 the delay slot instruction, STATE does not really reflect the state
18918 of the pipeline for the instruction after the delay slot. Also,
18919 branch likely instructions tend to incur a penalty when not taken,
18920 so there will probably be an extra delay between the branch and
18921 the instruction after the delay slot. */
18922 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
18923 mips_sim_reset (state);
18924 break;
18925
18926 default:
18927 break;
18928 }
18929 }
18930
18931 /* Use simulator state STATE to calculate the execution time of
18932 instruction sequence SEQ. */
18933
18934 static unsigned int
18935 mips_seq_time (struct mips_sim *state, rtx_insn *seq)
18936 {
18937 mips_sim_reset (state);
18938 for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
18939 {
18940 mips_sim_wait_insn (state, insn);
18941 mips_sim_issue_insn (state, insn);
18942 }
18943 return state->time;
18944 }
18945
18946 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
18947 setting SETTING, using STATE to simulate instruction sequences. */
18948
18949 static unsigned int
18950 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
18951 {
18952 mips_tuning_info.fast_mult_zero_zero_p = setting;
18953 start_sequence ();
18954
18955 machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
18956 rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
18957 mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
18958
18959 /* If the target provides mulsidi3_32bit then that's the most likely
18960 consumer of the result. Test for bypasses. */
18961 if (dword_mode == DImode && HAVE_maddsidi4)
18962 {
18963 rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
18964 emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
18965 }
18966
18967 unsigned int time = mips_seq_time (state, get_insns ());
18968 end_sequence ();
18969 return time;
18970 }
18971
18972 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
18973 and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
18974 Prefer MULT -- which is shorter -- in the event of a tie. */
18975
18976 static void
18977 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
18978 {
18979 if (TARGET_MIPS16 || !ISA_HAS_HILO)
18980 /* No MTLO or MTHI available for MIPS16. Also, when there are no HI or LO
18981 registers then there is no reason to zero them, arbitrarily choose to
18982 say that "MULT $0,$0" would be faster. */
18983 mips_tuning_info.fast_mult_zero_zero_p = true;
18984 else
18985 {
18986 unsigned int true_time = mips_mult_zero_zero_cost (state, true);
18987 unsigned int false_time = mips_mult_zero_zero_cost (state, false);
18988 mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
18989 }
18990 }
18991
18992 /* Set up costs based on the current architecture and tuning settings. */
18993
18994 static void
18995 mips_set_tuning_info (void)
18996 {
18997 if (mips_tuning_info.initialized_p
18998 && mips_tuning_info.arch == mips_arch
18999 && mips_tuning_info.tune == mips_tune
19000 && mips_tuning_info.mips16_p == TARGET_MIPS16)
19001 return;
19002
19003 mips_tuning_info.arch = mips_arch;
19004 mips_tuning_info.tune = mips_tune;
19005 mips_tuning_info.mips16_p = TARGET_MIPS16;
19006 mips_tuning_info.initialized_p = true;
19007
19008 dfa_start ();
19009
19010 struct mips_sim state;
19011 mips_sim_init (&state, alloca (state_size ()));
19012
19013 mips_set_fast_mult_zero_zero_p (&state);
19014
19015 dfa_finish ();
19016 }
19017
19018 /* Implement TARGET_EXPAND_TO_RTL_HOOK. */
19019
19020 static void
19021 mips_expand_to_rtl_hook (void)
19022 {
19023 /* We need to call this at a point where we can safely create sequences
19024 of instructions, so TARGET_OVERRIDE_OPTIONS is too early. We also
19025 need to call it at a point where the DFA infrastructure is not
19026 already in use, so we can't just call it lazily on demand.
19027
19028 At present, mips_tuning_info is only needed during post-expand
19029 RTL passes such as split_insns, so this hook should be early enough.
19030 We may need to move the call elsewhere if mips_tuning_info starts
19031 to be used for other things (such as rtx_costs, or expanders that
19032 could be called during gimple optimization). */
19033 mips_set_tuning_info ();
19034 }
19035
19036 /* The VR4130 pipeline issues aligned pairs of instructions together,
19037 but it stalls the second instruction if it depends on the first.
19038 In order to cut down the amount of logic required, this dependence
19039 check is not based on a full instruction decode. Instead, any non-SPECIAL
19040 instruction is assumed to modify the register specified by bits 20-16
19041 (which is usually the "rt" field).
19042
19043 In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
19044 input, so we can end up with a false dependence between the branch
19045 and its delay slot. If this situation occurs in instruction INSN,
19046 try to avoid it by swapping rs and rt. */
19047
19048 static void
19049 vr4130_avoid_branch_rt_conflict (rtx_insn *insn)
19050 {
19051 rtx_insn *first, *second;
19052
19053 first = SEQ_BEGIN (insn);
19054 second = SEQ_END (insn);
19055 if (JUMP_P (first)
19056 && NONJUMP_INSN_P (second)
19057 && GET_CODE (PATTERN (first)) == SET
19058 && GET_CODE (SET_DEST (PATTERN (first))) == PC
19059 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
19060 {
19061 /* Check for the right kind of condition. */
19062 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
19063 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
19064 && REG_P (XEXP (cond, 0))
19065 && REG_P (XEXP (cond, 1))
19066 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
19067 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
19068 {
19069 /* SECOND mentions the rt register but not the rs register. */
19070 rtx tmp = XEXP (cond, 0);
19071 XEXP (cond, 0) = XEXP (cond, 1);
19072 XEXP (cond, 1) = tmp;
19073 }
19074 }
19075 }
19076
19077 /* Implement -mvr4130-align. Go through each basic block and simulate the
19078 processor pipeline. If we find that a pair of instructions could execute
19079 in parallel, and the first of those instructions is not 8-byte aligned,
19080 insert a nop to make it aligned. */
19081
19082 static void
19083 vr4130_align_insns (void)
19084 {
19085 struct mips_sim state;
19086 rtx_insn *insn, *subinsn, *last, *last2, *next;
19087 bool aligned_p;
19088
19089 dfa_start ();
19090
19091 /* LAST is the last instruction before INSN to have a nonzero length.
19092 LAST2 is the last such instruction before LAST. */
19093 last = 0;
19094 last2 = 0;
19095
19096 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
19097 aligned_p = true;
19098
19099 mips_sim_init (&state, alloca (state_size ()));
19100 for (insn = get_insns (); insn != 0; insn = next)
19101 {
19102 unsigned int length;
19103
19104 next = NEXT_INSN (insn);
19105
19106 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
19107 This isn't really related to the alignment pass, but we do it on
19108 the fly to avoid a separate instruction walk. */
19109 vr4130_avoid_branch_rt_conflict (insn);
19110
19111 length = get_attr_length (insn);
19112 if (length > 0 && USEFUL_INSN_P (insn))
19113 FOR_EACH_SUBINSN (subinsn, insn)
19114 {
19115 mips_sim_wait_insn (&state, subinsn);
19116
19117 /* If we want this instruction to issue in parallel with the
19118 previous one, make sure that the previous instruction is
19119 aligned. There are several reasons why this isn't worthwhile
19120 when the second instruction is a call:
19121
19122 - Calls are less likely to be performance critical,
19123 - There's a good chance that the delay slot can execute
19124 in parallel with the call.
19125 - The return address would then be unaligned.
19126
19127 In general, if we're going to insert a nop between instructions
19128 X and Y, it's better to insert it immediately after X. That
19129 way, if the nop makes Y aligned, it will also align any labels
19130 between X and Y. */
19131 if (state.insns_left != state.issue_rate
19132 && !CALL_P (subinsn))
19133 {
19134 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
19135 {
19136 /* SUBINSN is the first instruction in INSN and INSN is
19137 aligned. We want to align the previous instruction
19138 instead, so insert a nop between LAST2 and LAST.
19139
19140 Note that LAST could be either a single instruction
19141 or a branch with a delay slot. In the latter case,
19142 LAST, like INSN, is already aligned, but the delay
19143 slot must have some extra delay that stops it from
19144 issuing at the same time as the branch. We therefore
19145 insert a nop before the branch in order to align its
19146 delay slot. */
19147 gcc_assert (last2);
19148 emit_insn_after (gen_nop (), last2);
19149 aligned_p = false;
19150 }
19151 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
19152 {
19153 /* SUBINSN is the delay slot of INSN, but INSN is
19154 currently unaligned. Insert a nop between
19155 LAST and INSN to align it. */
19156 gcc_assert (last);
19157 emit_insn_after (gen_nop (), last);
19158 aligned_p = true;
19159 }
19160 }
19161 mips_sim_issue_insn (&state, subinsn);
19162 }
19163 mips_sim_finish_insn (&state, insn);
19164
19165 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
19166 length = get_attr_length (insn);
19167 if (length > 0)
19168 {
19169 /* If the instruction is an asm statement or multi-instruction
19170 mips.md pattern, the length is only an estimate. Insert an
19171 8 byte alignment after it so that the following instructions
19172 can be handled correctly. */
19173 if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
19174 && (recog_memoized (insn) < 0 || length >= 8))
19175 {
19176 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
19177 next = NEXT_INSN (next);
19178 mips_sim_next_cycle (&state);
19179 aligned_p = true;
19180 }
19181 else if (length & 4)
19182 aligned_p = !aligned_p;
19183 last2 = last;
19184 last = insn;
19185 }
19186
19187 /* See whether INSN is an aligned label. */
19188 if (LABEL_P (insn) && label_to_alignment (insn).levels[0].log >= 3)
19189 aligned_p = true;
19190 }
19191 dfa_finish ();
19192 }
19193
19194 /* This structure records that the current function has a LO_SUM
19195 involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
19196 the largest offset applied to BASE by all such LO_SUMs. */
19197 struct mips_lo_sum_offset {
19198 rtx base;
19199 HOST_WIDE_INT offset;
19200 };
19201
19202 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE. */
19203
19204 static hashval_t
19205 mips_hash_base (rtx base)
19206 {
19207 int do_not_record_p;
19208
19209 return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
19210 }
19211
19212 /* Hashtable helpers. */
19213
19214 struct mips_lo_sum_offset_hasher : free_ptr_hash <mips_lo_sum_offset>
19215 {
19216 typedef rtx_def *compare_type;
19217 static inline hashval_t hash (const mips_lo_sum_offset *);
19218 static inline bool equal (const mips_lo_sum_offset *, const rtx_def *);
19219 };
19220
19221 /* Hash-table callbacks for mips_lo_sum_offsets. */
19222
19223 inline hashval_t
19224 mips_lo_sum_offset_hasher::hash (const mips_lo_sum_offset *entry)
19225 {
19226 return mips_hash_base (entry->base);
19227 }
19228
19229 inline bool
19230 mips_lo_sum_offset_hasher::equal (const mips_lo_sum_offset *entry,
19231 const rtx_def *value)
19232 {
19233 return rtx_equal_p (entry->base, value);
19234 }
19235
19236 typedef hash_table<mips_lo_sum_offset_hasher> mips_offset_table;
19237
19238 /* Look up symbolic constant X in HTAB, which is a hash table of
19239 mips_lo_sum_offsets. If OPTION is NO_INSERT, return true if X can be
19240 paired with a recorded LO_SUM, otherwise record X in the table. */
19241
19242 static bool
19243 mips_lo_sum_offset_lookup (mips_offset_table *htab, rtx x,
19244 enum insert_option option)
19245 {
19246 rtx base, offset;
19247 mips_lo_sum_offset **slot;
19248 struct mips_lo_sum_offset *entry;
19249
19250 /* Split X into a base and offset. */
19251 split_const (x, &base, &offset);
19252 if (UNSPEC_ADDRESS_P (base))
19253 base = UNSPEC_ADDRESS (base);
19254
19255 /* Look up the base in the hash table. */
19256 slot = htab->find_slot_with_hash (base, mips_hash_base (base), option);
19257 if (slot == NULL)
19258 return false;
19259
19260 entry = (struct mips_lo_sum_offset *) *slot;
19261 if (option == INSERT)
19262 {
19263 if (entry == NULL)
19264 {
19265 entry = XNEW (struct mips_lo_sum_offset);
19266 entry->base = base;
19267 entry->offset = INTVAL (offset);
19268 *slot = entry;
19269 }
19270 else
19271 {
19272 if (INTVAL (offset) > entry->offset)
19273 entry->offset = INTVAL (offset);
19274 }
19275 }
19276 return INTVAL (offset) <= entry->offset;
19277 }
19278
19279 /* Search X for LO_SUMs and record them in HTAB. */
19280
19281 static void
19282 mips_record_lo_sums (const_rtx x, mips_offset_table *htab)
19283 {
19284 subrtx_iterator::array_type array;
19285 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
19286 if (GET_CODE (*iter) == LO_SUM)
19287 mips_lo_sum_offset_lookup (htab, XEXP (*iter, 1), INSERT);
19288 }
19289
19290 /* Return true if INSN is a SET of an orphaned high-part relocation.
19291 HTAB is a hash table of mips_lo_sum_offsets that describes all the
19292 LO_SUMs in the current function. */
19293
19294 static bool
19295 mips_orphaned_high_part_p (mips_offset_table *htab, rtx_insn *insn)
19296 {
19297 enum mips_symbol_type type;
19298 rtx x, set;
19299
19300 set = single_set (insn);
19301 if (set)
19302 {
19303 /* Check for %his. */
19304 x = SET_SRC (set);
19305 if (GET_CODE (x) == HIGH
19306 && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
19307 return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
19308
19309 /* Check for local %gots (and %got_pages, which is redundant but OK). */
19310 if (GET_CODE (x) == UNSPEC
19311 && XINT (x, 1) == UNSPEC_LOAD_GOT
19312 && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
19313 SYMBOL_CONTEXT_LEA, &type)
19314 && type == SYMBOL_GOTOFF_PAGE)
19315 return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
19316 }
19317 return false;
19318 }
19319
19320 /* Subroutine of mips_avoid_hazard. We classify unconditional branches
19321 of interest for the P6600 for performance reasons. We're interested
19322 in differentiating BALC from JIC, JIALC and BC. */
19323
19324 static enum mips_ucbranch_type
19325 mips_classify_branch_p6600 (rtx_insn *insn)
19326 {
19327 /* We ignore sequences here as they represent a filled delay slot. */
19328 if (!insn
19329 || !USEFUL_INSN_P (insn)
19330 || GET_CODE (PATTERN (insn)) == SEQUENCE)
19331 return UC_UNDEFINED;
19332
19333 if (get_attr_jal (insn) == JAL_INDIRECT /* JIC and JIALC. */
19334 || get_attr_type (insn) == TYPE_JUMP) /* BC. */
19335 return UC_OTHER;
19336
19337 if (CALL_P (insn) && get_attr_jal (insn) == JAL_DIRECT)
19338 return UC_BALC;
19339
19340 return UC_UNDEFINED;
19341 }
19342
19343 /* Subroutine of mips_reorg_process_insns. If there is a hazard between
19344 INSN and a previous instruction, avoid it by inserting nops after
19345 instruction AFTER.
19346
19347 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
19348 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
19349 before using the value of that register. *HILO_DELAY counts the
19350 number of instructions since the last hilo hazard (that is,
19351 the number of instructions since the last MFLO or MFHI).
19352
19353 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
19354 for the next instruction.
19355
19356 LO_REG is an rtx for the LO register, used in dependence checking. */
19357
19358 static void
19359 mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
19360 rtx *delayed_reg, rtx lo_reg, bool *fs_delay)
19361 {
19362 rtx pattern, set;
19363 int nops, ninsns;
19364
19365 pattern = PATTERN (insn);
19366
19367 /* Do not put the whole function in .set noreorder if it contains
19368 an asm statement. We don't know whether there will be hazards
19369 between the asm statement and the gcc-generated code. */
19370 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
19371 cfun->machine->all_noreorder_p = false;
19372
19373 /* Ignore zero-length instructions (barriers and the like). */
19374 ninsns = get_attr_length (insn) / 4;
19375 if (ninsns == 0)
19376 return;
19377
19378 /* Work out how many nops are needed. Note that we only care about
19379 registers that are explicitly mentioned in the instruction's pattern.
19380 It doesn't matter that calls use the argument registers or that they
19381 clobber hi and lo. */
19382 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
19383 nops = 2 - *hilo_delay;
19384 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
19385 nops = 1;
19386 /* If processing a forbidden slot hazard then a NOP is required if the
19387 branch instruction was not in a sequence (as the sequence would
19388 imply it is not actually a compact branch anyway) and the current
19389 insn is not an inline asm, and can't go in a delay slot. */
19390 else if (*fs_delay && get_attr_can_delay (insn) == CAN_DELAY_NO
19391 && GET_CODE (PATTERN (after)) != SEQUENCE
19392 && GET_CODE (pattern) != ASM_INPUT
19393 && asm_noperands (pattern) < 0)
19394 nops = 1;
19395 /* The P6600's branch predictor can handle static sequences of back-to-back
19396 branches in the following cases:
19397
19398 (1) BALC followed by any conditional compact branch
19399 (2) BALC followed by BALC
19400
19401 Any other combinations of compact branches will incur performance
19402 penalty. Inserting a no-op only costs space as the dispatch unit will
19403 disregard the nop. */
19404 else if (TUNE_P6600 && TARGET_CB_MAYBE && !optimize_size
19405 && ((mips_classify_branch_p6600 (after) == UC_BALC
19406 && mips_classify_branch_p6600 (insn) == UC_OTHER)
19407 || (mips_classify_branch_p6600 (insn) == UC_BALC
19408 && mips_classify_branch_p6600 (after) == UC_OTHER)))
19409 nops = 1;
19410 else
19411 nops = 0;
19412
19413 /* Insert the nops between this instruction and the previous one.
19414 Each new nop takes us further from the last hilo hazard. */
19415 *hilo_delay += nops;
19416
19417 /* Move to the next real instruction if we are inserting a NOP and this
19418 instruction is a call with debug information. The reason being that
19419 we can't separate the call from the debug info. */
19420 rtx_insn *real_after = after;
19421 if (real_after && nops && CALL_P (real_after))
19422 while (real_after
19423 && (NOTE_P (NEXT_INSN (real_after))
19424 || BARRIER_P (NEXT_INSN (real_after))))
19425 real_after = NEXT_INSN (real_after);
19426
19427 while (nops-- > 0)
19428 emit_insn_after (gen_hazard_nop (), real_after);
19429
19430 /* Set up the state for the next instruction. */
19431 *hilo_delay += ninsns;
19432 *delayed_reg = 0;
19433 *fs_delay = false;
19434 if (INSN_CODE (insn) >= 0)
19435 switch (get_attr_hazard (insn))
19436 {
19437 case HAZARD_NONE:
19438 /* For the P6600, flag some unconditional branches as having a
19439 pseudo-forbidden slot. This will cause additional nop insertion
19440 or SEQUENCE breaking as required. This is for performance
19441 reasons not correctness. */
19442 if (TUNE_P6600
19443 && !optimize_size
19444 && TARGET_CB_MAYBE
19445 && mips_classify_branch_p6600 (insn) == UC_OTHER)
19446 *fs_delay = true;
19447 break;
19448
19449 case HAZARD_FORBIDDEN_SLOT:
19450 if (TARGET_CB_MAYBE)
19451 *fs_delay = true;
19452 break;
19453
19454 case HAZARD_HILO:
19455 *hilo_delay = 0;
19456 break;
19457
19458 case HAZARD_DELAY:
19459 set = single_set (insn);
19460 gcc_assert (set);
19461 *delayed_reg = SET_DEST (set);
19462 break;
19463 }
19464 }
19465
19466 /* Emit a speculation barrier.
19467 JR.HB is needed, so we put speculation_barrier_libfunc in libgcc. */
19468 void
19469 mips_emit_speculation_barrier_function ()
19470 {
19471 emit_library_call (speculation_barrier_libfunc, LCT_NORMAL, VOIDmode);
19472 }
19473
19474 /* A SEQUENCE is breakable iff the branch inside it has a compact form
19475 and the target has compact branches. */
19476
19477 static bool
19478 mips_breakable_sequence_p (rtx_insn *insn)
19479 {
19480 return (insn && GET_CODE (PATTERN (insn)) == SEQUENCE
19481 && TARGET_CB_MAYBE
19482 && get_attr_compact_form (SEQ_BEGIN (insn)) != COMPACT_FORM_NEVER);
19483 }
19484
19485 /* Remove a SEQUENCE and replace it with the delay slot instruction
19486 followed by the branch and return the instruction in the delay slot.
19487 Return the first of the two new instructions.
19488 Subroutine of mips_reorg_process_insns. */
19489
19490 static rtx_insn *
19491 mips_break_sequence (rtx_insn *insn)
19492 {
19493 rtx_insn *before = PREV_INSN (insn);
19494 rtx_insn *branch = SEQ_BEGIN (insn);
19495 rtx_insn *ds = SEQ_END (insn);
19496 remove_insn (insn);
19497 add_insn_after (ds, before, NULL);
19498 add_insn_after (branch, ds, NULL);
19499 return ds;
19500 }
19501
19502 /* Go through the instruction stream and insert nops where necessary.
19503 Also delete any high-part relocations whose partnering low parts
19504 are now all dead. See if the whole function can then be put into
19505 .set noreorder and .set nomacro. */
19506
19507 static void
19508 mips_reorg_process_insns (void)
19509 {
19510 rtx_insn *insn, *last_insn, *subinsn, *next_insn;
19511 rtx lo_reg, delayed_reg;
19512 int hilo_delay;
19513 bool fs_delay;
19514
19515 /* Force all instructions to be split into their final form. */
19516 split_all_insns_noflow ();
19517
19518 /* Recalculate instruction lengths without taking nops into account. */
19519 cfun->machine->ignore_hazard_length_p = true;
19520 shorten_branches (get_insns ());
19521
19522 cfun->machine->all_noreorder_p = true;
19523
19524 /* We don't track MIPS16 PC-relative offsets closely enough to make
19525 a good job of "set .noreorder" code in MIPS16 mode. */
19526 if (TARGET_MIPS16)
19527 cfun->machine->all_noreorder_p = false;
19528
19529 /* Code that doesn't use explicit relocs can't be ".set nomacro". */
19530 if (!TARGET_EXPLICIT_RELOCS)
19531 cfun->machine->all_noreorder_p = false;
19532
19533 /* Profiled functions can't be all noreorder because the profiler
19534 support uses assembler macros. */
19535 if (crtl->profile)
19536 cfun->machine->all_noreorder_p = false;
19537
19538 /* Code compiled with -mfix-vr4120, -mfix-r5900, -mfix-rm7000 or
19539 -mfix-24k can't be all noreorder because we rely on the assembler
19540 to work around some errata. The R5900 target has several bugs. */
19541 if (TARGET_FIX_VR4120
19542 || TARGET_FIX_RM7000
19543 || TARGET_FIX_24K
19544 || TARGET_FIX_R5900)
19545 cfun->machine->all_noreorder_p = false;
19546
19547 /* The same is true for -mfix-vr4130 if we might generate MFLO or
19548 MFHI instructions. Note that we avoid using MFLO and MFHI if
19549 the VR4130 MACC and DMACC instructions are available instead;
19550 see the *mfhilo_{si,di}_macc patterns. */
19551 if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
19552 cfun->machine->all_noreorder_p = false;
19553
19554 mips_offset_table htab (37);
19555
19556 /* Make a first pass over the instructions, recording all the LO_SUMs. */
19557 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
19558 FOR_EACH_SUBINSN (subinsn, insn)
19559 if (USEFUL_INSN_P (subinsn))
19560 {
19561 rtx body = PATTERN (insn);
19562 int noperands = asm_noperands (body);
19563 if (noperands >= 0)
19564 {
19565 rtx *ops = XALLOCAVEC (rtx, noperands);
19566 bool *used = XALLOCAVEC (bool, noperands);
19567 const char *string = decode_asm_operands (body, ops, NULL, NULL,
19568 NULL, NULL);
19569 get_referenced_operands (string, used, noperands);
19570 for (int i = 0; i < noperands; ++i)
19571 if (used[i])
19572 mips_record_lo_sums (ops[i], &htab);
19573 }
19574 else
19575 mips_record_lo_sums (PATTERN (subinsn), &htab);
19576 }
19577
19578 last_insn = 0;
19579 hilo_delay = 2;
19580 delayed_reg = 0;
19581 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
19582 fs_delay = false;
19583
19584 /* Make a second pass over the instructions. Delete orphaned
19585 high-part relocations or turn them into NOPs. Avoid hazards
19586 by inserting NOPs. */
19587 for (insn = get_insns (); insn != 0; insn = next_insn)
19588 {
19589 next_insn = NEXT_INSN (insn);
19590 if (USEFUL_INSN_P (insn))
19591 {
19592 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
19593 {
19594 rtx_insn *next_active = next_active_insn (insn);
19595 /* Undo delay slots to avoid bubbles if the next instruction can
19596 be placed in a forbidden slot or the cost of adding an
19597 explicit NOP in a forbidden slot is OK and if the SEQUENCE is
19598 safely breakable. */
19599 if (TARGET_CB_MAYBE
19600 && mips_breakable_sequence_p (insn)
19601 && INSN_P (SEQ_BEGIN (insn))
19602 && INSN_P (SEQ_END (insn))
19603 && ((next_active
19604 && INSN_P (next_active)
19605 && GET_CODE (PATTERN (next_active)) != SEQUENCE
19606 && get_attr_can_delay (next_active) == CAN_DELAY_YES)
19607 || !optimize_size))
19608 {
19609 /* To hide a potential pipeline bubble, if we scan backwards
19610 from the current SEQUENCE and find that there is a load
19611 of a value that is used in the CTI and there are no
19612 dependencies between the CTI and instruction in the delay
19613 slot, break the sequence so the load delay is hidden. */
19614 HARD_REG_SET uses;
19615 CLEAR_HARD_REG_SET (uses);
19616 note_uses (&PATTERN (SEQ_BEGIN (insn)), record_hard_reg_uses,
19617 &uses);
19618 HARD_REG_SET delay_sets;
19619 CLEAR_HARD_REG_SET (delay_sets);
19620 note_stores (SEQ_END (insn), record_hard_reg_sets,
19621 &delay_sets);
19622
19623 rtx_insn *prev = prev_active_insn (insn);
19624 if (prev
19625 && GET_CODE (PATTERN (prev)) == SET
19626 && MEM_P (SET_SRC (PATTERN (prev))))
19627 {
19628 HARD_REG_SET sets;
19629 CLEAR_HARD_REG_SET (sets);
19630 note_stores (prev, record_hard_reg_sets, &sets);
19631
19632 /* Re-order if safe. */
19633 if (!hard_reg_set_intersect_p (delay_sets, uses)
19634 && hard_reg_set_intersect_p (uses, sets))
19635 {
19636 next_insn = mips_break_sequence (insn);
19637 /* Need to process the hazards of the newly
19638 introduced instructions. */
19639 continue;
19640 }
19641 }
19642
19643 /* If we find an orphaned high-part relocation in a delay
19644 slot then we can convert to a compact branch and get
19645 the orphaned high part deleted. */
19646 if (mips_orphaned_high_part_p (&htab, SEQ_END (insn)))
19647 {
19648 next_insn = mips_break_sequence (insn);
19649 /* Need to process the hazards of the newly
19650 introduced instructions. */
19651 continue;
19652 }
19653 }
19654
19655 /* If we find an orphaned high-part relocation in a delay
19656 slot, it's easier to turn that instruction into a NOP than
19657 to delete it. The delay slot will be a NOP either way. */
19658 FOR_EACH_SUBINSN (subinsn, insn)
19659 if (INSN_P (subinsn))
19660 {
19661 if (mips_orphaned_high_part_p (&htab, subinsn))
19662 {
19663 PATTERN (subinsn) = gen_nop ();
19664 INSN_CODE (subinsn) = CODE_FOR_nop;
19665 }
19666 mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
19667 &delayed_reg, lo_reg, &fs_delay);
19668 }
19669 last_insn = insn;
19670 }
19671 else
19672 {
19673 /* INSN is a single instruction. Delete it if it's an
19674 orphaned high-part relocation. */
19675 if (mips_orphaned_high_part_p (&htab, insn))
19676 delete_insn (insn);
19677 /* Also delete cache barriers if the last instruction
19678 was an annulled branch. INSN will not be speculatively
19679 executed. */
19680 else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
19681 && last_insn
19682 && JUMP_P (SEQ_BEGIN (last_insn))
19683 && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
19684 delete_insn (insn);
19685 else
19686 {
19687 mips_avoid_hazard (last_insn, insn, &hilo_delay,
19688 &delayed_reg, lo_reg, &fs_delay);
19689 /* When a compact branch introduces a forbidden slot hazard
19690 and the next useful instruction is a SEQUENCE of a jump
19691 and a non-nop instruction in the delay slot, remove the
19692 sequence and replace it with the delay slot instruction
19693 then the jump to clear the forbidden slot hazard.
19694
19695 For the P6600, this optimisation solves the performance
19696 penalty associated with BALC followed by a delay slot
19697 branch. We do not set fs_delay as we do not want
19698 the full logic of a forbidden slot; the penalty exists
19699 only against branches not the full class of forbidden
19700 slot instructions. */
19701
19702 if (fs_delay || (TUNE_P6600
19703 && TARGET_CB_MAYBE
19704 && mips_classify_branch_p6600 (insn)
19705 == UC_BALC))
19706 {
19707 /* Search onwards from the current position looking for
19708 a SEQUENCE. We are looking for pipeline hazards here
19709 and do not need to worry about labels or barriers as
19710 the optimization only undoes delay slot filling which
19711 only affects the order of the branch and its delay
19712 slot. */
19713 rtx_insn *next = next_active_insn (insn);
19714 if (next
19715 && USEFUL_INSN_P (next)
19716 && GET_CODE (PATTERN (next)) == SEQUENCE
19717 && mips_breakable_sequence_p (next))
19718 {
19719 last_insn = insn;
19720 next_insn = mips_break_sequence (next);
19721 /* Need to process the hazards of the newly
19722 introduced instructions. */
19723 continue;
19724 }
19725 }
19726 last_insn = insn;
19727 }
19728 }
19729 }
19730 }
19731 }
19732
19733 /* Return true if the function has a long branch instruction. */
19734
19735 static bool
19736 mips_has_long_branch_p (void)
19737 {
19738 rtx_insn *insn, *subinsn;
19739 int normal_length;
19740
19741 /* We need up-to-date instruction lengths. */
19742 shorten_branches (get_insns ());
19743
19744 /* Look for a branch that is longer than normal. The normal length for
19745 non-MIPS16 branches is 8, because the length includes the delay slot.
19746 It is 4 for MIPS16, because MIPS16 branches are extended instructions,
19747 but they have no delay slot. */
19748 normal_length = (TARGET_MIPS16 ? 4 : 8);
19749 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19750 FOR_EACH_SUBINSN (subinsn, insn)
19751 if (JUMP_P (subinsn)
19752 && get_attr_length (subinsn) > normal_length
19753 && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
19754 return true;
19755
19756 return false;
19757 }
19758
19759 /* If we are using a GOT, but have not decided to use a global pointer yet,
19760 see whether we need one to implement long branches. Convert the ghost
19761 global-pointer instructions into real ones if so. */
19762
19763 static bool
19764 mips_expand_ghost_gp_insns (void)
19765 {
19766 /* Quick exit if we already know that we will or won't need a
19767 global pointer. */
19768 if (!TARGET_USE_GOT
19769 || cfun->machine->global_pointer == INVALID_REGNUM
19770 || mips_must_initialize_gp_p ())
19771 return false;
19772
19773 /* Run a full check for long branches. */
19774 if (!mips_has_long_branch_p ())
19775 return false;
19776
19777 /* We've now established that we need $gp. */
19778 cfun->machine->must_initialize_gp_p = true;
19779 split_all_insns_noflow ();
19780
19781 return true;
19782 }
19783
19784 /* Subroutine of mips_reorg to manage passes that require DF. */
19785
19786 static void
19787 mips_df_reorg (void)
19788 {
19789 /* Create def-use chains. */
19790 df_set_flags (DF_EQ_NOTES);
19791 df_chain_add_problem (DF_UD_CHAIN);
19792 df_analyze ();
19793
19794 if (TARGET_RELAX_PIC_CALLS)
19795 mips_annotate_pic_calls ();
19796
19797 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
19798 r10k_insert_cache_barriers ();
19799
19800 df_finish_pass (false);
19801 }
19802
19803 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST. This is
19804 called very late in mips_reorg, but the caller is required to run
19805 mips16_lay_out_constants on the result. */
19806
19807 static void
19808 mips16_load_branch_target (rtx dest, rtx src)
19809 {
19810 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
19811 {
19812 rtx page, low;
19813
19814 if (mips_cfun_has_cprestore_slot_p ())
19815 mips_emit_move (dest, mips_cprestore_slot (dest, true));
19816 else
19817 mips_emit_move (dest, pic_offset_table_rtx);
19818 page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
19819 low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
19820 emit_insn (gen_rtx_SET (dest,
19821 PMODE_INSN (gen_unspec_got, (dest, page))));
19822 emit_insn (gen_rtx_SET (dest, gen_rtx_LO_SUM (Pmode, dest, low)));
19823 }
19824 else
19825 {
19826 src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
19827 mips_emit_move (dest, src);
19828 }
19829 }
19830
19831 /* If we're compiling a MIPS16 function, look for and split any long branches.
19832 This must be called after all other instruction modifications in
19833 mips_reorg. */
19834
19835 static void
19836 mips16_split_long_branches (void)
19837 {
19838 bool something_changed;
19839
19840 if (!TARGET_MIPS16)
19841 return;
19842
19843 /* Loop until the alignments for all targets are sufficient. */
19844 do
19845 {
19846 rtx_insn *insn;
19847 rtx_jump_insn *jump_insn;
19848
19849 shorten_branches (get_insns ());
19850 something_changed = false;
19851 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19852 if ((jump_insn = dyn_cast <rtx_jump_insn *> (insn))
19853 && get_attr_length (jump_insn) > 4
19854 && (any_condjump_p (jump_insn) || any_uncondjump_p (jump_insn)))
19855 {
19856 rtx old_label, temp, saved_temp;
19857 rtx_code_label *new_label;
19858 rtx target;
19859 rtx_insn *jump, *jump_sequence;
19860
19861 start_sequence ();
19862
19863 /* Free up a MIPS16 register by saving it in $1. */
19864 saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
19865 temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
19866 emit_move_insn (saved_temp, temp);
19867
19868 /* Load the branch target into TEMP. */
19869 old_label = JUMP_LABEL (jump_insn);
19870 target = gen_rtx_LABEL_REF (Pmode, old_label);
19871 mips16_load_branch_target (temp, target);
19872
19873 /* Jump to the target and restore the register's
19874 original value. */
19875 jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
19876 (temp, temp, saved_temp)));
19877 JUMP_LABEL (jump) = old_label;
19878 LABEL_NUSES (old_label)++;
19879
19880 /* Rewrite any symbolic references that are supposed to use
19881 a PC-relative constant pool. */
19882 mips16_lay_out_constants (false);
19883
19884 if (simplejump_p (jump_insn))
19885 /* We're going to replace INSN with a longer form. */
19886 new_label = NULL;
19887 else
19888 {
19889 /* Create a branch-around label for the original
19890 instruction. */
19891 new_label = gen_label_rtx ();
19892 emit_label (new_label);
19893 }
19894
19895 jump_sequence = get_insns ();
19896 end_sequence ();
19897
19898 emit_insn_after (jump_sequence, jump_insn);
19899 if (new_label)
19900 invert_jump (jump_insn, new_label, false);
19901 else
19902 delete_insn (jump_insn);
19903 something_changed = true;
19904 }
19905 }
19906 while (something_changed);
19907 }
19908
19909 /* Insert a `.insn' assembly pseudo-op after any labels followed by
19910 a MIPS16 constant pool or no insn at all. This is needed so that
19911 targets that have been optimized away are still marked as code
19912 and therefore branches that remained and point to them are known
19913 to retain the ISA mode and as such can be successfully assembled. */
19914
19915 static void
19916 mips_insert_insn_pseudos (void)
19917 {
19918 bool insn_pseudo_needed = TRUE;
19919 rtx_insn *insn;
19920
19921 for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
19922 switch (GET_CODE (insn))
19923 {
19924 case INSN:
19925 if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
19926 && XINT (PATTERN (insn), 1) == UNSPEC_CONSTTABLE)
19927 {
19928 insn_pseudo_needed = TRUE;
19929 break;
19930 }
19931 /* Fall through. */
19932 case JUMP_INSN:
19933 case CALL_INSN:
19934 case JUMP_TABLE_DATA:
19935 insn_pseudo_needed = FALSE;
19936 break;
19937 case CODE_LABEL:
19938 if (insn_pseudo_needed)
19939 {
19940 emit_insn_after (gen_insn_pseudo (), insn);
19941 insn_pseudo_needed = FALSE;
19942 }
19943 break;
19944 default:
19945 break;
19946 }
19947 }
19948
19949 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
19950
19951 static void
19952 mips_reorg (void)
19953 {
19954 /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF. Also during
19955 insn splitting in mips16_lay_out_constants, DF insn info is only kept up
19956 to date if the CFG is available. */
19957 if (mips_cfg_in_reorg ())
19958 compute_bb_for_insn ();
19959 mips16_lay_out_constants (true);
19960 if (mips_cfg_in_reorg ())
19961 {
19962 mips_df_reorg ();
19963 free_bb_for_insn ();
19964 }
19965 }
19966
19967 /* We use a machine specific pass to do a second machine dependent reorg
19968 pass after delay branch scheduling. */
19969
19970 static unsigned int
19971 mips_machine_reorg2 (void)
19972 {
19973 mips_reorg_process_insns ();
19974 if (!TARGET_MIPS16
19975 && TARGET_EXPLICIT_RELOCS
19976 && TUNE_MIPS4130
19977 && TARGET_VR4130_ALIGN)
19978 vr4130_align_insns ();
19979 if (mips_expand_ghost_gp_insns ())
19980 /* The expansion could invalidate some of the VR4130 alignment
19981 optimizations, but this should be an extremely rare case anyhow. */
19982 mips_reorg_process_insns ();
19983 mips16_split_long_branches ();
19984 mips_insert_insn_pseudos ();
19985 return 0;
19986 }
19987
19988 namespace {
19989
19990 const pass_data pass_data_mips_machine_reorg2 =
19991 {
19992 RTL_PASS, /* type */
19993 "mach2", /* name */
19994 OPTGROUP_NONE, /* optinfo_flags */
19995 TV_MACH_DEP, /* tv_id */
19996 0, /* properties_required */
19997 0, /* properties_provided */
19998 0, /* properties_destroyed */
19999 0, /* todo_flags_start */
20000 0, /* todo_flags_finish */
20001 };
20002
20003 class pass_mips_machine_reorg2 : public rtl_opt_pass
20004 {
20005 public:
20006 pass_mips_machine_reorg2(gcc::context *ctxt)
20007 : rtl_opt_pass(pass_data_mips_machine_reorg2, ctxt)
20008 {}
20009
20010 /* opt_pass methods: */
20011 virtual unsigned int execute (function *) { return mips_machine_reorg2 (); }
20012
20013 }; // class pass_mips_machine_reorg2
20014
20015 } // anon namespace
20016
20017 rtl_opt_pass *
20018 make_pass_mips_machine_reorg2 (gcc::context *ctxt)
20019 {
20020 return new pass_mips_machine_reorg2 (ctxt);
20021 }
20022
20023
20024 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
20025 in order to avoid duplicating too much logic from elsewhere. */
20026
20027 static void
20028 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
20029 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
20030 tree function)
20031 {
20032 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
20033 rtx this_rtx, temp1, temp2, fnaddr;
20034 rtx_insn *insn;
20035 bool use_sibcall_p;
20036
20037 /* Pretend to be a post-reload pass while generating rtl. */
20038 reload_completed = 1;
20039
20040 /* Mark the end of the (empty) prologue. */
20041 emit_note (NOTE_INSN_PROLOGUE_END);
20042
20043 /* Determine if we can use a sibcall to call FUNCTION directly. */
20044 fnaddr = XEXP (DECL_RTL (function), 0);
20045 use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
20046 && const_call_insn_operand (fnaddr, Pmode));
20047
20048 /* Determine if we need to load FNADDR from the GOT. */
20049 if (!use_sibcall_p
20050 && (mips_got_symbol_type_p
20051 (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
20052 {
20053 /* Pick a global pointer. Use a call-clobbered register if
20054 TARGET_CALL_SAVED_GP. */
20055 cfun->machine->global_pointer
20056 = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
20057 cfun->machine->must_initialize_gp_p = true;
20058 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
20059
20060 /* Set up the global pointer for n32 or n64 abicalls. */
20061 mips_emit_loadgp ();
20062 }
20063
20064 /* We need two temporary registers in some cases. */
20065 temp1 = gen_rtx_REG (Pmode, 2);
20066 temp2 = gen_rtx_REG (Pmode, 3);
20067
20068 /* Find out which register contains the "this" pointer. */
20069 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
20070 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
20071 else
20072 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
20073
20074 /* Add DELTA to THIS_RTX. */
20075 if (delta != 0)
20076 {
20077 rtx offset = GEN_INT (delta);
20078 if (!SMALL_OPERAND (delta))
20079 {
20080 mips_emit_move (temp1, offset);
20081 offset = temp1;
20082 }
20083 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
20084 }
20085
20086 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
20087 if (vcall_offset != 0)
20088 {
20089 rtx addr;
20090
20091 /* Set TEMP1 to *THIS_RTX. */
20092 mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
20093
20094 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
20095 addr = mips_add_offset (temp2, temp1, vcall_offset);
20096
20097 /* Load the offset and add it to THIS_RTX. */
20098 mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
20099 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
20100 }
20101
20102 /* Jump to the target function. Use a sibcall if direct jumps are
20103 allowed, otherwise load the address into a register first. */
20104 if (use_sibcall_p)
20105 {
20106 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
20107 SIBLING_CALL_P (insn) = 1;
20108 }
20109 else
20110 {
20111 /* This is messy. GAS treats "la $25,foo" as part of a call
20112 sequence and may allow a global "foo" to be lazily bound.
20113 The general move patterns therefore reject this combination.
20114
20115 In this context, lazy binding would actually be OK
20116 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
20117 TARGET_CALL_SAVED_GP; see mips_load_call_address.
20118 We must therefore load the address via a temporary
20119 register if mips_dangerous_for_la25_p.
20120
20121 If we jump to the temporary register rather than $25,
20122 the assembler can use the move insn to fill the jump's
20123 delay slot.
20124
20125 We can use the same technique for MIPS16 code, where $25
20126 is not a valid JR register. */
20127 if (TARGET_USE_PIC_FN_ADDR_REG
20128 && !TARGET_MIPS16
20129 && !mips_dangerous_for_la25_p (fnaddr))
20130 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
20131 mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
20132
20133 if (TARGET_USE_PIC_FN_ADDR_REG
20134 && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
20135 mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
20136 emit_jump_insn (gen_indirect_jump (temp1));
20137 }
20138
20139 /* Run just enough of rest_of_compilation. This sequence was
20140 "borrowed" from alpha.cc. */
20141 insn = get_insns ();
20142 split_all_insns_noflow ();
20143 mips16_lay_out_constants (true);
20144 shorten_branches (insn);
20145 assemble_start_function (thunk_fndecl, fnname);
20146 final_start_function (insn, file, 1);
20147 final (insn, file, 1);
20148 final_end_function ();
20149 assemble_end_function (thunk_fndecl, fnname);
20150
20151 /* Clean up the vars set above. Note that final_end_function resets
20152 the global pointer for us. */
20153 reload_completed = 0;
20154 }
20155
20156
20157 /* The last argument passed to mips_set_compression_mode,
20158 or negative if the function hasn't been called yet. */
20159 static unsigned int old_compression_mode = -1;
20160
20161 /* Set up the target-dependent global state for ISA mode COMPRESSION_MODE,
20162 which is either MASK_MIPS16 or MASK_MICROMIPS. */
20163
20164 static void
20165 mips_set_compression_mode (unsigned int compression_mode)
20166 {
20167
20168 if (compression_mode == old_compression_mode)
20169 return;
20170
20171 /* Restore base settings of various flags. */
20172 target_flags = mips_base_target_flags;
20173 flag_schedule_insns = mips_base_schedule_insns;
20174 flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
20175 flag_move_loop_invariants = mips_base_move_loop_invariants;
20176 str_align_loops = mips_base_align_loops;
20177 str_align_jumps = mips_base_align_jumps;
20178 str_align_functions = mips_base_align_functions;
20179 target_flags &= ~(MASK_MIPS16 | MASK_MICROMIPS);
20180 target_flags |= compression_mode;
20181
20182 if (compression_mode & MASK_MIPS16)
20183 {
20184 /* Switch to MIPS16 mode. */
20185 target_flags |= MASK_MIPS16;
20186
20187 /* Turn off SYNCI if it was on, MIPS16 doesn't support it. */
20188 target_flags &= ~MASK_SYNCI;
20189
20190 /* Don't run the scheduler before reload, since it tends to
20191 increase register pressure. */
20192 flag_schedule_insns = 0;
20193
20194 /* Don't do hot/cold partitioning. mips16_lay_out_constants expects
20195 the whole function to be in a single section. */
20196 flag_reorder_blocks_and_partition = 0;
20197
20198 /* Don't move loop invariants, because it tends to increase
20199 register pressure. It also introduces an extra move in cases
20200 where the constant is the first operand in a two-operand binary
20201 instruction, or when it forms a register argument to a functon
20202 call. */
20203 flag_move_loop_invariants = 0;
20204
20205 /* Experiments suggest we get the best overall section-anchor
20206 results from using the range of an unextended LW or SW. Code
20207 that makes heavy use of byte or short accesses can do better
20208 with ranges of 0...31 and 0...63 respectively, but most code is
20209 sensitive to the range of LW and SW instead. */
20210 targetm.min_anchor_offset = 0;
20211 targetm.max_anchor_offset = 127;
20212
20213 targetm.const_anchor = 0;
20214
20215 /* MIPS16 has no BAL instruction. */
20216 target_flags &= ~MASK_RELAX_PIC_CALLS;
20217
20218 /* The R4000 errata don't apply to any known MIPS16 cores.
20219 It's simpler to make the R4000 fixes and MIPS16 mode
20220 mutually exclusive. */
20221 target_flags &= ~MASK_FIX_R4000;
20222
20223 if (flag_pic && !TARGET_OLDABI)
20224 sorry ("MIPS16 PIC for ABIs other than o32 and o64");
20225
20226 if (TARGET_XGOT)
20227 sorry ("MIPS16 %<-mxgot%> code");
20228
20229 if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
20230 sorry ("%<hard-float%> MIPS16 code for ABIs other than o32 and o64");
20231
20232 if (TARGET_MSA)
20233 sorry ("MSA MIPS16 code");
20234
20235 if (!TARGET_EXPLICIT_RELOCS)
20236 sorry ("MIPS16 requires %<-mexplicit-relocs%>");
20237 }
20238 else
20239 {
20240 /* Switch to microMIPS or the standard encoding. */
20241
20242 if (TARGET_MICROMIPS)
20243 /* Avoid branch likely. */
20244 target_flags &= ~MASK_BRANCHLIKELY;
20245
20246 /* Provide default values for align_* for 64-bit targets. */
20247 if (TARGET_64BIT)
20248 {
20249 if (flag_align_loops && !str_align_loops)
20250 str_align_loops = "8";
20251 if (flag_align_jumps && !str_align_jumps)
20252 str_align_jumps = "8";
20253 if (flag_align_functions && !str_align_functions)
20254 str_align_functions = "8";
20255 }
20256
20257 targetm.min_anchor_offset = -32768;
20258 targetm.max_anchor_offset = 32767;
20259
20260 targetm.const_anchor = 0x8000;
20261 }
20262
20263 /* (Re)initialize MIPS target internals for new ISA. */
20264 mips_init_relocs ();
20265
20266 if (compression_mode & MASK_MIPS16)
20267 {
20268 if (!mips16_globals)
20269 mips16_globals = save_target_globals_default_opts ();
20270 else
20271 restore_target_globals (mips16_globals);
20272 }
20273 else if (compression_mode & MASK_MICROMIPS)
20274 {
20275 if (!micromips_globals)
20276 micromips_globals = save_target_globals_default_opts ();
20277 else
20278 restore_target_globals (micromips_globals);
20279 }
20280 else
20281 restore_target_globals (&default_target_globals);
20282
20283 old_compression_mode = compression_mode;
20284 }
20285
20286 /* Implement TARGET_SET_CURRENT_FUNCTION. Decide whether the current
20287 function should use the MIPS16 or microMIPS ISA and switch modes
20288 accordingly. Also set the current code_readable mode. */
20289
20290 static void
20291 mips_set_current_function (tree fndecl)
20292 {
20293 enum mips_code_readable_setting old_code_readable = mips_code_readable;
20294
20295 mips_set_compression_mode (mips_get_compress_mode (fndecl));
20296
20297 mips_code_readable = mips_get_code_readable_attr (fndecl);
20298
20299 /* Since the mips_code_readable setting has potentially changed, the
20300 relocation tables must be reinitialized. Otherwise GCC will not
20301 split symbols for functions that are code_readable ("no") when others
20302 are code_readable ("yes") and ICE later on in places such as
20303 mips_emit_move. Ditto for similar paired cases. It must be restored
20304 to its previous state as well. */
20305 if (old_code_readable != mips_code_readable)
20306 mips_init_relocs ();
20307 }
20308
20309 /* Allocate a chunk of memory for per-function machine-dependent data. */
20310
20311 static struct machine_function *
20312 mips_init_machine_status (void)
20313 {
20314 return ggc_cleared_alloc<machine_function> ();
20315 }
20316
20317 /* Return the processor associated with the given ISA level, or null
20318 if the ISA isn't valid. */
20319
20320 static const struct mips_cpu_info *
20321 mips_cpu_info_from_isa (int isa)
20322 {
20323 unsigned int i;
20324
20325 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
20326 if (mips_cpu_info_table[i].isa == isa)
20327 return mips_cpu_info_table + i;
20328
20329 return NULL;
20330 }
20331
20332 /* Return a mips_cpu_info entry determined by an option valued
20333 OPT. */
20334
20335 static const struct mips_cpu_info *
20336 mips_cpu_info_from_opt (int opt)
20337 {
20338 switch (opt)
20339 {
20340 case MIPS_ARCH_OPTION_FROM_ABI:
20341 /* 'from-abi' selects the most compatible architecture for the
20342 given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
20343 ABIs. For the EABIs, we have to decide whether we're using
20344 the 32-bit or 64-bit version. */
20345 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
20346 : ABI_NEEDS_64BIT_REGS ? 3
20347 : (TARGET_64BIT ? 3 : 1));
20348
20349 case MIPS_ARCH_OPTION_NATIVE:
20350 gcc_unreachable ();
20351
20352 default:
20353 return &mips_cpu_info_table[opt];
20354 }
20355 }
20356
20357 /* Return a default mips_cpu_info entry, given that no -march= option
20358 was explicitly specified. */
20359
20360 static const struct mips_cpu_info *
20361 mips_default_arch (void)
20362 {
20363 #if defined (MIPS_CPU_STRING_DEFAULT)
20364 unsigned int i;
20365 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
20366 if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
20367 return mips_cpu_info_table + i;
20368 gcc_unreachable ();
20369 #elif defined (MIPS_ISA_DEFAULT)
20370 return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
20371 #else
20372 /* 'from-abi' makes a good default: you get whatever the ABI
20373 requires. */
20374 return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
20375 #endif
20376 }
20377
20378 /* Set up globals to generate code for the ISA or processor
20379 described by INFO. */
20380
20381 static void
20382 mips_set_architecture (const struct mips_cpu_info *info)
20383 {
20384 if (info != 0)
20385 {
20386 mips_arch_info = info;
20387 mips_arch = info->cpu;
20388 mips_isa = info->isa;
20389 if (mips_isa < MIPS_ISA_MIPS32)
20390 mips_isa_rev = 0;
20391 else
20392 /* we can do this is due to the
20393 * enum of MIPS32rN is from 32 to 37
20394 * enum of MIPS64rN is from 64 to 69 */
20395 mips_isa_rev = (mips_isa & 31) + 1;
20396 }
20397 }
20398
20399 /* Likewise for tuning. */
20400
20401 static void
20402 mips_set_tune (const struct mips_cpu_info *info)
20403 {
20404 if (info != 0)
20405 {
20406 mips_tune_info = info;
20407 mips_tune = info->cpu;
20408 }
20409 }
20410
20411 /* Implement TARGET_OPTION_OVERRIDE. */
20412
20413 static void
20414 mips_option_override (void)
20415 {
20416 int i, regno, mode;
20417
20418 if (OPTION_SET_P (mips_isa_option))
20419 mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
20420
20421 #ifdef SUBTARGET_OVERRIDE_OPTIONS
20422 SUBTARGET_OVERRIDE_OPTIONS;
20423 #endif
20424
20425 /* MIPS16 and microMIPS cannot coexist. */
20426 if (TARGET_MICROMIPS && TARGET_MIPS16)
20427 error ("unsupported combination: %s", "-mips16 -mmicromips");
20428
20429 /* Prohibit Paired-Single and MSA combination. This is software restriction
20430 rather than architectural. */
20431 if (ISA_HAS_MSA && TARGET_PAIRED_SINGLE_FLOAT)
20432 error ("unsupported combination: %s", "-mmsa -mpaired-single");
20433
20434 /* Save the base compression state and process flags as though we
20435 were generating uncompressed code. */
20436 mips_base_compression_flags = TARGET_COMPRESSION;
20437 target_flags &= ~TARGET_COMPRESSION;
20438 mips_base_code_readable = mips_code_readable;
20439
20440 /* -mno-float overrides -mhard-float and -msoft-float. */
20441 if (TARGET_NO_FLOAT)
20442 {
20443 target_flags |= MASK_SOFT_FLOAT_ABI;
20444 target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
20445 }
20446
20447 if (TARGET_FLIP_MIPS16)
20448 TARGET_INTERLINK_COMPRESSED = 1;
20449
20450 /* Set the small data limit. */
20451 mips_small_data_threshold = (OPTION_SET_P (g_switch_value)
20452 ? g_switch_value
20453 : MIPS_DEFAULT_GVALUE);
20454
20455 /* The following code determines the architecture and register size.
20456 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
20457 The GAS and GCC code should be kept in sync as much as possible. */
20458
20459 if (OPTION_SET_P (mips_arch_option))
20460 mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
20461
20462 if (mips_isa_option_info != 0)
20463 {
20464 if (mips_arch_info == 0)
20465 mips_set_architecture (mips_isa_option_info);
20466 else if (mips_arch_info->isa != mips_isa_option_info->isa)
20467 error ("%<-%s%> conflicts with the other architecture options, "
20468 "which specify a %s processor",
20469 mips_isa_option_info->name,
20470 mips_cpu_info_from_isa (mips_arch_info->isa)->name);
20471 }
20472
20473 if (mips_arch_info == 0)
20474 mips_set_architecture (mips_default_arch ());
20475
20476 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
20477 error ("%<-march=%s%> is not compatible with the selected ABI",
20478 mips_arch_info->name);
20479
20480 /* Optimize for mips_arch, unless -mtune selects a different processor. */
20481 if (OPTION_SET_P (mips_tune_option))
20482 mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
20483
20484 if (mips_tune_info == 0)
20485 mips_set_tune (mips_arch_info);
20486
20487 if ((target_flags_explicit & MASK_64BIT) != 0)
20488 {
20489 /* The user specified the size of the integer registers. Make sure
20490 it agrees with the ABI and ISA. */
20491 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
20492 error ("%<-mgp64%> used with a 32-bit processor");
20493 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
20494 error ("%<-mgp32%> used with a 64-bit ABI");
20495 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
20496 error ("%<-mgp64%> used with a 32-bit ABI");
20497 }
20498 else
20499 {
20500 /* Infer the integer register size from the ABI and processor.
20501 Restrict ourselves to 32-bit registers if that's all the
20502 processor has, or if the ABI cannot handle 64-bit registers. */
20503 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
20504 target_flags &= ~MASK_64BIT;
20505 else
20506 target_flags |= MASK_64BIT;
20507 }
20508
20509 /* -fsanitize=address needs to turn on -fasynchronous-unwind-tables in
20510 order for tracebacks to be complete but not if any
20511 -fasynchronous-unwind-table were already specified. */
20512 if (flag_sanitize & SANITIZE_USER_ADDRESS
20513 && !global_options_set.x_flag_asynchronous_unwind_tables)
20514 flag_asynchronous_unwind_tables = 1;
20515
20516 if ((target_flags_explicit & MASK_FLOAT64) != 0)
20517 {
20518 if (mips_isa_rev >= 6 && !TARGET_FLOAT64)
20519 error ("the %qs architecture does not support %<-mfp32%>",
20520 mips_arch_info->name);
20521 else if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
20522 error ("unsupported combination: %s", "-mfp64 -msingle-float");
20523 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
20524 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
20525 else if (!TARGET_64BIT && TARGET_FLOAT64)
20526 {
20527 if (!ISA_HAS_MXHC1)
20528 error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
20529 " the target supports the mfhc1 and mthc1 instructions");
20530 else if (mips_abi != ABI_32)
20531 error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
20532 " the o32 ABI");
20533 }
20534 }
20535 else
20536 {
20537 /* -msingle-float selects 32-bit float registers. On r6 and later,
20538 -mdouble-float selects 64-bit float registers, since the old paired
20539 register model is not supported. In other cases the float registers
20540 should be the same size as the integer ones. */
20541 if (mips_isa_rev >= 6 && TARGET_DOUBLE_FLOAT && !TARGET_FLOATXX)
20542 target_flags |= MASK_FLOAT64;
20543 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
20544 target_flags |= MASK_FLOAT64;
20545 else if (mips_abi == ABI_32 && ISA_HAS_MSA && !TARGET_FLOATXX)
20546 target_flags |= MASK_FLOAT64;
20547 else
20548 target_flags &= ~MASK_FLOAT64;
20549 }
20550
20551 if (mips_abi != ABI_32 && TARGET_FLOATXX)
20552 error ("%<-mfpxx%> can only be used with the o32 ABI");
20553 else if (TARGET_FLOAT64 && TARGET_FLOATXX)
20554 error ("unsupported combination: %s", "-mfp64 -mfpxx");
20555 else if (ISA_MIPS1 && !TARGET_FLOAT32)
20556 error ("%<-march=%s%> requires %<-mfp32%>", mips_arch_info->name);
20557
20558 /* End of code shared with GAS. */
20559
20560 /* The R5900 FPU only supports single precision. */
20561 if (TARGET_MIPS5900 && TARGET_HARD_FLOAT_ABI && TARGET_DOUBLE_FLOAT)
20562 error ("unsupported combination: %s",
20563 "-march=r5900 -mhard-float -mdouble-float");
20564
20565 /* If a -mlong* option was given, check that it matches the ABI,
20566 otherwise infer the -mlong* setting from the other options. */
20567 if ((target_flags_explicit & MASK_LONG64) != 0)
20568 {
20569 if (TARGET_LONG64)
20570 {
20571 if (mips_abi == ABI_N32)
20572 error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
20573 else if (mips_abi == ABI_32)
20574 error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
20575 else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
20576 /* We have traditionally allowed non-abicalls code to use
20577 an LP64 form of o64. However, it would take a bit more
20578 effort to support the combination of 32-bit GOT entries
20579 and 64-bit pointers, so we treat the abicalls case as
20580 an error. */
20581 error ("the combination of %qs and %qs is incompatible with %qs",
20582 "-mabi=o64", "-mabicalls", "-mlong64");
20583 }
20584 else
20585 {
20586 if (mips_abi == ABI_64)
20587 error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
20588 }
20589 }
20590 else
20591 {
20592 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
20593 target_flags |= MASK_LONG64;
20594 else
20595 target_flags &= ~MASK_LONG64;
20596 }
20597
20598 if (!TARGET_OLDABI)
20599 flag_pcc_struct_return = 0;
20600
20601 /* Decide which rtx_costs structure to use. */
20602 if (optimize_size)
20603 mips_cost = &mips_rtx_cost_optimize_size;
20604 else
20605 mips_cost = &mips_rtx_cost_data[mips_tune];
20606
20607 /* If the user hasn't specified a branch cost, use the processor's
20608 default. */
20609 if (mips_branch_cost == 0)
20610 mips_branch_cost = mips_cost->branch_cost;
20611
20612 /* If neither -mbranch-likely nor -mno-branch-likely was given
20613 on the command line, set MASK_BRANCHLIKELY based on the target
20614 architecture and tuning flags. Annulled delay slots are a
20615 size win, so we only consider the processor-specific tuning
20616 for !optimize_size. */
20617 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
20618 {
20619 if (ISA_HAS_BRANCHLIKELY
20620 && ((optimize_size
20621 && (mips_tune_info->tune_flags
20622 & PTF_AVOID_BRANCHLIKELY_SIZE) == 0)
20623 || (!optimize_size
20624 && optimize > 0
20625 && (mips_tune_info->tune_flags
20626 & PTF_AVOID_BRANCHLIKELY_SPEED) == 0)
20627 || (mips_tune_info->tune_flags
20628 & PTF_AVOID_BRANCHLIKELY_ALWAYS) == 0))
20629 target_flags |= MASK_BRANCHLIKELY;
20630 else
20631 target_flags &= ~MASK_BRANCHLIKELY;
20632 }
20633 else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
20634 warning (0, "the %qs architecture does not support branch-likely"
20635 " instructions", mips_arch_info->name);
20636
20637 /* If the user hasn't specified -mimadd or -mno-imadd set
20638 MASK_IMADD based on the target architecture and tuning
20639 flags. */
20640 if ((target_flags_explicit & MASK_IMADD) == 0)
20641 {
20642 if (ISA_HAS_MADD_MSUB &&
20643 (mips_tune_info->tune_flags & PTF_AVOID_IMADD) == 0)
20644 target_flags |= MASK_IMADD;
20645 else
20646 target_flags &= ~MASK_IMADD;
20647 }
20648 else if (TARGET_IMADD && !ISA_HAS_MADD_MSUB)
20649 warning (0, "the %qs architecture does not support madd or msub"
20650 " instructions", mips_arch_info->name);
20651
20652 /* If neither -modd-spreg nor -mno-odd-spreg was given on the command
20653 line, set MASK_ODD_SPREG based on the ISA and ABI. */
20654 if ((target_flags_explicit & MASK_ODD_SPREG) == 0)
20655 {
20656 /* Disable TARGET_ODD_SPREG when using the o32 FPXX ABI. */
20657 if (!ISA_HAS_ODD_SPREG || TARGET_FLOATXX)
20658 target_flags &= ~MASK_ODD_SPREG;
20659 else
20660 target_flags |= MASK_ODD_SPREG;
20661 }
20662 else if (TARGET_ODD_SPREG && !ISA_HAS_ODD_SPREG)
20663 warning (0, "the %qs architecture does not support odd single-precision"
20664 " registers", mips_arch_info->name);
20665
20666 if (!TARGET_ODD_SPREG && TARGET_64BIT)
20667 {
20668 error ("unsupported combination: %s", "-mgp64 -mno-odd-spreg");
20669 /* Allow compilation to continue further even though invalid output
20670 will be produced. */
20671 target_flags |= MASK_ODD_SPREG;
20672 }
20673
20674 if (!ISA_HAS_DELAY_SLOTS && mips_cb == MIPS_CB_NEVER)
20675 {
20676 error ("unsupported combination: %qs%s %s",
20677 mips_arch_info->name, TARGET_MICROMIPS ? " -mmicromips" : "",
20678 "-mcompact-branches=never");
20679 }
20680
20681 /* Require explicit relocs for MIPS R6 onwards. This enables simplification
20682 of the compact branch and jump support through the backend. */
20683 if (!TARGET_EXPLICIT_RELOCS && mips_isa_rev >= 6)
20684 {
20685 error ("unsupported combination: %qs %s",
20686 mips_arch_info->name, "-mno-explicit-relocs");
20687 }
20688
20689 /* The effect of -mabicalls isn't defined for the EABI. */
20690 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
20691 {
20692 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
20693 target_flags &= ~MASK_ABICALLS;
20694 }
20695
20696 /* PIC requires -mabicalls. */
20697 if (flag_pic)
20698 {
20699 if (mips_abi == ABI_EABI)
20700 error ("cannot generate position-independent code for %qs",
20701 "-mabi=eabi");
20702 else if (!TARGET_ABICALLS)
20703 error ("position-independent code requires %qs", "-mabicalls");
20704 }
20705
20706 if (TARGET_ABICALLS_PIC2)
20707 /* We need to set flag_pic for executables as well as DSOs
20708 because we may reference symbols that are not defined in
20709 the final executable. (MIPS does not use things like
20710 copy relocs, for example.)
20711
20712 There is a body of code that uses __PIC__ to distinguish
20713 between -mabicalls and -mno-abicalls code. The non-__PIC__
20714 variant is usually appropriate for TARGET_ABICALLS_PIC0, as
20715 long as any indirect jumps use $25. */
20716 flag_pic = 1;
20717
20718 /* -mvr4130-align is a "speed over size" optimization: it usually produces
20719 faster code, but at the expense of more nops. Enable it at -O3 and
20720 above. */
20721 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
20722 target_flags |= MASK_VR4130_ALIGN;
20723
20724 /* Prefer a call to memcpy over inline code when optimizing for size,
20725 though see MOVE_RATIO in mips.h. */
20726 if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
20727 target_flags |= MASK_MEMCPY;
20728
20729 /* If we have a nonzero small-data limit, check that the -mgpopt
20730 setting is consistent with the other target flags. */
20731 if (mips_small_data_threshold > 0)
20732 {
20733 if (!TARGET_GPOPT)
20734 {
20735 if (!TARGET_EXPLICIT_RELOCS)
20736 error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
20737
20738 TARGET_LOCAL_SDATA = false;
20739 TARGET_EXTERN_SDATA = false;
20740 }
20741 else
20742 {
20743 if (TARGET_VXWORKS_RTP)
20744 warning (0, "cannot use small-data accesses for %qs", "-mrtp");
20745
20746 if (TARGET_ABICALLS)
20747 warning (0, "cannot use small-data accesses for %qs",
20748 "-mabicalls");
20749 }
20750 }
20751
20752 /* Set NaN and ABS defaults. */
20753 if (mips_nan == MIPS_IEEE_754_DEFAULT && !ISA_HAS_IEEE_754_LEGACY)
20754 mips_nan = MIPS_IEEE_754_2008;
20755 if (mips_abs == MIPS_IEEE_754_DEFAULT && mips_nan == MIPS_IEEE_754_2008)
20756 mips_abs = MIPS_IEEE_754_2008;
20757 if (mips_abs == MIPS_IEEE_754_DEFAULT && !ISA_HAS_IEEE_754_LEGACY)
20758 mips_abs = MIPS_IEEE_754_2008;
20759
20760 /* Check for IEEE 754 legacy/2008 support. */
20761 if ((mips_nan == MIPS_IEEE_754_LEGACY
20762 || mips_abs == MIPS_IEEE_754_LEGACY)
20763 && !ISA_HAS_IEEE_754_LEGACY)
20764 warning (0, "the %qs architecture does not support %<-m%s=legacy%>",
20765 mips_arch_info->name,
20766 mips_nan == MIPS_IEEE_754_LEGACY ? "nan" : "abs");
20767
20768 if ((mips_nan == MIPS_IEEE_754_2008
20769 || mips_abs == MIPS_IEEE_754_2008)
20770 && !ISA_HAS_IEEE_754_2008)
20771 warning (0, "the %qs architecture does not support %<-m%s=2008%>",
20772 mips_arch_info->name,
20773 mips_nan == MIPS_IEEE_754_2008 ? "nan" : "abs");
20774
20775 /* Pre-IEEE 754-2008 MIPS hardware has a quirky almost-IEEE format
20776 for all its floating point. */
20777 if (mips_nan != MIPS_IEEE_754_2008)
20778 {
20779 REAL_MODE_FORMAT (SFmode) = &mips_single_format;
20780 REAL_MODE_FORMAT (DFmode) = &mips_double_format;
20781 REAL_MODE_FORMAT (TFmode) = &mips_quad_format;
20782 }
20783
20784 /* Make sure that the user didn't turn off paired single support when
20785 MIPS-3D support is requested. */
20786 if (TARGET_MIPS3D
20787 && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
20788 && !TARGET_PAIRED_SINGLE_FLOAT)
20789 error ("%<-mips3d%> requires %<-mpaired-single%>");
20790
20791 /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT. */
20792 if (TARGET_MIPS3D)
20793 target_flags |= MASK_PAIRED_SINGLE_FLOAT;
20794
20795 /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
20796 and TARGET_HARD_FLOAT_ABI are both true. */
20797 if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
20798 {
20799 error ("%qs must be used with %qs",
20800 TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
20801 TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
20802 target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
20803 TARGET_MIPS3D = 0;
20804 }
20805
20806 /* Make sure that when ISA_HAS_MSA is true, TARGET_FLOAT64 and
20807 TARGET_HARD_FLOAT_ABI and both true. */
20808 if (ISA_HAS_MSA && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
20809 error ("%<-mmsa%> must be used with %<-mfp64%> and %<-mhard-float%>");
20810
20811 /* Make sure that -mpaired-single is only used on ISAs that support it.
20812 We must disable it otherwise since it relies on other ISA properties
20813 like ISA_HAS_8CC having their normal values. */
20814 if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
20815 {
20816 error ("the %qs architecture does not support paired-single"
20817 " instructions", mips_arch_info->name);
20818 target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
20819 TARGET_MIPS3D = 0;
20820 }
20821
20822 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
20823 && !TARGET_CACHE_BUILTIN)
20824 {
20825 error ("%qs requires a target that provides the %qs instruction",
20826 "-mr10k-cache-barrier", "cache");
20827 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
20828 }
20829
20830 /* If TARGET_DSPR2, enable TARGET_DSP. */
20831 if (TARGET_DSPR2)
20832 TARGET_DSP = true;
20833
20834 if (TARGET_DSP && mips_isa_rev >= 6)
20835 {
20836 error ("the %qs architecture does not support DSP instructions",
20837 mips_arch_info->name);
20838 TARGET_DSP = false;
20839 TARGET_DSPR2 = false;
20840 }
20841
20842 /* Make sure that when TARGET_LOONGSON_MMI is true, TARGET_HARD_FLOAT_ABI
20843 is true. In o32 pairs of floating-point registers provide 64-bit
20844 values. */
20845 if (TARGET_LOONGSON_MMI && !TARGET_HARD_FLOAT_ABI)
20846 error ("%<-mloongson-mmi%> must be used with %<-mhard-float%>");
20847
20848 /* If TARGET_LOONGSON_EXT2, enable TARGET_LOONGSON_EXT. */
20849 if (TARGET_LOONGSON_EXT2)
20850 {
20851 /* Make sure that when TARGET_LOONGSON_EXT2 is true, TARGET_LOONGSON_EXT
20852 is true. If a user explicitly says -mloongson-ext2 -mno-loongson-ext
20853 then that is an error. */
20854 if (!TARGET_LOONGSON_EXT
20855 && (target_flags_explicit & MASK_LOONGSON_EXT) != 0)
20856 error ("%<-mloongson-ext2%> must be used with %<-mloongson-ext%>");
20857 target_flags |= MASK_LOONGSON_EXT;
20858 }
20859
20860 /* .eh_frame addresses should be the same width as a C pointer.
20861 Most MIPS ABIs support only one pointer size, so the assembler
20862 will usually know exactly how big an .eh_frame address is.
20863
20864 Unfortunately, this is not true of the 64-bit EABI. The ABI was
20865 originally defined to use 64-bit pointers (i.e. it is LP64), and
20866 this is still the default mode. However, we also support an n32-like
20867 ILP32 mode, which is selected by -mlong32. The problem is that the
20868 assembler has traditionally not had an -mlong option, so it has
20869 traditionally not known whether we're using the ILP32 or LP64 form.
20870
20871 As it happens, gas versions up to and including 2.19 use _32-bit_
20872 addresses for EABI64 .cfi_* directives. This is wrong for the
20873 default LP64 mode, so we can't use the directives by default.
20874 Moreover, since gas's current behavior is at odds with gcc's
20875 default behavior, it seems unwise to rely on future versions
20876 of gas behaving the same way. We therefore avoid using .cfi
20877 directives for -mlong32 as well. */
20878 if (mips_abi == ABI_EABI && TARGET_64BIT)
20879 flag_dwarf2_cfi_asm = 0;
20880
20881 /* .cfi_* directives generate a read-only section, so fall back on
20882 manual .eh_frame creation if we need the section to be writable. */
20883 if (TARGET_WRITABLE_EH_FRAME)
20884 flag_dwarf2_cfi_asm = 0;
20885
20886 mips_init_print_operand_punct ();
20887
20888 /* Set up array to map GCC register number to debug register number.
20889 Ignore the special purpose register numbers. */
20890
20891 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
20892 {
20893 if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
20894 mips_dwarf_regno[i] = i;
20895 else
20896 mips_dwarf_regno[i] = INVALID_REGNUM;
20897 }
20898
20899 /* Accumulator debug registers use big-endian ordering. */
20900 mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
20901 mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
20902 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
20903 {
20904 mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
20905 mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
20906 }
20907
20908 /* Set up mips_hard_regno_mode_ok. */
20909 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
20910 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
20911 mips_hard_regno_mode_ok_p[mode][regno]
20912 = mips_hard_regno_mode_ok_uncached (regno, (machine_mode) mode);
20913
20914 /* Function to allocate machine-dependent function status. */
20915 init_machine_status = &mips_init_machine_status;
20916
20917 /* Default to working around R4000 errata only if the processor
20918 was selected explicitly. */
20919 if ((target_flags_explicit & MASK_FIX_R4000) == 0
20920 && strcmp (mips_arch_info->name, "r4000") == 0)
20921 target_flags |= MASK_FIX_R4000;
20922
20923 /* Default to working around R4400 errata only if the processor
20924 was selected explicitly. */
20925 if ((target_flags_explicit & MASK_FIX_R4400) == 0
20926 && strcmp (mips_arch_info->name, "r4400") == 0)
20927 target_flags |= MASK_FIX_R4400;
20928
20929 /* Default to working around R5900 errata only if the processor
20930 was selected explicitly. */
20931 if ((target_flags_explicit & MASK_FIX_R5900) == 0
20932 && strcmp (mips_arch_info->name, "r5900") == 0)
20933 target_flags |= MASK_FIX_R5900;
20934
20935 /* Default to working around R10000 errata only if the processor
20936 was selected explicitly. */
20937 if ((target_flags_explicit & MASK_FIX_R10000) == 0
20938 && strcmp (mips_arch_info->name, "r10000") == 0)
20939 target_flags |= MASK_FIX_R10000;
20940
20941 /* Make sure that branch-likely instructions available when using
20942 -mfix-r10000. The instructions are not available if either:
20943
20944 1. -mno-branch-likely was passed.
20945 2. The selected ISA does not support branch-likely and
20946 the command line does not include -mbranch-likely. */
20947 if (TARGET_FIX_R10000
20948 && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
20949 ? !ISA_HAS_BRANCHLIKELY
20950 : !TARGET_BRANCHLIKELY))
20951 sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
20952
20953 if (TARGET_SYNCI && !ISA_HAS_SYNCI)
20954 {
20955 warning (0, "the %qs architecture does not support the synci "
20956 "instruction", mips_arch_info->name);
20957 target_flags &= ~MASK_SYNCI;
20958 }
20959
20960 /* Only optimize PIC indirect calls if they are actually required. */
20961 if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
20962 target_flags &= ~MASK_RELAX_PIC_CALLS;
20963
20964 /* Save base state of options. */
20965 mips_base_target_flags = target_flags;
20966 mips_base_schedule_insns = flag_schedule_insns;
20967 mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
20968 mips_base_move_loop_invariants = flag_move_loop_invariants;
20969 mips_base_align_loops = str_align_loops;
20970 mips_base_align_jumps = str_align_jumps;
20971 mips_base_align_functions = str_align_functions;
20972
20973 /* Now select the ISA mode.
20974
20975 Do all CPP-sensitive stuff in uncompressed mode; we'll switch modes
20976 later if required. */
20977 mips_set_compression_mode (0);
20978
20979 /* We register a second machine specific reorg pass after delay slot
20980 filling. Registering the pass must be done at start up. It's
20981 convenient to do it here. */
20982 opt_pass *new_pass = make_pass_mips_machine_reorg2 (g);
20983 struct register_pass_info insert_pass_mips_machine_reorg2 =
20984 {
20985 new_pass, /* pass */
20986 "dbr", /* reference_pass_name */
20987 1, /* ref_pass_instance_number */
20988 PASS_POS_INSERT_AFTER /* po_op */
20989 };
20990 register_pass (&insert_pass_mips_machine_reorg2);
20991
20992 if (TARGET_HARD_FLOAT_ABI && TARGET_MIPS5900)
20993 REAL_MODE_FORMAT (SFmode) = &spu_single_format;
20994
20995 mips_register_frame_header_opt ();
20996 }
20997
20998 /* Swap the register information for registers I and I + 1, which
20999 currently have the wrong endianness. Note that the registers'
21000 fixedness and call-clobberedness might have been set on the
21001 command line. */
21002
21003 static void
21004 mips_swap_registers (unsigned int i)
21005 {
21006 int tmpi;
21007 const char *tmps;
21008
21009 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
21010 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
21011
21012 SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
21013 SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
21014 SWAP_STRING (reg_names[i], reg_names[i + 1]);
21015
21016 #undef SWAP_STRING
21017 #undef SWAP_INT
21018 }
21019
21020 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
21021
21022 static void
21023 mips_conditional_register_usage (void)
21024 {
21025
21026 if (ISA_HAS_DSP)
21027 {
21028 /* These DSP control register fields are global. */
21029 global_regs[CCDSP_PO_REGNUM] = 1;
21030 global_regs[CCDSP_SC_REGNUM] = 1;
21031 }
21032 else
21033 accessible_reg_set &= ~reg_class_contents[DSP_ACC_REGS];
21034
21035 if (!ISA_HAS_HILO)
21036 accessible_reg_set &= ~reg_class_contents[MD_REGS];
21037
21038 if (!TARGET_HARD_FLOAT)
21039 accessible_reg_set &= ~(reg_class_contents[FP_REGS]
21040 | reg_class_contents[ST_REGS]);
21041 else if (!ISA_HAS_8CC)
21042 {
21043 /* We only have a single condition-code register. We implement
21044 this by fixing all the condition-code registers and generating
21045 RTL that refers directly to ST_REG_FIRST. */
21046 accessible_reg_set &= ~reg_class_contents[ST_REGS];
21047 if (!ISA_HAS_CCF)
21048 SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
21049 fixed_regs[FPSW_REGNUM] = 1;
21050 }
21051 if (TARGET_MIPS16)
21052 {
21053 /* In MIPS16 mode, we prohibit the unused $s registers, since they
21054 are call-saved, and saving them via a MIPS16 register would
21055 probably waste more time than just reloading the value.
21056
21057 We permit the $t temporary registers when optimizing for speed
21058 but not when optimizing for space because using them results in
21059 code that is larger (but faster) then not using them. We do
21060 allow $24 (t8) because it is used in CMP and CMPI instructions
21061 and $25 (t9) because it is used as the function call address in
21062 SVR4 PIC code. */
21063
21064 fixed_regs[18] = 1;
21065 fixed_regs[19] = 1;
21066 fixed_regs[20] = 1;
21067 fixed_regs[21] = 1;
21068 fixed_regs[22] = 1;
21069 fixed_regs[23] = 1;
21070 fixed_regs[26] = 1;
21071 fixed_regs[27] = 1;
21072 fixed_regs[30] = 1;
21073 if (optimize_size)
21074 {
21075 fixed_regs[8] = 1;
21076 fixed_regs[9] = 1;
21077 fixed_regs[10] = 1;
21078 fixed_regs[11] = 1;
21079 fixed_regs[12] = 1;
21080 fixed_regs[13] = 1;
21081 fixed_regs[14] = 1;
21082 fixed_regs[15] = 1;
21083 }
21084
21085 /* Do not allow HI and LO to be treated as register operands.
21086 There are no MTHI or MTLO instructions (or any real need
21087 for them) and one-way registers cannot easily be reloaded. */
21088 operand_reg_set &= ~reg_class_contents[MD_REGS];
21089 }
21090 /* $f20-$f23 are call-clobbered for n64. */
21091 if (mips_abi == ABI_64)
21092 {
21093 int regno;
21094 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
21095 call_used_regs[regno] = 1;
21096 }
21097 /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
21098 for n32 and o32 FP64. */
21099 if (mips_abi == ABI_N32
21100 || (mips_abi == ABI_32
21101 && TARGET_FLOAT64))
21102 {
21103 int regno;
21104 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
21105 call_used_regs[regno] = 1;
21106 }
21107 /* Make sure that double-register accumulator values are correctly
21108 ordered for the current endianness. */
21109 if (TARGET_LITTLE_ENDIAN)
21110 {
21111 unsigned int regno;
21112
21113 mips_swap_registers (MD_REG_FIRST);
21114 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
21115 mips_swap_registers (regno);
21116 }
21117 }
21118
21119 /* Implement EH_USES. */
21120
21121 bool
21122 mips_eh_uses (unsigned int regno)
21123 {
21124 if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
21125 {
21126 /* We need to force certain registers to be live in order to handle
21127 PIC long branches correctly. See mips_must_initialize_gp_p for
21128 details. */
21129 if (mips_cfun_has_cprestore_slot_p ())
21130 {
21131 if (regno == CPRESTORE_SLOT_REGNUM)
21132 return true;
21133 }
21134 else
21135 {
21136 if (cfun->machine->global_pointer == regno)
21137 return true;
21138 }
21139 }
21140
21141 return false;
21142 }
21143
21144 /* Implement EPILOGUE_USES. */
21145
21146 bool
21147 mips_epilogue_uses (unsigned int regno)
21148 {
21149 /* Say that the epilogue uses the return address register. Note that
21150 in the case of sibcalls, the values "used by the epilogue" are
21151 considered live at the start of the called function. */
21152 if (regno == RETURN_ADDR_REGNUM)
21153 return true;
21154
21155 /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
21156 See the comment above load_call<mode> for details. */
21157 if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
21158 return true;
21159
21160 /* An interrupt handler must preserve some registers that are
21161 ordinarily call-clobbered. */
21162 if (cfun->machine->interrupt_handler_p
21163 && mips_interrupt_extra_call_saved_reg_p (regno))
21164 return true;
21165
21166 return false;
21167 }
21168
21169 /* Return true if INSN needs to be wrapped in ".set noat".
21170 INSN has NOPERANDS operands, stored in OPVEC. */
21171
21172 static bool
21173 mips_need_noat_wrapper_p (rtx_insn *insn, rtx *opvec, int noperands)
21174 {
21175 if (recog_memoized (insn) >= 0)
21176 {
21177 subrtx_iterator::array_type array;
21178 for (int i = 0; i < noperands; i++)
21179 FOR_EACH_SUBRTX (iter, array, opvec[i], NONCONST)
21180 if (REG_P (*iter) && REGNO (*iter) == AT_REGNUM)
21181 return true;
21182 }
21183 return false;
21184 }
21185
21186 /* Implement FINAL_PRESCAN_INSN. Mark MIPS16 inline constant pools
21187 as data for the purpose of disassembly. For simplicity embed the
21188 pool's initial label number in the local symbol produced so that
21189 multiple pools within a single function end up marked with unique
21190 symbols. The label number is carried by the `consttable' insn
21191 emitted at the beginning of each pool. */
21192
21193 void
21194 mips_final_prescan_insn (rtx_insn *insn, rtx *opvec, int noperands)
21195 {
21196 if (INSN_P (insn)
21197 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
21198 && XINT (PATTERN (insn), 1) == UNSPEC_CONSTTABLE)
21199 mips_set_text_contents_type (asm_out_file, "__pool_",
21200 INTVAL (XVECEXP (PATTERN (insn), 0, 0)),
21201 FALSE);
21202
21203 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
21204 mips_push_asm_switch (&mips_noat);
21205 }
21206
21207 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN. Reset text marking to
21208 code after a MIPS16 inline constant pool. Like with the beginning
21209 of a pool table use the pool's initial label number to keep symbols
21210 unique. The label number is carried by the `consttable_end' insn
21211 emitted at the end of each pool. */
21212
21213 static void
21214 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx_insn *insn,
21215 rtx *opvec, int noperands)
21216 {
21217 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
21218 mips_pop_asm_switch (&mips_noat);
21219
21220 if (INSN_P (insn)
21221 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
21222 && XINT (PATTERN (insn), 1) == UNSPEC_CONSTTABLE_END)
21223 {
21224 rtx_insn *next_insn = next_real_nondebug_insn (insn);
21225 bool code_p = (next_insn != NULL
21226 && INSN_P (next_insn)
21227 && (GET_CODE (PATTERN (next_insn)) != UNSPEC_VOLATILE
21228 || XINT (PATTERN (next_insn), 1) != UNSPEC_CONSTTABLE));
21229
21230 /* Switch content type depending on whether there is code beyond
21231 the constant pool. */
21232 mips_set_text_contents_type (asm_out_file, "__pend_",
21233 INTVAL (XVECEXP (PATTERN (insn), 0, 0)),
21234 code_p);
21235 }
21236 }
21237
21238 /* Return the function that is used to expand the <u>mulsidi3 pattern.
21239 EXT_CODE is the code of the extension used. Return NULL if widening
21240 multiplication shouldn't be used. */
21241
21242 mulsidi3_gen_fn
21243 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
21244 {
21245 bool signed_p;
21246
21247 signed_p = ext_code == SIGN_EXTEND;
21248 if (TARGET_64BIT)
21249 {
21250 /* Don't use widening multiplication with MULT when we have DMUL. Even
21251 with the extension of its input operands DMUL is faster. Note that
21252 the extension is not needed for signed multiplication. In order to
21253 ensure that we always remove the redundant sign-extension in this
21254 case we still expand mulsidi3 for DMUL. */
21255 if (ISA_HAS_R6DMUL)
21256 return signed_p ? gen_mulsidi3_64bit_r6dmul : NULL;
21257 if (ISA_HAS_DMUL3)
21258 return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
21259 if (TARGET_MIPS16)
21260 return (signed_p
21261 ? gen_mulsidi3_64bit_mips16
21262 : gen_umulsidi3_64bit_mips16);
21263 if (TARGET_FIX_R4000)
21264 return NULL;
21265 return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
21266 }
21267 else
21268 {
21269 if (ISA_HAS_R6MUL)
21270 return (signed_p ? gen_mulsidi3_32bit_r6 : gen_umulsidi3_32bit_r6);
21271 if (TARGET_MIPS16)
21272 return (signed_p
21273 ? gen_mulsidi3_32bit_mips16
21274 : gen_umulsidi3_32bit_mips16);
21275 if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
21276 return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
21277 return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
21278 }
21279 }
21280
21281 /* Return true if PATTERN matches the kind of instruction generated by
21282 umips_build_save_restore. SAVE_P is true for store. */
21283
21284 bool
21285 umips_save_restore_pattern_p (bool save_p, rtx pattern)
21286 {
21287 int n;
21288 unsigned int i;
21289 HOST_WIDE_INT first_offset = 0;
21290 rtx first_base = 0;
21291 unsigned int regmask = 0;
21292
21293 for (n = 0; n < XVECLEN (pattern, 0); n++)
21294 {
21295 rtx set, reg, mem, this_base;
21296 HOST_WIDE_INT this_offset;
21297
21298 /* Check that we have a SET. */
21299 set = XVECEXP (pattern, 0, n);
21300 if (GET_CODE (set) != SET)
21301 return false;
21302
21303 /* Check that the SET is a load (if restoring) or a store
21304 (if saving). */
21305 mem = save_p ? SET_DEST (set) : SET_SRC (set);
21306 if (!MEM_P (mem) || MEM_VOLATILE_P (mem))
21307 return false;
21308
21309 /* Check that the address is the sum of base and a possibly-zero
21310 constant offset. Determine if the offset is in range. */
21311 mips_split_plus (XEXP (mem, 0), &this_base, &this_offset);
21312 if (!REG_P (this_base))
21313 return false;
21314
21315 if (n == 0)
21316 {
21317 if (!UMIPS_12BIT_OFFSET_P (this_offset))
21318 return false;
21319 first_base = this_base;
21320 first_offset = this_offset;
21321 }
21322 else
21323 {
21324 /* Check that the save slots are consecutive. */
21325 if (REGNO (this_base) != REGNO (first_base)
21326 || this_offset != first_offset + UNITS_PER_WORD * n)
21327 return false;
21328 }
21329
21330 /* Check that SET's other operand is a register. */
21331 reg = save_p ? SET_SRC (set) : SET_DEST (set);
21332 if (!REG_P (reg))
21333 return false;
21334
21335 regmask |= 1 << REGNO (reg);
21336 }
21337
21338 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
21339 if (regmask == umips_swm_mask[i])
21340 return true;
21341
21342 return false;
21343 }
21344
21345 /* Return the assembly instruction for microMIPS LWM or SWM.
21346 SAVE_P and PATTERN are as for umips_save_restore_pattern_p. */
21347
21348 const char *
21349 umips_output_save_restore (bool save_p, rtx pattern)
21350 {
21351 static char buffer[300];
21352 char *s;
21353 int n;
21354 HOST_WIDE_INT offset;
21355 rtx base, mem, set, last_set, last_reg;
21356
21357 /* Parse the pattern. */
21358 gcc_assert (umips_save_restore_pattern_p (save_p, pattern));
21359
21360 s = strcpy (buffer, save_p ? "swm\t" : "lwm\t");
21361 s += strlen (s);
21362 n = XVECLEN (pattern, 0);
21363
21364 set = XVECEXP (pattern, 0, 0);
21365 mem = save_p ? SET_DEST (set) : SET_SRC (set);
21366 mips_split_plus (XEXP (mem, 0), &base, &offset);
21367
21368 last_set = XVECEXP (pattern, 0, n - 1);
21369 last_reg = save_p ? SET_SRC (last_set) : SET_DEST (last_set);
21370
21371 if (REGNO (last_reg) == 31)
21372 n--;
21373
21374 gcc_assert (n <= 9);
21375 if (n == 0)
21376 ;
21377 else if (n == 1)
21378 s += sprintf (s, "%s,", reg_names[16]);
21379 else if (n < 9)
21380 s += sprintf (s, "%s-%s,", reg_names[16], reg_names[15 + n]);
21381 else if (n == 9)
21382 s += sprintf (s, "%s-%s,%s,", reg_names[16], reg_names[23],
21383 reg_names[30]);
21384
21385 if (REGNO (last_reg) == 31)
21386 s += sprintf (s, "%s,", reg_names[31]);
21387
21388 s += sprintf (s, "%d(%s)", (int)offset, reg_names[REGNO (base)]);
21389 return buffer;
21390 }
21391
21392 /* Return true if MEM1 and MEM2 use the same base register, and the
21393 offset of MEM2 equals the offset of MEM1 plus 4. FIRST_REG is the
21394 register into (from) which the contents of MEM1 will be loaded
21395 (stored), depending on the value of LOAD_P.
21396 SWAP_P is true when the 1st and 2nd instructions are swapped. */
21397
21398 static bool
21399 umips_load_store_pair_p_1 (bool load_p, bool swap_p,
21400 rtx first_reg, rtx mem1, rtx mem2)
21401 {
21402 rtx base1, base2;
21403 HOST_WIDE_INT offset1, offset2;
21404
21405 if (!MEM_P (mem1) || !MEM_P (mem2))
21406 return false;
21407
21408 mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
21409 mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
21410
21411 if (!REG_P (base1) || !rtx_equal_p (base1, base2))
21412 return false;
21413
21414 /* Avoid invalid load pair instructions. */
21415 if (load_p && REGNO (first_reg) == REGNO (base1))
21416 return false;
21417
21418 /* We must avoid this case for anti-dependence.
21419 Ex: lw $3, 4($3)
21420 lw $2, 0($3)
21421 first_reg is $2, but the base is $3. */
21422 if (load_p
21423 && swap_p
21424 && REGNO (first_reg) + 1 == REGNO (base1))
21425 return false;
21426
21427 if (offset2 != offset1 + 4)
21428 return false;
21429
21430 if (!UMIPS_12BIT_OFFSET_P (offset1))
21431 return false;
21432
21433 return true;
21434 }
21435
21436 bool
21437 mips_load_store_bonding_p (rtx *operands, machine_mode mode, bool load_p)
21438 {
21439 rtx reg1, reg2, mem1, mem2, base1, base2;
21440 enum reg_class rc1, rc2;
21441 HOST_WIDE_INT offset1, offset2;
21442
21443 if (load_p)
21444 {
21445 reg1 = operands[0];
21446 reg2 = operands[2];
21447 mem1 = operands[1];
21448 mem2 = operands[3];
21449 }
21450 else
21451 {
21452 reg1 = operands[1];
21453 reg2 = operands[3];
21454 mem1 = operands[0];
21455 mem2 = operands[2];
21456 }
21457
21458 if (mips_address_insns (XEXP (mem1, 0), mode, false) == 0
21459 || mips_address_insns (XEXP (mem2, 0), mode, false) == 0)
21460 return false;
21461
21462 mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
21463 mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
21464
21465 /* Base regs do not match. */
21466 if (!REG_P (base1) || !rtx_equal_p (base1, base2))
21467 return false;
21468
21469 /* Either of the loads is clobbering base register. It is legitimate to bond
21470 loads if second load clobbers base register. However, hardware does not
21471 support such bonding. */
21472 if (load_p
21473 && (REGNO (reg1) == REGNO (base1)
21474 || (REGNO (reg2) == REGNO (base1))))
21475 return false;
21476
21477 /* Loading in same registers. */
21478 if (load_p
21479 && REGNO (reg1) == REGNO (reg2))
21480 return false;
21481
21482 /* The loads/stores are not of same type. */
21483 rc1 = REGNO_REG_CLASS (REGNO (reg1));
21484 rc2 = REGNO_REG_CLASS (REGNO (reg2));
21485 if (rc1 != rc2
21486 && !reg_class_subset_p (rc1, rc2)
21487 && !reg_class_subset_p (rc2, rc1))
21488 return false;
21489
21490 if (abs (offset1 - offset2) != GET_MODE_SIZE (mode))
21491 return false;
21492
21493 return true;
21494 }
21495
21496 /* OPERANDS describes the operands to a pair of SETs, in the order
21497 dest1, src1, dest2, src2. Return true if the operands can be used
21498 in an LWP or SWP instruction; LOAD_P says which. */
21499
21500 bool
21501 umips_load_store_pair_p (bool load_p, rtx *operands)
21502 {
21503 rtx reg1, reg2, mem1, mem2;
21504
21505 if (load_p)
21506 {
21507 reg1 = operands[0];
21508 reg2 = operands[2];
21509 mem1 = operands[1];
21510 mem2 = operands[3];
21511 }
21512 else
21513 {
21514 reg1 = operands[1];
21515 reg2 = operands[3];
21516 mem1 = operands[0];
21517 mem2 = operands[2];
21518 }
21519
21520 if (REGNO (reg2) == REGNO (reg1) + 1)
21521 return umips_load_store_pair_p_1 (load_p, false, reg1, mem1, mem2);
21522
21523 if (REGNO (reg1) == REGNO (reg2) + 1)
21524 return umips_load_store_pair_p_1 (load_p, true, reg2, mem2, mem1);
21525
21526 return false;
21527 }
21528
21529 /* Return the assembly instruction for a microMIPS LWP or SWP in which
21530 the first register is REG and the first memory slot is MEM.
21531 LOAD_P is true for LWP. */
21532
21533 static void
21534 umips_output_load_store_pair_1 (bool load_p, rtx reg, rtx mem)
21535 {
21536 rtx ops[] = {reg, mem};
21537
21538 if (load_p)
21539 output_asm_insn ("lwp\t%0,%1", ops);
21540 else
21541 output_asm_insn ("swp\t%0,%1", ops);
21542 }
21543
21544 /* Output the assembly instruction for a microMIPS LWP or SWP instruction.
21545 LOAD_P and OPERANDS are as for umips_load_store_pair_p. */
21546
21547 void
21548 umips_output_load_store_pair (bool load_p, rtx *operands)
21549 {
21550 rtx reg1, reg2, mem1, mem2;
21551 if (load_p)
21552 {
21553 reg1 = operands[0];
21554 reg2 = operands[2];
21555 mem1 = operands[1];
21556 mem2 = operands[3];
21557 }
21558 else
21559 {
21560 reg1 = operands[1];
21561 reg2 = operands[3];
21562 mem1 = operands[0];
21563 mem2 = operands[2];
21564 }
21565
21566 if (REGNO (reg2) == REGNO (reg1) + 1)
21567 {
21568 umips_output_load_store_pair_1 (load_p, reg1, mem1);
21569 return;
21570 }
21571
21572 gcc_assert (REGNO (reg1) == REGNO (reg2) + 1);
21573 umips_output_load_store_pair_1 (load_p, reg2, mem2);
21574 }
21575
21576 /* Return true if REG1 and REG2 match the criteria for a movep insn. */
21577
21578 bool
21579 umips_movep_target_p (rtx reg1, rtx reg2)
21580 {
21581 int regno1, regno2, pair;
21582 unsigned int i;
21583 static const int match[8] = {
21584 0x00000060, /* 5, 6 */
21585 0x000000a0, /* 5, 7 */
21586 0x000000c0, /* 6, 7 */
21587 0x00200010, /* 4, 21 */
21588 0x00400010, /* 4, 22 */
21589 0x00000030, /* 4, 5 */
21590 0x00000050, /* 4, 6 */
21591 0x00000090 /* 4, 7 */
21592 };
21593
21594 if (!REG_P (reg1) || !REG_P (reg2))
21595 return false;
21596
21597 regno1 = REGNO (reg1);
21598 regno2 = REGNO (reg2);
21599
21600 if (!GP_REG_P (regno1) || !GP_REG_P (regno2))
21601 return false;
21602
21603 pair = (1 << regno1) | (1 << regno2);
21604
21605 for (i = 0; i < ARRAY_SIZE (match); i++)
21606 if (pair == match[i])
21607 return true;
21608
21609 return false;
21610 }
21611
21612 /* Return the size in bytes of the trampoline code, padded to
21613 TRAMPOLINE_ALIGNMENT bits. The static chain pointer and target
21614 function address immediately follow. */
21615
21616 int
21617 mips_trampoline_code_size (void)
21618 {
21619 if (TARGET_USE_PIC_FN_ADDR_REG)
21620 return 4 * 4;
21621 else if (ptr_mode == DImode)
21622 return 8 * 4;
21623 else if (ISA_HAS_LOAD_DELAY)
21624 return 6 * 4;
21625 else
21626 return 4 * 4;
21627 }
21628
21629 /* Implement TARGET_TRAMPOLINE_INIT. */
21630
21631 static void
21632 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
21633 {
21634 rtx addr, end_addr, high, low, opcode, mem;
21635 rtx trampoline[8];
21636 unsigned int i, j;
21637 HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
21638
21639 /* Work out the offsets of the pointers from the start of the
21640 trampoline code. */
21641 end_addr_offset = mips_trampoline_code_size ();
21642 static_chain_offset = end_addr_offset;
21643 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
21644
21645 /* Get pointers to the beginning and end of the code block. */
21646 addr = force_reg (Pmode, XEXP (m_tramp, 0));
21647 end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
21648
21649 #define OP(X) gen_int_mode (X, SImode)
21650
21651 /* Build up the code in TRAMPOLINE. */
21652 i = 0;
21653 if (TARGET_USE_PIC_FN_ADDR_REG)
21654 {
21655 /* $25 contains the address of the trampoline. Emit code of the form:
21656
21657 l[wd] $1, target_function_offset($25)
21658 l[wd] $static_chain, static_chain_offset($25)
21659 jr $1
21660 move $25,$1. */
21661 trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
21662 target_function_offset,
21663 PIC_FUNCTION_ADDR_REGNUM));
21664 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
21665 static_chain_offset,
21666 PIC_FUNCTION_ADDR_REGNUM));
21667 trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
21668 trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
21669 }
21670 else if (ptr_mode == DImode)
21671 {
21672 /* It's too cumbersome to create the full 64-bit address, so let's
21673 instead use:
21674
21675 move $1, $31
21676 bal 1f
21677 nop
21678 1: l[wd] $25, target_function_offset - 12($31)
21679 l[wd] $static_chain, static_chain_offset - 12($31)
21680 jr $25
21681 move $31, $1
21682
21683 where 12 is the offset of "1:" from the start of the code block. */
21684 trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
21685 trampoline[i++] = OP (MIPS_BAL (1));
21686 trampoline[i++] = OP (MIPS_NOP);
21687 trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
21688 target_function_offset - 12,
21689 RETURN_ADDR_REGNUM));
21690 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
21691 static_chain_offset - 12,
21692 RETURN_ADDR_REGNUM));
21693 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
21694 trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
21695 }
21696 else
21697 {
21698 /* If the target has load delays, emit:
21699
21700 lui $1, %hi(end_addr)
21701 lw $25, %lo(end_addr + ...)($1)
21702 lw $static_chain, %lo(end_addr + ...)($1)
21703 jr $25
21704 nop
21705
21706 Otherwise emit:
21707
21708 lui $1, %hi(end_addr)
21709 lw $25, %lo(end_addr + ...)($1)
21710 jr $25
21711 lw $static_chain, %lo(end_addr + ...)($1). */
21712
21713 /* Split END_ADDR into %hi and %lo values. Trampolines are aligned
21714 to 64 bits, so the %lo value will have the bottom 3 bits clear. */
21715 high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
21716 NULL, false, OPTAB_WIDEN);
21717 high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
21718 NULL, false, OPTAB_WIDEN);
21719 low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
21720
21721 /* Emit the LUI. */
21722 opcode = OP (MIPS_LUI (AT_REGNUM, 0));
21723 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
21724 NULL, false, OPTAB_WIDEN);
21725
21726 /* Emit the load of the target function. */
21727 opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
21728 target_function_offset - end_addr_offset,
21729 AT_REGNUM));
21730 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
21731 NULL, false, OPTAB_WIDEN);
21732
21733 /* Emit the JR here, if we can. */
21734 if (!ISA_HAS_LOAD_DELAY)
21735 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
21736
21737 /* Emit the load of the static chain register. */
21738 opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
21739 static_chain_offset - end_addr_offset,
21740 AT_REGNUM));
21741 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
21742 NULL, false, OPTAB_WIDEN);
21743
21744 /* Emit the JR, if we couldn't above. */
21745 if (ISA_HAS_LOAD_DELAY)
21746 {
21747 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
21748 trampoline[i++] = OP (MIPS_NOP);
21749 }
21750 }
21751
21752 #undef OP
21753
21754 /* If we are using compact branches we don't have delay slots so
21755 place the instruction that was in the delay slot before the JRC
21756 instruction. */
21757
21758 if (TARGET_CB_ALWAYS)
21759 {
21760 rtx temp;
21761 temp = trampoline[i-2];
21762 trampoline[i-2] = trampoline[i-1];
21763 trampoline[i-1] = temp;
21764 }
21765
21766 /* Copy the trampoline code. Leave any padding uninitialized. */
21767 for (j = 0; j < i; j++)
21768 {
21769 mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
21770 mips_emit_move (mem, trampoline[j]);
21771 }
21772
21773 /* Set up the static chain pointer field. */
21774 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
21775 mips_emit_move (mem, chain_value);
21776
21777 /* Set up the target function field. */
21778 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
21779 mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
21780
21781 /* Flush the code part of the trampoline. */
21782 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
21783 emit_insn (gen_clear_cache (addr, end_addr));
21784 }
21785
21786 /* Implement FUNCTION_PROFILER. */
21787
21788 void mips_function_profiler (FILE *file)
21789 {
21790 if (TARGET_MIPS16)
21791 sorry ("mips16 function profiling");
21792 if (TARGET_LONG_CALLS)
21793 {
21794 /* For TARGET_LONG_CALLS use $3 for the address of _mcount. */
21795 if (Pmode == DImode)
21796 fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
21797 else
21798 fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
21799 }
21800 mips_push_asm_switch (&mips_noat);
21801 fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
21802 reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
21803 /* _mcount treats $2 as the static chain register. */
21804 if (cfun->static_chain_decl != NULL)
21805 fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
21806 reg_names[STATIC_CHAIN_REGNUM]);
21807 if (TARGET_MCOUNT_RA_ADDRESS)
21808 {
21809 /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
21810 ra save location. */
21811 if (cfun->machine->frame.ra_fp_offset == 0)
21812 /* ra not saved, pass zero. */
21813 fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
21814 else
21815 fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
21816 Pmode == DImode ? "dla" : "la", reg_names[12],
21817 cfun->machine->frame.ra_fp_offset,
21818 reg_names[STACK_POINTER_REGNUM]);
21819 }
21820 if (!TARGET_NEWABI)
21821 fprintf (file,
21822 "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from stack\n",
21823 TARGET_64BIT ? "dsubu" : "subu",
21824 reg_names[STACK_POINTER_REGNUM],
21825 reg_names[STACK_POINTER_REGNUM],
21826 Pmode == DImode ? 16 : 8);
21827
21828 if (TARGET_LONG_CALLS)
21829 fprintf (file, "\tjalr\t%s\n", reg_names[3]);
21830 else
21831 fprintf (file, "\tjal\t_mcount\n");
21832 mips_pop_asm_switch (&mips_noat);
21833 /* _mcount treats $2 as the static chain register. */
21834 if (cfun->static_chain_decl != NULL)
21835 fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
21836 reg_names[2]);
21837 }
21838
21839 /* Implement TARGET_SHIFT_TRUNCATION_MASK. We want to keep the default
21840 behavior of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
21841 when TARGET_LOONGSON_MMI is true. */
21842
21843 static unsigned HOST_WIDE_INT
21844 mips_shift_truncation_mask (machine_mode mode)
21845 {
21846 if (TARGET_LOONGSON_MMI && VECTOR_MODE_P (mode))
21847 return 0;
21848
21849 return GET_MODE_BITSIZE (mode) - 1;
21850 }
21851
21852 /* Implement TARGET_PREPARE_PCH_SAVE. */
21853
21854 static void
21855 mips_prepare_pch_save (void)
21856 {
21857 /* We are called in a context where the current compression vs.
21858 non-compression setting should be irrelevant. The question then is:
21859 which setting makes most sense at load time?
21860
21861 The PCH is loaded before the first token is read. We should never have
21862 switched into a compression mode by that point, and thus should not have
21863 populated mips16_globals or micromips_globals. Nor can we load the
21864 entire contents of mips16_globals or micromips_globals from the PCH file,
21865 because they contain a combination of GGC and non-GGC data.
21866
21867 There is therefore no point in trying save the GGC part of
21868 mips16_globals/micromips_globals to the PCH file, or to preserve a
21869 compression setting across the PCH save and load. The loading compiler
21870 would not have access to the non-GGC parts of mips16_globals or
21871 micromips_globals (either from the PCH file, or from a copy that the
21872 loading compiler generated itself) and would have to call target_reinit
21873 anyway.
21874
21875 It therefore seems best to switch back to non-MIPS16 mode and
21876 non-microMIPS mode to save time, and to ensure that mips16_globals and
21877 micromips_globals remain null after a PCH load. */
21878 mips_set_compression_mode (0);
21879 mips16_globals = 0;
21880 micromips_globals = 0;
21881 }
21882
21883 /* Generate or test for an insn that supports a constant permutation. */
21884
21885 #define MAX_VECT_LEN 16
21886
21887 struct expand_vec_perm_d
21888 {
21889 rtx target, op0, op1;
21890 unsigned char perm[MAX_VECT_LEN];
21891 machine_mode vmode;
21892 unsigned char nelt;
21893 bool one_vector_p;
21894 bool testing_p;
21895 };
21896
21897 /* Construct (set target (vec_select op0 (parallel perm))) and
21898 return true if that's a valid instruction in the active ISA. */
21899
21900 static bool
21901 mips_expand_vselect (rtx target, rtx op0,
21902 const unsigned char *perm, unsigned nelt)
21903 {
21904 rtx rperm[MAX_VECT_LEN], x;
21905 rtx_insn *insn;
21906 unsigned i;
21907
21908 for (i = 0; i < nelt; ++i)
21909 rperm[i] = GEN_INT (perm[i]);
21910
21911 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
21912 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
21913 x = gen_rtx_SET (target, x);
21914
21915 insn = emit_insn (x);
21916 if (recog_memoized (insn) < 0)
21917 {
21918 remove_insn (insn);
21919 return false;
21920 }
21921 return true;
21922 }
21923
21924 /* Similar, but generate a vec_concat from op0 and op1 as well. */
21925
21926 static bool
21927 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
21928 const unsigned char *perm, unsigned nelt)
21929 {
21930 machine_mode v2mode;
21931 rtx x;
21932
21933 if (!GET_MODE_2XWIDER_MODE (GET_MODE (op0)).exists (&v2mode))
21934 return false;
21935 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
21936 return mips_expand_vselect (target, x, perm, nelt);
21937 }
21938
21939 /* Recognize patterns for even-odd extraction. */
21940
21941 static bool
21942 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
21943 {
21944 unsigned i, odd, nelt = d->nelt;
21945 rtx t0, t1, t2, t3;
21946
21947 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_MMI))
21948 return false;
21949 /* Even-odd for V2SI/V2SFmode is matched by interleave directly. */
21950 if (nelt < 4)
21951 return false;
21952
21953 odd = d->perm[0];
21954 if (odd > 1)
21955 return false;
21956 for (i = 1; i < nelt; ++i)
21957 if (d->perm[i] != i * 2 + odd)
21958 return false;
21959
21960 if (d->testing_p)
21961 return true;
21962
21963 /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
21964 t0 = gen_reg_rtx (d->vmode);
21965 t1 = gen_reg_rtx (d->vmode);
21966 switch (d->vmode)
21967 {
21968 case E_V4HImode:
21969 emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
21970 emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
21971 if (odd)
21972 emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
21973 else
21974 emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
21975 break;
21976
21977 case E_V8QImode:
21978 t2 = gen_reg_rtx (d->vmode);
21979 t3 = gen_reg_rtx (d->vmode);
21980 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
21981 emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
21982 emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
21983 emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
21984 if (odd)
21985 emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
21986 else
21987 emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
21988 break;
21989
21990 default:
21991 gcc_unreachable ();
21992 }
21993 return true;
21994 }
21995
21996 /* Recognize patterns for the Loongson PSHUFH instruction. */
21997
21998 static bool
21999 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
22000 {
22001 unsigned i, mask;
22002 rtx rmask;
22003
22004 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_MMI))
22005 return false;
22006 if (d->vmode != V4HImode)
22007 return false;
22008 if (d->testing_p)
22009 return true;
22010
22011 /* Convert the selector into the packed 8-bit form for pshufh. */
22012 /* Recall that loongson is little-endian only. No big-endian
22013 adjustment required. */
22014 for (i = mask = 0; i < 4; i++)
22015 mask |= (d->perm[i] & 3) << (i * 2);
22016 rmask = force_reg (SImode, GEN_INT (mask));
22017
22018 if (d->one_vector_p)
22019 emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
22020 else
22021 {
22022 rtx t0, t1, x, merge, rmerge[4];
22023
22024 t0 = gen_reg_rtx (V4HImode);
22025 t1 = gen_reg_rtx (V4HImode);
22026 emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
22027 emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
22028
22029 for (i = 0; i < 4; ++i)
22030 rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
22031 merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
22032 merge = force_reg (V4HImode, merge);
22033
22034 x = gen_rtx_AND (V4HImode, merge, t1);
22035 emit_insn (gen_rtx_SET (t1, x));
22036
22037 x = gen_rtx_NOT (V4HImode, merge);
22038 x = gen_rtx_AND (V4HImode, x, t0);
22039 emit_insn (gen_rtx_SET (t0, x));
22040
22041 x = gen_rtx_IOR (V4HImode, t0, t1);
22042 emit_insn (gen_rtx_SET (d->target, x));
22043 }
22044
22045 return true;
22046 }
22047
22048 /* Recognize broadcast patterns for the Loongson. */
22049
22050 static bool
22051 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
22052 {
22053 unsigned i, elt;
22054 rtx t0, t1;
22055
22056 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_MMI))
22057 return false;
22058 /* Note that we've already matched V2SI via punpck and V4HI via pshufh. */
22059 if (d->vmode != V8QImode)
22060 return false;
22061 if (!d->one_vector_p)
22062 return false;
22063
22064 elt = d->perm[0];
22065 for (i = 1; i < 8; ++i)
22066 if (d->perm[i] != elt)
22067 return false;
22068
22069 if (d->testing_p)
22070 return true;
22071
22072 /* With one interleave we put two of the desired element adjacent. */
22073 t0 = gen_reg_rtx (V8QImode);
22074 if (elt < 4)
22075 emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
22076 else
22077 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
22078
22079 /* Shuffle that one HImode element into all locations. */
22080 elt &= 3;
22081 elt *= 0x55;
22082 t1 = gen_reg_rtx (V4HImode);
22083 emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
22084 force_reg (SImode, GEN_INT (elt))));
22085
22086 emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
22087 return true;
22088 }
22089
22090 /* Construct (set target (vec_select op0 (parallel selector))) and
22091 return true if that's a valid instruction in the active ISA. */
22092
22093 static bool
22094 mips_expand_msa_shuffle (struct expand_vec_perm_d *d)
22095 {
22096 rtx x, elts[MAX_VECT_LEN];
22097 rtvec v;
22098 rtx_insn *insn;
22099 unsigned i;
22100
22101 if (!ISA_HAS_MSA)
22102 return false;
22103
22104 for (i = 0; i < d->nelt; i++)
22105 elts[i] = GEN_INT (d->perm[i]);
22106
22107 v = gen_rtvec_v (d->nelt, elts);
22108 x = gen_rtx_PARALLEL (VOIDmode, v);
22109
22110 if (!mips_const_vector_shuffle_set_p (x, d->vmode))
22111 return false;
22112
22113 x = gen_rtx_VEC_SELECT (d->vmode, d->op0, x);
22114 x = gen_rtx_SET (d->target, x);
22115
22116 insn = emit_insn (x);
22117 if (recog_memoized (insn) < 0)
22118 {
22119 remove_insn (insn);
22120 return false;
22121 }
22122 return true;
22123 }
22124
22125 static bool
22126 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
22127 {
22128 unsigned int i, nelt = d->nelt;
22129 unsigned char perm2[MAX_VECT_LEN];
22130
22131 if (d->one_vector_p)
22132 {
22133 /* Try interleave with alternating operands. */
22134 memcpy (perm2, d->perm, sizeof(perm2));
22135 for (i = 1; i < nelt; i += 2)
22136 perm2[i] += nelt;
22137 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
22138 return true;
22139 }
22140 else
22141 {
22142 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
22143 d->perm, nelt))
22144 return true;
22145
22146 /* Try again with swapped operands. */
22147 for (i = 0; i < nelt; ++i)
22148 perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
22149 if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
22150 return true;
22151 }
22152
22153 if (mips_expand_vpc_loongson_even_odd (d))
22154 return true;
22155 if (mips_expand_vpc_loongson_pshufh (d))
22156 return true;
22157 if (mips_expand_vpc_loongson_bcast (d))
22158 return true;
22159 if (mips_expand_msa_shuffle (d))
22160 return true;
22161 return false;
22162 }
22163
22164 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST. */
22165
22166 static bool
22167 mips_vectorize_vec_perm_const (machine_mode vmode, machine_mode op_mode,
22168 rtx target, rtx op0, rtx op1,
22169 const vec_perm_indices &sel)
22170 {
22171 if (vmode != op_mode)
22172 return false;
22173
22174 struct expand_vec_perm_d d;
22175 int i, nelt, which;
22176 unsigned char orig_perm[MAX_VECT_LEN];
22177 bool ok;
22178
22179 d.target = target;
22180 if (op0)
22181 {
22182 rtx nop0 = force_reg (vmode, op0);
22183 if (op0 == op1)
22184 op1 = nop0;
22185 op0 = nop0;
22186 }
22187 if (op1)
22188 op1 = force_reg (vmode, op1);
22189 d.op0 = op0;
22190 d.op1 = op1;
22191
22192 d.vmode = vmode;
22193 gcc_assert (VECTOR_MODE_P (vmode));
22194 d.nelt = nelt = GET_MODE_NUNITS (vmode);
22195 d.testing_p = !target;
22196
22197 /* This is overly conservative, but ensures we don't get an
22198 uninitialized warning on ORIG_PERM. */
22199 memset (orig_perm, 0, MAX_VECT_LEN);
22200 for (i = which = 0; i < nelt; ++i)
22201 {
22202 int ei = sel[i] & (2 * nelt - 1);
22203 which |= (ei < nelt ? 1 : 2);
22204 orig_perm[i] = ei;
22205 }
22206 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
22207
22208 switch (which)
22209 {
22210 default:
22211 gcc_unreachable();
22212
22213 case 3:
22214 d.one_vector_p = false;
22215 if (d.testing_p || !rtx_equal_p (d.op0, d.op1))
22216 break;
22217 /* FALLTHRU */
22218
22219 case 2:
22220 for (i = 0; i < nelt; ++i)
22221 d.perm[i] &= nelt - 1;
22222 d.op0 = d.op1;
22223 d.one_vector_p = true;
22224 break;
22225
22226 case 1:
22227 d.op1 = d.op0;
22228 d.one_vector_p = true;
22229 break;
22230 }
22231
22232 if (d.testing_p)
22233 {
22234 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
22235 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
22236 if (!d.one_vector_p)
22237 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
22238
22239 start_sequence ();
22240 ok = mips_expand_vec_perm_const_1 (&d);
22241 end_sequence ();
22242 return ok;
22243 }
22244
22245 ok = mips_expand_vec_perm_const_1 (&d);
22246
22247 /* If we were given a two-vector permutation which just happened to
22248 have both input vectors equal, we folded this into a one-vector
22249 permutation. There are several loongson patterns that are matched
22250 via direct vec_select+vec_concat expansion, but we do not have
22251 support in mips_expand_vec_perm_const_1 to guess the adjustment
22252 that should be made for a single operand. Just try again with
22253 the original permutation. */
22254 if (!ok && which == 3)
22255 {
22256 d.op0 = op0;
22257 d.op1 = op1;
22258 d.one_vector_p = false;
22259 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
22260 ok = mips_expand_vec_perm_const_1 (&d);
22261 }
22262
22263 return ok;
22264 }
22265
22266 /* Expand a vector reduction. FN is the binary pattern to reduce;
22267 DEST is the destination; IN is the input vector. */
22268
22269 void
22270 mips_expand_msa_reduc (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
22271 {
22272 rtx swap, vec = in;
22273 machine_mode mode = GET_MODE (in);
22274 unsigned int i, gelt;
22275 const unsigned nelt = GET_MODE_BITSIZE (mode) / GET_MODE_UNIT_BITSIZE (mode);
22276 unsigned char perm[MAX_VECT_LEN];
22277
22278 /* We have no SHF.d. */
22279 if (nelt == 2)
22280 {
22281 perm[0] = 2;
22282 perm[1] = 3;
22283 perm[2] = 0;
22284 perm[3] = 1;
22285 rtx rsi = simplify_gen_subreg (V4SImode, in, mode, 0);
22286 swap = gen_reg_rtx (V4SImode);
22287 mips_expand_vselect (swap, rsi, perm, 4);
22288 emit_move_insn (dest, gen_rtx_SUBREG (mode, swap, 0));
22289 emit_insn (fn (dest, dest, vec));
22290 return;
22291 }
22292
22293 for (gelt=1; gelt<=nelt/2; gelt *= 2)
22294 {
22295 for (i = 0; i<nelt; i++)
22296 perm[i] = ((i/gelt)%2) ? (i-gelt) : (i+gelt);
22297 if (gelt == nelt/2)
22298 swap = dest;
22299 else
22300 swap = gen_reg_rtx (mode);
22301 mips_expand_vselect (swap, vec, perm, nelt);
22302 emit_insn (fn (swap, swap, vec));
22303 vec = swap;
22304 }
22305 }
22306
22307 /* Implement TARGET_SCHED_REASSOCIATION_WIDTH. */
22308
22309 static int
22310 mips_sched_reassociation_width (unsigned int opc ATTRIBUTE_UNUSED,
22311 machine_mode mode)
22312 {
22313 if (MSA_SUPPORTED_MODE_P (mode))
22314 return 2;
22315 return 1;
22316 }
22317
22318 /* Expand an integral vector unpack operation. */
22319
22320 void
22321 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
22322 {
22323 machine_mode imode = GET_MODE (operands[1]);
22324 rtx (*unpack) (rtx, rtx, rtx);
22325 rtx (*cmpFunc) (rtx, rtx, rtx);
22326 rtx tmp, dest, zero;
22327
22328 if (MSA_SUPPORTED_MODE_P (imode))
22329 {
22330 switch (imode)
22331 {
22332 case E_V4SImode:
22333 if (BYTES_BIG_ENDIAN != high_p)
22334 unpack = gen_msa_ilvl_w;
22335 else
22336 unpack = gen_msa_ilvr_w;
22337
22338 cmpFunc = gen_msa_clt_s_w;
22339 break;
22340
22341 case E_V8HImode:
22342 if (BYTES_BIG_ENDIAN != high_p)
22343 unpack = gen_msa_ilvl_h;
22344 else
22345 unpack = gen_msa_ilvr_h;
22346
22347 cmpFunc = gen_msa_clt_s_h;
22348 break;
22349
22350 case E_V16QImode:
22351 if (BYTES_BIG_ENDIAN != high_p)
22352 unpack = gen_msa_ilvl_b;
22353 else
22354 unpack = gen_msa_ilvr_b;
22355
22356 cmpFunc = gen_msa_clt_s_b;
22357 break;
22358
22359 default:
22360 gcc_unreachable ();
22361 break;
22362 }
22363
22364 if (!unsigned_p)
22365 {
22366 /* Extract sign extention for each element comparing each element
22367 with immediate zero. */
22368 tmp = gen_reg_rtx (imode);
22369 emit_insn (cmpFunc (tmp, operands[1], CONST0_RTX (imode)));
22370 }
22371 else
22372 tmp = force_reg (imode, CONST0_RTX (imode));
22373
22374 dest = gen_reg_rtx (imode);
22375
22376 emit_insn (unpack (dest, operands[1], tmp));
22377 emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
22378 return;
22379 }
22380
22381 switch (imode)
22382 {
22383 case E_V8QImode:
22384 if (high_p)
22385 unpack = gen_loongson_punpckhbh;
22386 else
22387 unpack = gen_loongson_punpcklbh;
22388 cmpFunc = gen_loongson_pcmpgtb;
22389 break;
22390 case E_V4HImode:
22391 if (high_p)
22392 unpack = gen_loongson_punpckhhw;
22393 else
22394 unpack = gen_loongson_punpcklhw;
22395 cmpFunc = gen_loongson_pcmpgth;
22396 break;
22397 default:
22398 gcc_unreachable ();
22399 }
22400
22401 zero = force_reg (imode, CONST0_RTX (imode));
22402 if (unsigned_p)
22403 tmp = zero;
22404 else
22405 {
22406 tmp = gen_reg_rtx (imode);
22407 emit_insn (cmpFunc (tmp, zero, operands[1]));
22408 }
22409
22410 dest = gen_reg_rtx (imode);
22411 emit_insn (unpack (dest, operands[1], tmp));
22412
22413 emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
22414 }
22415
22416 /* Construct and return PARALLEL RTX with CONST_INTs for HIGH (high_p == TRUE)
22417 or LOW (high_p == FALSE) half of a vector for mode MODE. */
22418
22419 rtx
22420 mips_msa_vec_parallel_const_half (machine_mode mode, bool high_p)
22421 {
22422 int nunits = GET_MODE_NUNITS (mode);
22423 rtvec v = rtvec_alloc (nunits / 2);
22424 int base;
22425 int i;
22426
22427 if (BYTES_BIG_ENDIAN)
22428 base = high_p ? 0 : nunits / 2;
22429 else
22430 base = high_p ? nunits / 2 : 0;
22431
22432 for (i = 0; i < nunits / 2; i++)
22433 RTVEC_ELT (v, i) = GEN_INT (base + i);
22434
22435 return gen_rtx_PARALLEL (VOIDmode, v);
22436 }
22437
22438 /* Construct and return i8 of SHF.df. No error will happen since tt has
22439 been constrained by mips_const_vector_shuffle_set_p.
22440 Return (IMM | (INSN << 8)): The range of IMM is [0, 0xFF].
22441 The INSN can be 0 (error)/1 (SHF.B)/2 (SHF.H)/4 (SHF.W). */
22442
22443 HOST_WIDE_INT
22444 mips_msa_shf_i8 (rtx *operands)
22445 {
22446 HOST_WIDE_INT rval = 0, val[16];
22447 unsigned int i;
22448 machine_mode mode = GET_MODE (operands[0]);
22449 int which_op = 0;
22450
22451 /* We use shf.w to swap 2 doubleword part. */
22452 if (mode == V2DImode || mode == V2DFmode)
22453 {
22454 val[0] = INTVAL (XVECEXP (operands[2], 0, 0));
22455 val[1] = INTVAL (XVECEXP (operands[2], 0, 1));
22456 val[3] = val[1] == 0 ? 1 : 3;
22457 val[2] = val[1] == 0 ? 0 : 2;
22458 val[1] = val[0] == 0 ? 1 : 3;
22459 val[0] = val[0] == 0 ? 0 : 2;
22460 which_op = 4;
22461 }
22462 else if (mode == V16QImode)
22463 {
22464 for (i = 0; i < 16; i++)
22465 val[i] = INTVAL (XVECEXP (operands[2], 0, i));
22466 if (val[1] - val[0] == 1
22467 && val[2] - val[1] == 1
22468 && val[3] - val[2] == 1)
22469 {
22470 which_op = 4;
22471 val[0] = val[0] / 4;
22472 val[1] = val[4] / 4;
22473 val[2] = val[8] / 4;
22474 val[3] = val[12] / 4;
22475 }
22476 else if (val[1] - val[0] == 1
22477 && val[3] - val[2] == 1)
22478 {
22479 which_op = 2;
22480 val[0] = val[0] / 2;
22481 val[1] = val[2] / 2;
22482 val[2] = val[4] / 2;
22483 val[3] = val[6] / 2;
22484 }
22485 else
22486 which_op = 1;
22487 }
22488 else if (mode == V8HImode)
22489 {
22490 for (i = 0; i < 8; i++)
22491 val[i] = INTVAL (XVECEXP (operands[2], 0, i));
22492 if (val[1] - val[0] == 1
22493 && val[3] - val[2] == 1
22494 && val[5] - val[4] == 1
22495 && val[7] - val[6] == 1)
22496 {
22497 which_op = 4;
22498 val[0] = val[0] / 2;
22499 val[1] = val[2] / 2;
22500 val[2] = val[4] / 2;
22501 val[3] = val[6] / 2;
22502 }
22503 else
22504 which_op = 2;
22505 }
22506 else if (mode == V4SImode || mode == V4SFmode)
22507 {
22508 for (i = 0; i < 4; i++)
22509 val[i] = INTVAL (XVECEXP (operands[2], 0, i));
22510 which_op = 4;
22511 }
22512
22513 /* We convert the selection to an immediate. */
22514 for (i = 0; i < 4; i++)
22515 rval |= val[i] << (2 * i);
22516
22517 rval |= (which_op << 8);
22518 return rval;
22519 }
22520
22521 /* A subroutine of mips_expand_vec_init, match constant vector elements. */
22522
22523 static inline bool
22524 mips_constant_elt_p (rtx x)
22525 {
22526 return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
22527 }
22528
22529 /* A subroutine of mips_expand_vec_init, expand via broadcast. */
22530
22531 static void
22532 mips_expand_vi_broadcast (machine_mode vmode, rtx target, rtx elt)
22533 {
22534 struct expand_vec_perm_d d;
22535 rtx t1;
22536 bool ok;
22537
22538 if (elt != const0_rtx)
22539 elt = force_reg (GET_MODE_INNER (vmode), elt);
22540 if (REG_P (elt))
22541 elt = gen_lowpart (DImode, elt);
22542
22543 t1 = gen_reg_rtx (vmode);
22544 switch (vmode)
22545 {
22546 case E_V8QImode:
22547 emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
22548 break;
22549 case E_V4HImode:
22550 emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
22551 break;
22552 default:
22553 gcc_unreachable ();
22554 }
22555
22556 memset (&d, 0, sizeof (d));
22557 d.target = target;
22558 d.op0 = t1;
22559 d.op1 = t1;
22560 d.vmode = vmode;
22561 d.nelt = GET_MODE_NUNITS (vmode);
22562 d.one_vector_p = true;
22563
22564 ok = mips_expand_vec_perm_const_1 (&d);
22565 gcc_assert (ok);
22566 }
22567
22568 /* Return a const_int vector of VAL with mode MODE. */
22569
22570 rtx
22571 mips_gen_const_int_vector (machine_mode mode, HOST_WIDE_INT val)
22572 {
22573 rtx c = gen_int_mode (val, GET_MODE_INNER (mode));
22574 return gen_const_vec_duplicate (mode, c);
22575 }
22576
22577 /* Return a vector of repeated 4-element sets generated from
22578 immediate VAL in mode MODE. */
22579
22580 static rtx
22581 mips_gen_const_int_vector_shuffle (machine_mode mode, int val)
22582 {
22583 int nunits = GET_MODE_NUNITS (mode);
22584 int nsets = nunits / 4;
22585 rtx elts[MAX_VECT_LEN];
22586 int set = 0;
22587 int i, j;
22588
22589 /* Generate a const_int vector replicating the same 4-element set
22590 from an immediate. */
22591 for (j = 0; j < nsets; j++, set = 4 * j)
22592 for (i = 0; i < 4; i++)
22593 elts[set + i] = GEN_INT (set + ((val >> (2 * i)) & 0x3));
22594
22595 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nunits, elts));
22596 }
22597
22598 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
22599 elements of VALS with zeros, copy the constant vector to TARGET. */
22600
22601 static void
22602 mips_expand_vi_constant (machine_mode vmode, unsigned nelt,
22603 rtx target, rtx vals)
22604 {
22605 rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
22606 unsigned i;
22607
22608 for (i = 0; i < nelt; ++i)
22609 {
22610 rtx elem = RTVEC_ELT (vec, i);
22611 if (!mips_constant_elt_p (elem))
22612 RTVEC_ELT (vec, i) = CONST0_RTX (GET_MODE (elem));
22613 }
22614
22615 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
22616 }
22617
22618
22619 /* A subroutine of mips_expand_vec_init, expand via pinsrh. */
22620
22621 static void
22622 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
22623 {
22624 mips_expand_vi_constant (V4HImode, 4, target, vals);
22625
22626 emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
22627 GEN_INT (one_var)));
22628 }
22629
22630 /* A subroutine of mips_expand_vec_init, expand anything via memory. */
22631
22632 static void
22633 mips_expand_vi_general (machine_mode vmode, machine_mode imode,
22634 unsigned nelt, unsigned nvar, rtx target, rtx vals)
22635 {
22636 rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
22637 unsigned int i, isize = GET_MODE_SIZE (imode);
22638
22639 if (nvar < nelt)
22640 mips_expand_vi_constant (vmode, nelt, mem, vals);
22641
22642 for (i = 0; i < nelt; ++i)
22643 {
22644 rtx x = XVECEXP (vals, 0, i);
22645 if (!mips_constant_elt_p (x))
22646 emit_move_insn (adjust_address (mem, imode, i * isize), x);
22647 }
22648
22649 emit_move_insn (target, mem);
22650 }
22651
22652 /* Expand a vector initialization. */
22653
22654 void
22655 mips_expand_vector_init (rtx target, rtx vals)
22656 {
22657 machine_mode vmode = GET_MODE (target);
22658 machine_mode imode = GET_MODE_INNER (vmode);
22659 unsigned i, nelt = GET_MODE_NUNITS (vmode);
22660 unsigned nvar = 0, one_var = -1u;
22661 bool all_same = true;
22662 rtx x;
22663
22664 for (i = 0; i < nelt; ++i)
22665 {
22666 x = XVECEXP (vals, 0, i);
22667 if (!mips_constant_elt_p (x))
22668 nvar++, one_var = i;
22669 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
22670 all_same = false;
22671 }
22672
22673 if (MSA_SUPPORTED_MODE_P (vmode))
22674 {
22675 if (all_same)
22676 {
22677 rtx same = XVECEXP (vals, 0, 0);
22678 rtx temp, temp2;
22679
22680 if (CONST_INT_P (same) && nvar == 0
22681 && mips_signed_immediate_p (INTVAL (same), 10, 0))
22682 {
22683 switch (vmode)
22684 {
22685 case E_V16QImode:
22686 case E_V8HImode:
22687 case E_V4SImode:
22688 case E_V2DImode:
22689 temp = gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0));
22690 emit_move_insn (target, temp);
22691 return;
22692
22693 default:
22694 gcc_unreachable ();
22695 }
22696 }
22697 temp = gen_reg_rtx (imode);
22698 if (imode == GET_MODE (same))
22699 temp2 = same;
22700 else if (GET_MODE_SIZE (imode) >= UNITS_PER_WORD)
22701 temp2 = simplify_gen_subreg (imode, same, GET_MODE (same), 0);
22702 else
22703 temp2 = lowpart_subreg (imode, same, GET_MODE (same));
22704 emit_move_insn (temp, temp2);
22705
22706 switch (vmode)
22707 {
22708 case E_V16QImode:
22709 case E_V8HImode:
22710 case E_V4SImode:
22711 case E_V2DImode:
22712 mips_emit_move (target, gen_rtx_VEC_DUPLICATE (vmode, temp));
22713 break;
22714
22715 case E_V4SFmode:
22716 emit_insn (gen_msa_splati_w_f_scalar (target, temp));
22717 break;
22718
22719 case E_V2DFmode:
22720 emit_insn (gen_msa_splati_d_f_scalar (target, temp));
22721 break;
22722
22723 default:
22724 gcc_unreachable ();
22725 }
22726 }
22727 else
22728 {
22729 emit_move_insn (target, CONST0_RTX (vmode));
22730
22731 for (i = 0; i < nelt; ++i)
22732 {
22733 rtx temp = gen_reg_rtx (imode);
22734 emit_move_insn (temp, XVECEXP (vals, 0, i));
22735 switch (vmode)
22736 {
22737 case E_V16QImode:
22738 emit_insn (gen_vec_setv16qi (target, temp, GEN_INT (i)));
22739 break;
22740
22741 case E_V8HImode:
22742 emit_insn (gen_vec_setv8hi (target, temp, GEN_INT (i)));
22743 break;
22744
22745 case E_V4SImode:
22746 emit_insn (gen_vec_setv4si (target, temp, GEN_INT (i)));
22747 break;
22748
22749 case E_V2DImode:
22750 emit_insn (gen_vec_setv2di (target, temp, GEN_INT (i)));
22751 break;
22752
22753 case E_V4SFmode:
22754 emit_insn (gen_vec_setv4sf (target, temp, GEN_INT (i)));
22755 break;
22756
22757 case E_V2DFmode:
22758 emit_insn (gen_vec_setv2df (target, temp, GEN_INT (i)));
22759 break;
22760
22761 default:
22762 gcc_unreachable ();
22763 }
22764 }
22765 }
22766 return;
22767 }
22768
22769 /* Load constants from the pool, or whatever's handy. */
22770 if (nvar == 0)
22771 {
22772 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
22773 return;
22774 }
22775
22776 /* For two-part initialization, always use CONCAT. */
22777 if (nelt == 2)
22778 {
22779 rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
22780 rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
22781 x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
22782 emit_insn (gen_rtx_SET (target, x));
22783 return;
22784 }
22785
22786 /* Loongson is the only cpu with vectors with more elements. */
22787 gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_MMI);
22788
22789 /* If all values are identical, broadcast the value. */
22790 if (all_same)
22791 {
22792 mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
22793 return;
22794 }
22795
22796 /* If we've only got one non-variable V4HImode, use PINSRH. */
22797 if (nvar == 1 && vmode == V4HImode)
22798 {
22799 mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
22800 return;
22801 }
22802
22803 mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
22804 }
22805
22806 /* Expand a vector reduction. */
22807
22808 void
22809 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
22810 {
22811 machine_mode vmode = GET_MODE (in);
22812 unsigned char perm2[2];
22813 rtx last, next, fold, x;
22814 bool ok;
22815
22816 last = in;
22817 fold = gen_reg_rtx (vmode);
22818 switch (vmode)
22819 {
22820 case E_V2SFmode:
22821 /* Use PUL/PLU to produce { L, H } op { H, L }.
22822 By reversing the pair order, rather than a pure interleave high,
22823 we avoid erroneous exceptional conditions that we might otherwise
22824 produce from the computation of H op H. */
22825 perm2[0] = 1;
22826 perm2[1] = 2;
22827 ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
22828 gcc_assert (ok);
22829 break;
22830
22831 case E_V2SImode:
22832 /* Use interleave to produce { H, L } op { H, H }. */
22833 emit_insn (gen_loongson_punpckhwd (fold, last, last));
22834 break;
22835
22836 case E_V4HImode:
22837 /* Perform the first reduction with interleave,
22838 and subsequent reductions with shifts. */
22839 emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
22840
22841 next = gen_reg_rtx (vmode);
22842 emit_insn (gen (next, last, fold));
22843 last = next;
22844
22845 fold = gen_reg_rtx (vmode);
22846 x = force_reg (SImode, GEN_INT (16));
22847 emit_insn (gen_vec_shr_v4hi (fold, last, x));
22848 break;
22849
22850 case E_V8QImode:
22851 emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
22852
22853 next = gen_reg_rtx (vmode);
22854 emit_insn (gen (next, last, fold));
22855 last = next;
22856
22857 fold = gen_reg_rtx (vmode);
22858 x = force_reg (SImode, GEN_INT (16));
22859 emit_insn (gen_vec_shr_v8qi (fold, last, x));
22860
22861 next = gen_reg_rtx (vmode);
22862 emit_insn (gen (next, last, fold));
22863 last = next;
22864
22865 fold = gen_reg_rtx (vmode);
22866 x = force_reg (SImode, GEN_INT (8));
22867 emit_insn (gen_vec_shr_v8qi (fold, last, x));
22868 break;
22869
22870 default:
22871 gcc_unreachable ();
22872 }
22873
22874 emit_insn (gen (target, last, fold));
22875 }
22876
22877 /* Expand a vector minimum/maximum. */
22878
22879 void
22880 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
22881 rtx (*cmp) (rtx, rtx, rtx), bool min_p)
22882 {
22883 machine_mode vmode = GET_MODE (target);
22884 rtx tc, t0, t1, x;
22885
22886 tc = gen_reg_rtx (vmode);
22887 t0 = gen_reg_rtx (vmode);
22888 t1 = gen_reg_rtx (vmode);
22889
22890 /* op0 > op1 */
22891 emit_insn (cmp (tc, op0, op1));
22892
22893 x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
22894 emit_insn (gen_rtx_SET (t0, x));
22895
22896 x = gen_rtx_NOT (vmode, tc);
22897 x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
22898 emit_insn (gen_rtx_SET (t1, x));
22899
22900 x = gen_rtx_IOR (vmode, t0, t1);
22901 emit_insn (gen_rtx_SET (target, x));
22902 }
22903
22904 /* Implement HARD_REGNO_CALLER_SAVE_MODE. */
22905
22906 machine_mode
22907 mips_hard_regno_caller_save_mode (unsigned int regno,
22908 unsigned int nregs,
22909 machine_mode mode)
22910 {
22911 /* For performance, avoid saving/restoring upper parts of a register
22912 by returning MODE as save mode when the mode is known. */
22913 if (mode == VOIDmode)
22914 return choose_hard_reg_mode (regno, nregs, NULL);
22915 else
22916 return mode;
22917 }
22918
22919 /* Generate RTL for comparing CMP_OP0 and CMP_OP1 using condition COND and
22920 store the result -1 or 0 in DEST. */
22921
22922 static void
22923 mips_expand_msa_cmp (rtx dest, enum rtx_code cond, rtx op0, rtx op1)
22924 {
22925 machine_mode cmp_mode = GET_MODE (op0);
22926 int unspec = -1;
22927 bool negate = false;
22928
22929 switch (cmp_mode)
22930 {
22931 case E_V16QImode:
22932 case E_V8HImode:
22933 case E_V4SImode:
22934 case E_V2DImode:
22935 switch (cond)
22936 {
22937 case NE:
22938 cond = reverse_condition (cond);
22939 negate = true;
22940 break;
22941 case EQ:
22942 case LT:
22943 case LE:
22944 case LTU:
22945 case LEU:
22946 break;
22947 case GE:
22948 case GT:
22949 case GEU:
22950 case GTU:
22951 std::swap (op0, op1);
22952 cond = swap_condition (cond);
22953 break;
22954 default:
22955 gcc_unreachable ();
22956 }
22957 mips_emit_binary (cond, dest, op0, op1);
22958 if (negate)
22959 emit_move_insn (dest, gen_rtx_NOT (GET_MODE (dest), dest));
22960 break;
22961
22962 case E_V4SFmode:
22963 case E_V2DFmode:
22964 switch (cond)
22965 {
22966 case UNORDERED:
22967 case ORDERED:
22968 case EQ:
22969 case NE:
22970 case UNEQ:
22971 case UNLE:
22972 case UNLT:
22973 break;
22974 case LTGT: cond = NE; break;
22975 case UNGE: cond = UNLE; std::swap (op0, op1); break;
22976 case UNGT: cond = UNLT; std::swap (op0, op1); break;
22977 case LE: unspec = UNSPEC_MSA_FSLE; break;
22978 case LT: unspec = UNSPEC_MSA_FSLT; break;
22979 case GE: unspec = UNSPEC_MSA_FSLE; std::swap (op0, op1); break;
22980 case GT: unspec = UNSPEC_MSA_FSLT; std::swap (op0, op1); break;
22981 default:
22982 gcc_unreachable ();
22983 }
22984 if (unspec < 0)
22985 mips_emit_binary (cond, dest, op0, op1);
22986 else
22987 {
22988 rtx x = gen_rtx_UNSPEC (GET_MODE (dest),
22989 gen_rtvec (2, op0, op1), unspec);
22990 emit_insn (gen_rtx_SET (dest, x));
22991 }
22992 break;
22993
22994 default:
22995 gcc_unreachable ();
22996 break;
22997 }
22998 }
22999
23000 void
23001 mips_expand_vec_cmp_expr (rtx *operands)
23002 {
23003 rtx cond = operands[1];
23004 rtx op0 = operands[2];
23005 rtx op1 = operands[3];
23006 rtx res = operands[0];
23007
23008 mips_expand_msa_cmp (res, GET_CODE (cond), op0, op1);
23009 }
23010
23011 /* Expand VEC_COND_EXPR, where:
23012 MODE is mode of the result
23013 VIMODE equivalent integer mode
23014 OPERANDS operands of VEC_COND_EXPR. */
23015
23016 void
23017 mips_expand_vec_cond_expr (machine_mode mode, machine_mode vimode,
23018 rtx *operands, bool mask)
23019 {
23020 rtx cmp_res;
23021 if (mask)
23022 cmp_res = operands[3];
23023 else
23024 {
23025 rtx cond = operands[3];
23026 rtx cmp_op0 = operands[4];
23027 rtx cmp_op1 = operands[5];
23028 cmp_res = gen_reg_rtx (vimode);
23029
23030 mips_expand_msa_cmp (cmp_res, GET_CODE (cond), cmp_op0, cmp_op1);
23031 }
23032
23033 /* We handle the following cases:
23034 1) r = a CMP b ? -1 : 0
23035 2) r = a CMP b ? -1 : v
23036 3) r = a CMP b ? v : 0
23037 4) r = a CMP b ? v1 : v2 */
23038
23039 /* Case (1) above. We only move the results. */
23040 if (operands[1] == CONSTM1_RTX (vimode)
23041 && operands[2] == CONST0_RTX (vimode))
23042 emit_move_insn (operands[0], cmp_res);
23043 else
23044 {
23045 rtx src1 = gen_reg_rtx (vimode);
23046 rtx src2 = gen_reg_rtx (vimode);
23047 rtx mask = gen_reg_rtx (vimode);
23048 rtx bsel;
23049
23050 /* Move the vector result to use it as a mask. */
23051 emit_move_insn (mask, cmp_res);
23052
23053 if (register_operand (operands[1], mode))
23054 {
23055 rtx xop1 = operands[1];
23056 if (mode != vimode)
23057 {
23058 xop1 = gen_reg_rtx (vimode);
23059 emit_move_insn (xop1, gen_lowpart (vimode, operands[1]));
23060 }
23061 emit_move_insn (src1, xop1);
23062 }
23063 else
23064 {
23065 gcc_assert (operands[1] == CONSTM1_RTX (vimode));
23066 /* Case (2) if the below doesn't move the mask to src2. */
23067 emit_move_insn (src1, mask);
23068 }
23069
23070 if (register_operand (operands[2], mode))
23071 {
23072 rtx xop2 = operands[2];
23073 if (mode != vimode)
23074 {
23075 xop2 = gen_reg_rtx (vimode);
23076 emit_move_insn (xop2, gen_lowpart (vimode, operands[2]));
23077 }
23078 emit_move_insn (src2, xop2);
23079 }
23080 else
23081 {
23082 gcc_assert (operands[2] == CONST0_RTX (mode));
23083 /* Case (3) if the above didn't move the mask to src1. */
23084 emit_move_insn (src2, mask);
23085 }
23086
23087 /* We deal with case (4) if the mask wasn't moved to either src1 or src2.
23088 In any case, we eventually do vector mask-based copy. */
23089 bsel = gen_rtx_IOR (vimode,
23090 gen_rtx_AND (vimode,
23091 gen_rtx_NOT (vimode, mask), src2),
23092 gen_rtx_AND (vimode, mask, src1));
23093 /* The result is placed back to a register with the mask. */
23094 emit_insn (gen_rtx_SET (mask, bsel));
23095 emit_move_insn (operands[0], gen_rtx_SUBREG (mode, mask, 0));
23096 }
23097 }
23098
23099 /* Implement TARGET_CASE_VALUES_THRESHOLD. */
23100
23101 unsigned int
23102 mips_case_values_threshold (void)
23103 {
23104 /* In MIPS16 mode using a larger case threshold generates smaller code. */
23105 if (TARGET_MIPS16 && optimize_size)
23106 return 10;
23107 else
23108 return default_case_values_threshold ();
23109 }
23110
23111 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
23112
23113 static void
23114 mips_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
23115 {
23116 if (!TARGET_HARD_FLOAT_ABI)
23117 return;
23118 tree exceptions_var = create_tmp_var_raw (MIPS_ATYPE_USI);
23119 tree fcsr_orig_var = create_tmp_var_raw (MIPS_ATYPE_USI);
23120 tree fcsr_mod_var = create_tmp_var_raw (MIPS_ATYPE_USI);
23121 tree get_fcsr = mips_builtin_decls[MIPS_GET_FCSR];
23122 tree set_fcsr = mips_builtin_decls[MIPS_SET_FCSR];
23123 tree get_fcsr_hold_call = build_call_expr (get_fcsr, 0);
23124 tree hold_assign_orig = build4 (TARGET_EXPR, MIPS_ATYPE_USI,
23125 fcsr_orig_var, get_fcsr_hold_call, NULL, NULL);
23126 tree hold_mod_val = build2 (BIT_AND_EXPR, MIPS_ATYPE_USI, fcsr_orig_var,
23127 build_int_cst (MIPS_ATYPE_USI, 0xfffff003));
23128 tree hold_assign_mod = build4 (TARGET_EXPR, MIPS_ATYPE_USI,
23129 fcsr_mod_var, hold_mod_val, NULL, NULL);
23130 tree set_fcsr_hold_call = build_call_expr (set_fcsr, 1, fcsr_mod_var);
23131 tree hold_all = build2 (COMPOUND_EXPR, MIPS_ATYPE_USI,
23132 hold_assign_orig, hold_assign_mod);
23133 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_all,
23134 set_fcsr_hold_call);
23135
23136 *clear = build_call_expr (set_fcsr, 1, fcsr_mod_var);
23137
23138 tree get_fcsr_update_call = build_call_expr (get_fcsr, 0);
23139 *update = build4 (TARGET_EXPR, MIPS_ATYPE_USI,
23140 exceptions_var, get_fcsr_update_call, NULL, NULL);
23141 tree set_fcsr_update_call = build_call_expr (set_fcsr, 1, fcsr_orig_var);
23142 *update = build2 (COMPOUND_EXPR, void_type_node, *update,
23143 set_fcsr_update_call);
23144 tree atomic_feraiseexcept
23145 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
23146 tree int_exceptions_var = fold_convert (integer_type_node,
23147 exceptions_var);
23148 tree atomic_feraiseexcept_call = build_call_expr (atomic_feraiseexcept,
23149 1, int_exceptions_var);
23150 *update = build2 (COMPOUND_EXPR, void_type_node, *update,
23151 atomic_feraiseexcept_call);
23152 }
23153
23154 /* Implement TARGET_SPILL_CLASS. */
23155
23156 static reg_class_t
23157 mips_spill_class (reg_class_t rclass ATTRIBUTE_UNUSED,
23158 machine_mode mode ATTRIBUTE_UNUSED)
23159 {
23160 if (TARGET_MIPS16)
23161 return SPILL_REGS;
23162 return NO_REGS;
23163 }
23164
23165 /* Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS. */
23166
23167 static reg_class_t
23168 mips_ira_change_pseudo_allocno_class (int regno, reg_class_t allocno_class,
23169 reg_class_t best_class ATTRIBUTE_UNUSED)
23170 {
23171 /* LRA will allocate an FPR for an integer mode pseudo instead of spilling
23172 to memory if an FPR is present in the allocno class. It is rare that
23173 we actually need to place an integer mode value in an FPR so where
23174 possible limit the allocation to GR_REGS. This will slightly pessimize
23175 code that involves integer to/from float conversions as these will have
23176 to reload into FPRs in LRA. Such reloads are sometimes eliminated and
23177 sometimes only partially eliminated. We choose to take this penalty
23178 in order to eliminate usage of FPRs in code that does not use floating
23179 point data.
23180
23181 This change has a similar effect to increasing the cost of FPR->GPR
23182 register moves for integer modes so that they are higher than the cost
23183 of memory but changing the allocno class is more reliable.
23184
23185 This is also similar to forbidding integer mode values in FPRs entirely
23186 but this would lead to an inconsistency in the integer to/from float
23187 instructions that say integer mode values must be placed in FPRs. */
23188 if (INTEGRAL_MODE_P (PSEUDO_REGNO_MODE (regno)) && allocno_class == ALL_REGS)
23189 return GR_REGS;
23190 return allocno_class;
23191 }
23192
23193 /* Implement TARGET_PROMOTE_FUNCTION_MODE */
23194
23195 /* This function is equivalent to default_promote_function_mode_always_promote
23196 except that it returns a promoted mode even if type is NULL_TREE. This is
23197 needed by libcalls which have no type (only a mode) such as fixed conversion
23198 routines that take a signed or unsigned char/short argument and convert it
23199 to a fixed type. */
23200
23201 static machine_mode
23202 mips_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
23203 machine_mode mode,
23204 int *punsignedp ATTRIBUTE_UNUSED,
23205 const_tree fntype ATTRIBUTE_UNUSED,
23206 int for_return ATTRIBUTE_UNUSED)
23207 {
23208 int unsignedp;
23209
23210 if (type != NULL_TREE)
23211 return promote_mode (type, mode, punsignedp);
23212
23213 unsignedp = *punsignedp;
23214 PROMOTE_MODE (mode, unsignedp, type);
23215 *punsignedp = unsignedp;
23216 return mode;
23217 }
23218
23219 /* Implement TARGET_TRULY_NOOP_TRUNCATION. */
23220
23221 static bool
23222 mips_truly_noop_truncation (poly_uint64 outprec, poly_uint64 inprec)
23223 {
23224 return !TARGET_64BIT || inprec <= 32 || outprec > 32;
23225 }
23226
23227 /* Implement TARGET_CONSTANT_ALIGNMENT. */
23228
23229 static HOST_WIDE_INT
23230 mips_constant_alignment (const_tree exp, HOST_WIDE_INT align)
23231 {
23232 if (TREE_CODE (exp) == STRING_CST || TREE_CODE (exp) == CONSTRUCTOR)
23233 return MAX (align, BITS_PER_WORD);
23234 return align;
23235 }
23236
23237 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
23238
23239 static unsigned HOST_WIDE_INT
23240 mips_asan_shadow_offset (void)
23241 {
23242 if (mips_abi == ABI_N32)
23243 return (HOST_WIDE_INT_1 << 29);
23244 if (POINTER_SIZE == 64)
23245 return (HOST_WIDE_INT_1 << 37);
23246 else
23247 return HOST_WIDE_INT_C (0x0aaa0000);
23248 }
23249
23250 /* Implement TARGET_STARTING_FRAME_OFFSET. See mips_compute_frame_info
23251 for details about the frame layout. */
23252
23253 static HOST_WIDE_INT
23254 mips_starting_frame_offset (void)
23255 {
23256 if (FRAME_GROWS_DOWNWARD)
23257 return 0;
23258 return crtl->outgoing_args_size + MIPS_GP_SAVE_AREA_SIZE;
23259 }
23260
23261 static void
23262 mips_asm_file_end (void)
23263 {
23264 if (NEED_INDICATE_EXEC_STACK)
23265 file_end_indicate_exec_stack ();
23266 }
23267
23268 /* Implement TARGET_C_MODE_FOR_FLOATING_TYPE. Return TFmode or DFmode
23269 for TI_LONG_DOUBLE_TYPE which is for long double type, go with the
23270 default one for the others. */
23271
23272 static machine_mode
23273 mips_c_mode_for_floating_type (enum tree_index ti)
23274 {
23275 if (ti == TI_LONG_DOUBLE_TYPE)
23276 return MIPS_LONG_DOUBLE_TYPE_SIZE == 64 ? DFmode : TFmode;
23277 return default_mode_for_floating_type (ti);
23278 }
23279
23280 void
23281 mips_bit_clear_info (enum machine_mode mode, unsigned HOST_WIDE_INT m,
23282 int *start_pos, int *size)
23283 {
23284 unsigned int shift = 0;
23285 unsigned int change_count = 0;
23286 unsigned int prev_val = 1;
23287 unsigned int curr_val = 0;
23288 unsigned int end_pos = GET_MODE_SIZE (mode) * BITS_PER_UNIT;
23289
23290 for (shift = 0 ; shift < (GET_MODE_SIZE (mode) * BITS_PER_UNIT) ; shift++)
23291 {
23292 curr_val = (unsigned int)((m & (unsigned int)(1 << shift)) >> shift);
23293 if (curr_val != prev_val)
23294 {
23295 change_count++;
23296 switch (change_count)
23297 {
23298 case 1:
23299 *start_pos = shift;
23300 break;
23301 case 2:
23302 end_pos = shift;
23303 break;
23304 default:
23305 gcc_unreachable ();
23306 }
23307 }
23308 prev_val = curr_val;
23309 }
23310 *size = (end_pos - *start_pos);
23311 }
23312
23313 bool
23314 mips_bit_clear_p (enum machine_mode mode, unsigned HOST_WIDE_INT m)
23315 {
23316 unsigned int shift = 0;
23317 unsigned int change_count = 0;
23318 unsigned int prev_val = 1;
23319 unsigned int curr_val = 0;
23320
23321 if (mode != SImode && mode != VOIDmode)
23322 return false;
23323
23324 if (!ISA_HAS_EXT_INS)
23325 return false;
23326
23327 for (shift = 0 ; shift < (UNITS_PER_WORD * BITS_PER_UNIT) ; shift++)
23328 {
23329 curr_val = (unsigned int)((m & (unsigned int)(1 << shift)) >> shift);
23330 if (curr_val != prev_val)
23331 change_count++;
23332 prev_val = curr_val;
23333 }
23334
23335 if (change_count == 2)
23336 return true;
23337
23338 return false;
23339 }
23340
23341 /* Initialize the GCC target structure. */
23342 #undef TARGET_ASM_ALIGNED_HI_OP
23343 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
23344 #undef TARGET_ASM_ALIGNED_SI_OP
23345 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
23346 #undef TARGET_ASM_ALIGNED_DI_OP
23347 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
23348
23349 #undef TARGET_OPTION_OVERRIDE
23350 #define TARGET_OPTION_OVERRIDE mips_option_override
23351
23352 #undef TARGET_LEGITIMIZE_ADDRESS
23353 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
23354
23355 #undef TARGET_ASM_FUNCTION_PROLOGUE
23356 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
23357 #undef TARGET_ASM_FUNCTION_EPILOGUE
23358 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
23359 #undef TARGET_ASM_SELECT_RTX_SECTION
23360 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
23361
23362 #undef TARGET_SCHED_INIT
23363 #define TARGET_SCHED_INIT mips_sched_init
23364 #undef TARGET_SCHED_REORDER
23365 #define TARGET_SCHED_REORDER mips_sched_reorder
23366 #undef TARGET_SCHED_REORDER2
23367 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
23368 #undef TARGET_SCHED_VARIABLE_ISSUE
23369 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
23370 #undef TARGET_SCHED_ADJUST_COST
23371 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
23372 #undef TARGET_SCHED_ISSUE_RATE
23373 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
23374 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
23375 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
23376 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
23377 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
23378 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
23379 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
23380 mips_multipass_dfa_lookahead
23381 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
23382 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
23383 mips_small_register_classes_for_mode_p
23384
23385 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
23386 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
23387
23388 #undef TARGET_INSERT_ATTRIBUTES
23389 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
23390 #undef TARGET_MERGE_DECL_ATTRIBUTES
23391 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
23392 #undef TARGET_CAN_INLINE_P
23393 #define TARGET_CAN_INLINE_P mips_can_inline_p
23394 #undef TARGET_SET_CURRENT_FUNCTION
23395 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
23396
23397 #undef TARGET_VALID_POINTER_MODE
23398 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
23399 #undef TARGET_REGISTER_MOVE_COST
23400 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
23401 #undef TARGET_REGISTER_PRIORITY
23402 #define TARGET_REGISTER_PRIORITY mips_register_priority
23403 #undef TARGET_MEMORY_MOVE_COST
23404 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
23405 #undef TARGET_RTX_COSTS
23406 #define TARGET_RTX_COSTS mips_rtx_costs
23407 #undef TARGET_ADDRESS_COST
23408 #define TARGET_ADDRESS_COST mips_address_cost
23409 #undef TARGET_INSN_COST
23410 #define TARGET_INSN_COST mips_insn_cost
23411
23412 #undef TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P
23413 #define TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P mips_no_speculation_in_delay_slots_p
23414
23415 #undef TARGET_IN_SMALL_DATA_P
23416 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
23417
23418 #undef TARGET_MACHINE_DEPENDENT_REORG
23419 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
23420
23421 #undef TARGET_PREFERRED_RELOAD_CLASS
23422 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
23423
23424 #undef TARGET_EXPAND_TO_RTL_HOOK
23425 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
23426 #undef TARGET_ASM_FILE_START
23427 #define TARGET_ASM_FILE_START mips_file_start
23428 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
23429 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
23430 #undef TARGET_ASM_CODE_END
23431 #define TARGET_ASM_CODE_END mips_code_end
23432
23433 #undef TARGET_INIT_LIBFUNCS
23434 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
23435
23436 #undef TARGET_BUILD_BUILTIN_VA_LIST
23437 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
23438 #undef TARGET_EXPAND_BUILTIN_VA_START
23439 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
23440 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
23441 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
23442
23443 #undef TARGET_PROMOTE_FUNCTION_MODE
23444 #define TARGET_PROMOTE_FUNCTION_MODE mips_promote_function_mode
23445 #undef TARGET_FUNCTION_VALUE
23446 #define TARGET_FUNCTION_VALUE mips_function_value
23447 #undef TARGET_LIBCALL_VALUE
23448 #define TARGET_LIBCALL_VALUE mips_libcall_value
23449 #undef TARGET_FUNCTION_VALUE_REGNO_P
23450 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
23451 #undef TARGET_RETURN_IN_MEMORY
23452 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
23453 #undef TARGET_RETURN_IN_MSB
23454 #define TARGET_RETURN_IN_MSB mips_return_in_msb
23455
23456 #undef TARGET_ASM_OUTPUT_MI_THUNK
23457 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
23458 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
23459 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
23460
23461 #undef TARGET_PRINT_OPERAND
23462 #define TARGET_PRINT_OPERAND mips_print_operand
23463 #undef TARGET_PRINT_OPERAND_ADDRESS
23464 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
23465 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
23466 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
23467
23468 #undef TARGET_SETUP_INCOMING_VARARGS
23469 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
23470 #undef TARGET_STRICT_ARGUMENT_NAMING
23471 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
23472 #undef TARGET_MUST_PASS_IN_STACK
23473 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
23474 #undef TARGET_PASS_BY_REFERENCE
23475 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
23476 #undef TARGET_CALLEE_COPIES
23477 #define TARGET_CALLEE_COPIES mips_callee_copies
23478 #undef TARGET_ARG_PARTIAL_BYTES
23479 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
23480 #undef TARGET_FUNCTION_ARG
23481 #define TARGET_FUNCTION_ARG mips_function_arg
23482 #undef TARGET_FUNCTION_ARG_ADVANCE
23483 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
23484 #undef TARGET_FUNCTION_ARG_PADDING
23485 #define TARGET_FUNCTION_ARG_PADDING mips_function_arg_padding
23486 #undef TARGET_FUNCTION_ARG_BOUNDARY
23487 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
23488 #undef TARGET_GET_RAW_RESULT_MODE
23489 #define TARGET_GET_RAW_RESULT_MODE mips_get_reg_raw_mode
23490 #undef TARGET_GET_RAW_ARG_MODE
23491 #define TARGET_GET_RAW_ARG_MODE mips_get_reg_raw_mode
23492
23493 #undef TARGET_MODE_REP_EXTENDED
23494 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
23495
23496 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
23497 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
23498 mips_builtin_vectorized_function
23499 #undef TARGET_VECTOR_MODE_SUPPORTED_P
23500 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
23501
23502 #undef TARGET_SCALAR_MODE_SUPPORTED_P
23503 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
23504
23505 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
23506 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
23507 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES
23508 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES \
23509 mips_autovectorize_vector_modes
23510
23511 #undef TARGET_INIT_BUILTINS
23512 #define TARGET_INIT_BUILTINS mips_init_builtins
23513 #undef TARGET_BUILTIN_DECL
23514 #define TARGET_BUILTIN_DECL mips_builtin_decl
23515 #undef TARGET_EXPAND_BUILTIN
23516 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
23517
23518 #undef TARGET_HAVE_TLS
23519 #define TARGET_HAVE_TLS HAVE_AS_TLS
23520
23521 #undef TARGET_CANNOT_FORCE_CONST_MEM
23522 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
23523
23524 #undef TARGET_LEGITIMATE_CONSTANT_P
23525 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
23526
23527 #undef TARGET_ENCODE_SECTION_INFO
23528 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
23529
23530 #undef TARGET_ATTRIBUTE_TABLE
23531 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
23532 /* All our function attributes are related to how out-of-line copies should
23533 be compiled or called. They don't in themselves prevent inlining. */
23534 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
23535 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
23536
23537 #undef TARGET_EXTRA_LIVE_ON_ENTRY
23538 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
23539
23540 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
23541 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
23542 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
23543 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
23544
23545 #undef TARGET_COMP_TYPE_ATTRIBUTES
23546 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
23547
23548 #ifdef HAVE_AS_DTPRELWORD
23549 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
23550 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
23551 #endif
23552 #undef TARGET_DWARF_REGISTER_SPAN
23553 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
23554 #undef TARGET_DWARF_FRAME_REG_MODE
23555 #define TARGET_DWARF_FRAME_REG_MODE mips_dwarf_frame_reg_mode
23556
23557 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
23558 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
23559
23560 #undef TARGET_LEGITIMATE_ADDRESS_P
23561 #define TARGET_LEGITIMATE_ADDRESS_P mips_legitimate_address_p
23562
23563 #undef TARGET_FRAME_POINTER_REQUIRED
23564 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
23565
23566 #undef TARGET_CAN_ELIMINATE
23567 #define TARGET_CAN_ELIMINATE mips_can_eliminate
23568
23569 #undef TARGET_CONDITIONAL_REGISTER_USAGE
23570 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
23571
23572 #undef TARGET_TRAMPOLINE_INIT
23573 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
23574
23575 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
23576 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
23577
23578 #undef TARGET_SHIFT_TRUNCATION_MASK
23579 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
23580
23581 #undef TARGET_PREPARE_PCH_SAVE
23582 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
23583
23584 #undef TARGET_VECTORIZE_VEC_PERM_CONST
23585 #define TARGET_VECTORIZE_VEC_PERM_CONST mips_vectorize_vec_perm_const
23586
23587 #undef TARGET_SCHED_REASSOCIATION_WIDTH
23588 #define TARGET_SCHED_REASSOCIATION_WIDTH mips_sched_reassociation_width
23589
23590 #undef TARGET_CASE_VALUES_THRESHOLD
23591 #define TARGET_CASE_VALUES_THRESHOLD mips_case_values_threshold
23592
23593 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
23594 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV mips_atomic_assign_expand_fenv
23595
23596 #undef TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS
23597 #define TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS true
23598
23599 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
23600 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
23601 mips_use_by_pieces_infrastructure_p
23602
23603 #undef TARGET_SPILL_CLASS
23604 #define TARGET_SPILL_CLASS mips_spill_class
23605 #undef TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS
23606 #define TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS mips_ira_change_pseudo_allocno_class
23607
23608 #undef TARGET_HARD_REGNO_SCRATCH_OK
23609 #define TARGET_HARD_REGNO_SCRATCH_OK mips_hard_regno_scratch_ok
23610
23611 #undef TARGET_HARD_REGNO_NREGS
23612 #define TARGET_HARD_REGNO_NREGS mips_hard_regno_nregs
23613 #undef TARGET_HARD_REGNO_MODE_OK
23614 #define TARGET_HARD_REGNO_MODE_OK mips_hard_regno_mode_ok
23615
23616 #undef TARGET_MODES_TIEABLE_P
23617 #define TARGET_MODES_TIEABLE_P mips_modes_tieable_p
23618
23619 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
23620 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
23621 mips_hard_regno_call_part_clobbered
23622
23623 /* The architecture reserves bit 0 for MIPS16 so use bit 1 for descriptors. */
23624 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
23625 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 2
23626
23627 #undef TARGET_SECONDARY_MEMORY_NEEDED
23628 #define TARGET_SECONDARY_MEMORY_NEEDED mips_secondary_memory_needed
23629
23630 #undef TARGET_CAN_CHANGE_MODE_CLASS
23631 #define TARGET_CAN_CHANGE_MODE_CLASS mips_can_change_mode_class
23632
23633 #undef TARGET_TRULY_NOOP_TRUNCATION
23634 #define TARGET_TRULY_NOOP_TRUNCATION mips_truly_noop_truncation
23635
23636 #undef TARGET_CONSTANT_ALIGNMENT
23637 #define TARGET_CONSTANT_ALIGNMENT mips_constant_alignment
23638
23639 #undef TARGET_ASAN_SHADOW_OFFSET
23640 #define TARGET_ASAN_SHADOW_OFFSET mips_asan_shadow_offset
23641
23642 #undef TARGET_STARTING_FRAME_OFFSET
23643 #define TARGET_STARTING_FRAME_OFFSET mips_starting_frame_offset
23644
23645 #undef TARGET_ASM_FILE_END
23646 #define TARGET_ASM_FILE_END mips_asm_file_end
23647
23648 #undef TARGET_C_MODE_FOR_FLOATING_TYPE
23649 #define TARGET_C_MODE_FOR_FLOATING_TYPE mips_c_mode_for_floating_type
23650
23651 #undef TARGET_DOCUMENTATION_NAME
23652 #define TARGET_DOCUMENTATION_NAME "MIPS"
23653
23654 struct gcc_target targetm = TARGET_INITIALIZER;
23655
23656 #include "gt-mips.h"