MIPS32R6 and MIPS64R6 support
[official-gcc.git] / gcc / config / mips / mips.c
blob3b8daca0121871798912716c332a416825919130
1 /* Subroutines used for MIPS code generation.
2 Copyright (C) 1989-2014 Free Software Foundation, Inc.
3 Contributed by A. Lichnewsky, lich@inria.inria.fr.
4 Changes by Michael Meissner, meissner@osf.org.
5 64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
6 Brendan Eich, brendan@microunity.com.
8 This file is part of GCC.
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.
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.
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/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-attr.h"
34 #include "recog.h"
35 #include "output.h"
36 #include "tree.h"
37 #include "varasm.h"
38 #include "stringpool.h"
39 #include "stor-layout.h"
40 #include "calls.h"
41 #include "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "input.h"
46 #include "function.h"
47 #include "expr.h"
48 #include "insn-codes.h"
49 #include "optabs.h"
50 #include "libfuncs.h"
51 #include "flags.h"
52 #include "reload.h"
53 #include "tm_p.h"
54 #include "ggc.h"
55 #include "gstab.h"
56 #include "hash-table.h"
57 #include "debug.h"
58 #include "target.h"
59 #include "target-def.h"
60 #include "common/common-target.h"
61 #include "langhooks.h"
62 #include "dominance.h"
63 #include "cfg.h"
64 #include "cfgrtl.h"
65 #include "cfganal.h"
66 #include "lcm.h"
67 #include "cfgbuild.h"
68 #include "cfgcleanup.h"
69 #include "predict.h"
70 #include "basic-block.h"
71 #include "sched-int.h"
72 #include "tree-ssa-alias.h"
73 #include "internal-fn.h"
74 #include "gimple-fold.h"
75 #include "tree-eh.h"
76 #include "gimple-expr.h"
77 #include "is-a.h"
78 #include "gimple.h"
79 #include "gimplify.h"
80 #include "bitmap.h"
81 #include "diagnostic.h"
82 #include "target-globals.h"
83 #include "opts.h"
84 #include "tree-pass.h"
85 #include "context.h"
86 #include "hash-map.h"
87 #include "plugin-api.h"
88 #include "ipa-ref.h"
89 #include "cgraph.h"
90 #include "builtins.h"
91 #include "rtl-iter.h"
93 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
94 #define UNSPEC_ADDRESS_P(X) \
95 (GET_CODE (X) == UNSPEC \
96 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
97 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
99 /* Extract the symbol or label from UNSPEC wrapper X. */
100 #define UNSPEC_ADDRESS(X) \
101 XVECEXP (X, 0, 0)
103 /* Extract the symbol type from UNSPEC wrapper X. */
104 #define UNSPEC_ADDRESS_TYPE(X) \
105 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
107 /* The maximum distance between the top of the stack frame and the
108 value $sp has when we save and restore registers.
110 The value for normal-mode code must be a SMALL_OPERAND and must
111 preserve the maximum stack alignment. We therefore use a value
112 of 0x7ff0 in this case.
114 microMIPS LWM and SWM support 12-bit offsets (from -0x800 to 0x7ff),
115 so we use a maximum of 0x7f0 for TARGET_MICROMIPS.
117 MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
118 up to 0x7f8 bytes and can usually save or restore all the registers
119 that we need to save or restore. (Note that we can only use these
120 instructions for o32, for which the stack alignment is 8 bytes.)
122 We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
123 RESTORE are not available. We can then use unextended instructions
124 to save and restore registers, and to allocate and deallocate the top
125 part of the frame. */
126 #define MIPS_MAX_FIRST_STACK_STEP \
127 (!TARGET_COMPRESSION ? 0x7ff0 \
128 : TARGET_MICROMIPS || GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8 \
129 : TARGET_64BIT ? 0x100 : 0x400)
131 /* True if INSN is a mips.md pattern or asm statement. */
132 /* ??? This test exists through the compiler, perhaps it should be
133 moved to rtl.h. */
134 #define USEFUL_INSN_P(INSN) \
135 (NONDEBUG_INSN_P (INSN) \
136 && GET_CODE (PATTERN (INSN)) != USE \
137 && GET_CODE (PATTERN (INSN)) != CLOBBER)
139 /* If INSN is a delayed branch sequence, return the first instruction
140 in the sequence, otherwise return INSN itself. */
141 #define SEQ_BEGIN(INSN) \
142 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
143 ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN), 0, 0)) \
144 : (INSN))
146 /* Likewise for the last instruction in a delayed branch sequence. */
147 #define SEQ_END(INSN) \
148 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
149 ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN), \
150 0, \
151 XVECLEN (PATTERN (INSN), 0) - 1)) \
152 : (INSN))
154 /* Execute the following loop body with SUBINSN set to each instruction
155 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
156 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
157 for ((SUBINSN) = SEQ_BEGIN (INSN); \
158 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
159 (SUBINSN) = NEXT_INSN (SUBINSN))
161 /* True if bit BIT is set in VALUE. */
162 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
164 /* Return the opcode for a ptr_mode load of the form:
166 l[wd] DEST, OFFSET(BASE). */
167 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE) \
168 (((ptr_mode == DImode ? 0x37 : 0x23) << 26) \
169 | ((BASE) << 21) \
170 | ((DEST) << 16) \
171 | (OFFSET))
173 /* Return the opcode to move register SRC into register DEST. */
174 #define MIPS_MOVE(DEST, SRC) \
175 ((TARGET_64BIT ? 0x2d : 0x21) \
176 | ((DEST) << 11) \
177 | ((SRC) << 21))
179 /* Return the opcode for:
181 lui DEST, VALUE. */
182 #define MIPS_LUI(DEST, VALUE) \
183 ((0xf << 26) | ((DEST) << 16) | (VALUE))
185 /* Return the opcode to jump to register DEST. When the JR opcode is not
186 available use JALR $0, DEST. */
187 #define MIPS_JR(DEST) \
188 (((DEST) << 21) | (ISA_HAS_JR ? 0x8 : 0x9))
190 /* Return the opcode for:
192 bal . + (1 + OFFSET) * 4. */
193 #define MIPS_BAL(OFFSET) \
194 ((0x1 << 26) | (0x11 << 16) | (OFFSET))
196 /* Return the usual opcode for a nop. */
197 #define MIPS_NOP 0
199 /* Classifies an address.
201 ADDRESS_REG
202 A natural register + offset address. The register satisfies
203 mips_valid_base_register_p and the offset is a const_arith_operand.
205 ADDRESS_LO_SUM
206 A LO_SUM rtx. The first operand is a valid base register and
207 the second operand is a symbolic address.
209 ADDRESS_CONST_INT
210 A signed 16-bit constant address.
212 ADDRESS_SYMBOLIC:
213 A constant symbolic address. */
214 enum mips_address_type {
215 ADDRESS_REG,
216 ADDRESS_LO_SUM,
217 ADDRESS_CONST_INT,
218 ADDRESS_SYMBOLIC
221 /* Macros to create an enumeration identifier for a function prototype. */
222 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
223 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
224 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
225 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
227 /* Classifies the prototype of a built-in function. */
228 enum mips_function_type {
229 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
230 #include "config/mips/mips-ftypes.def"
231 #undef DEF_MIPS_FTYPE
232 MIPS_MAX_FTYPE_MAX
235 /* Specifies how a built-in function should be converted into rtl. */
236 enum mips_builtin_type {
237 /* The function corresponds directly to an .md pattern. The return
238 value is mapped to operand 0 and the arguments are mapped to
239 operands 1 and above. */
240 MIPS_BUILTIN_DIRECT,
242 /* The function corresponds directly to an .md pattern. There is no return
243 value and the arguments are mapped to operands 0 and above. */
244 MIPS_BUILTIN_DIRECT_NO_TARGET,
246 /* The function corresponds to a comparison instruction followed by
247 a mips_cond_move_tf_ps pattern. The first two arguments are the
248 values to compare and the second two arguments are the vector
249 operands for the movt.ps or movf.ps instruction (in assembly order). */
250 MIPS_BUILTIN_MOVF,
251 MIPS_BUILTIN_MOVT,
253 /* The function corresponds to a V2SF comparison instruction. Operand 0
254 of this instruction is the result of the comparison, which has mode
255 CCV2 or CCV4. The function arguments are mapped to operands 1 and
256 above. The function's return value is an SImode boolean that is
257 true under the following conditions:
259 MIPS_BUILTIN_CMP_ANY: one of the registers is true
260 MIPS_BUILTIN_CMP_ALL: all of the registers are true
261 MIPS_BUILTIN_CMP_LOWER: the first register is true
262 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
263 MIPS_BUILTIN_CMP_ANY,
264 MIPS_BUILTIN_CMP_ALL,
265 MIPS_BUILTIN_CMP_UPPER,
266 MIPS_BUILTIN_CMP_LOWER,
268 /* As above, but the instruction only sets a single $fcc register. */
269 MIPS_BUILTIN_CMP_SINGLE,
271 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
272 MIPS_BUILTIN_BPOSGE32
275 /* Invoke MACRO (COND) for each C.cond.fmt condition. */
276 #define MIPS_FP_CONDITIONS(MACRO) \
277 MACRO (f), \
278 MACRO (un), \
279 MACRO (eq), \
280 MACRO (ueq), \
281 MACRO (olt), \
282 MACRO (ult), \
283 MACRO (ole), \
284 MACRO (ule), \
285 MACRO (sf), \
286 MACRO (ngle), \
287 MACRO (seq), \
288 MACRO (ngl), \
289 MACRO (lt), \
290 MACRO (nge), \
291 MACRO (le), \
292 MACRO (ngt)
294 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
295 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
296 enum mips_fp_condition {
297 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
299 #undef DECLARE_MIPS_COND
301 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
302 #define STRINGIFY(X) #X
303 static const char *const mips_fp_conditions[] = {
304 MIPS_FP_CONDITIONS (STRINGIFY)
306 #undef STRINGIFY
308 /* A class used to control a comdat-style stub that we output in each
309 translation unit that needs it. */
310 class mips_one_only_stub {
311 public:
312 virtual ~mips_one_only_stub () {}
314 /* Return the name of the stub. */
315 virtual const char *get_name () = 0;
317 /* Output the body of the function to asm_out_file. */
318 virtual void output_body () = 0;
321 /* Tuning information that is automatically derived from other sources
322 (such as the scheduler). */
323 static struct {
324 /* The architecture and tuning settings that this structure describes. */
325 enum processor arch;
326 enum processor tune;
328 /* True if this structure describes MIPS16 settings. */
329 bool mips16_p;
331 /* True if the structure has been initialized. */
332 bool initialized_p;
334 /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
335 when optimizing for speed. */
336 bool fast_mult_zero_zero_p;
337 } mips_tuning_info;
339 /* Information about a function's frame layout. */
340 struct GTY(()) mips_frame_info {
341 /* The size of the frame in bytes. */
342 HOST_WIDE_INT total_size;
344 /* The number of bytes allocated to variables. */
345 HOST_WIDE_INT var_size;
347 /* The number of bytes allocated to outgoing function arguments. */
348 HOST_WIDE_INT args_size;
350 /* The number of bytes allocated to the .cprestore slot, or 0 if there
351 is no such slot. */
352 HOST_WIDE_INT cprestore_size;
354 /* Bit X is set if the function saves or restores GPR X. */
355 unsigned int mask;
357 /* Likewise FPR X. */
358 unsigned int fmask;
360 /* Likewise doubleword accumulator X ($acX). */
361 unsigned int acc_mask;
363 /* The number of GPRs, FPRs, doubleword accumulators and COP0
364 registers saved. */
365 unsigned int num_gp;
366 unsigned int num_fp;
367 unsigned int num_acc;
368 unsigned int num_cop0_regs;
370 /* The offset of the topmost GPR, FPR, accumulator and COP0-register
371 save slots from the top of the frame, or zero if no such slots are
372 needed. */
373 HOST_WIDE_INT gp_save_offset;
374 HOST_WIDE_INT fp_save_offset;
375 HOST_WIDE_INT acc_save_offset;
376 HOST_WIDE_INT cop0_save_offset;
378 /* Likewise, but giving offsets from the bottom of the frame. */
379 HOST_WIDE_INT gp_sp_offset;
380 HOST_WIDE_INT fp_sp_offset;
381 HOST_WIDE_INT acc_sp_offset;
382 HOST_WIDE_INT cop0_sp_offset;
384 /* Similar, but the value passed to _mcount. */
385 HOST_WIDE_INT ra_fp_offset;
387 /* The offset of arg_pointer_rtx from the bottom of the frame. */
388 HOST_WIDE_INT arg_pointer_offset;
390 /* The offset of hard_frame_pointer_rtx from the bottom of the frame. */
391 HOST_WIDE_INT hard_frame_pointer_offset;
394 struct GTY(()) machine_function {
395 /* The next floating-point condition-code register to allocate
396 for ISA_HAS_8CC targets, relative to ST_REG_FIRST. */
397 unsigned int next_fcc;
399 /* The register returned by mips16_gp_pseudo_reg; see there for details. */
400 rtx mips16_gp_pseudo_rtx;
402 /* The number of extra stack bytes taken up by register varargs.
403 This area is allocated by the callee at the very top of the frame. */
404 int varargs_size;
406 /* The current frame information, calculated by mips_compute_frame_info. */
407 struct mips_frame_info frame;
409 /* The register to use as the function's global pointer, or INVALID_REGNUM
410 if the function doesn't need one. */
411 unsigned int global_pointer;
413 /* How many instructions it takes to load a label into $AT, or 0 if
414 this property hasn't yet been calculated. */
415 unsigned int load_label_num_insns;
417 /* True if mips_adjust_insn_length should ignore an instruction's
418 hazard attribute. */
419 bool ignore_hazard_length_p;
421 /* True if the whole function is suitable for .set noreorder and
422 .set nomacro. */
423 bool all_noreorder_p;
425 /* True if the function has "inflexible" and "flexible" references
426 to the global pointer. See mips_cfun_has_inflexible_gp_ref_p
427 and mips_cfun_has_flexible_gp_ref_p for details. */
428 bool has_inflexible_gp_insn_p;
429 bool has_flexible_gp_insn_p;
431 /* True if the function's prologue must load the global pointer
432 value into pic_offset_table_rtx and store the same value in
433 the function's cprestore slot (if any). Even if this value
434 is currently false, we may decide to set it to true later;
435 see mips_must_initialize_gp_p () for details. */
436 bool must_initialize_gp_p;
438 /* True if the current function must restore $gp after any potential
439 clobber. This value is only meaningful during the first post-epilogue
440 split_insns pass; see mips_must_initialize_gp_p () for details. */
441 bool must_restore_gp_when_clobbered_p;
443 /* True if this is an interrupt handler. */
444 bool interrupt_handler_p;
446 /* True if this is an interrupt handler that uses shadow registers. */
447 bool use_shadow_register_set_p;
449 /* True if this is an interrupt handler that should keep interrupts
450 masked. */
451 bool keep_interrupts_masked_p;
453 /* True if this is an interrupt handler that should use DERET
454 instead of ERET. */
455 bool use_debug_exception_return_p;
458 /* Information about a single argument. */
459 struct mips_arg_info {
460 /* True if the argument is passed in a floating-point register, or
461 would have been if we hadn't run out of registers. */
462 bool fpr_p;
464 /* The number of words passed in registers, rounded up. */
465 unsigned int reg_words;
467 /* For EABI, the offset of the first register from GP_ARG_FIRST or
468 FP_ARG_FIRST. For other ABIs, the offset of the first register from
469 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
470 comment for details).
472 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
473 on the stack. */
474 unsigned int reg_offset;
476 /* The number of words that must be passed on the stack, rounded up. */
477 unsigned int stack_words;
479 /* The offset from the start of the stack overflow area of the argument's
480 first stack word. Only meaningful when STACK_WORDS is nonzero. */
481 unsigned int stack_offset;
484 /* Information about an address described by mips_address_type.
486 ADDRESS_CONST_INT
487 No fields are used.
489 ADDRESS_REG
490 REG is the base register and OFFSET is the constant offset.
492 ADDRESS_LO_SUM
493 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
494 is the type of symbol it references.
496 ADDRESS_SYMBOLIC
497 SYMBOL_TYPE is the type of symbol that the address references. */
498 struct mips_address_info {
499 enum mips_address_type type;
500 rtx reg;
501 rtx offset;
502 enum mips_symbol_type symbol_type;
505 /* One stage in a constant building sequence. These sequences have
506 the form:
508 A = VALUE[0]
509 A = A CODE[1] VALUE[1]
510 A = A CODE[2] VALUE[2]
513 where A is an accumulator, each CODE[i] is a binary rtl operation
514 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
515 struct mips_integer_op {
516 enum rtx_code code;
517 unsigned HOST_WIDE_INT value;
520 /* The largest number of operations needed to load an integer constant.
521 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
522 When the lowest bit is clear, we can try, but reject a sequence with
523 an extra SLL at the end. */
524 #define MIPS_MAX_INTEGER_OPS 7
526 /* Information about a MIPS16e SAVE or RESTORE instruction. */
527 struct mips16e_save_restore_info {
528 /* The number of argument registers saved by a SAVE instruction.
529 0 for RESTORE instructions. */
530 unsigned int nargs;
532 /* Bit X is set if the instruction saves or restores GPR X. */
533 unsigned int mask;
535 /* The total number of bytes to allocate. */
536 HOST_WIDE_INT size;
539 /* Costs of various operations on the different architectures. */
541 struct mips_rtx_cost_data
543 unsigned short fp_add;
544 unsigned short fp_mult_sf;
545 unsigned short fp_mult_df;
546 unsigned short fp_div_sf;
547 unsigned short fp_div_df;
548 unsigned short int_mult_si;
549 unsigned short int_mult_di;
550 unsigned short int_div_si;
551 unsigned short int_div_di;
552 unsigned short branch_cost;
553 unsigned short memory_latency;
556 /* Global variables for machine-dependent things. */
558 /* The -G setting, or the configuration's default small-data limit if
559 no -G option is given. */
560 static unsigned int mips_small_data_threshold;
562 /* The number of file directives written by mips_output_filename. */
563 int num_source_filenames;
565 /* The name that appeared in the last .file directive written by
566 mips_output_filename, or "" if mips_output_filename hasn't
567 written anything yet. */
568 const char *current_function_file = "";
570 /* Arrays that map GCC register numbers to debugger register numbers. */
571 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
572 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
574 /* Information about the current function's epilogue, used only while
575 expanding it. */
576 static struct {
577 /* A list of queued REG_CFA_RESTORE notes. */
578 rtx cfa_restores;
580 /* The CFA is currently defined as CFA_REG + CFA_OFFSET. */
581 rtx cfa_reg;
582 HOST_WIDE_INT cfa_offset;
584 /* The offset of the CFA from the stack pointer while restoring
585 registers. */
586 HOST_WIDE_INT cfa_restore_sp_offset;
587 } mips_epilogue;
589 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs. */
590 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
591 struct mips_asm_switch mips_nomacro = { "macro", 0 };
592 struct mips_asm_switch mips_noat = { "at", 0 };
594 /* True if we're writing out a branch-likely instruction rather than a
595 normal branch. */
596 static bool mips_branch_likely;
598 /* The current instruction-set architecture. */
599 enum processor mips_arch;
600 const struct mips_cpu_info *mips_arch_info;
602 /* The processor that we should tune the code for. */
603 enum processor mips_tune;
604 const struct mips_cpu_info *mips_tune_info;
606 /* The ISA level associated with mips_arch. */
607 int mips_isa;
609 /* The ISA revision level. This is 0 for MIPS I to V and N for
610 MIPS{32,64}rN. */
611 int mips_isa_rev;
613 /* The architecture selected by -mipsN, or null if -mipsN wasn't used. */
614 static const struct mips_cpu_info *mips_isa_option_info;
616 /* Which cost information to use. */
617 static const struct mips_rtx_cost_data *mips_cost;
619 /* The ambient target flags, excluding MASK_MIPS16. */
620 static int mips_base_target_flags;
622 /* The default compression mode. */
623 unsigned int mips_base_compression_flags;
625 /* The ambient values of other global variables. */
626 static int mips_base_schedule_insns; /* flag_schedule_insns */
627 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
628 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
629 static int mips_base_align_loops; /* align_loops */
630 static int mips_base_align_jumps; /* align_jumps */
631 static int mips_base_align_functions; /* align_functions */
633 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
634 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
636 /* Index C is true if character C is a valid PRINT_OPERAND punctation
637 character. */
638 static bool mips_print_operand_punct[256];
640 static GTY (()) int mips_output_filename_first_time = 1;
642 /* mips_split_p[X] is true if symbols of type X can be split by
643 mips_split_symbol. */
644 bool mips_split_p[NUM_SYMBOL_TYPES];
646 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
647 can be split by mips_split_symbol. */
648 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
650 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
651 forced into a PC-relative constant pool. */
652 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
654 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
655 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
656 if they are matched by a special .md file pattern. */
657 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
659 /* Likewise for HIGHs. */
660 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
662 /* Target state for MIPS16. */
663 struct target_globals *mips16_globals;
665 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
666 and returned from mips_sched_reorder2. */
667 static int cached_can_issue_more;
669 /* The stubs for various MIPS16 support functions, if used. */
670 static mips_one_only_stub *mips16_rdhwr_stub;
671 static mips_one_only_stub *mips16_get_fcsr_stub;
672 static mips_one_only_stub *mips16_set_fcsr_stub;
674 /* Index R is the smallest register class that contains register R. */
675 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
676 LEA_REGS, LEA_REGS, M16_STORE_REGS, V1_REG,
677 M16_STORE_REGS, M16_STORE_REGS, M16_STORE_REGS, M16_STORE_REGS,
678 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
679 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
680 M16_REGS, M16_STORE_REGS, LEA_REGS, LEA_REGS,
681 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
682 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
683 LEA_REGS, M16_SP_REGS, LEA_REGS, LEA_REGS,
685 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
686 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
687 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
688 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
689 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
690 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
691 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
692 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
693 MD0_REG, MD1_REG, NO_REGS, ST_REGS,
694 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
695 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
696 NO_REGS, FRAME_REGS, FRAME_REGS, NO_REGS,
697 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
698 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
699 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
700 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
701 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
702 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
703 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
704 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
705 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
706 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
707 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
708 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
709 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
710 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
711 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
712 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
713 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
714 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
715 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
716 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
717 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
718 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
719 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
720 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
721 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
722 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
723 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
726 /* The value of TARGET_ATTRIBUTE_TABLE. */
727 static const struct attribute_spec mips_attribute_table[] = {
728 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
729 om_diagnostic } */
730 { "long_call", 0, 0, false, true, true, NULL, false },
731 { "far", 0, 0, false, true, true, NULL, false },
732 { "near", 0, 0, false, true, true, NULL, false },
733 /* We would really like to treat "mips16" and "nomips16" as type
734 attributes, but GCC doesn't provide the hooks we need to support
735 the right conversion rules. As declaration attributes, they affect
736 code generation but don't carry other semantics. */
737 { "mips16", 0, 0, true, false, false, NULL, false },
738 { "nomips16", 0, 0, true, false, false, NULL, false },
739 { "micromips", 0, 0, true, false, false, NULL, false },
740 { "nomicromips", 0, 0, true, false, false, NULL, false },
741 { "nocompression", 0, 0, true, false, false, NULL, false },
742 /* Allow functions to be specified as interrupt handlers */
743 { "interrupt", 0, 0, false, true, true, NULL, false },
744 { "use_shadow_register_set", 0, 0, false, true, true, NULL, false },
745 { "keep_interrupts_masked", 0, 0, false, true, true, NULL, false },
746 { "use_debug_exception_return", 0, 0, false, true, true, NULL, false },
747 { NULL, 0, 0, false, false, false, NULL, false }
750 /* A table describing all the processors GCC knows about; see
751 mips-cpus.def for details. */
752 static const struct mips_cpu_info mips_cpu_info_table[] = {
753 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
754 { NAME, CPU, ISA, FLAGS },
755 #include "mips-cpus.def"
756 #undef MIPS_CPU
759 /* Default costs. If these are used for a processor we should look
760 up the actual costs. */
761 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
762 COSTS_N_INSNS (7), /* fp_mult_sf */ \
763 COSTS_N_INSNS (8), /* fp_mult_df */ \
764 COSTS_N_INSNS (23), /* fp_div_sf */ \
765 COSTS_N_INSNS (36), /* fp_div_df */ \
766 COSTS_N_INSNS (10), /* int_mult_si */ \
767 COSTS_N_INSNS (10), /* int_mult_di */ \
768 COSTS_N_INSNS (69), /* int_div_si */ \
769 COSTS_N_INSNS (69), /* int_div_di */ \
770 2, /* branch_cost */ \
771 4 /* memory_latency */
773 /* Floating-point costs for processors without an FPU. Just assume that
774 all floating-point libcalls are very expensive. */
775 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
776 COSTS_N_INSNS (256), /* fp_mult_sf */ \
777 COSTS_N_INSNS (256), /* fp_mult_df */ \
778 COSTS_N_INSNS (256), /* fp_div_sf */ \
779 COSTS_N_INSNS (256) /* fp_div_df */
781 /* Costs to use when optimizing for size. */
782 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
783 COSTS_N_INSNS (1), /* fp_add */
784 COSTS_N_INSNS (1), /* fp_mult_sf */
785 COSTS_N_INSNS (1), /* fp_mult_df */
786 COSTS_N_INSNS (1), /* fp_div_sf */
787 COSTS_N_INSNS (1), /* fp_div_df */
788 COSTS_N_INSNS (1), /* int_mult_si */
789 COSTS_N_INSNS (1), /* int_mult_di */
790 COSTS_N_INSNS (1), /* int_div_si */
791 COSTS_N_INSNS (1), /* int_div_di */
792 2, /* branch_cost */
793 4 /* memory_latency */
796 /* Costs to use when optimizing for speed, indexed by processor. */
797 static const struct mips_rtx_cost_data
798 mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
799 { /* R3000 */
800 COSTS_N_INSNS (2), /* fp_add */
801 COSTS_N_INSNS (4), /* fp_mult_sf */
802 COSTS_N_INSNS (5), /* fp_mult_df */
803 COSTS_N_INSNS (12), /* fp_div_sf */
804 COSTS_N_INSNS (19), /* fp_div_df */
805 COSTS_N_INSNS (12), /* int_mult_si */
806 COSTS_N_INSNS (12), /* int_mult_di */
807 COSTS_N_INSNS (35), /* int_div_si */
808 COSTS_N_INSNS (35), /* int_div_di */
809 1, /* branch_cost */
810 4 /* memory_latency */
812 { /* 4KC */
813 SOFT_FP_COSTS,
814 COSTS_N_INSNS (6), /* int_mult_si */
815 COSTS_N_INSNS (6), /* int_mult_di */
816 COSTS_N_INSNS (36), /* int_div_si */
817 COSTS_N_INSNS (36), /* int_div_di */
818 1, /* branch_cost */
819 4 /* memory_latency */
821 { /* 4KP */
822 SOFT_FP_COSTS,
823 COSTS_N_INSNS (36), /* int_mult_si */
824 COSTS_N_INSNS (36), /* int_mult_di */
825 COSTS_N_INSNS (37), /* int_div_si */
826 COSTS_N_INSNS (37), /* int_div_di */
827 1, /* branch_cost */
828 4 /* memory_latency */
830 { /* 5KC */
831 SOFT_FP_COSTS,
832 COSTS_N_INSNS (4), /* int_mult_si */
833 COSTS_N_INSNS (11), /* int_mult_di */
834 COSTS_N_INSNS (36), /* int_div_si */
835 COSTS_N_INSNS (68), /* int_div_di */
836 1, /* branch_cost */
837 4 /* memory_latency */
839 { /* 5KF */
840 COSTS_N_INSNS (4), /* fp_add */
841 COSTS_N_INSNS (4), /* fp_mult_sf */
842 COSTS_N_INSNS (5), /* fp_mult_df */
843 COSTS_N_INSNS (17), /* fp_div_sf */
844 COSTS_N_INSNS (32), /* fp_div_df */
845 COSTS_N_INSNS (4), /* int_mult_si */
846 COSTS_N_INSNS (11), /* int_mult_di */
847 COSTS_N_INSNS (36), /* int_div_si */
848 COSTS_N_INSNS (68), /* int_div_di */
849 1, /* branch_cost */
850 4 /* memory_latency */
852 { /* 20KC */
853 COSTS_N_INSNS (4), /* fp_add */
854 COSTS_N_INSNS (4), /* fp_mult_sf */
855 COSTS_N_INSNS (5), /* fp_mult_df */
856 COSTS_N_INSNS (17), /* fp_div_sf */
857 COSTS_N_INSNS (32), /* fp_div_df */
858 COSTS_N_INSNS (4), /* int_mult_si */
859 COSTS_N_INSNS (7), /* int_mult_di */
860 COSTS_N_INSNS (42), /* int_div_si */
861 COSTS_N_INSNS (72), /* int_div_di */
862 1, /* branch_cost */
863 4 /* memory_latency */
865 { /* 24KC */
866 SOFT_FP_COSTS,
867 COSTS_N_INSNS (5), /* int_mult_si */
868 COSTS_N_INSNS (5), /* int_mult_di */
869 COSTS_N_INSNS (41), /* int_div_si */
870 COSTS_N_INSNS (41), /* int_div_di */
871 1, /* branch_cost */
872 4 /* memory_latency */
874 { /* 24KF2_1 */
875 COSTS_N_INSNS (8), /* fp_add */
876 COSTS_N_INSNS (8), /* fp_mult_sf */
877 COSTS_N_INSNS (10), /* fp_mult_df */
878 COSTS_N_INSNS (34), /* fp_div_sf */
879 COSTS_N_INSNS (64), /* fp_div_df */
880 COSTS_N_INSNS (5), /* int_mult_si */
881 COSTS_N_INSNS (5), /* int_mult_di */
882 COSTS_N_INSNS (41), /* int_div_si */
883 COSTS_N_INSNS (41), /* int_div_di */
884 1, /* branch_cost */
885 4 /* memory_latency */
887 { /* 24KF1_1 */
888 COSTS_N_INSNS (4), /* fp_add */
889 COSTS_N_INSNS (4), /* fp_mult_sf */
890 COSTS_N_INSNS (5), /* fp_mult_df */
891 COSTS_N_INSNS (17), /* fp_div_sf */
892 COSTS_N_INSNS (32), /* fp_div_df */
893 COSTS_N_INSNS (5), /* int_mult_si */
894 COSTS_N_INSNS (5), /* int_mult_di */
895 COSTS_N_INSNS (41), /* int_div_si */
896 COSTS_N_INSNS (41), /* int_div_di */
897 1, /* branch_cost */
898 4 /* memory_latency */
900 { /* 74KC */
901 SOFT_FP_COSTS,
902 COSTS_N_INSNS (5), /* int_mult_si */
903 COSTS_N_INSNS (5), /* int_mult_di */
904 COSTS_N_INSNS (41), /* int_div_si */
905 COSTS_N_INSNS (41), /* int_div_di */
906 1, /* branch_cost */
907 4 /* memory_latency */
909 { /* 74KF2_1 */
910 COSTS_N_INSNS (8), /* fp_add */
911 COSTS_N_INSNS (8), /* fp_mult_sf */
912 COSTS_N_INSNS (10), /* fp_mult_df */
913 COSTS_N_INSNS (34), /* fp_div_sf */
914 COSTS_N_INSNS (64), /* fp_div_df */
915 COSTS_N_INSNS (5), /* int_mult_si */
916 COSTS_N_INSNS (5), /* int_mult_di */
917 COSTS_N_INSNS (41), /* int_div_si */
918 COSTS_N_INSNS (41), /* int_div_di */
919 1, /* branch_cost */
920 4 /* memory_latency */
922 { /* 74KF1_1 */
923 COSTS_N_INSNS (4), /* fp_add */
924 COSTS_N_INSNS (4), /* fp_mult_sf */
925 COSTS_N_INSNS (5), /* fp_mult_df */
926 COSTS_N_INSNS (17), /* fp_div_sf */
927 COSTS_N_INSNS (32), /* fp_div_df */
928 COSTS_N_INSNS (5), /* int_mult_si */
929 COSTS_N_INSNS (5), /* int_mult_di */
930 COSTS_N_INSNS (41), /* int_div_si */
931 COSTS_N_INSNS (41), /* int_div_di */
932 1, /* branch_cost */
933 4 /* memory_latency */
935 { /* 74KF3_2 */
936 COSTS_N_INSNS (6), /* fp_add */
937 COSTS_N_INSNS (6), /* fp_mult_sf */
938 COSTS_N_INSNS (7), /* fp_mult_df */
939 COSTS_N_INSNS (25), /* fp_div_sf */
940 COSTS_N_INSNS (48), /* fp_div_df */
941 COSTS_N_INSNS (5), /* int_mult_si */
942 COSTS_N_INSNS (5), /* int_mult_di */
943 COSTS_N_INSNS (41), /* int_div_si */
944 COSTS_N_INSNS (41), /* int_div_di */
945 1, /* branch_cost */
946 4 /* memory_latency */
948 { /* Loongson-2E */
949 DEFAULT_COSTS
951 { /* Loongson-2F */
952 DEFAULT_COSTS
954 { /* Loongson-3A */
955 DEFAULT_COSTS
957 { /* M4k */
958 DEFAULT_COSTS
960 /* Octeon */
962 SOFT_FP_COSTS,
963 COSTS_N_INSNS (5), /* int_mult_si */
964 COSTS_N_INSNS (5), /* int_mult_di */
965 COSTS_N_INSNS (72), /* int_div_si */
966 COSTS_N_INSNS (72), /* int_div_di */
967 1, /* branch_cost */
968 4 /* memory_latency */
970 /* Octeon II */
972 SOFT_FP_COSTS,
973 COSTS_N_INSNS (6), /* int_mult_si */
974 COSTS_N_INSNS (6), /* int_mult_di */
975 COSTS_N_INSNS (18), /* int_div_si */
976 COSTS_N_INSNS (35), /* int_div_di */
977 4, /* branch_cost */
978 4 /* memory_latency */
980 /* Octeon III */
982 COSTS_N_INSNS (6), /* fp_add */
983 COSTS_N_INSNS (6), /* fp_mult_sf */
984 COSTS_N_INSNS (7), /* fp_mult_df */
985 COSTS_N_INSNS (25), /* fp_div_sf */
986 COSTS_N_INSNS (48), /* fp_div_df */
987 COSTS_N_INSNS (6), /* int_mult_si */
988 COSTS_N_INSNS (6), /* int_mult_di */
989 COSTS_N_INSNS (18), /* int_div_si */
990 COSTS_N_INSNS (35), /* int_div_di */
991 4, /* branch_cost */
992 4 /* memory_latency */
994 { /* R3900 */
995 COSTS_N_INSNS (2), /* fp_add */
996 COSTS_N_INSNS (4), /* fp_mult_sf */
997 COSTS_N_INSNS (5), /* fp_mult_df */
998 COSTS_N_INSNS (12), /* fp_div_sf */
999 COSTS_N_INSNS (19), /* fp_div_df */
1000 COSTS_N_INSNS (2), /* int_mult_si */
1001 COSTS_N_INSNS (2), /* int_mult_di */
1002 COSTS_N_INSNS (35), /* int_div_si */
1003 COSTS_N_INSNS (35), /* int_div_di */
1004 1, /* branch_cost */
1005 4 /* memory_latency */
1007 { /* R6000 */
1008 COSTS_N_INSNS (3), /* fp_add */
1009 COSTS_N_INSNS (5), /* fp_mult_sf */
1010 COSTS_N_INSNS (6), /* fp_mult_df */
1011 COSTS_N_INSNS (15), /* fp_div_sf */
1012 COSTS_N_INSNS (16), /* fp_div_df */
1013 COSTS_N_INSNS (17), /* int_mult_si */
1014 COSTS_N_INSNS (17), /* int_mult_di */
1015 COSTS_N_INSNS (38), /* int_div_si */
1016 COSTS_N_INSNS (38), /* int_div_di */
1017 2, /* branch_cost */
1018 6 /* memory_latency */
1020 { /* R4000 */
1021 COSTS_N_INSNS (6), /* fp_add */
1022 COSTS_N_INSNS (7), /* fp_mult_sf */
1023 COSTS_N_INSNS (8), /* fp_mult_df */
1024 COSTS_N_INSNS (23), /* fp_div_sf */
1025 COSTS_N_INSNS (36), /* fp_div_df */
1026 COSTS_N_INSNS (10), /* int_mult_si */
1027 COSTS_N_INSNS (10), /* int_mult_di */
1028 COSTS_N_INSNS (69), /* int_div_si */
1029 COSTS_N_INSNS (69), /* int_div_di */
1030 2, /* branch_cost */
1031 6 /* memory_latency */
1033 { /* R4100 */
1034 DEFAULT_COSTS
1036 { /* R4111 */
1037 DEFAULT_COSTS
1039 { /* R4120 */
1040 DEFAULT_COSTS
1042 { /* R4130 */
1043 /* The only costs that appear to be updated here are
1044 integer multiplication. */
1045 SOFT_FP_COSTS,
1046 COSTS_N_INSNS (4), /* int_mult_si */
1047 COSTS_N_INSNS (6), /* int_mult_di */
1048 COSTS_N_INSNS (69), /* int_div_si */
1049 COSTS_N_INSNS (69), /* int_div_di */
1050 1, /* branch_cost */
1051 4 /* memory_latency */
1053 { /* R4300 */
1054 DEFAULT_COSTS
1056 { /* R4600 */
1057 DEFAULT_COSTS
1059 { /* R4650 */
1060 DEFAULT_COSTS
1062 { /* R4700 */
1063 DEFAULT_COSTS
1065 { /* R5000 */
1066 COSTS_N_INSNS (6), /* fp_add */
1067 COSTS_N_INSNS (4), /* fp_mult_sf */
1068 COSTS_N_INSNS (5), /* fp_mult_df */
1069 COSTS_N_INSNS (23), /* fp_div_sf */
1070 COSTS_N_INSNS (36), /* fp_div_df */
1071 COSTS_N_INSNS (5), /* int_mult_si */
1072 COSTS_N_INSNS (5), /* int_mult_di */
1073 COSTS_N_INSNS (36), /* int_div_si */
1074 COSTS_N_INSNS (36), /* int_div_di */
1075 1, /* branch_cost */
1076 4 /* memory_latency */
1078 { /* R5400 */
1079 COSTS_N_INSNS (6), /* fp_add */
1080 COSTS_N_INSNS (5), /* fp_mult_sf */
1081 COSTS_N_INSNS (6), /* fp_mult_df */
1082 COSTS_N_INSNS (30), /* fp_div_sf */
1083 COSTS_N_INSNS (59), /* fp_div_df */
1084 COSTS_N_INSNS (3), /* int_mult_si */
1085 COSTS_N_INSNS (4), /* int_mult_di */
1086 COSTS_N_INSNS (42), /* int_div_si */
1087 COSTS_N_INSNS (74), /* int_div_di */
1088 1, /* branch_cost */
1089 4 /* memory_latency */
1091 { /* R5500 */
1092 COSTS_N_INSNS (6), /* fp_add */
1093 COSTS_N_INSNS (5), /* fp_mult_sf */
1094 COSTS_N_INSNS (6), /* fp_mult_df */
1095 COSTS_N_INSNS (30), /* fp_div_sf */
1096 COSTS_N_INSNS (59), /* fp_div_df */
1097 COSTS_N_INSNS (5), /* int_mult_si */
1098 COSTS_N_INSNS (9), /* int_mult_di */
1099 COSTS_N_INSNS (42), /* int_div_si */
1100 COSTS_N_INSNS (74), /* int_div_di */
1101 1, /* branch_cost */
1102 4 /* memory_latency */
1104 { /* R5900 */
1105 COSTS_N_INSNS (4), /* fp_add */
1106 COSTS_N_INSNS (4), /* fp_mult_sf */
1107 COSTS_N_INSNS (256), /* fp_mult_df */
1108 COSTS_N_INSNS (8), /* fp_div_sf */
1109 COSTS_N_INSNS (256), /* fp_div_df */
1110 COSTS_N_INSNS (4), /* int_mult_si */
1111 COSTS_N_INSNS (256), /* int_mult_di */
1112 COSTS_N_INSNS (37), /* int_div_si */
1113 COSTS_N_INSNS (256), /* int_div_di */
1114 1, /* branch_cost */
1115 4 /* memory_latency */
1117 { /* R7000 */
1118 /* The only costs that are changed here are
1119 integer multiplication. */
1120 COSTS_N_INSNS (6), /* fp_add */
1121 COSTS_N_INSNS (7), /* fp_mult_sf */
1122 COSTS_N_INSNS (8), /* fp_mult_df */
1123 COSTS_N_INSNS (23), /* fp_div_sf */
1124 COSTS_N_INSNS (36), /* fp_div_df */
1125 COSTS_N_INSNS (5), /* int_mult_si */
1126 COSTS_N_INSNS (9), /* int_mult_di */
1127 COSTS_N_INSNS (69), /* int_div_si */
1128 COSTS_N_INSNS (69), /* int_div_di */
1129 1, /* branch_cost */
1130 4 /* memory_latency */
1132 { /* R8000 */
1133 DEFAULT_COSTS
1135 { /* R9000 */
1136 /* The only costs that are changed here are
1137 integer multiplication. */
1138 COSTS_N_INSNS (6), /* fp_add */
1139 COSTS_N_INSNS (7), /* fp_mult_sf */
1140 COSTS_N_INSNS (8), /* fp_mult_df */
1141 COSTS_N_INSNS (23), /* fp_div_sf */
1142 COSTS_N_INSNS (36), /* fp_div_df */
1143 COSTS_N_INSNS (3), /* int_mult_si */
1144 COSTS_N_INSNS (8), /* int_mult_di */
1145 COSTS_N_INSNS (69), /* int_div_si */
1146 COSTS_N_INSNS (69), /* int_div_di */
1147 1, /* branch_cost */
1148 4 /* memory_latency */
1150 { /* R1x000 */
1151 COSTS_N_INSNS (2), /* fp_add */
1152 COSTS_N_INSNS (2), /* fp_mult_sf */
1153 COSTS_N_INSNS (2), /* fp_mult_df */
1154 COSTS_N_INSNS (12), /* fp_div_sf */
1155 COSTS_N_INSNS (19), /* fp_div_df */
1156 COSTS_N_INSNS (5), /* int_mult_si */
1157 COSTS_N_INSNS (9), /* int_mult_di */
1158 COSTS_N_INSNS (34), /* int_div_si */
1159 COSTS_N_INSNS (66), /* int_div_di */
1160 1, /* branch_cost */
1161 4 /* memory_latency */
1163 { /* SB1 */
1164 /* These costs are the same as the SB-1A below. */
1165 COSTS_N_INSNS (4), /* fp_add */
1166 COSTS_N_INSNS (4), /* fp_mult_sf */
1167 COSTS_N_INSNS (4), /* fp_mult_df */
1168 COSTS_N_INSNS (24), /* fp_div_sf */
1169 COSTS_N_INSNS (32), /* fp_div_df */
1170 COSTS_N_INSNS (3), /* int_mult_si */
1171 COSTS_N_INSNS (4), /* int_mult_di */
1172 COSTS_N_INSNS (36), /* int_div_si */
1173 COSTS_N_INSNS (68), /* int_div_di */
1174 1, /* branch_cost */
1175 4 /* memory_latency */
1177 { /* SB1-A */
1178 /* These costs are the same as the SB-1 above. */
1179 COSTS_N_INSNS (4), /* fp_add */
1180 COSTS_N_INSNS (4), /* fp_mult_sf */
1181 COSTS_N_INSNS (4), /* fp_mult_df */
1182 COSTS_N_INSNS (24), /* fp_div_sf */
1183 COSTS_N_INSNS (32), /* fp_div_df */
1184 COSTS_N_INSNS (3), /* int_mult_si */
1185 COSTS_N_INSNS (4), /* int_mult_di */
1186 COSTS_N_INSNS (36), /* int_div_si */
1187 COSTS_N_INSNS (68), /* int_div_di */
1188 1, /* branch_cost */
1189 4 /* memory_latency */
1191 { /* SR71000 */
1192 DEFAULT_COSTS
1194 { /* XLR */
1195 SOFT_FP_COSTS,
1196 COSTS_N_INSNS (8), /* int_mult_si */
1197 COSTS_N_INSNS (8), /* int_mult_di */
1198 COSTS_N_INSNS (72), /* int_div_si */
1199 COSTS_N_INSNS (72), /* int_div_di */
1200 1, /* branch_cost */
1201 4 /* memory_latency */
1203 { /* XLP */
1204 /* These costs are the same as 5KF above. */
1205 COSTS_N_INSNS (4), /* fp_add */
1206 COSTS_N_INSNS (4), /* fp_mult_sf */
1207 COSTS_N_INSNS (5), /* fp_mult_df */
1208 COSTS_N_INSNS (17), /* fp_div_sf */
1209 COSTS_N_INSNS (32), /* fp_div_df */
1210 COSTS_N_INSNS (4), /* int_mult_si */
1211 COSTS_N_INSNS (11), /* int_mult_di */
1212 COSTS_N_INSNS (36), /* int_div_si */
1213 COSTS_N_INSNS (68), /* int_div_di */
1214 1, /* branch_cost */
1215 4 /* memory_latency */
1217 { /* P5600 */
1218 COSTS_N_INSNS (4), /* fp_add */
1219 COSTS_N_INSNS (5), /* fp_mult_sf */
1220 COSTS_N_INSNS (5), /* fp_mult_df */
1221 COSTS_N_INSNS (17), /* fp_div_sf */
1222 COSTS_N_INSNS (17), /* fp_div_df */
1223 COSTS_N_INSNS (5), /* int_mult_si */
1224 COSTS_N_INSNS (5), /* int_mult_di */
1225 COSTS_N_INSNS (8), /* int_div_si */
1226 COSTS_N_INSNS (8), /* int_div_di */
1227 2, /* branch_cost */
1228 4 /* memory_latency */
1230 { /* W32 */
1231 COSTS_N_INSNS (4), /* fp_add */
1232 COSTS_N_INSNS (4), /* fp_mult_sf */
1233 COSTS_N_INSNS (5), /* fp_mult_df */
1234 COSTS_N_INSNS (17), /* fp_div_sf */
1235 COSTS_N_INSNS (32), /* fp_div_df */
1236 COSTS_N_INSNS (5), /* int_mult_si */
1237 COSTS_N_INSNS (5), /* int_mult_di */
1238 COSTS_N_INSNS (41), /* int_div_si */
1239 COSTS_N_INSNS (41), /* int_div_di */
1240 1, /* branch_cost */
1241 4 /* memory_latency */
1243 { /* W64 */
1244 COSTS_N_INSNS (4), /* fp_add */
1245 COSTS_N_INSNS (4), /* fp_mult_sf */
1246 COSTS_N_INSNS (5), /* fp_mult_df */
1247 COSTS_N_INSNS (17), /* fp_div_sf */
1248 COSTS_N_INSNS (32), /* fp_div_df */
1249 COSTS_N_INSNS (5), /* int_mult_si */
1250 COSTS_N_INSNS (5), /* int_mult_di */
1251 COSTS_N_INSNS (41), /* int_div_si */
1252 COSTS_N_INSNS (41), /* int_div_di */
1253 1, /* branch_cost */
1254 4 /* memory_latency */
1258 static rtx mips_find_pic_call_symbol (rtx_insn *, rtx, bool);
1259 static int mips_register_move_cost (machine_mode, reg_class_t,
1260 reg_class_t);
1261 static unsigned int mips_function_arg_boundary (machine_mode, const_tree);
1262 static machine_mode mips_get_reg_raw_mode (int regno);
1264 struct mips16_flip_traits : default_hashmap_traits
1266 static hashval_t hash (const char *s) { return htab_hash_string (s); }
1267 static bool
1268 equal_keys (const char *a, const char *b)
1270 return !strcmp (a, b);
1274 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1275 for -mflip_mips16. It maps decl names onto a boolean mode setting. */
1276 static GTY (()) hash_map<const char *, bool, mips16_flip_traits> *
1277 mflip_mips16_htab;
1279 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1280 mode, false if it should next add an attribute for the opposite mode. */
1281 static GTY(()) bool mips16_flipper;
1283 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1284 for -mflip-mips16. Return true if it should use "mips16" and false if
1285 it should use "nomips16". */
1287 static bool
1288 mflip_mips16_use_mips16_p (tree decl)
1290 const char *name;
1291 bool base_is_mips16 = (mips_base_compression_flags & MASK_MIPS16) != 0;
1293 /* Use the opposite of the command-line setting for anonymous decls. */
1294 if (!DECL_NAME (decl))
1295 return !base_is_mips16;
1297 if (!mflip_mips16_htab)
1298 mflip_mips16_htab
1299 = hash_map<const char *, bool, mips16_flip_traits>::create_ggc (37);
1301 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1303 bool existed;
1304 bool *slot = &mflip_mips16_htab->get_or_insert (name, &existed);
1305 if (!existed)
1307 mips16_flipper = !mips16_flipper;
1308 *slot = mips16_flipper ? !base_is_mips16 : base_is_mips16;
1310 return *slot;
1313 /* Predicates to test for presence of "near" and "far"/"long_call"
1314 attributes on the given TYPE. */
1316 static bool
1317 mips_near_type_p (const_tree type)
1319 return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1322 static bool
1323 mips_far_type_p (const_tree type)
1325 return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1326 || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1330 /* Check if the interrupt attribute is set for a function. */
1332 static bool
1333 mips_interrupt_type_p (tree type)
1335 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1338 /* Check if the attribute to use shadow register set is set for a function. */
1340 static bool
1341 mips_use_shadow_register_set_p (tree type)
1343 return lookup_attribute ("use_shadow_register_set",
1344 TYPE_ATTRIBUTES (type)) != NULL;
1347 /* Check if the attribute to keep interrupts masked is set for a function. */
1349 static bool
1350 mips_keep_interrupts_masked_p (tree type)
1352 return lookup_attribute ("keep_interrupts_masked",
1353 TYPE_ATTRIBUTES (type)) != NULL;
1356 /* Check if the attribute to use debug exception return is set for
1357 a function. */
1359 static bool
1360 mips_use_debug_exception_return_p (tree type)
1362 return lookup_attribute ("use_debug_exception_return",
1363 TYPE_ATTRIBUTES (type)) != NULL;
1366 /* Return the set of compression modes that are explicitly required
1367 by the attributes in ATTRIBUTES. */
1369 static unsigned int
1370 mips_get_compress_on_flags (tree attributes)
1372 unsigned int flags = 0;
1374 if (lookup_attribute ("mips16", attributes) != NULL)
1375 flags |= MASK_MIPS16;
1377 if (lookup_attribute ("micromips", attributes) != NULL)
1378 flags |= MASK_MICROMIPS;
1380 return flags;
1383 /* Return the set of compression modes that are explicitly forbidden
1384 by the attributes in ATTRIBUTES. */
1386 static unsigned int
1387 mips_get_compress_off_flags (tree attributes)
1389 unsigned int flags = 0;
1391 if (lookup_attribute ("nocompression", attributes) != NULL)
1392 flags |= MASK_MIPS16 | MASK_MICROMIPS;
1394 if (lookup_attribute ("nomips16", attributes) != NULL)
1395 flags |= MASK_MIPS16;
1397 if (lookup_attribute ("nomicromips", attributes) != NULL)
1398 flags |= MASK_MICROMIPS;
1400 return flags;
1403 /* Return the compression mode that should be used for function DECL.
1404 Return the ambient setting if DECL is null. */
1406 static unsigned int
1407 mips_get_compress_mode (tree decl)
1409 unsigned int flags, force_on;
1411 flags = mips_base_compression_flags;
1412 if (decl)
1414 /* Nested functions must use the same frame pointer as their
1415 parent and must therefore use the same ISA mode. */
1416 tree parent = decl_function_context (decl);
1417 if (parent)
1418 decl = parent;
1419 force_on = mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1420 if (force_on)
1421 return force_on;
1422 flags &= ~mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1424 return flags;
1427 /* Return the attribute name associated with MASK_MIPS16 and MASK_MICROMIPS
1428 flags FLAGS. */
1430 static const char *
1431 mips_get_compress_on_name (unsigned int flags)
1433 if (flags == MASK_MIPS16)
1434 return "mips16";
1435 return "micromips";
1438 /* Return the attribute name that forbids MASK_MIPS16 and MASK_MICROMIPS
1439 flags FLAGS. */
1441 static const char *
1442 mips_get_compress_off_name (unsigned int flags)
1444 if (flags == MASK_MIPS16)
1445 return "nomips16";
1446 if (flags == MASK_MICROMIPS)
1447 return "nomicromips";
1448 return "nocompression";
1451 /* Implement TARGET_COMP_TYPE_ATTRIBUTES. */
1453 static int
1454 mips_comp_type_attributes (const_tree type1, const_tree type2)
1456 /* Disallow mixed near/far attributes. */
1457 if (mips_far_type_p (type1) && mips_near_type_p (type2))
1458 return 0;
1459 if (mips_near_type_p (type1) && mips_far_type_p (type2))
1460 return 0;
1461 return 1;
1464 /* Implement TARGET_INSERT_ATTRIBUTES. */
1466 static void
1467 mips_insert_attributes (tree decl, tree *attributes)
1469 const char *name;
1470 unsigned int compression_flags, nocompression_flags;
1472 /* Check for "mips16" and "nomips16" attributes. */
1473 compression_flags = mips_get_compress_on_flags (*attributes);
1474 nocompression_flags = mips_get_compress_off_flags (*attributes);
1476 if (TREE_CODE (decl) != FUNCTION_DECL)
1478 if (nocompression_flags)
1479 error ("%qs attribute only applies to functions",
1480 mips_get_compress_off_name (nocompression_flags));
1482 if (compression_flags)
1483 error ("%qs attribute only applies to functions",
1484 mips_get_compress_on_name (nocompression_flags));
1486 else
1488 compression_flags |= mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1489 nocompression_flags |=
1490 mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1492 if (compression_flags && nocompression_flags)
1493 error ("%qE cannot have both %qs and %qs attributes",
1494 DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
1495 mips_get_compress_off_name (nocompression_flags));
1497 if (compression_flags & MASK_MIPS16
1498 && compression_flags & MASK_MICROMIPS)
1499 error ("%qE cannot have both %qs and %qs attributes",
1500 DECL_NAME (decl), "mips16", "micromips");
1502 if (TARGET_FLIP_MIPS16
1503 && !DECL_ARTIFICIAL (decl)
1504 && compression_flags == 0
1505 && nocompression_flags == 0)
1507 /* Implement -mflip-mips16. If DECL has neither a "nomips16" nor a
1508 "mips16" attribute, arbitrarily pick one. We must pick the same
1509 setting for duplicate declarations of a function. */
1510 name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1511 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1512 name = "nomicromips";
1513 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1518 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
1520 static tree
1521 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1523 unsigned int diff;
1525 diff = (mips_get_compress_on_flags (DECL_ATTRIBUTES (olddecl))
1526 ^ mips_get_compress_on_flags (DECL_ATTRIBUTES (newdecl)));
1527 if (diff)
1528 error ("%qE redeclared with conflicting %qs attributes",
1529 DECL_NAME (newdecl), mips_get_compress_on_name (diff));
1531 diff = (mips_get_compress_off_flags (DECL_ATTRIBUTES (olddecl))
1532 ^ mips_get_compress_off_flags (DECL_ATTRIBUTES (newdecl)));
1533 if (diff)
1534 error ("%qE redeclared with conflicting %qs attributes",
1535 DECL_NAME (newdecl), mips_get_compress_off_name (diff));
1537 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1538 DECL_ATTRIBUTES (newdecl));
1541 /* Implement TARGET_CAN_INLINE_P. */
1543 static bool
1544 mips_can_inline_p (tree caller, tree callee)
1546 if (mips_get_compress_mode (callee) != mips_get_compress_mode (caller))
1547 return false;
1548 return default_target_can_inline_p (caller, callee);
1551 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1552 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
1554 static void
1555 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1557 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1559 *base_ptr = XEXP (x, 0);
1560 *offset_ptr = INTVAL (XEXP (x, 1));
1562 else
1564 *base_ptr = x;
1565 *offset_ptr = 0;
1569 static unsigned int mips_build_integer (struct mips_integer_op *,
1570 unsigned HOST_WIDE_INT);
1572 /* A subroutine of mips_build_integer, with the same interface.
1573 Assume that the final action in the sequence should be a left shift. */
1575 static unsigned int
1576 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1578 unsigned int i, shift;
1580 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1581 since signed numbers are easier to load than unsigned ones. */
1582 shift = 0;
1583 while ((value & 1) == 0)
1584 value /= 2, shift++;
1586 i = mips_build_integer (codes, value);
1587 codes[i].code = ASHIFT;
1588 codes[i].value = shift;
1589 return i + 1;
1592 /* As for mips_build_shift, but assume that the final action will be
1593 an IOR or PLUS operation. */
1595 static unsigned int
1596 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1598 unsigned HOST_WIDE_INT high;
1599 unsigned int i;
1601 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1602 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1604 /* The constant is too complex to load with a simple LUI/ORI pair,
1605 so we want to give the recursive call as many trailing zeros as
1606 possible. In this case, we know bit 16 is set and that the
1607 low 16 bits form a negative number. If we subtract that number
1608 from VALUE, we will clear at least the lowest 17 bits, maybe more. */
1609 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1610 codes[i].code = PLUS;
1611 codes[i].value = CONST_LOW_PART (value);
1613 else
1615 /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1616 bits gives a value with at least 17 trailing zeros. */
1617 i = mips_build_integer (codes, high);
1618 codes[i].code = IOR;
1619 codes[i].value = value & 0xffff;
1621 return i + 1;
1624 /* Fill CODES with a sequence of rtl operations to load VALUE.
1625 Return the number of operations needed. */
1627 static unsigned int
1628 mips_build_integer (struct mips_integer_op *codes,
1629 unsigned HOST_WIDE_INT value)
1631 if (SMALL_OPERAND (value)
1632 || SMALL_OPERAND_UNSIGNED (value)
1633 || LUI_OPERAND (value))
1635 /* The value can be loaded with a single instruction. */
1636 codes[0].code = UNKNOWN;
1637 codes[0].value = value;
1638 return 1;
1640 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1642 /* Either the constant is a simple LUI/ORI combination or its
1643 lowest bit is set. We don't want to shift in this case. */
1644 return mips_build_lower (codes, value);
1646 else if ((value & 0xffff) == 0)
1648 /* The constant will need at least three actions. The lowest
1649 16 bits are clear, so the final action will be a shift. */
1650 return mips_build_shift (codes, value);
1652 else
1654 /* The final action could be a shift, add or inclusive OR.
1655 Rather than use a complex condition to select the best
1656 approach, try both mips_build_shift and mips_build_lower
1657 and pick the one that gives the shortest sequence.
1658 Note that this case is only used once per constant. */
1659 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1660 unsigned int cost, alt_cost;
1662 cost = mips_build_shift (codes, value);
1663 alt_cost = mips_build_lower (alt_codes, value);
1664 if (alt_cost < cost)
1666 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1667 cost = alt_cost;
1669 return cost;
1673 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
1675 static bool
1676 mips_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1678 return mips_const_insns (x) > 0;
1681 /* Return a SYMBOL_REF for a MIPS16 function called NAME. */
1683 static rtx
1684 mips16_stub_function (const char *name)
1686 rtx x;
1688 x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1689 SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1690 return x;
1693 /* Return a legitimate call address for STUB, given that STUB is a MIPS16
1694 support function. */
1696 static rtx
1697 mips16_stub_call_address (mips_one_only_stub *stub)
1699 rtx fn = mips16_stub_function (stub->get_name ());
1700 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
1701 if (!call_insn_operand (fn, VOIDmode))
1702 fn = force_reg (Pmode, fn);
1703 return fn;
1706 /* A stub for moving the thread pointer into TLS_GET_TP_REGNUM. */
1708 class mips16_rdhwr_one_only_stub : public mips_one_only_stub
1710 virtual const char *get_name ();
1711 virtual void output_body ();
1714 const char *
1715 mips16_rdhwr_one_only_stub::get_name ()
1717 return "__mips16_rdhwr";
1720 void
1721 mips16_rdhwr_one_only_stub::output_body ()
1723 fprintf (asm_out_file,
1724 "\t.set\tpush\n"
1725 "\t.set\tmips32r2\n"
1726 "\t.set\tnoreorder\n"
1727 "\trdhwr\t$3,$29\n"
1728 "\t.set\tpop\n"
1729 "\tj\t$31\n");
1732 /* A stub for moving the FCSR into GET_FCSR_REGNUM. */
1733 class mips16_get_fcsr_one_only_stub : public mips_one_only_stub
1735 virtual const char *get_name ();
1736 virtual void output_body ();
1739 const char *
1740 mips16_get_fcsr_one_only_stub::get_name ()
1742 return "__mips16_get_fcsr";
1745 void
1746 mips16_get_fcsr_one_only_stub::output_body ()
1748 fprintf (asm_out_file,
1749 "\tcfc1\t%s,$31\n"
1750 "\tj\t$31\n", reg_names[GET_FCSR_REGNUM]);
1753 /* A stub for moving SET_FCSR_REGNUM into the FCSR. */
1754 class mips16_set_fcsr_one_only_stub : public mips_one_only_stub
1756 virtual const char *get_name ();
1757 virtual void output_body ();
1760 const char *
1761 mips16_set_fcsr_one_only_stub::get_name ()
1763 return "__mips16_set_fcsr";
1766 void
1767 mips16_set_fcsr_one_only_stub::output_body ()
1769 fprintf (asm_out_file,
1770 "\tctc1\t%s,$31\n"
1771 "\tj\t$31\n", reg_names[SET_FCSR_REGNUM]);
1774 /* Return true if symbols of type TYPE require a GOT access. */
1776 static bool
1777 mips_got_symbol_type_p (enum mips_symbol_type type)
1779 switch (type)
1781 case SYMBOL_GOT_PAGE_OFST:
1782 case SYMBOL_GOT_DISP:
1783 return true;
1785 default:
1786 return false;
1790 /* Return true if X is a thread-local symbol. */
1792 static bool
1793 mips_tls_symbol_p (rtx x)
1795 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1798 /* Return true if SYMBOL_REF X is associated with a global symbol
1799 (in the STB_GLOBAL sense). */
1801 static bool
1802 mips_global_symbol_p (const_rtx x)
1804 const_tree decl = SYMBOL_REF_DECL (x);
1806 if (!decl)
1807 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1809 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1810 or weak symbols. Relocations in the object file will be against
1811 the target symbol, so it's that symbol's binding that matters here. */
1812 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1815 /* Return true if function X is a libgcc MIPS16 stub function. */
1817 static bool
1818 mips16_stub_function_p (const_rtx x)
1820 return (GET_CODE (x) == SYMBOL_REF
1821 && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1824 /* Return true if function X is a locally-defined and locally-binding
1825 MIPS16 function. */
1827 static bool
1828 mips16_local_function_p (const_rtx x)
1830 return (GET_CODE (x) == SYMBOL_REF
1831 && SYMBOL_REF_LOCAL_P (x)
1832 && !SYMBOL_REF_EXTERNAL_P (x)
1833 && (mips_get_compress_mode (SYMBOL_REF_DECL (x)) & MASK_MIPS16));
1836 /* Return true if SYMBOL_REF X binds locally. */
1838 static bool
1839 mips_symbol_binds_local_p (const_rtx x)
1841 return (SYMBOL_REF_DECL (x)
1842 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1843 : SYMBOL_REF_LOCAL_P (x));
1846 /* Return true if rtx constants of mode MODE should be put into a small
1847 data section. */
1849 static bool
1850 mips_rtx_constant_in_small_data_p (machine_mode mode)
1852 return (!TARGET_EMBEDDED_DATA
1853 && TARGET_LOCAL_SDATA
1854 && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1857 /* Return true if X should not be moved directly into register $25.
1858 We need this because many versions of GAS will treat "la $25,foo" as
1859 part of a call sequence and so allow a global "foo" to be lazily bound. */
1861 bool
1862 mips_dangerous_for_la25_p (rtx x)
1864 return (!TARGET_EXPLICIT_RELOCS
1865 && TARGET_USE_GOT
1866 && GET_CODE (x) == SYMBOL_REF
1867 && mips_global_symbol_p (x));
1870 /* Return true if calls to X might need $25 to be valid on entry. */
1872 bool
1873 mips_use_pic_fn_addr_reg_p (const_rtx x)
1875 if (!TARGET_USE_PIC_FN_ADDR_REG)
1876 return false;
1878 /* MIPS16 stub functions are guaranteed not to use $25. */
1879 if (mips16_stub_function_p (x))
1880 return false;
1882 if (GET_CODE (x) == SYMBOL_REF)
1884 /* If PLTs and copy relocations are available, the static linker
1885 will make sure that $25 is valid on entry to the target function. */
1886 if (TARGET_ABICALLS_PIC0)
1887 return false;
1889 /* Locally-defined functions use absolute accesses to set up
1890 the global pointer. */
1891 if (TARGET_ABSOLUTE_ABICALLS
1892 && mips_symbol_binds_local_p (x)
1893 && !SYMBOL_REF_EXTERNAL_P (x))
1894 return false;
1897 return true;
1900 /* Return the method that should be used to access SYMBOL_REF or
1901 LABEL_REF X in context CONTEXT. */
1903 static enum mips_symbol_type
1904 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1906 if (TARGET_RTP_PIC)
1907 return SYMBOL_GOT_DISP;
1909 if (GET_CODE (x) == LABEL_REF)
1911 /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
1912 code and if we know that the label is in the current function's
1913 text section. LABEL_REFs are used for jump tables as well as
1914 text labels, so we must check whether jump tables live in the
1915 text section. */
1916 if (TARGET_MIPS16_SHORT_JUMP_TABLES
1917 && !LABEL_REF_NONLOCAL_P (x))
1918 return SYMBOL_PC_RELATIVE;
1920 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1921 return SYMBOL_GOT_PAGE_OFST;
1923 return SYMBOL_ABSOLUTE;
1926 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1928 if (SYMBOL_REF_TLS_MODEL (x))
1929 return SYMBOL_TLS;
1931 if (CONSTANT_POOL_ADDRESS_P (x))
1933 if (TARGET_MIPS16_TEXT_LOADS)
1934 return SYMBOL_PC_RELATIVE;
1936 if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1937 return SYMBOL_PC_RELATIVE;
1939 if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1940 return SYMBOL_GP_RELATIVE;
1943 /* Do not use small-data accesses for weak symbols; they may end up
1944 being zero. */
1945 if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1946 return SYMBOL_GP_RELATIVE;
1948 /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1949 is in effect. */
1950 if (TARGET_ABICALLS_PIC2
1951 && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1953 /* There are three cases to consider:
1955 - o32 PIC (either with or without explicit relocs)
1956 - n32/n64 PIC without explicit relocs
1957 - n32/n64 PIC with explicit relocs
1959 In the first case, both local and global accesses will use an
1960 R_MIPS_GOT16 relocation. We must correctly predict which of
1961 the two semantics (local or global) the assembler and linker
1962 will apply. The choice depends on the symbol's binding rather
1963 than its visibility.
1965 In the second case, the assembler will not use R_MIPS_GOT16
1966 relocations, but it chooses between local and global accesses
1967 in the same way as for o32 PIC.
1969 In the third case we have more freedom since both forms of
1970 access will work for any kind of symbol. However, there seems
1971 little point in doing things differently. */
1972 if (mips_global_symbol_p (x))
1973 return SYMBOL_GOT_DISP;
1975 return SYMBOL_GOT_PAGE_OFST;
1978 return SYMBOL_ABSOLUTE;
1981 /* Classify the base of symbolic expression X, given that X appears in
1982 context CONTEXT. */
1984 static enum mips_symbol_type
1985 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1987 rtx offset;
1989 split_const (x, &x, &offset);
1990 if (UNSPEC_ADDRESS_P (x))
1991 return UNSPEC_ADDRESS_TYPE (x);
1993 return mips_classify_symbol (x, context);
1996 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1997 is the alignment in bytes of SYMBOL_REF X. */
1999 static bool
2000 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
2002 HOST_WIDE_INT align;
2004 align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
2005 return IN_RANGE (offset, 0, align - 1);
2008 /* Return true if X is a symbolic constant that can be used in context
2009 CONTEXT. If it is, store the type of the symbol in *SYMBOL_TYPE. */
2011 bool
2012 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
2013 enum mips_symbol_type *symbol_type)
2015 rtx offset;
2017 split_const (x, &x, &offset);
2018 if (UNSPEC_ADDRESS_P (x))
2020 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
2021 x = UNSPEC_ADDRESS (x);
2023 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
2025 *symbol_type = mips_classify_symbol (x, context);
2026 if (*symbol_type == SYMBOL_TLS)
2027 return false;
2029 else
2030 return false;
2032 if (offset == const0_rtx)
2033 return true;
2035 /* Check whether a nonzero offset is valid for the underlying
2036 relocations. */
2037 switch (*symbol_type)
2039 case SYMBOL_ABSOLUTE:
2040 case SYMBOL_64_HIGH:
2041 case SYMBOL_64_MID:
2042 case SYMBOL_64_LOW:
2043 /* If the target has 64-bit pointers and the object file only
2044 supports 32-bit symbols, the values of those symbols will be
2045 sign-extended. In this case we can't allow an arbitrary offset
2046 in case the 32-bit value X + OFFSET has a different sign from X. */
2047 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
2048 return offset_within_block_p (x, INTVAL (offset));
2050 /* In other cases the relocations can handle any offset. */
2051 return true;
2053 case SYMBOL_PC_RELATIVE:
2054 /* Allow constant pool references to be converted to LABEL+CONSTANT.
2055 In this case, we no longer have access to the underlying constant,
2056 but the original symbol-based access was known to be valid. */
2057 if (GET_CODE (x) == LABEL_REF)
2058 return true;
2060 /* Fall through. */
2062 case SYMBOL_GP_RELATIVE:
2063 /* Make sure that the offset refers to something within the
2064 same object block. This should guarantee that the final
2065 PC- or GP-relative offset is within the 16-bit limit. */
2066 return offset_within_block_p (x, INTVAL (offset));
2068 case SYMBOL_GOT_PAGE_OFST:
2069 case SYMBOL_GOTOFF_PAGE:
2070 /* If the symbol is global, the GOT entry will contain the symbol's
2071 address, and we will apply a 16-bit offset after loading it.
2072 If the symbol is local, the linker should provide enough local
2073 GOT entries for a 16-bit offset, but larger offsets may lead
2074 to GOT overflow. */
2075 return SMALL_INT (offset);
2077 case SYMBOL_TPREL:
2078 case SYMBOL_DTPREL:
2079 /* There is no carry between the HI and LO REL relocations, so the
2080 offset is only valid if we know it won't lead to such a carry. */
2081 return mips_offset_within_alignment_p (x, INTVAL (offset));
2083 case SYMBOL_GOT_DISP:
2084 case SYMBOL_GOTOFF_DISP:
2085 case SYMBOL_GOTOFF_CALL:
2086 case SYMBOL_GOTOFF_LOADGP:
2087 case SYMBOL_TLSGD:
2088 case SYMBOL_TLSLDM:
2089 case SYMBOL_GOTTPREL:
2090 case SYMBOL_TLS:
2091 case SYMBOL_HALF:
2092 return false;
2094 gcc_unreachable ();
2097 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
2098 single instruction. We rely on the fact that, in the worst case,
2099 all instructions involved in a MIPS16 address calculation are usually
2100 extended ones. */
2102 static int
2103 mips_symbol_insns_1 (enum mips_symbol_type type, machine_mode mode)
2105 if (mips_use_pcrel_pool_p[(int) type])
2107 if (mode == MAX_MACHINE_MODE)
2108 /* LEAs will be converted into constant-pool references by
2109 mips_reorg. */
2110 type = SYMBOL_PC_RELATIVE;
2111 else
2112 /* The constant must be loaded and then dereferenced. */
2113 return 0;
2116 switch (type)
2118 case SYMBOL_ABSOLUTE:
2119 /* When using 64-bit symbols, we need 5 preparatory instructions,
2120 such as:
2122 lui $at,%highest(symbol)
2123 daddiu $at,$at,%higher(symbol)
2124 dsll $at,$at,16
2125 daddiu $at,$at,%hi(symbol)
2126 dsll $at,$at,16
2128 The final address is then $at + %lo(symbol). With 32-bit
2129 symbols we just need a preparatory LUI for normal mode and
2130 a preparatory LI and SLL for MIPS16. */
2131 return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
2133 case SYMBOL_GP_RELATIVE:
2134 /* Treat GP-relative accesses as taking a single instruction on
2135 MIPS16 too; the copy of $gp can often be shared. */
2136 return 1;
2138 case SYMBOL_PC_RELATIVE:
2139 /* PC-relative constants can be only be used with ADDIUPC,
2140 DADDIUPC, LWPC and LDPC. */
2141 if (mode == MAX_MACHINE_MODE
2142 || GET_MODE_SIZE (mode) == 4
2143 || GET_MODE_SIZE (mode) == 8)
2144 return 1;
2146 /* The constant must be loaded using ADDIUPC or DADDIUPC first. */
2147 return 0;
2149 case SYMBOL_GOT_DISP:
2150 /* The constant will have to be loaded from the GOT before it
2151 is used in an address. */
2152 if (mode != MAX_MACHINE_MODE)
2153 return 0;
2155 /* Fall through. */
2157 case SYMBOL_GOT_PAGE_OFST:
2158 /* Unless -funit-at-a-time is in effect, we can't be sure whether the
2159 local/global classification is accurate. The worst cases are:
2161 (1) For local symbols when generating o32 or o64 code. The assembler
2162 will use:
2164 lw $at,%got(symbol)
2167 ...and the final address will be $at + %lo(symbol).
2169 (2) For global symbols when -mxgot. The assembler will use:
2171 lui $at,%got_hi(symbol)
2172 (d)addu $at,$at,$gp
2174 ...and the final address will be $at + %got_lo(symbol). */
2175 return 3;
2177 case SYMBOL_GOTOFF_PAGE:
2178 case SYMBOL_GOTOFF_DISP:
2179 case SYMBOL_GOTOFF_CALL:
2180 case SYMBOL_GOTOFF_LOADGP:
2181 case SYMBOL_64_HIGH:
2182 case SYMBOL_64_MID:
2183 case SYMBOL_64_LOW:
2184 case SYMBOL_TLSGD:
2185 case SYMBOL_TLSLDM:
2186 case SYMBOL_DTPREL:
2187 case SYMBOL_GOTTPREL:
2188 case SYMBOL_TPREL:
2189 case SYMBOL_HALF:
2190 /* A 16-bit constant formed by a single relocation, or a 32-bit
2191 constant formed from a high 16-bit relocation and a low 16-bit
2192 relocation. Use mips_split_p to determine which. 32-bit
2193 constants need an "lui; addiu" sequence for normal mode and
2194 an "li; sll; addiu" sequence for MIPS16 mode. */
2195 return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
2197 case SYMBOL_TLS:
2198 /* We don't treat a bare TLS symbol as a constant. */
2199 return 0;
2201 gcc_unreachable ();
2204 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
2205 to load symbols of type TYPE into a register. Return 0 if the given
2206 type of symbol cannot be used as an immediate operand.
2208 Otherwise, return the number of instructions needed to load or store
2209 values of mode MODE to or from addresses of type TYPE. Return 0 if
2210 the given type of symbol is not valid in addresses.
2212 In both cases, instruction counts are based off BASE_INSN_LENGTH. */
2214 static int
2215 mips_symbol_insns (enum mips_symbol_type type, machine_mode mode)
2217 return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
2220 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2222 static bool
2223 mips_cannot_force_const_mem (machine_mode mode, rtx x)
2225 enum mips_symbol_type type;
2226 rtx base, offset;
2228 /* There is no assembler syntax for expressing an address-sized
2229 high part. */
2230 if (GET_CODE (x) == HIGH)
2231 return true;
2233 /* As an optimization, reject constants that mips_legitimize_move
2234 can expand inline.
2236 Suppose we have a multi-instruction sequence that loads constant C
2237 into register R. If R does not get allocated a hard register, and
2238 R is used in an operand that allows both registers and memory
2239 references, reload will consider forcing C into memory and using
2240 one of the instruction's memory alternatives. Returning false
2241 here will force it to use an input reload instead. */
2242 if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
2243 return true;
2245 split_const (x, &base, &offset);
2246 if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
2248 /* See whether we explicitly want these symbols in the pool. */
2249 if (mips_use_pcrel_pool_p[(int) type])
2250 return false;
2252 /* The same optimization as for CONST_INT. */
2253 if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2254 return true;
2256 /* If MIPS16 constant pools live in the text section, they should
2257 not refer to anything that might need run-time relocation. */
2258 if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2259 return true;
2262 /* TLS symbols must be computed by mips_legitimize_move. */
2263 if (tls_referenced_p (x))
2264 return true;
2266 return false;
2269 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. We can't use blocks for
2270 constants when we're using a per-function constant pool. */
2272 static bool
2273 mips_use_blocks_for_constant_p (machine_mode mode ATTRIBUTE_UNUSED,
2274 const_rtx x ATTRIBUTE_UNUSED)
2276 return !TARGET_MIPS16_PCREL_LOADS;
2279 /* Return true if register REGNO is a valid base register for mode MODE.
2280 STRICT_P is true if REG_OK_STRICT is in effect. */
2283 mips_regno_mode_ok_for_base_p (int regno, machine_mode mode,
2284 bool strict_p)
2286 if (!HARD_REGISTER_NUM_P (regno))
2288 if (!strict_p)
2289 return true;
2290 regno = reg_renumber[regno];
2293 /* These fake registers will be eliminated to either the stack or
2294 hard frame pointer, both of which are usually valid base registers.
2295 Reload deals with the cases where the eliminated form isn't valid. */
2296 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2297 return true;
2299 /* In MIPS16 mode, the stack pointer can only address word and doubleword
2300 values, nothing smaller. */
2301 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2302 return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2304 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2307 /* Return true if X is a valid base register for mode MODE.
2308 STRICT_P is true if REG_OK_STRICT is in effect. */
2310 static bool
2311 mips_valid_base_register_p (rtx x, machine_mode mode, bool strict_p)
2313 if (!strict_p && GET_CODE (x) == SUBREG)
2314 x = SUBREG_REG (x);
2316 return (REG_P (x)
2317 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2320 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2321 can address a value of mode MODE. */
2323 static bool
2324 mips_valid_offset_p (rtx x, machine_mode mode)
2326 /* Check that X is a signed 16-bit number. */
2327 if (!const_arith_operand (x, Pmode))
2328 return false;
2330 /* We may need to split multiword moves, so make sure that every word
2331 is accessible. */
2332 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2333 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2334 return false;
2336 return true;
2339 /* Return true if a LO_SUM can address a value of mode MODE when the
2340 LO_SUM symbol has type SYMBOL_TYPE. */
2342 static bool
2343 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, machine_mode mode)
2345 /* Check that symbols of type SYMBOL_TYPE can be used to access values
2346 of mode MODE. */
2347 if (mips_symbol_insns (symbol_type, mode) == 0)
2348 return false;
2350 /* Check that there is a known low-part relocation. */
2351 if (mips_lo_relocs[symbol_type] == NULL)
2352 return false;
2354 /* We may need to split multiword moves, so make sure that each word
2355 can be accessed without inducing a carry. This is mainly needed
2356 for o64, which has historically only guaranteed 64-bit alignment
2357 for 128-bit types. */
2358 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2359 && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2360 return false;
2362 return true;
2365 /* Return true if X is a valid address for machine mode MODE. If it is,
2366 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
2367 effect. */
2369 static bool
2370 mips_classify_address (struct mips_address_info *info, rtx x,
2371 machine_mode mode, bool strict_p)
2373 switch (GET_CODE (x))
2375 case REG:
2376 case SUBREG:
2377 info->type = ADDRESS_REG;
2378 info->reg = x;
2379 info->offset = const0_rtx;
2380 return mips_valid_base_register_p (info->reg, mode, strict_p);
2382 case PLUS:
2383 info->type = ADDRESS_REG;
2384 info->reg = XEXP (x, 0);
2385 info->offset = XEXP (x, 1);
2386 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2387 && mips_valid_offset_p (info->offset, mode));
2389 case LO_SUM:
2390 info->type = ADDRESS_LO_SUM;
2391 info->reg = XEXP (x, 0);
2392 info->offset = XEXP (x, 1);
2393 /* We have to trust the creator of the LO_SUM to do something vaguely
2394 sane. Target-independent code that creates a LO_SUM should also
2395 create and verify the matching HIGH. Target-independent code that
2396 adds an offset to a LO_SUM must prove that the offset will not
2397 induce a carry. Failure to do either of these things would be
2398 a bug, and we are not required to check for it here. The MIPS
2399 backend itself should only create LO_SUMs for valid symbolic
2400 constants, with the high part being either a HIGH or a copy
2401 of _gp. */
2402 info->symbol_type
2403 = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2404 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2405 && mips_valid_lo_sum_p (info->symbol_type, mode));
2407 case CONST_INT:
2408 /* Small-integer addresses don't occur very often, but they
2409 are legitimate if $0 is a valid base register. */
2410 info->type = ADDRESS_CONST_INT;
2411 return !TARGET_MIPS16 && SMALL_INT (x);
2413 case CONST:
2414 case LABEL_REF:
2415 case SYMBOL_REF:
2416 info->type = ADDRESS_SYMBOLIC;
2417 return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2418 &info->symbol_type)
2419 && mips_symbol_insns (info->symbol_type, mode) > 0
2420 && !mips_split_p[info->symbol_type]);
2422 default:
2423 return false;
2427 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2429 static bool
2430 mips_legitimate_address_p (machine_mode mode, rtx x, bool strict_p)
2432 struct mips_address_info addr;
2434 return mips_classify_address (&addr, x, mode, strict_p);
2437 /* Return true if X is a legitimate $sp-based address for mode MDOE. */
2439 bool
2440 mips_stack_address_p (rtx x, machine_mode mode)
2442 struct mips_address_info addr;
2444 return (mips_classify_address (&addr, x, mode, false)
2445 && addr.type == ADDRESS_REG
2446 && addr.reg == stack_pointer_rtx);
2449 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2450 address instruction. Note that such addresses are not considered
2451 legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2452 is so restricted. */
2454 static bool
2455 mips_lwxs_address_p (rtx addr)
2457 if (ISA_HAS_LWXS
2458 && GET_CODE (addr) == PLUS
2459 && REG_P (XEXP (addr, 1)))
2461 rtx offset = XEXP (addr, 0);
2462 if (GET_CODE (offset) == MULT
2463 && REG_P (XEXP (offset, 0))
2464 && CONST_INT_P (XEXP (offset, 1))
2465 && INTVAL (XEXP (offset, 1)) == 4)
2466 return true;
2468 return false;
2471 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load
2472 indexed address instruction. Note that such addresses are
2473 not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2474 sense, because their use is so restricted. */
2476 static bool
2477 mips_lx_address_p (rtx addr, machine_mode mode)
2479 if (GET_CODE (addr) != PLUS
2480 || !REG_P (XEXP (addr, 0))
2481 || !REG_P (XEXP (addr, 1)))
2482 return false;
2483 if (ISA_HAS_LBX && mode == QImode)
2484 return true;
2485 if (ISA_HAS_LHX && mode == HImode)
2486 return true;
2487 if (ISA_HAS_LWX && mode == SImode)
2488 return true;
2489 if (ISA_HAS_LDX && mode == DImode)
2490 return true;
2491 return false;
2494 /* Return true if a value at OFFSET bytes from base register BASE can be
2495 accessed using an unextended MIPS16 instruction. MODE is the mode of
2496 the value.
2498 Usually the offset in an unextended instruction is a 5-bit field.
2499 The offset is unsigned and shifted left once for LH and SH, twice
2500 for LW and SW, and so on. An exception is LWSP and SWSP, which have
2501 an 8-bit immediate field that's shifted left twice. */
2503 static bool
2504 mips16_unextended_reference_p (machine_mode mode, rtx base,
2505 unsigned HOST_WIDE_INT offset)
2507 if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2509 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2510 return offset < 256U * GET_MODE_SIZE (mode);
2511 return offset < 32U * GET_MODE_SIZE (mode);
2513 return false;
2516 /* Return the number of instructions needed to load or store a value
2517 of mode MODE at address X, assuming that BASE_INSN_LENGTH is the
2518 length of one instruction. Return 0 if X isn't valid for MODE.
2519 Assume that multiword moves may need to be split into word moves
2520 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2521 enough. */
2524 mips_address_insns (rtx x, machine_mode mode, bool might_split_p)
2526 struct mips_address_info addr;
2527 int factor;
2529 /* BLKmode is used for single unaligned loads and stores and should
2530 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
2531 meaningless, so we have to single it out as a special case one way
2532 or the other.) */
2533 if (mode != BLKmode && might_split_p)
2534 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2535 else
2536 factor = 1;
2538 if (mips_classify_address (&addr, x, mode, false))
2539 switch (addr.type)
2541 case ADDRESS_REG:
2542 if (TARGET_MIPS16
2543 && !mips16_unextended_reference_p (mode, addr.reg,
2544 UINTVAL (addr.offset)))
2545 return factor * 2;
2546 return factor;
2548 case ADDRESS_LO_SUM:
2549 return TARGET_MIPS16 ? factor * 2 : factor;
2551 case ADDRESS_CONST_INT:
2552 return factor;
2554 case ADDRESS_SYMBOLIC:
2555 return factor * mips_symbol_insns (addr.symbol_type, mode);
2557 return 0;
2560 /* Return true if X fits within an unsigned field of BITS bits that is
2561 shifted left SHIFT bits before being used. */
2563 bool
2564 mips_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2566 return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2569 /* Return true if X fits within a signed field of BITS bits that is
2570 shifted left SHIFT bits before being used. */
2572 bool
2573 mips_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2575 x += 1 << (bits + shift - 1);
2576 return mips_unsigned_immediate_p (x, bits, shift);
2579 /* Return true if X is legitimate for accessing values of mode MODE,
2580 if it is based on a MIPS16 register, and if the offset satisfies
2581 OFFSET_PREDICATE. */
2583 bool
2584 m16_based_address_p (rtx x, machine_mode mode,
2585 insn_operand_predicate_fn offset_predicate)
2587 struct mips_address_info addr;
2589 return (mips_classify_address (&addr, x, mode, false)
2590 && addr.type == ADDRESS_REG
2591 && M16_REG_P (REGNO (addr.reg))
2592 && offset_predicate (addr.offset, mode));
2595 /* Return true if X is a legitimate address that conforms to the requirements
2596 for a microMIPS LWSP or SWSP insn. */
2598 bool
2599 lwsp_swsp_address_p (rtx x, machine_mode mode)
2601 struct mips_address_info addr;
2603 return (mips_classify_address (&addr, x, mode, false)
2604 && addr.type == ADDRESS_REG
2605 && REGNO (addr.reg) == STACK_POINTER_REGNUM
2606 && uw5_operand (addr.offset, mode));
2609 /* Return true if X is a legitimate address with a 12-bit offset.
2610 MODE is the mode of the value being accessed. */
2612 bool
2613 umips_12bit_offset_address_p (rtx x, machine_mode mode)
2615 struct mips_address_info addr;
2617 return (mips_classify_address (&addr, x, mode, false)
2618 && addr.type == ADDRESS_REG
2619 && CONST_INT_P (addr.offset)
2620 && UMIPS_12BIT_OFFSET_P (INTVAL (addr.offset)));
2623 /* Return true if X is a legitimate address with a 9-bit offset.
2624 MODE is the mode of the value being accessed. */
2626 bool
2627 mips_9bit_offset_address_p (rtx x, machine_mode mode)
2629 struct mips_address_info addr;
2631 return (mips_classify_address (&addr, x, mode, false)
2632 && addr.type == ADDRESS_REG
2633 && CONST_INT_P (addr.offset)
2634 && MIPS_9BIT_OFFSET_P (INTVAL (addr.offset)));
2637 /* Return the number of instructions needed to load constant X,
2638 assuming that BASE_INSN_LENGTH is the length of one instruction.
2639 Return 0 if X isn't a valid constant. */
2642 mips_const_insns (rtx x)
2644 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2645 enum mips_symbol_type symbol_type;
2646 rtx offset;
2648 switch (GET_CODE (x))
2650 case HIGH:
2651 if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2652 &symbol_type)
2653 || !mips_split_p[symbol_type])
2654 return 0;
2656 /* This is simply an LUI for normal mode. It is an extended
2657 LI followed by an extended SLL for MIPS16. */
2658 return TARGET_MIPS16 ? 4 : 1;
2660 case CONST_INT:
2661 if (TARGET_MIPS16)
2662 /* Unsigned 8-bit constants can be loaded using an unextended
2663 LI instruction. Unsigned 16-bit constants can be loaded
2664 using an extended LI. Negative constants must be loaded
2665 using LI and then negated. */
2666 return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2667 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2668 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2669 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2670 : 0);
2672 return mips_build_integer (codes, INTVAL (x));
2674 case CONST_DOUBLE:
2675 case CONST_VECTOR:
2676 /* Allow zeros for normal mode, where we can use $0. */
2677 return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2679 case CONST:
2680 if (CONST_GP_P (x))
2681 return 1;
2683 /* See if we can refer to X directly. */
2684 if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2685 return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2687 /* Otherwise try splitting the constant into a base and offset.
2688 If the offset is a 16-bit value, we can load the base address
2689 into a register and then use (D)ADDIU to add in the offset.
2690 If the offset is larger, we can load the base and offset
2691 into separate registers and add them together with (D)ADDU.
2692 However, the latter is only possible before reload; during
2693 and after reload, we must have the option of forcing the
2694 constant into the pool instead. */
2695 split_const (x, &x, &offset);
2696 if (offset != 0)
2698 int n = mips_const_insns (x);
2699 if (n != 0)
2701 if (SMALL_INT (offset))
2702 return n + 1;
2703 else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2704 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2707 return 0;
2709 case SYMBOL_REF:
2710 case LABEL_REF:
2711 return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2712 MAX_MACHINE_MODE);
2714 default:
2715 return 0;
2719 /* X is a doubleword constant that can be handled by splitting it into
2720 two words and loading each word separately. Return the number of
2721 instructions required to do this, assuming that BASE_INSN_LENGTH
2722 is the length of one instruction. */
2725 mips_split_const_insns (rtx x)
2727 unsigned int low, high;
2729 low = mips_const_insns (mips_subword (x, false));
2730 high = mips_const_insns (mips_subword (x, true));
2731 gcc_assert (low > 0 && high > 0);
2732 return low + high;
2735 /* Return the number of instructions needed to implement INSN,
2736 given that it loads from or stores to MEM. Assume that
2737 BASE_INSN_LENGTH is the length of one instruction. */
2740 mips_load_store_insns (rtx mem, rtx_insn *insn)
2742 machine_mode mode;
2743 bool might_split_p;
2744 rtx set;
2746 gcc_assert (MEM_P (mem));
2747 mode = GET_MODE (mem);
2749 /* Try to prove that INSN does not need to be split. */
2750 might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2751 if (might_split_p)
2753 set = single_set (insn);
2754 if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
2755 might_split_p = false;
2758 return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2761 /* Return the number of instructions needed for an integer division,
2762 assuming that BASE_INSN_LENGTH is the length of one instruction. */
2765 mips_idiv_insns (void)
2767 int count;
2769 count = 1;
2770 if (TARGET_CHECK_ZERO_DIV)
2772 if (GENERATE_DIVIDE_TRAPS)
2773 count++;
2774 else
2775 count += 2;
2778 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2779 count++;
2780 return count;
2783 /* Emit a move from SRC to DEST. Assume that the move expanders can
2784 handle all moves if !can_create_pseudo_p (). The distinction is
2785 important because, unlike emit_move_insn, the move expanders know
2786 how to force Pmode objects into the constant pool even when the
2787 constant pool address is not itself legitimate. */
2789 rtx_insn *
2790 mips_emit_move (rtx dest, rtx src)
2792 return (can_create_pseudo_p ()
2793 ? emit_move_insn (dest, src)
2794 : emit_move_insn_1 (dest, src));
2797 /* Emit a move from SRC to DEST, splitting compound moves into individual
2798 instructions. SPLIT_TYPE is the type of split to perform. */
2800 static void
2801 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
2803 if (mips_split_move_p (dest, src, split_type))
2804 mips_split_move (dest, src, split_type);
2805 else
2806 mips_emit_move (dest, src);
2809 /* Emit an instruction of the form (set TARGET (CODE OP0)). */
2811 static void
2812 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2814 emit_insn (gen_rtx_SET (VOIDmode, target,
2815 gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2818 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2819 Return that new register. */
2821 static rtx
2822 mips_force_unary (machine_mode mode, enum rtx_code code, rtx op0)
2824 rtx reg;
2826 reg = gen_reg_rtx (mode);
2827 mips_emit_unary (code, reg, op0);
2828 return reg;
2831 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2833 void
2834 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2836 emit_insn (gen_rtx_SET (VOIDmode, target,
2837 gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2840 /* Compute (CODE OP0 OP1) and store the result in a new register
2841 of mode MODE. Return that new register. */
2843 static rtx
2844 mips_force_binary (machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2846 rtx reg;
2848 reg = gen_reg_rtx (mode);
2849 mips_emit_binary (code, reg, op0, op1);
2850 return reg;
2853 /* Copy VALUE to a register and return that register. If new pseudos
2854 are allowed, copy it into a new register, otherwise use DEST. */
2856 static rtx
2857 mips_force_temporary (rtx dest, rtx value)
2859 if (can_create_pseudo_p ())
2860 return force_reg (Pmode, value);
2861 else
2863 mips_emit_move (dest, value);
2864 return dest;
2868 /* Emit a call sequence with call pattern PATTERN and return the call
2869 instruction itself (which is not necessarily the last instruction
2870 emitted). ORIG_ADDR is the original, unlegitimized address,
2871 ADDR is the legitimized form, and LAZY_P is true if the call
2872 address is lazily-bound. */
2874 static rtx_insn *
2875 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2877 rtx_insn *insn;
2878 rtx reg;
2880 insn = emit_call_insn (pattern);
2882 if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2884 /* MIPS16 JALRs only take MIPS16 registers. If the target
2885 function requires $25 to be valid on entry, we must copy it
2886 there separately. The move instruction can be put in the
2887 call's delay slot. */
2888 reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2889 emit_insn_before (gen_move_insn (reg, addr), insn);
2890 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2893 if (lazy_p)
2894 /* Lazy-binding stubs require $gp to be valid on entry. */
2895 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2897 if (TARGET_USE_GOT)
2899 /* See the comment above load_call<mode> for details. */
2900 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2901 gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2902 emit_insn (gen_update_got_version ());
2905 if (TARGET_MIPS16
2906 && TARGET_EXPLICIT_RELOCS
2907 && TARGET_CALL_CLOBBERED_GP)
2909 rtx post_call_tmp_reg = gen_rtx_REG (word_mode, POST_CALL_TMP_REG);
2910 clobber_reg (&CALL_INSN_FUNCTION_USAGE (insn), post_call_tmp_reg);
2913 return insn;
2916 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2917 then add CONST_INT OFFSET to the result. */
2919 static rtx
2920 mips_unspec_address_offset (rtx base, rtx offset,
2921 enum mips_symbol_type symbol_type)
2923 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2924 UNSPEC_ADDRESS_FIRST + symbol_type);
2925 if (offset != const0_rtx)
2926 base = gen_rtx_PLUS (Pmode, base, offset);
2927 return gen_rtx_CONST (Pmode, base);
2930 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2931 type SYMBOL_TYPE. */
2934 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2936 rtx base, offset;
2938 split_const (address, &base, &offset);
2939 return mips_unspec_address_offset (base, offset, symbol_type);
2942 /* If OP is an UNSPEC address, return the address to which it refers,
2943 otherwise return OP itself. */
2946 mips_strip_unspec_address (rtx op)
2948 rtx base, offset;
2950 split_const (op, &base, &offset);
2951 if (UNSPEC_ADDRESS_P (base))
2952 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2953 return op;
2956 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2957 high part to BASE and return the result. Just return BASE otherwise.
2958 TEMP is as for mips_force_temporary.
2960 The returned expression can be used as the first operand to a LO_SUM. */
2962 static rtx
2963 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2964 enum mips_symbol_type symbol_type)
2966 if (mips_split_p[symbol_type])
2968 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2969 addr = mips_force_temporary (temp, addr);
2970 base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2972 return base;
2975 /* Return an instruction that copies $gp into register REG. We want
2976 GCC to treat the register's value as constant, so that its value
2977 can be rematerialized on demand. */
2979 static rtx
2980 gen_load_const_gp (rtx reg)
2982 return PMODE_INSN (gen_load_const_gp, (reg));
2985 /* Return a pseudo register that contains the value of $gp throughout
2986 the current function. Such registers are needed by MIPS16 functions,
2987 for which $gp itself is not a valid base register or addition operand. */
2989 static rtx
2990 mips16_gp_pseudo_reg (void)
2992 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2994 rtx_insn *scan;
2996 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2998 push_topmost_sequence ();
3000 scan = get_insns ();
3001 while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
3002 scan = NEXT_INSN (scan);
3004 rtx set = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
3005 rtx_insn *insn = emit_insn_after (set, scan);
3006 INSN_LOCATION (insn) = 0;
3008 pop_topmost_sequence ();
3011 return cfun->machine->mips16_gp_pseudo_rtx;
3014 /* Return a base register that holds pic_offset_table_rtx.
3015 TEMP, if nonnull, is a scratch Pmode base register. */
3018 mips_pic_base_register (rtx temp)
3020 if (!TARGET_MIPS16)
3021 return pic_offset_table_rtx;
3023 if (currently_expanding_to_rtl)
3024 return mips16_gp_pseudo_reg ();
3026 if (can_create_pseudo_p ())
3027 temp = gen_reg_rtx (Pmode);
3029 if (TARGET_USE_GOT)
3030 /* The first post-reload split exposes all references to $gp
3031 (both uses and definitions). All references must remain
3032 explicit after that point.
3034 It is safe to introduce uses of $gp at any time, so for
3035 simplicity, we do that before the split too. */
3036 mips_emit_move (temp, pic_offset_table_rtx);
3037 else
3038 emit_insn (gen_load_const_gp (temp));
3039 return temp;
3042 /* Return the RHS of a load_call<mode> insn. */
3044 static rtx
3045 mips_unspec_call (rtx reg, rtx symbol)
3047 rtvec vec;
3049 vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
3050 return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
3053 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
3054 reference. Return NULL_RTX otherwise. */
3056 static rtx
3057 mips_strip_unspec_call (rtx src)
3059 if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
3060 return mips_strip_unspec_address (XVECEXP (src, 0, 1));
3061 return NULL_RTX;
3064 /* Create and return a GOT reference of type TYPE for address ADDR.
3065 TEMP, if nonnull, is a scratch Pmode base register. */
3068 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
3070 rtx base, high, lo_sum_symbol;
3072 base = mips_pic_base_register (temp);
3074 /* If we used the temporary register to load $gp, we can't use
3075 it for the high part as well. */
3076 if (temp != NULL && reg_overlap_mentioned_p (base, temp))
3077 temp = NULL;
3079 high = mips_unspec_offset_high (temp, base, addr, type);
3080 lo_sum_symbol = mips_unspec_address (addr, type);
3082 if (type == SYMBOL_GOTOFF_CALL)
3083 return mips_unspec_call (high, lo_sum_symbol);
3084 else
3085 return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
3088 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
3089 it appears in a MEM of that mode. Return true if ADDR is a legitimate
3090 constant in that context and can be split into high and low parts.
3091 If so, and if LOW_OUT is nonnull, emit the high part and store the
3092 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
3094 TEMP is as for mips_force_temporary and is used to load the high
3095 part into a register.
3097 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
3098 a legitimize SET_SRC for an .md pattern, otherwise the low part
3099 is guaranteed to be a legitimate address for mode MODE. */
3101 bool
3102 mips_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
3104 enum mips_symbol_context context;
3105 enum mips_symbol_type symbol_type;
3106 rtx high;
3108 context = (mode == MAX_MACHINE_MODE
3109 ? SYMBOL_CONTEXT_LEA
3110 : SYMBOL_CONTEXT_MEM);
3111 if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
3113 addr = XEXP (addr, 0);
3114 if (mips_symbolic_constant_p (addr, context, &symbol_type)
3115 && mips_symbol_insns (symbol_type, mode) > 0
3116 && mips_split_hi_p[symbol_type])
3118 if (low_out)
3119 switch (symbol_type)
3121 case SYMBOL_GOT_PAGE_OFST:
3122 /* The high part of a page/ofst pair is loaded from the GOT. */
3123 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
3124 break;
3126 default:
3127 gcc_unreachable ();
3129 return true;
3132 else
3134 if (mips_symbolic_constant_p (addr, context, &symbol_type)
3135 && mips_symbol_insns (symbol_type, mode) > 0
3136 && mips_split_p[symbol_type])
3138 if (low_out)
3139 switch (symbol_type)
3141 case SYMBOL_GOT_DISP:
3142 /* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
3143 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
3144 break;
3146 case SYMBOL_GP_RELATIVE:
3147 high = mips_pic_base_register (temp);
3148 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3149 break;
3151 default:
3152 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
3153 high = mips_force_temporary (temp, high);
3154 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3155 break;
3157 return true;
3160 return false;
3163 /* Return a legitimate address for REG + OFFSET. TEMP is as for
3164 mips_force_temporary; it is only needed when OFFSET is not a
3165 SMALL_OPERAND. */
3167 static rtx
3168 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
3170 if (!SMALL_OPERAND (offset))
3172 rtx high;
3174 if (TARGET_MIPS16)
3176 /* Load the full offset into a register so that we can use
3177 an unextended instruction for the address itself. */
3178 high = GEN_INT (offset);
3179 offset = 0;
3181 else
3183 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
3184 The addition inside the macro CONST_HIGH_PART may cause an
3185 overflow, so we need to force a sign-extension check. */
3186 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
3187 offset = CONST_LOW_PART (offset);
3189 high = mips_force_temporary (temp, high);
3190 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
3192 return plus_constant (Pmode, reg, offset);
3195 /* The __tls_get_attr symbol. */
3196 static GTY(()) rtx mips_tls_symbol;
3198 /* Return an instruction sequence that calls __tls_get_addr. SYM is
3199 the TLS symbol we are referencing and TYPE is the symbol type to use
3200 (either global dynamic or local dynamic). V0 is an RTX for the
3201 return value location. */
3203 static rtx
3204 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
3206 rtx insn, loc, a0;
3208 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3210 if (!mips_tls_symbol)
3211 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
3213 loc = mips_unspec_address (sym, type);
3215 start_sequence ();
3217 emit_insn (gen_rtx_SET (Pmode, a0,
3218 gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
3219 insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
3220 const0_rtx, NULL_RTX, false);
3221 RTL_CONST_CALL_P (insn) = 1;
3222 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
3223 insn = get_insns ();
3225 end_sequence ();
3227 return insn;
3230 /* Return a pseudo register that contains the current thread pointer. */
3233 mips_expand_thread_pointer (rtx tp)
3235 rtx fn;
3237 if (TARGET_MIPS16)
3239 if (!mips16_rdhwr_stub)
3240 mips16_rdhwr_stub = new mips16_rdhwr_one_only_stub ();
3241 fn = mips16_stub_call_address (mips16_rdhwr_stub);
3242 emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
3244 else
3245 emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
3246 return tp;
3249 static rtx
3250 mips_get_tp (void)
3252 return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
3255 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
3256 its address. The return value will be both a valid address and a valid
3257 SET_SRC (either a REG or a LO_SUM). */
3259 static rtx
3260 mips_legitimize_tls_address (rtx loc)
3262 rtx dest, insn, v0, tp, tmp1, tmp2, eqv, offset;
3263 enum tls_model model;
3265 model = SYMBOL_REF_TLS_MODEL (loc);
3266 /* Only TARGET_ABICALLS code can have more than one module; other
3267 code must be be static and should not use a GOT. All TLS models
3268 reduce to local exec in this situation. */
3269 if (!TARGET_ABICALLS)
3270 model = TLS_MODEL_LOCAL_EXEC;
3272 switch (model)
3274 case TLS_MODEL_GLOBAL_DYNAMIC:
3275 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3276 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
3277 dest = gen_reg_rtx (Pmode);
3278 emit_libcall_block (insn, dest, v0, loc);
3279 break;
3281 case TLS_MODEL_LOCAL_DYNAMIC:
3282 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3283 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
3284 tmp1 = gen_reg_rtx (Pmode);
3286 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3287 share the LDM result with other LD model accesses. */
3288 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3289 UNSPEC_TLS_LDM);
3290 emit_libcall_block (insn, tmp1, v0, eqv);
3292 offset = mips_unspec_address (loc, SYMBOL_DTPREL);
3293 if (mips_split_p[SYMBOL_DTPREL])
3295 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
3296 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3298 else
3299 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3300 0, 0, OPTAB_DIRECT);
3301 break;
3303 case TLS_MODEL_INITIAL_EXEC:
3304 tp = mips_get_tp ();
3305 tmp1 = gen_reg_rtx (Pmode);
3306 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
3307 if (Pmode == DImode)
3308 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
3309 else
3310 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
3311 dest = gen_reg_rtx (Pmode);
3312 emit_insn (gen_add3_insn (dest, tmp1, tp));
3313 break;
3315 case TLS_MODEL_LOCAL_EXEC:
3316 tmp1 = mips_get_tp ();
3317 offset = mips_unspec_address (loc, SYMBOL_TPREL);
3318 if (mips_split_p[SYMBOL_TPREL])
3320 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
3321 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3323 else
3324 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3325 0, 0, OPTAB_DIRECT);
3326 break;
3328 default:
3329 gcc_unreachable ();
3331 return dest;
3334 /* Implement "TARGET = __builtin_mips_get_fcsr ()" for MIPS16,
3335 using a stub. */
3337 void
3338 mips16_expand_get_fcsr (rtx target)
3340 if (!mips16_get_fcsr_stub)
3341 mips16_get_fcsr_stub = new mips16_get_fcsr_one_only_stub ();
3342 rtx fn = mips16_stub_call_address (mips16_get_fcsr_stub);
3343 emit_insn (PMODE_INSN (gen_mips_get_fcsr_mips16, (fn)));
3344 emit_move_insn (target, gen_rtx_REG (SImode, GET_FCSR_REGNUM));
3347 /* Implement __builtin_mips_set_fcsr (TARGET) for MIPS16, using a stub. */
3349 void
3350 mips16_expand_set_fcsr (rtx newval)
3352 if (!mips16_set_fcsr_stub)
3353 mips16_set_fcsr_stub = new mips16_set_fcsr_one_only_stub ();
3354 rtx fn = mips16_stub_call_address (mips16_set_fcsr_stub);
3355 emit_move_insn (gen_rtx_REG (SImode, SET_FCSR_REGNUM), newval);
3356 emit_insn (PMODE_INSN (gen_mips_set_fcsr_mips16, (fn)));
3359 /* If X is not a valid address for mode MODE, force it into a register. */
3361 static rtx
3362 mips_force_address (rtx x, machine_mode mode)
3364 if (!mips_legitimate_address_p (mode, x, false))
3365 x = force_reg (Pmode, x);
3366 return x;
3369 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
3370 be legitimized in a way that the generic machinery might not expect,
3371 return a new address, otherwise return NULL. MODE is the mode of
3372 the memory being accessed. */
3374 static rtx
3375 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3376 machine_mode mode)
3378 rtx base, addr;
3379 HOST_WIDE_INT offset;
3381 if (mips_tls_symbol_p (x))
3382 return mips_legitimize_tls_address (x);
3384 /* See if the address can split into a high part and a LO_SUM. */
3385 if (mips_split_symbol (NULL, x, mode, &addr))
3386 return mips_force_address (addr, mode);
3388 /* Handle BASE + OFFSET using mips_add_offset. */
3389 mips_split_plus (x, &base, &offset);
3390 if (offset != 0)
3392 if (!mips_valid_base_register_p (base, mode, false))
3393 base = copy_to_mode_reg (Pmode, base);
3394 addr = mips_add_offset (NULL, base, offset);
3395 return mips_force_address (addr, mode);
3398 return x;
3401 /* Load VALUE into DEST. TEMP is as for mips_force_temporary. */
3403 void
3404 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3406 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3407 machine_mode mode;
3408 unsigned int i, num_ops;
3409 rtx x;
3411 mode = GET_MODE (dest);
3412 num_ops = mips_build_integer (codes, value);
3414 /* Apply each binary operation to X. Invariant: X is a legitimate
3415 source operand for a SET pattern. */
3416 x = GEN_INT (codes[0].value);
3417 for (i = 1; i < num_ops; i++)
3419 if (!can_create_pseudo_p ())
3421 emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3422 x = temp;
3424 else
3425 x = force_reg (mode, x);
3426 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3429 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3432 /* Subroutine of mips_legitimize_move. Move constant SRC into register
3433 DEST given that SRC satisfies immediate_operand but doesn't satisfy
3434 move_operand. */
3436 static void
3437 mips_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
3439 rtx base, offset;
3441 /* Split moves of big integers into smaller pieces. */
3442 if (splittable_const_int_operand (src, mode))
3444 mips_move_integer (dest, dest, INTVAL (src));
3445 return;
3448 /* Split moves of symbolic constants into high/low pairs. */
3449 if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3451 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3452 return;
3455 /* Generate the appropriate access sequences for TLS symbols. */
3456 if (mips_tls_symbol_p (src))
3458 mips_emit_move (dest, mips_legitimize_tls_address (src));
3459 return;
3462 /* If we have (const (plus symbol offset)), and that expression cannot
3463 be forced into memory, load the symbol first and add in the offset.
3464 In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3465 forced into memory, as it usually produces better code. */
3466 split_const (src, &base, &offset);
3467 if (offset != const0_rtx
3468 && (targetm.cannot_force_const_mem (mode, src)
3469 || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3471 base = mips_force_temporary (dest, base);
3472 mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3473 return;
3476 src = force_const_mem (mode, src);
3478 /* When using explicit relocs, constant pool references are sometimes
3479 not legitimate addresses. */
3480 mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3481 mips_emit_move (dest, src);
3484 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3485 sequence that is valid. */
3487 bool
3488 mips_legitimize_move (machine_mode mode, rtx dest, rtx src)
3490 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3492 mips_emit_move (dest, force_reg (mode, src));
3493 return true;
3496 /* We need to deal with constants that would be legitimate
3497 immediate_operands but aren't legitimate move_operands. */
3498 if (CONSTANT_P (src) && !move_operand (src, mode))
3500 mips_legitimize_const_move (mode, dest, src);
3501 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3502 return true;
3504 return false;
3507 /* Return true if value X in context CONTEXT is a small-data address
3508 that can be rewritten as a LO_SUM. */
3510 static bool
3511 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3513 enum mips_symbol_type symbol_type;
3515 return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3516 && !mips_split_p[SYMBOL_GP_RELATIVE]
3517 && mips_symbolic_constant_p (x, context, &symbol_type)
3518 && symbol_type == SYMBOL_GP_RELATIVE);
3521 /* Return true if OP refers to small data symbols directly, not through
3522 a LO_SUM. CONTEXT is the context in which X appears. */
3524 static int
3525 mips_small_data_pattern_1 (rtx x, enum mips_symbol_context context)
3527 subrtx_var_iterator::array_type array;
3528 FOR_EACH_SUBRTX_VAR (iter, array, x, ALL)
3530 rtx x = *iter;
3532 /* Ignore things like "g" constraints in asms. We make no particular
3533 guarantee about which symbolic constants are acceptable as asm operands
3534 versus which must be forced into a GPR. */
3535 if (GET_CODE (x) == LO_SUM || GET_CODE (x) == ASM_OPERANDS)
3536 iter.skip_subrtxes ();
3537 else if (MEM_P (x))
3539 if (mips_small_data_pattern_1 (XEXP (x, 0), SYMBOL_CONTEXT_MEM))
3540 return true;
3541 iter.skip_subrtxes ();
3543 else if (mips_rewrite_small_data_p (x, context))
3544 return true;
3546 return false;
3549 /* Return true if OP refers to small data symbols directly, not through
3550 a LO_SUM. */
3552 bool
3553 mips_small_data_pattern_p (rtx op)
3555 return mips_small_data_pattern_1 (op, SYMBOL_CONTEXT_LEA);
3558 /* Rewrite *LOC so that it refers to small data using explicit
3559 relocations. CONTEXT is the context in which *LOC appears. */
3561 static void
3562 mips_rewrite_small_data_1 (rtx *loc, enum mips_symbol_context context)
3564 subrtx_ptr_iterator::array_type array;
3565 FOR_EACH_SUBRTX_PTR (iter, array, loc, ALL)
3567 rtx *loc = *iter;
3568 if (MEM_P (*loc))
3570 mips_rewrite_small_data_1 (&XEXP (*loc, 0), SYMBOL_CONTEXT_MEM);
3571 iter.skip_subrtxes ();
3573 else if (mips_rewrite_small_data_p (*loc, context))
3575 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3576 iter.skip_subrtxes ();
3578 else if (GET_CODE (*loc) == LO_SUM)
3579 iter.skip_subrtxes ();
3583 /* Rewrite instruction pattern PATTERN so that it refers to small data
3584 using explicit relocations. */
3587 mips_rewrite_small_data (rtx pattern)
3589 pattern = copy_insn (pattern);
3590 mips_rewrite_small_data_1 (&pattern, SYMBOL_CONTEXT_LEA);
3591 return pattern;
3594 /* The cost of loading values from the constant pool. It should be
3595 larger than the cost of any constant we want to synthesize inline. */
3596 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3598 /* Return the cost of X when used as an operand to the MIPS16 instruction
3599 that implements CODE. Return -1 if there is no such instruction, or if
3600 X is not a valid immediate operand for it. */
3602 static int
3603 mips16_constant_cost (int code, HOST_WIDE_INT x)
3605 switch (code)
3607 case ASHIFT:
3608 case ASHIFTRT:
3609 case LSHIFTRT:
3610 /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3611 other shifts are extended. The shift patterns truncate the shift
3612 count to the right size, so there are no out-of-range values. */
3613 if (IN_RANGE (x, 1, 8))
3614 return 0;
3615 return COSTS_N_INSNS (1);
3617 case PLUS:
3618 if (IN_RANGE (x, -128, 127))
3619 return 0;
3620 if (SMALL_OPERAND (x))
3621 return COSTS_N_INSNS (1);
3622 return -1;
3624 case LEU:
3625 /* Like LE, but reject the always-true case. */
3626 if (x == -1)
3627 return -1;
3628 case LE:
3629 /* We add 1 to the immediate and use SLT. */
3630 x += 1;
3631 case XOR:
3632 /* We can use CMPI for an xor with an unsigned 16-bit X. */
3633 case LT:
3634 case LTU:
3635 if (IN_RANGE (x, 0, 255))
3636 return 0;
3637 if (SMALL_OPERAND_UNSIGNED (x))
3638 return COSTS_N_INSNS (1);
3639 return -1;
3641 case EQ:
3642 case NE:
3643 /* Equality comparisons with 0 are cheap. */
3644 if (x == 0)
3645 return 0;
3646 return -1;
3648 default:
3649 return -1;
3653 /* Return true if there is a non-MIPS16 instruction that implements CODE
3654 and if that instruction accepts X as an immediate operand. */
3656 static int
3657 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3659 switch (code)
3661 case ASHIFT:
3662 case ASHIFTRT:
3663 case LSHIFTRT:
3664 /* All shift counts are truncated to a valid constant. */
3665 return true;
3667 case ROTATE:
3668 case ROTATERT:
3669 /* Likewise rotates, if the target supports rotates at all. */
3670 return ISA_HAS_ROR;
3672 case AND:
3673 case IOR:
3674 case XOR:
3675 /* These instructions take 16-bit unsigned immediates. */
3676 return SMALL_OPERAND_UNSIGNED (x);
3678 case PLUS:
3679 case LT:
3680 case LTU:
3681 /* These instructions take 16-bit signed immediates. */
3682 return SMALL_OPERAND (x);
3684 case EQ:
3685 case NE:
3686 case GT:
3687 case GTU:
3688 /* The "immediate" forms of these instructions are really
3689 implemented as comparisons with register 0. */
3690 return x == 0;
3692 case GE:
3693 case GEU:
3694 /* Likewise, meaning that the only valid immediate operand is 1. */
3695 return x == 1;
3697 case LE:
3698 /* We add 1 to the immediate and use SLT. */
3699 return SMALL_OPERAND (x + 1);
3701 case LEU:
3702 /* Likewise SLTU, but reject the always-true case. */
3703 return SMALL_OPERAND (x + 1) && x + 1 != 0;
3705 case SIGN_EXTRACT:
3706 case ZERO_EXTRACT:
3707 /* The bit position and size are immediate operands. */
3708 return ISA_HAS_EXT_INS;
3710 default:
3711 /* By default assume that $0 can be used for 0. */
3712 return x == 0;
3716 /* Return the cost of binary operation X, given that the instruction
3717 sequence for a word-sized or smaller operation has cost SINGLE_COST
3718 and that the sequence of a double-word operation has cost DOUBLE_COST.
3719 If SPEED is true, optimize for speed otherwise optimize for size. */
3721 static int
3722 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3724 int cost;
3726 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3727 cost = double_cost;
3728 else
3729 cost = single_cost;
3730 return (cost
3731 + set_src_cost (XEXP (x, 0), speed)
3732 + rtx_cost (XEXP (x, 1), GET_CODE (x), 1, speed));
3735 /* Return the cost of floating-point multiplications of mode MODE. */
3737 static int
3738 mips_fp_mult_cost (machine_mode mode)
3740 return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3743 /* Return the cost of floating-point divisions of mode MODE. */
3745 static int
3746 mips_fp_div_cost (machine_mode mode)
3748 return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3751 /* Return the cost of sign-extending OP to mode MODE, not including the
3752 cost of OP itself. */
3754 static int
3755 mips_sign_extend_cost (machine_mode mode, rtx op)
3757 if (MEM_P (op))
3758 /* Extended loads are as cheap as unextended ones. */
3759 return 0;
3761 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3762 /* A sign extension from SImode to DImode in 64-bit mode is free. */
3763 return 0;
3765 if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3766 /* We can use SEB or SEH. */
3767 return COSTS_N_INSNS (1);
3769 /* We need to use a shift left and a shift right. */
3770 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3773 /* Return the cost of zero-extending OP to mode MODE, not including the
3774 cost of OP itself. */
3776 static int
3777 mips_zero_extend_cost (machine_mode mode, rtx op)
3779 if (MEM_P (op))
3780 /* Extended loads are as cheap as unextended ones. */
3781 return 0;
3783 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3784 /* We need a shift left by 32 bits and a shift right by 32 bits. */
3785 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3787 if (GENERATE_MIPS16E)
3788 /* We can use ZEB or ZEH. */
3789 return COSTS_N_INSNS (1);
3791 if (TARGET_MIPS16)
3792 /* We need to load 0xff or 0xffff into a register and use AND. */
3793 return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3795 /* We can use ANDI. */
3796 return COSTS_N_INSNS (1);
3799 /* Return the cost of moving between two registers of mode MODE,
3800 assuming that the move will be in pieces of at most UNITS bytes. */
3802 static int
3803 mips_set_reg_reg_piece_cost (machine_mode mode, unsigned int units)
3805 return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3808 /* Return the cost of moving between two registers of mode MODE. */
3810 static int
3811 mips_set_reg_reg_cost (machine_mode mode)
3813 switch (GET_MODE_CLASS (mode))
3815 case MODE_CC:
3816 return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3818 case MODE_FLOAT:
3819 case MODE_COMPLEX_FLOAT:
3820 case MODE_VECTOR_FLOAT:
3821 if (TARGET_HARD_FLOAT)
3822 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3823 /* Fall through */
3825 default:
3826 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3830 /* Implement TARGET_RTX_COSTS. */
3832 static bool
3833 mips_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3834 int *total, bool speed)
3836 machine_mode mode = GET_MODE (x);
3837 bool float_mode_p = FLOAT_MODE_P (mode);
3838 int cost;
3839 rtx addr;
3841 /* The cost of a COMPARE is hard to define for MIPS. COMPAREs don't
3842 appear in the instruction stream, and the cost of a comparison is
3843 really the cost of the branch or scc condition. At the time of
3844 writing, GCC only uses an explicit outer COMPARE code when optabs
3845 is testing whether a constant is expensive enough to force into a
3846 register. We want optabs to pass such constants through the MIPS
3847 expanders instead, so make all constants very cheap here. */
3848 if (outer_code == COMPARE)
3850 gcc_assert (CONSTANT_P (x));
3851 *total = 0;
3852 return true;
3855 switch (code)
3857 case CONST_INT:
3858 /* Treat *clear_upper32-style ANDs as having zero cost in the
3859 second operand. The cost is entirely in the first operand.
3861 ??? This is needed because we would otherwise try to CSE
3862 the constant operand. Although that's the right thing for
3863 instructions that continue to be a register operation throughout
3864 compilation, it is disastrous for instructions that could
3865 later be converted into a memory operation. */
3866 if (TARGET_64BIT
3867 && outer_code == AND
3868 && UINTVAL (x) == 0xffffffff)
3870 *total = 0;
3871 return true;
3874 if (TARGET_MIPS16)
3876 cost = mips16_constant_cost (outer_code, INTVAL (x));
3877 if (cost >= 0)
3879 *total = cost;
3880 return true;
3883 else
3885 /* When not optimizing for size, we care more about the cost
3886 of hot code, and hot code is often in a loop. If a constant
3887 operand needs to be forced into a register, we will often be
3888 able to hoist the constant load out of the loop, so the load
3889 should not contribute to the cost. */
3890 if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3892 *total = 0;
3893 return true;
3896 /* Fall through. */
3898 case CONST:
3899 case SYMBOL_REF:
3900 case LABEL_REF:
3901 case CONST_DOUBLE:
3902 if (force_to_mem_operand (x, VOIDmode))
3904 *total = COSTS_N_INSNS (1);
3905 return true;
3907 cost = mips_const_insns (x);
3908 if (cost > 0)
3910 /* If the constant is likely to be stored in a GPR, SETs of
3911 single-insn constants are as cheap as register sets; we
3912 never want to CSE them.
3914 Don't reduce the cost of storing a floating-point zero in
3915 FPRs. If we have a zero in an FPR for other reasons, we
3916 can get better cfg-cleanup and delayed-branch results by
3917 using it consistently, rather than using $0 sometimes and
3918 an FPR at other times. Also, moves between floating-point
3919 registers are sometimes cheaper than (D)MTC1 $0. */
3920 if (cost == 1
3921 && outer_code == SET
3922 && !(float_mode_p && TARGET_HARD_FLOAT))
3923 cost = 0;
3924 /* When non-MIPS16 code loads a constant N>1 times, we rarely
3925 want to CSE the constant itself. It is usually better to
3926 have N copies of the last operation in the sequence and one
3927 shared copy of the other operations. (Note that this is
3928 not true for MIPS16 code, where the final operation in the
3929 sequence is often an extended instruction.)
3931 Also, if we have a CONST_INT, we don't know whether it is
3932 for a word or doubleword operation, so we cannot rely on
3933 the result of mips_build_integer. */
3934 else if (!TARGET_MIPS16
3935 && (outer_code == SET || mode == VOIDmode))
3936 cost = 1;
3937 *total = COSTS_N_INSNS (cost);
3938 return true;
3940 /* The value will need to be fetched from the constant pool. */
3941 *total = CONSTANT_POOL_COST;
3942 return true;
3944 case MEM:
3945 /* If the address is legitimate, return the number of
3946 instructions it needs. */
3947 addr = XEXP (x, 0);
3948 cost = mips_address_insns (addr, mode, true);
3949 if (cost > 0)
3951 *total = COSTS_N_INSNS (cost + 1);
3952 return true;
3954 /* Check for a scaled indexed address. */
3955 if (mips_lwxs_address_p (addr)
3956 || mips_lx_address_p (addr, mode))
3958 *total = COSTS_N_INSNS (2);
3959 return true;
3961 /* Otherwise use the default handling. */
3962 return false;
3964 case FFS:
3965 *total = COSTS_N_INSNS (6);
3966 return false;
3968 case NOT:
3969 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3970 return false;
3972 case AND:
3973 /* Check for a *clear_upper32 pattern and treat it like a zero
3974 extension. See the pattern's comment for details. */
3975 if (TARGET_64BIT
3976 && mode == DImode
3977 && CONST_INT_P (XEXP (x, 1))
3978 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3980 *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3981 + set_src_cost (XEXP (x, 0), speed));
3982 return true;
3984 if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
3986 rtx op = XEXP (x, 0);
3987 if (GET_CODE (op) == ASHIFT
3988 && CONST_INT_P (XEXP (op, 1))
3989 && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
3991 *total = COSTS_N_INSNS (1) + set_src_cost (XEXP (op, 0), speed);
3992 return true;
3995 /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
3996 a single instruction. */
3997 if (!TARGET_MIPS16
3998 && GET_CODE (XEXP (x, 0)) == NOT
3999 && GET_CODE (XEXP (x, 1)) == NOT)
4001 cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
4002 *total = (COSTS_N_INSNS (cost)
4003 + set_src_cost (XEXP (XEXP (x, 0), 0), speed)
4004 + set_src_cost (XEXP (XEXP (x, 1), 0), speed));
4005 return true;
4008 /* Fall through. */
4010 case IOR:
4011 case XOR:
4012 /* Double-word operations use two single-word operations. */
4013 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
4014 speed);
4015 return true;
4017 case ASHIFT:
4018 case ASHIFTRT:
4019 case LSHIFTRT:
4020 case ROTATE:
4021 case ROTATERT:
4022 if (CONSTANT_P (XEXP (x, 1)))
4023 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
4024 speed);
4025 else
4026 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
4027 speed);
4028 return true;
4030 case ABS:
4031 if (float_mode_p)
4032 *total = mips_cost->fp_add;
4033 else
4034 *total = COSTS_N_INSNS (4);
4035 return false;
4037 case LO_SUM:
4038 /* Low-part immediates need an extended MIPS16 instruction. */
4039 *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
4040 + set_src_cost (XEXP (x, 0), speed));
4041 return true;
4043 case LT:
4044 case LTU:
4045 case LE:
4046 case LEU:
4047 case GT:
4048 case GTU:
4049 case GE:
4050 case GEU:
4051 case EQ:
4052 case NE:
4053 case UNORDERED:
4054 case LTGT:
4055 /* Branch comparisons have VOIDmode, so use the first operand's
4056 mode instead. */
4057 mode = GET_MODE (XEXP (x, 0));
4058 if (FLOAT_MODE_P (mode))
4060 *total = mips_cost->fp_add;
4061 return false;
4063 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
4064 speed);
4065 return true;
4067 case MINUS:
4068 if (float_mode_p
4069 && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
4070 && TARGET_FUSED_MADD
4071 && !HONOR_NANS (mode)
4072 && !HONOR_SIGNED_ZEROS (mode))
4074 /* See if we can use NMADD or NMSUB. See mips.md for the
4075 associated patterns. */
4076 rtx op0 = XEXP (x, 0);
4077 rtx op1 = XEXP (x, 1);
4078 if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
4080 *total = (mips_fp_mult_cost (mode)
4081 + set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
4082 + set_src_cost (XEXP (op0, 1), speed)
4083 + set_src_cost (op1, speed));
4084 return true;
4086 if (GET_CODE (op1) == MULT)
4088 *total = (mips_fp_mult_cost (mode)
4089 + set_src_cost (op0, speed)
4090 + set_src_cost (XEXP (op1, 0), speed)
4091 + set_src_cost (XEXP (op1, 1), speed));
4092 return true;
4095 /* Fall through. */
4097 case PLUS:
4098 if (float_mode_p)
4100 /* If this is part of a MADD or MSUB, treat the PLUS as
4101 being free. */
4102 if ((ISA_HAS_FP_MADD4_MSUB4 || ISA_HAS_FP_MADD3_MSUB3)
4103 && TARGET_FUSED_MADD
4104 && GET_CODE (XEXP (x, 0)) == MULT)
4105 *total = 0;
4106 else
4107 *total = mips_cost->fp_add;
4108 return false;
4111 /* Double-word operations require three single-word operations and
4112 an SLTU. The MIPS16 version then needs to move the result of
4113 the SLTU from $24 to a MIPS16 register. */
4114 *total = mips_binary_cost (x, COSTS_N_INSNS (1),
4115 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
4116 speed);
4117 return true;
4119 case NEG:
4120 if (float_mode_p
4121 && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
4122 && TARGET_FUSED_MADD
4123 && !HONOR_NANS (mode)
4124 && HONOR_SIGNED_ZEROS (mode))
4126 /* See if we can use NMADD or NMSUB. See mips.md for the
4127 associated patterns. */
4128 rtx op = XEXP (x, 0);
4129 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
4130 && GET_CODE (XEXP (op, 0)) == MULT)
4132 *total = (mips_fp_mult_cost (mode)
4133 + set_src_cost (XEXP (XEXP (op, 0), 0), speed)
4134 + set_src_cost (XEXP (XEXP (op, 0), 1), speed)
4135 + set_src_cost (XEXP (op, 1), speed));
4136 return true;
4140 if (float_mode_p)
4141 *total = mips_cost->fp_add;
4142 else
4143 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
4144 return false;
4146 case FMA:
4147 if (ISA_HAS_FP_MADDF_MSUBF)
4148 *total = mips_fp_mult_cost (mode);
4149 return false;
4151 case MULT:
4152 if (float_mode_p)
4153 *total = mips_fp_mult_cost (mode);
4154 else if (mode == DImode && !TARGET_64BIT)
4155 /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
4156 where the mulsidi3 always includes an MFHI and an MFLO. */
4157 *total = (speed
4158 ? mips_cost->int_mult_si * 3 + 6
4159 : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
4160 else if (!speed)
4161 *total = COSTS_N_INSNS ((ISA_HAS_MUL3 || ISA_HAS_R6MUL) ? 1 : 2) + 1;
4162 else if (mode == DImode)
4163 *total = mips_cost->int_mult_di;
4164 else
4165 *total = mips_cost->int_mult_si;
4166 return false;
4168 case DIV:
4169 /* Check for a reciprocal. */
4170 if (float_mode_p
4171 && ISA_HAS_FP_RECIP_RSQRT (mode)
4172 && flag_unsafe_math_optimizations
4173 && XEXP (x, 0) == CONST1_RTX (mode))
4175 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
4176 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
4177 division as being free. */
4178 *total = set_src_cost (XEXP (x, 1), speed);
4179 else
4180 *total = (mips_fp_div_cost (mode)
4181 + set_src_cost (XEXP (x, 1), speed));
4182 return true;
4184 /* Fall through. */
4186 case SQRT:
4187 case MOD:
4188 if (float_mode_p)
4190 *total = mips_fp_div_cost (mode);
4191 return false;
4193 /* Fall through. */
4195 case UDIV:
4196 case UMOD:
4197 if (!speed)
4199 /* It is our responsibility to make division by a power of 2
4200 as cheap as 2 register additions if we want the division
4201 expanders to be used for such operations; see the setting
4202 of sdiv_pow2_cheap in optabs.c. Using (D)DIV for MIPS16
4203 should always produce shorter code than using
4204 expand_sdiv2_pow2. */
4205 if (TARGET_MIPS16
4206 && CONST_INT_P (XEXP (x, 1))
4207 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
4209 *total = COSTS_N_INSNS (2) + set_src_cost (XEXP (x, 0), speed);
4210 return true;
4212 *total = COSTS_N_INSNS (mips_idiv_insns ());
4214 else if (mode == DImode)
4215 *total = mips_cost->int_div_di;
4216 else
4217 *total = mips_cost->int_div_si;
4218 return false;
4220 case SIGN_EXTEND:
4221 *total = mips_sign_extend_cost (mode, XEXP (x, 0));
4222 return false;
4224 case ZERO_EXTEND:
4225 if (outer_code == SET
4226 && ISA_HAS_BADDU
4227 && (GET_CODE (XEXP (x, 0)) == TRUNCATE
4228 || GET_CODE (XEXP (x, 0)) == SUBREG)
4229 && GET_MODE (XEXP (x, 0)) == QImode
4230 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
4232 *total = set_src_cost (XEXP (XEXP (x, 0), 0), speed);
4233 return true;
4235 *total = mips_zero_extend_cost (mode, XEXP (x, 0));
4236 return false;
4237 case TRUNCATE:
4238 /* Costings for highpart multiplies. Matching patterns of the form:
4240 (lshiftrt:DI (mult:DI (sign_extend:DI (...)
4241 (sign_extend:DI (...))
4242 (const_int 32)
4244 if (ISA_HAS_R6MUL
4245 && (GET_CODE (XEXP (x, 0)) == ASHIFTRT
4246 || GET_CODE (XEXP (x, 0)) == LSHIFTRT)
4247 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4248 && ((INTVAL (XEXP (XEXP (x, 0), 1)) == 32
4249 && GET_MODE (XEXP (x, 0)) == DImode)
4250 || (ISA_HAS_R6DMUL
4251 && INTVAL (XEXP (XEXP (x, 0), 1)) == 64
4252 && GET_MODE (XEXP (x, 0)) == TImode))
4253 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4254 && ((GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND
4255 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SIGN_EXTEND)
4256 || (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4257 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1))
4258 == ZERO_EXTEND))))
4260 if (!speed)
4261 *total = COSTS_N_INSNS (1) + 1;
4262 else if (mode == DImode)
4263 *total = mips_cost->int_mult_di;
4264 else
4265 *total = mips_cost->int_mult_si;
4267 /* Sign extension is free, zero extension costs for DImode when
4268 on a 64bit core / when DMUL is present. */
4269 for (int i = 0; i < 2; ++i)
4271 rtx op = XEXP (XEXP (XEXP (x, 0), 0), i);
4272 if (ISA_HAS_R6DMUL
4273 && GET_CODE (op) == ZERO_EXTEND
4274 && GET_MODE (op) == DImode)
4275 *total += rtx_cost (op, MULT, i, speed);
4276 else
4277 *total += rtx_cost (XEXP (op, 0), GET_CODE (op), 0, speed);
4280 return true;
4282 return false;
4284 case FLOAT:
4285 case UNSIGNED_FLOAT:
4286 case FIX:
4287 case FLOAT_EXTEND:
4288 case FLOAT_TRUNCATE:
4289 *total = mips_cost->fp_add;
4290 return false;
4292 case SET:
4293 if (register_operand (SET_DEST (x), VOIDmode)
4294 && reg_or_0_operand (SET_SRC (x), VOIDmode))
4296 *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4297 return true;
4299 return false;
4301 default:
4302 return false;
4306 /* Implement TARGET_ADDRESS_COST. */
4308 static int
4309 mips_address_cost (rtx addr, machine_mode mode,
4310 addr_space_t as ATTRIBUTE_UNUSED,
4311 bool speed ATTRIBUTE_UNUSED)
4313 return mips_address_insns (addr, mode, false);
4316 /* Information about a single instruction in a multi-instruction
4317 asm sequence. */
4318 struct mips_multi_member {
4319 /* True if this is a label, false if it is code. */
4320 bool is_label_p;
4322 /* The output_asm_insn format of the instruction. */
4323 const char *format;
4325 /* The operands to the instruction. */
4326 rtx operands[MAX_RECOG_OPERANDS];
4328 typedef struct mips_multi_member mips_multi_member;
4330 /* The instructions that make up the current multi-insn sequence. */
4331 static vec<mips_multi_member> mips_multi_members;
4333 /* How many instructions (as opposed to labels) are in the current
4334 multi-insn sequence. */
4335 static unsigned int mips_multi_num_insns;
4337 /* Start a new multi-insn sequence. */
4339 static void
4340 mips_multi_start (void)
4342 mips_multi_members.truncate (0);
4343 mips_multi_num_insns = 0;
4346 /* Add a new, uninitialized member to the current multi-insn sequence. */
4348 static struct mips_multi_member *
4349 mips_multi_add (void)
4351 mips_multi_member empty;
4352 return mips_multi_members.safe_push (empty);
4355 /* Add a normal insn with the given asm format to the current multi-insn
4356 sequence. The other arguments are a null-terminated list of operands. */
4358 static void
4359 mips_multi_add_insn (const char *format, ...)
4361 struct mips_multi_member *member;
4362 va_list ap;
4363 unsigned int i;
4364 rtx op;
4366 member = mips_multi_add ();
4367 member->is_label_p = false;
4368 member->format = format;
4369 va_start (ap, format);
4370 i = 0;
4371 while ((op = va_arg (ap, rtx)))
4372 member->operands[i++] = op;
4373 va_end (ap);
4374 mips_multi_num_insns++;
4377 /* Add the given label definition to the current multi-insn sequence.
4378 The definition should include the colon. */
4380 static void
4381 mips_multi_add_label (const char *label)
4383 struct mips_multi_member *member;
4385 member = mips_multi_add ();
4386 member->is_label_p = true;
4387 member->format = label;
4390 /* Return the index of the last member of the current multi-insn sequence. */
4392 static unsigned int
4393 mips_multi_last_index (void)
4395 return mips_multi_members.length () - 1;
4398 /* Add a copy of an existing instruction to the current multi-insn
4399 sequence. I is the index of the instruction that should be copied. */
4401 static void
4402 mips_multi_copy_insn (unsigned int i)
4404 struct mips_multi_member *member;
4406 member = mips_multi_add ();
4407 memcpy (member, &mips_multi_members[i], sizeof (*member));
4408 gcc_assert (!member->is_label_p);
4411 /* Change the operand of an existing instruction in the current
4412 multi-insn sequence. I is the index of the instruction,
4413 OP is the index of the operand, and X is the new value. */
4415 static void
4416 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4418 mips_multi_members[i].operands[op] = x;
4421 /* Write out the asm code for the current multi-insn sequence. */
4423 static void
4424 mips_multi_write (void)
4426 struct mips_multi_member *member;
4427 unsigned int i;
4429 FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4430 if (member->is_label_p)
4431 fprintf (asm_out_file, "%s\n", member->format);
4432 else
4433 output_asm_insn (member->format, member->operands);
4436 /* Return one word of double-word value OP, taking into account the fixed
4437 endianness of certain registers. HIGH_P is true to select the high part,
4438 false to select the low part. */
4441 mips_subword (rtx op, bool high_p)
4443 unsigned int byte, offset;
4444 machine_mode mode;
4446 mode = GET_MODE (op);
4447 if (mode == VOIDmode)
4448 mode = TARGET_64BIT ? TImode : DImode;
4450 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4451 byte = UNITS_PER_WORD;
4452 else
4453 byte = 0;
4455 if (FP_REG_RTX_P (op))
4457 /* Paired FPRs are always ordered little-endian. */
4458 offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4459 return gen_rtx_REG (word_mode, REGNO (op) + offset);
4462 if (MEM_P (op))
4463 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4465 return simplify_gen_subreg (word_mode, op, mode, byte);
4468 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4469 SPLIT_TYPE is the condition under which moves should be split. */
4471 static bool
4472 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4474 return ((split_type != SPLIT_FOR_SPEED
4475 || mips_tuning_info.fast_mult_zero_zero_p)
4476 && src == const0_rtx
4477 && REG_P (dest)
4478 && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4479 && (ISA_HAS_DSP_MULT
4480 ? ACC_REG_P (REGNO (dest))
4481 : MD_REG_P (REGNO (dest))));
4484 /* Return true if a move from SRC to DEST should be split into two.
4485 SPLIT_TYPE describes the split condition. */
4487 bool
4488 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4490 /* Check whether the move can be done using some variant of MULT $0,$0. */
4491 if (mips_mult_move_p (dest, src, split_type))
4492 return false;
4494 /* FPR-to-FPR moves can be done in a single instruction, if they're
4495 allowed at all. */
4496 unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4497 if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4498 return false;
4500 /* Check for floating-point loads and stores. */
4501 if (size == 8 && ISA_HAS_LDC1_SDC1)
4503 if (FP_REG_RTX_P (dest) && MEM_P (src))
4504 return false;
4505 if (FP_REG_RTX_P (src) && MEM_P (dest))
4506 return false;
4509 /* Otherwise split all multiword moves. */
4510 return size > UNITS_PER_WORD;
4513 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
4514 SPLIT_TYPE describes the split condition. */
4516 void
4517 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type)
4519 rtx low_dest;
4521 gcc_checking_assert (mips_split_move_p (dest, src, split_type));
4522 if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4524 if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4525 emit_insn (gen_move_doubleword_fprdi (dest, src));
4526 else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4527 emit_insn (gen_move_doubleword_fprdf (dest, src));
4528 else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4529 emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4530 else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4531 emit_insn (gen_move_doubleword_fprv2si (dest, src));
4532 else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4533 emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4534 else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4535 emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4536 else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4537 emit_insn (gen_move_doubleword_fprtf (dest, src));
4538 else
4539 gcc_unreachable ();
4541 else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4543 low_dest = mips_subword (dest, false);
4544 mips_emit_move (low_dest, mips_subword (src, false));
4545 if (TARGET_64BIT)
4546 emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4547 else
4548 emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4550 else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4552 mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4553 if (TARGET_64BIT)
4554 emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4555 else
4556 emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4558 else
4560 /* The operation can be split into two normal moves. Decide in
4561 which order to do them. */
4562 low_dest = mips_subword (dest, false);
4563 if (REG_P (low_dest)
4564 && reg_overlap_mentioned_p (low_dest, src))
4566 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4567 mips_emit_move (low_dest, mips_subword (src, false));
4569 else
4571 mips_emit_move (low_dest, mips_subword (src, false));
4572 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4577 /* Return the split type for instruction INSN. */
4579 static enum mips_split_type
4580 mips_insn_split_type (rtx insn)
4582 basic_block bb = BLOCK_FOR_INSN (insn);
4583 if (bb)
4585 if (optimize_bb_for_speed_p (bb))
4586 return SPLIT_FOR_SPEED;
4587 else
4588 return SPLIT_FOR_SIZE;
4590 /* Once CFG information has been removed, we should trust the optimization
4591 decisions made by previous passes and only split where necessary. */
4592 return SPLIT_IF_NECESSARY;
4595 /* Return true if a move from SRC to DEST in INSN should be split. */
4597 bool
4598 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
4600 return mips_split_move_p (dest, src, mips_insn_split_type (insn));
4603 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
4604 holds. */
4606 void
4607 mips_split_move_insn (rtx dest, rtx src, rtx insn)
4609 mips_split_move (dest, src, mips_insn_split_type (insn));
4612 /* Return the appropriate instructions to move SRC into DEST. Assume
4613 that SRC is operand 1 and DEST is operand 0. */
4615 const char *
4616 mips_output_move (rtx dest, rtx src)
4618 enum rtx_code dest_code, src_code;
4619 machine_mode mode;
4620 enum mips_symbol_type symbol_type;
4621 bool dbl_p;
4623 dest_code = GET_CODE (dest);
4624 src_code = GET_CODE (src);
4625 mode = GET_MODE (dest);
4626 dbl_p = (GET_MODE_SIZE (mode) == 8);
4628 if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
4629 return "#";
4631 if ((src_code == REG && GP_REG_P (REGNO (src)))
4632 || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4634 if (dest_code == REG)
4636 if (GP_REG_P (REGNO (dest)))
4637 return "move\t%0,%z1";
4639 if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
4641 if (ISA_HAS_DSP_MULT)
4642 return "mult\t%q0,%.,%.";
4643 else
4644 return "mult\t%.,%.";
4647 /* Moves to HI are handled by special .md insns. */
4648 if (REGNO (dest) == LO_REGNUM)
4649 return "mtlo\t%z1";
4651 if (DSP_ACC_REG_P (REGNO (dest)))
4653 static char retval[] = "mt__\t%z1,%q0";
4655 retval[2] = reg_names[REGNO (dest)][4];
4656 retval[3] = reg_names[REGNO (dest)][5];
4657 return retval;
4660 if (FP_REG_P (REGNO (dest)))
4661 return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4663 if (ALL_COP_REG_P (REGNO (dest)))
4665 static char retval[] = "dmtc_\t%z1,%0";
4667 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4668 return dbl_p ? retval : retval + 1;
4671 if (dest_code == MEM)
4672 switch (GET_MODE_SIZE (mode))
4674 case 1: return "sb\t%z1,%0";
4675 case 2: return "sh\t%z1,%0";
4676 case 4: return "sw\t%z1,%0";
4677 case 8: return "sd\t%z1,%0";
4680 if (dest_code == REG && GP_REG_P (REGNO (dest)))
4682 if (src_code == REG)
4684 /* Moves from HI are handled by special .md insns. */
4685 if (REGNO (src) == LO_REGNUM)
4687 /* When generating VR4120 or VR4130 code, we use MACC and
4688 DMACC instead of MFLO. This avoids both the normal
4689 MIPS III HI/LO hazards and the errata related to
4690 -mfix-vr4130. */
4691 if (ISA_HAS_MACCHI)
4692 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4693 return "mflo\t%0";
4696 if (DSP_ACC_REG_P (REGNO (src)))
4698 static char retval[] = "mf__\t%0,%q1";
4700 retval[2] = reg_names[REGNO (src)][4];
4701 retval[3] = reg_names[REGNO (src)][5];
4702 return retval;
4705 if (FP_REG_P (REGNO (src)))
4706 return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4708 if (ALL_COP_REG_P (REGNO (src)))
4710 static char retval[] = "dmfc_\t%0,%1";
4712 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4713 return dbl_p ? retval : retval + 1;
4717 if (src_code == MEM)
4718 switch (GET_MODE_SIZE (mode))
4720 case 1: return "lbu\t%0,%1";
4721 case 2: return "lhu\t%0,%1";
4722 case 4: return "lw\t%0,%1";
4723 case 8: return "ld\t%0,%1";
4726 if (src_code == CONST_INT)
4728 /* Don't use the X format for the operand itself, because that
4729 will give out-of-range numbers for 64-bit hosts and 32-bit
4730 targets. */
4731 if (!TARGET_MIPS16)
4732 return "li\t%0,%1\t\t\t# %X1";
4734 if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4735 return "li\t%0,%1";
4737 if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4738 return "#";
4741 if (src_code == HIGH)
4742 return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4744 if (CONST_GP_P (src))
4745 return "move\t%0,%1";
4747 if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4748 && mips_lo_relocs[symbol_type] != 0)
4750 /* A signed 16-bit constant formed by applying a relocation
4751 operator to a symbolic address. */
4752 gcc_assert (!mips_split_p[symbol_type]);
4753 return "li\t%0,%R1";
4756 if (symbolic_operand (src, VOIDmode))
4758 gcc_assert (TARGET_MIPS16
4759 ? TARGET_MIPS16_TEXT_LOADS
4760 : !TARGET_EXPLICIT_RELOCS);
4761 return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4764 if (src_code == REG && FP_REG_P (REGNO (src)))
4766 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4768 if (GET_MODE (dest) == V2SFmode)
4769 return "mov.ps\t%0,%1";
4770 else
4771 return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4774 if (dest_code == MEM)
4775 return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4777 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4779 if (src_code == MEM)
4780 return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4782 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4784 static char retval[] = "l_c_\t%0,%1";
4786 retval[1] = (dbl_p ? 'd' : 'w');
4787 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4788 return retval;
4790 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4792 static char retval[] = "s_c_\t%1,%0";
4794 retval[1] = (dbl_p ? 'd' : 'w');
4795 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4796 return retval;
4798 gcc_unreachable ();
4801 /* Return true if CMP1 is a suitable second operand for integer ordering
4802 test CODE. See also the *sCC patterns in mips.md. */
4804 static bool
4805 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4807 switch (code)
4809 case GT:
4810 case GTU:
4811 return reg_or_0_operand (cmp1, VOIDmode);
4813 case GE:
4814 case GEU:
4815 return !TARGET_MIPS16 && cmp1 == const1_rtx;
4817 case LT:
4818 case LTU:
4819 return arith_operand (cmp1, VOIDmode);
4821 case LE:
4822 return sle_operand (cmp1, VOIDmode);
4824 case LEU:
4825 return sleu_operand (cmp1, VOIDmode);
4827 default:
4828 gcc_unreachable ();
4832 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4833 integer ordering test *CODE, or if an equivalent combination can
4834 be formed by adjusting *CODE and *CMP1. When returning true, update
4835 *CODE and *CMP1 with the chosen code and operand, otherwise leave
4836 them alone. */
4838 static bool
4839 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4840 machine_mode mode)
4842 HOST_WIDE_INT plus_one;
4844 if (mips_int_order_operand_ok_p (*code, *cmp1))
4845 return true;
4847 if (CONST_INT_P (*cmp1))
4848 switch (*code)
4850 case LE:
4851 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4852 if (INTVAL (*cmp1) < plus_one)
4854 *code = LT;
4855 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4856 return true;
4858 break;
4860 case LEU:
4861 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4862 if (plus_one != 0)
4864 *code = LTU;
4865 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4866 return true;
4868 break;
4870 default:
4871 break;
4873 return false;
4876 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4877 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
4878 is nonnull, it's OK to set TARGET to the inverse of the result and
4879 flip *INVERT_PTR instead. */
4881 static void
4882 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4883 rtx target, rtx cmp0, rtx cmp1)
4885 machine_mode mode;
4887 /* First see if there is a MIPS instruction that can do this operation.
4888 If not, try doing the same for the inverse operation. If that also
4889 fails, force CMP1 into a register and try again. */
4890 mode = GET_MODE (cmp0);
4891 if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4892 mips_emit_binary (code, target, cmp0, cmp1);
4893 else
4895 enum rtx_code inv_code = reverse_condition (code);
4896 if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4898 cmp1 = force_reg (mode, cmp1);
4899 mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4901 else if (invert_ptr == 0)
4903 rtx inv_target;
4905 inv_target = mips_force_binary (GET_MODE (target),
4906 inv_code, cmp0, cmp1);
4907 mips_emit_binary (XOR, target, inv_target, const1_rtx);
4909 else
4911 *invert_ptr = !*invert_ptr;
4912 mips_emit_binary (inv_code, target, cmp0, cmp1);
4917 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4918 The register will have the same mode as CMP0. */
4920 static rtx
4921 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4923 if (cmp1 == const0_rtx)
4924 return cmp0;
4926 if (uns_arith_operand (cmp1, VOIDmode))
4927 return expand_binop (GET_MODE (cmp0), xor_optab,
4928 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4930 return expand_binop (GET_MODE (cmp0), sub_optab,
4931 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4934 /* Convert *CODE into a code that can be used in a floating-point
4935 scc instruction (C.cond.fmt). Return true if the values of
4936 the condition code registers will be inverted, with 0 indicating
4937 that the condition holds. */
4939 static bool
4940 mips_reversed_fp_cond (enum rtx_code *code)
4942 switch (*code)
4944 case NE:
4945 case LTGT:
4946 case ORDERED:
4947 *code = reverse_condition_maybe_unordered (*code);
4948 return true;
4950 default:
4951 return false;
4955 /* Allocate a floating-point condition-code register of mode MODE.
4957 These condition code registers are used for certain kinds
4958 of compound operation, such as compare and branches, vconds,
4959 and built-in functions. At expand time, their use is entirely
4960 controlled by MIPS-specific code and is entirely internal
4961 to these compound operations.
4963 We could (and did in the past) expose condition-code values
4964 as pseudo registers and leave the register allocator to pick
4965 appropriate registers. The problem is that it is not practically
4966 possible for the rtl optimizers to guarantee that no spills will
4967 be needed, even when AVOID_CCMODE_COPIES is defined. We would
4968 therefore need spill and reload sequences to handle the worst case.
4970 Although such sequences do exist, they are very expensive and are
4971 not something we'd want to use. This is especially true of CCV2 and
4972 CCV4, where all the shuffling would greatly outweigh whatever benefit
4973 the vectorization itself provides.
4975 The main benefit of having more than one condition-code register
4976 is to allow the pipelining of operations, especially those involving
4977 comparisons and conditional moves. We don't really expect the
4978 registers to be live for long periods, and certainly never want
4979 them to be live across calls.
4981 Also, there should be no penalty attached to using all the available
4982 registers. They are simply bits in the same underlying FPU control
4983 register.
4985 We therefore expose the hardware registers from the outset and use
4986 a simple round-robin allocation scheme. */
4988 static rtx
4989 mips_allocate_fcc (machine_mode mode)
4991 unsigned int regno, count;
4993 gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
4995 if (mode == CCmode)
4996 count = 1;
4997 else if (mode == CCV2mode)
4998 count = 2;
4999 else if (mode == CCV4mode)
5000 count = 4;
5001 else
5002 gcc_unreachable ();
5004 cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
5005 if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
5006 cfun->machine->next_fcc = 0;
5007 regno = ST_REG_FIRST + cfun->machine->next_fcc;
5008 cfun->machine->next_fcc += count;
5009 return gen_rtx_REG (mode, regno);
5012 /* Convert a comparison into something that can be used in a branch or
5013 conditional move. On entry, *OP0 and *OP1 are the values being
5014 compared and *CODE is the code used to compare them.
5016 Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
5017 If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
5018 otherwise any standard branch condition can be used. The standard branch
5019 conditions are:
5021 - EQ or NE between two registers.
5022 - any comparison between a register and zero. */
5024 static void
5025 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
5027 rtx cmp_op0 = *op0;
5028 rtx cmp_op1 = *op1;
5030 if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
5032 if (!need_eq_ne_p && *op1 == const0_rtx)
5034 else if (*code == EQ || *code == NE)
5036 if (need_eq_ne_p)
5038 *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
5039 *op1 = const0_rtx;
5041 else
5042 *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
5044 else
5046 /* The comparison needs a separate scc instruction. Store the
5047 result of the scc in *OP0 and compare it against zero. */
5048 bool invert = false;
5049 *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
5050 mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
5051 *code = (invert ? EQ : NE);
5052 *op1 = const0_rtx;
5055 else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
5057 *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
5058 mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
5059 *code = NE;
5060 *op1 = const0_rtx;
5062 else
5064 enum rtx_code cmp_code;
5066 /* Floating-point tests use a separate C.cond.fmt or CMP.cond.fmt
5067 comparison to set a register. The branch or conditional move will
5068 then compare that register against zero.
5070 Set CMP_CODE to the code of the comparison instruction and
5071 *CODE to the code that the branch or move should use. */
5072 cmp_code = *code;
5073 if (ISA_HAS_CCF)
5075 /* All FP conditions can be implemented directly with CMP.cond.fmt
5076 or by reversing the operands. */
5077 *code = NE;
5078 *op0 = gen_reg_rtx (CCFmode);
5080 else
5082 /* Three FP conditions cannot be implemented by reversing the
5083 operands for C.cond.fmt, instead a reversed condition code is
5084 required and a test for false. */
5085 *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
5086 if (ISA_HAS_8CC)
5087 *op0 = mips_allocate_fcc (CCmode);
5088 else
5089 *op0 = gen_rtx_REG (CCmode, FPSW_REGNUM);
5092 *op1 = const0_rtx;
5093 mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
5097 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
5098 and OPERAND[3]. Store the result in OPERANDS[0].
5100 On 64-bit targets, the mode of the comparison and target will always be
5101 SImode, thus possibly narrower than that of the comparison's operands. */
5103 void
5104 mips_expand_scc (rtx operands[])
5106 rtx target = operands[0];
5107 enum rtx_code code = GET_CODE (operands[1]);
5108 rtx op0 = operands[2];
5109 rtx op1 = operands[3];
5111 gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
5113 if (code == EQ || code == NE)
5115 if (ISA_HAS_SEQ_SNE
5116 && reg_imm10_operand (op1, GET_MODE (op1)))
5117 mips_emit_binary (code, target, op0, op1);
5118 else
5120 rtx zie = mips_zero_if_equal (op0, op1);
5121 mips_emit_binary (code, target, zie, const0_rtx);
5124 else
5125 mips_emit_int_order_test (code, 0, target, op0, op1);
5128 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
5129 CODE and jump to OPERANDS[3] if the condition holds. */
5131 void
5132 mips_expand_conditional_branch (rtx *operands)
5134 enum rtx_code code = GET_CODE (operands[0]);
5135 rtx op0 = operands[1];
5136 rtx op1 = operands[2];
5137 rtx condition;
5139 mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
5140 condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
5141 emit_jump_insn (gen_condjump (condition, operands[3]));
5144 /* Implement:
5146 (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
5147 (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS)) */
5149 void
5150 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
5151 enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
5153 rtx cmp_result;
5154 bool reversed_p;
5156 reversed_p = mips_reversed_fp_cond (&cond);
5157 cmp_result = mips_allocate_fcc (CCV2mode);
5158 emit_insn (gen_scc_ps (cmp_result,
5159 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
5160 if (reversed_p)
5161 emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
5162 cmp_result));
5163 else
5164 emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
5165 cmp_result));
5168 /* Perform the comparison in OPERANDS[1]. Move OPERANDS[2] into OPERANDS[0]
5169 if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0]. */
5171 void
5172 mips_expand_conditional_move (rtx *operands)
5174 rtx cond;
5175 enum rtx_code code = GET_CODE (operands[1]);
5176 rtx op0 = XEXP (operands[1], 0);
5177 rtx op1 = XEXP (operands[1], 1);
5179 mips_emit_compare (&code, &op0, &op1, true);
5180 cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
5182 /* There is no direct support for general conditional GP move involving
5183 two registers using SEL. */
5184 if (ISA_HAS_SEL
5185 && INTEGRAL_MODE_P (GET_MODE (operands[2]))
5186 && register_operand (operands[2], VOIDmode)
5187 && register_operand (operands[3], VOIDmode))
5189 machine_mode mode = GET_MODE (operands[0]);
5190 rtx temp = gen_reg_rtx (mode);
5191 rtx temp2 = gen_reg_rtx (mode);
5193 emit_insn (gen_rtx_SET (VOIDmode, temp,
5194 gen_rtx_IF_THEN_ELSE (mode, cond,
5195 operands[2], const0_rtx)));
5197 /* Flip the test for the second operand. */
5198 cond = gen_rtx_fmt_ee ((code == EQ) ? NE : EQ, GET_MODE (op0), op0, op1);
5200 emit_insn (gen_rtx_SET (VOIDmode, temp2,
5201 gen_rtx_IF_THEN_ELSE (mode, cond,
5202 operands[3], const0_rtx)));
5204 /* Merge the two results, at least one is guaranteed to be zero. */
5205 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
5206 gen_rtx_IOR (mode, temp, temp2)));
5208 else
5210 if (FLOAT_MODE_P (GET_MODE (operands[2])) && !ISA_HAS_SEL)
5212 operands[2] = force_reg (GET_MODE (operands[0]), operands[2]);
5213 operands[3] = force_reg (GET_MODE (operands[0]), operands[3]);
5216 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
5217 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
5218 operands[2], operands[3])));
5222 /* Perform the comparison in COMPARISON, then trap if the condition holds. */
5224 void
5225 mips_expand_conditional_trap (rtx comparison)
5227 rtx op0, op1;
5228 machine_mode mode;
5229 enum rtx_code code;
5231 /* MIPS conditional trap instructions don't have GT or LE flavors,
5232 so we must swap the operands and convert to LT and GE respectively. */
5233 code = GET_CODE (comparison);
5234 switch (code)
5236 case GT:
5237 case LE:
5238 case GTU:
5239 case LEU:
5240 code = swap_condition (code);
5241 op0 = XEXP (comparison, 1);
5242 op1 = XEXP (comparison, 0);
5243 break;
5245 default:
5246 op0 = XEXP (comparison, 0);
5247 op1 = XEXP (comparison, 1);
5248 break;
5251 mode = GET_MODE (XEXP (comparison, 0));
5252 op0 = force_reg (mode, op0);
5253 if (!(ISA_HAS_COND_TRAPI
5254 ? arith_operand (op1, mode)
5255 : reg_or_0_operand (op1, mode)))
5256 op1 = force_reg (mode, op1);
5258 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
5259 gen_rtx_fmt_ee (code, mode, op0, op1),
5260 const0_rtx));
5263 /* Initialize *CUM for a call to a function of type FNTYPE. */
5265 void
5266 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
5268 memset (cum, 0, sizeof (*cum));
5269 cum->prototype = (fntype && prototype_p (fntype));
5270 cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
5273 /* Fill INFO with information about a single argument. CUM is the
5274 cumulative state for earlier arguments. MODE is the mode of this
5275 argument and TYPE is its type (if known). NAMED is true if this
5276 is a named (fixed) argument rather than a variable one. */
5278 static void
5279 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
5280 machine_mode mode, const_tree type, bool named)
5282 bool doubleword_aligned_p;
5283 unsigned int num_bytes, num_words, max_regs;
5285 /* Work out the size of the argument. */
5286 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5287 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5289 /* Decide whether it should go in a floating-point register, assuming
5290 one is free. Later code checks for availability.
5292 The checks against UNITS_PER_FPVALUE handle the soft-float and
5293 single-float cases. */
5294 switch (mips_abi)
5296 case ABI_EABI:
5297 /* The EABI conventions have traditionally been defined in terms
5298 of TYPE_MODE, regardless of the actual type. */
5299 info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
5300 || mode == V2SFmode)
5301 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5302 break;
5304 case ABI_32:
5305 case ABI_O64:
5306 /* Only leading floating-point scalars are passed in
5307 floating-point registers. We also handle vector floats the same
5308 say, which is OK because they are not covered by the standard ABI. */
5309 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
5310 info->fpr_p = (!cum->gp_reg_found
5311 && cum->arg_number < 2
5312 && (type == 0
5313 || SCALAR_FLOAT_TYPE_P (type)
5314 || VECTOR_FLOAT_TYPE_P (type))
5315 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5316 || mode == V2SFmode)
5317 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5318 break;
5320 case ABI_N32:
5321 case ABI_64:
5322 /* Scalar, complex and vector floating-point types are passed in
5323 floating-point registers, as long as this is a named rather
5324 than a variable argument. */
5325 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
5326 info->fpr_p = (named
5327 && (type == 0 || FLOAT_TYPE_P (type))
5328 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5329 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5330 || mode == V2SFmode)
5331 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
5333 /* ??? According to the ABI documentation, the real and imaginary
5334 parts of complex floats should be passed in individual registers.
5335 The real and imaginary parts of stack arguments are supposed
5336 to be contiguous and there should be an extra word of padding
5337 at the end.
5339 This has two problems. First, it makes it impossible to use a
5340 single "void *" va_list type, since register and stack arguments
5341 are passed differently. (At the time of writing, MIPSpro cannot
5342 handle complex float varargs correctly.) Second, it's unclear
5343 what should happen when there is only one register free.
5345 For now, we assume that named complex floats should go into FPRs
5346 if there are two FPRs free, otherwise they should be passed in the
5347 same way as a struct containing two floats. */
5348 if (info->fpr_p
5349 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5350 && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
5352 if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
5353 info->fpr_p = false;
5354 else
5355 num_words = 2;
5357 break;
5359 default:
5360 gcc_unreachable ();
5363 /* See whether the argument has doubleword alignment. */
5364 doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
5365 > BITS_PER_WORD);
5367 /* Set REG_OFFSET to the register count we're interested in.
5368 The EABI allocates the floating-point registers separately,
5369 but the other ABIs allocate them like integer registers. */
5370 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
5371 ? cum->num_fprs
5372 : cum->num_gprs);
5374 /* Advance to an even register if the argument is doubleword-aligned. */
5375 if (doubleword_aligned_p)
5376 info->reg_offset += info->reg_offset & 1;
5378 /* Work out the offset of a stack argument. */
5379 info->stack_offset = cum->stack_words;
5380 if (doubleword_aligned_p)
5381 info->stack_offset += info->stack_offset & 1;
5383 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
5385 /* Partition the argument between registers and stack. */
5386 info->reg_words = MIN (num_words, max_regs);
5387 info->stack_words = num_words - info->reg_words;
5390 /* INFO describes a register argument that has the normal format for the
5391 argument's mode. Return the register it uses, assuming that FPRs are
5392 available if HARD_FLOAT_P. */
5394 static unsigned int
5395 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
5397 if (!info->fpr_p || !hard_float_p)
5398 return GP_ARG_FIRST + info->reg_offset;
5399 else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
5400 /* In o32, the second argument is always passed in $f14
5401 for TARGET_DOUBLE_FLOAT, regardless of whether the
5402 first argument was a word or doubleword. */
5403 return FP_ARG_FIRST + 2;
5404 else
5405 return FP_ARG_FIRST + info->reg_offset;
5408 /* Implement TARGET_STRICT_ARGUMENT_NAMING. */
5410 static bool
5411 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
5413 return !TARGET_OLDABI;
5416 /* Implement TARGET_FUNCTION_ARG. */
5418 static rtx
5419 mips_function_arg (cumulative_args_t cum_v, machine_mode mode,
5420 const_tree type, bool named)
5422 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5423 struct mips_arg_info info;
5425 /* We will be called with a mode of VOIDmode after the last argument
5426 has been seen. Whatever we return will be passed to the call expander.
5427 If we need a MIPS16 fp_code, return a REG with the code stored as
5428 the mode. */
5429 if (mode == VOIDmode)
5431 if (TARGET_MIPS16 && cum->fp_code != 0)
5432 return gen_rtx_REG ((machine_mode) cum->fp_code, 0);
5433 else
5434 return NULL;
5437 mips_get_arg_info (&info, cum, mode, type, named);
5439 /* Return straight away if the whole argument is passed on the stack. */
5440 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
5441 return NULL;
5443 /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
5444 contains a double in its entirety, then that 64-bit chunk is passed
5445 in a floating-point register. */
5446 if (TARGET_NEWABI
5447 && TARGET_HARD_FLOAT
5448 && named
5449 && type != 0
5450 && TREE_CODE (type) == RECORD_TYPE
5451 && TYPE_SIZE_UNIT (type)
5452 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
5454 tree field;
5456 /* First check to see if there is any such field. */
5457 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5458 if (TREE_CODE (field) == FIELD_DECL
5459 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5460 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
5461 && tree_fits_shwi_p (bit_position (field))
5462 && int_bit_position (field) % BITS_PER_WORD == 0)
5463 break;
5465 if (field != 0)
5467 /* Now handle the special case by returning a PARALLEL
5468 indicating where each 64-bit chunk goes. INFO.REG_WORDS
5469 chunks are passed in registers. */
5470 unsigned int i;
5471 HOST_WIDE_INT bitpos;
5472 rtx ret;
5474 /* assign_parms checks the mode of ENTRY_PARM, so we must
5475 use the actual mode here. */
5476 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
5478 bitpos = 0;
5479 field = TYPE_FIELDS (type);
5480 for (i = 0; i < info.reg_words; i++)
5482 rtx reg;
5484 for (; field; field = DECL_CHAIN (field))
5485 if (TREE_CODE (field) == FIELD_DECL
5486 && int_bit_position (field) >= bitpos)
5487 break;
5489 if (field
5490 && int_bit_position (field) == bitpos
5491 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5492 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
5493 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
5494 else
5495 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
5497 XVECEXP (ret, 0, i)
5498 = gen_rtx_EXPR_LIST (VOIDmode, reg,
5499 GEN_INT (bitpos / BITS_PER_UNIT));
5501 bitpos += BITS_PER_WORD;
5503 return ret;
5507 /* Handle the n32/n64 conventions for passing complex floating-point
5508 arguments in FPR pairs. The real part goes in the lower register
5509 and the imaginary part goes in the upper register. */
5510 if (TARGET_NEWABI
5511 && info.fpr_p
5512 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5514 rtx real, imag;
5515 machine_mode inner;
5516 unsigned int regno;
5518 inner = GET_MODE_INNER (mode);
5519 regno = FP_ARG_FIRST + info.reg_offset;
5520 if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
5522 /* Real part in registers, imaginary part on stack. */
5523 gcc_assert (info.stack_words == info.reg_words);
5524 return gen_rtx_REG (inner, regno);
5526 else
5528 gcc_assert (info.stack_words == 0);
5529 real = gen_rtx_EXPR_LIST (VOIDmode,
5530 gen_rtx_REG (inner, regno),
5531 const0_rtx);
5532 imag = gen_rtx_EXPR_LIST (VOIDmode,
5533 gen_rtx_REG (inner,
5534 regno + info.reg_words / 2),
5535 GEN_INT (GET_MODE_SIZE (inner)));
5536 return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
5540 return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
5543 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
5545 static void
5546 mips_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
5547 const_tree type, bool named)
5549 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5550 struct mips_arg_info info;
5552 mips_get_arg_info (&info, cum, mode, type, named);
5554 if (!info.fpr_p)
5555 cum->gp_reg_found = true;
5557 /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
5558 an explanation of what this code does. It assumes that we're using
5559 either the o32 or the o64 ABI, both of which pass at most 2 arguments
5560 in FPRs. */
5561 if (cum->arg_number < 2 && info.fpr_p)
5562 cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
5564 /* Advance the register count. This has the effect of setting
5565 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
5566 argument required us to skip the final GPR and pass the whole
5567 argument on the stack. */
5568 if (mips_abi != ABI_EABI || !info.fpr_p)
5569 cum->num_gprs = info.reg_offset + info.reg_words;
5570 else if (info.reg_words > 0)
5571 cum->num_fprs += MAX_FPRS_PER_FMT;
5573 /* Advance the stack word count. */
5574 if (info.stack_words > 0)
5575 cum->stack_words = info.stack_offset + info.stack_words;
5577 cum->arg_number++;
5580 /* Implement TARGET_ARG_PARTIAL_BYTES. */
5582 static int
5583 mips_arg_partial_bytes (cumulative_args_t cum,
5584 machine_mode mode, tree type, bool named)
5586 struct mips_arg_info info;
5588 mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
5589 return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
5592 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
5593 least PARM_BOUNDARY bits of alignment, but will be given anything up
5594 to STACK_BOUNDARY bits if the type requires it. */
5596 static unsigned int
5597 mips_function_arg_boundary (machine_mode mode, const_tree type)
5599 unsigned int alignment;
5601 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5602 if (alignment < PARM_BOUNDARY)
5603 alignment = PARM_BOUNDARY;
5604 if (alignment > STACK_BOUNDARY)
5605 alignment = STACK_BOUNDARY;
5606 return alignment;
5609 /* Implement TARGET_GET_RAW_RESULT_MODE and TARGET_GET_RAW_ARG_MODE. */
5611 static machine_mode
5612 mips_get_reg_raw_mode (int regno)
5614 if (TARGET_FLOATXX && FP_REG_P (regno))
5615 return DFmode;
5616 return default_get_reg_raw_mode (regno);
5619 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5620 upward rather than downward. In other words, return true if the
5621 first byte of the stack slot has useful data, false if the last
5622 byte does. */
5624 bool
5625 mips_pad_arg_upward (machine_mode mode, const_tree type)
5627 /* On little-endian targets, the first byte of every stack argument
5628 is passed in the first byte of the stack slot. */
5629 if (!BYTES_BIG_ENDIAN)
5630 return true;
5632 /* Otherwise, integral types are padded downward: the last byte of a
5633 stack argument is passed in the last byte of the stack slot. */
5634 if (type != 0
5635 ? (INTEGRAL_TYPE_P (type)
5636 || POINTER_TYPE_P (type)
5637 || FIXED_POINT_TYPE_P (type))
5638 : (SCALAR_INT_MODE_P (mode)
5639 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5640 return false;
5642 /* Big-endian o64 pads floating-point arguments downward. */
5643 if (mips_abi == ABI_O64)
5644 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5645 return false;
5647 /* Other types are padded upward for o32, o64, n32 and n64. */
5648 if (mips_abi != ABI_EABI)
5649 return true;
5651 /* Arguments smaller than a stack slot are padded downward. */
5652 if (mode != BLKmode)
5653 return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5654 else
5655 return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5658 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
5659 if the least significant byte of the register has useful data. Return
5660 the opposite if the most significant byte does. */
5662 bool
5663 mips_pad_reg_upward (machine_mode mode, tree type)
5665 /* No shifting is required for floating-point arguments. */
5666 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5667 return !BYTES_BIG_ENDIAN;
5669 /* Otherwise, apply the same padding to register arguments as we do
5670 to stack arguments. */
5671 return mips_pad_arg_upward (mode, type);
5674 /* Return nonzero when an argument must be passed by reference. */
5676 static bool
5677 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5678 machine_mode mode, const_tree type,
5679 bool named ATTRIBUTE_UNUSED)
5681 if (mips_abi == ABI_EABI)
5683 int size;
5685 /* ??? How should SCmode be handled? */
5686 if (mode == DImode || mode == DFmode
5687 || mode == DQmode || mode == UDQmode
5688 || mode == DAmode || mode == UDAmode)
5689 return 0;
5691 size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5692 return size == -1 || size > UNITS_PER_WORD;
5694 else
5696 /* If we have a variable-sized parameter, we have no choice. */
5697 return targetm.calls.must_pass_in_stack (mode, type);
5701 /* Implement TARGET_CALLEE_COPIES. */
5703 static bool
5704 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
5705 machine_mode mode ATTRIBUTE_UNUSED,
5706 const_tree type ATTRIBUTE_UNUSED, bool named)
5708 return mips_abi == ABI_EABI && named;
5711 /* See whether VALTYPE is a record whose fields should be returned in
5712 floating-point registers. If so, return the number of fields and
5713 list them in FIELDS (which should have two elements). Return 0
5714 otherwise.
5716 For n32 & n64, a structure with one or two fields is returned in
5717 floating-point registers as long as every field has a floating-point
5718 type. */
5720 static int
5721 mips_fpr_return_fields (const_tree valtype, tree *fields)
5723 tree field;
5724 int i;
5726 if (!TARGET_NEWABI)
5727 return 0;
5729 if (TREE_CODE (valtype) != RECORD_TYPE)
5730 return 0;
5732 i = 0;
5733 for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5735 if (TREE_CODE (field) != FIELD_DECL)
5736 continue;
5738 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5739 return 0;
5741 if (i == 2)
5742 return 0;
5744 fields[i++] = field;
5746 return i;
5749 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
5750 a value in the most significant part of $2/$3 if:
5752 - the target is big-endian;
5754 - the value has a structure or union type (we generalize this to
5755 cover aggregates from other languages too); and
5757 - the structure is not returned in floating-point registers. */
5759 static bool
5760 mips_return_in_msb (const_tree valtype)
5762 tree fields[2];
5764 return (TARGET_NEWABI
5765 && TARGET_BIG_ENDIAN
5766 && AGGREGATE_TYPE_P (valtype)
5767 && mips_fpr_return_fields (valtype, fields) == 0);
5770 /* Return true if the function return value MODE will get returned in a
5771 floating-point register. */
5773 static bool
5774 mips_return_mode_in_fpr_p (machine_mode mode)
5776 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT || mode != V2SFmode);
5777 return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5778 || mode == V2SFmode
5779 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5780 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5783 /* Return the representation of an FPR return register when the
5784 value being returned in FP_RETURN has mode VALUE_MODE and the
5785 return type itself has mode TYPE_MODE. On NewABI targets,
5786 the two modes may be different for structures like:
5788 struct __attribute__((packed)) foo { float f; }
5790 where we return the SFmode value of "f" in FP_RETURN, but where
5791 the structure itself has mode BLKmode. */
5793 static rtx
5794 mips_return_fpr_single (machine_mode type_mode,
5795 machine_mode value_mode)
5797 rtx x;
5799 x = gen_rtx_REG (value_mode, FP_RETURN);
5800 if (type_mode != value_mode)
5802 x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5803 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5805 return x;
5808 /* Return a composite value in a pair of floating-point registers.
5809 MODE1 and OFFSET1 are the mode and byte offset for the first value,
5810 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
5811 complete value.
5813 For n32 & n64, $f0 always holds the first value and $f2 the second.
5814 Otherwise the values are packed together as closely as possible. */
5816 static rtx
5817 mips_return_fpr_pair (machine_mode mode,
5818 machine_mode mode1, HOST_WIDE_INT offset1,
5819 machine_mode mode2, HOST_WIDE_INT offset2)
5821 int inc;
5823 inc = (TARGET_NEWABI || mips_abi == ABI_32 ? 2 : MAX_FPRS_PER_FMT);
5824 return gen_rtx_PARALLEL
5825 (mode,
5826 gen_rtvec (2,
5827 gen_rtx_EXPR_LIST (VOIDmode,
5828 gen_rtx_REG (mode1, FP_RETURN),
5829 GEN_INT (offset1)),
5830 gen_rtx_EXPR_LIST (VOIDmode,
5831 gen_rtx_REG (mode2, FP_RETURN + inc),
5832 GEN_INT (offset2))));
5836 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
5837 For normal calls, VALTYPE is the return type and MODE is VOIDmode.
5838 For libcalls, VALTYPE is null and MODE is the mode of the return value. */
5840 static rtx
5841 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
5842 machine_mode mode)
5844 if (valtype)
5846 tree fields[2];
5847 int unsigned_p;
5848 const_tree func;
5850 if (fn_decl_or_type && DECL_P (fn_decl_or_type))
5851 func = fn_decl_or_type;
5852 else
5853 func = NULL;
5855 mode = TYPE_MODE (valtype);
5856 unsigned_p = TYPE_UNSIGNED (valtype);
5858 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5859 return values, promote the mode here too. */
5860 mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5862 /* Handle structures whose fields are returned in $f0/$f2. */
5863 switch (mips_fpr_return_fields (valtype, fields))
5865 case 1:
5866 return mips_return_fpr_single (mode,
5867 TYPE_MODE (TREE_TYPE (fields[0])));
5869 case 2:
5870 return mips_return_fpr_pair (mode,
5871 TYPE_MODE (TREE_TYPE (fields[0])),
5872 int_byte_position (fields[0]),
5873 TYPE_MODE (TREE_TYPE (fields[1])),
5874 int_byte_position (fields[1]));
5877 /* If a value is passed in the most significant part of a register, see
5878 whether we have to round the mode up to a whole number of words. */
5879 if (mips_return_in_msb (valtype))
5881 HOST_WIDE_INT size = int_size_in_bytes (valtype);
5882 if (size % UNITS_PER_WORD != 0)
5884 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5885 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5889 /* For EABI, the class of return register depends entirely on MODE.
5890 For example, "struct { some_type x; }" and "union { some_type x; }"
5891 are returned in the same way as a bare "some_type" would be.
5892 Other ABIs only use FPRs for scalar, complex or vector types. */
5893 if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5894 return gen_rtx_REG (mode, GP_RETURN);
5897 if (!TARGET_MIPS16)
5899 /* Handle long doubles for n32 & n64. */
5900 if (mode == TFmode)
5901 return mips_return_fpr_pair (mode,
5902 DImode, 0,
5903 DImode, GET_MODE_SIZE (mode) / 2);
5905 if (mips_return_mode_in_fpr_p (mode))
5907 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5908 return mips_return_fpr_pair (mode,
5909 GET_MODE_INNER (mode), 0,
5910 GET_MODE_INNER (mode),
5911 GET_MODE_SIZE (mode) / 2);
5912 else
5913 return gen_rtx_REG (mode, FP_RETURN);
5917 return gen_rtx_REG (mode, GP_RETURN);
5920 /* Implement TARGET_FUNCTION_VALUE. */
5922 static rtx
5923 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
5924 bool outgoing ATTRIBUTE_UNUSED)
5926 return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5929 /* Implement TARGET_LIBCALL_VALUE. */
5931 static rtx
5932 mips_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5934 return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
5937 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5939 On the MIPS, R2 R3 and F0 F2 are the only register thus used. */
5941 static bool
5942 mips_function_value_regno_p (const unsigned int regno)
5944 /* Most types only require one GPR or one FPR for return values but for
5945 hard-float two FPRs can be used for _Complex types (for all ABIs)
5946 and long doubles (for n64). */
5947 if (regno == GP_RETURN
5948 || regno == FP_RETURN
5949 || (FP_RETURN != GP_RETURN
5950 && regno == FP_RETURN + 2))
5951 return true;
5953 /* For o32 FP32, _Complex double will be returned in four 32-bit registers.
5954 This does not apply to o32 FPXX as floating-point function argument and
5955 return registers are described as 64-bit even though floating-point
5956 registers are primarily described as 32-bit internally.
5957 See: mips_get_reg_raw_mode. */
5958 if ((mips_abi == ABI_32 && TARGET_FLOAT32)
5959 && FP_RETURN != GP_RETURN
5960 && (regno == FP_RETURN + 1
5961 || regno == FP_RETURN + 3))
5962 return true;
5964 return false;
5967 /* Implement TARGET_RETURN_IN_MEMORY. Under the o32 and o64 ABIs,
5968 all BLKmode objects are returned in memory. Under the n32, n64
5969 and embedded ABIs, small structures are returned in a register.
5970 Objects with varying size must still be returned in memory, of
5971 course. */
5973 static bool
5974 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5976 return (TARGET_OLDABI
5977 ? TYPE_MODE (type) == BLKmode
5978 : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5981 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
5983 static void
5984 mips_setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
5985 tree type, int *pretend_size ATTRIBUTE_UNUSED,
5986 int no_rtl)
5988 CUMULATIVE_ARGS local_cum;
5989 int gp_saved, fp_saved;
5991 /* The caller has advanced CUM up to, but not beyond, the last named
5992 argument. Advance a local copy of CUM past the last "real" named
5993 argument, to find out how many registers are left over. */
5994 local_cum = *get_cumulative_args (cum);
5995 mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
5996 true);
5998 /* Found out how many registers we need to save. */
5999 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
6000 fp_saved = (EABI_FLOAT_VARARGS_P
6001 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
6002 : 0);
6004 if (!no_rtl)
6006 if (gp_saved > 0)
6008 rtx ptr, mem;
6010 ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
6011 REG_PARM_STACK_SPACE (cfun->decl)
6012 - gp_saved * UNITS_PER_WORD);
6013 mem = gen_frame_mem (BLKmode, ptr);
6014 set_mem_alias_set (mem, get_varargs_alias_set ());
6016 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
6017 mem, gp_saved);
6019 if (fp_saved > 0)
6021 /* We can't use move_block_from_reg, because it will use
6022 the wrong mode. */
6023 machine_mode mode;
6024 int off, i;
6026 /* Set OFF to the offset from virtual_incoming_args_rtx of
6027 the first float register. The FP save area lies below
6028 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
6029 off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
6030 off -= fp_saved * UNITS_PER_FPREG;
6032 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
6034 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
6035 i += MAX_FPRS_PER_FMT)
6037 rtx ptr, mem;
6039 ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
6040 mem = gen_frame_mem (mode, ptr);
6041 set_mem_alias_set (mem, get_varargs_alias_set ());
6042 mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
6043 off += UNITS_PER_HWFPVALUE;
6047 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
6048 cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
6049 + fp_saved * UNITS_PER_FPREG);
6052 /* Implement TARGET_BUILTIN_VA_LIST. */
6054 static tree
6055 mips_build_builtin_va_list (void)
6057 if (EABI_FLOAT_VARARGS_P)
6059 /* We keep 3 pointers, and two offsets.
6061 Two pointers are to the overflow area, which starts at the CFA.
6062 One of these is constant, for addressing into the GPR save area
6063 below it. The other is advanced up the stack through the
6064 overflow region.
6066 The third pointer is to the bottom of the GPR save area.
6067 Since the FPR save area is just below it, we can address
6068 FPR slots off this pointer.
6070 We also keep two one-byte offsets, which are to be subtracted
6071 from the constant pointers to yield addresses in the GPR and
6072 FPR save areas. These are downcounted as float or non-float
6073 arguments are used, and when they get to zero, the argument
6074 must be obtained from the overflow region. */
6075 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
6076 tree array, index;
6078 record = lang_hooks.types.make_type (RECORD_TYPE);
6080 f_ovfl = build_decl (BUILTINS_LOCATION,
6081 FIELD_DECL, get_identifier ("__overflow_argptr"),
6082 ptr_type_node);
6083 f_gtop = build_decl (BUILTINS_LOCATION,
6084 FIELD_DECL, get_identifier ("__gpr_top"),
6085 ptr_type_node);
6086 f_ftop = build_decl (BUILTINS_LOCATION,
6087 FIELD_DECL, get_identifier ("__fpr_top"),
6088 ptr_type_node);
6089 f_goff = build_decl (BUILTINS_LOCATION,
6090 FIELD_DECL, get_identifier ("__gpr_offset"),
6091 unsigned_char_type_node);
6092 f_foff = build_decl (BUILTINS_LOCATION,
6093 FIELD_DECL, get_identifier ("__fpr_offset"),
6094 unsigned_char_type_node);
6095 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
6096 warn on every user file. */
6097 index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
6098 array = build_array_type (unsigned_char_type_node,
6099 build_index_type (index));
6100 f_res = build_decl (BUILTINS_LOCATION,
6101 FIELD_DECL, get_identifier ("__reserved"), array);
6103 DECL_FIELD_CONTEXT (f_ovfl) = record;
6104 DECL_FIELD_CONTEXT (f_gtop) = record;
6105 DECL_FIELD_CONTEXT (f_ftop) = record;
6106 DECL_FIELD_CONTEXT (f_goff) = record;
6107 DECL_FIELD_CONTEXT (f_foff) = record;
6108 DECL_FIELD_CONTEXT (f_res) = record;
6110 TYPE_FIELDS (record) = f_ovfl;
6111 DECL_CHAIN (f_ovfl) = f_gtop;
6112 DECL_CHAIN (f_gtop) = f_ftop;
6113 DECL_CHAIN (f_ftop) = f_goff;
6114 DECL_CHAIN (f_goff) = f_foff;
6115 DECL_CHAIN (f_foff) = f_res;
6117 layout_type (record);
6118 return record;
6120 else
6121 /* Otherwise, we use 'void *'. */
6122 return ptr_type_node;
6125 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
6127 static void
6128 mips_va_start (tree valist, rtx nextarg)
6130 if (EABI_FLOAT_VARARGS_P)
6132 const CUMULATIVE_ARGS *cum;
6133 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
6134 tree ovfl, gtop, ftop, goff, foff;
6135 tree t;
6136 int gpr_save_area_size;
6137 int fpr_save_area_size;
6138 int fpr_offset;
6140 cum = &crtl->args.info;
6141 gpr_save_area_size
6142 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
6143 fpr_save_area_size
6144 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
6146 f_ovfl = TYPE_FIELDS (va_list_type_node);
6147 f_gtop = DECL_CHAIN (f_ovfl);
6148 f_ftop = DECL_CHAIN (f_gtop);
6149 f_goff = DECL_CHAIN (f_ftop);
6150 f_foff = DECL_CHAIN (f_goff);
6152 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6153 NULL_TREE);
6154 gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
6155 NULL_TREE);
6156 ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
6157 NULL_TREE);
6158 goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
6159 NULL_TREE);
6160 foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
6161 NULL_TREE);
6163 /* Emit code to initialize OVFL, which points to the next varargs
6164 stack argument. CUM->STACK_WORDS gives the number of stack
6165 words used by named arguments. */
6166 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
6167 if (cum->stack_words > 0)
6168 t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
6169 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
6170 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6172 /* Emit code to initialize GTOP, the top of the GPR save area. */
6173 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
6174 t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
6175 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6177 /* Emit code to initialize FTOP, the top of the FPR save area.
6178 This address is gpr_save_area_bytes below GTOP, rounded
6179 down to the next fp-aligned boundary. */
6180 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
6181 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
6182 fpr_offset &= -UNITS_PER_FPVALUE;
6183 if (fpr_offset)
6184 t = fold_build_pointer_plus_hwi (t, -fpr_offset);
6185 t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
6186 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6188 /* Emit code to initialize GOFF, the offset from GTOP of the
6189 next GPR argument. */
6190 t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
6191 build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
6192 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6194 /* Likewise emit code to initialize FOFF, the offset from FTOP
6195 of the next FPR argument. */
6196 t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
6197 build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
6198 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6200 else
6202 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
6203 std_expand_builtin_va_start (valist, nextarg);
6207 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
6208 types as well. */
6210 static tree
6211 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6212 gimple_seq *post_p)
6214 tree addr, t, type_size, rounded_size, valist_tmp;
6215 unsigned HOST_WIDE_INT align, boundary;
6216 bool indirect;
6218 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
6219 if (indirect)
6220 type = build_pointer_type (type);
6222 align = PARM_BOUNDARY / BITS_PER_UNIT;
6223 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
6225 /* When we align parameter on stack for caller, if the parameter
6226 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
6227 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
6228 here with caller. */
6229 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
6230 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
6232 boundary /= BITS_PER_UNIT;
6234 /* Hoist the valist value into a temporary for the moment. */
6235 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
6237 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
6238 requires greater alignment, we must perform dynamic alignment. */
6239 if (boundary > align)
6241 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6242 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
6243 gimplify_and_add (t, pre_p);
6245 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6246 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
6247 valist_tmp,
6248 build_int_cst (TREE_TYPE (valist), -boundary)));
6249 gimplify_and_add (t, pre_p);
6251 else
6252 boundary = align;
6254 /* If the actual alignment is less than the alignment of the type,
6255 adjust the type accordingly so that we don't assume strict alignment
6256 when dereferencing the pointer. */
6257 boundary *= BITS_PER_UNIT;
6258 if (boundary < TYPE_ALIGN (type))
6260 type = build_variant_type_copy (type);
6261 TYPE_ALIGN (type) = boundary;
6264 /* Compute the rounded size of the type. */
6265 type_size = size_in_bytes (type);
6266 rounded_size = round_up (type_size, align);
6268 /* Reduce rounded_size so it's sharable with the postqueue. */
6269 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
6271 /* Get AP. */
6272 addr = valist_tmp;
6273 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
6275 /* Small args are padded downward. */
6276 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
6277 rounded_size, size_int (align));
6278 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
6279 size_binop (MINUS_EXPR, rounded_size, type_size));
6280 addr = fold_build_pointer_plus (addr, t);
6283 /* Compute new value for AP. */
6284 t = fold_build_pointer_plus (valist_tmp, rounded_size);
6285 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
6286 gimplify_and_add (t, pre_p);
6288 addr = fold_convert (build_pointer_type (type), addr);
6290 if (indirect)
6291 addr = build_va_arg_indirect_ref (addr);
6293 return build_va_arg_indirect_ref (addr);
6296 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
6298 static tree
6299 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6300 gimple_seq *post_p)
6302 tree addr;
6303 bool indirect_p;
6305 indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
6306 if (indirect_p)
6307 type = build_pointer_type (type);
6309 if (!EABI_FLOAT_VARARGS_P)
6310 addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
6311 else
6313 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
6314 tree ovfl, top, off, align;
6315 HOST_WIDE_INT size, rsize, osize;
6316 tree t, u;
6318 f_ovfl = TYPE_FIELDS (va_list_type_node);
6319 f_gtop = DECL_CHAIN (f_ovfl);
6320 f_ftop = DECL_CHAIN (f_gtop);
6321 f_goff = DECL_CHAIN (f_ftop);
6322 f_foff = DECL_CHAIN (f_goff);
6324 /* Let:
6326 TOP be the top of the GPR or FPR save area;
6327 OFF be the offset from TOP of the next register;
6328 ADDR_RTX be the address of the argument;
6329 SIZE be the number of bytes in the argument type;
6330 RSIZE be the number of bytes used to store the argument
6331 when it's in the register save area; and
6332 OSIZE be the number of bytes used to store it when it's
6333 in the stack overflow area.
6335 The code we want is:
6337 1: off &= -rsize; // round down
6338 2: if (off != 0)
6339 3: {
6340 4: addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
6341 5: off -= rsize;
6342 6: }
6343 7: else
6344 8: {
6345 9: ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
6346 10: addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
6347 11: ovfl += osize;
6348 14: }
6350 [1] and [9] can sometimes be optimized away. */
6352 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6353 NULL_TREE);
6354 size = int_size_in_bytes (type);
6356 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
6357 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
6359 top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
6360 unshare_expr (valist), f_ftop, NULL_TREE);
6361 off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
6362 unshare_expr (valist), f_foff, NULL_TREE);
6364 /* When va_start saves FPR arguments to the stack, each slot
6365 takes up UNITS_PER_HWFPVALUE bytes, regardless of the
6366 argument's precision. */
6367 rsize = UNITS_PER_HWFPVALUE;
6369 /* Overflow arguments are padded to UNITS_PER_WORD bytes
6370 (= PARM_BOUNDARY bits). This can be different from RSIZE
6371 in two cases:
6373 (1) On 32-bit targets when TYPE is a structure such as:
6375 struct s { float f; };
6377 Such structures are passed in paired FPRs, so RSIZE
6378 will be 8 bytes. However, the structure only takes
6379 up 4 bytes of memory, so OSIZE will only be 4.
6381 (2) In combinations such as -mgp64 -msingle-float
6382 -fshort-double. Doubles passed in registers will then take
6383 up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
6384 stack take up UNITS_PER_WORD bytes. */
6385 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
6387 else
6389 top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
6390 unshare_expr (valist), f_gtop, NULL_TREE);
6391 off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
6392 unshare_expr (valist), f_goff, NULL_TREE);
6393 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6394 if (rsize > UNITS_PER_WORD)
6396 /* [1] Emit code for: off &= -rsize. */
6397 t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
6398 build_int_cst (TREE_TYPE (off), -rsize));
6399 gimplify_assign (unshare_expr (off), t, pre_p);
6401 osize = rsize;
6404 /* [2] Emit code to branch if off == 0. */
6405 t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
6406 build_int_cst (TREE_TYPE (off), 0));
6407 addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
6409 /* [5] Emit code for: off -= rsize. We do this as a form of
6410 post-decrement not available to C. */
6411 t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
6412 t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
6414 /* [4] Emit code for:
6415 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0). */
6416 t = fold_convert (sizetype, t);
6417 t = fold_build1 (NEGATE_EXPR, sizetype, t);
6418 t = fold_build_pointer_plus (top, t);
6419 if (BYTES_BIG_ENDIAN && rsize > size)
6420 t = fold_build_pointer_plus_hwi (t, rsize - size);
6421 COND_EXPR_THEN (addr) = t;
6423 if (osize > UNITS_PER_WORD)
6425 /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize. */
6426 t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
6427 u = build_int_cst (TREE_TYPE (t), -osize);
6428 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
6429 align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
6430 unshare_expr (ovfl), t);
6432 else
6433 align = NULL;
6435 /* [10, 11] Emit code for:
6436 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
6437 ovfl += osize. */
6438 u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
6439 t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
6440 if (BYTES_BIG_ENDIAN && osize > size)
6441 t = fold_build_pointer_plus_hwi (t, osize - size);
6443 /* String [9] and [10, 11] together. */
6444 if (align)
6445 t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
6446 COND_EXPR_ELSE (addr) = t;
6448 addr = fold_convert (build_pointer_type (type), addr);
6449 addr = build_va_arg_indirect_ref (addr);
6452 if (indirect_p)
6453 addr = build_va_arg_indirect_ref (addr);
6455 return addr;
6458 /* Declare a unique, locally-binding function called NAME, then start
6459 its definition. */
6461 static void
6462 mips_start_unique_function (const char *name)
6464 tree decl;
6466 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
6467 get_identifier (name),
6468 build_function_type_list (void_type_node, NULL_TREE));
6469 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
6470 NULL_TREE, void_type_node);
6471 TREE_PUBLIC (decl) = 1;
6472 TREE_STATIC (decl) = 1;
6474 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
6476 targetm.asm_out.unique_section (decl, 0);
6477 switch_to_section (get_named_section (decl, NULL, 0));
6479 targetm.asm_out.globalize_label (asm_out_file, name);
6480 fputs ("\t.hidden\t", asm_out_file);
6481 assemble_name (asm_out_file, name);
6482 putc ('\n', asm_out_file);
6485 /* Start a definition of function NAME. MIPS16_P indicates whether the
6486 function contains MIPS16 code. */
6488 static void
6489 mips_start_function_definition (const char *name, bool mips16_p)
6491 if (mips16_p)
6492 fprintf (asm_out_file, "\t.set\tmips16\n");
6493 else
6494 fprintf (asm_out_file, "\t.set\tnomips16\n");
6496 if (TARGET_MICROMIPS)
6497 fprintf (asm_out_file, "\t.set\tmicromips\n");
6498 #ifdef HAVE_GAS_MICROMIPS
6499 else
6500 fprintf (asm_out_file, "\t.set\tnomicromips\n");
6501 #endif
6503 if (!flag_inhibit_size_directive)
6505 fputs ("\t.ent\t", asm_out_file);
6506 assemble_name (asm_out_file, name);
6507 fputs ("\n", asm_out_file);
6510 ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
6512 /* Start the definition proper. */
6513 assemble_name (asm_out_file, name);
6514 fputs (":\n", asm_out_file);
6517 /* End a function definition started by mips_start_function_definition. */
6519 static void
6520 mips_end_function_definition (const char *name)
6522 if (!flag_inhibit_size_directive)
6524 fputs ("\t.end\t", asm_out_file);
6525 assemble_name (asm_out_file, name);
6526 fputs ("\n", asm_out_file);
6530 /* If *STUB_PTR points to a stub, output a comdat-style definition for it,
6531 then free *STUB_PTR. */
6533 static void
6534 mips_finish_stub (mips_one_only_stub **stub_ptr)
6536 mips_one_only_stub *stub = *stub_ptr;
6537 if (!stub)
6538 return;
6540 const char *name = stub->get_name ();
6541 mips_start_unique_function (name);
6542 mips_start_function_definition (name, false);
6543 stub->output_body ();
6544 mips_end_function_definition (name);
6545 delete stub;
6546 *stub_ptr = 0;
6549 /* Return true if calls to X can use R_MIPS_CALL* relocations. */
6551 static bool
6552 mips_ok_for_lazy_binding_p (rtx x)
6554 return (TARGET_USE_GOT
6555 && GET_CODE (x) == SYMBOL_REF
6556 && !SYMBOL_REF_BIND_NOW_P (x)
6557 && !mips_symbol_binds_local_p (x));
6560 /* Load function address ADDR into register DEST. TYPE is as for
6561 mips_expand_call. Return true if we used an explicit lazy-binding
6562 sequence. */
6564 static bool
6565 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
6567 /* If we're generating PIC, and this call is to a global function,
6568 try to allow its address to be resolved lazily. This isn't
6569 possible for sibcalls when $gp is call-saved because the value
6570 of $gp on entry to the stub would be our caller's gp, not ours. */
6571 if (TARGET_EXPLICIT_RELOCS
6572 && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
6573 && mips_ok_for_lazy_binding_p (addr))
6575 addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
6576 emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
6577 return true;
6579 else
6581 mips_emit_move (dest, addr);
6582 return false;
6586 struct local_alias_traits : default_hashmap_traits
6588 static hashval_t hash (rtx);
6589 static bool equal_keys (rtx, rtx);
6592 /* Each locally-defined hard-float MIPS16 function has a local symbol
6593 associated with it. This hash table maps the function symbol (FUNC)
6594 to the local symbol (LOCAL). */
6595 static GTY (()) hash_map<rtx, rtx, local_alias_traits> *mips16_local_aliases;
6597 /* Hash table callbacks for mips16_local_aliases. */
6599 hashval_t
6600 local_alias_traits::hash (rtx func)
6602 return htab_hash_string (XSTR (func, 0));
6605 bool
6606 local_alias_traits::equal_keys (rtx func1, rtx func2)
6608 return rtx_equal_p (func1, func2);
6611 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
6612 Return a local alias for it, creating a new one if necessary. */
6614 static rtx
6615 mips16_local_alias (rtx func)
6617 /* Create the hash table if this is the first call. */
6618 if (mips16_local_aliases == NULL)
6619 mips16_local_aliases
6620 = hash_map<rtx, rtx, local_alias_traits>::create_ggc (37);
6622 /* Look up the function symbol, creating a new entry if need be. */
6623 bool existed;
6624 rtx *slot = &mips16_local_aliases->get_or_insert (func, &existed);
6625 gcc_assert (slot != NULL);
6627 if (!existed)
6629 const char *func_name, *local_name;
6630 rtx local;
6632 /* Create a new SYMBOL_REF for the local symbol. The choice of
6633 __fn_local_* is based on the __fn_stub_* names that we've
6634 traditionally used for the non-MIPS16 stub. */
6635 func_name = targetm.strip_name_encoding (XSTR (func, 0));
6636 local_name = ACONCAT (("__fn_local_", func_name, NULL));
6637 local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
6638 SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
6640 /* Create a new structure to represent the mapping. */
6641 *slot = local;
6643 return *slot;
6646 /* A chained list of functions for which mips16_build_call_stub has already
6647 generated a stub. NAME is the name of the function and FP_RET_P is true
6648 if the function returns a value in floating-point registers. */
6649 struct mips16_stub {
6650 struct mips16_stub *next;
6651 char *name;
6652 bool fp_ret_p;
6654 static struct mips16_stub *mips16_stubs;
6656 /* Return the two-character string that identifies floating-point
6657 return mode MODE in the name of a MIPS16 function stub. */
6659 static const char *
6660 mips16_call_stub_mode_suffix (machine_mode mode)
6662 if (mode == SFmode)
6663 return "sf";
6664 else if (mode == DFmode)
6665 return "df";
6666 else if (mode == SCmode)
6667 return "sc";
6668 else if (mode == DCmode)
6669 return "dc";
6670 else if (mode == V2SFmode)
6672 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT);
6673 return "df";
6675 else
6676 gcc_unreachable ();
6679 /* Write instructions to move a 32-bit value between general register
6680 GPREG and floating-point register FPREG. DIRECTION is 't' to move
6681 from GPREG to FPREG and 'f' to move in the opposite direction. */
6683 static void
6684 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6686 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6687 reg_names[gpreg], reg_names[fpreg]);
6690 /* Likewise for 64-bit values. */
6692 static void
6693 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6695 if (TARGET_64BIT)
6696 fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
6697 reg_names[gpreg], reg_names[fpreg]);
6698 else if (ISA_HAS_MXHC1)
6700 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6701 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6702 fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
6703 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
6705 else if (TARGET_FLOATXX && direction == 't')
6707 /* Use the argument save area to move via memory. */
6708 fprintf (asm_out_file, "\tsw\t%s,0($sp)\n", reg_names[gpreg]);
6709 fprintf (asm_out_file, "\tsw\t%s,4($sp)\n", reg_names[gpreg + 1]);
6710 fprintf (asm_out_file, "\tldc1\t%s,0($sp)\n", reg_names[fpreg]);
6712 else if (TARGET_FLOATXX && direction == 'f')
6714 /* Use the argument save area to move via memory. */
6715 fprintf (asm_out_file, "\tsdc1\t%s,0($sp)\n", reg_names[fpreg]);
6716 fprintf (asm_out_file, "\tlw\t%s,0($sp)\n", reg_names[gpreg]);
6717 fprintf (asm_out_file, "\tlw\t%s,4($sp)\n", reg_names[gpreg + 1]);
6719 else
6721 /* Move the least-significant word. */
6722 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6723 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6724 /* ...then the most significant word. */
6725 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6726 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
6730 /* Write out code to move floating-point arguments into or out of
6731 general registers. FP_CODE is the code describing which arguments
6732 are present (see the comment above the definition of CUMULATIVE_ARGS
6733 in mips.h). DIRECTION is as for mips_output_32bit_xfer. */
6735 static void
6736 mips_output_args_xfer (int fp_code, char direction)
6738 unsigned int gparg, fparg, f;
6739 CUMULATIVE_ARGS cum;
6741 /* This code only works for o32 and o64. */
6742 gcc_assert (TARGET_OLDABI);
6744 mips_init_cumulative_args (&cum, NULL);
6746 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6748 machine_mode mode;
6749 struct mips_arg_info info;
6751 if ((f & 3) == 1)
6752 mode = SFmode;
6753 else if ((f & 3) == 2)
6754 mode = DFmode;
6755 else
6756 gcc_unreachable ();
6758 mips_get_arg_info (&info, &cum, mode, NULL, true);
6759 gparg = mips_arg_regno (&info, false);
6760 fparg = mips_arg_regno (&info, true);
6762 if (mode == SFmode)
6763 mips_output_32bit_xfer (direction, gparg, fparg);
6764 else
6765 mips_output_64bit_xfer (direction, gparg, fparg);
6767 mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
6771 /* Write a MIPS16 stub for the current function. This stub is used
6772 for functions which take arguments in the floating-point registers.
6773 It is normal-mode code that moves the floating-point arguments
6774 into the general registers and then jumps to the MIPS16 code. */
6776 static void
6777 mips16_build_function_stub (void)
6779 const char *fnname, *alias_name, *separator;
6780 char *secname, *stubname;
6781 tree stubdecl;
6782 unsigned int f;
6783 rtx symbol, alias;
6785 /* Create the name of the stub, and its unique section. */
6786 symbol = XEXP (DECL_RTL (current_function_decl), 0);
6787 alias = mips16_local_alias (symbol);
6789 fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6790 alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6791 secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6792 stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6794 /* Build a decl for the stub. */
6795 stubdecl = build_decl (BUILTINS_LOCATION,
6796 FUNCTION_DECL, get_identifier (stubname),
6797 build_function_type_list (void_type_node, NULL_TREE));
6798 set_decl_section_name (stubdecl, secname);
6799 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6800 RESULT_DECL, NULL_TREE, void_type_node);
6802 /* Output a comment. */
6803 fprintf (asm_out_file, "\t# Stub function for %s (",
6804 current_function_name ());
6805 separator = "";
6806 for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6808 fprintf (asm_out_file, "%s%s", separator,
6809 (f & 3) == 1 ? "float" : "double");
6810 separator = ", ";
6812 fprintf (asm_out_file, ")\n");
6814 /* Start the function definition. */
6815 assemble_start_function (stubdecl, stubname);
6816 mips_start_function_definition (stubname, false);
6818 /* If generating pic2 code, either set up the global pointer or
6819 switch to pic0. */
6820 if (TARGET_ABICALLS_PIC2)
6822 if (TARGET_ABSOLUTE_ABICALLS)
6823 fprintf (asm_out_file, "\t.option\tpic0\n");
6824 else
6826 output_asm_insn ("%(.cpload\t%^%)", NULL);
6827 /* Emit an R_MIPS_NONE relocation to tell the linker what the
6828 target function is. Use a local GOT access when loading the
6829 symbol, to cut down on the number of unnecessary GOT entries
6830 for stubs that aren't needed. */
6831 output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6832 symbol = alias;
6836 /* Load the address of the MIPS16 function into $25. Do this first so
6837 that targets with coprocessor interlocks can use an MFC1 to fill the
6838 delay slot. */
6839 output_asm_insn ("la\t%^,%0", &symbol);
6841 /* Move the arguments from floating-point registers to general registers. */
6842 mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6844 /* Jump to the MIPS16 function. */
6845 output_asm_insn ("jr\t%^", NULL);
6847 if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6848 fprintf (asm_out_file, "\t.option\tpic2\n");
6850 mips_end_function_definition (stubname);
6852 /* If the linker needs to create a dynamic symbol for the target
6853 function, it will associate the symbol with the stub (which,
6854 unlike the target function, follows the proper calling conventions).
6855 It is therefore useful to have a local alias for the target function,
6856 so that it can still be identified as MIPS16 code. As an optimization,
6857 this symbol can also be used for indirect MIPS16 references from
6858 within this file. */
6859 ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6861 switch_to_section (function_section (current_function_decl));
6864 /* The current function is a MIPS16 function that returns a value in an FPR.
6865 Copy the return value from its soft-float to its hard-float location.
6866 libgcc2 has special non-MIPS16 helper functions for each case. */
6868 static void
6869 mips16_copy_fpr_return_value (void)
6871 rtx fn, insn, retval;
6872 tree return_type;
6873 machine_mode return_mode;
6874 const char *name;
6876 return_type = DECL_RESULT (current_function_decl);
6877 return_mode = DECL_MODE (return_type);
6879 name = ACONCAT (("__mips16_ret_",
6880 mips16_call_stub_mode_suffix (return_mode),
6881 NULL));
6882 fn = mips16_stub_function (name);
6884 /* The function takes arguments in $2 (and possibly $3), so calls
6885 to it cannot be lazily bound. */
6886 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6888 /* Model the call as something that takes the GPR return value as
6889 argument and returns an "updated" value. */
6890 retval = gen_rtx_REG (return_mode, GP_RETURN);
6891 insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6892 const0_rtx, NULL_RTX, false);
6893 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6896 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6897 RETVAL is the location of the return value, or null if this is
6898 a "call" rather than a "call_value". ARGS_SIZE is the size of the
6899 arguments and FP_CODE is the code built by mips_function_arg;
6900 see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6902 There are three alternatives:
6904 - If a stub was needed, emit the call and return the call insn itself.
6906 - If we can avoid using a stub by redirecting the call, set *FN_PTR
6907 to the new target and return null.
6909 - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6910 unmodified.
6912 A stub is needed for calls to functions that, in normal mode,
6913 receive arguments in FPRs or return values in FPRs. The stub
6914 copies the arguments from their soft-float positions to their
6915 hard-float positions, calls the real function, then copies the
6916 return value from its hard-float position to its soft-float
6917 position.
6919 We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6920 If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6921 automatically redirects the JAL to the stub, otherwise the JAL
6922 continues to call FN directly. */
6924 static rtx_insn *
6925 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6927 const char *fnname;
6928 bool fp_ret_p;
6929 struct mips16_stub *l;
6930 rtx_insn *insn;
6931 rtx pattern, fn;
6933 /* We don't need to do anything if we aren't in MIPS16 mode, or if
6934 we were invoked with the -msoft-float option. */
6935 if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
6936 return NULL;
6938 /* Figure out whether the value might come back in a floating-point
6939 register. */
6940 fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
6942 /* We don't need to do anything if there were no floating-point
6943 arguments and the value will not be returned in a floating-point
6944 register. */
6945 if (fp_code == 0 && !fp_ret_p)
6946 return NULL;
6948 /* We don't need to do anything if this is a call to a special
6949 MIPS16 support function. */
6950 fn = *fn_ptr;
6951 if (mips16_stub_function_p (fn))
6952 return NULL;
6954 /* If we're calling a locally-defined MIPS16 function, we know that
6955 it will return values in both the "soft-float" and "hard-float"
6956 registers. There is no need to use a stub to move the latter
6957 to the former. */
6958 if (fp_code == 0 && mips16_local_function_p (fn))
6959 return NULL;
6961 /* This code will only work for o32 and o64 abis. The other ABI's
6962 require more sophisticated support. */
6963 gcc_assert (TARGET_OLDABI);
6965 /* If we're calling via a function pointer, use one of the magic
6966 libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
6967 Each stub expects the function address to arrive in register $2. */
6968 if (GET_CODE (fn) != SYMBOL_REF
6969 || !call_insn_operand (fn, VOIDmode))
6971 char buf[30];
6972 rtx stub_fn, addr;
6973 rtx_insn *insn;
6974 bool lazy_p;
6976 /* If this is a locally-defined and locally-binding function,
6977 avoid the stub by calling the local alias directly. */
6978 if (mips16_local_function_p (fn))
6980 *fn_ptr = mips16_local_alias (fn);
6981 return NULL;
6984 /* Create a SYMBOL_REF for the libgcc.a function. */
6985 if (fp_ret_p)
6986 sprintf (buf, "__mips16_call_stub_%s_%d",
6987 mips16_call_stub_mode_suffix (GET_MODE (retval)),
6988 fp_code);
6989 else
6990 sprintf (buf, "__mips16_call_stub_%d", fp_code);
6991 stub_fn = mips16_stub_function (buf);
6993 /* The function uses $2 as an argument, so calls to it
6994 cannot be lazily bound. */
6995 SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6997 /* Load the target function into $2. */
6998 addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6999 lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
7001 /* Emit the call. */
7002 insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
7003 args_size, NULL_RTX, lazy_p);
7005 /* Tell GCC that this call does indeed use the value of $2. */
7006 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
7008 /* If we are handling a floating-point return value, we need to
7009 save $18 in the function prologue. Putting a note on the
7010 call will mean that df_regs_ever_live_p ($18) will be true if the
7011 call is not eliminated, and we can check that in the prologue
7012 code. */
7013 if (fp_ret_p)
7014 CALL_INSN_FUNCTION_USAGE (insn) =
7015 gen_rtx_EXPR_LIST (VOIDmode,
7016 gen_rtx_CLOBBER (VOIDmode,
7017 gen_rtx_REG (word_mode, 18)),
7018 CALL_INSN_FUNCTION_USAGE (insn));
7020 return insn;
7023 /* We know the function we are going to call. If we have already
7024 built a stub, we don't need to do anything further. */
7025 fnname = targetm.strip_name_encoding (XSTR (fn, 0));
7026 for (l = mips16_stubs; l != NULL; l = l->next)
7027 if (strcmp (l->name, fnname) == 0)
7028 break;
7030 if (l == NULL)
7032 const char *separator;
7033 char *secname, *stubname;
7034 tree stubid, stubdecl;
7035 unsigned int f;
7037 /* If the function does not return in FPRs, the special stub
7038 section is named
7039 .mips16.call.FNNAME
7041 If the function does return in FPRs, the stub section is named
7042 .mips16.call.fp.FNNAME
7044 Build a decl for the stub. */
7045 secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
7046 fnname, NULL));
7047 stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
7048 fnname, NULL));
7049 stubid = get_identifier (stubname);
7050 stubdecl = build_decl (BUILTINS_LOCATION,
7051 FUNCTION_DECL, stubid,
7052 build_function_type_list (void_type_node,
7053 NULL_TREE));
7054 set_decl_section_name (stubdecl, secname);
7055 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
7056 RESULT_DECL, NULL_TREE,
7057 void_type_node);
7059 /* Output a comment. */
7060 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
7061 (fp_ret_p
7062 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
7063 : ""),
7064 fnname);
7065 separator = "";
7066 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7068 fprintf (asm_out_file, "%s%s", separator,
7069 (f & 3) == 1 ? "float" : "double");
7070 separator = ", ";
7072 fprintf (asm_out_file, ")\n");
7074 /* Start the function definition. */
7075 assemble_start_function (stubdecl, stubname);
7076 mips_start_function_definition (stubname, false);
7078 if (fp_ret_p)
7080 fprintf (asm_out_file, "\t.cfi_startproc\n");
7082 /* Create a fake CFA 4 bytes below the stack pointer.
7083 This works around unwinders (like libgcc's) that expect
7084 the CFA for non-signal frames to be unique. */
7085 fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
7087 /* "Save" $sp in itself so we don't use the fake CFA.
7088 This is: DW_CFA_val_expression r29, { DW_OP_reg29 }. */
7089 fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
7091 /* Save the return address in $18. The stub's caller knows
7092 that $18 might be clobbered, even though $18 is usually
7093 a call-saved register.
7095 Do it early on in case the last move to a floating-point
7096 register can be scheduled into the delay slot of the
7097 call we are about to make. */
7098 fprintf (asm_out_file, "\tmove\t%s,%s\n",
7099 reg_names[GP_REG_FIRST + 18],
7100 reg_names[RETURN_ADDR_REGNUM]);
7102 else
7104 /* Load the address of the MIPS16 function into $25. Do this
7105 first so that targets with coprocessor interlocks can use
7106 an MFC1 to fill the delay slot. */
7107 if (TARGET_EXPLICIT_RELOCS)
7109 output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
7110 output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
7112 else
7113 output_asm_insn ("la\t%^,%0", &fn);
7116 /* Move the arguments from general registers to floating-point
7117 registers. */
7118 mips_output_args_xfer (fp_code, 't');
7120 if (fp_ret_p)
7122 /* Now call the non-MIPS16 function. */
7123 output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
7124 fprintf (asm_out_file, "\t.cfi_register 31,18\n");
7126 /* Move the result from floating-point registers to
7127 general registers. */
7128 switch (GET_MODE (retval))
7130 case SCmode:
7131 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
7132 TARGET_BIG_ENDIAN
7133 ? FP_REG_FIRST + 2
7134 : FP_REG_FIRST);
7135 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
7136 TARGET_LITTLE_ENDIAN
7137 ? FP_REG_FIRST + 2
7138 : FP_REG_FIRST);
7139 if (GET_MODE (retval) == SCmode && TARGET_64BIT)
7141 /* On 64-bit targets, complex floats are returned in
7142 a single GPR, such that "sd" on a suitably-aligned
7143 target would store the value correctly. */
7144 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
7145 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
7146 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
7147 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
7148 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
7149 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
7150 fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
7151 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
7152 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
7153 fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
7154 reg_names[GP_RETURN],
7155 reg_names[GP_RETURN],
7156 reg_names[GP_RETURN + 1]);
7158 break;
7160 case SFmode:
7161 mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
7162 break;
7164 case DCmode:
7165 mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
7166 FP_REG_FIRST + 2);
7167 /* Fall though. */
7168 case DFmode:
7169 case V2SFmode:
7170 gcc_assert (TARGET_PAIRED_SINGLE_FLOAT
7171 || GET_MODE (retval) != V2SFmode);
7172 mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
7173 break;
7175 default:
7176 gcc_unreachable ();
7178 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
7179 fprintf (asm_out_file, "\t.cfi_endproc\n");
7181 else
7183 /* Jump to the previously-loaded address. */
7184 output_asm_insn ("jr\t%^", NULL);
7187 #ifdef ASM_DECLARE_FUNCTION_SIZE
7188 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
7189 #endif
7191 mips_end_function_definition (stubname);
7193 /* Record this stub. */
7194 l = XNEW (struct mips16_stub);
7195 l->name = xstrdup (fnname);
7196 l->fp_ret_p = fp_ret_p;
7197 l->next = mips16_stubs;
7198 mips16_stubs = l;
7201 /* If we expect a floating-point return value, but we've built a
7202 stub which does not expect one, then we're in trouble. We can't
7203 use the existing stub, because it won't handle the floating-point
7204 value. We can't build a new stub, because the linker won't know
7205 which stub to use for the various calls in this object file.
7206 Fortunately, this case is illegal, since it means that a function
7207 was declared in two different ways in a single compilation. */
7208 if (fp_ret_p && !l->fp_ret_p)
7209 error ("cannot handle inconsistent calls to %qs", fnname);
7211 if (retval == NULL_RTX)
7212 pattern = gen_call_internal_direct (fn, args_size);
7213 else
7214 pattern = gen_call_value_internal_direct (retval, fn, args_size);
7215 insn = mips_emit_call_insn (pattern, fn, fn, false);
7217 /* If we are calling a stub which handles a floating-point return
7218 value, we need to arrange to save $18 in the prologue. We do this
7219 by marking the function call as using the register. The prologue
7220 will later see that it is used, and emit code to save it. */
7221 if (fp_ret_p)
7222 CALL_INSN_FUNCTION_USAGE (insn) =
7223 gen_rtx_EXPR_LIST (VOIDmode,
7224 gen_rtx_CLOBBER (VOIDmode,
7225 gen_rtx_REG (word_mode, 18)),
7226 CALL_INSN_FUNCTION_USAGE (insn));
7228 return insn;
7231 /* Expand a call of type TYPE. RESULT is where the result will go (null
7232 for "call"s and "sibcall"s), ADDR is the address of the function,
7233 ARGS_SIZE is the size of the arguments and AUX is the value passed
7234 to us by mips_function_arg. LAZY_P is true if this call already
7235 involves a lazily-bound function address (such as when calling
7236 functions through a MIPS16 hard-float stub).
7238 Return the call itself. */
7240 rtx_insn *
7241 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
7242 rtx args_size, rtx aux, bool lazy_p)
7244 rtx orig_addr, pattern;
7245 rtx_insn *insn;
7246 int fp_code;
7248 fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
7249 insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
7250 if (insn)
7252 gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
7253 return insn;
7256 orig_addr = addr;
7257 if (!call_insn_operand (addr, VOIDmode))
7259 if (type == MIPS_CALL_EPILOGUE)
7260 addr = MIPS_EPILOGUE_TEMP (Pmode);
7261 else
7262 addr = gen_reg_rtx (Pmode);
7263 lazy_p |= mips_load_call_address (type, addr, orig_addr);
7266 if (result == 0)
7268 rtx (*fn) (rtx, rtx);
7270 if (type == MIPS_CALL_SIBCALL)
7271 fn = gen_sibcall_internal;
7272 else
7273 fn = gen_call_internal;
7275 pattern = fn (addr, args_size);
7277 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
7279 /* Handle return values created by mips_return_fpr_pair. */
7280 rtx (*fn) (rtx, rtx, rtx, rtx);
7281 rtx reg1, reg2;
7283 if (type == MIPS_CALL_SIBCALL)
7284 fn = gen_sibcall_value_multiple_internal;
7285 else
7286 fn = gen_call_value_multiple_internal;
7288 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
7289 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
7290 pattern = fn (reg1, addr, args_size, reg2);
7292 else
7294 rtx (*fn) (rtx, rtx, rtx);
7296 if (type == MIPS_CALL_SIBCALL)
7297 fn = gen_sibcall_value_internal;
7298 else
7299 fn = gen_call_value_internal;
7301 /* Handle return values created by mips_return_fpr_single. */
7302 if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
7303 result = XEXP (XVECEXP (result, 0, 0), 0);
7304 pattern = fn (result, addr, args_size);
7307 return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
7310 /* Split call instruction INSN into a $gp-clobbering call and
7311 (where necessary) an instruction to restore $gp from its save slot.
7312 CALL_PATTERN is the pattern of the new call. */
7314 void
7315 mips_split_call (rtx insn, rtx call_pattern)
7317 emit_call_insn (call_pattern);
7318 if (!find_reg_note (insn, REG_NORETURN, 0))
7319 mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode,
7320 POST_CALL_TMP_REG));
7323 /* Return true if a call to DECL may need to use JALX. */
7325 static bool
7326 mips_call_may_need_jalx_p (tree decl)
7328 /* If the current translation unit would use a different mode for DECL,
7329 assume that the call needs JALX. */
7330 if (mips_get_compress_mode (decl) != TARGET_COMPRESSION)
7331 return true;
7333 /* mips_get_compress_mode is always accurate for locally-binding
7334 functions in the current translation unit. */
7335 if (!DECL_EXTERNAL (decl) && targetm.binds_local_p (decl))
7336 return false;
7338 /* When -minterlink-compressed is in effect, assume that functions
7339 could use a different encoding mode unless an attribute explicitly
7340 tells us otherwise. */
7341 if (TARGET_INTERLINK_COMPRESSED)
7343 if (!TARGET_COMPRESSION
7344 && mips_get_compress_off_flags (DECL_ATTRIBUTES (decl)) ==0)
7345 return true;
7346 if (TARGET_COMPRESSION
7347 && mips_get_compress_on_flags (DECL_ATTRIBUTES (decl)) == 0)
7348 return true;
7351 return false;
7354 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
7356 static bool
7357 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
7359 if (!TARGET_SIBCALLS)
7360 return false;
7362 /* Interrupt handlers need special epilogue code and therefore can't
7363 use sibcalls. */
7364 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
7365 return false;
7367 /* Direct Js are only possible to functions that use the same ISA encoding.
7368 There is no JX counterpoart of JALX. */
7369 if (decl
7370 && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode)
7371 && mips_call_may_need_jalx_p (decl))
7372 return false;
7374 /* Sibling calls should not prevent lazy binding. Lazy-binding stubs
7375 require $gp to be valid on entry, so sibcalls can only use stubs
7376 if $gp is call-clobbered. */
7377 if (decl
7378 && TARGET_CALL_SAVED_GP
7379 && !TARGET_ABICALLS_PIC0
7380 && !targetm.binds_local_p (decl))
7381 return false;
7383 /* Otherwise OK. */
7384 return true;
7387 /* Implement TARGET_USE_MOVE_BY_PIECES_INFRASTRUCTURE_P. */
7389 bool
7390 mips_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
7391 unsigned int align,
7392 enum by_pieces_operation op,
7393 bool speed_p)
7395 if (op == STORE_BY_PIECES)
7396 return mips_store_by_pieces_p (size, align);
7397 if (op == MOVE_BY_PIECES && HAVE_movmemsi)
7399 /* movmemsi is meant to generate code that is at least as good as
7400 move_by_pieces. However, movmemsi effectively uses a by-pieces
7401 implementation both for moves smaller than a word and for
7402 word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
7403 bytes. We should allow the tree-level optimisers to do such
7404 moves by pieces, as it often exposes other optimization
7405 opportunities. We might as well continue to use movmemsi at
7406 the rtl level though, as it produces better code when
7407 scheduling is disabled (such as at -O). */
7408 if (currently_expanding_to_rtl)
7409 return false;
7410 if (align < BITS_PER_WORD)
7411 return size < UNITS_PER_WORD;
7412 return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
7415 return default_use_by_pieces_infrastructure_p (size, align, op, speed_p);
7418 /* Implement a handler for STORE_BY_PIECES operations
7419 for TARGET_USE_MOVE_BY_PIECES_INFRASTRUCTURE_P. */
7421 bool
7422 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7424 /* Storing by pieces involves moving constants into registers
7425 of size MIN (ALIGN, BITS_PER_WORD), then storing them.
7426 We need to decide whether it is cheaper to load the address of
7427 constant data into a register and use a block move instead. */
7429 /* If the data is only byte aligned, then:
7431 (a1) A block move of less than 4 bytes would involve three 3 LBs and
7432 3 SBs. We might as well use 3 single-instruction LIs and 3 SBs
7433 instead.
7435 (a2) A block move of 4 bytes from aligned source data can use an
7436 LW/SWL/SWR sequence. This is often better than the 4 LIs and
7437 4 SBs that we would generate when storing by pieces. */
7438 if (align <= BITS_PER_UNIT)
7439 return size < 4;
7441 /* If the data is 2-byte aligned, then:
7443 (b1) A block move of less than 4 bytes would use a combination of LBs,
7444 LHs, SBs and SHs. We get better code by using single-instruction
7445 LIs, SBs and SHs instead.
7447 (b2) A block move of 4 bytes from aligned source data would again use
7448 an LW/SWL/SWR sequence. In most cases, loading the address of
7449 the source data would require at least one extra instruction.
7450 It is often more efficient to use 2 single-instruction LIs and
7451 2 SHs instead.
7453 (b3) A block move of up to 3 additional bytes would be like (b1).
7455 (b4) A block move of 8 bytes from aligned source data can use two
7456 LW/SWL/SWR sequences or a single LD/SDL/SDR sequence. Both
7457 sequences are better than the 4 LIs and 4 SHs that we'd generate
7458 when storing by pieces.
7460 The reasoning for higher alignments is similar:
7462 (c1) A block move of less than 4 bytes would be the same as (b1).
7464 (c2) A block move of 4 bytes would use an LW/SW sequence. Again,
7465 loading the address of the source data would typically require
7466 at least one extra instruction. It is generally better to use
7467 LUI/ORI/SW instead.
7469 (c3) A block move of up to 3 additional bytes would be like (b1).
7471 (c4) A block move of 8 bytes can use two LW/SW sequences or a single
7472 LD/SD sequence, and in these cases we've traditionally preferred
7473 the memory copy over the more bulky constant moves. */
7474 return size < 8;
7477 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
7478 Assume that the areas do not overlap. */
7480 static void
7481 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
7483 HOST_WIDE_INT offset, delta;
7484 unsigned HOST_WIDE_INT bits;
7485 int i;
7486 machine_mode mode;
7487 rtx *regs;
7489 /* Work out how many bits to move at a time. If both operands have
7490 half-word alignment, it is usually better to move in half words.
7491 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
7492 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
7493 Otherwise move word-sized chunks. */
7494 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
7495 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
7496 bits = BITS_PER_WORD / 2;
7497 else
7498 bits = BITS_PER_WORD;
7500 mode = mode_for_size (bits, MODE_INT, 0);
7501 delta = bits / BITS_PER_UNIT;
7503 /* Allocate a buffer for the temporary registers. */
7504 regs = XALLOCAVEC (rtx, length / delta);
7506 /* Load as many BITS-sized chunks as possible. Use a normal load if
7507 the source has enough alignment, otherwise use left/right pairs. */
7508 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7510 regs[i] = gen_reg_rtx (mode);
7511 if (MEM_ALIGN (src) >= bits)
7512 mips_emit_move (regs[i], adjust_address (src, mode, offset));
7513 else
7515 rtx part = adjust_address (src, BLKmode, offset);
7516 set_mem_size (part, delta);
7517 if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
7518 gcc_unreachable ();
7522 /* Copy the chunks to the destination. */
7523 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7524 if (MEM_ALIGN (dest) >= bits)
7525 mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
7526 else
7528 rtx part = adjust_address (dest, BLKmode, offset);
7529 set_mem_size (part, delta);
7530 if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
7531 gcc_unreachable ();
7534 /* Mop up any left-over bytes. */
7535 if (offset < length)
7537 src = adjust_address (src, BLKmode, offset);
7538 dest = adjust_address (dest, BLKmode, offset);
7539 move_by_pieces (dest, src, length - offset,
7540 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
7544 /* Helper function for doing a loop-based block operation on memory
7545 reference MEM. Each iteration of the loop will operate on LENGTH
7546 bytes of MEM.
7548 Create a new base register for use within the loop and point it to
7549 the start of MEM. Create a new memory reference that uses this
7550 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
7552 static void
7553 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
7554 rtx *loop_reg, rtx *loop_mem)
7556 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
7558 /* Although the new mem does not refer to a known location,
7559 it does keep up to LENGTH bytes of alignment. */
7560 *loop_mem = change_address (mem, BLKmode, *loop_reg);
7561 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
7564 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
7565 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
7566 the memory regions do not overlap. */
7568 static void
7569 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
7570 HOST_WIDE_INT bytes_per_iter)
7572 rtx_code_label *label;
7573 rtx src_reg, dest_reg, final_src, test;
7574 HOST_WIDE_INT leftover;
7576 leftover = length % bytes_per_iter;
7577 length -= leftover;
7579 /* Create registers and memory references for use within the loop. */
7580 mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
7581 mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
7583 /* Calculate the value that SRC_REG should have after the last iteration
7584 of the loop. */
7585 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
7586 0, 0, OPTAB_WIDEN);
7588 /* Emit the start of the loop. */
7589 label = gen_label_rtx ();
7590 emit_label (label);
7592 /* Emit the loop body. */
7593 mips_block_move_straight (dest, src, bytes_per_iter);
7595 /* Move on to the next block. */
7596 mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
7597 mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
7599 /* Emit the loop condition. */
7600 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
7601 if (Pmode == DImode)
7602 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
7603 else
7604 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
7606 /* Mop up any left-over bytes. */
7607 if (leftover)
7608 mips_block_move_straight (dest, src, leftover);
7611 /* Expand a movmemsi instruction, which copies LENGTH bytes from
7612 memory reference SRC to memory reference DEST. */
7614 bool
7615 mips_expand_block_move (rtx dest, rtx src, rtx length)
7617 /* Disable entirely for R6 initially. */
7618 if (!ISA_HAS_LWL_LWR)
7619 return false;
7621 if (CONST_INT_P (length))
7623 if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
7625 mips_block_move_straight (dest, src, INTVAL (length));
7626 return true;
7628 else if (optimize)
7630 mips_block_move_loop (dest, src, INTVAL (length),
7631 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
7632 return true;
7635 return false;
7638 /* Expand a loop of synci insns for the address range [BEGIN, END). */
7640 void
7641 mips_expand_synci_loop (rtx begin, rtx end)
7643 rtx inc, cmp_result, mask, length;
7644 rtx_code_label *label, *end_label;
7646 /* Create end_label. */
7647 end_label = gen_label_rtx ();
7649 /* Check if begin equals end. */
7650 cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
7651 emit_jump_insn (gen_condjump (cmp_result, end_label));
7653 /* Load INC with the cache line size (rdhwr INC,$1). */
7654 inc = gen_reg_rtx (Pmode);
7655 emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
7657 /* Check if inc is 0. */
7658 cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
7659 emit_jump_insn (gen_condjump (cmp_result, end_label));
7661 /* Calculate mask. */
7662 mask = mips_force_unary (Pmode, NEG, inc);
7664 /* Mask out begin by mask. */
7665 begin = mips_force_binary (Pmode, AND, begin, mask);
7667 /* Calculate length. */
7668 length = mips_force_binary (Pmode, MINUS, end, begin);
7670 /* Loop back to here. */
7671 label = gen_label_rtx ();
7672 emit_label (label);
7674 emit_insn (gen_synci (begin));
7676 /* Update length. */
7677 mips_emit_binary (MINUS, length, length, inc);
7679 /* Update begin. */
7680 mips_emit_binary (PLUS, begin, begin, inc);
7682 /* Check if length is greater than 0. */
7683 cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
7684 emit_jump_insn (gen_condjump (cmp_result, label));
7686 emit_label (end_label);
7689 /* Expand a QI or HI mode atomic memory operation.
7691 GENERATOR contains a pointer to the gen_* function that generates
7692 the SI mode underlying atomic operation using masks that we
7693 calculate.
7695 RESULT is the return register for the operation. Its value is NULL
7696 if unused.
7698 MEM is the location of the atomic access.
7700 OLDVAL is the first operand for the operation.
7702 NEWVAL is the optional second operand for the operation. Its value
7703 is NULL if unused. */
7705 void
7706 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
7707 rtx result, rtx mem, rtx oldval, rtx newval)
7709 rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
7710 rtx unshifted_mask_reg, mask, inverted_mask, si_op;
7711 rtx res = NULL;
7712 machine_mode mode;
7714 mode = GET_MODE (mem);
7716 /* Compute the address of the containing SImode value. */
7717 orig_addr = force_reg (Pmode, XEXP (mem, 0));
7718 memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
7719 force_reg (Pmode, GEN_INT (-4)));
7721 /* Create a memory reference for it. */
7722 memsi = gen_rtx_MEM (SImode, memsi_addr);
7723 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
7724 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
7726 /* Work out the byte offset of the QImode or HImode value,
7727 counting from the least significant byte. */
7728 shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
7729 if (TARGET_BIG_ENDIAN)
7730 mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
7732 /* Multiply by eight to convert the shift value from bytes to bits. */
7733 mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
7735 /* Make the final shift an SImode value, so that it can be used in
7736 SImode operations. */
7737 shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
7739 /* Set MASK to an inclusive mask of the QImode or HImode value. */
7740 unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
7741 unshifted_mask_reg = force_reg (SImode, unshifted_mask);
7742 mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
7744 /* Compute the equivalent exclusive mask. */
7745 inverted_mask = gen_reg_rtx (SImode);
7746 emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
7747 gen_rtx_NOT (SImode, mask)));
7749 /* Shift the old value into place. */
7750 if (oldval != const0_rtx)
7752 oldval = convert_modes (SImode, mode, oldval, true);
7753 oldval = force_reg (SImode, oldval);
7754 oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
7757 /* Do the same for the new value. */
7758 if (newval && newval != const0_rtx)
7760 newval = convert_modes (SImode, mode, newval, true);
7761 newval = force_reg (SImode, newval);
7762 newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
7765 /* Do the SImode atomic access. */
7766 if (result)
7767 res = gen_reg_rtx (SImode);
7768 if (newval)
7769 si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
7770 else if (result)
7771 si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
7772 else
7773 si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
7775 emit_insn (si_op);
7777 if (result)
7779 /* Shift and convert the result. */
7780 mips_emit_binary (AND, res, res, mask);
7781 mips_emit_binary (LSHIFTRT, res, res, shiftsi);
7782 mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
7786 /* Return true if it is possible to use left/right accesses for a
7787 bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
7788 When returning true, update *LEFT and *RIGHT as follows:
7790 *LEFT is a QImode reference to the first byte if big endian or
7791 the last byte if little endian. This address can be used in the
7792 left-side instructions (LWL, SWL, LDL, SDL).
7794 *RIGHT is a QImode reference to the opposite end of the field and
7795 can be used in the patterning right-side instruction. */
7797 static bool
7798 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
7799 rtx *left, rtx *right)
7801 rtx first, last;
7803 /* Check that the size is valid. */
7804 if (width != 32 && (!TARGET_64BIT || width != 64))
7805 return false;
7807 /* We can only access byte-aligned values. Since we are always passed
7808 a reference to the first byte of the field, it is not necessary to
7809 do anything with BITPOS after this check. */
7810 if (bitpos % BITS_PER_UNIT != 0)
7811 return false;
7813 /* Reject aligned bitfields: we want to use a normal load or store
7814 instead of a left/right pair. */
7815 if (MEM_ALIGN (op) >= width)
7816 return false;
7818 /* Get references to both ends of the field. */
7819 first = adjust_address (op, QImode, 0);
7820 last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
7822 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
7823 correspond to the MSB and RIGHT to the LSB. */
7824 if (TARGET_BIG_ENDIAN)
7825 *left = first, *right = last;
7826 else
7827 *left = last, *right = first;
7829 return true;
7832 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
7833 DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
7834 the operation is the equivalent of:
7836 (set DEST (*_extract SRC WIDTH BITPOS))
7838 Return true on success. */
7840 bool
7841 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
7842 HOST_WIDE_INT bitpos, bool unsigned_p)
7844 rtx left, right, temp;
7845 rtx dest1 = NULL_RTX;
7847 /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
7848 be a DImode, create a new temp and emit a zero extend at the end. */
7849 if (GET_MODE (dest) == DImode
7850 && REG_P (dest)
7851 && GET_MODE_BITSIZE (SImode) == width)
7853 dest1 = dest;
7854 dest = gen_reg_rtx (SImode);
7857 if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
7858 return false;
7860 temp = gen_reg_rtx (GET_MODE (dest));
7861 if (GET_MODE (dest) == DImode)
7863 emit_insn (gen_mov_ldl (temp, src, left));
7864 emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
7866 else
7868 emit_insn (gen_mov_lwl (temp, src, left));
7869 emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
7872 /* If we were loading 32bits and the original register was DI then
7873 sign/zero extend into the orignal dest. */
7874 if (dest1)
7876 if (unsigned_p)
7877 emit_insn (gen_zero_extendsidi2 (dest1, dest));
7878 else
7879 emit_insn (gen_extendsidi2 (dest1, dest));
7881 return true;
7884 /* Try to use left/right stores to expand an "ins" pattern. DEST, WIDTH,
7885 BITPOS and SRC are the operands passed to the expander; the operation
7886 is the equivalent of:
7888 (set (zero_extract DEST WIDTH BITPOS) SRC)
7890 Return true on success. */
7892 bool
7893 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
7894 HOST_WIDE_INT bitpos)
7896 rtx left, right;
7897 machine_mode mode;
7899 if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
7900 return false;
7902 mode = mode_for_size (width, MODE_INT, 0);
7903 src = gen_lowpart (mode, src);
7904 if (mode == DImode)
7906 emit_insn (gen_mov_sdl (dest, src, left));
7907 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7909 else
7911 emit_insn (gen_mov_swl (dest, src, left));
7912 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7914 return true;
7917 /* Return true if X is a MEM with the same size as MODE. */
7919 bool
7920 mips_mem_fits_mode_p (machine_mode mode, rtx x)
7922 return (MEM_P (x)
7923 && MEM_SIZE_KNOWN_P (x)
7924 && MEM_SIZE (x) == GET_MODE_SIZE (mode));
7927 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7928 source of an "ext" instruction or the destination of an "ins"
7929 instruction. OP must be a register operand and the following
7930 conditions must hold:
7932 0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7933 0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7934 0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7936 Also reject lengths equal to a word as they are better handled
7937 by the move patterns. */
7939 bool
7940 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7942 if (!ISA_HAS_EXT_INS
7943 || !register_operand (op, VOIDmode)
7944 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7945 return false;
7947 if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7948 return false;
7950 if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7951 return false;
7953 return true;
7956 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7957 operation if MAXLEN is the maxium length of consecutive bits that
7958 can make up MASK. MODE is the mode of the operation. See
7959 mask_low_and_shift_len for the actual definition. */
7961 bool
7962 mask_low_and_shift_p (machine_mode mode, rtx mask, rtx shift, int maxlen)
7964 return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7967 /* Return true iff OP1 and OP2 are valid operands together for the
7968 *and<MODE>3 and *and<MODE>3_mips16 patterns. For the cases to consider,
7969 see the table in the comment before the pattern. */
7971 bool
7972 and_operands_ok (machine_mode mode, rtx op1, rtx op2)
7974 return (memory_operand (op1, mode)
7975 ? and_load_operand (op2, mode)
7976 : and_reg_operand (op2, mode));
7979 /* The canonical form of a mask-low-and-shift-left operation is
7980 (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7981 cleared. Thus we need to shift MASK to the right before checking if it
7982 is a valid mask value. MODE is the mode of the operation. If true
7983 return the length of the mask, otherwise return -1. */
7986 mask_low_and_shift_len (machine_mode mode, rtx mask, rtx shift)
7988 HOST_WIDE_INT shval;
7990 shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7991 return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7994 /* Return true if -msplit-addresses is selected and should be honored.
7996 -msplit-addresses is a half-way house between explicit relocations
7997 and the traditional assembler macros. It can split absolute 32-bit
7998 symbolic constants into a high/lo_sum pair but uses macros for other
7999 sorts of access.
8001 Like explicit relocation support for REL targets, it relies
8002 on GNU extensions in the assembler and the linker.
8004 Although this code should work for -O0, it has traditionally
8005 been treated as an optimization. */
8007 static bool
8008 mips_split_addresses_p (void)
8010 return (TARGET_SPLIT_ADDRESSES
8011 && optimize
8012 && !TARGET_MIPS16
8013 && !flag_pic
8014 && !ABI_HAS_64BIT_SYMBOLS);
8017 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs. */
8019 static void
8020 mips_init_relocs (void)
8022 memset (mips_split_p, '\0', sizeof (mips_split_p));
8023 memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
8024 memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
8025 memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
8026 memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
8028 if (TARGET_MIPS16_PCREL_LOADS)
8029 mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
8030 else
8032 if (ABI_HAS_64BIT_SYMBOLS)
8034 if (TARGET_EXPLICIT_RELOCS)
8036 mips_split_p[SYMBOL_64_HIGH] = true;
8037 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
8038 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
8040 mips_split_p[SYMBOL_64_MID] = true;
8041 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
8042 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
8044 mips_split_p[SYMBOL_64_LOW] = true;
8045 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
8046 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
8048 mips_split_p[SYMBOL_ABSOLUTE] = true;
8049 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
8052 else
8054 if (TARGET_EXPLICIT_RELOCS
8055 || mips_split_addresses_p ()
8056 || TARGET_MIPS16)
8058 mips_split_p[SYMBOL_ABSOLUTE] = true;
8059 mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
8060 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
8065 if (TARGET_MIPS16)
8067 /* The high part is provided by a pseudo copy of $gp. */
8068 mips_split_p[SYMBOL_GP_RELATIVE] = true;
8069 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
8071 else if (TARGET_EXPLICIT_RELOCS)
8072 /* Small data constants are kept whole until after reload,
8073 then lowered by mips_rewrite_small_data. */
8074 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
8076 if (TARGET_EXPLICIT_RELOCS)
8078 mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
8079 if (TARGET_NEWABI)
8081 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
8082 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
8084 else
8086 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
8087 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
8089 if (TARGET_MIPS16)
8090 /* Expose the use of $28 as soon as possible. */
8091 mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
8093 if (TARGET_XGOT)
8095 /* The HIGH and LO_SUM are matched by special .md patterns. */
8096 mips_split_p[SYMBOL_GOT_DISP] = true;
8098 mips_split_p[SYMBOL_GOTOFF_DISP] = true;
8099 mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
8100 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
8102 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
8103 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
8104 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
8106 else
8108 if (TARGET_NEWABI)
8109 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
8110 else
8111 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
8112 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
8113 if (TARGET_MIPS16)
8114 /* Expose the use of $28 as soon as possible. */
8115 mips_split_p[SYMBOL_GOT_DISP] = true;
8119 if (TARGET_NEWABI)
8121 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
8122 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
8123 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
8126 mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
8127 mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
8129 if (TARGET_MIPS16_PCREL_LOADS)
8131 mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
8132 mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
8134 else
8136 mips_split_p[SYMBOL_DTPREL] = true;
8137 mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
8138 mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
8140 mips_split_p[SYMBOL_TPREL] = true;
8141 mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
8142 mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
8145 mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
8146 mips_lo_relocs[SYMBOL_HALF] = "%half(";
8149 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
8150 in context CONTEXT. RELOCS is the array of relocations to use. */
8152 static void
8153 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
8154 const char **relocs)
8156 enum mips_symbol_type symbol_type;
8157 const char *p;
8159 symbol_type = mips_classify_symbolic_expression (op, context);
8160 gcc_assert (relocs[symbol_type]);
8162 fputs (relocs[symbol_type], file);
8163 output_addr_const (file, mips_strip_unspec_address (op));
8164 for (p = relocs[symbol_type]; *p != 0; p++)
8165 if (*p == '(')
8166 fputc (')', file);
8169 /* Start a new block with the given asm switch enabled. If we need
8170 to print a directive, emit PREFIX before it and SUFFIX after it. */
8172 static void
8173 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
8174 const char *prefix, const char *suffix)
8176 if (asm_switch->nesting_level == 0)
8177 fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
8178 asm_switch->nesting_level++;
8181 /* Likewise, but end a block. */
8183 static void
8184 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
8185 const char *prefix, const char *suffix)
8187 gcc_assert (asm_switch->nesting_level);
8188 asm_switch->nesting_level--;
8189 if (asm_switch->nesting_level == 0)
8190 fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
8193 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
8194 that either print a complete line or print nothing. */
8196 void
8197 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
8199 mips_push_asm_switch_1 (asm_switch, "\t", "\n");
8202 void
8203 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
8205 mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
8208 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
8209 The punctuation characters are:
8211 '(' Start a nested ".set noreorder" block.
8212 ')' End a nested ".set noreorder" block.
8213 '[' Start a nested ".set noat" block.
8214 ']' End a nested ".set noat" block.
8215 '<' Start a nested ".set nomacro" block.
8216 '>' End a nested ".set nomacro" block.
8217 '*' Behave like %(%< if generating a delayed-branch sequence.
8218 '#' Print a nop if in a ".set noreorder" block.
8219 '/' Like '#', but do nothing within a delayed-branch sequence.
8220 '?' Print "l" if mips_branch_likely is true
8221 '~' Print a nop if mips_branch_likely is true
8222 '.' Print the name of the register with a hard-wired zero (zero or $0).
8223 '@' Print the name of the assembler temporary register (at or $1).
8224 '^' Print the name of the pic call-through register (t9 or $25).
8225 '+' Print the name of the gp register (usually gp or $28).
8226 '$' Print the name of the stack pointer register (sp or $29).
8227 ':' Print "c" to use the compact version if the delay slot is a nop.
8228 '!' Print "s" to use the short version if the delay slot contains a
8229 16-bit instruction.
8231 See also mips_init_print_operand_pucnt. */
8233 static void
8234 mips_print_operand_punctuation (FILE *file, int ch)
8236 switch (ch)
8238 case '(':
8239 mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
8240 break;
8242 case ')':
8243 mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
8244 break;
8246 case '[':
8247 mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
8248 break;
8250 case ']':
8251 mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
8252 break;
8254 case '<':
8255 mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
8256 break;
8258 case '>':
8259 mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
8260 break;
8262 case '*':
8263 if (final_sequence != 0)
8265 mips_print_operand_punctuation (file, '(');
8266 mips_print_operand_punctuation (file, '<');
8268 break;
8270 case '#':
8271 if (mips_noreorder.nesting_level > 0)
8272 fputs ("\n\tnop", file);
8273 break;
8275 case '/':
8276 /* Print an extra newline so that the delayed insn is separated
8277 from the following ones. This looks neater and is consistent
8278 with non-nop delayed sequences. */
8279 if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
8280 fputs ("\n\tnop\n", file);
8281 break;
8283 case '?':
8284 if (mips_branch_likely)
8285 putc ('l', file);
8286 break;
8288 case '~':
8289 if (mips_branch_likely)
8290 fputs ("\n\tnop", file);
8291 break;
8293 case '.':
8294 fputs (reg_names[GP_REG_FIRST + 0], file);
8295 break;
8297 case '@':
8298 fputs (reg_names[AT_REGNUM], file);
8299 break;
8301 case '^':
8302 fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
8303 break;
8305 case '+':
8306 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
8307 break;
8309 case '$':
8310 fputs (reg_names[STACK_POINTER_REGNUM], file);
8311 break;
8313 case ':':
8314 /* When final_sequence is 0, the delay slot will be a nop. We can
8315 use the compact version for microMIPS. */
8316 if (final_sequence == 0)
8317 putc ('c', file);
8318 break;
8320 case '!':
8321 /* If the delay slot instruction is short, then use the
8322 compact version. */
8323 if (final_sequence == 0
8324 || get_attr_length (final_sequence->insn (1)) == 2)
8325 putc ('s', file);
8326 break;
8328 default:
8329 gcc_unreachable ();
8330 break;
8334 /* Initialize mips_print_operand_punct. */
8336 static void
8337 mips_init_print_operand_punct (void)
8339 const char *p;
8341 for (p = "()[]<>*#/?~.@^+$:!"; *p; p++)
8342 mips_print_operand_punct[(unsigned char) *p] = true;
8345 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
8346 associated with condition CODE. Print the condition part of the
8347 opcode to FILE. */
8349 static void
8350 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
8352 switch (code)
8354 case EQ:
8355 case NE:
8356 case GT:
8357 case GE:
8358 case LT:
8359 case LE:
8360 case GTU:
8361 case GEU:
8362 case LTU:
8363 case LEU:
8364 /* Conveniently, the MIPS names for these conditions are the same
8365 as their RTL equivalents. */
8366 fputs (GET_RTX_NAME (code), file);
8367 break;
8369 default:
8370 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8371 break;
8375 /* Likewise floating-point branches. */
8377 static void
8378 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
8380 switch (code)
8382 case EQ:
8383 if (ISA_HAS_CCF)
8384 fputs ("c1eqz", file);
8385 else
8386 fputs ("c1f", file);
8387 break;
8389 case NE:
8390 if (ISA_HAS_CCF)
8391 fputs ("c1nez", file);
8392 else
8393 fputs ("c1t", file);
8394 break;
8396 default:
8397 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8398 break;
8402 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
8404 static bool
8405 mips_print_operand_punct_valid_p (unsigned char code)
8407 return mips_print_operand_punct[code];
8410 /* Implement TARGET_PRINT_OPERAND. The MIPS-specific operand codes are:
8412 'X' Print CONST_INT OP in hexadecimal format.
8413 'x' Print the low 16 bits of CONST_INT OP in hexadecimal format.
8414 'd' Print CONST_INT OP in decimal.
8415 'm' Print one less than CONST_INT OP in decimal.
8416 'h' Print the high-part relocation associated with OP, after stripping
8417 any outermost HIGH.
8418 'R' Print the low-part relocation associated with OP.
8419 'C' Print the integer branch condition for comparison OP.
8420 'N' Print the inverse of the integer branch condition for comparison OP.
8421 'F' Print the FPU branch condition for comparison OP.
8422 'W' Print the inverse of the FPU branch condition for comparison OP.
8423 'T' Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
8424 'z' for (eq:?I ...), 'n' for (ne:?I ...).
8425 't' Like 'T', but with the EQ/NE cases reversed
8426 'Y' Print mips_fp_conditions[INTVAL (OP)]
8427 'Z' Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
8428 'q' Print a DSP accumulator register.
8429 'D' Print the second part of a double-word register or memory operand.
8430 'L' Print the low-order register in a double-word register operand.
8431 'M' Print high-order register in a double-word register operand.
8432 'z' Print $0 if OP is zero, otherwise print OP normally.
8433 'b' Print the address of a memory operand, without offset. */
8435 static void
8436 mips_print_operand (FILE *file, rtx op, int letter)
8438 enum rtx_code code;
8440 if (mips_print_operand_punct_valid_p (letter))
8442 mips_print_operand_punctuation (file, letter);
8443 return;
8446 gcc_assert (op);
8447 code = GET_CODE (op);
8449 switch (letter)
8451 case 'X':
8452 if (CONST_INT_P (op))
8453 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
8454 else
8455 output_operand_lossage ("invalid use of '%%%c'", letter);
8456 break;
8458 case 'x':
8459 if (CONST_INT_P (op))
8460 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
8461 else
8462 output_operand_lossage ("invalid use of '%%%c'", letter);
8463 break;
8465 case 'd':
8466 if (CONST_INT_P (op))
8467 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
8468 else
8469 output_operand_lossage ("invalid use of '%%%c'", letter);
8470 break;
8472 case 'm':
8473 if (CONST_INT_P (op))
8474 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
8475 else
8476 output_operand_lossage ("invalid use of '%%%c'", letter);
8477 break;
8479 case 'h':
8480 if (code == HIGH)
8481 op = XEXP (op, 0);
8482 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
8483 break;
8485 case 'R':
8486 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
8487 break;
8489 case 'C':
8490 mips_print_int_branch_condition (file, code, letter);
8491 break;
8493 case 'N':
8494 mips_print_int_branch_condition (file, reverse_condition (code), letter);
8495 break;
8497 case 'F':
8498 mips_print_float_branch_condition (file, code, letter);
8499 break;
8501 case 'W':
8502 mips_print_float_branch_condition (file, reverse_condition (code),
8503 letter);
8504 break;
8506 case 'T':
8507 case 't':
8509 int truth = (code == NE) == (letter == 'T');
8510 fputc ("zfnt"[truth * 2 + ST_REG_P (REGNO (XEXP (op, 0)))], file);
8512 break;
8514 case 'Y':
8515 if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
8516 fputs (mips_fp_conditions[UINTVAL (op)], file);
8517 else
8518 output_operand_lossage ("'%%%c' is not a valid operand prefix",
8519 letter);
8520 break;
8522 case 'Z':
8523 if (ISA_HAS_8CC || ISA_HAS_CCF)
8525 mips_print_operand (file, op, 0);
8526 fputc (',', file);
8528 break;
8530 case 'q':
8531 if (code == REG && MD_REG_P (REGNO (op)))
8532 fprintf (file, "$ac0");
8533 else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
8534 fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
8535 else
8536 output_operand_lossage ("invalid use of '%%%c'", letter);
8537 break;
8539 default:
8540 switch (code)
8542 case REG:
8544 unsigned int regno = REGNO (op);
8545 if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
8546 || (letter == 'L' && TARGET_BIG_ENDIAN)
8547 || letter == 'D')
8548 regno++;
8549 else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
8550 output_operand_lossage ("invalid use of '%%%c'", letter);
8551 /* We need to print $0 .. $31 for COP0 registers. */
8552 if (COP0_REG_P (regno))
8553 fprintf (file, "$%s", &reg_names[regno][4]);
8554 else
8555 fprintf (file, "%s", reg_names[regno]);
8557 break;
8559 case MEM:
8560 if (letter == 'D')
8561 output_address (plus_constant (Pmode, XEXP (op, 0), 4));
8562 else if (letter == 'b')
8564 gcc_assert (REG_P (XEXP (op, 0)));
8565 mips_print_operand (file, XEXP (op, 0), 0);
8567 else if (letter && letter != 'z')
8568 output_operand_lossage ("invalid use of '%%%c'", letter);
8569 else
8570 output_address (XEXP (op, 0));
8571 break;
8573 default:
8574 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
8575 fputs (reg_names[GP_REG_FIRST], file);
8576 else if (letter && letter != 'z')
8577 output_operand_lossage ("invalid use of '%%%c'", letter);
8578 else if (CONST_GP_P (op))
8579 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
8580 else
8581 output_addr_const (file, mips_strip_unspec_address (op));
8582 break;
8587 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
8589 static void
8590 mips_print_operand_address (FILE *file, rtx x)
8592 struct mips_address_info addr;
8594 if (mips_classify_address (&addr, x, word_mode, true))
8595 switch (addr.type)
8597 case ADDRESS_REG:
8598 mips_print_operand (file, addr.offset, 0);
8599 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8600 return;
8602 case ADDRESS_LO_SUM:
8603 mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
8604 mips_lo_relocs);
8605 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8606 return;
8608 case ADDRESS_CONST_INT:
8609 output_addr_const (file, x);
8610 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
8611 return;
8613 case ADDRESS_SYMBOLIC:
8614 output_addr_const (file, mips_strip_unspec_address (x));
8615 return;
8617 gcc_unreachable ();
8620 /* Implement TARGET_ENCODE_SECTION_INFO. */
8622 static void
8623 mips_encode_section_info (tree decl, rtx rtl, int first)
8625 default_encode_section_info (decl, rtl, first);
8627 if (TREE_CODE (decl) == FUNCTION_DECL)
8629 rtx symbol = XEXP (rtl, 0);
8630 tree type = TREE_TYPE (decl);
8632 /* Encode whether the symbol is short or long. */
8633 if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
8634 || mips_far_type_p (type))
8635 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
8639 /* Implement TARGET_SELECT_RTX_SECTION. */
8641 static section *
8642 mips_select_rtx_section (machine_mode mode, rtx x,
8643 unsigned HOST_WIDE_INT align)
8645 /* ??? Consider using mergeable small data sections. */
8646 if (mips_rtx_constant_in_small_data_p (mode))
8647 return get_named_section (NULL, ".sdata", 0);
8649 return default_elf_select_rtx_section (mode, x, align);
8652 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
8654 The complication here is that, with the combination TARGET_ABICALLS
8655 && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
8656 absolute addresses, and should therefore not be included in the
8657 read-only part of a DSO. Handle such cases by selecting a normal
8658 data section instead of a read-only one. The logic apes that in
8659 default_function_rodata_section. */
8661 static section *
8662 mips_function_rodata_section (tree decl)
8664 if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
8665 return default_function_rodata_section (decl);
8667 if (decl && DECL_SECTION_NAME (decl))
8669 const char *name = DECL_SECTION_NAME (decl);
8670 if (DECL_COMDAT_GROUP (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
8672 char *rname = ASTRDUP (name);
8673 rname[14] = 'd';
8674 return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
8676 else if (flag_function_sections
8677 && flag_data_sections
8678 && strncmp (name, ".text.", 6) == 0)
8680 char *rname = ASTRDUP (name);
8681 memcpy (rname + 1, "data", 4);
8682 return get_section (rname, SECTION_WRITE, decl);
8685 return data_section;
8688 /* Implement TARGET_IN_SMALL_DATA_P. */
8690 static bool
8691 mips_in_small_data_p (const_tree decl)
8693 unsigned HOST_WIDE_INT size;
8695 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
8696 return false;
8698 /* We don't yet generate small-data references for -mabicalls
8699 or VxWorks RTP code. See the related -G handling in
8700 mips_option_override. */
8701 if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
8702 return false;
8704 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
8706 const char *name;
8708 /* Reject anything that isn't in a known small-data section. */
8709 name = DECL_SECTION_NAME (decl);
8710 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
8711 return false;
8713 /* If a symbol is defined externally, the assembler will use the
8714 usual -G rules when deciding how to implement macros. */
8715 if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
8716 return true;
8718 else if (TARGET_EMBEDDED_DATA)
8720 /* Don't put constants into the small data section: we want them
8721 to be in ROM rather than RAM. */
8722 if (TREE_CODE (decl) != VAR_DECL)
8723 return false;
8725 if (TREE_READONLY (decl)
8726 && !TREE_SIDE_EFFECTS (decl)
8727 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
8728 return false;
8731 /* Enforce -mlocal-sdata. */
8732 if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
8733 return false;
8735 /* Enforce -mextern-sdata. */
8736 if (!TARGET_EXTERN_SDATA && DECL_P (decl))
8738 if (DECL_EXTERNAL (decl))
8739 return false;
8740 if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
8741 return false;
8744 /* We have traditionally not treated zero-sized objects as small data,
8745 so this is now effectively part of the ABI. */
8746 size = int_size_in_bytes (TREE_TYPE (decl));
8747 return size > 0 && size <= mips_small_data_threshold;
8750 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P. We don't want to use
8751 anchors for small data: the GP register acts as an anchor in that
8752 case. We also don't want to use them for PC-relative accesses,
8753 where the PC acts as an anchor. */
8755 static bool
8756 mips_use_anchors_for_symbol_p (const_rtx symbol)
8758 switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
8760 case SYMBOL_PC_RELATIVE:
8761 case SYMBOL_GP_RELATIVE:
8762 return false;
8764 default:
8765 return default_use_anchors_for_symbol_p (symbol);
8769 /* The MIPS debug format wants all automatic variables and arguments
8770 to be in terms of the virtual frame pointer (stack pointer before
8771 any adjustment in the function), while the MIPS 3.0 linker wants
8772 the frame pointer to be the stack pointer after the initial
8773 adjustment. So, we do the adjustment here. The arg pointer (which
8774 is eliminated) points to the virtual frame pointer, while the frame
8775 pointer (which may be eliminated) points to the stack pointer after
8776 the initial adjustments. */
8778 HOST_WIDE_INT
8779 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
8781 rtx offset2 = const0_rtx;
8782 rtx reg = eliminate_constant_term (addr, &offset2);
8784 if (offset == 0)
8785 offset = INTVAL (offset2);
8787 if (reg == stack_pointer_rtx
8788 || reg == frame_pointer_rtx
8789 || reg == hard_frame_pointer_rtx)
8791 offset -= cfun->machine->frame.total_size;
8792 if (reg == hard_frame_pointer_rtx)
8793 offset += cfun->machine->frame.hard_frame_pointer_offset;
8796 return offset;
8799 /* Implement ASM_OUTPUT_EXTERNAL. */
8801 void
8802 mips_output_external (FILE *file, tree decl, const char *name)
8804 default_elf_asm_output_external (file, decl, name);
8806 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8807 set in order to avoid putting out names that are never really
8808 used. */
8809 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
8811 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
8813 /* When using assembler macros, emit .extern directives for
8814 all small-data externs so that the assembler knows how
8815 big they are.
8817 In most cases it would be safe (though pointless) to emit
8818 .externs for other symbols too. One exception is when an
8819 object is within the -G limit but declared by the user to
8820 be in a section other than .sbss or .sdata. */
8821 fputs ("\t.extern\t", file);
8822 assemble_name (file, name);
8823 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
8824 int_size_in_bytes (TREE_TYPE (decl)));
8829 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8831 static void
8832 mips_output_filename (FILE *stream, const char *name)
8834 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
8835 directives. */
8836 if (write_symbols == DWARF2_DEBUG)
8837 return;
8838 else if (mips_output_filename_first_time)
8840 mips_output_filename_first_time = 0;
8841 num_source_filenames += 1;
8842 current_function_file = name;
8843 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8844 output_quoted_string (stream, name);
8845 putc ('\n', stream);
8847 /* If we are emitting stabs, let dbxout.c handle this (except for
8848 the mips_output_filename_first_time case). */
8849 else if (write_symbols == DBX_DEBUG)
8850 return;
8851 else if (name != current_function_file
8852 && strcmp (name, current_function_file) != 0)
8854 num_source_filenames += 1;
8855 current_function_file = name;
8856 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8857 output_quoted_string (stream, name);
8858 putc ('\n', stream);
8862 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL. */
8864 static void ATTRIBUTE_UNUSED
8865 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
8867 switch (size)
8869 case 4:
8870 fputs ("\t.dtprelword\t", file);
8871 break;
8873 case 8:
8874 fputs ("\t.dtpreldword\t", file);
8875 break;
8877 default:
8878 gcc_unreachable ();
8880 output_addr_const (file, x);
8881 fputs ("+0x8000", file);
8884 /* Implement TARGET_DWARF_REGISTER_SPAN. */
8886 static rtx
8887 mips_dwarf_register_span (rtx reg)
8889 rtx high, low;
8890 machine_mode mode;
8892 /* TARGET_FLOATXX is implemented as 32-bit floating-point registers but
8893 ensures that double-precision registers are treated as if they were
8894 64-bit physical registers. The code will run correctly with 32-bit or
8895 64-bit registers which means that dwarf information cannot be precise
8896 for all scenarios. We choose to state that the 64-bit values are stored
8897 in a single 64-bit 'piece'. This slightly unusual construct can then be
8898 interpreted as either a pair of registers if the registers are 32-bit or
8899 a single 64-bit register depending on hardware. */
8900 mode = GET_MODE (reg);
8901 if (FP_REG_P (REGNO (reg))
8902 && TARGET_FLOATXX
8903 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8905 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, reg));
8907 /* By default, GCC maps increasing register numbers to increasing
8908 memory locations, but paired FPRs are always little-endian,
8909 regardless of the prevailing endianness. */
8910 else if (FP_REG_P (REGNO (reg))
8911 && TARGET_BIG_ENDIAN
8912 && MAX_FPRS_PER_FMT > 1
8913 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8915 gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
8916 high = mips_subword (reg, true);
8917 low = mips_subword (reg, false);
8918 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
8921 return NULL_RTX;
8924 /* Implement TARGET_DWARF_FRAME_REG_MODE. */
8926 static machine_mode
8927 mips_dwarf_frame_reg_mode (int regno)
8929 machine_mode mode = default_dwarf_frame_reg_mode (regno);
8931 if (FP_REG_P (regno) && mips_abi == ABI_32 && TARGET_FLOAT64)
8932 mode = SImode;
8934 return mode;
8937 /* DSP ALU can bypass data with no delays for the following pairs. */
8938 enum insn_code dspalu_bypass_table[][2] =
8940 {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
8941 {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
8942 {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
8943 {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
8944 {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
8945 {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
8946 {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
8947 {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
8951 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
8953 int i;
8954 int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
8955 enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
8956 enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
8958 for (i = 0; i < num_bypass; i++)
8960 if (out_icode == dspalu_bypass_table[i][0]
8961 && in_icode == dspalu_bypass_table[i][1])
8962 return true;
8965 return false;
8967 /* Implement ASM_OUTPUT_ASCII. */
8969 void
8970 mips_output_ascii (FILE *stream, const char *string, size_t len)
8972 size_t i;
8973 int cur_pos;
8975 cur_pos = 17;
8976 fprintf (stream, "\t.ascii\t\"");
8977 for (i = 0; i < len; i++)
8979 int c;
8981 c = (unsigned char) string[i];
8982 if (ISPRINT (c))
8984 if (c == '\\' || c == '\"')
8986 putc ('\\', stream);
8987 cur_pos++;
8989 putc (c, stream);
8990 cur_pos++;
8992 else
8994 fprintf (stream, "\\%03o", c);
8995 cur_pos += 4;
8998 if (cur_pos > 72 && i+1 < len)
9000 cur_pos = 17;
9001 fprintf (stream, "\"\n\t.ascii\t\"");
9004 fprintf (stream, "\"\n");
9007 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
9008 Update *ADDR with the operand that should be printed. */
9010 const char *
9011 mips_output_tls_reloc_directive (rtx *addr)
9013 enum mips_symbol_type type;
9015 type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
9016 *addr = mips_strip_unspec_address (*addr);
9017 switch (type)
9019 case SYMBOL_DTPREL:
9020 return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
9022 case SYMBOL_TPREL:
9023 return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
9025 default:
9026 gcc_unreachable ();
9030 /* Emit either a label, .comm, or .lcomm directive. When using assembler
9031 macros, mark the symbol as written so that mips_asm_output_external
9032 won't emit an .extern for it. STREAM is the output file, NAME is the
9033 name of the symbol, INIT_STRING is the string that should be written
9034 before the symbol and FINAL_STRING is the string that should be
9035 written after it. FINAL_STRING is a printf format that consumes the
9036 remaining arguments. */
9038 void
9039 mips_declare_object (FILE *stream, const char *name, const char *init_string,
9040 const char *final_string, ...)
9042 va_list ap;
9044 fputs (init_string, stream);
9045 assemble_name (stream, name);
9046 va_start (ap, final_string);
9047 vfprintf (stream, final_string, ap);
9048 va_end (ap);
9050 if (!TARGET_EXPLICIT_RELOCS)
9052 tree name_tree = get_identifier (name);
9053 TREE_ASM_WRITTEN (name_tree) = 1;
9057 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
9058 NAME is the name of the object and ALIGN is the required alignment
9059 in bytes. TAKES_ALIGNMENT_P is true if the directive takes a third
9060 alignment argument. */
9062 void
9063 mips_declare_common_object (FILE *stream, const char *name,
9064 const char *init_string,
9065 unsigned HOST_WIDE_INT size,
9066 unsigned int align, bool takes_alignment_p)
9068 if (!takes_alignment_p)
9070 size += (align / BITS_PER_UNIT) - 1;
9071 size -= size % (align / BITS_PER_UNIT);
9072 mips_declare_object (stream, name, init_string,
9073 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
9075 else
9076 mips_declare_object (stream, name, init_string,
9077 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
9078 size, align / BITS_PER_UNIT);
9081 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as the
9082 elfos.h version, but we also need to handle -muninit-const-in-rodata. */
9084 void
9085 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
9086 unsigned HOST_WIDE_INT size,
9087 unsigned int align)
9089 /* If the target wants uninitialized const declarations in
9090 .rdata then don't put them in .comm. */
9091 if (TARGET_EMBEDDED_DATA
9092 && TARGET_UNINIT_CONST_IN_RODATA
9093 && TREE_CODE (decl) == VAR_DECL
9094 && TREE_READONLY (decl)
9095 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
9097 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
9098 targetm.asm_out.globalize_label (stream, name);
9100 switch_to_section (readonly_data_section);
9101 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
9102 mips_declare_object (stream, name, "",
9103 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
9104 size);
9106 else
9107 mips_declare_common_object (stream, name, "\n\t.comm\t",
9108 size, align, true);
9111 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
9112 extern int size_directive_output;
9114 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
9115 definitions except that it uses mips_declare_object to emit the label. */
9117 void
9118 mips_declare_object_name (FILE *stream, const char *name,
9119 tree decl ATTRIBUTE_UNUSED)
9121 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
9122 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
9123 #endif
9125 size_directive_output = 0;
9126 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
9128 HOST_WIDE_INT size;
9130 size_directive_output = 1;
9131 size = int_size_in_bytes (TREE_TYPE (decl));
9132 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
9135 mips_declare_object (stream, name, "", ":\n");
9138 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
9140 void
9141 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
9143 const char *name;
9145 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
9146 if (!flag_inhibit_size_directive
9147 && DECL_SIZE (decl) != 0
9148 && !at_end
9149 && top_level
9150 && DECL_INITIAL (decl) == error_mark_node
9151 && !size_directive_output)
9153 HOST_WIDE_INT size;
9155 size_directive_output = 1;
9156 size = int_size_in_bytes (TREE_TYPE (decl));
9157 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
9160 #endif
9162 /* Return the FOO in the name of the ".mdebug.FOO" section associated
9163 with the current ABI. */
9165 static const char *
9166 mips_mdebug_abi_name (void)
9168 switch (mips_abi)
9170 case ABI_32:
9171 return "abi32";
9172 case ABI_O64:
9173 return "abiO64";
9174 case ABI_N32:
9175 return "abiN32";
9176 case ABI_64:
9177 return "abi64";
9178 case ABI_EABI:
9179 return TARGET_64BIT ? "eabi64" : "eabi32";
9180 default:
9181 gcc_unreachable ();
9185 /* Implement TARGET_ASM_FILE_START. */
9187 static void
9188 mips_file_start (void)
9190 default_file_start ();
9192 /* Generate a special section to describe the ABI switches used to
9193 produce the resultant binary. */
9195 /* Record the ABI itself. Modern versions of binutils encode
9196 this information in the ELF header flags, but GDB needs the
9197 information in order to correctly debug binaries produced by
9198 older binutils. See the function mips_gdbarch_init in
9199 gdb/mips-tdep.c. */
9200 fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
9201 mips_mdebug_abi_name ());
9203 /* There is no ELF header flag to distinguish long32 forms of the
9204 EABI from long64 forms. Emit a special section to help tools
9205 such as GDB. Do the same for o64, which is sometimes used with
9206 -mlong64. */
9207 if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
9208 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
9209 "\t.previous\n", TARGET_LONG64 ? 64 : 32);
9211 /* Record the NaN encoding. */
9212 if (HAVE_AS_NAN || mips_nan != MIPS_IEEE_754_DEFAULT)
9213 fprintf (asm_out_file, "\t.nan\t%s\n",
9214 mips_nan == MIPS_IEEE_754_2008 ? "2008" : "legacy");
9216 #ifdef HAVE_AS_DOT_MODULE
9217 /* Record the FP ABI. See below for comments. */
9218 if (TARGET_NO_FLOAT)
9219 #ifdef HAVE_AS_GNU_ATTRIBUTE
9220 fputs ("\t.gnu_attribute 4, 0\n", asm_out_file);
9221 #else
9223 #endif
9224 else if (!TARGET_HARD_FLOAT_ABI)
9225 fputs ("\t.module\tsoftfloat\n", asm_out_file);
9226 else if (!TARGET_DOUBLE_FLOAT)
9227 fputs ("\t.module\tsinglefloat\n", asm_out_file);
9228 else if (TARGET_FLOATXX)
9229 fputs ("\t.module\tfp=xx\n", asm_out_file);
9230 else if (TARGET_FLOAT64)
9231 fputs ("\t.module\tfp=64\n", asm_out_file);
9232 else
9233 fputs ("\t.module\tfp=32\n", asm_out_file);
9235 if (TARGET_ODD_SPREG)
9236 fputs ("\t.module\toddspreg\n", asm_out_file);
9237 else
9238 fputs ("\t.module\tnooddspreg\n", asm_out_file);
9240 #else
9241 #ifdef HAVE_AS_GNU_ATTRIBUTE
9243 int attr;
9245 /* No floating-point operations, -mno-float. */
9246 if (TARGET_NO_FLOAT)
9247 attr = 0;
9248 /* Soft-float code, -msoft-float. */
9249 else if (!TARGET_HARD_FLOAT_ABI)
9250 attr = 3;
9251 /* Single-float code, -msingle-float. */
9252 else if (!TARGET_DOUBLE_FLOAT)
9253 attr = 2;
9254 /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64.
9255 Reserved attr=4.
9256 This case used 12 callee-saved double-precision registers
9257 and is deprecated. */
9258 /* 64-bit or 32-bit FP registers on a 32-bit target, -mfpxx. */
9259 else if (TARGET_FLOATXX)
9260 attr = 5;
9261 /* 64-bit FP registers on a 32-bit target, -mfp64 -modd-spreg. */
9262 else if (mips_abi == ABI_32 && TARGET_FLOAT64 && TARGET_ODD_SPREG)
9263 attr = 6;
9264 /* 64-bit FP registers on a 32-bit target, -mfp64 -mno-odd-spreg. */
9265 else if (mips_abi == ABI_32 && TARGET_FLOAT64)
9266 attr = 7;
9267 /* Regular FP code, FP regs same size as GP regs, -mdouble-float. */
9268 else
9269 attr = 1;
9271 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
9273 #endif
9274 #endif
9276 /* If TARGET_ABICALLS, tell GAS to generate -KPIC code. */
9277 if (TARGET_ABICALLS)
9279 fprintf (asm_out_file, "\t.abicalls\n");
9280 if (TARGET_ABICALLS_PIC0)
9281 fprintf (asm_out_file, "\t.option\tpic0\n");
9284 if (flag_verbose_asm)
9285 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
9286 ASM_COMMENT_START,
9287 mips_small_data_threshold, mips_arch_info->name, mips_isa);
9290 /* Implement TARGET_ASM_CODE_END. */
9292 static void
9293 mips_code_end (void)
9295 mips_finish_stub (&mips16_rdhwr_stub);
9296 mips_finish_stub (&mips16_get_fcsr_stub);
9297 mips_finish_stub (&mips16_set_fcsr_stub);
9300 /* Make the last instruction frame-related and note that it performs
9301 the operation described by FRAME_PATTERN. */
9303 static void
9304 mips_set_frame_expr (rtx frame_pattern)
9306 rtx_insn *insn;
9308 insn = get_last_insn ();
9309 RTX_FRAME_RELATED_P (insn) = 1;
9310 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9311 frame_pattern,
9312 REG_NOTES (insn));
9315 /* Return a frame-related rtx that stores REG at MEM.
9316 REG must be a single register. */
9318 static rtx
9319 mips_frame_set (rtx mem, rtx reg)
9321 rtx set;
9323 set = gen_rtx_SET (VOIDmode, mem, reg);
9324 RTX_FRAME_RELATED_P (set) = 1;
9326 return set;
9329 /* Record that the epilogue has restored call-saved register REG. */
9331 static void
9332 mips_add_cfa_restore (rtx reg)
9334 mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
9335 mips_epilogue.cfa_restores);
9338 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
9339 mips16e_s2_s8_regs[X], it must also save the registers in indexes
9340 X + 1 onwards. Likewise mips16e_a0_a3_regs. */
9341 static const unsigned char mips16e_s2_s8_regs[] = {
9342 30, 23, 22, 21, 20, 19, 18
9344 static const unsigned char mips16e_a0_a3_regs[] = {
9345 4, 5, 6, 7
9348 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
9349 ordered from the uppermost in memory to the lowest in memory. */
9350 static const unsigned char mips16e_save_restore_regs[] = {
9351 31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
9354 /* Return the index of the lowest X in the range [0, SIZE) for which
9355 bit REGS[X] is set in MASK. Return SIZE if there is no such X. */
9357 static unsigned int
9358 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
9359 unsigned int size)
9361 unsigned int i;
9363 for (i = 0; i < size; i++)
9364 if (BITSET_P (mask, regs[i]))
9365 break;
9367 return i;
9370 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
9371 is the number of set bits. If *MASK_PTR contains REGS[X] for some X
9372 in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
9373 is true for all indexes (X, SIZE). */
9375 static void
9376 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
9377 unsigned int size, unsigned int *num_regs_ptr)
9379 unsigned int i;
9381 i = mips16e_find_first_register (*mask_ptr, regs, size);
9382 for (i++; i < size; i++)
9383 if (!BITSET_P (*mask_ptr, regs[i]))
9385 *num_regs_ptr += 1;
9386 *mask_ptr |= 1 << regs[i];
9390 /* Return a simplified form of X using the register values in REG_VALUES.
9391 REG_VALUES[R] is the last value assigned to hard register R, or null
9392 if R has not been modified.
9394 This function is rather limited, but is good enough for our purposes. */
9396 static rtx
9397 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
9399 x = avoid_constant_pool_reference (x);
9401 if (UNARY_P (x))
9403 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9404 return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
9405 x0, GET_MODE (XEXP (x, 0)));
9408 if (ARITHMETIC_P (x))
9410 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9411 rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
9412 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
9415 if (REG_P (x)
9416 && reg_values[REGNO (x)]
9417 && !rtx_unstable_p (reg_values[REGNO (x)]))
9418 return reg_values[REGNO (x)];
9420 return x;
9423 /* Return true if (set DEST SRC) stores an argument register into its
9424 caller-allocated save slot, storing the number of that argument
9425 register in *REGNO_PTR if so. REG_VALUES is as for
9426 mips16e_collect_propagate_value. */
9428 static bool
9429 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
9430 unsigned int *regno_ptr)
9432 unsigned int argno, regno;
9433 HOST_WIDE_INT offset, required_offset;
9434 rtx addr, base;
9436 /* Check that this is a word-mode store. */
9437 if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
9438 return false;
9440 /* Check that the register being saved is an unmodified argument
9441 register. */
9442 regno = REGNO (src);
9443 if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
9444 return false;
9445 argno = regno - GP_ARG_FIRST;
9447 /* Check whether the address is an appropriate stack-pointer or
9448 frame-pointer access. */
9449 addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
9450 mips_split_plus (addr, &base, &offset);
9451 required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
9452 if (base == hard_frame_pointer_rtx)
9453 required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
9454 else if (base != stack_pointer_rtx)
9455 return false;
9456 if (offset != required_offset)
9457 return false;
9459 *regno_ptr = regno;
9460 return true;
9463 /* A subroutine of mips_expand_prologue, called only when generating
9464 MIPS16e SAVE instructions. Search the start of the function for any
9465 instructions that save argument registers into their caller-allocated
9466 save slots. Delete such instructions and return a value N such that
9467 saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
9468 instructions redundant. */
9470 static unsigned int
9471 mips16e_collect_argument_saves (void)
9473 rtx reg_values[FIRST_PSEUDO_REGISTER];
9474 rtx_insn *insn, *next;
9475 rtx set, dest, src;
9476 unsigned int nargs, regno;
9478 push_topmost_sequence ();
9479 nargs = 0;
9480 memset (reg_values, 0, sizeof (reg_values));
9481 for (insn = get_insns (); insn; insn = next)
9483 next = NEXT_INSN (insn);
9484 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
9485 continue;
9487 if (!INSN_P (insn))
9488 break;
9490 set = PATTERN (insn);
9491 if (GET_CODE (set) != SET)
9492 break;
9494 dest = SET_DEST (set);
9495 src = SET_SRC (set);
9496 if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
9498 if (!BITSET_P (cfun->machine->frame.mask, regno))
9500 delete_insn (insn);
9501 nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
9504 else if (REG_P (dest) && GET_MODE (dest) == word_mode)
9505 reg_values[REGNO (dest)]
9506 = mips16e_collect_propagate_value (src, reg_values);
9507 else
9508 break;
9510 pop_topmost_sequence ();
9512 return nargs;
9515 /* Return a move between register REGNO and memory location SP + OFFSET.
9516 REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
9517 Make the move a load if RESTORE_P, otherwise make it a store. */
9519 static rtx
9520 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
9521 HOST_WIDE_INT offset, unsigned int regno)
9523 rtx reg, mem;
9525 mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
9526 offset));
9527 reg = gen_rtx_REG (SImode, regno);
9528 if (restore_p)
9530 mips_add_cfa_restore (reg);
9531 return gen_rtx_SET (VOIDmode, reg, mem);
9533 if (reg_parm_p)
9534 return gen_rtx_SET (VOIDmode, mem, reg);
9535 return mips_frame_set (mem, reg);
9538 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
9539 The instruction must:
9541 - Allocate or deallocate SIZE bytes in total; SIZE is known
9542 to be nonzero.
9544 - Save or restore as many registers in *MASK_PTR as possible.
9545 The instruction saves the first registers at the top of the
9546 allocated area, with the other registers below it.
9548 - Save NARGS argument registers above the allocated area.
9550 (NARGS is always zero if RESTORE_P.)
9552 The SAVE and RESTORE instructions cannot save and restore all general
9553 registers, so there may be some registers left over for the caller to
9554 handle. Destructively modify *MASK_PTR so that it contains the registers
9555 that still need to be saved or restored. The caller can save these
9556 registers in the memory immediately below *OFFSET_PTR, which is a
9557 byte offset from the bottom of the allocated stack area. */
9559 static rtx
9560 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
9561 HOST_WIDE_INT *offset_ptr, unsigned int nargs,
9562 HOST_WIDE_INT size)
9564 rtx pattern, set;
9565 HOST_WIDE_INT offset, top_offset;
9566 unsigned int i, regno;
9567 int n;
9569 gcc_assert (cfun->machine->frame.num_fp == 0);
9571 /* Calculate the number of elements in the PARALLEL. We need one element
9572 for the stack adjustment, one for each argument register save, and one
9573 for each additional register move. */
9574 n = 1 + nargs;
9575 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9576 if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
9577 n++;
9579 /* Create the final PARALLEL. */
9580 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
9581 n = 0;
9583 /* Add the stack pointer adjustment. */
9584 set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9585 plus_constant (Pmode, stack_pointer_rtx,
9586 restore_p ? size : -size));
9587 RTX_FRAME_RELATED_P (set) = 1;
9588 XVECEXP (pattern, 0, n++) = set;
9590 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
9591 top_offset = restore_p ? size : 0;
9593 /* Save the arguments. */
9594 for (i = 0; i < nargs; i++)
9596 offset = top_offset + i * UNITS_PER_WORD;
9597 set = mips16e_save_restore_reg (restore_p, true, offset,
9598 GP_ARG_FIRST + i);
9599 XVECEXP (pattern, 0, n++) = set;
9602 /* Then fill in the other register moves. */
9603 offset = top_offset;
9604 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9606 regno = mips16e_save_restore_regs[i];
9607 if (BITSET_P (*mask_ptr, regno))
9609 offset -= UNITS_PER_WORD;
9610 set = mips16e_save_restore_reg (restore_p, false, offset, regno);
9611 XVECEXP (pattern, 0, n++) = set;
9612 *mask_ptr &= ~(1 << regno);
9616 /* Tell the caller what offset it should use for the remaining registers. */
9617 *offset_ptr = size + (offset - top_offset);
9619 gcc_assert (n == XVECLEN (pattern, 0));
9621 return pattern;
9624 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
9625 pointer. Return true if PATTERN matches the kind of instruction
9626 generated by mips16e_build_save_restore. If INFO is nonnull,
9627 initialize it when returning true. */
9629 bool
9630 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
9631 struct mips16e_save_restore_info *info)
9633 unsigned int i, nargs, mask, extra;
9634 HOST_WIDE_INT top_offset, save_offset, offset;
9635 rtx set, reg, mem, base;
9636 int n;
9638 if (!GENERATE_MIPS16E_SAVE_RESTORE)
9639 return false;
9641 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
9642 top_offset = adjust > 0 ? adjust : 0;
9644 /* Interpret all other members of the PARALLEL. */
9645 save_offset = top_offset - UNITS_PER_WORD;
9646 mask = 0;
9647 nargs = 0;
9648 i = 0;
9649 for (n = 1; n < XVECLEN (pattern, 0); n++)
9651 /* Check that we have a SET. */
9652 set = XVECEXP (pattern, 0, n);
9653 if (GET_CODE (set) != SET)
9654 return false;
9656 /* Check that the SET is a load (if restoring) or a store
9657 (if saving). */
9658 mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
9659 if (!MEM_P (mem))
9660 return false;
9662 /* Check that the address is the sum of the stack pointer and a
9663 possibly-zero constant offset. */
9664 mips_split_plus (XEXP (mem, 0), &base, &offset);
9665 if (base != stack_pointer_rtx)
9666 return false;
9668 /* Check that SET's other operand is a register. */
9669 reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
9670 if (!REG_P (reg))
9671 return false;
9673 /* Check for argument saves. */
9674 if (offset == top_offset + nargs * UNITS_PER_WORD
9675 && REGNO (reg) == GP_ARG_FIRST + nargs)
9676 nargs++;
9677 else if (offset == save_offset)
9679 while (mips16e_save_restore_regs[i++] != REGNO (reg))
9680 if (i == ARRAY_SIZE (mips16e_save_restore_regs))
9681 return false;
9683 mask |= 1 << REGNO (reg);
9684 save_offset -= UNITS_PER_WORD;
9686 else
9687 return false;
9690 /* Check that the restrictions on register ranges are met. */
9691 extra = 0;
9692 mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
9693 ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
9694 mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
9695 ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
9696 if (extra != 0)
9697 return false;
9699 /* Make sure that the topmost argument register is not saved twice.
9700 The checks above ensure that the same is then true for the other
9701 argument registers. */
9702 if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
9703 return false;
9705 /* Pass back information, if requested. */
9706 if (info)
9708 info->nargs = nargs;
9709 info->mask = mask;
9710 info->size = (adjust > 0 ? adjust : -adjust);
9713 return true;
9716 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
9717 for the register range [MIN_REG, MAX_REG]. Return a pointer to
9718 the null terminator. */
9720 static char *
9721 mips16e_add_register_range (char *s, unsigned int min_reg,
9722 unsigned int max_reg)
9724 if (min_reg != max_reg)
9725 s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
9726 else
9727 s += sprintf (s, ",%s", reg_names[min_reg]);
9728 return s;
9731 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
9732 PATTERN and ADJUST are as for mips16e_save_restore_pattern_p. */
9734 const char *
9735 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
9737 static char buffer[300];
9739 struct mips16e_save_restore_info info;
9740 unsigned int i, end;
9741 char *s;
9743 /* Parse the pattern. */
9744 if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
9745 gcc_unreachable ();
9747 /* Add the mnemonic. */
9748 s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
9749 s += strlen (s);
9751 /* Save the arguments. */
9752 if (info.nargs > 1)
9753 s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
9754 reg_names[GP_ARG_FIRST + info.nargs - 1]);
9755 else if (info.nargs == 1)
9756 s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
9758 /* Emit the amount of stack space to allocate or deallocate. */
9759 s += sprintf (s, "%d", (int) info.size);
9761 /* Save or restore $16. */
9762 if (BITSET_P (info.mask, 16))
9763 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
9765 /* Save or restore $17. */
9766 if (BITSET_P (info.mask, 17))
9767 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
9769 /* Save or restore registers in the range $s2...$s8, which
9770 mips16e_s2_s8_regs lists in decreasing order. Note that this
9771 is a software register range; the hardware registers are not
9772 numbered consecutively. */
9773 end = ARRAY_SIZE (mips16e_s2_s8_regs);
9774 i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
9775 if (i < end)
9776 s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
9777 mips16e_s2_s8_regs[i]);
9779 /* Save or restore registers in the range $a0...$a3. */
9780 end = ARRAY_SIZE (mips16e_a0_a3_regs);
9781 i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
9782 if (i < end)
9783 s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
9784 mips16e_a0_a3_regs[end - 1]);
9786 /* Save or restore $31. */
9787 if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
9788 s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
9790 return buffer;
9793 /* Return true if the current function returns its value in a floating-point
9794 register in MIPS16 mode. */
9796 static bool
9797 mips16_cfun_returns_in_fpr_p (void)
9799 tree return_type = DECL_RESULT (current_function_decl);
9800 return (TARGET_MIPS16
9801 && TARGET_HARD_FLOAT_ABI
9802 && !aggregate_value_p (return_type, current_function_decl)
9803 && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
9806 /* Return true if predicate PRED is true for at least one instruction.
9807 Cache the result in *CACHE, and assume that the result is true
9808 if *CACHE is already true. */
9810 static bool
9811 mips_find_gp_ref (bool *cache, bool (*pred) (rtx_insn *))
9813 rtx_insn *insn;
9815 if (!*cache)
9817 push_topmost_sequence ();
9818 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9819 if (USEFUL_INSN_P (insn) && pred (insn))
9821 *cache = true;
9822 break;
9824 pop_topmost_sequence ();
9826 return *cache;
9829 /* Return true if INSN refers to the global pointer in an "inflexible" way.
9830 See mips_cfun_has_inflexible_gp_ref_p for details. */
9832 static bool
9833 mips_insn_has_inflexible_gp_ref_p (rtx_insn *insn)
9835 /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
9836 indicate that the target could be a traditional MIPS
9837 lazily-binding stub. */
9838 return find_reg_fusage (insn, USE, pic_offset_table_rtx);
9841 /* Return true if the current function refers to the global pointer
9842 in a way that forces $28 to be valid. This means that we can't
9843 change the choice of global pointer, even for NewABI code.
9845 One example of this (and one which needs several checks) is that
9846 $28 must be valid when calling traditional MIPS lazy-binding stubs.
9847 (This restriction does not apply to PLTs.) */
9849 static bool
9850 mips_cfun_has_inflexible_gp_ref_p (void)
9852 /* If the function has a nonlocal goto, $28 must hold the correct
9853 global pointer for the target function. That is, the target
9854 of the goto implicitly uses $28. */
9855 if (crtl->has_nonlocal_goto)
9856 return true;
9858 if (TARGET_ABICALLS_PIC2)
9860 /* Symbolic accesses implicitly use the global pointer unless
9861 -mexplicit-relocs is in effect. JAL macros to symbolic addresses
9862 might go to traditional MIPS lazy-binding stubs. */
9863 if (!TARGET_EXPLICIT_RELOCS)
9864 return true;
9866 /* FUNCTION_PROFILER includes a JAL to _mcount, which again
9867 can be lazily-bound. */
9868 if (crtl->profile)
9869 return true;
9871 /* MIPS16 functions that return in FPRs need to call an
9872 external libgcc routine. This call is only made explict
9873 during mips_expand_epilogue, and it too might be lazily bound. */
9874 if (mips16_cfun_returns_in_fpr_p ())
9875 return true;
9878 return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
9879 mips_insn_has_inflexible_gp_ref_p);
9882 /* Return true if INSN refers to the global pointer in a "flexible" way.
9883 See mips_cfun_has_flexible_gp_ref_p for details. */
9885 static bool
9886 mips_insn_has_flexible_gp_ref_p (rtx_insn *insn)
9888 return (get_attr_got (insn) != GOT_UNSET
9889 || mips_small_data_pattern_p (PATTERN (insn))
9890 || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
9893 /* Return true if the current function references the global pointer,
9894 but if those references do not inherently require the global pointer
9895 to be $28. Assume !mips_cfun_has_inflexible_gp_ref_p (). */
9897 static bool
9898 mips_cfun_has_flexible_gp_ref_p (void)
9900 /* Reload can sometimes introduce constant pool references
9901 into a function that otherwise didn't need them. For example,
9902 suppose we have an instruction like:
9904 (set (reg:DF R1) (float:DF (reg:SI R2)))
9906 If R2 turns out to be a constant such as 1, the instruction may
9907 have a REG_EQUAL note saying that R1 == 1.0. Reload then has
9908 the option of using this constant if R2 doesn't get allocated
9909 to a register.
9911 In cases like these, reload will have added the constant to the
9912 pool but no instruction will yet refer to it. */
9913 if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
9914 return true;
9916 return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
9917 mips_insn_has_flexible_gp_ref_p);
9920 /* Return the register that should be used as the global pointer
9921 within this function. Return INVALID_REGNUM if the function
9922 doesn't need a global pointer. */
9924 static unsigned int
9925 mips_global_pointer (void)
9927 unsigned int regno;
9929 /* $gp is always available unless we're using a GOT. */
9930 if (!TARGET_USE_GOT)
9931 return GLOBAL_POINTER_REGNUM;
9933 /* If there are inflexible references to $gp, we must use the
9934 standard register. */
9935 if (mips_cfun_has_inflexible_gp_ref_p ())
9936 return GLOBAL_POINTER_REGNUM;
9938 /* If there are no current references to $gp, then the only uses
9939 we can introduce later are those involved in long branches. */
9940 if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
9941 return INVALID_REGNUM;
9943 /* If the global pointer is call-saved, try to use a call-clobbered
9944 alternative. */
9945 if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
9946 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9947 if (!df_regs_ever_live_p (regno)
9948 && call_really_used_regs[regno]
9949 && !fixed_regs[regno]
9950 && regno != PIC_FUNCTION_ADDR_REGNUM)
9951 return regno;
9953 return GLOBAL_POINTER_REGNUM;
9956 /* Return true if the current function's prologue must load the global
9957 pointer value into pic_offset_table_rtx and store the same value in
9958 the function's cprestore slot (if any).
9960 One problem we have to deal with is that, when emitting GOT-based
9961 position independent code, long-branch sequences will need to load
9962 the address of the branch target from the GOT. We don't know until
9963 the very end of compilation whether (and where) the function needs
9964 long branches, so we must ensure that _any_ branch can access the
9965 global pointer in some form. However, we do not want to pessimize
9966 the usual case in which all branches are short.
9968 We handle this as follows:
9970 (1) During reload, we set cfun->machine->global_pointer to
9971 INVALID_REGNUM if we _know_ that the current function
9972 doesn't need a global pointer. This is only valid if
9973 long branches don't need the GOT.
9975 Otherwise, we assume that we might need a global pointer
9976 and pick an appropriate register.
9978 (2) If cfun->machine->global_pointer != INVALID_REGNUM,
9979 we ensure that the global pointer is available at every
9980 block boundary bar entry and exit. We do this in one of two ways:
9982 - If the function has a cprestore slot, we ensure that this
9983 slot is valid at every branch. However, as explained in
9984 point (6) below, there is no guarantee that pic_offset_table_rtx
9985 itself is valid if new uses of the global pointer are introduced
9986 after the first post-epilogue split.
9988 We guarantee that the cprestore slot is valid by loading it
9989 into a fake register, CPRESTORE_SLOT_REGNUM. We then make
9990 this register live at every block boundary bar function entry
9991 and exit. It is then invalid to move the load (and thus the
9992 preceding store) across a block boundary.
9994 - If the function has no cprestore slot, we guarantee that
9995 pic_offset_table_rtx itself is valid at every branch.
9997 See mips_eh_uses for the handling of the register liveness.
9999 (3) During prologue and epilogue generation, we emit "ghost"
10000 placeholder instructions to manipulate the global pointer.
10002 (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
10003 and cfun->machine->must_restore_gp_when_clobbered_p if we already know
10004 that the function needs a global pointer. (There is no need to set
10005 them earlier than this, and doing it as late as possible leads to
10006 fewer false positives.)
10008 (5) If cfun->machine->must_initialize_gp_p is true during a
10009 split_insns pass, we split the ghost instructions into real
10010 instructions. These split instructions can then be optimized in
10011 the usual way. Otherwise, we keep the ghost instructions intact,
10012 and optimize for the case where they aren't needed. We still
10013 have the option of splitting them later, if we need to introduce
10014 new uses of the global pointer.
10016 For example, the scheduler ignores a ghost instruction that
10017 stores $28 to the stack, but it handles the split form of
10018 the ghost instruction as an ordinary store.
10020 (6) [OldABI only.] If cfun->machine->must_restore_gp_when_clobbered_p
10021 is true during the first post-epilogue split_insns pass, we split
10022 calls and restore_gp patterns into instructions that explicitly
10023 load pic_offset_table_rtx from the cprestore slot. Otherwise,
10024 we split these patterns into instructions that _don't_ load from
10025 the cprestore slot.
10027 If cfun->machine->must_restore_gp_when_clobbered_p is true at the
10028 time of the split, then any instructions that exist at that time
10029 can make free use of pic_offset_table_rtx. However, if we want
10030 to introduce new uses of the global pointer after the split,
10031 we must explicitly load the value from the cprestore slot, since
10032 pic_offset_table_rtx itself might not be valid at a given point
10033 in the function.
10035 The idea is that we want to be able to delete redundant
10036 loads from the cprestore slot in the usual case where no
10037 long branches are needed.
10039 (7) If cfun->machine->must_initialize_gp_p is still false at the end
10040 of md_reorg, we decide whether the global pointer is needed for
10041 long branches. If so, we set cfun->machine->must_initialize_gp_p
10042 to true and split the ghost instructions into real instructions
10043 at that stage.
10045 Note that the ghost instructions must have a zero length for three reasons:
10047 - Giving the length of the underlying $gp sequence might cause
10048 us to use long branches in cases where they aren't really needed.
10050 - They would perturb things like alignment calculations.
10052 - More importantly, the hazard detection in md_reorg relies on
10053 empty instructions having a zero length.
10055 If we find a long branch and split the ghost instructions at the
10056 end of md_reorg, the split could introduce more long branches.
10057 That isn't a problem though, because we still do the split before
10058 the final shorten_branches pass.
10060 This is extremely ugly, but it seems like the best compromise between
10061 correctness and efficiency. */
10063 bool
10064 mips_must_initialize_gp_p (void)
10066 return cfun->machine->must_initialize_gp_p;
10069 /* Return true if REGNO is a register that is ordinarily call-clobbered
10070 but must nevertheless be preserved by an interrupt handler. */
10072 static bool
10073 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
10075 if ((ISA_HAS_HILO || TARGET_DSP)
10076 && MD_REG_P (regno))
10077 return true;
10079 if (TARGET_DSP && DSP_ACC_REG_P (regno))
10080 return true;
10082 if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
10084 /* $0 is hard-wired. */
10085 if (regno == GP_REG_FIRST)
10086 return false;
10088 /* The interrupt handler can treat kernel registers as
10089 scratch registers. */
10090 if (KERNEL_REG_P (regno))
10091 return false;
10093 /* The function will return the stack pointer to its original value
10094 anyway. */
10095 if (regno == STACK_POINTER_REGNUM)
10096 return false;
10098 /* Otherwise, return true for registers that aren't ordinarily
10099 call-clobbered. */
10100 return call_really_used_regs[regno];
10103 return false;
10106 /* Return true if the current function should treat register REGNO
10107 as call-saved. */
10109 static bool
10110 mips_cfun_call_saved_reg_p (unsigned int regno)
10112 /* If the user makes an ordinarily-call-saved register global,
10113 that register is no longer call-saved. */
10114 if (global_regs[regno])
10115 return false;
10117 /* Interrupt handlers need to save extra registers. */
10118 if (cfun->machine->interrupt_handler_p
10119 && mips_interrupt_extra_call_saved_reg_p (regno))
10120 return true;
10122 /* call_insns preserve $28 unless they explicitly say otherwise,
10123 so call_really_used_regs[] treats $28 as call-saved. However,
10124 we want the ABI property rather than the default call_insn
10125 property here. */
10126 return (regno == GLOBAL_POINTER_REGNUM
10127 ? TARGET_CALL_SAVED_GP
10128 : !call_really_used_regs[regno]);
10131 /* Return true if the function body might clobber register REGNO.
10132 We know that REGNO is call-saved. */
10134 static bool
10135 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
10137 /* Some functions should be treated as clobbering all call-saved
10138 registers. */
10139 if (crtl->saves_all_registers)
10140 return true;
10142 /* DF handles cases where a register is explicitly referenced in
10143 the rtl. Incoming values are passed in call-clobbered registers,
10144 so we can assume that any live call-saved register is set within
10145 the function. */
10146 if (df_regs_ever_live_p (regno))
10147 return true;
10149 /* Check for registers that are clobbered by FUNCTION_PROFILER.
10150 These clobbers are not explicit in the rtl. */
10151 if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
10152 return true;
10154 /* If we're using a call-saved global pointer, the function's
10155 prologue will need to set it up. */
10156 if (cfun->machine->global_pointer == regno)
10157 return true;
10159 /* The function's prologue will need to set the frame pointer if
10160 frame_pointer_needed. */
10161 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
10162 return true;
10164 /* If a MIPS16 function returns a value in FPRs, its epilogue
10165 will need to call an external libgcc routine. This yet-to-be
10166 generated call_insn will clobber $31. */
10167 if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
10168 return true;
10170 /* If REGNO is ordinarily call-clobbered, we must assume that any
10171 called function could modify it. */
10172 if (cfun->machine->interrupt_handler_p
10173 && !crtl->is_leaf
10174 && mips_interrupt_extra_call_saved_reg_p (regno))
10175 return true;
10177 return false;
10180 /* Return true if the current function must save register REGNO. */
10182 static bool
10183 mips_save_reg_p (unsigned int regno)
10185 if (mips_cfun_call_saved_reg_p (regno))
10187 if (mips_cfun_might_clobber_call_saved_reg_p (regno))
10188 return true;
10190 /* Save both registers in an FPR pair if either one is used. This is
10191 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
10192 register to be used without the even register. */
10193 if (FP_REG_P (regno)
10194 && MAX_FPRS_PER_FMT == 2
10195 && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
10196 return true;
10199 /* We need to save the incoming return address if __builtin_eh_return
10200 is being used to set a different return address. */
10201 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
10202 return true;
10204 return false;
10207 /* Populate the current function's mips_frame_info structure.
10209 MIPS stack frames look like:
10211 +-------------------------------+
10213 | incoming stack arguments |
10215 +-------------------------------+
10217 | caller-allocated save area |
10218 A | for register arguments |
10220 +-------------------------------+ <-- incoming stack pointer
10222 | callee-allocated save area |
10223 B | for arguments that are |
10224 | split between registers and |
10225 | the stack |
10227 +-------------------------------+ <-- arg_pointer_rtx
10229 C | callee-allocated save area |
10230 | for register varargs |
10232 +-------------------------------+ <-- frame_pointer_rtx
10233 | | + cop0_sp_offset
10234 | COP0 reg save area | + UNITS_PER_WORD
10236 +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
10237 | | + UNITS_PER_WORD
10238 | accumulator save area |
10240 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
10241 | | + UNITS_PER_HWFPVALUE
10242 | FPR save area |
10244 +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
10245 | | + UNITS_PER_WORD
10246 | GPR save area |
10248 +-------------------------------+ <-- frame_pointer_rtx with
10249 | | \ -fstack-protector
10250 | local variables | | var_size
10251 | | /
10252 +-------------------------------+
10253 | | \
10254 | $gp save area | | cprestore_size
10255 | | /
10256 P +-------------------------------+ <-- hard_frame_pointer_rtx for
10257 | | \ MIPS16 code
10258 | outgoing stack arguments | |
10259 | | |
10260 +-------------------------------+ | args_size
10261 | | |
10262 | caller-allocated save area | |
10263 | for register arguments | |
10264 | | /
10265 +-------------------------------+ <-- stack_pointer_rtx
10266 frame_pointer_rtx without
10267 -fstack-protector
10268 hard_frame_pointer_rtx for
10269 non-MIPS16 code.
10271 At least two of A, B and C will be empty.
10273 Dynamic stack allocations such as alloca insert data at point P.
10274 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
10275 hard_frame_pointer_rtx unchanged. */
10277 static void
10278 mips_compute_frame_info (void)
10280 struct mips_frame_info *frame;
10281 HOST_WIDE_INT offset, size;
10282 unsigned int regno, i;
10284 /* Set this function's interrupt properties. */
10285 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
10287 if (mips_isa_rev < 2)
10288 error ("the %<interrupt%> attribute requires a MIPS32r2 processor or greater");
10289 else if (TARGET_HARD_FLOAT)
10290 error ("the %<interrupt%> attribute requires %<-msoft-float%>");
10291 else if (TARGET_MIPS16)
10292 error ("interrupt handlers cannot be MIPS16 functions");
10293 else
10295 cfun->machine->interrupt_handler_p = true;
10296 cfun->machine->use_shadow_register_set_p =
10297 mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
10298 cfun->machine->keep_interrupts_masked_p =
10299 mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
10300 cfun->machine->use_debug_exception_return_p =
10301 mips_use_debug_exception_return_p (TREE_TYPE
10302 (current_function_decl));
10306 frame = &cfun->machine->frame;
10307 memset (frame, 0, sizeof (*frame));
10308 size = get_frame_size ();
10310 cfun->machine->global_pointer = mips_global_pointer ();
10312 /* The first two blocks contain the outgoing argument area and the $gp save
10313 slot. This area isn't needed in leaf functions, but if the
10314 target-independent frame size is nonzero, we have already committed to
10315 allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD. */
10316 if ((size == 0 || FRAME_GROWS_DOWNWARD) && crtl->is_leaf)
10318 /* The MIPS 3.0 linker does not like functions that dynamically
10319 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
10320 looks like we are trying to create a second frame pointer to the
10321 function, so allocate some stack space to make it happy. */
10322 if (cfun->calls_alloca)
10323 frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
10324 else
10325 frame->args_size = 0;
10326 frame->cprestore_size = 0;
10328 else
10330 frame->args_size = crtl->outgoing_args_size;
10331 frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
10333 offset = frame->args_size + frame->cprestore_size;
10335 /* Move above the local variables. */
10336 frame->var_size = MIPS_STACK_ALIGN (size);
10337 offset += frame->var_size;
10339 /* Find out which GPRs we need to save. */
10340 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
10341 if (mips_save_reg_p (regno))
10343 frame->num_gp++;
10344 frame->mask |= 1 << (regno - GP_REG_FIRST);
10347 /* If this function calls eh_return, we must also save and restore the
10348 EH data registers. */
10349 if (crtl->calls_eh_return)
10350 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
10352 frame->num_gp++;
10353 frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
10356 /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
10357 $a3-$a0 and $s2-$s8. If we save one register in the range, we must
10358 save all later registers too. */
10359 if (GENERATE_MIPS16E_SAVE_RESTORE)
10361 mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
10362 ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
10363 mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
10364 ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
10367 /* Move above the GPR save area. */
10368 if (frame->num_gp > 0)
10370 offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
10371 frame->gp_sp_offset = offset - UNITS_PER_WORD;
10374 /* Find out which FPRs we need to save. This loop must iterate over
10375 the same space as its companion in mips_for_each_saved_gpr_and_fpr. */
10376 if (TARGET_HARD_FLOAT)
10377 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
10378 if (mips_save_reg_p (regno))
10380 frame->num_fp += MAX_FPRS_PER_FMT;
10381 frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
10384 /* Move above the FPR save area. */
10385 if (frame->num_fp > 0)
10387 offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
10388 frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
10391 /* Add in space for the interrupt context information. */
10392 if (cfun->machine->interrupt_handler_p)
10394 /* Check HI/LO. */
10395 if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
10397 frame->num_acc++;
10398 frame->acc_mask |= (1 << 0);
10401 /* Check accumulators 1, 2, 3. */
10402 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
10403 if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
10405 frame->num_acc++;
10406 frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
10409 /* All interrupt context functions need space to preserve STATUS. */
10410 frame->num_cop0_regs++;
10412 /* If we don't keep interrupts masked, we need to save EPC. */
10413 if (!cfun->machine->keep_interrupts_masked_p)
10414 frame->num_cop0_regs++;
10417 /* Move above the accumulator save area. */
10418 if (frame->num_acc > 0)
10420 /* Each accumulator needs 2 words. */
10421 offset += frame->num_acc * 2 * UNITS_PER_WORD;
10422 frame->acc_sp_offset = offset - UNITS_PER_WORD;
10425 /* Move above the COP0 register save area. */
10426 if (frame->num_cop0_regs > 0)
10428 offset += frame->num_cop0_regs * UNITS_PER_WORD;
10429 frame->cop0_sp_offset = offset - UNITS_PER_WORD;
10432 /* Move above the callee-allocated varargs save area. */
10433 offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
10434 frame->arg_pointer_offset = offset;
10436 /* Move above the callee-allocated area for pretend stack arguments. */
10437 offset += crtl->args.pretend_args_size;
10438 frame->total_size = offset;
10440 /* Work out the offsets of the save areas from the top of the frame. */
10441 if (frame->gp_sp_offset > 0)
10442 frame->gp_save_offset = frame->gp_sp_offset - offset;
10443 if (frame->fp_sp_offset > 0)
10444 frame->fp_save_offset = frame->fp_sp_offset - offset;
10445 if (frame->acc_sp_offset > 0)
10446 frame->acc_save_offset = frame->acc_sp_offset - offset;
10447 if (frame->num_cop0_regs > 0)
10448 frame->cop0_save_offset = frame->cop0_sp_offset - offset;
10450 /* MIPS16 code offsets the frame pointer by the size of the outgoing
10451 arguments. This tends to increase the chances of using unextended
10452 instructions for local variables and incoming arguments. */
10453 if (TARGET_MIPS16)
10454 frame->hard_frame_pointer_offset = frame->args_size;
10457 /* Return the style of GP load sequence that is being used for the
10458 current function. */
10460 enum mips_loadgp_style
10461 mips_current_loadgp_style (void)
10463 if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
10464 return LOADGP_NONE;
10466 if (TARGET_RTP_PIC)
10467 return LOADGP_RTP;
10469 if (TARGET_ABSOLUTE_ABICALLS)
10470 return LOADGP_ABSOLUTE;
10472 return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
10475 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
10477 static bool
10478 mips_frame_pointer_required (void)
10480 /* If the function contains dynamic stack allocations, we need to
10481 use the frame pointer to access the static parts of the frame. */
10482 if (cfun->calls_alloca)
10483 return true;
10485 /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
10486 reload may be unable to compute the address of a local variable,
10487 since there is no way to add a large constant to the stack pointer
10488 without using a second temporary register. */
10489 if (TARGET_MIPS16)
10491 mips_compute_frame_info ();
10492 if (!SMALL_OPERAND (cfun->machine->frame.total_size))
10493 return true;
10496 return false;
10499 /* Make sure that we're not trying to eliminate to the wrong hard frame
10500 pointer. */
10502 static bool
10503 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
10505 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
10508 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
10509 or argument pointer. TO is either the stack pointer or hard frame
10510 pointer. */
10512 HOST_WIDE_INT
10513 mips_initial_elimination_offset (int from, int to)
10515 HOST_WIDE_INT offset;
10517 mips_compute_frame_info ();
10519 /* Set OFFSET to the offset from the end-of-prologue stack pointer. */
10520 switch (from)
10522 case FRAME_POINTER_REGNUM:
10523 if (FRAME_GROWS_DOWNWARD)
10524 offset = (cfun->machine->frame.args_size
10525 + cfun->machine->frame.cprestore_size
10526 + cfun->machine->frame.var_size);
10527 else
10528 offset = 0;
10529 break;
10531 case ARG_POINTER_REGNUM:
10532 offset = cfun->machine->frame.arg_pointer_offset;
10533 break;
10535 default:
10536 gcc_unreachable ();
10539 if (to == HARD_FRAME_POINTER_REGNUM)
10540 offset -= cfun->machine->frame.hard_frame_pointer_offset;
10542 return offset;
10545 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY. */
10547 static void
10548 mips_extra_live_on_entry (bitmap regs)
10550 if (TARGET_USE_GOT)
10552 /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
10553 the global pointer. */
10554 if (!TARGET_ABSOLUTE_ABICALLS)
10555 bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
10557 /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
10558 the global pointer. */
10559 if (TARGET_MIPS16)
10560 bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
10562 /* See the comment above load_call<mode> for details. */
10563 bitmap_set_bit (regs, GOT_VERSION_REGNUM);
10567 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
10568 previous frame. */
10571 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
10573 if (count != 0)
10574 return const0_rtx;
10576 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
10579 /* Emit code to change the current function's return address to
10580 ADDRESS. SCRATCH is available as a scratch register, if needed.
10581 ADDRESS and SCRATCH are both word-mode GPRs. */
10583 void
10584 mips_set_return_address (rtx address, rtx scratch)
10586 rtx slot_address;
10588 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
10589 slot_address = mips_add_offset (scratch, stack_pointer_rtx,
10590 cfun->machine->frame.gp_sp_offset);
10591 mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
10594 /* Return true if the current function has a cprestore slot. */
10596 bool
10597 mips_cfun_has_cprestore_slot_p (void)
10599 return (cfun->machine->global_pointer != INVALID_REGNUM
10600 && cfun->machine->frame.cprestore_size > 0);
10603 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
10604 cprestore slot. LOAD_P is true if the caller wants to load from
10605 the cprestore slot; it is false if the caller wants to store to
10606 the slot. */
10608 static void
10609 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
10610 bool load_p)
10612 const struct mips_frame_info *frame;
10614 frame = &cfun->machine->frame;
10615 /* .cprestore always uses the stack pointer instead of the frame pointer.
10616 We have a free choice for direct stores for non-MIPS16 functions,
10617 and for MIPS16 functions whose cprestore slot is in range of the
10618 stack pointer. Using the stack pointer would sometimes give more
10619 (early) scheduling freedom, but using the frame pointer would
10620 sometimes give more (late) scheduling freedom. It's hard to
10621 predict which applies to a given function, so let's keep things
10622 simple.
10624 Loads must always use the frame pointer in functions that call
10625 alloca, and there's little benefit to using the stack pointer
10626 otherwise. */
10627 if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
10629 *base = hard_frame_pointer_rtx;
10630 *offset = frame->args_size - frame->hard_frame_pointer_offset;
10632 else
10634 *base = stack_pointer_rtx;
10635 *offset = frame->args_size;
10639 /* Return true if X is the load or store address of the cprestore slot;
10640 LOAD_P says which. */
10642 bool
10643 mips_cprestore_address_p (rtx x, bool load_p)
10645 rtx given_base, required_base;
10646 HOST_WIDE_INT given_offset, required_offset;
10648 mips_split_plus (x, &given_base, &given_offset);
10649 mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
10650 return given_base == required_base && given_offset == required_offset;
10653 /* Return a MEM rtx for the cprestore slot. LOAD_P is true if we are
10654 going to load from it, false if we are going to store to it.
10655 Use TEMP as a temporary register if need be. */
10657 static rtx
10658 mips_cprestore_slot (rtx temp, bool load_p)
10660 rtx base;
10661 HOST_WIDE_INT offset;
10663 mips_get_cprestore_base_and_offset (&base, &offset, load_p);
10664 return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
10667 /* Emit instructions to save global pointer value GP into cprestore
10668 slot MEM. OFFSET is the offset that MEM applies to the base register.
10670 MEM may not be a legitimate address. If it isn't, TEMP is a
10671 temporary register that can be used, otherwise it is a SCRATCH. */
10673 void
10674 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
10676 if (TARGET_CPRESTORE_DIRECTIVE)
10678 gcc_assert (gp == pic_offset_table_rtx);
10679 emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
10681 else
10682 mips_emit_move (mips_cprestore_slot (temp, false), gp);
10685 /* Restore $gp from its save slot, using TEMP as a temporary base register
10686 if need be. This function is for o32 and o64 abicalls only.
10688 See mips_must_initialize_gp_p for details about how we manage the
10689 global pointer. */
10691 void
10692 mips_restore_gp_from_cprestore_slot (rtx temp)
10694 gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
10696 if (!cfun->machine->must_restore_gp_when_clobbered_p)
10698 emit_note (NOTE_INSN_DELETED);
10699 return;
10702 if (TARGET_MIPS16)
10704 mips_emit_move (temp, mips_cprestore_slot (temp, true));
10705 mips_emit_move (pic_offset_table_rtx, temp);
10707 else
10708 mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
10709 if (!TARGET_EXPLICIT_RELOCS)
10710 emit_insn (gen_blockage ());
10713 /* A function to save or store a register. The first argument is the
10714 register and the second is the stack slot. */
10715 typedef void (*mips_save_restore_fn) (rtx, rtx);
10717 /* Use FN to save or restore register REGNO. MODE is the register's
10718 mode and OFFSET is the offset of its save slot from the current
10719 stack pointer. */
10721 static void
10722 mips_save_restore_reg (machine_mode mode, int regno,
10723 HOST_WIDE_INT offset, mips_save_restore_fn fn)
10725 rtx mem;
10727 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
10728 offset));
10729 fn (gen_rtx_REG (mode, regno), mem);
10732 /* Call FN for each accumlator that is saved by the current function.
10733 SP_OFFSET is the offset of the current stack pointer from the start
10734 of the frame. */
10736 static void
10737 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
10739 HOST_WIDE_INT offset;
10740 int regno;
10742 offset = cfun->machine->frame.acc_sp_offset - sp_offset;
10743 if (BITSET_P (cfun->machine->frame.acc_mask, 0))
10745 mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
10746 offset -= UNITS_PER_WORD;
10747 mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
10748 offset -= UNITS_PER_WORD;
10751 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
10752 if (BITSET_P (cfun->machine->frame.acc_mask,
10753 ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
10755 mips_save_restore_reg (word_mode, regno, offset, fn);
10756 offset -= UNITS_PER_WORD;
10760 /* Save register REG to MEM. Make the instruction frame-related. */
10762 static void
10763 mips_save_reg (rtx reg, rtx mem)
10765 if (GET_MODE (reg) == DFmode
10766 && (!TARGET_FLOAT64
10767 || mips_abi == ABI_32))
10769 rtx x1, x2;
10771 mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
10773 x1 = mips_frame_set (mips_subword (mem, false),
10774 mips_subword (reg, false));
10775 x2 = mips_frame_set (mips_subword (mem, true),
10776 mips_subword (reg, true));
10777 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
10779 else
10780 mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
10783 /* Capture the register combinations that are allowed in a SWM or LWM
10784 instruction. The entries are ordered by number of registers set in
10785 the mask. We also ignore the single register encodings because a
10786 normal SW/LW is preferred. */
10788 static const unsigned int umips_swm_mask[17] = {
10789 0xc0ff0000, 0x80ff0000, 0x40ff0000, 0x807f0000,
10790 0x00ff0000, 0x803f0000, 0x007f0000, 0x801f0000,
10791 0x003f0000, 0x800f0000, 0x001f0000, 0x80070000,
10792 0x000f0000, 0x80030000, 0x00070000, 0x80010000,
10793 0x00030000
10796 static const unsigned int umips_swm_encoding[17] = {
10797 25, 24, 9, 23, 8, 22, 7, 21, 6, 20, 5, 19, 4, 18, 3, 17, 2
10800 /* Try to use a microMIPS LWM or SWM instruction to save or restore
10801 as many GPRs in *MASK as possible. *OFFSET is the offset from the
10802 stack pointer of the topmost save slot.
10804 Remove from *MASK all registers that were handled using LWM and SWM.
10805 Update *OFFSET so that it points to the first unused save slot. */
10807 static bool
10808 umips_build_save_restore (mips_save_restore_fn fn,
10809 unsigned *mask, HOST_WIDE_INT *offset)
10811 int nregs;
10812 unsigned int i, j;
10813 rtx pattern, set, reg, mem;
10814 HOST_WIDE_INT this_offset;
10815 rtx this_base;
10817 /* Try matching $16 to $31 (s0 to ra). */
10818 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
10819 if ((*mask & 0xffff0000) == umips_swm_mask[i])
10820 break;
10822 if (i == ARRAY_SIZE (umips_swm_mask))
10823 return false;
10825 /* Get the offset of the lowest save slot. */
10826 nregs = (umips_swm_encoding[i] & 0xf) + (umips_swm_encoding[i] >> 4);
10827 this_offset = *offset - UNITS_PER_WORD * (nregs - 1);
10829 /* LWM/SWM can only support offsets from -2048 to 2047. */
10830 if (!UMIPS_12BIT_OFFSET_P (this_offset))
10831 return false;
10833 /* Create the final PARALLEL. */
10834 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
10835 this_base = stack_pointer_rtx;
10837 /* For registers $16-$23 and $30. */
10838 for (j = 0; j < (umips_swm_encoding[i] & 0xf); j++)
10840 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10841 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10842 unsigned int regno = (j != 8) ? 16 + j : 30;
10843 *mask &= ~(1 << regno);
10844 reg = gen_rtx_REG (SImode, regno);
10845 if (fn == mips_save_reg)
10846 set = mips_frame_set (mem, reg);
10847 else
10849 set = gen_rtx_SET (VOIDmode, reg, mem);
10850 mips_add_cfa_restore (reg);
10852 XVECEXP (pattern, 0, j) = set;
10855 /* For register $31. */
10856 if (umips_swm_encoding[i] >> 4)
10858 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10859 *mask &= ~(1 << 31);
10860 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10861 reg = gen_rtx_REG (SImode, 31);
10862 if (fn == mips_save_reg)
10863 set = mips_frame_set (mem, reg);
10864 else
10866 set = gen_rtx_SET (VOIDmode, reg, mem);
10867 mips_add_cfa_restore (reg);
10869 XVECEXP (pattern, 0, j) = set;
10872 pattern = emit_insn (pattern);
10873 if (fn == mips_save_reg)
10874 RTX_FRAME_RELATED_P (pattern) = 1;
10876 /* Adjust the last offset. */
10877 *offset -= UNITS_PER_WORD * nregs;
10879 return true;
10882 /* Call FN for each register that is saved by the current function.
10883 SP_OFFSET is the offset of the current stack pointer from the start
10884 of the frame. */
10886 static void
10887 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
10888 mips_save_restore_fn fn)
10890 machine_mode fpr_mode;
10891 int regno;
10892 const struct mips_frame_info *frame = &cfun->machine->frame;
10893 HOST_WIDE_INT offset;
10894 unsigned int mask;
10896 /* Save registers starting from high to low. The debuggers prefer at least
10897 the return register be stored at func+4, and also it allows us not to
10898 need a nop in the epilogue if at least one register is reloaded in
10899 addition to return address. */
10900 offset = frame->gp_sp_offset - sp_offset;
10901 mask = frame->mask;
10903 if (TARGET_MICROMIPS)
10904 umips_build_save_restore (fn, &mask, &offset);
10906 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
10907 if (BITSET_P (mask, regno - GP_REG_FIRST))
10909 /* Record the ra offset for use by mips_function_profiler. */
10910 if (regno == RETURN_ADDR_REGNUM)
10911 cfun->machine->frame.ra_fp_offset = offset + sp_offset;
10912 mips_save_restore_reg (word_mode, regno, offset, fn);
10913 offset -= UNITS_PER_WORD;
10916 /* This loop must iterate over the same space as its companion in
10917 mips_compute_frame_info. */
10918 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
10919 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
10920 for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
10921 regno >= FP_REG_FIRST;
10922 regno -= MAX_FPRS_PER_FMT)
10923 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
10925 if (!TARGET_FLOAT64 && TARGET_DOUBLE_FLOAT
10926 && (fixed_regs[regno] || fixed_regs[regno + 1]))
10928 if (fixed_regs[regno])
10929 mips_save_restore_reg (SFmode, regno + 1, offset, fn);
10930 else
10931 mips_save_restore_reg (SFmode, regno, offset, fn);
10933 else
10934 mips_save_restore_reg (fpr_mode, regno, offset, fn);
10935 offset -= GET_MODE_SIZE (fpr_mode);
10939 /* Return true if a move between register REGNO and its save slot (MEM)
10940 can be done in a single move. LOAD_P is true if we are loading
10941 from the slot, false if we are storing to it. */
10943 static bool
10944 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
10946 /* There is a specific MIPS16 instruction for saving $31 to the stack. */
10947 if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
10948 return false;
10950 return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
10951 GET_MODE (mem), mem, load_p) == NO_REGS;
10954 /* Emit a move from SRC to DEST, given that one of them is a register
10955 save slot and that the other is a register. TEMP is a temporary
10956 GPR of the same mode that is available if need be. */
10958 void
10959 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
10961 unsigned int regno;
10962 rtx mem;
10964 if (REG_P (src))
10966 regno = REGNO (src);
10967 mem = dest;
10969 else
10971 regno = REGNO (dest);
10972 mem = src;
10975 if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
10977 /* We don't yet know whether we'll need this instruction or not.
10978 Postpone the decision by emitting a ghost move. This move
10979 is specifically not frame-related; only the split version is. */
10980 if (TARGET_64BIT)
10981 emit_insn (gen_move_gpdi (dest, src));
10982 else
10983 emit_insn (gen_move_gpsi (dest, src));
10984 return;
10987 if (regno == HI_REGNUM)
10989 if (REG_P (dest))
10991 mips_emit_move (temp, src);
10992 if (TARGET_64BIT)
10993 emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
10994 temp, gen_rtx_REG (DImode, LO_REGNUM)));
10995 else
10996 emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
10997 temp, gen_rtx_REG (SImode, LO_REGNUM)));
10999 else
11001 if (TARGET_64BIT)
11002 emit_insn (gen_mfhidi_ti (temp,
11003 gen_rtx_REG (TImode, MD_REG_FIRST)));
11004 else
11005 emit_insn (gen_mfhisi_di (temp,
11006 gen_rtx_REG (DImode, MD_REG_FIRST)));
11007 mips_emit_move (dest, temp);
11010 else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
11011 mips_emit_move (dest, src);
11012 else
11014 gcc_assert (!reg_overlap_mentioned_p (dest, temp));
11015 mips_emit_move (temp, src);
11016 mips_emit_move (dest, temp);
11018 if (MEM_P (dest))
11019 mips_set_frame_expr (mips_frame_set (dest, src));
11022 /* If we're generating n32 or n64 abicalls, and the current function
11023 does not use $28 as its global pointer, emit a cplocal directive.
11024 Use pic_offset_table_rtx as the argument to the directive. */
11026 static void
11027 mips_output_cplocal (void)
11029 if (!TARGET_EXPLICIT_RELOCS
11030 && mips_must_initialize_gp_p ()
11031 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
11032 output_asm_insn (".cplocal %+", 0);
11035 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE. */
11037 static void
11038 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
11040 const char *fnname;
11042 /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
11043 floating-point arguments. */
11044 if (TARGET_MIPS16
11045 && TARGET_HARD_FLOAT_ABI
11046 && crtl->args.info.fp_code != 0)
11047 mips16_build_function_stub ();
11049 /* Get the function name the same way that toplev.c does before calling
11050 assemble_start_function. This is needed so that the name used here
11051 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
11052 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
11053 mips_start_function_definition (fnname, TARGET_MIPS16);
11055 /* Output MIPS-specific frame information. */
11056 if (!flag_inhibit_size_directive)
11058 const struct mips_frame_info *frame;
11060 frame = &cfun->machine->frame;
11062 /* .frame FRAMEREG, FRAMESIZE, RETREG. */
11063 fprintf (file,
11064 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
11065 "# vars= " HOST_WIDE_INT_PRINT_DEC
11066 ", regs= %d/%d"
11067 ", args= " HOST_WIDE_INT_PRINT_DEC
11068 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
11069 reg_names[frame_pointer_needed
11070 ? HARD_FRAME_POINTER_REGNUM
11071 : STACK_POINTER_REGNUM],
11072 (frame_pointer_needed
11073 ? frame->total_size - frame->hard_frame_pointer_offset
11074 : frame->total_size),
11075 reg_names[RETURN_ADDR_REGNUM],
11076 frame->var_size,
11077 frame->num_gp, frame->num_fp,
11078 frame->args_size,
11079 frame->cprestore_size);
11081 /* .mask MASK, OFFSET. */
11082 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
11083 frame->mask, frame->gp_save_offset);
11085 /* .fmask MASK, OFFSET. */
11086 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
11087 frame->fmask, frame->fp_save_offset);
11090 /* Handle the initialization of $gp for SVR4 PIC, if applicable.
11091 Also emit the ".set noreorder; .set nomacro" sequence for functions
11092 that need it. */
11093 if (mips_must_initialize_gp_p ()
11094 && mips_current_loadgp_style () == LOADGP_OLDABI)
11096 if (TARGET_MIPS16)
11098 /* This is a fixed-form sequence. The position of the
11099 first two instructions is important because of the
11100 way _gp_disp is defined. */
11101 output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
11102 output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
11103 output_asm_insn ("sll\t$2,16", 0);
11104 output_asm_insn ("addu\t$2,$3", 0);
11106 else
11108 /* .cpload must be in a .set noreorder but not a
11109 .set nomacro block. */
11110 mips_push_asm_switch (&mips_noreorder);
11111 output_asm_insn (".cpload\t%^", 0);
11112 if (!cfun->machine->all_noreorder_p)
11113 mips_pop_asm_switch (&mips_noreorder);
11114 else
11115 mips_push_asm_switch (&mips_nomacro);
11118 else if (cfun->machine->all_noreorder_p)
11120 mips_push_asm_switch (&mips_noreorder);
11121 mips_push_asm_switch (&mips_nomacro);
11124 /* Tell the assembler which register we're using as the global
11125 pointer. This is needed for thunks, since they can use either
11126 explicit relocs or assembler macros. */
11127 mips_output_cplocal ();
11130 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE. */
11132 static void
11133 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
11134 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
11136 const char *fnname;
11138 /* Reinstate the normal $gp. */
11139 SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
11140 mips_output_cplocal ();
11142 if (cfun->machine->all_noreorder_p)
11144 mips_pop_asm_switch (&mips_nomacro);
11145 mips_pop_asm_switch (&mips_noreorder);
11148 /* Get the function name the same way that toplev.c does before calling
11149 assemble_start_function. This is needed so that the name used here
11150 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
11151 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
11152 mips_end_function_definition (fnname);
11155 /* Emit an optimisation barrier for accesses to the current frame. */
11157 static void
11158 mips_frame_barrier (void)
11160 emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
11164 /* The __gnu_local_gp symbol. */
11166 static GTY(()) rtx mips_gnu_local_gp;
11168 /* If we're generating n32 or n64 abicalls, emit instructions
11169 to set up the global pointer. */
11171 static void
11172 mips_emit_loadgp (void)
11174 rtx addr, offset, incoming_address, base, index, pic_reg;
11176 pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
11177 switch (mips_current_loadgp_style ())
11179 case LOADGP_ABSOLUTE:
11180 if (mips_gnu_local_gp == NULL)
11182 mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
11183 SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
11185 emit_insn (PMODE_INSN (gen_loadgp_absolute,
11186 (pic_reg, mips_gnu_local_gp)));
11187 break;
11189 case LOADGP_OLDABI:
11190 /* Added by mips_output_function_prologue. */
11191 break;
11193 case LOADGP_NEWABI:
11194 addr = XEXP (DECL_RTL (current_function_decl), 0);
11195 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
11196 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
11197 emit_insn (PMODE_INSN (gen_loadgp_newabi,
11198 (pic_reg, offset, incoming_address)));
11199 break;
11201 case LOADGP_RTP:
11202 base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
11203 index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
11204 emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
11205 break;
11207 default:
11208 return;
11211 if (TARGET_MIPS16)
11212 emit_insn (PMODE_INSN (gen_copygp_mips16,
11213 (pic_offset_table_rtx, pic_reg)));
11215 /* Emit a blockage if there are implicit uses of the GP register.
11216 This includes profiled functions, because FUNCTION_PROFILE uses
11217 a jal macro. */
11218 if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
11219 emit_insn (gen_loadgp_blockage ());
11222 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
11224 #if PROBE_INTERVAL > 32768
11225 #error Cannot use indexed addressing mode for stack probing
11226 #endif
11228 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
11229 inclusive. These are offsets from the current stack pointer. */
11231 static void
11232 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
11234 if (TARGET_MIPS16)
11235 sorry ("-fstack-check=specific not implemented for MIPS16");
11237 /* See if we have a constant small number of probes to generate. If so,
11238 that's the easy case. */
11239 if (first + size <= 32768)
11241 HOST_WIDE_INT i;
11243 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
11244 it exceeds SIZE. If only one probe is needed, this will not
11245 generate any code. Then probe at FIRST + SIZE. */
11246 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
11247 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
11248 -(first + i)));
11250 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
11251 -(first + size)));
11254 /* Otherwise, do the same as above, but in a loop. Note that we must be
11255 extra careful with variables wrapping around because we might be at
11256 the very top (or the very bottom) of the address space and we have
11257 to be able to handle this case properly; in particular, we use an
11258 equality test for the loop condition. */
11259 else
11261 HOST_WIDE_INT rounded_size;
11262 rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
11263 rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
11265 /* Sanity check for the addressing mode we're going to use. */
11266 gcc_assert (first <= 32768);
11269 /* Step 1: round SIZE to the previous multiple of the interval. */
11271 rounded_size = size & -PROBE_INTERVAL;
11274 /* Step 2: compute initial and final value of the loop counter. */
11276 /* TEST_ADDR = SP + FIRST. */
11277 emit_insn (gen_rtx_SET (VOIDmode, r3,
11278 plus_constant (Pmode, stack_pointer_rtx,
11279 -first)));
11281 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
11282 if (rounded_size > 32768)
11284 emit_move_insn (r12, GEN_INT (rounded_size));
11285 emit_insn (gen_rtx_SET (VOIDmode, r12,
11286 gen_rtx_MINUS (Pmode, r3, r12)));
11288 else
11289 emit_insn (gen_rtx_SET (VOIDmode, r12,
11290 plus_constant (Pmode, r3, -rounded_size)));
11293 /* Step 3: the loop
11295 while (TEST_ADDR != LAST_ADDR)
11297 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
11298 probe at TEST_ADDR
11301 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
11302 until it is equal to ROUNDED_SIZE. */
11304 emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
11307 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
11308 that SIZE is equal to ROUNDED_SIZE. */
11310 if (size != rounded_size)
11311 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
11314 /* Make sure nothing is scheduled before we are done. */
11315 emit_insn (gen_blockage ());
11318 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
11319 absolute addresses. */
11321 const char *
11322 mips_output_probe_stack_range (rtx reg1, rtx reg2)
11324 static int labelno = 0;
11325 char loop_lab[32], end_lab[32], tmp[64];
11326 rtx xops[2];
11328 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
11329 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
11331 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
11333 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
11334 xops[0] = reg1;
11335 xops[1] = reg2;
11336 strcpy (tmp, "%(%<beq\t%0,%1,");
11337 output_asm_insn (strcat (tmp, &end_lab[1]), xops);
11339 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
11340 xops[1] = GEN_INT (-PROBE_INTERVAL);
11341 if (TARGET_64BIT && TARGET_LONG64)
11342 output_asm_insn ("daddiu\t%0,%0,%1", xops);
11343 else
11344 output_asm_insn ("addiu\t%0,%0,%1", xops);
11346 /* Probe at TEST_ADDR and branch. */
11347 fprintf (asm_out_file, "\tb\t");
11348 assemble_name_raw (asm_out_file, loop_lab);
11349 fputc ('\n', asm_out_file);
11350 if (TARGET_64BIT)
11351 output_asm_insn ("sd\t$0,0(%0)%)", xops);
11352 else
11353 output_asm_insn ("sw\t$0,0(%0)%)", xops);
11355 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
11357 return "";
11360 /* Return true if X contains a kernel register. */
11362 static bool
11363 mips_refers_to_kernel_reg_p (const_rtx x)
11365 subrtx_iterator::array_type array;
11366 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
11367 if (REG_P (*iter) && KERNEL_REG_P (REGNO (*iter)))
11368 return true;
11369 return false;
11372 /* Expand the "prologue" pattern. */
11374 void
11375 mips_expand_prologue (void)
11377 const struct mips_frame_info *frame;
11378 HOST_WIDE_INT size;
11379 unsigned int nargs;
11381 if (cfun->machine->global_pointer != INVALID_REGNUM)
11383 /* Check whether an insn uses pic_offset_table_rtx, either explicitly
11384 or implicitly. If so, we can commit to using a global pointer
11385 straight away, otherwise we need to defer the decision. */
11386 if (mips_cfun_has_inflexible_gp_ref_p ()
11387 || mips_cfun_has_flexible_gp_ref_p ())
11389 cfun->machine->must_initialize_gp_p = true;
11390 cfun->machine->must_restore_gp_when_clobbered_p = true;
11393 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
11396 frame = &cfun->machine->frame;
11397 size = frame->total_size;
11399 if (flag_stack_usage_info)
11400 current_function_static_stack_size = size;
11402 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
11404 if (crtl->is_leaf && !cfun->calls_alloca)
11406 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
11407 mips_emit_probe_stack_range (STACK_CHECK_PROTECT,
11408 size - STACK_CHECK_PROTECT);
11410 else if (size > 0)
11411 mips_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
11414 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
11415 bytes beforehand; this is enough to cover the register save area
11416 without going out of range. */
11417 if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
11418 || frame->num_cop0_regs > 0)
11420 HOST_WIDE_INT step1;
11422 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
11423 if (GENERATE_MIPS16E_SAVE_RESTORE)
11425 HOST_WIDE_INT offset;
11426 unsigned int mask, regno;
11428 /* Try to merge argument stores into the save instruction. */
11429 nargs = mips16e_collect_argument_saves ();
11431 /* Build the save instruction. */
11432 mask = frame->mask;
11433 rtx insn = mips16e_build_save_restore (false, &mask, &offset,
11434 nargs, step1);
11435 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11436 mips_frame_barrier ();
11437 size -= step1;
11439 /* Check if we need to save other registers. */
11440 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11441 if (BITSET_P (mask, regno - GP_REG_FIRST))
11443 offset -= UNITS_PER_WORD;
11444 mips_save_restore_reg (word_mode, regno,
11445 offset, mips_save_reg);
11448 else
11450 if (cfun->machine->interrupt_handler_p)
11452 HOST_WIDE_INT offset;
11453 rtx mem;
11455 /* If this interrupt is using a shadow register set, we need to
11456 get the stack pointer from the previous register set. */
11457 if (cfun->machine->use_shadow_register_set_p)
11458 emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
11459 stack_pointer_rtx));
11461 if (!cfun->machine->keep_interrupts_masked_p)
11463 /* Move from COP0 Cause to K0. */
11464 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
11465 gen_rtx_REG (SImode,
11466 COP0_CAUSE_REG_NUM)));
11467 /* Move from COP0 EPC to K1. */
11468 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11469 gen_rtx_REG (SImode,
11470 COP0_EPC_REG_NUM)));
11473 /* Allocate the first part of the frame. */
11474 rtx insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
11475 GEN_INT (-step1));
11476 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11477 mips_frame_barrier ();
11478 size -= step1;
11480 /* Start at the uppermost location for saving. */
11481 offset = frame->cop0_sp_offset - size;
11482 if (!cfun->machine->keep_interrupts_masked_p)
11484 /* Push EPC into its stack slot. */
11485 mem = gen_frame_mem (word_mode,
11486 plus_constant (Pmode, stack_pointer_rtx,
11487 offset));
11488 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11489 offset -= UNITS_PER_WORD;
11492 /* Move from COP0 Status to K1. */
11493 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11494 gen_rtx_REG (SImode,
11495 COP0_STATUS_REG_NUM)));
11497 /* Right justify the RIPL in k0. */
11498 if (!cfun->machine->keep_interrupts_masked_p)
11499 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
11500 gen_rtx_REG (SImode, K0_REG_NUM),
11501 GEN_INT (CAUSE_IPL)));
11503 /* Push Status into its stack slot. */
11504 mem = gen_frame_mem (word_mode,
11505 plus_constant (Pmode, stack_pointer_rtx,
11506 offset));
11507 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11508 offset -= UNITS_PER_WORD;
11510 /* Insert the RIPL into our copy of SR (k1) as the new IPL. */
11511 if (!cfun->machine->keep_interrupts_masked_p)
11512 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11513 GEN_INT (6),
11514 GEN_INT (SR_IPL),
11515 gen_rtx_REG (SImode, K0_REG_NUM)));
11517 if (!cfun->machine->keep_interrupts_masked_p)
11518 /* Enable interrupts by clearing the KSU ERL and EXL bits.
11519 IE is already the correct value, so we don't have to do
11520 anything explicit. */
11521 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11522 GEN_INT (4),
11523 GEN_INT (SR_EXL),
11524 gen_rtx_REG (SImode, GP_REG_FIRST)));
11525 else
11526 /* Disable interrupts by clearing the KSU, ERL, EXL,
11527 and IE bits. */
11528 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11529 GEN_INT (5),
11530 GEN_INT (SR_IE),
11531 gen_rtx_REG (SImode, GP_REG_FIRST)));
11533 else
11535 rtx insn = gen_add3_insn (stack_pointer_rtx,
11536 stack_pointer_rtx,
11537 GEN_INT (-step1));
11538 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11539 mips_frame_barrier ();
11540 size -= step1;
11542 mips_for_each_saved_acc (size, mips_save_reg);
11543 mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
11547 /* Allocate the rest of the frame. */
11548 if (size > 0)
11550 if (SMALL_OPERAND (-size))
11551 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
11552 stack_pointer_rtx,
11553 GEN_INT (-size)))) = 1;
11554 else
11556 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
11557 if (TARGET_MIPS16)
11559 /* There are no instructions to add or subtract registers
11560 from the stack pointer, so use the frame pointer as a
11561 temporary. We should always be using a frame pointer
11562 in this case anyway. */
11563 gcc_assert (frame_pointer_needed);
11564 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11565 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
11566 hard_frame_pointer_rtx,
11567 MIPS_PROLOGUE_TEMP (Pmode)));
11568 mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
11570 else
11571 emit_insn (gen_sub3_insn (stack_pointer_rtx,
11572 stack_pointer_rtx,
11573 MIPS_PROLOGUE_TEMP (Pmode)));
11575 /* Describe the combined effect of the previous instructions. */
11576 mips_set_frame_expr
11577 (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11578 plus_constant (Pmode, stack_pointer_rtx, -size)));
11580 mips_frame_barrier ();
11583 /* Set up the frame pointer, if we're using one. */
11584 if (frame_pointer_needed)
11586 HOST_WIDE_INT offset;
11588 offset = frame->hard_frame_pointer_offset;
11589 if (offset == 0)
11591 rtx insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11592 RTX_FRAME_RELATED_P (insn) = 1;
11594 else if (SMALL_OPERAND (offset))
11596 rtx insn = gen_add3_insn (hard_frame_pointer_rtx,
11597 stack_pointer_rtx, GEN_INT (offset));
11598 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11600 else
11602 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
11603 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11604 emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
11605 hard_frame_pointer_rtx,
11606 MIPS_PROLOGUE_TEMP (Pmode)));
11607 mips_set_frame_expr
11608 (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11609 plus_constant (Pmode, stack_pointer_rtx, offset)));
11613 mips_emit_loadgp ();
11615 /* Initialize the $gp save slot. */
11616 if (mips_cfun_has_cprestore_slot_p ())
11618 rtx base, mem, gp, temp;
11619 HOST_WIDE_INT offset;
11621 mips_get_cprestore_base_and_offset (&base, &offset, false);
11622 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11623 gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
11624 temp = (SMALL_OPERAND (offset)
11625 ? gen_rtx_SCRATCH (Pmode)
11626 : MIPS_PROLOGUE_TEMP (Pmode));
11627 emit_insn (PMODE_INSN (gen_potential_cprestore,
11628 (mem, GEN_INT (offset), gp, temp)));
11630 mips_get_cprestore_base_and_offset (&base, &offset, true);
11631 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11632 emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
11635 /* We need to search back to the last use of K0 or K1. */
11636 if (cfun->machine->interrupt_handler_p)
11638 rtx_insn *insn;
11639 for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
11640 if (INSN_P (insn)
11641 && mips_refers_to_kernel_reg_p (PATTERN (insn)))
11642 break;
11643 /* Emit a move from K1 to COP0 Status after insn. */
11644 gcc_assert (insn != NULL_RTX);
11645 emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11646 gen_rtx_REG (SImode, K1_REG_NUM)),
11647 insn);
11650 /* If we are profiling, make sure no instructions are scheduled before
11651 the call to mcount. */
11652 if (crtl->profile)
11653 emit_insn (gen_blockage ());
11656 /* Attach all pending register saves to the previous instruction.
11657 Return that instruction. */
11659 static rtx_insn *
11660 mips_epilogue_emit_cfa_restores (void)
11662 rtx_insn *insn;
11664 insn = get_last_insn ();
11665 gcc_assert (insn && !REG_NOTES (insn));
11666 if (mips_epilogue.cfa_restores)
11668 RTX_FRAME_RELATED_P (insn) = 1;
11669 REG_NOTES (insn) = mips_epilogue.cfa_restores;
11670 mips_epilogue.cfa_restores = 0;
11672 return insn;
11675 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
11676 now at REG + OFFSET. */
11678 static void
11679 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
11681 rtx_insn *insn;
11683 insn = mips_epilogue_emit_cfa_restores ();
11684 if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
11686 RTX_FRAME_RELATED_P (insn) = 1;
11687 REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
11688 plus_constant (Pmode, reg, offset),
11689 REG_NOTES (insn));
11690 mips_epilogue.cfa_reg = reg;
11691 mips_epilogue.cfa_offset = offset;
11695 /* Emit instructions to restore register REG from slot MEM. Also update
11696 the cfa_restores list. */
11698 static void
11699 mips_restore_reg (rtx reg, rtx mem)
11701 /* There's no MIPS16 instruction to load $31 directly. Load into
11702 $7 instead and adjust the return insn appropriately. */
11703 if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
11704 reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
11705 else if (GET_MODE (reg) == DFmode
11706 && (!TARGET_FLOAT64
11707 || mips_abi == ABI_32))
11709 mips_add_cfa_restore (mips_subword (reg, true));
11710 mips_add_cfa_restore (mips_subword (reg, false));
11712 else
11713 mips_add_cfa_restore (reg);
11715 mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
11716 if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
11717 /* The CFA is currently defined in terms of the register whose
11718 value we have just restored. Redefine the CFA in terms of
11719 the stack pointer. */
11720 mips_epilogue_set_cfa (stack_pointer_rtx,
11721 mips_epilogue.cfa_restore_sp_offset);
11724 /* Emit code to set the stack pointer to BASE + OFFSET, given that
11725 BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
11726 BASE, if not the stack pointer, is available as a temporary. */
11728 static void
11729 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
11731 if (base == stack_pointer_rtx && offset == const0_rtx)
11732 return;
11734 mips_frame_barrier ();
11735 if (offset == const0_rtx)
11737 emit_move_insn (stack_pointer_rtx, base);
11738 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11740 else if (TARGET_MIPS16 && base != stack_pointer_rtx)
11742 emit_insn (gen_add3_insn (base, base, offset));
11743 mips_epilogue_set_cfa (base, new_frame_size);
11744 emit_move_insn (stack_pointer_rtx, base);
11746 else
11748 emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
11749 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11753 /* Emit any instructions needed before a return. */
11755 void
11756 mips_expand_before_return (void)
11758 /* When using a call-clobbered gp, we start out with unified call
11759 insns that include instructions to restore the gp. We then split
11760 these unified calls after reload. These split calls explicitly
11761 clobber gp, so there is no need to define
11762 PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
11764 For consistency, we should also insert an explicit clobber of $28
11765 before return insns, so that the post-reload optimizers know that
11766 the register is not live on exit. */
11767 if (TARGET_CALL_CLOBBERED_GP)
11768 emit_clobber (pic_offset_table_rtx);
11771 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
11772 says which. */
11774 void
11775 mips_expand_epilogue (bool sibcall_p)
11777 const struct mips_frame_info *frame;
11778 HOST_WIDE_INT step1, step2;
11779 rtx base, adjust;
11780 rtx_insn *insn;
11781 bool use_jraddiusp_p = false;
11783 if (!sibcall_p && mips_can_use_return_insn ())
11785 emit_jump_insn (gen_return ());
11786 return;
11789 /* In MIPS16 mode, if the return value should go into a floating-point
11790 register, we need to call a helper routine to copy it over. */
11791 if (mips16_cfun_returns_in_fpr_p ())
11792 mips16_copy_fpr_return_value ();
11794 /* Split the frame into two. STEP1 is the amount of stack we should
11795 deallocate before restoring the registers. STEP2 is the amount we
11796 should deallocate afterwards.
11798 Start off by assuming that no registers need to be restored. */
11799 frame = &cfun->machine->frame;
11800 step1 = frame->total_size;
11801 step2 = 0;
11803 /* Work out which register holds the frame address. */
11804 if (!frame_pointer_needed)
11805 base = stack_pointer_rtx;
11806 else
11808 base = hard_frame_pointer_rtx;
11809 step1 -= frame->hard_frame_pointer_offset;
11811 mips_epilogue.cfa_reg = base;
11812 mips_epilogue.cfa_offset = step1;
11813 mips_epilogue.cfa_restores = NULL_RTX;
11815 /* If we need to restore registers, deallocate as much stack as
11816 possible in the second step without going out of range. */
11817 if ((frame->mask | frame->fmask | frame->acc_mask) != 0
11818 || frame->num_cop0_regs > 0)
11820 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
11821 step1 -= step2;
11824 /* Get an rtx for STEP1 that we can add to BASE. */
11825 adjust = GEN_INT (step1);
11826 if (!SMALL_OPERAND (step1))
11828 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
11829 adjust = MIPS_EPILOGUE_TEMP (Pmode);
11831 mips_deallocate_stack (base, adjust, step2);
11833 /* If we're using addressing macros, $gp is implicitly used by all
11834 SYMBOL_REFs. We must emit a blockage insn before restoring $gp
11835 from the stack. */
11836 if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
11837 emit_insn (gen_blockage ());
11839 mips_epilogue.cfa_restore_sp_offset = step2;
11840 if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
11842 unsigned int regno, mask;
11843 HOST_WIDE_INT offset;
11844 rtx restore;
11846 /* Generate the restore instruction. */
11847 mask = frame->mask;
11848 restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
11850 /* Restore any other registers manually. */
11851 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11852 if (BITSET_P (mask, regno - GP_REG_FIRST))
11854 offset -= UNITS_PER_WORD;
11855 mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
11858 /* Restore the remaining registers and deallocate the final bit
11859 of the frame. */
11860 mips_frame_barrier ();
11861 emit_insn (restore);
11862 mips_epilogue_set_cfa (stack_pointer_rtx, 0);
11864 else
11866 /* Restore the registers. */
11867 mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
11868 mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
11869 mips_restore_reg);
11871 if (cfun->machine->interrupt_handler_p)
11873 HOST_WIDE_INT offset;
11874 rtx mem;
11876 offset = frame->cop0_sp_offset - (frame->total_size - step2);
11877 if (!cfun->machine->keep_interrupts_masked_p)
11879 /* Restore the original EPC. */
11880 mem = gen_frame_mem (word_mode,
11881 plus_constant (Pmode, stack_pointer_rtx,
11882 offset));
11883 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11884 offset -= UNITS_PER_WORD;
11886 /* Move to COP0 EPC. */
11887 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
11888 gen_rtx_REG (SImode, K0_REG_NUM)));
11891 /* Restore the original Status. */
11892 mem = gen_frame_mem (word_mode,
11893 plus_constant (Pmode, stack_pointer_rtx,
11894 offset));
11895 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11896 offset -= UNITS_PER_WORD;
11898 /* If we don't use shadow register set, we need to update SP. */
11899 if (!cfun->machine->use_shadow_register_set_p)
11900 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11901 else
11902 /* The choice of position is somewhat arbitrary in this case. */
11903 mips_epilogue_emit_cfa_restores ();
11905 /* Move to COP0 Status. */
11906 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11907 gen_rtx_REG (SImode, K0_REG_NUM)));
11909 else if (TARGET_MICROMIPS
11910 && !crtl->calls_eh_return
11911 && !sibcall_p
11912 && step2 > 0
11913 && mips_unsigned_immediate_p (step2, 5, 2))
11914 use_jraddiusp_p = true;
11915 else
11916 /* Deallocate the final bit of the frame. */
11917 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11920 if (!use_jraddiusp_p)
11921 gcc_assert (!mips_epilogue.cfa_restores);
11923 /* Add in the __builtin_eh_return stack adjustment. We need to
11924 use a temporary in MIPS16 code. */
11925 if (crtl->calls_eh_return)
11927 if (TARGET_MIPS16)
11929 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
11930 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
11931 MIPS_EPILOGUE_TEMP (Pmode),
11932 EH_RETURN_STACKADJ_RTX));
11933 mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
11935 else
11936 emit_insn (gen_add3_insn (stack_pointer_rtx,
11937 stack_pointer_rtx,
11938 EH_RETURN_STACKADJ_RTX));
11941 if (!sibcall_p)
11943 mips_expand_before_return ();
11944 if (cfun->machine->interrupt_handler_p)
11946 /* Interrupt handlers generate eret or deret. */
11947 if (cfun->machine->use_debug_exception_return_p)
11948 emit_jump_insn (gen_mips_deret ());
11949 else
11950 emit_jump_insn (gen_mips_eret ());
11952 else
11954 rtx pat;
11956 /* When generating MIPS16 code, the normal
11957 mips_for_each_saved_gpr_and_fpr path will restore the return
11958 address into $7 rather than $31. */
11959 if (TARGET_MIPS16
11960 && !GENERATE_MIPS16E_SAVE_RESTORE
11961 && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
11963 /* simple_returns cannot rely on values that are only available
11964 on paths through the epilogue (because return paths that do
11965 not pass through the epilogue may nevertheless reuse a
11966 simple_return that occurs at the end of the epilogue).
11967 Use a normal return here instead. */
11968 rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
11969 pat = gen_return_internal (reg);
11971 else if (use_jraddiusp_p)
11972 pat = gen_jraddiusp (GEN_INT (step2));
11973 else
11975 rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
11976 pat = gen_simple_return_internal (reg);
11978 emit_jump_insn (pat);
11979 if (use_jraddiusp_p)
11980 mips_epilogue_set_cfa (stack_pointer_rtx, step2);
11984 /* Search from the beginning to the first use of K0 or K1. */
11985 if (cfun->machine->interrupt_handler_p
11986 && !cfun->machine->keep_interrupts_masked_p)
11988 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
11989 if (INSN_P (insn)
11990 && mips_refers_to_kernel_reg_p (PATTERN (insn)))
11991 break;
11992 gcc_assert (insn != NULL_RTX);
11993 /* Insert disable interrupts before the first use of K0 or K1. */
11994 emit_insn_before (gen_mips_di (), insn);
11995 emit_insn_before (gen_mips_ehb (), insn);
11999 /* Return nonzero if this function is known to have a null epilogue.
12000 This allows the optimizer to omit jumps to jumps if no stack
12001 was created. */
12003 bool
12004 mips_can_use_return_insn (void)
12006 /* Interrupt handlers need to go through the epilogue. */
12007 if (cfun->machine->interrupt_handler_p)
12008 return false;
12010 if (!reload_completed)
12011 return false;
12013 if (crtl->profile)
12014 return false;
12016 /* In MIPS16 mode, a function that returns a floating-point value
12017 needs to arrange to copy the return value into the floating-point
12018 registers. */
12019 if (mips16_cfun_returns_in_fpr_p ())
12020 return false;
12022 return cfun->machine->frame.total_size == 0;
12025 /* Return true if register REGNO can store a value of mode MODE.
12026 The result of this function is cached in mips_hard_regno_mode_ok. */
12028 static bool
12029 mips_hard_regno_mode_ok_p (unsigned int regno, machine_mode mode)
12031 unsigned int size;
12032 enum mode_class mclass;
12034 if (mode == CCV2mode)
12035 return (ISA_HAS_8CC
12036 && ST_REG_P (regno)
12037 && (regno - ST_REG_FIRST) % 2 == 0);
12039 if (mode == CCV4mode)
12040 return (ISA_HAS_8CC
12041 && ST_REG_P (regno)
12042 && (regno - ST_REG_FIRST) % 4 == 0);
12044 if (mode == CCmode)
12045 return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
12047 size = GET_MODE_SIZE (mode);
12048 mclass = GET_MODE_CLASS (mode);
12050 if (GP_REG_P (regno) && mode != CCFmode)
12051 return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
12053 if (FP_REG_P (regno)
12054 && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
12055 || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
12057 /* Deny use of odd-numbered registers for 32-bit data for
12058 the o32 FP64A ABI. */
12059 if (TARGET_O32_FP64A_ABI && size <= 4 && (regno & 1) != 0)
12060 return false;
12062 /* The FPXX ABI requires double-precision values to be placed in
12063 even-numbered registers. Disallow odd-numbered registers with
12064 CCFmode because CCFmode double-precision compares will write a
12065 64-bit value to a register. */
12066 if (mode == CCFmode)
12067 return !(TARGET_FLOATXX && (regno & 1) != 0);
12069 /* Allow 64-bit vector modes for Loongson-2E/2F. */
12070 if (TARGET_LOONGSON_VECTORS
12071 && (mode == V2SImode
12072 || mode == V4HImode
12073 || mode == V8QImode
12074 || mode == DImode))
12075 return true;
12077 if (mclass == MODE_FLOAT
12078 || mclass == MODE_COMPLEX_FLOAT
12079 || mclass == MODE_VECTOR_FLOAT)
12080 return size <= UNITS_PER_FPVALUE;
12082 /* Allow integer modes that fit into a single register. We need
12083 to put integers into FPRs when using instructions like CVT
12084 and TRUNC. There's no point allowing sizes smaller than a word,
12085 because the FPU has no appropriate load/store instructions. */
12086 if (mclass == MODE_INT)
12087 return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
12090 if (ACC_REG_P (regno)
12091 && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
12093 if (MD_REG_P (regno))
12095 /* After a multiplication or division, clobbering HI makes
12096 the value of LO unpredictable, and vice versa. This means
12097 that, for all interesting cases, HI and LO are effectively
12098 a single register.
12100 We model this by requiring that any value that uses HI
12101 also uses LO. */
12102 if (size <= UNITS_PER_WORD * 2)
12103 return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
12105 else
12107 /* DSP accumulators do not have the same restrictions as
12108 HI and LO, so we can treat them as normal doubleword
12109 registers. */
12110 if (size <= UNITS_PER_WORD)
12111 return true;
12113 if (size <= UNITS_PER_WORD * 2
12114 && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
12115 return true;
12119 if (ALL_COP_REG_P (regno))
12120 return mclass == MODE_INT && size <= UNITS_PER_WORD;
12122 if (regno == GOT_VERSION_REGNUM)
12123 return mode == SImode;
12125 return false;
12128 /* Implement HARD_REGNO_NREGS. */
12130 unsigned int
12131 mips_hard_regno_nregs (int regno, machine_mode mode)
12133 if (ST_REG_P (regno))
12134 /* The size of FP status registers is always 4, because they only hold
12135 CCmode values, and CCmode is always considered to be 4 bytes wide. */
12136 return (GET_MODE_SIZE (mode) + 3) / 4;
12138 if (FP_REG_P (regno))
12139 return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
12141 /* All other registers are word-sized. */
12142 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
12145 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
12146 in mips_hard_regno_nregs. */
12149 mips_class_max_nregs (enum reg_class rclass, machine_mode mode)
12151 int size;
12152 HARD_REG_SET left;
12154 size = 0x8000;
12155 COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
12156 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
12158 if (HARD_REGNO_MODE_OK (ST_REG_FIRST, mode))
12159 size = MIN (size, 4);
12160 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
12162 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
12164 if (HARD_REGNO_MODE_OK (FP_REG_FIRST, mode))
12165 size = MIN (size, UNITS_PER_FPREG);
12166 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
12168 if (!hard_reg_set_empty_p (left))
12169 size = MIN (size, UNITS_PER_WORD);
12170 return (GET_MODE_SIZE (mode) + size - 1) / size;
12173 /* Implement CANNOT_CHANGE_MODE_CLASS. */
12175 bool
12176 mips_cannot_change_mode_class (machine_mode from,
12177 machine_mode to,
12178 enum reg_class rclass)
12180 /* Allow conversions between different Loongson integer vectors,
12181 and between those vectors and DImode. */
12182 if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
12183 && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
12184 return false;
12186 /* Otherwise, there are several problems with changing the modes of
12187 values in floating-point registers:
12189 - When a multi-word value is stored in paired floating-point
12190 registers, the first register always holds the low word. We
12191 therefore can't allow FPRs to change between single-word and
12192 multi-word modes on big-endian targets.
12194 - GCC assumes that each word of a multiword register can be
12195 accessed individually using SUBREGs. This is not true for
12196 floating-point registers if they are bigger than a word.
12198 - Loading a 32-bit value into a 64-bit floating-point register
12199 will not sign-extend the value, despite what LOAD_EXTEND_OP
12200 says. We can't allow FPRs to change from SImode to a wider
12201 mode on 64-bit targets.
12203 - If the FPU has already interpreted a value in one format, we
12204 must not ask it to treat the value as having a different
12205 format.
12207 We therefore disallow all mode changes involving FPRs. */
12209 return reg_classes_intersect_p (FP_REGS, rclass);
12212 /* Implement target hook small_register_classes_for_mode_p. */
12214 static bool
12215 mips_small_register_classes_for_mode_p (machine_mode mode
12216 ATTRIBUTE_UNUSED)
12218 return TARGET_MIPS16;
12221 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction. */
12223 static bool
12224 mips_mode_ok_for_mov_fmt_p (machine_mode mode)
12226 switch (mode)
12228 case CCFmode:
12229 case SFmode:
12230 return TARGET_HARD_FLOAT;
12232 case DFmode:
12233 return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
12235 case V2SFmode:
12236 return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
12238 default:
12239 return false;
12243 /* Implement MODES_TIEABLE_P. */
12245 bool
12246 mips_modes_tieable_p (machine_mode mode1, machine_mode mode2)
12248 /* FPRs allow no mode punning, so it's not worth tying modes if we'd
12249 prefer to put one of them in FPRs. */
12250 return (mode1 == mode2
12251 || (!mips_mode_ok_for_mov_fmt_p (mode1)
12252 && !mips_mode_ok_for_mov_fmt_p (mode2)));
12255 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
12257 static reg_class_t
12258 mips_preferred_reload_class (rtx x, reg_class_t rclass)
12260 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
12261 return LEA_REGS;
12263 if (reg_class_subset_p (FP_REGS, rclass)
12264 && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
12265 return FP_REGS;
12267 if (reg_class_subset_p (GR_REGS, rclass))
12268 rclass = GR_REGS;
12270 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
12271 rclass = M16_REGS;
12273 return rclass;
12276 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
12277 Return a "canonical" class to represent it in later calculations. */
12279 static reg_class_t
12280 mips_canonicalize_move_class (reg_class_t rclass)
12282 /* All moves involving accumulator registers have the same cost. */
12283 if (reg_class_subset_p (rclass, ACC_REGS))
12284 rclass = ACC_REGS;
12286 /* Likewise promote subclasses of general registers to the most
12287 interesting containing class. */
12288 if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
12289 rclass = M16_REGS;
12290 else if (reg_class_subset_p (rclass, GENERAL_REGS))
12291 rclass = GENERAL_REGS;
12293 return rclass;
12296 /* Return the cost of moving a value from a register of class FROM to a GPR.
12297 Return 0 for classes that are unions of other classes handled by this
12298 function. */
12300 static int
12301 mips_move_to_gpr_cost (reg_class_t from)
12303 switch (from)
12305 case M16_REGS:
12306 case GENERAL_REGS:
12307 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
12308 return 2;
12310 case ACC_REGS:
12311 /* MFLO and MFHI. */
12312 return 6;
12314 case FP_REGS:
12315 /* MFC1, etc. */
12316 return 4;
12318 case COP0_REGS:
12319 case COP2_REGS:
12320 case COP3_REGS:
12321 /* This choice of value is historical. */
12322 return 5;
12324 default:
12325 return 0;
12329 /* Return the cost of moving a value from a GPR to a register of class TO.
12330 Return 0 for classes that are unions of other classes handled by this
12331 function. */
12333 static int
12334 mips_move_from_gpr_cost (reg_class_t to)
12336 switch (to)
12338 case M16_REGS:
12339 case GENERAL_REGS:
12340 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
12341 return 2;
12343 case ACC_REGS:
12344 /* MTLO and MTHI. */
12345 return 6;
12347 case FP_REGS:
12348 /* MTC1, etc. */
12349 return 4;
12351 case COP0_REGS:
12352 case COP2_REGS:
12353 case COP3_REGS:
12354 /* This choice of value is historical. */
12355 return 5;
12357 default:
12358 return 0;
12362 /* Implement TARGET_REGISTER_MOVE_COST. Return 0 for classes that are the
12363 maximum of the move costs for subclasses; regclass will work out
12364 the maximum for us. */
12366 static int
12367 mips_register_move_cost (machine_mode mode,
12368 reg_class_t from, reg_class_t to)
12370 reg_class_t dregs;
12371 int cost1, cost2;
12373 from = mips_canonicalize_move_class (from);
12374 to = mips_canonicalize_move_class (to);
12376 /* Handle moves that can be done without using general-purpose registers. */
12377 if (from == FP_REGS)
12379 if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
12380 /* MOV.FMT. */
12381 return 4;
12384 /* Handle cases in which only one class deviates from the ideal. */
12385 dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
12386 if (from == dregs)
12387 return mips_move_from_gpr_cost (to);
12388 if (to == dregs)
12389 return mips_move_to_gpr_cost (from);
12391 /* Handles cases that require a GPR temporary. */
12392 cost1 = mips_move_to_gpr_cost (from);
12393 if (cost1 != 0)
12395 cost2 = mips_move_from_gpr_cost (to);
12396 if (cost2 != 0)
12397 return cost1 + cost2;
12400 return 0;
12403 /* Implement TARGET_REGISTER_PRIORITY. */
12405 static int
12406 mips_register_priority (int hard_regno)
12408 /* Treat MIPS16 registers with higher priority than other regs. */
12409 if (TARGET_MIPS16
12410 && TEST_HARD_REG_BIT (reg_class_contents[M16_REGS], hard_regno))
12411 return 1;
12412 return 0;
12415 /* Implement TARGET_MEMORY_MOVE_COST. */
12417 static int
12418 mips_memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
12420 return (mips_cost->memory_latency
12421 + memory_move_secondary_cost (mode, rclass, in));
12424 /* Implement SECONDARY_MEMORY_NEEDED. */
12426 bool
12427 mips_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
12428 machine_mode mode)
12430 /* Ignore spilled pseudos. */
12431 if (lra_in_progress && (class1 == NO_REGS || class2 == NO_REGS))
12432 return false;
12434 if (((class1 == FP_REGS) != (class2 == FP_REGS))
12435 && ((TARGET_FLOATXX && !ISA_HAS_MXHC1)
12436 || TARGET_O32_FP64A_ABI)
12437 && GET_MODE_SIZE (mode) >= 8)
12438 return true;
12440 return false;
12443 /* Return the register class required for a secondary register when
12444 copying between one of the registers in RCLASS and value X, which
12445 has mode MODE. X is the source of the move if IN_P, otherwise it
12446 is the destination. Return NO_REGS if no secondary register is
12447 needed. */
12449 enum reg_class
12450 mips_secondary_reload_class (enum reg_class rclass,
12451 machine_mode mode, rtx x, bool)
12453 int regno;
12455 /* If X is a constant that cannot be loaded into $25, it must be loaded
12456 into some other GPR. No other register class allows a direct move. */
12457 if (mips_dangerous_for_la25_p (x))
12458 return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
12460 regno = true_regnum (x);
12461 if (TARGET_MIPS16)
12463 /* In MIPS16 mode, every move must involve a member of M16_REGS. */
12464 if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
12465 return M16_REGS;
12467 return NO_REGS;
12470 /* Copying from accumulator registers to anywhere other than a general
12471 register requires a temporary general register. */
12472 if (reg_class_subset_p (rclass, ACC_REGS))
12473 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12474 if (ACC_REG_P (regno))
12475 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12477 if (reg_class_subset_p (rclass, FP_REGS))
12479 if (regno < 0
12480 || (MEM_P (x)
12481 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)))
12482 /* In this case we can use lwc1, swc1, ldc1 or sdc1. We'll use
12483 pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported. */
12484 return NO_REGS;
12486 if (GP_REG_P (regno) || x == CONST0_RTX (mode))
12487 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
12488 return NO_REGS;
12490 if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
12491 /* We can force the constant to memory and use lwc1
12492 and ldc1. As above, we will use pairs of lwc1s if
12493 ldc1 is not supported. */
12494 return NO_REGS;
12496 if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
12497 /* In this case we can use mov.fmt. */
12498 return NO_REGS;
12500 /* Otherwise, we need to reload through an integer register. */
12501 return GR_REGS;
12503 if (FP_REG_P (regno))
12504 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12506 return NO_REGS;
12509 /* Implement TARGET_MODE_REP_EXTENDED. */
12511 static int
12512 mips_mode_rep_extended (machine_mode mode, machine_mode mode_rep)
12514 /* On 64-bit targets, SImode register values are sign-extended to DImode. */
12515 if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
12516 return SIGN_EXTEND;
12518 return UNKNOWN;
12521 /* Implement TARGET_VALID_POINTER_MODE. */
12523 static bool
12524 mips_valid_pointer_mode (machine_mode mode)
12526 return mode == SImode || (TARGET_64BIT && mode == DImode);
12529 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P. */
12531 static bool
12532 mips_vector_mode_supported_p (machine_mode mode)
12534 switch (mode)
12536 case V2SFmode:
12537 return TARGET_PAIRED_SINGLE_FLOAT;
12539 case V2HImode:
12540 case V4QImode:
12541 case V2HQmode:
12542 case V2UHQmode:
12543 case V2HAmode:
12544 case V2UHAmode:
12545 case V4QQmode:
12546 case V4UQQmode:
12547 return TARGET_DSP;
12549 case V2SImode:
12550 case V4HImode:
12551 case V8QImode:
12552 return TARGET_LOONGSON_VECTORS;
12554 default:
12555 return false;
12559 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
12561 static bool
12562 mips_scalar_mode_supported_p (machine_mode mode)
12564 if (ALL_FIXED_POINT_MODE_P (mode)
12565 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
12566 return true;
12568 return default_scalar_mode_supported_p (mode);
12571 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
12573 static machine_mode
12574 mips_preferred_simd_mode (machine_mode mode ATTRIBUTE_UNUSED)
12576 if (TARGET_PAIRED_SINGLE_FLOAT
12577 && mode == SFmode)
12578 return V2SFmode;
12579 return word_mode;
12582 /* Implement TARGET_INIT_LIBFUNCS. */
12584 static void
12585 mips_init_libfuncs (void)
12587 if (TARGET_FIX_VR4120)
12589 /* Register the special divsi3 and modsi3 functions needed to work
12590 around VR4120 division errata. */
12591 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
12592 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
12595 if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
12597 /* Register the MIPS16 -mhard-float stubs. */
12598 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
12599 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
12600 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
12601 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
12603 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
12604 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
12605 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
12606 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
12607 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
12608 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
12609 set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
12611 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
12612 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
12613 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
12615 if (TARGET_DOUBLE_FLOAT)
12617 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
12618 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
12619 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
12620 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
12622 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
12623 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
12624 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
12625 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
12626 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
12627 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
12628 set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
12630 set_conv_libfunc (sext_optab, DFmode, SFmode,
12631 "__mips16_extendsfdf2");
12632 set_conv_libfunc (trunc_optab, SFmode, DFmode,
12633 "__mips16_truncdfsf2");
12634 set_conv_libfunc (sfix_optab, SImode, DFmode,
12635 "__mips16_fix_truncdfsi");
12636 set_conv_libfunc (sfloat_optab, DFmode, SImode,
12637 "__mips16_floatsidf");
12638 set_conv_libfunc (ufloat_optab, DFmode, SImode,
12639 "__mips16_floatunsidf");
12643 /* The MIPS16 ISA does not have an encoding for "sync", so we rely
12644 on an external non-MIPS16 routine to implement __sync_synchronize.
12645 Similarly for the rest of the ll/sc libfuncs. */
12646 if (TARGET_MIPS16)
12648 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
12649 init_sync_libfuncs (UNITS_PER_WORD);
12653 /* Build up a multi-insn sequence that loads label TARGET into $AT. */
12655 static void
12656 mips_process_load_label (rtx target)
12658 rtx base, gp, intop;
12659 HOST_WIDE_INT offset;
12661 mips_multi_start ();
12662 switch (mips_abi)
12664 case ABI_N32:
12665 mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
12666 mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
12667 break;
12669 case ABI_64:
12670 mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
12671 mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
12672 break;
12674 default:
12675 gp = pic_offset_table_rtx;
12676 if (mips_cfun_has_cprestore_slot_p ())
12678 gp = gen_rtx_REG (Pmode, AT_REGNUM);
12679 mips_get_cprestore_base_and_offset (&base, &offset, true);
12680 if (!SMALL_OPERAND (offset))
12682 intop = GEN_INT (CONST_HIGH_PART (offset));
12683 mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
12684 mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
12686 base = gp;
12687 offset = CONST_LOW_PART (offset);
12689 intop = GEN_INT (offset);
12690 if (ISA_HAS_LOAD_DELAY)
12691 mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
12692 else
12693 mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
12695 if (ISA_HAS_LOAD_DELAY)
12696 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
12697 else
12698 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
12699 mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
12700 break;
12704 /* Return the number of instructions needed to load a label into $AT. */
12706 static unsigned int
12707 mips_load_label_num_insns (void)
12709 if (cfun->machine->load_label_num_insns == 0)
12711 mips_process_load_label (pc_rtx);
12712 cfun->machine->load_label_num_insns = mips_multi_num_insns;
12714 return cfun->machine->load_label_num_insns;
12717 /* Emit an asm sequence to start a noat block and load the address
12718 of a label into $1. */
12720 void
12721 mips_output_load_label (rtx target)
12723 mips_push_asm_switch (&mips_noat);
12724 if (TARGET_EXPLICIT_RELOCS)
12726 mips_process_load_label (target);
12727 mips_multi_write ();
12729 else
12731 if (Pmode == DImode)
12732 output_asm_insn ("dla\t%@,%0", &target);
12733 else
12734 output_asm_insn ("la\t%@,%0", &target);
12738 /* Return the length of INSN. LENGTH is the initial length computed by
12739 attributes in the machine-description file. */
12742 mips_adjust_insn_length (rtx_insn *insn, int length)
12744 /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
12745 of a PIC long-branch sequence. Substitute the correct value. */
12746 if (length == MAX_PIC_BRANCH_LENGTH
12747 && JUMP_P (insn)
12748 && INSN_CODE (insn) >= 0
12749 && get_attr_type (insn) == TYPE_BRANCH)
12751 /* Add the branch-over instruction and its delay slot, if this
12752 is a conditional branch. */
12753 length = simplejump_p (insn) ? 0 : 8;
12755 /* Add the size of a load into $AT. */
12756 length += BASE_INSN_LENGTH * mips_load_label_num_insns ();
12758 /* Add the length of an indirect jump, ignoring the delay slot. */
12759 length += TARGET_COMPRESSION ? 2 : 4;
12762 /* A unconditional jump has an unfilled delay slot if it is not part
12763 of a sequence. A conditional jump normally has a delay slot, but
12764 does not on MIPS16. */
12765 if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
12766 length += TARGET_MIPS16 ? 2 : 4;
12768 /* See how many nops might be needed to avoid hardware hazards. */
12769 if (!cfun->machine->ignore_hazard_length_p
12770 && INSN_P (insn)
12771 && INSN_CODE (insn) >= 0)
12772 switch (get_attr_hazard (insn))
12774 case HAZARD_NONE:
12775 break;
12777 case HAZARD_DELAY:
12778 length += NOP_INSN_LENGTH;
12779 break;
12781 case HAZARD_HILO:
12782 length += NOP_INSN_LENGTH * 2;
12783 break;
12786 return length;
12789 /* Return the assembly code for INSN, which has the operands given by
12790 OPERANDS, and which branches to OPERANDS[0] if some condition is true.
12791 BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
12792 is in range of a direct branch. BRANCH_IF_FALSE is an inverted
12793 version of BRANCH_IF_TRUE. */
12795 const char *
12796 mips_output_conditional_branch (rtx_insn *insn, rtx *operands,
12797 const char *branch_if_true,
12798 const char *branch_if_false)
12800 unsigned int length;
12801 rtx taken;
12803 gcc_assert (LABEL_P (operands[0]));
12805 length = get_attr_length (insn);
12806 if (length <= 8)
12808 /* Just a simple conditional branch. */
12809 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
12810 return branch_if_true;
12813 /* Generate a reversed branch around a direct jump. This fallback does
12814 not use branch-likely instructions. */
12815 mips_branch_likely = false;
12816 rtx_code_label *not_taken = gen_label_rtx ();
12817 taken = operands[0];
12819 /* Generate the reversed branch to NOT_TAKEN. */
12820 operands[0] = not_taken;
12821 output_asm_insn (branch_if_false, operands);
12823 /* If INSN has a delay slot, we must provide delay slots for both the
12824 branch to NOT_TAKEN and the conditional jump. We must also ensure
12825 that INSN's delay slot is executed in the appropriate cases. */
12826 if (final_sequence)
12828 /* This first delay slot will always be executed, so use INSN's
12829 delay slot if is not annulled. */
12830 if (!INSN_ANNULLED_BRANCH_P (insn))
12832 final_scan_insn (final_sequence->insn (1),
12833 asm_out_file, optimize, 1, NULL);
12834 final_sequence->insn (1)->set_deleted ();
12836 else
12837 output_asm_insn ("nop", 0);
12838 fprintf (asm_out_file, "\n");
12841 /* Output the unconditional branch to TAKEN. */
12842 if (TARGET_ABSOLUTE_JUMPS)
12843 output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
12844 else
12846 mips_output_load_label (taken);
12847 output_asm_insn ("jr\t%@%]%/", 0);
12850 /* Now deal with its delay slot; see above. */
12851 if (final_sequence)
12853 /* This delay slot will only be executed if the branch is taken.
12854 Use INSN's delay slot if is annulled. */
12855 if (INSN_ANNULLED_BRANCH_P (insn))
12857 final_scan_insn (final_sequence->insn (1),
12858 asm_out_file, optimize, 1, NULL);
12859 final_sequence->insn (1)->set_deleted ();
12861 else
12862 output_asm_insn ("nop", 0);
12863 fprintf (asm_out_file, "\n");
12866 /* Output NOT_TAKEN. */
12867 targetm.asm_out.internal_label (asm_out_file, "L",
12868 CODE_LABEL_NUMBER (not_taken));
12869 return "";
12872 /* Return the assembly code for INSN, which branches to OPERANDS[0]
12873 if some ordering condition is true. The condition is given by
12874 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
12875 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
12876 its second is always zero. */
12878 const char *
12879 mips_output_order_conditional_branch (rtx_insn *insn, rtx *operands, bool inverted_p)
12881 const char *branch[2];
12883 /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
12884 Make BRANCH[0] branch on the inverse condition. */
12885 switch (GET_CODE (operands[1]))
12887 /* These cases are equivalent to comparisons against zero. */
12888 case LEU:
12889 inverted_p = !inverted_p;
12890 /* Fall through. */
12891 case GTU:
12892 branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
12893 branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
12894 break;
12896 /* These cases are always true or always false. */
12897 case LTU:
12898 inverted_p = !inverted_p;
12899 /* Fall through. */
12900 case GEU:
12901 branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
12902 branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
12903 break;
12905 default:
12906 branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
12907 branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
12908 break;
12910 return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
12913 /* Start a block of code that needs access to the LL, SC and SYNC
12914 instructions. */
12916 static void
12917 mips_start_ll_sc_sync_block (void)
12919 if (!ISA_HAS_LL_SC)
12921 output_asm_insn (".set\tpush", 0);
12922 if (TARGET_64BIT)
12923 output_asm_insn (".set\tmips3", 0);
12924 else
12925 output_asm_insn (".set\tmips2", 0);
12929 /* End a block started by mips_start_ll_sc_sync_block. */
12931 static void
12932 mips_end_ll_sc_sync_block (void)
12934 if (!ISA_HAS_LL_SC)
12935 output_asm_insn (".set\tpop", 0);
12938 /* Output and/or return the asm template for a sync instruction. */
12940 const char *
12941 mips_output_sync (void)
12943 mips_start_ll_sc_sync_block ();
12944 output_asm_insn ("sync", 0);
12945 mips_end_ll_sc_sync_block ();
12946 return "";
12949 /* Return the asm template associated with sync_insn1 value TYPE.
12950 IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation. */
12952 static const char *
12953 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
12955 switch (type)
12957 case SYNC_INSN1_MOVE:
12958 return "move\t%0,%z2";
12959 case SYNC_INSN1_LI:
12960 return "li\t%0,%2";
12961 case SYNC_INSN1_ADDU:
12962 return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
12963 case SYNC_INSN1_ADDIU:
12964 return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
12965 case SYNC_INSN1_SUBU:
12966 return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
12967 case SYNC_INSN1_AND:
12968 return "and\t%0,%1,%z2";
12969 case SYNC_INSN1_ANDI:
12970 return "andi\t%0,%1,%2";
12971 case SYNC_INSN1_OR:
12972 return "or\t%0,%1,%z2";
12973 case SYNC_INSN1_ORI:
12974 return "ori\t%0,%1,%2";
12975 case SYNC_INSN1_XOR:
12976 return "xor\t%0,%1,%z2";
12977 case SYNC_INSN1_XORI:
12978 return "xori\t%0,%1,%2";
12980 gcc_unreachable ();
12983 /* Return the asm template associated with sync_insn2 value TYPE. */
12985 static const char *
12986 mips_sync_insn2_template (enum attr_sync_insn2 type)
12988 switch (type)
12990 case SYNC_INSN2_NOP:
12991 gcc_unreachable ();
12992 case SYNC_INSN2_AND:
12993 return "and\t%0,%1,%z2";
12994 case SYNC_INSN2_XOR:
12995 return "xor\t%0,%1,%z2";
12996 case SYNC_INSN2_NOT:
12997 return "nor\t%0,%1,%.";
12999 gcc_unreachable ();
13002 /* OPERANDS are the operands to a sync loop instruction and INDEX is
13003 the value of the one of the sync_* attributes. Return the operand
13004 referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
13005 have the associated attribute. */
13007 static rtx
13008 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
13010 if (index > 0)
13011 default_value = operands[index - 1];
13012 return default_value;
13015 /* INSN is a sync loop with operands OPERANDS. Build up a multi-insn
13016 sequence for it. */
13018 static void
13019 mips_process_sync_loop (rtx_insn *insn, rtx *operands)
13021 rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
13022 rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
13023 unsigned int tmp3_insn;
13024 enum attr_sync_insn1 insn1;
13025 enum attr_sync_insn2 insn2;
13026 bool is_64bit_p;
13027 int memmodel_attr;
13028 enum memmodel model;
13030 /* Read an operand from the sync_WHAT attribute and store it in
13031 variable WHAT. DEFAULT is the default value if no attribute
13032 is specified. */
13033 #define READ_OPERAND(WHAT, DEFAULT) \
13034 WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
13035 DEFAULT)
13037 /* Read the memory. */
13038 READ_OPERAND (mem, 0);
13039 gcc_assert (mem);
13040 is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
13042 /* Read the other attributes. */
13043 at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
13044 READ_OPERAND (oldval, at);
13045 READ_OPERAND (cmp, 0);
13046 READ_OPERAND (newval, at);
13047 READ_OPERAND (inclusive_mask, 0);
13048 READ_OPERAND (exclusive_mask, 0);
13049 READ_OPERAND (required_oldval, 0);
13050 READ_OPERAND (insn1_op2, 0);
13051 insn1 = get_attr_sync_insn1 (insn);
13052 insn2 = get_attr_sync_insn2 (insn);
13054 /* Don't bother setting CMP result that is never used. */
13055 if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
13056 cmp = 0;
13058 memmodel_attr = get_attr_sync_memmodel (insn);
13059 switch (memmodel_attr)
13061 case 10:
13062 model = MEMMODEL_ACQ_REL;
13063 break;
13064 case 11:
13065 model = MEMMODEL_ACQUIRE;
13066 break;
13067 default:
13068 model = (enum memmodel) INTVAL (operands[memmodel_attr]);
13071 mips_multi_start ();
13073 /* Output the release side of the memory barrier. */
13074 if (need_atomic_barrier_p (model, true))
13076 if (required_oldval == 0 && TARGET_OCTEON)
13078 /* Octeon doesn't reorder reads, so a full barrier can be
13079 created by using SYNCW to order writes combined with the
13080 write from the following SC. When the SC successfully
13081 completes, we know that all preceding writes are also
13082 committed to the coherent memory system. It is possible
13083 for a single SYNCW to fail, but a pair of them will never
13084 fail, so we use two. */
13085 mips_multi_add_insn ("syncw", NULL);
13086 mips_multi_add_insn ("syncw", NULL);
13088 else
13089 mips_multi_add_insn ("sync", NULL);
13092 /* Output the branch-back label. */
13093 mips_multi_add_label ("1:");
13095 /* OLDVAL = *MEM. */
13096 mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
13097 oldval, mem, NULL);
13099 /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2. */
13100 if (required_oldval)
13102 if (inclusive_mask == 0)
13103 tmp1 = oldval;
13104 else
13106 gcc_assert (oldval != at);
13107 mips_multi_add_insn ("and\t%0,%1,%2",
13108 at, oldval, inclusive_mask, NULL);
13109 tmp1 = at;
13111 mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
13113 /* CMP = 0 [delay slot]. */
13114 if (cmp)
13115 mips_multi_add_insn ("li\t%0,0", cmp, NULL);
13118 /* $TMP1 = OLDVAL & EXCLUSIVE_MASK. */
13119 if (exclusive_mask == 0)
13120 tmp1 = const0_rtx;
13121 else
13123 gcc_assert (oldval != at);
13124 mips_multi_add_insn ("and\t%0,%1,%z2",
13125 at, oldval, exclusive_mask, NULL);
13126 tmp1 = at;
13129 /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
13131 We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
13132 at least one instruction in that case. */
13133 if (insn1 == SYNC_INSN1_MOVE
13134 && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
13135 tmp2 = insn1_op2;
13136 else
13138 mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
13139 newval, oldval, insn1_op2, NULL);
13140 tmp2 = newval;
13143 /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK). */
13144 if (insn2 == SYNC_INSN2_NOP)
13145 tmp3 = tmp2;
13146 else
13148 mips_multi_add_insn (mips_sync_insn2_template (insn2),
13149 newval, tmp2, inclusive_mask, NULL);
13150 tmp3 = newval;
13152 tmp3_insn = mips_multi_last_index ();
13154 /* $AT = $TMP1 | $TMP3. */
13155 if (tmp1 == const0_rtx || tmp3 == const0_rtx)
13157 mips_multi_set_operand (tmp3_insn, 0, at);
13158 tmp3 = at;
13160 else
13162 gcc_assert (tmp1 != tmp3);
13163 mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
13166 /* if (!commit (*MEM = $AT)) goto 1.
13168 This will sometimes be a delayed branch; see the write code below
13169 for details. */
13170 mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
13172 /* When using branch likely (-mfix-r10000), the delay slot instruction
13173 will be annulled on false. The normal delay slot instructions
13174 calculate the overall result of the atomic operation and must not
13175 be annulled. To ensure this behaviour unconditionally use a NOP
13176 in the delay slot for the branch likely case. */
13178 mips_multi_add_insn ("beq%?\t%0,%.,1b%~", at, NULL);
13180 /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot]. */
13181 if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
13183 mips_multi_copy_insn (tmp3_insn);
13184 mips_multi_set_operand (mips_multi_last_index (), 0, newval);
13186 else if (!(required_oldval && cmp) && !mips_branch_likely)
13187 mips_multi_add_insn ("nop", NULL);
13189 /* CMP = 1 -- either standalone or in a delay slot. */
13190 if (required_oldval && cmp)
13191 mips_multi_add_insn ("li\t%0,1", cmp, NULL);
13193 /* Output the acquire side of the memory barrier. */
13194 if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
13195 mips_multi_add_insn ("sync", NULL);
13197 /* Output the exit label, if needed. */
13198 if (required_oldval)
13199 mips_multi_add_label ("2:");
13201 #undef READ_OPERAND
13204 /* Output and/or return the asm template for sync loop INSN, which has
13205 the operands given by OPERANDS. */
13207 const char *
13208 mips_output_sync_loop (rtx_insn *insn, rtx *operands)
13210 /* Use branch-likely instructions to work around the LL/SC R10000
13211 errata. */
13212 mips_branch_likely = TARGET_FIX_R10000;
13214 mips_process_sync_loop (insn, operands);
13216 mips_push_asm_switch (&mips_noreorder);
13217 mips_push_asm_switch (&mips_nomacro);
13218 mips_push_asm_switch (&mips_noat);
13219 mips_start_ll_sc_sync_block ();
13221 mips_multi_write ();
13223 mips_end_ll_sc_sync_block ();
13224 mips_pop_asm_switch (&mips_noat);
13225 mips_pop_asm_switch (&mips_nomacro);
13226 mips_pop_asm_switch (&mips_noreorder);
13228 return "";
13231 /* Return the number of individual instructions in sync loop INSN,
13232 which has the operands given by OPERANDS. */
13234 unsigned int
13235 mips_sync_loop_insns (rtx_insn *insn, rtx *operands)
13237 /* Use branch-likely instructions to work around the LL/SC R10000
13238 errata. */
13239 mips_branch_likely = TARGET_FIX_R10000;
13240 mips_process_sync_loop (insn, operands);
13241 return mips_multi_num_insns;
13244 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
13245 the operands given by OPERANDS. Add in a divide-by-zero check if needed.
13247 When working around R4000 and R4400 errata, we need to make sure that
13248 the division is not immediately followed by a shift[1][2]. We also
13249 need to stop the division from being put into a branch delay slot[3].
13250 The easiest way to avoid both problems is to add a nop after the
13251 division. When a divide-by-zero check is needed, this nop can be
13252 used to fill the branch delay slot.
13254 [1] If a double-word or a variable shift executes immediately
13255 after starting an integer division, the shift may give an
13256 incorrect result. See quotations of errata #16 and #28 from
13257 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
13258 in mips.md for details.
13260 [2] A similar bug to [1] exists for all revisions of the
13261 R4000 and the R4400 when run in an MC configuration.
13262 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
13264 "19. In this following sequence:
13266 ddiv (or ddivu or div or divu)
13267 dsll32 (or dsrl32, dsra32)
13269 if an MPT stall occurs, while the divide is slipping the cpu
13270 pipeline, then the following double shift would end up with an
13271 incorrect result.
13273 Workaround: The compiler needs to avoid generating any
13274 sequence with divide followed by extended double shift."
13276 This erratum is also present in "MIPS R4400MC Errata, Processor
13277 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
13278 & 3.0" as errata #10 and #4, respectively.
13280 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
13281 (also valid for MIPS R4000MC processors):
13283 "52. R4000SC: This bug does not apply for the R4000PC.
13285 There are two flavors of this bug:
13287 1) If the instruction just after divide takes an RF exception
13288 (tlb-refill, tlb-invalid) and gets an instruction cache
13289 miss (both primary and secondary) and the line which is
13290 currently in secondary cache at this index had the first
13291 data word, where the bits 5..2 are set, then R4000 would
13292 get a wrong result for the div.
13296 div r8, r9
13297 ------------------- # end-of page. -tlb-refill
13301 div r8, r9
13302 ------------------- # end-of page. -tlb-invalid
13305 2) If the divide is in the taken branch delay slot, where the
13306 target takes RF exception and gets an I-cache miss for the
13307 exception vector or where I-cache miss occurs for the
13308 target address, under the above mentioned scenarios, the
13309 div would get wrong results.
13312 j r2 # to next page mapped or unmapped
13313 div r8,r9 # this bug would be there as long
13314 # as there is an ICache miss and
13315 nop # the "data pattern" is present
13318 beq r0, r0, NextPage # to Next page
13319 div r8,r9
13322 This bug is present for div, divu, ddiv, and ddivu
13323 instructions.
13325 Workaround: For item 1), OS could make sure that the next page
13326 after the divide instruction is also mapped. For item 2), the
13327 compiler could make sure that the divide instruction is not in
13328 the branch delay slot."
13330 These processors have PRId values of 0x00004220 and 0x00004300 for
13331 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
13333 const char *
13334 mips_output_division (const char *division, rtx *operands)
13336 const char *s;
13338 s = division;
13339 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
13341 output_asm_insn (s, operands);
13342 s = "nop";
13344 if (TARGET_CHECK_ZERO_DIV)
13346 if (TARGET_MIPS16)
13348 output_asm_insn (s, operands);
13349 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
13351 else if (GENERATE_DIVIDE_TRAPS)
13353 /* Avoid long replay penalty on load miss by putting the trap before
13354 the divide. */
13355 if (TUNE_74K)
13356 output_asm_insn ("teq\t%2,%.,7", operands);
13357 else
13359 output_asm_insn (s, operands);
13360 s = "teq\t%2,%.,7";
13363 else
13365 output_asm_insn ("%(bne\t%2,%.,1f", operands);
13366 output_asm_insn (s, operands);
13367 s = "break\t7%)\n1:";
13370 return s;
13373 /* Return true if destination of IN_INSN is used as add source in
13374 OUT_INSN. Both IN_INSN and OUT_INSN are of type fmadd. Example:
13375 madd.s dst, x, y, z
13376 madd.s a, dst, b, c */
13378 bool
13379 mips_fmadd_bypass (rtx_insn *out_insn, rtx_insn *in_insn)
13381 int dst_reg, src_reg;
13383 gcc_assert (get_attr_type (in_insn) == TYPE_FMADD);
13384 gcc_assert (get_attr_type (out_insn) == TYPE_FMADD);
13386 extract_insn (in_insn);
13387 dst_reg = REG_P (recog_data.operand[0]);
13389 extract_insn (out_insn);
13390 src_reg = REG_P (recog_data.operand[1]);
13392 if (dst_reg == src_reg)
13393 return true;
13395 return false;
13398 /* Return true if IN_INSN is a multiply-add or multiply-subtract
13399 instruction and if OUT_INSN assigns to the accumulator operand. */
13401 bool
13402 mips_linked_madd_p (rtx_insn *out_insn, rtx_insn *in_insn)
13404 enum attr_accum_in accum_in;
13405 int accum_in_opnum;
13406 rtx accum_in_op;
13408 if (recog_memoized (in_insn) < 0)
13409 return false;
13411 accum_in = get_attr_accum_in (in_insn);
13412 if (accum_in == ACCUM_IN_NONE)
13413 return false;
13415 accum_in_opnum = accum_in - ACCUM_IN_0;
13417 extract_insn (in_insn);
13418 gcc_assert (accum_in_opnum < recog_data.n_operands);
13419 accum_in_op = recog_data.operand[accum_in_opnum];
13421 return reg_set_p (accum_in_op, out_insn);
13424 /* True if the dependency between OUT_INSN and IN_INSN is on the store
13425 data rather than the address. We need this because the cprestore
13426 pattern is type "store", but is defined using an UNSPEC_VOLATILE,
13427 which causes the default routine to abort. We just return false
13428 for that case. */
13430 bool
13431 mips_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
13433 if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
13434 return false;
13436 return !store_data_bypass_p (out_insn, in_insn);
13440 /* Variables and flags used in scheduler hooks when tuning for
13441 Loongson 2E/2F. */
13442 static struct
13444 /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
13445 strategy. */
13447 /* If true, then next ALU1/2 instruction will go to ALU1. */
13448 bool alu1_turn_p;
13450 /* If true, then next FALU1/2 unstruction will go to FALU1. */
13451 bool falu1_turn_p;
13453 /* Codes to query if [f]alu{1,2}_core units are subscribed or not. */
13454 int alu1_core_unit_code;
13455 int alu2_core_unit_code;
13456 int falu1_core_unit_code;
13457 int falu2_core_unit_code;
13459 /* True if current cycle has a multi instruction.
13460 This flag is used in mips_ls2_dfa_post_advance_cycle. */
13461 bool cycle_has_multi_p;
13463 /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
13464 These are used in mips_ls2_dfa_post_advance_cycle to initialize
13465 DFA state.
13466 E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
13467 instruction to go ALU1. */
13468 rtx_insn *alu1_turn_enabled_insn;
13469 rtx_insn *alu2_turn_enabled_insn;
13470 rtx_insn *falu1_turn_enabled_insn;
13471 rtx_insn *falu2_turn_enabled_insn;
13472 } mips_ls2;
13474 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
13475 dependencies have no cost, except on the 20Kc where output-dependence
13476 is treated like input-dependence. */
13478 static int
13479 mips_adjust_cost (rtx_insn *insn ATTRIBUTE_UNUSED, rtx link,
13480 rtx_insn *dep ATTRIBUTE_UNUSED, int cost)
13482 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
13483 && TUNE_20KC)
13484 return cost;
13485 if (REG_NOTE_KIND (link) != 0)
13486 return 0;
13487 return cost;
13490 /* Return the number of instructions that can be issued per cycle. */
13492 static int
13493 mips_issue_rate (void)
13495 switch (mips_tune)
13497 case PROCESSOR_74KC:
13498 case PROCESSOR_74KF2_1:
13499 case PROCESSOR_74KF1_1:
13500 case PROCESSOR_74KF3_2:
13501 /* The 74k is not strictly quad-issue cpu, but can be seen as one
13502 by the scheduler. It can issue 1 ALU, 1 AGEN and 2 FPU insns,
13503 but in reality only a maximum of 3 insns can be issued as
13504 floating-point loads and stores also require a slot in the
13505 AGEN pipe. */
13506 case PROCESSOR_R10000:
13507 /* All R10K Processors are quad-issue (being the first MIPS
13508 processors to support this feature). */
13509 return 4;
13511 case PROCESSOR_20KC:
13512 case PROCESSOR_R4130:
13513 case PROCESSOR_R5400:
13514 case PROCESSOR_R5500:
13515 case PROCESSOR_R5900:
13516 case PROCESSOR_R7000:
13517 case PROCESSOR_R9000:
13518 case PROCESSOR_OCTEON:
13519 case PROCESSOR_OCTEON2:
13520 case PROCESSOR_OCTEON3:
13521 return 2;
13523 case PROCESSOR_SB1:
13524 case PROCESSOR_SB1A:
13525 /* This is actually 4, but we get better performance if we claim 3.
13526 This is partly because of unwanted speculative code motion with the
13527 larger number, and partly because in most common cases we can't
13528 reach the theoretical max of 4. */
13529 return 3;
13531 case PROCESSOR_LOONGSON_2E:
13532 case PROCESSOR_LOONGSON_2F:
13533 case PROCESSOR_LOONGSON_3A:
13534 case PROCESSOR_P5600:
13535 return 4;
13537 case PROCESSOR_XLP:
13538 return (reload_completed ? 4 : 3);
13540 default:
13541 return 1;
13545 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2. */
13547 static void
13548 mips_ls2_init_dfa_post_cycle_insn (void)
13550 start_sequence ();
13551 emit_insn (gen_ls2_alu1_turn_enabled_insn ());
13552 mips_ls2.alu1_turn_enabled_insn = get_insns ();
13553 end_sequence ();
13555 start_sequence ();
13556 emit_insn (gen_ls2_alu2_turn_enabled_insn ());
13557 mips_ls2.alu2_turn_enabled_insn = get_insns ();
13558 end_sequence ();
13560 start_sequence ();
13561 emit_insn (gen_ls2_falu1_turn_enabled_insn ());
13562 mips_ls2.falu1_turn_enabled_insn = get_insns ();
13563 end_sequence ();
13565 start_sequence ();
13566 emit_insn (gen_ls2_falu2_turn_enabled_insn ());
13567 mips_ls2.falu2_turn_enabled_insn = get_insns ();
13568 end_sequence ();
13570 mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
13571 mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
13572 mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
13573 mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
13576 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
13577 Init data used in mips_dfa_post_advance_cycle. */
13579 static void
13580 mips_init_dfa_post_cycle_insn (void)
13582 if (TUNE_LOONGSON_2EF)
13583 mips_ls2_init_dfa_post_cycle_insn ();
13586 /* Initialize STATE when scheduling for Loongson 2E/2F.
13587 Support round-robin dispatch scheme by enabling only one of
13588 ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
13589 respectively. */
13591 static void
13592 mips_ls2_dfa_post_advance_cycle (state_t state)
13594 if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
13596 /* Though there are no non-pipelined ALU1 insns,
13597 we can get an instruction of type 'multi' before reload. */
13598 gcc_assert (mips_ls2.cycle_has_multi_p);
13599 mips_ls2.alu1_turn_p = false;
13602 mips_ls2.cycle_has_multi_p = false;
13604 if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
13605 /* We have a non-pipelined alu instruction in the core,
13606 adjust round-robin counter. */
13607 mips_ls2.alu1_turn_p = true;
13609 if (mips_ls2.alu1_turn_p)
13611 if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
13612 gcc_unreachable ();
13614 else
13616 if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
13617 gcc_unreachable ();
13620 if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
13622 /* There are no non-pipelined FALU1 insns. */
13623 gcc_unreachable ();
13624 mips_ls2.falu1_turn_p = false;
13627 if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
13628 /* We have a non-pipelined falu instruction in the core,
13629 adjust round-robin counter. */
13630 mips_ls2.falu1_turn_p = true;
13632 if (mips_ls2.falu1_turn_p)
13634 if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
13635 gcc_unreachable ();
13637 else
13639 if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
13640 gcc_unreachable ();
13644 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
13645 This hook is being called at the start of each cycle. */
13647 static void
13648 mips_dfa_post_advance_cycle (void)
13650 if (TUNE_LOONGSON_2EF)
13651 mips_ls2_dfa_post_advance_cycle (curr_state);
13654 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
13655 be as wide as the scheduling freedom in the DFA. */
13657 static int
13658 mips_multipass_dfa_lookahead (void)
13660 /* Can schedule up to 4 of the 6 function units in any one cycle. */
13661 if (TUNE_SB1)
13662 return 4;
13664 if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
13665 return 4;
13667 if (TUNE_OCTEON)
13668 return 2;
13670 if (TUNE_P5600)
13671 return 4;
13673 return 0;
13676 /* Remove the instruction at index LOWER from ready queue READY and
13677 reinsert it in front of the instruction at index HIGHER. LOWER must
13678 be <= HIGHER. */
13680 static void
13681 mips_promote_ready (rtx_insn **ready, int lower, int higher)
13683 rtx_insn *new_head;
13684 int i;
13686 new_head = ready[lower];
13687 for (i = lower; i < higher; i++)
13688 ready[i] = ready[i + 1];
13689 ready[i] = new_head;
13692 /* If the priority of the instruction at POS2 in the ready queue READY
13693 is within LIMIT units of that of the instruction at POS1, swap the
13694 instructions if POS2 is not already less than POS1. */
13696 static void
13697 mips_maybe_swap_ready (rtx_insn **ready, int pos1, int pos2, int limit)
13699 if (pos1 < pos2
13700 && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
13702 rtx_insn *temp;
13704 temp = ready[pos1];
13705 ready[pos1] = ready[pos2];
13706 ready[pos2] = temp;
13710 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
13711 that may clobber hi or lo. */
13712 static rtx_insn *mips_macc_chains_last_hilo;
13714 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
13715 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
13717 static void
13718 mips_macc_chains_record (rtx_insn *insn)
13720 if (get_attr_may_clobber_hilo (insn))
13721 mips_macc_chains_last_hilo = insn;
13724 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
13725 has NREADY elements, looking for a multiply-add or multiply-subtract
13726 instruction that is cumulative with mips_macc_chains_last_hilo.
13727 If there is one, promote it ahead of anything else that might
13728 clobber hi or lo. */
13730 static void
13731 mips_macc_chains_reorder (rtx_insn **ready, int nready)
13733 int i, j;
13735 if (mips_macc_chains_last_hilo != 0)
13736 for (i = nready - 1; i >= 0; i--)
13737 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
13739 for (j = nready - 1; j > i; j--)
13740 if (recog_memoized (ready[j]) >= 0
13741 && get_attr_may_clobber_hilo (ready[j]))
13743 mips_promote_ready (ready, i, j);
13744 break;
13746 break;
13750 /* The last instruction to be scheduled. */
13751 static rtx_insn *vr4130_last_insn;
13753 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
13754 points to an rtx that is initially an instruction. Nullify the rtx
13755 if the instruction uses the value of register X. */
13757 static void
13758 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
13759 void *data)
13761 rtx *insn_ptr;
13763 insn_ptr = (rtx *) data;
13764 if (REG_P (x)
13765 && *insn_ptr != 0
13766 && reg_referenced_p (x, PATTERN (*insn_ptr)))
13767 *insn_ptr = 0;
13770 /* Return true if there is true register dependence between vr4130_last_insn
13771 and INSN. */
13773 static bool
13774 vr4130_true_reg_dependence_p (rtx insn)
13776 note_stores (PATTERN (vr4130_last_insn),
13777 vr4130_true_reg_dependence_p_1, &insn);
13778 return insn == 0;
13781 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
13782 the ready queue and that INSN2 is the instruction after it, return
13783 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
13784 in which INSN1 and INSN2 can probably issue in parallel, but for
13785 which (INSN2, INSN1) should be less sensitive to instruction
13786 alignment than (INSN1, INSN2). See 4130.md for more details. */
13788 static bool
13789 vr4130_swap_insns_p (rtx_insn *insn1, rtx_insn *insn2)
13791 sd_iterator_def sd_it;
13792 dep_t dep;
13794 /* Check for the following case:
13796 1) there is some other instruction X with an anti dependence on INSN1;
13797 2) X has a higher priority than INSN2; and
13798 3) X is an arithmetic instruction (and thus has no unit restrictions).
13800 If INSN1 is the last instruction blocking X, it would better to
13801 choose (INSN1, X) over (INSN2, INSN1). */
13802 FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
13803 if (DEP_TYPE (dep) == REG_DEP_ANTI
13804 && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
13805 && recog_memoized (DEP_CON (dep)) >= 0
13806 && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
13807 return false;
13809 if (vr4130_last_insn != 0
13810 && recog_memoized (insn1) >= 0
13811 && recog_memoized (insn2) >= 0)
13813 /* See whether INSN1 and INSN2 use different execution units,
13814 or if they are both ALU-type instructions. If so, they can
13815 probably execute in parallel. */
13816 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
13817 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
13818 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
13820 /* If only one of the instructions has a dependence on
13821 vr4130_last_insn, prefer to schedule the other one first. */
13822 bool dep1_p = vr4130_true_reg_dependence_p (insn1);
13823 bool dep2_p = vr4130_true_reg_dependence_p (insn2);
13824 if (dep1_p != dep2_p)
13825 return dep1_p;
13827 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
13828 is not an ALU-type instruction and if INSN1 uses the same
13829 execution unit. (Note that if this condition holds, we already
13830 know that INSN2 uses a different execution unit.) */
13831 if (class1 != VR4130_CLASS_ALU
13832 && recog_memoized (vr4130_last_insn) >= 0
13833 && class1 == get_attr_vr4130_class (vr4130_last_insn))
13834 return true;
13837 return false;
13840 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
13841 queue with at least two instructions. Swap the first two if
13842 vr4130_swap_insns_p says that it could be worthwhile. */
13844 static void
13845 vr4130_reorder (rtx_insn **ready, int nready)
13847 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
13848 mips_promote_ready (ready, nready - 2, nready - 1);
13851 /* Record whether last 74k AGEN instruction was a load or store. */
13852 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
13854 /* Initialize mips_last_74k_agen_insn from INSN. A null argument
13855 resets to TYPE_UNKNOWN state. */
13857 static void
13858 mips_74k_agen_init (rtx_insn *insn)
13860 if (!insn || CALL_P (insn) || JUMP_P (insn))
13861 mips_last_74k_agen_insn = TYPE_UNKNOWN;
13862 else
13864 enum attr_type type = get_attr_type (insn);
13865 if (type == TYPE_LOAD || type == TYPE_STORE)
13866 mips_last_74k_agen_insn = type;
13870 /* A TUNE_74K helper function. The 74K AGEN pipeline likes multiple
13871 loads to be grouped together, and multiple stores to be grouped
13872 together. Swap things around in the ready queue to make this happen. */
13874 static void
13875 mips_74k_agen_reorder (rtx_insn **ready, int nready)
13877 int i;
13878 int store_pos, load_pos;
13880 store_pos = -1;
13881 load_pos = -1;
13883 for (i = nready - 1; i >= 0; i--)
13885 rtx_insn *insn = ready[i];
13886 if (USEFUL_INSN_P (insn))
13887 switch (get_attr_type (insn))
13889 case TYPE_STORE:
13890 if (store_pos == -1)
13891 store_pos = i;
13892 break;
13894 case TYPE_LOAD:
13895 if (load_pos == -1)
13896 load_pos = i;
13897 break;
13899 default:
13900 break;
13904 if (load_pos == -1 || store_pos == -1)
13905 return;
13907 switch (mips_last_74k_agen_insn)
13909 case TYPE_UNKNOWN:
13910 /* Prefer to schedule loads since they have a higher latency. */
13911 case TYPE_LOAD:
13912 /* Swap loads to the front of the queue. */
13913 mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
13914 break;
13915 case TYPE_STORE:
13916 /* Swap stores to the front of the queue. */
13917 mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
13918 break;
13919 default:
13920 break;
13924 /* Implement TARGET_SCHED_INIT. */
13926 static void
13927 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13928 int max_ready ATTRIBUTE_UNUSED)
13930 mips_macc_chains_last_hilo = 0;
13931 vr4130_last_insn = 0;
13932 mips_74k_agen_init (NULL);
13934 /* When scheduling for Loongson2, branch instructions go to ALU1,
13935 therefore basic block is most likely to start with round-robin counter
13936 pointed to ALU2. */
13937 mips_ls2.alu1_turn_p = false;
13938 mips_ls2.falu1_turn_p = true;
13941 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2. */
13943 static void
13944 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13945 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13947 if (!reload_completed
13948 && TUNE_MACC_CHAINS
13949 && *nreadyp > 0)
13950 mips_macc_chains_reorder (ready, *nreadyp);
13952 if (reload_completed
13953 && TUNE_MIPS4130
13954 && !TARGET_VR4130_ALIGN
13955 && *nreadyp > 1)
13956 vr4130_reorder (ready, *nreadyp);
13958 if (TUNE_74K)
13959 mips_74k_agen_reorder (ready, *nreadyp);
13962 /* Implement TARGET_SCHED_REORDER. */
13964 static int
13965 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13966 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13968 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13969 return mips_issue_rate ();
13972 /* Implement TARGET_SCHED_REORDER2. */
13974 static int
13975 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13976 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13978 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13979 return cached_can_issue_more;
13982 /* Update round-robin counters for ALU1/2 and FALU1/2. */
13984 static void
13985 mips_ls2_variable_issue (rtx_insn *insn)
13987 if (mips_ls2.alu1_turn_p)
13989 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
13990 mips_ls2.alu1_turn_p = false;
13992 else
13994 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
13995 mips_ls2.alu1_turn_p = true;
13998 if (mips_ls2.falu1_turn_p)
14000 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
14001 mips_ls2.falu1_turn_p = false;
14003 else
14005 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
14006 mips_ls2.falu1_turn_p = true;
14009 if (recog_memoized (insn) >= 0)
14010 mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
14013 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
14015 static int
14016 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
14017 rtx_insn *insn, int more)
14019 /* Ignore USEs and CLOBBERs; don't count them against the issue rate. */
14020 if (USEFUL_INSN_P (insn))
14022 if (get_attr_type (insn) != TYPE_GHOST)
14023 more--;
14024 if (!reload_completed && TUNE_MACC_CHAINS)
14025 mips_macc_chains_record (insn);
14026 vr4130_last_insn = insn;
14027 if (TUNE_74K)
14028 mips_74k_agen_init (insn);
14029 else if (TUNE_LOONGSON_2EF)
14030 mips_ls2_variable_issue (insn);
14033 /* Instructions of type 'multi' should all be split before
14034 the second scheduling pass. */
14035 gcc_assert (!reload_completed
14036 || recog_memoized (insn) < 0
14037 || get_attr_type (insn) != TYPE_MULTI);
14039 cached_can_issue_more = more;
14040 return more;
14043 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
14044 return the first operand of the associated PREF or PREFX insn. */
14047 mips_prefetch_cookie (rtx write, rtx locality)
14049 /* store_streamed / load_streamed. */
14050 if (INTVAL (locality) <= 0)
14051 return GEN_INT (INTVAL (write) + 4);
14053 /* store / load. */
14054 if (INTVAL (locality) <= 2)
14055 return write;
14057 /* store_retained / load_retained. */
14058 return GEN_INT (INTVAL (write) + 6);
14061 /* Flags that indicate when a built-in function is available.
14063 BUILTIN_AVAIL_NON_MIPS16
14064 The function is available on the current target if !TARGET_MIPS16.
14066 BUILTIN_AVAIL_MIPS16
14067 The function is available on the current target if TARGET_MIPS16. */
14068 #define BUILTIN_AVAIL_NON_MIPS16 1
14069 #define BUILTIN_AVAIL_MIPS16 2
14071 /* Declare an availability predicate for built-in functions that
14072 require non-MIPS16 mode and also require COND to be true.
14073 NAME is the main part of the predicate's name. */
14074 #define AVAIL_NON_MIPS16(NAME, COND) \
14075 static unsigned int \
14076 mips_builtin_avail_##NAME (void) \
14078 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0; \
14081 /* Declare an availability predicate for built-in functions that
14082 support both MIPS16 and non-MIPS16 code and also require COND
14083 to be true. NAME is the main part of the predicate's name. */
14084 #define AVAIL_ALL(NAME, COND) \
14085 static unsigned int \
14086 mips_builtin_avail_##NAME (void) \
14088 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 | BUILTIN_AVAIL_MIPS16 : 0; \
14091 /* This structure describes a single built-in function. */
14092 struct mips_builtin_description {
14093 /* The code of the main .md file instruction. See mips_builtin_type
14094 for more information. */
14095 enum insn_code icode;
14097 /* The floating-point comparison code to use with ICODE, if any. */
14098 enum mips_fp_condition cond;
14100 /* The name of the built-in function. */
14101 const char *name;
14103 /* Specifies how the function should be expanded. */
14104 enum mips_builtin_type builtin_type;
14106 /* The function's prototype. */
14107 enum mips_function_type function_type;
14109 /* Whether the function is available. */
14110 unsigned int (*avail) (void);
14113 AVAIL_ALL (hard_float, TARGET_HARD_FLOAT_ABI)
14114 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
14115 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
14116 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
14117 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
14118 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
14119 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
14120 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
14121 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
14122 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
14123 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
14125 /* Construct a mips_builtin_description from the given arguments.
14127 INSN is the name of the associated instruction pattern, without the
14128 leading CODE_FOR_mips_.
14130 CODE is the floating-point condition code associated with the
14131 function. It can be 'f' if the field is not applicable.
14133 NAME is the name of the function itself, without the leading
14134 "__builtin_mips_".
14136 BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
14138 AVAIL is the name of the availability predicate, without the leading
14139 mips_builtin_avail_. */
14140 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE, \
14141 FUNCTION_TYPE, AVAIL) \
14142 { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND, \
14143 "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE, \
14144 mips_builtin_avail_ ## AVAIL }
14146 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
14147 mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE and AVAIL
14148 are as for MIPS_BUILTIN. */
14149 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
14150 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
14152 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
14153 are subject to mips_builtin_avail_<AVAIL>. */
14154 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL) \
14155 MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s", \
14156 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL), \
14157 MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d", \
14158 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
14160 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
14161 The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
14162 while the any and all forms are subject to mips_builtin_avail_mips3d. */
14163 #define CMP_PS_BUILTINS(INSN, COND, AVAIL) \
14164 MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps", \
14165 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF, \
14166 mips3d), \
14167 MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps", \
14168 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF, \
14169 mips3d), \
14170 MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
14171 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF, \
14172 AVAIL), \
14173 MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
14174 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF, \
14175 AVAIL)
14177 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s. The functions
14178 are subject to mips_builtin_avail_mips3d. */
14179 #define CMP_4S_BUILTINS(INSN, COND) \
14180 MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s", \
14181 MIPS_BUILTIN_CMP_ANY, \
14182 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d), \
14183 MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s", \
14184 MIPS_BUILTIN_CMP_ALL, \
14185 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
14187 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps. The comparison
14188 instruction requires mips_builtin_avail_<AVAIL>. */
14189 #define MOVTF_BUILTINS(INSN, COND, AVAIL) \
14190 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps", \
14191 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
14192 AVAIL), \
14193 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps", \
14194 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
14195 AVAIL)
14197 /* Define all the built-in functions related to C.cond.fmt condition COND. */
14198 #define CMP_BUILTINS(COND) \
14199 MOVTF_BUILTINS (c, COND, paired_single), \
14200 MOVTF_BUILTINS (cabs, COND, mips3d), \
14201 CMP_SCALAR_BUILTINS (cabs, COND, mips3d), \
14202 CMP_PS_BUILTINS (c, COND, paired_single), \
14203 CMP_PS_BUILTINS (cabs, COND, mips3d), \
14204 CMP_4S_BUILTINS (c, COND), \
14205 CMP_4S_BUILTINS (cabs, COND)
14207 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
14208 function mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE
14209 and AVAIL are as for MIPS_BUILTIN. */
14210 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
14211 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET, \
14212 FUNCTION_TYPE, AVAIL)
14214 /* Define __builtin_mips_bposge<VALUE>. <VALUE> is 32 for the MIPS32 DSP
14215 branch instruction. AVAIL is as for MIPS_BUILTIN. */
14216 #define BPOSGE_BUILTIN(VALUE, AVAIL) \
14217 MIPS_BUILTIN (bposge, f, "bposge" #VALUE, \
14218 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
14220 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
14221 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
14222 builtin_description field. */
14223 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE) \
14224 { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f, \
14225 "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT, \
14226 FUNCTION_TYPE, mips_builtin_avail_loongson }
14228 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
14229 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
14230 builtin_description field. */
14231 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE) \
14232 LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
14234 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
14235 We use functions of this form when the same insn can be usefully applied
14236 to more than one datatype. */
14237 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE) \
14238 LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
14240 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
14241 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
14242 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
14243 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
14244 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
14245 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
14246 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
14247 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
14249 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
14250 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
14251 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
14252 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
14253 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
14254 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
14255 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
14256 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
14257 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
14258 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
14259 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
14260 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
14261 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
14262 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
14263 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
14264 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
14265 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
14266 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
14267 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
14268 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
14269 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
14270 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
14271 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
14272 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
14273 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
14274 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
14275 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
14276 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
14277 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
14278 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
14280 static const struct mips_builtin_description mips_builtins[] = {
14281 #define MIPS_GET_FCSR 0
14282 DIRECT_BUILTIN (get_fcsr, MIPS_USI_FTYPE_VOID, hard_float),
14283 #define MIPS_SET_FCSR 1
14284 DIRECT_NO_TARGET_BUILTIN (set_fcsr, MIPS_VOID_FTYPE_USI, hard_float),
14286 DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
14287 DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
14288 DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
14289 DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
14290 DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
14291 DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
14292 DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
14293 DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
14295 DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
14296 DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
14297 DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
14298 DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
14299 DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
14301 DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
14302 DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
14303 DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
14304 DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
14305 DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
14306 DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
14308 DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
14309 DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
14310 DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
14311 DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
14312 DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
14313 DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
14315 MIPS_FP_CONDITIONS (CMP_BUILTINS),
14317 /* Built-in functions for the SB-1 processor. */
14318 DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
14320 /* Built-in functions for the DSP ASE (32-bit and 64-bit). */
14321 DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14322 DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14323 DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
14324 DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
14325 DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
14326 DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14327 DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14328 DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
14329 DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
14330 DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
14331 DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
14332 DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
14333 DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
14334 DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
14335 DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
14336 DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
14337 DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
14338 DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
14339 DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
14340 DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
14341 DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
14342 DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
14343 DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
14344 DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
14345 DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
14346 DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
14347 DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
14348 DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
14349 DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
14350 DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
14351 DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
14352 DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14353 DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14354 DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
14355 DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
14356 DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14357 DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14358 DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
14359 DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
14360 DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
14361 DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14362 DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
14363 DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
14364 DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
14365 DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
14366 DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
14367 DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
14368 DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14369 DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14370 DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14371 DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14372 DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14373 DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14374 DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14375 DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14376 DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14377 DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
14378 DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14379 DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14380 DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
14381 DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
14382 DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
14383 DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
14384 DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
14385 BPOSGE_BUILTIN (32, dsp),
14387 /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit). */
14388 DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
14389 DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14390 DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14391 DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14392 DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14393 DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14394 DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14395 DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14396 DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14397 DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14398 DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14399 DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14400 DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14401 DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14402 DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14403 DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
14404 DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
14405 DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
14406 DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14407 DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
14408 DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
14409 DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
14410 DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14411 DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14412 DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14413 DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14414 DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14415 DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14416 DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14417 DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14418 DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14419 DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14420 DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14421 DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14423 /* Built-in functions for the DSP ASE (32-bit only). */
14424 DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14425 DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14426 DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14427 DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14428 DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14429 DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14430 DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14431 DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14432 DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14433 DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14434 DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14435 DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14436 DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14437 DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14438 DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14439 DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14440 DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
14441 DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
14442 DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
14443 DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
14444 DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
14445 DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14446 DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
14447 DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14448 DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
14449 DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
14450 DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
14452 /* Built-in functions for the DSP ASE (64-bit only). */
14453 DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
14455 /* The following are for the MIPS DSP ASE REV 2 (32-bit only). */
14456 DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14457 DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14458 DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14459 DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14460 DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14461 DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14462 DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14463 DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14464 DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14466 /* Builtin functions for ST Microelectronics Loongson-2E/2F cores. */
14467 LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
14468 LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
14469 LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
14470 LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14471 LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14472 LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14473 LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14474 LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14475 LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14476 LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
14477 LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
14478 LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14479 LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14480 LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14481 LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14482 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
14483 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14484 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14485 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14486 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
14487 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
14488 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14489 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14490 LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14491 LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14492 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14493 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14494 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14495 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14496 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14497 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14498 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14499 LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14500 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14501 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14502 LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14503 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14504 LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
14505 LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
14506 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14507 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14508 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14509 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14510 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14511 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14512 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14513 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14514 LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
14515 LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14516 LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14517 LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14518 LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14519 LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
14520 LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
14521 LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14522 LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14523 LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14524 LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
14525 LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14526 LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
14527 LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
14528 LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14529 LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14530 LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14531 LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14532 LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14533 LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14534 LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14535 LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14536 LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14537 LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14538 LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14539 LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14540 LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14541 LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14542 LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14543 LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14544 LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14545 LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14546 LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14547 LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14548 LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
14549 LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
14550 LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14551 LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14552 LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14553 LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14554 LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14555 LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14556 LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14557 LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14558 LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14559 LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14560 LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14561 LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14562 LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14563 LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14564 LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14565 LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14567 /* Sundry other built-in functions. */
14568 DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
14571 /* Index I is the function declaration for mips_builtins[I], or null if the
14572 function isn't defined on this target. */
14573 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
14575 /* MODE is a vector mode whose elements have type TYPE. Return the type
14576 of the vector itself. */
14578 static tree
14579 mips_builtin_vector_type (tree type, machine_mode mode)
14581 static tree types[2 * (int) MAX_MACHINE_MODE];
14582 int mode_index;
14584 mode_index = (int) mode;
14586 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
14587 mode_index += MAX_MACHINE_MODE;
14589 if (types[mode_index] == NULL_TREE)
14590 types[mode_index] = build_vector_type_for_mode (type, mode);
14591 return types[mode_index];
14594 /* Return a type for 'const volatile void *'. */
14596 static tree
14597 mips_build_cvpointer_type (void)
14599 static tree cache;
14601 if (cache == NULL_TREE)
14602 cache = build_pointer_type (build_qualified_type
14603 (void_type_node,
14604 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
14605 return cache;
14608 /* Source-level argument types. */
14609 #define MIPS_ATYPE_VOID void_type_node
14610 #define MIPS_ATYPE_INT integer_type_node
14611 #define MIPS_ATYPE_POINTER ptr_type_node
14612 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
14614 /* Standard mode-based argument types. */
14615 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
14616 #define MIPS_ATYPE_SI intSI_type_node
14617 #define MIPS_ATYPE_USI unsigned_intSI_type_node
14618 #define MIPS_ATYPE_DI intDI_type_node
14619 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
14620 #define MIPS_ATYPE_SF float_type_node
14621 #define MIPS_ATYPE_DF double_type_node
14623 /* Vector argument types. */
14624 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
14625 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
14626 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
14627 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
14628 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
14629 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
14630 #define MIPS_ATYPE_UV2SI \
14631 mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
14632 #define MIPS_ATYPE_UV4HI \
14633 mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
14634 #define MIPS_ATYPE_UV8QI \
14635 mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
14637 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
14638 their associated MIPS_ATYPEs. */
14639 #define MIPS_FTYPE_ATYPES1(A, B) \
14640 MIPS_ATYPE_##A, MIPS_ATYPE_##B
14642 #define MIPS_FTYPE_ATYPES2(A, B, C) \
14643 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
14645 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
14646 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
14648 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
14649 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
14650 MIPS_ATYPE_##E
14652 /* Return the function type associated with function prototype TYPE. */
14654 static tree
14655 mips_build_function_type (enum mips_function_type type)
14657 static tree types[(int) MIPS_MAX_FTYPE_MAX];
14659 if (types[(int) type] == NULL_TREE)
14660 switch (type)
14662 #define DEF_MIPS_FTYPE(NUM, ARGS) \
14663 case MIPS_FTYPE_NAME##NUM ARGS: \
14664 types[(int) type] \
14665 = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS, \
14666 NULL_TREE); \
14667 break;
14668 #include "config/mips/mips-ftypes.def"
14669 #undef DEF_MIPS_FTYPE
14670 default:
14671 gcc_unreachable ();
14674 return types[(int) type];
14677 /* Implement TARGET_INIT_BUILTINS. */
14679 static void
14680 mips_init_builtins (void)
14682 const struct mips_builtin_description *d;
14683 unsigned int i;
14685 /* Iterate through all of the bdesc arrays, initializing all of the
14686 builtin functions. */
14687 for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
14689 d = &mips_builtins[i];
14690 if (d->avail ())
14691 mips_builtin_decls[i]
14692 = add_builtin_function (d->name,
14693 mips_build_function_type (d->function_type),
14694 i, BUILT_IN_MD, NULL, NULL);
14698 /* Implement TARGET_BUILTIN_DECL. */
14700 static tree
14701 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
14703 if (code >= ARRAY_SIZE (mips_builtins))
14704 return error_mark_node;
14705 return mips_builtin_decls[code];
14708 /* Take argument ARGNO from EXP's argument list and convert it into
14709 an expand operand. Store the operand in *OP. */
14711 static void
14712 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
14713 unsigned int argno)
14715 tree arg;
14716 rtx value;
14718 arg = CALL_EXPR_ARG (exp, argno);
14719 value = expand_normal (arg);
14720 create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
14723 /* Expand instruction ICODE as part of a built-in function sequence.
14724 Use the first NOPS elements of OPS as the instruction's operands.
14725 HAS_TARGET_P is true if operand 0 is a target; it is false if the
14726 instruction has no target.
14728 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
14730 static rtx
14731 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
14732 struct expand_operand *ops, bool has_target_p)
14734 if (!maybe_expand_insn (icode, nops, ops))
14736 error ("invalid argument to built-in function");
14737 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
14739 return has_target_p ? ops[0].value : const0_rtx;
14742 /* Expand a floating-point comparison for built-in function call EXP.
14743 The first NARGS arguments are the values to be compared. ICODE is
14744 the .md pattern that does the comparison and COND is the condition
14745 that is being tested. Return an rtx for the result. */
14747 static rtx
14748 mips_expand_builtin_compare_1 (enum insn_code icode,
14749 enum mips_fp_condition cond,
14750 tree exp, int nargs)
14752 struct expand_operand ops[MAX_RECOG_OPERANDS];
14753 rtx output;
14754 int opno, argno;
14756 /* The instruction should have a target operand, an operand for each
14757 argument, and an operand for COND. */
14758 gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
14760 output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
14761 opno = 0;
14762 create_fixed_operand (&ops[opno++], output);
14763 for (argno = 0; argno < nargs; argno++)
14764 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14765 create_integer_operand (&ops[opno++], (int) cond);
14766 return mips_expand_builtin_insn (icode, opno, ops, true);
14769 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
14770 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
14771 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
14772 suggests a good place to put the result. */
14774 static rtx
14775 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
14776 bool has_target_p)
14778 struct expand_operand ops[MAX_RECOG_OPERANDS];
14779 int opno, argno;
14781 /* Map any target to operand 0. */
14782 opno = 0;
14783 if (has_target_p)
14784 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
14786 /* Map the arguments to the other operands. */
14787 gcc_assert (opno + call_expr_nargs (exp)
14788 == insn_data[icode].n_generator_args);
14789 for (argno = 0; argno < call_expr_nargs (exp); argno++)
14790 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14792 return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
14795 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
14796 function; TYPE says which. EXP is the CALL_EXPR that calls the
14797 function, ICODE is the instruction that should be used to compare
14798 the first two arguments, and COND is the condition it should test.
14799 TARGET, if nonnull, suggests a good place to put the result. */
14801 static rtx
14802 mips_expand_builtin_movtf (enum mips_builtin_type type,
14803 enum insn_code icode, enum mips_fp_condition cond,
14804 rtx target, tree exp)
14806 struct expand_operand ops[4];
14807 rtx cmp_result;
14809 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
14810 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
14811 if (type == MIPS_BUILTIN_MOVT)
14813 mips_prepare_builtin_arg (&ops[2], exp, 2);
14814 mips_prepare_builtin_arg (&ops[1], exp, 3);
14816 else
14818 mips_prepare_builtin_arg (&ops[1], exp, 2);
14819 mips_prepare_builtin_arg (&ops[2], exp, 3);
14821 create_fixed_operand (&ops[3], cmp_result);
14822 return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
14823 4, ops, true);
14826 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
14827 into TARGET otherwise. Return TARGET. */
14829 static rtx
14830 mips_builtin_branch_and_move (rtx condition, rtx target,
14831 rtx value_if_true, rtx value_if_false)
14833 rtx_code_label *true_label, *done_label;
14835 true_label = gen_label_rtx ();
14836 done_label = gen_label_rtx ();
14838 /* First assume that CONDITION is false. */
14839 mips_emit_move (target, value_if_false);
14841 /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise. */
14842 emit_jump_insn (gen_condjump (condition, true_label));
14843 emit_jump_insn (gen_jump (done_label));
14844 emit_barrier ();
14846 /* Fix TARGET if CONDITION is true. */
14847 emit_label (true_label);
14848 mips_emit_move (target, value_if_true);
14850 emit_label (done_label);
14851 return target;
14854 /* Expand a comparison built-in function of type BUILTIN_TYPE. EXP is
14855 the CALL_EXPR that calls the function, ICODE is the code of the
14856 comparison instruction, and COND is the condition it should test.
14857 TARGET, if nonnull, suggests a good place to put the boolean result. */
14859 static rtx
14860 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
14861 enum insn_code icode, enum mips_fp_condition cond,
14862 rtx target, tree exp)
14864 rtx offset, condition, cmp_result;
14866 if (target == 0 || GET_MODE (target) != SImode)
14867 target = gen_reg_rtx (SImode);
14868 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
14869 call_expr_nargs (exp));
14871 /* If the comparison sets more than one register, we define the result
14872 to be 0 if all registers are false and -1 if all registers are true.
14873 The value of the complete result is indeterminate otherwise. */
14874 switch (builtin_type)
14876 case MIPS_BUILTIN_CMP_ALL:
14877 condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
14878 return mips_builtin_branch_and_move (condition, target,
14879 const0_rtx, const1_rtx);
14881 case MIPS_BUILTIN_CMP_UPPER:
14882 case MIPS_BUILTIN_CMP_LOWER:
14883 offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
14884 condition = gen_single_cc (cmp_result, offset);
14885 return mips_builtin_branch_and_move (condition, target,
14886 const1_rtx, const0_rtx);
14888 default:
14889 condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
14890 return mips_builtin_branch_and_move (condition, target,
14891 const1_rtx, const0_rtx);
14895 /* Expand a bposge built-in function of type BUILTIN_TYPE. TARGET,
14896 if nonnull, suggests a good place to put the boolean result. */
14898 static rtx
14899 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
14901 rtx condition, cmp_result;
14902 int cmp_value;
14904 if (target == 0 || GET_MODE (target) != SImode)
14905 target = gen_reg_rtx (SImode);
14907 cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
14909 if (builtin_type == MIPS_BUILTIN_BPOSGE32)
14910 cmp_value = 32;
14911 else
14912 gcc_assert (0);
14914 condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
14915 return mips_builtin_branch_and_move (condition, target,
14916 const1_rtx, const0_rtx);
14919 /* Implement TARGET_EXPAND_BUILTIN. */
14921 static rtx
14922 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
14923 machine_mode mode, int ignore)
14925 tree fndecl;
14926 unsigned int fcode, avail;
14927 const struct mips_builtin_description *d;
14929 fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14930 fcode = DECL_FUNCTION_CODE (fndecl);
14931 gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
14932 d = &mips_builtins[fcode];
14933 avail = d->avail ();
14934 gcc_assert (avail != 0);
14935 if (TARGET_MIPS16 && !(avail & BUILTIN_AVAIL_MIPS16))
14937 error ("built-in function %qE not supported for MIPS16",
14938 DECL_NAME (fndecl));
14939 return ignore ? const0_rtx : CONST0_RTX (mode);
14941 switch (d->builtin_type)
14943 case MIPS_BUILTIN_DIRECT:
14944 return mips_expand_builtin_direct (d->icode, target, exp, true);
14946 case MIPS_BUILTIN_DIRECT_NO_TARGET:
14947 return mips_expand_builtin_direct (d->icode, target, exp, false);
14949 case MIPS_BUILTIN_MOVT:
14950 case MIPS_BUILTIN_MOVF:
14951 return mips_expand_builtin_movtf (d->builtin_type, d->icode,
14952 d->cond, target, exp);
14954 case MIPS_BUILTIN_CMP_ANY:
14955 case MIPS_BUILTIN_CMP_ALL:
14956 case MIPS_BUILTIN_CMP_UPPER:
14957 case MIPS_BUILTIN_CMP_LOWER:
14958 case MIPS_BUILTIN_CMP_SINGLE:
14959 return mips_expand_builtin_compare (d->builtin_type, d->icode,
14960 d->cond, target, exp);
14962 case MIPS_BUILTIN_BPOSGE32:
14963 return mips_expand_builtin_bposge (d->builtin_type, target);
14965 gcc_unreachable ();
14968 /* An entry in the MIPS16 constant pool. VALUE is the pool constant,
14969 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
14970 struct mips16_constant {
14971 struct mips16_constant *next;
14972 rtx value;
14973 rtx_code_label *label;
14974 machine_mode mode;
14977 /* Information about an incomplete MIPS16 constant pool. FIRST is the
14978 first constant, HIGHEST_ADDRESS is the highest address that the first
14979 byte of the pool can have, and INSN_ADDRESS is the current instruction
14980 address. */
14981 struct mips16_constant_pool {
14982 struct mips16_constant *first;
14983 int highest_address;
14984 int insn_address;
14987 /* Add constant VALUE to POOL and return its label. MODE is the
14988 value's mode (used for CONST_INTs, etc.). */
14990 static rtx_code_label *
14991 mips16_add_constant (struct mips16_constant_pool *pool,
14992 rtx value, machine_mode mode)
14994 struct mips16_constant **p, *c;
14995 bool first_of_size_p;
14997 /* See whether the constant is already in the pool. If so, return the
14998 existing label, otherwise leave P pointing to the place where the
14999 constant should be added.
15001 Keep the pool sorted in increasing order of mode size so that we can
15002 reduce the number of alignments needed. */
15003 first_of_size_p = true;
15004 for (p = &pool->first; *p != 0; p = &(*p)->next)
15006 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
15007 return (*p)->label;
15008 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
15009 break;
15010 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
15011 first_of_size_p = false;
15014 /* In the worst case, the constant needed by the earliest instruction
15015 will end up at the end of the pool. The entire pool must then be
15016 accessible from that instruction.
15018 When adding the first constant, set the pool's highest address to
15019 the address of the first out-of-range byte. Adjust this address
15020 downwards each time a new constant is added. */
15021 if (pool->first == 0)
15022 /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
15023 of the instruction with the lowest two bits clear. The base PC
15024 value for LDPC has the lowest three bits clear. Assume the worst
15025 case here; namely that the PC-relative instruction occupies the
15026 last 2 bytes in an aligned word. */
15027 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
15028 pool->highest_address -= GET_MODE_SIZE (mode);
15029 if (first_of_size_p)
15030 /* Take into account the worst possible padding due to alignment. */
15031 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
15033 /* Create a new entry. */
15034 c = XNEW (struct mips16_constant);
15035 c->value = value;
15036 c->mode = mode;
15037 c->label = gen_label_rtx ();
15038 c->next = *p;
15039 *p = c;
15041 return c->label;
15044 /* Output constant VALUE after instruction INSN and return the last
15045 instruction emitted. MODE is the mode of the constant. */
15047 static rtx_insn *
15048 mips16_emit_constants_1 (machine_mode mode, rtx value, rtx_insn *insn)
15050 if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
15052 rtx size = GEN_INT (GET_MODE_SIZE (mode));
15053 return emit_insn_after (gen_consttable_int (value, size), insn);
15056 if (SCALAR_FLOAT_MODE_P (mode))
15057 return emit_insn_after (gen_consttable_float (value), insn);
15059 if (VECTOR_MODE_P (mode))
15061 int i;
15063 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
15064 insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
15065 CONST_VECTOR_ELT (value, i), insn);
15066 return insn;
15069 gcc_unreachable ();
15072 /* Dump out the constants in CONSTANTS after INSN. */
15074 static void
15075 mips16_emit_constants (struct mips16_constant *constants, rtx_insn *insn)
15077 struct mips16_constant *c, *next;
15078 int align;
15080 align = 0;
15081 for (c = constants; c != NULL; c = next)
15083 /* If necessary, increase the alignment of PC. */
15084 if (align < GET_MODE_SIZE (c->mode))
15086 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
15087 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
15089 align = GET_MODE_SIZE (c->mode);
15091 insn = emit_label_after (c->label, insn);
15092 insn = mips16_emit_constants_1 (c->mode, c->value, insn);
15094 next = c->next;
15095 free (c);
15098 emit_barrier_after (insn);
15101 /* Return the length of instruction INSN. */
15103 static int
15104 mips16_insn_length (rtx_insn *insn)
15106 if (JUMP_TABLE_DATA_P (insn))
15108 rtx body = PATTERN (insn);
15109 if (GET_CODE (body) == ADDR_VEC)
15110 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
15111 else if (GET_CODE (body) == ADDR_DIFF_VEC)
15112 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
15113 else
15114 gcc_unreachable ();
15116 return get_attr_length (insn);
15119 /* If *X is a symbolic constant that refers to the constant pool, add
15120 the constant to POOL and rewrite *X to use the constant's label. */
15122 static void
15123 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
15125 rtx base, offset;
15126 rtx_code_label *label;
15128 split_const (*x, &base, &offset);
15129 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
15131 label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
15132 get_pool_mode (base));
15133 base = gen_rtx_LABEL_REF (Pmode, label);
15134 *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
15138 /* Rewrite INSN so that constant pool references refer to the constant's
15139 label instead. */
15141 static void
15142 mips16_rewrite_pool_refs (rtx_insn *insn, struct mips16_constant_pool *pool)
15144 subrtx_ptr_iterator::array_type array;
15145 FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), ALL)
15147 rtx *loc = *iter;
15149 if (force_to_mem_operand (*loc, Pmode))
15151 rtx mem = force_const_mem (GET_MODE (*loc), *loc);
15152 validate_change (insn, loc, mem, false);
15155 if (MEM_P (*loc))
15157 mips16_rewrite_pool_constant (pool, &XEXP (*loc, 0));
15158 iter.skip_subrtxes ();
15160 else
15162 if (TARGET_MIPS16_TEXT_LOADS)
15163 mips16_rewrite_pool_constant (pool, loc);
15164 if (GET_CODE (*loc) == CONST
15165 /* Don't rewrite the __mips16_rdwr symbol. */
15166 || (GET_CODE (*loc) == UNSPEC
15167 && XINT (*loc, 1) == UNSPEC_TLS_GET_TP))
15168 iter.skip_subrtxes ();
15173 /* Return whether CFG is used in mips_reorg. */
15175 static bool
15176 mips_cfg_in_reorg (void)
15178 return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
15179 || TARGET_RELAX_PIC_CALLS);
15182 /* Build MIPS16 constant pools. Split the instructions if SPLIT_P,
15183 otherwise assume that they are already split. */
15185 static void
15186 mips16_lay_out_constants (bool split_p)
15188 struct mips16_constant_pool pool;
15189 rtx_insn *insn, *barrier;
15191 if (!TARGET_MIPS16_PCREL_LOADS)
15192 return;
15194 if (split_p)
15196 if (mips_cfg_in_reorg ())
15197 split_all_insns ();
15198 else
15199 split_all_insns_noflow ();
15201 barrier = 0;
15202 memset (&pool, 0, sizeof (pool));
15203 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15205 /* Rewrite constant pool references in INSN. */
15206 if (USEFUL_INSN_P (insn))
15207 mips16_rewrite_pool_refs (insn, &pool);
15209 pool.insn_address += mips16_insn_length (insn);
15211 if (pool.first != NULL)
15213 /* If there are no natural barriers between the first user of
15214 the pool and the highest acceptable address, we'll need to
15215 create a new instruction to jump around the constant pool.
15216 In the worst case, this instruction will be 4 bytes long.
15218 If it's too late to do this transformation after INSN,
15219 do it immediately before INSN. */
15220 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
15222 rtx_code_label *label;
15223 rtx_insn *jump;
15225 label = gen_label_rtx ();
15227 jump = emit_jump_insn_before (gen_jump (label), insn);
15228 JUMP_LABEL (jump) = label;
15229 LABEL_NUSES (label) = 1;
15230 barrier = emit_barrier_after (jump);
15232 emit_label_after (label, barrier);
15233 pool.insn_address += 4;
15236 /* See whether the constant pool is now out of range of the first
15237 user. If so, output the constants after the previous barrier.
15238 Note that any instructions between BARRIER and INSN (inclusive)
15239 will use negative offsets to refer to the pool. */
15240 if (pool.insn_address > pool.highest_address)
15242 mips16_emit_constants (pool.first, barrier);
15243 pool.first = NULL;
15244 barrier = 0;
15246 else if (BARRIER_P (insn))
15247 barrier = insn;
15250 mips16_emit_constants (pool.first, get_last_insn ());
15253 /* Return true if it is worth r10k_simplify_address's while replacing
15254 an address with X. We are looking for constants, and for addresses
15255 at a known offset from the incoming stack pointer. */
15257 static bool
15258 r10k_simplified_address_p (rtx x)
15260 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
15261 x = XEXP (x, 0);
15262 return x == virtual_incoming_args_rtx || CONSTANT_P (x);
15265 /* X is an expression that appears in INSN. Try to use the UD chains
15266 to simplify it, returning the simplified form on success and the
15267 original form otherwise. Replace the incoming value of $sp with
15268 virtual_incoming_args_rtx (which should never occur in X otherwise). */
15270 static rtx
15271 r10k_simplify_address (rtx x, rtx_insn *insn)
15273 rtx newx, op0, op1, set, note;
15274 rtx_insn *def_insn;
15275 df_ref use, def;
15276 struct df_link *defs;
15278 newx = NULL_RTX;
15279 if (UNARY_P (x))
15281 op0 = r10k_simplify_address (XEXP (x, 0), insn);
15282 if (op0 != XEXP (x, 0))
15283 newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
15284 op0, GET_MODE (XEXP (x, 0)));
15286 else if (BINARY_P (x))
15288 op0 = r10k_simplify_address (XEXP (x, 0), insn);
15289 op1 = r10k_simplify_address (XEXP (x, 1), insn);
15290 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
15291 newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
15293 else if (GET_CODE (x) == LO_SUM)
15295 /* LO_SUMs can be offset from HIGHs, if we know they won't
15296 overflow. See mips_classify_address for the rationale behind
15297 the lax check. */
15298 op0 = r10k_simplify_address (XEXP (x, 0), insn);
15299 if (GET_CODE (op0) == HIGH)
15300 newx = XEXP (x, 1);
15302 else if (REG_P (x))
15304 /* Uses are recorded by regno_reg_rtx, not X itself. */
15305 use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
15306 gcc_assert (use);
15307 defs = DF_REF_CHAIN (use);
15309 /* Require a single definition. */
15310 if (defs && defs->next == NULL)
15312 def = defs->ref;
15313 if (DF_REF_IS_ARTIFICIAL (def))
15315 /* Replace the incoming value of $sp with
15316 virtual_incoming_args_rtx. */
15317 if (x == stack_pointer_rtx
15318 && DF_REF_BB (def) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
15319 newx = virtual_incoming_args_rtx;
15321 else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
15322 DF_REF_BB (def)))
15324 /* Make sure that DEF_INSN is a single set of REG. */
15325 def_insn = DF_REF_INSN (def);
15326 if (NONJUMP_INSN_P (def_insn))
15328 set = single_set (def_insn);
15329 if (set && rtx_equal_p (SET_DEST (set), x))
15331 /* Prefer to use notes, since the def-use chains
15332 are often shorter. */
15333 note = find_reg_equal_equiv_note (def_insn);
15334 if (note)
15335 newx = XEXP (note, 0);
15336 else
15337 newx = SET_SRC (set);
15338 newx = r10k_simplify_address (newx, def_insn);
15344 if (newx && r10k_simplified_address_p (newx))
15345 return newx;
15346 return x;
15349 /* Return true if ADDRESS is known to be an uncached address
15350 on R10K systems. */
15352 static bool
15353 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
15355 unsigned HOST_WIDE_INT upper;
15357 /* Check for KSEG1. */
15358 if (address + 0x60000000 < 0x20000000)
15359 return true;
15361 /* Check for uncached XKPHYS addresses. */
15362 if (Pmode == DImode)
15364 upper = (address >> 40) & 0xf9ffff;
15365 if (upper == 0x900000 || upper == 0xb80000)
15366 return true;
15368 return false;
15371 /* Return true if we can prove that an access to address X in instruction
15372 INSN would be safe from R10K speculation. This X is a general
15373 expression; it might not be a legitimate address. */
15375 static bool
15376 r10k_safe_address_p (rtx x, rtx_insn *insn)
15378 rtx base, offset;
15379 HOST_WIDE_INT offset_val;
15381 x = r10k_simplify_address (x, insn);
15383 /* Check for references to the stack frame. It doesn't really matter
15384 how much of the frame has been allocated at INSN; -mr10k-cache-barrier
15385 allows us to assume that accesses to any part of the eventual frame
15386 is safe from speculation at any point in the function. */
15387 mips_split_plus (x, &base, &offset_val);
15388 if (base == virtual_incoming_args_rtx
15389 && offset_val >= -cfun->machine->frame.total_size
15390 && offset_val < cfun->machine->frame.args_size)
15391 return true;
15393 /* Check for uncached addresses. */
15394 if (CONST_INT_P (x))
15395 return r10k_uncached_address_p (INTVAL (x));
15397 /* Check for accesses to a static object. */
15398 split_const (x, &base, &offset);
15399 return offset_within_block_p (base, INTVAL (offset));
15402 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
15403 an in-range access to an automatic variable, or to an object with
15404 a link-time-constant address. */
15406 static bool
15407 r10k_safe_mem_expr_p (tree expr, unsigned HOST_WIDE_INT offset)
15409 HOST_WIDE_INT bitoffset, bitsize;
15410 tree inner, var_offset;
15411 machine_mode mode;
15412 int unsigned_p, volatile_p;
15414 inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
15415 &unsigned_p, &volatile_p, false);
15416 if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
15417 return false;
15419 offset += bitoffset / BITS_PER_UNIT;
15420 return offset < tree_to_uhwi (DECL_SIZE_UNIT (inner));
15423 /* Return true if X contains a MEM that is not safe from R10K speculation.
15424 INSN is the instruction that contains X. */
15426 static bool
15427 r10k_needs_protection_p_1 (rtx x, rtx_insn *insn)
15429 subrtx_var_iterator::array_type array;
15430 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
15432 rtx mem = *iter;
15433 if (MEM_P (mem))
15435 if ((MEM_EXPR (mem)
15436 && MEM_OFFSET_KNOWN_P (mem)
15437 && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
15438 || r10k_safe_address_p (XEXP (mem, 0), insn))
15439 iter.skip_subrtxes ();
15440 else
15441 return true;
15444 return false;
15447 /* A note_stores callback for which DATA points to an instruction pointer.
15448 If *DATA is nonnull, make it null if it X contains a MEM that is not
15449 safe from R10K speculation. */
15451 static void
15452 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
15453 void *data)
15455 rtx_insn **insn_ptr;
15457 insn_ptr = (rtx_insn **) data;
15458 if (*insn_ptr && r10k_needs_protection_p_1 (x, *insn_ptr))
15459 *insn_ptr = NULL;
15462 /* X is the pattern of a call instruction. Return true if the call is
15463 not to a declared function. */
15465 static bool
15466 r10k_needs_protection_p_call (const_rtx x)
15468 subrtx_iterator::array_type array;
15469 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
15471 const_rtx mem = *iter;
15472 if (MEM_P (mem))
15474 const_rtx addr = XEXP (mem, 0);
15475 if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DECL (addr))
15476 iter.skip_subrtxes ();
15477 else
15478 return true;
15481 return false;
15484 /* Return true if instruction INSN needs to be protected by an R10K
15485 cache barrier. */
15487 static bool
15488 r10k_needs_protection_p (rtx_insn *insn)
15490 if (CALL_P (insn))
15491 return r10k_needs_protection_p_call (PATTERN (insn));
15493 if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
15495 note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
15496 return insn == NULL_RTX;
15499 return r10k_needs_protection_p_1 (PATTERN (insn), insn);
15502 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
15503 edge is unconditional. */
15505 static bool
15506 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
15508 edge_iterator ei;
15509 edge e;
15511 FOR_EACH_EDGE (e, ei, bb->preds)
15512 if (!single_succ_p (e->src)
15513 || !bitmap_bit_p (protected_bbs, e->src->index)
15514 || (e->flags & EDGE_COMPLEX) != 0)
15515 return false;
15516 return true;
15519 /* Implement -mr10k-cache-barrier= for the current function. */
15521 static void
15522 r10k_insert_cache_barriers (void)
15524 int *rev_post_order;
15525 unsigned int i, n;
15526 basic_block bb;
15527 sbitmap protected_bbs;
15528 rtx_insn *insn, *end;
15529 rtx unprotected_region;
15531 if (TARGET_MIPS16)
15533 sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
15534 return;
15537 /* Calculate dominators. */
15538 calculate_dominance_info (CDI_DOMINATORS);
15540 /* Bit X of PROTECTED_BBS is set if the last operation in basic block
15541 X is protected by a cache barrier. */
15542 protected_bbs = sbitmap_alloc (last_basic_block_for_fn (cfun));
15543 bitmap_clear (protected_bbs);
15545 /* Iterate over the basic blocks in reverse post-order. */
15546 rev_post_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
15547 n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
15548 for (i = 0; i < n; i++)
15550 bb = BASIC_BLOCK_FOR_FN (cfun, rev_post_order[i]);
15552 /* If this block is only reached by unconditional edges, and if the
15553 source of every edge is protected, the beginning of the block is
15554 also protected. */
15555 if (r10k_protected_bb_p (bb, protected_bbs))
15556 unprotected_region = NULL_RTX;
15557 else
15558 unprotected_region = pc_rtx;
15559 end = NEXT_INSN (BB_END (bb));
15561 /* UNPROTECTED_REGION is:
15563 - null if we are processing a protected region,
15564 - pc_rtx if we are processing an unprotected region but have
15565 not yet found the first instruction in it
15566 - the first instruction in an unprotected region otherwise. */
15567 for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
15569 if (unprotected_region && USEFUL_INSN_P (insn))
15571 if (recog_memoized (insn) == CODE_FOR_mips_cache)
15572 /* This CACHE instruction protects the following code. */
15573 unprotected_region = NULL_RTX;
15574 else
15576 /* See if INSN is the first instruction in this
15577 unprotected region. */
15578 if (unprotected_region == pc_rtx)
15579 unprotected_region = insn;
15581 /* See if INSN needs to be protected. If so,
15582 we must insert a cache barrier somewhere between
15583 PREV_INSN (UNPROTECTED_REGION) and INSN. It isn't
15584 clear which position is better performance-wise,
15585 but as a tie-breaker, we assume that it is better
15586 to allow delay slots to be back-filled where
15587 possible, and that it is better not to insert
15588 barriers in the middle of already-scheduled code.
15589 We therefore insert the barrier at the beginning
15590 of the region. */
15591 if (r10k_needs_protection_p (insn))
15593 emit_insn_before (gen_r10k_cache_barrier (),
15594 unprotected_region);
15595 unprotected_region = NULL_RTX;
15600 if (CALL_P (insn))
15601 /* The called function is not required to protect the exit path.
15602 The code that follows a call is therefore unprotected. */
15603 unprotected_region = pc_rtx;
15606 /* Record whether the end of this block is protected. */
15607 if (unprotected_region == NULL_RTX)
15608 bitmap_set_bit (protected_bbs, bb->index);
15610 XDELETEVEC (rev_post_order);
15612 sbitmap_free (protected_bbs);
15614 free_dominance_info (CDI_DOMINATORS);
15617 /* If INSN is a call, return the underlying CALL expr. Return NULL_RTX
15618 otherwise. If INSN has two call rtx, then store the second one in
15619 SECOND_CALL. */
15621 static rtx
15622 mips_call_expr_from_insn (rtx_insn *insn, rtx *second_call)
15624 rtx x;
15625 rtx x2;
15627 if (!CALL_P (insn))
15628 return NULL_RTX;
15630 x = PATTERN (insn);
15631 if (GET_CODE (x) == PARALLEL)
15633 /* Calls returning complex values have two CALL rtx. Look for the second
15634 one here, and return it via the SECOND_CALL arg. */
15635 x2 = XVECEXP (x, 0, 1);
15636 if (GET_CODE (x2) == SET)
15637 x2 = XEXP (x2, 1);
15638 if (GET_CODE (x2) == CALL)
15639 *second_call = x2;
15641 x = XVECEXP (x, 0, 0);
15643 if (GET_CODE (x) == SET)
15644 x = XEXP (x, 1);
15645 gcc_assert (GET_CODE (x) == CALL);
15647 return x;
15650 /* REG is set in DEF. See if the definition is one of the ways we load a
15651 register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
15652 If it is, return the symbol reference of the function, otherwise return
15653 NULL_RTX.
15655 If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
15656 the values of source registers, otherwise treat such registers as
15657 having an unknown value. */
15659 static rtx
15660 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
15662 rtx_insn *def_insn;
15663 rtx set;
15665 if (DF_REF_IS_ARTIFICIAL (def))
15666 return NULL_RTX;
15668 def_insn = DF_REF_INSN (def);
15669 set = single_set (def_insn);
15670 if (set && rtx_equal_p (SET_DEST (set), reg))
15672 rtx note, src, symbol;
15674 /* First see whether the source is a plain symbol. This is used
15675 when calling symbols that are not lazily bound. */
15676 src = SET_SRC (set);
15677 if (GET_CODE (src) == SYMBOL_REF)
15678 return src;
15680 /* Handle %call16 references. */
15681 symbol = mips_strip_unspec_call (src);
15682 if (symbol)
15684 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15685 return symbol;
15688 /* If we have something more complicated, look for a
15689 REG_EQUAL or REG_EQUIV note. */
15690 note = find_reg_equal_equiv_note (def_insn);
15691 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
15692 return XEXP (note, 0);
15694 /* Follow at most one simple register copy. Such copies are
15695 interesting in cases like:
15697 for (...)
15699 locally_binding_fn (...);
15702 and:
15704 locally_binding_fn (...);
15706 locally_binding_fn (...);
15708 where the load of locally_binding_fn can legitimately be
15709 hoisted or shared. However, we do not expect to see complex
15710 chains of copies, so a full worklist solution to the problem
15711 would probably be overkill. */
15712 if (recurse_p && REG_P (src))
15713 return mips_find_pic_call_symbol (def_insn, src, false);
15716 return NULL_RTX;
15719 /* Find the definition of the use of REG in INSN. See if the definition
15720 is one of the ways we load a register with a symbol address for a
15721 mips_use_pic_fn_addr_reg_p call. If it is return the symbol reference
15722 of the function, otherwise return NULL_RTX. RECURSE_P is as for
15723 mips_pic_call_symbol_from_set. */
15725 static rtx
15726 mips_find_pic_call_symbol (rtx_insn *insn, rtx reg, bool recurse_p)
15728 df_ref use;
15729 struct df_link *defs;
15730 rtx symbol;
15732 use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
15733 if (!use)
15734 return NULL_RTX;
15735 defs = DF_REF_CHAIN (use);
15736 if (!defs)
15737 return NULL_RTX;
15738 symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15739 if (!symbol)
15740 return NULL_RTX;
15742 /* If we have more than one definition, they need to be identical. */
15743 for (defs = defs->next; defs; defs = defs->next)
15745 rtx other;
15747 other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15748 if (!rtx_equal_p (symbol, other))
15749 return NULL_RTX;
15752 return symbol;
15755 /* Replace the args_size operand of the call expression CALL with the
15756 call-attribute UNSPEC and fill in SYMBOL as the function symbol. */
15758 static void
15759 mips_annotate_pic_call_expr (rtx call, rtx symbol)
15761 rtx args_size;
15763 args_size = XEXP (call, 1);
15764 XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
15765 gen_rtvec (2, args_size, symbol),
15766 UNSPEC_CALL_ATTR);
15769 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression. See
15770 if instead of the arg_size argument it contains the call attributes. If
15771 yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
15772 symbol from the call attributes. Also return false if ARGS_SIZE_OPNO is
15773 -1. */
15775 bool
15776 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
15778 rtx args_size, symbol;
15780 if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
15781 return false;
15783 args_size = operands[args_size_opno];
15784 if (GET_CODE (args_size) != UNSPEC)
15785 return false;
15786 gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
15788 symbol = XVECEXP (args_size, 0, 1);
15789 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15791 operands[args_size_opno] = symbol;
15792 return true;
15795 /* Use DF to annotate PIC indirect calls with the function symbol they
15796 dispatch to. */
15798 static void
15799 mips_annotate_pic_calls (void)
15801 basic_block bb;
15802 rtx_insn *insn;
15804 FOR_EACH_BB_FN (bb, cfun)
15805 FOR_BB_INSNS (bb, insn)
15807 rtx call, reg, symbol, second_call;
15809 second_call = 0;
15810 call = mips_call_expr_from_insn (insn, &second_call);
15811 if (!call)
15812 continue;
15813 gcc_assert (MEM_P (XEXP (call, 0)));
15814 reg = XEXP (XEXP (call, 0), 0);
15815 if (!REG_P (reg))
15816 continue;
15818 symbol = mips_find_pic_call_symbol (insn, reg, true);
15819 if (symbol)
15821 mips_annotate_pic_call_expr (call, symbol);
15822 if (second_call)
15823 mips_annotate_pic_call_expr (second_call, symbol);
15828 /* A temporary variable used by note_uses callbacks, etc. */
15829 static rtx_insn *mips_sim_insn;
15831 /* A structure representing the state of the processor pipeline.
15832 Used by the mips_sim_* family of functions. */
15833 struct mips_sim {
15834 /* The maximum number of instructions that can be issued in a cycle.
15835 (Caches mips_issue_rate.) */
15836 unsigned int issue_rate;
15838 /* The current simulation time. */
15839 unsigned int time;
15841 /* How many more instructions can be issued in the current cycle. */
15842 unsigned int insns_left;
15844 /* LAST_SET[X].INSN is the last instruction to set register X.
15845 LAST_SET[X].TIME is the time at which that instruction was issued.
15846 INSN is null if no instruction has yet set register X. */
15847 struct {
15848 rtx_insn *insn;
15849 unsigned int time;
15850 } last_set[FIRST_PSEUDO_REGISTER];
15852 /* The pipeline's current DFA state. */
15853 state_t dfa_state;
15856 /* Reset STATE to the initial simulation state. */
15858 static void
15859 mips_sim_reset (struct mips_sim *state)
15861 curr_state = state->dfa_state;
15863 state->time = 0;
15864 state->insns_left = state->issue_rate;
15865 memset (&state->last_set, 0, sizeof (state->last_set));
15866 state_reset (curr_state);
15868 targetm.sched.init (0, false, 0);
15869 advance_state (curr_state);
15872 /* Initialize STATE before its first use. DFA_STATE points to an
15873 allocated but uninitialized DFA state. */
15875 static void
15876 mips_sim_init (struct mips_sim *state, state_t dfa_state)
15878 if (targetm.sched.init_dfa_pre_cycle_insn)
15879 targetm.sched.init_dfa_pre_cycle_insn ();
15881 if (targetm.sched.init_dfa_post_cycle_insn)
15882 targetm.sched.init_dfa_post_cycle_insn ();
15884 state->issue_rate = mips_issue_rate ();
15885 state->dfa_state = dfa_state;
15886 mips_sim_reset (state);
15889 /* Advance STATE by one clock cycle. */
15891 static void
15892 mips_sim_next_cycle (struct mips_sim *state)
15894 curr_state = state->dfa_state;
15896 state->time++;
15897 state->insns_left = state->issue_rate;
15898 advance_state (curr_state);
15901 /* Advance simulation state STATE until instruction INSN can read
15902 register REG. */
15904 static void
15905 mips_sim_wait_reg (struct mips_sim *state, rtx_insn *insn, rtx reg)
15907 unsigned int regno, end_regno;
15909 end_regno = END_REGNO (reg);
15910 for (regno = REGNO (reg); regno < end_regno; regno++)
15911 if (state->last_set[regno].insn != 0)
15913 unsigned int t;
15915 t = (state->last_set[regno].time
15916 + insn_latency (state->last_set[regno].insn, insn));
15917 while (state->time < t)
15918 mips_sim_next_cycle (state);
15922 /* A note_uses callback. For each register in *X, advance simulation
15923 state DATA until mips_sim_insn can read the register's value. */
15925 static void
15926 mips_sim_wait_regs_1 (rtx *x, void *data)
15928 subrtx_var_iterator::array_type array;
15929 FOR_EACH_SUBRTX_VAR (iter, array, *x, NONCONST)
15930 if (REG_P (*iter))
15931 mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *iter);
15934 /* Advance simulation state STATE until all of INSN's register
15935 dependencies are satisfied. */
15937 static void
15938 mips_sim_wait_regs (struct mips_sim *state, rtx_insn *insn)
15940 mips_sim_insn = insn;
15941 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
15944 /* Advance simulation state STATE until the units required by
15945 instruction INSN are available. */
15947 static void
15948 mips_sim_wait_units (struct mips_sim *state, rtx_insn *insn)
15950 state_t tmp_state;
15952 tmp_state = alloca (state_size ());
15953 while (state->insns_left == 0
15954 || (memcpy (tmp_state, state->dfa_state, state_size ()),
15955 state_transition (tmp_state, insn) >= 0))
15956 mips_sim_next_cycle (state);
15959 /* Advance simulation state STATE until INSN is ready to issue. */
15961 static void
15962 mips_sim_wait_insn (struct mips_sim *state, rtx_insn *insn)
15964 mips_sim_wait_regs (state, insn);
15965 mips_sim_wait_units (state, insn);
15968 /* mips_sim_insn has just set X. Update the LAST_SET array
15969 in simulation state DATA. */
15971 static void
15972 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
15974 struct mips_sim *state;
15976 state = (struct mips_sim *) data;
15977 if (REG_P (x))
15979 unsigned int regno, end_regno;
15981 end_regno = END_REGNO (x);
15982 for (regno = REGNO (x); regno < end_regno; regno++)
15984 state->last_set[regno].insn = mips_sim_insn;
15985 state->last_set[regno].time = state->time;
15990 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
15991 can issue immediately (i.e., that mips_sim_wait_insn has already
15992 been called). */
15994 static void
15995 mips_sim_issue_insn (struct mips_sim *state, rtx_insn *insn)
15997 curr_state = state->dfa_state;
15999 state_transition (curr_state, insn);
16000 state->insns_left = targetm.sched.variable_issue (0, false, insn,
16001 state->insns_left);
16003 mips_sim_insn = insn;
16004 note_stores (PATTERN (insn), mips_sim_record_set, state);
16007 /* Simulate issuing a NOP in state STATE. */
16009 static void
16010 mips_sim_issue_nop (struct mips_sim *state)
16012 if (state->insns_left == 0)
16013 mips_sim_next_cycle (state);
16014 state->insns_left--;
16017 /* Update simulation state STATE so that it's ready to accept the instruction
16018 after INSN. INSN should be part of the main rtl chain, not a member of a
16019 SEQUENCE. */
16021 static void
16022 mips_sim_finish_insn (struct mips_sim *state, rtx_insn *insn)
16024 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
16025 if (JUMP_P (insn))
16026 mips_sim_issue_nop (state);
16028 switch (GET_CODE (SEQ_BEGIN (insn)))
16030 case CODE_LABEL:
16031 case CALL_INSN:
16032 /* We can't predict the processor state after a call or label. */
16033 mips_sim_reset (state);
16034 break;
16036 case JUMP_INSN:
16037 /* The delay slots of branch likely instructions are only executed
16038 when the branch is taken. Therefore, if the caller has simulated
16039 the delay slot instruction, STATE does not really reflect the state
16040 of the pipeline for the instruction after the delay slot. Also,
16041 branch likely instructions tend to incur a penalty when not taken,
16042 so there will probably be an extra delay between the branch and
16043 the instruction after the delay slot. */
16044 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
16045 mips_sim_reset (state);
16046 break;
16048 default:
16049 break;
16053 /* Use simulator state STATE to calculate the execution time of
16054 instruction sequence SEQ. */
16056 static unsigned int
16057 mips_seq_time (struct mips_sim *state, rtx_insn *seq)
16059 mips_sim_reset (state);
16060 for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
16062 mips_sim_wait_insn (state, insn);
16063 mips_sim_issue_insn (state, insn);
16065 return state->time;
16068 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
16069 setting SETTING, using STATE to simulate instruction sequences. */
16071 static unsigned int
16072 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
16074 mips_tuning_info.fast_mult_zero_zero_p = setting;
16075 start_sequence ();
16077 machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
16078 rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
16079 mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
16081 /* If the target provides mulsidi3_32bit then that's the most likely
16082 consumer of the result. Test for bypasses. */
16083 if (dword_mode == DImode && HAVE_maddsidi4)
16085 rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
16086 emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
16089 unsigned int time = mips_seq_time (state, get_insns ());
16090 end_sequence ();
16091 return time;
16094 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
16095 and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
16096 Prefer MULT -- which is shorter -- in the event of a tie. */
16098 static void
16099 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
16101 if (TARGET_MIPS16 || !ISA_HAS_HILO)
16102 /* No MTLO or MTHI available for MIPS16. Also, when there are no HI or LO
16103 registers then there is no reason to zero them, arbitrarily choose to
16104 say that "MULT $0,$0" would be faster. */
16105 mips_tuning_info.fast_mult_zero_zero_p = true;
16106 else
16108 unsigned int true_time = mips_mult_zero_zero_cost (state, true);
16109 unsigned int false_time = mips_mult_zero_zero_cost (state, false);
16110 mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
16114 /* Set up costs based on the current architecture and tuning settings. */
16116 static void
16117 mips_set_tuning_info (void)
16119 if (mips_tuning_info.initialized_p
16120 && mips_tuning_info.arch == mips_arch
16121 && mips_tuning_info.tune == mips_tune
16122 && mips_tuning_info.mips16_p == TARGET_MIPS16)
16123 return;
16125 mips_tuning_info.arch = mips_arch;
16126 mips_tuning_info.tune = mips_tune;
16127 mips_tuning_info.mips16_p = TARGET_MIPS16;
16128 mips_tuning_info.initialized_p = true;
16130 dfa_start ();
16132 struct mips_sim state;
16133 mips_sim_init (&state, alloca (state_size ()));
16135 mips_set_fast_mult_zero_zero_p (&state);
16137 dfa_finish ();
16140 /* Implement TARGET_EXPAND_TO_RTL_HOOK. */
16142 static void
16143 mips_expand_to_rtl_hook (void)
16145 /* We need to call this at a point where we can safely create sequences
16146 of instructions, so TARGET_OVERRIDE_OPTIONS is too early. We also
16147 need to call it at a point where the DFA infrastructure is not
16148 already in use, so we can't just call it lazily on demand.
16150 At present, mips_tuning_info is only needed during post-expand
16151 RTL passes such as split_insns, so this hook should be early enough.
16152 We may need to move the call elsewhere if mips_tuning_info starts
16153 to be used for other things (such as rtx_costs, or expanders that
16154 could be called during gimple optimization). */
16155 mips_set_tuning_info ();
16158 /* The VR4130 pipeline issues aligned pairs of instructions together,
16159 but it stalls the second instruction if it depends on the first.
16160 In order to cut down the amount of logic required, this dependence
16161 check is not based on a full instruction decode. Instead, any non-SPECIAL
16162 instruction is assumed to modify the register specified by bits 20-16
16163 (which is usually the "rt" field).
16165 In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
16166 input, so we can end up with a false dependence between the branch
16167 and its delay slot. If this situation occurs in instruction INSN,
16168 try to avoid it by swapping rs and rt. */
16170 static void
16171 vr4130_avoid_branch_rt_conflict (rtx_insn *insn)
16173 rtx_insn *first, *second;
16175 first = SEQ_BEGIN (insn);
16176 second = SEQ_END (insn);
16177 if (JUMP_P (first)
16178 && NONJUMP_INSN_P (second)
16179 && GET_CODE (PATTERN (first)) == SET
16180 && GET_CODE (SET_DEST (PATTERN (first))) == PC
16181 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
16183 /* Check for the right kind of condition. */
16184 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
16185 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
16186 && REG_P (XEXP (cond, 0))
16187 && REG_P (XEXP (cond, 1))
16188 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
16189 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
16191 /* SECOND mentions the rt register but not the rs register. */
16192 rtx tmp = XEXP (cond, 0);
16193 XEXP (cond, 0) = XEXP (cond, 1);
16194 XEXP (cond, 1) = tmp;
16199 /* Implement -mvr4130-align. Go through each basic block and simulate the
16200 processor pipeline. If we find that a pair of instructions could execute
16201 in parallel, and the first of those instructions is not 8-byte aligned,
16202 insert a nop to make it aligned. */
16204 static void
16205 vr4130_align_insns (void)
16207 struct mips_sim state;
16208 rtx_insn *insn, *subinsn, *last, *last2, *next;
16209 bool aligned_p;
16211 dfa_start ();
16213 /* LAST is the last instruction before INSN to have a nonzero length.
16214 LAST2 is the last such instruction before LAST. */
16215 last = 0;
16216 last2 = 0;
16218 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
16219 aligned_p = true;
16221 mips_sim_init (&state, alloca (state_size ()));
16222 for (insn = get_insns (); insn != 0; insn = next)
16224 unsigned int length;
16226 next = NEXT_INSN (insn);
16228 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
16229 This isn't really related to the alignment pass, but we do it on
16230 the fly to avoid a separate instruction walk. */
16231 vr4130_avoid_branch_rt_conflict (insn);
16233 length = get_attr_length (insn);
16234 if (length > 0 && USEFUL_INSN_P (insn))
16235 FOR_EACH_SUBINSN (subinsn, insn)
16237 mips_sim_wait_insn (&state, subinsn);
16239 /* If we want this instruction to issue in parallel with the
16240 previous one, make sure that the previous instruction is
16241 aligned. There are several reasons why this isn't worthwhile
16242 when the second instruction is a call:
16244 - Calls are less likely to be performance critical,
16245 - There's a good chance that the delay slot can execute
16246 in parallel with the call.
16247 - The return address would then be unaligned.
16249 In general, if we're going to insert a nop between instructions
16250 X and Y, it's better to insert it immediately after X. That
16251 way, if the nop makes Y aligned, it will also align any labels
16252 between X and Y. */
16253 if (state.insns_left != state.issue_rate
16254 && !CALL_P (subinsn))
16256 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
16258 /* SUBINSN is the first instruction in INSN and INSN is
16259 aligned. We want to align the previous instruction
16260 instead, so insert a nop between LAST2 and LAST.
16262 Note that LAST could be either a single instruction
16263 or a branch with a delay slot. In the latter case,
16264 LAST, like INSN, is already aligned, but the delay
16265 slot must have some extra delay that stops it from
16266 issuing at the same time as the branch. We therefore
16267 insert a nop before the branch in order to align its
16268 delay slot. */
16269 gcc_assert (last2);
16270 emit_insn_after (gen_nop (), last2);
16271 aligned_p = false;
16273 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
16275 /* SUBINSN is the delay slot of INSN, but INSN is
16276 currently unaligned. Insert a nop between
16277 LAST and INSN to align it. */
16278 gcc_assert (last);
16279 emit_insn_after (gen_nop (), last);
16280 aligned_p = true;
16283 mips_sim_issue_insn (&state, subinsn);
16285 mips_sim_finish_insn (&state, insn);
16287 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
16288 length = get_attr_length (insn);
16289 if (length > 0)
16291 /* If the instruction is an asm statement or multi-instruction
16292 mips.md patern, the length is only an estimate. Insert an
16293 8 byte alignment after it so that the following instructions
16294 can be handled correctly. */
16295 if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
16296 && (recog_memoized (insn) < 0 || length >= 8))
16298 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
16299 next = NEXT_INSN (next);
16300 mips_sim_next_cycle (&state);
16301 aligned_p = true;
16303 else if (length & 4)
16304 aligned_p = !aligned_p;
16305 last2 = last;
16306 last = insn;
16309 /* See whether INSN is an aligned label. */
16310 if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
16311 aligned_p = true;
16313 dfa_finish ();
16316 /* This structure records that the current function has a LO_SUM
16317 involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
16318 the largest offset applied to BASE by all such LO_SUMs. */
16319 struct mips_lo_sum_offset {
16320 rtx base;
16321 HOST_WIDE_INT offset;
16324 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE. */
16326 static hashval_t
16327 mips_hash_base (rtx base)
16329 int do_not_record_p;
16331 return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
16334 /* Hashtable helpers. */
16336 struct mips_lo_sum_offset_hasher : typed_free_remove <mips_lo_sum_offset>
16338 typedef mips_lo_sum_offset value_type;
16339 typedef rtx_def compare_type;
16340 static inline hashval_t hash (const value_type *);
16341 static inline bool equal (const value_type *, const compare_type *);
16344 /* Hash-table callbacks for mips_lo_sum_offsets. */
16346 inline hashval_t
16347 mips_lo_sum_offset_hasher::hash (const value_type *entry)
16349 return mips_hash_base (entry->base);
16352 inline bool
16353 mips_lo_sum_offset_hasher::equal (const value_type *entry,
16354 const compare_type *value)
16356 return rtx_equal_p (entry->base, value);
16359 typedef hash_table<mips_lo_sum_offset_hasher> mips_offset_table;
16361 /* Look up symbolic constant X in HTAB, which is a hash table of
16362 mips_lo_sum_offsets. If OPTION is NO_INSERT, return true if X can be
16363 paired with a recorded LO_SUM, otherwise record X in the table. */
16365 static bool
16366 mips_lo_sum_offset_lookup (mips_offset_table *htab, rtx x,
16367 enum insert_option option)
16369 rtx base, offset;
16370 mips_lo_sum_offset **slot;
16371 struct mips_lo_sum_offset *entry;
16373 /* Split X into a base and offset. */
16374 split_const (x, &base, &offset);
16375 if (UNSPEC_ADDRESS_P (base))
16376 base = UNSPEC_ADDRESS (base);
16378 /* Look up the base in the hash table. */
16379 slot = htab->find_slot_with_hash (base, mips_hash_base (base), option);
16380 if (slot == NULL)
16381 return false;
16383 entry = (struct mips_lo_sum_offset *) *slot;
16384 if (option == INSERT)
16386 if (entry == NULL)
16388 entry = XNEW (struct mips_lo_sum_offset);
16389 entry->base = base;
16390 entry->offset = INTVAL (offset);
16391 *slot = entry;
16393 else
16395 if (INTVAL (offset) > entry->offset)
16396 entry->offset = INTVAL (offset);
16399 return INTVAL (offset) <= entry->offset;
16402 /* Search X for LO_SUMs and record them in HTAB. */
16404 static void
16405 mips_record_lo_sums (const_rtx x, mips_offset_table *htab)
16407 subrtx_iterator::array_type array;
16408 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
16409 if (GET_CODE (*iter) == LO_SUM)
16410 mips_lo_sum_offset_lookup (htab, XEXP (*iter, 1), INSERT);
16413 /* Return true if INSN is a SET of an orphaned high-part relocation.
16414 HTAB is a hash table of mips_lo_sum_offsets that describes all the
16415 LO_SUMs in the current function. */
16417 static bool
16418 mips_orphaned_high_part_p (mips_offset_table *htab, rtx_insn *insn)
16420 enum mips_symbol_type type;
16421 rtx x, set;
16423 set = single_set (insn);
16424 if (set)
16426 /* Check for %his. */
16427 x = SET_SRC (set);
16428 if (GET_CODE (x) == HIGH
16429 && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
16430 return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
16432 /* Check for local %gots (and %got_pages, which is redundant but OK). */
16433 if (GET_CODE (x) == UNSPEC
16434 && XINT (x, 1) == UNSPEC_LOAD_GOT
16435 && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
16436 SYMBOL_CONTEXT_LEA, &type)
16437 && type == SYMBOL_GOTOFF_PAGE)
16438 return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
16440 return false;
16443 /* Subroutine of mips_reorg_process_insns. If there is a hazard between
16444 INSN and a previous instruction, avoid it by inserting nops after
16445 instruction AFTER.
16447 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
16448 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
16449 before using the value of that register. *HILO_DELAY counts the
16450 number of instructions since the last hilo hazard (that is,
16451 the number of instructions since the last MFLO or MFHI).
16453 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
16454 for the next instruction.
16456 LO_REG is an rtx for the LO register, used in dependence checking. */
16458 static void
16459 mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
16460 rtx *delayed_reg, rtx lo_reg)
16462 rtx pattern, set;
16463 int nops, ninsns;
16465 pattern = PATTERN (insn);
16467 /* Do not put the whole function in .set noreorder if it contains
16468 an asm statement. We don't know whether there will be hazards
16469 between the asm statement and the gcc-generated code. */
16470 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
16471 cfun->machine->all_noreorder_p = false;
16473 /* Ignore zero-length instructions (barriers and the like). */
16474 ninsns = get_attr_length (insn) / 4;
16475 if (ninsns == 0)
16476 return;
16478 /* Work out how many nops are needed. Note that we only care about
16479 registers that are explicitly mentioned in the instruction's pattern.
16480 It doesn't matter that calls use the argument registers or that they
16481 clobber hi and lo. */
16482 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
16483 nops = 2 - *hilo_delay;
16484 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
16485 nops = 1;
16486 else
16487 nops = 0;
16489 /* Insert the nops between this instruction and the previous one.
16490 Each new nop takes us further from the last hilo hazard. */
16491 *hilo_delay += nops;
16492 while (nops-- > 0)
16493 emit_insn_after (gen_hazard_nop (), after);
16495 /* Set up the state for the next instruction. */
16496 *hilo_delay += ninsns;
16497 *delayed_reg = 0;
16498 if (INSN_CODE (insn) >= 0)
16499 switch (get_attr_hazard (insn))
16501 case HAZARD_NONE:
16502 break;
16504 case HAZARD_HILO:
16505 *hilo_delay = 0;
16506 break;
16508 case HAZARD_DELAY:
16509 set = single_set (insn);
16510 gcc_assert (set);
16511 *delayed_reg = SET_DEST (set);
16512 break;
16516 /* Go through the instruction stream and insert nops where necessary.
16517 Also delete any high-part relocations whose partnering low parts
16518 are now all dead. See if the whole function can then be put into
16519 .set noreorder and .set nomacro. */
16521 static void
16522 mips_reorg_process_insns (void)
16524 rtx_insn *insn, *last_insn, *subinsn, *next_insn;
16525 rtx lo_reg, delayed_reg;
16526 int hilo_delay;
16528 /* Force all instructions to be split into their final form. */
16529 split_all_insns_noflow ();
16531 /* Recalculate instruction lengths without taking nops into account. */
16532 cfun->machine->ignore_hazard_length_p = true;
16533 shorten_branches (get_insns ());
16535 cfun->machine->all_noreorder_p = true;
16537 /* We don't track MIPS16 PC-relative offsets closely enough to make
16538 a good job of "set .noreorder" code in MIPS16 mode. */
16539 if (TARGET_MIPS16)
16540 cfun->machine->all_noreorder_p = false;
16542 /* Code that doesn't use explicit relocs can't be ".set nomacro". */
16543 if (!TARGET_EXPLICIT_RELOCS)
16544 cfun->machine->all_noreorder_p = false;
16546 /* Profiled functions can't be all noreorder because the profiler
16547 support uses assembler macros. */
16548 if (crtl->profile)
16549 cfun->machine->all_noreorder_p = false;
16551 /* Code compiled with -mfix-vr4120, -mfix-rm7000 or -mfix-24k can't be
16552 all noreorder because we rely on the assembler to work around some
16553 errata. The R5900 too has several bugs. */
16554 if (TARGET_FIX_VR4120
16555 || TARGET_FIX_RM7000
16556 || TARGET_FIX_24K
16557 || TARGET_MIPS5900)
16558 cfun->machine->all_noreorder_p = false;
16560 /* The same is true for -mfix-vr4130 if we might generate MFLO or
16561 MFHI instructions. Note that we avoid using MFLO and MFHI if
16562 the VR4130 MACC and DMACC instructions are available instead;
16563 see the *mfhilo_{si,di}_macc patterns. */
16564 if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
16565 cfun->machine->all_noreorder_p = false;
16567 mips_offset_table htab (37);
16569 /* Make a first pass over the instructions, recording all the LO_SUMs. */
16570 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
16571 FOR_EACH_SUBINSN (subinsn, insn)
16572 if (USEFUL_INSN_P (subinsn))
16574 rtx body = PATTERN (insn);
16575 int noperands = asm_noperands (body);
16576 if (noperands >= 0)
16578 rtx *ops = XALLOCAVEC (rtx, noperands);
16579 bool *used = XALLOCAVEC (bool, noperands);
16580 const char *string = decode_asm_operands (body, ops, NULL, NULL,
16581 NULL, NULL);
16582 get_referenced_operands (string, used, noperands);
16583 for (int i = 0; i < noperands; ++i)
16584 if (used[i])
16585 mips_record_lo_sums (ops[i], &htab);
16587 else
16588 mips_record_lo_sums (PATTERN (subinsn), &htab);
16591 last_insn = 0;
16592 hilo_delay = 2;
16593 delayed_reg = 0;
16594 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
16596 /* Make a second pass over the instructions. Delete orphaned
16597 high-part relocations or turn them into NOPs. Avoid hazards
16598 by inserting NOPs. */
16599 for (insn = get_insns (); insn != 0; insn = next_insn)
16601 next_insn = NEXT_INSN (insn);
16602 if (USEFUL_INSN_P (insn))
16604 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
16606 /* If we find an orphaned high-part relocation in a delay
16607 slot, it's easier to turn that instruction into a NOP than
16608 to delete it. The delay slot will be a NOP either way. */
16609 FOR_EACH_SUBINSN (subinsn, insn)
16610 if (INSN_P (subinsn))
16612 if (mips_orphaned_high_part_p (&htab, subinsn))
16614 PATTERN (subinsn) = gen_nop ();
16615 INSN_CODE (subinsn) = CODE_FOR_nop;
16617 mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
16618 &delayed_reg, lo_reg);
16620 last_insn = insn;
16622 else
16624 /* INSN is a single instruction. Delete it if it's an
16625 orphaned high-part relocation. */
16626 if (mips_orphaned_high_part_p (&htab, insn))
16627 delete_insn (insn);
16628 /* Also delete cache barriers if the last instruction
16629 was an annulled branch. INSN will not be speculatively
16630 executed. */
16631 else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
16632 && last_insn
16633 && JUMP_P (SEQ_BEGIN (last_insn))
16634 && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
16635 delete_insn (insn);
16636 else
16638 mips_avoid_hazard (last_insn, insn, &hilo_delay,
16639 &delayed_reg, lo_reg);
16640 last_insn = insn;
16647 /* Return true if the function has a long branch instruction. */
16649 static bool
16650 mips_has_long_branch_p (void)
16652 rtx_insn *insn, *subinsn;
16653 int normal_length;
16655 /* We need up-to-date instruction lengths. */
16656 shorten_branches (get_insns ());
16658 /* Look for a branch that is longer than normal. The normal length for
16659 non-MIPS16 branches is 8, because the length includes the delay slot.
16660 It is 4 for MIPS16, because MIPS16 branches are extended instructions,
16661 but they have no delay slot. */
16662 normal_length = (TARGET_MIPS16 ? 4 : 8);
16663 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16664 FOR_EACH_SUBINSN (subinsn, insn)
16665 if (JUMP_P (subinsn)
16666 && get_attr_length (subinsn) > normal_length
16667 && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
16668 return true;
16670 return false;
16673 /* If we are using a GOT, but have not decided to use a global pointer yet,
16674 see whether we need one to implement long branches. Convert the ghost
16675 global-pointer instructions into real ones if so. */
16677 static bool
16678 mips_expand_ghost_gp_insns (void)
16680 /* Quick exit if we already know that we will or won't need a
16681 global pointer. */
16682 if (!TARGET_USE_GOT
16683 || cfun->machine->global_pointer == INVALID_REGNUM
16684 || mips_must_initialize_gp_p ())
16685 return false;
16687 /* Run a full check for long branches. */
16688 if (!mips_has_long_branch_p ())
16689 return false;
16691 /* We've now established that we need $gp. */
16692 cfun->machine->must_initialize_gp_p = true;
16693 split_all_insns_noflow ();
16695 return true;
16698 /* Subroutine of mips_reorg to manage passes that require DF. */
16700 static void
16701 mips_df_reorg (void)
16703 /* Create def-use chains. */
16704 df_set_flags (DF_EQ_NOTES);
16705 df_chain_add_problem (DF_UD_CHAIN);
16706 df_analyze ();
16708 if (TARGET_RELAX_PIC_CALLS)
16709 mips_annotate_pic_calls ();
16711 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
16712 r10k_insert_cache_barriers ();
16714 df_finish_pass (false);
16717 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST. This is
16718 called very late in mips_reorg, but the caller is required to run
16719 mips16_lay_out_constants on the result. */
16721 static void
16722 mips16_load_branch_target (rtx dest, rtx src)
16724 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
16726 rtx page, low;
16728 if (mips_cfun_has_cprestore_slot_p ())
16729 mips_emit_move (dest, mips_cprestore_slot (dest, true));
16730 else
16731 mips_emit_move (dest, pic_offset_table_rtx);
16732 page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
16733 low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
16734 emit_insn (gen_rtx_SET (VOIDmode, dest,
16735 PMODE_INSN (gen_unspec_got, (dest, page))));
16736 emit_insn (gen_rtx_SET (VOIDmode, dest,
16737 gen_rtx_LO_SUM (Pmode, dest, low)));
16739 else
16741 src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
16742 mips_emit_move (dest, src);
16746 /* If we're compiling a MIPS16 function, look for and split any long branches.
16747 This must be called after all other instruction modifications in
16748 mips_reorg. */
16750 static void
16751 mips16_split_long_branches (void)
16753 bool something_changed;
16755 if (!TARGET_MIPS16)
16756 return;
16758 /* Loop until the alignments for all targets are sufficient. */
16761 rtx_insn *insn;
16763 shorten_branches (get_insns ());
16764 something_changed = false;
16765 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16766 if (JUMP_P (insn)
16767 && get_attr_length (insn) > 4
16768 && (any_condjump_p (insn) || any_uncondjump_p (insn)))
16770 rtx old_label, temp, saved_temp;
16771 rtx_code_label *new_label;
16772 rtx target;
16773 rtx_insn *jump, *jump_sequence;
16775 start_sequence ();
16777 /* Free up a MIPS16 register by saving it in $1. */
16778 saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
16779 temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
16780 emit_move_insn (saved_temp, temp);
16782 /* Load the branch target into TEMP. */
16783 old_label = JUMP_LABEL (insn);
16784 target = gen_rtx_LABEL_REF (Pmode, old_label);
16785 mips16_load_branch_target (temp, target);
16787 /* Jump to the target and restore the register's
16788 original value. */
16789 jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
16790 (temp, temp, saved_temp)));
16791 JUMP_LABEL (jump) = old_label;
16792 LABEL_NUSES (old_label)++;
16794 /* Rewrite any symbolic references that are supposed to use
16795 a PC-relative constant pool. */
16796 mips16_lay_out_constants (false);
16798 if (simplejump_p (insn))
16799 /* We're going to replace INSN with a longer form. */
16800 new_label = NULL;
16801 else
16803 /* Create a branch-around label for the original
16804 instruction. */
16805 new_label = gen_label_rtx ();
16806 emit_label (new_label);
16809 jump_sequence = get_insns ();
16810 end_sequence ();
16812 emit_insn_after (jump_sequence, insn);
16813 if (new_label)
16814 invert_jump (insn, new_label, false);
16815 else
16816 delete_insn (insn);
16817 something_changed = true;
16820 while (something_changed);
16823 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
16825 static void
16826 mips_reorg (void)
16828 /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF. Also during
16829 insn splitting in mips16_lay_out_constants, DF insn info is only kept up
16830 to date if the CFG is available. */
16831 if (mips_cfg_in_reorg ())
16832 compute_bb_for_insn ();
16833 mips16_lay_out_constants (true);
16834 if (mips_cfg_in_reorg ())
16836 mips_df_reorg ();
16837 free_bb_for_insn ();
16841 /* We use a machine specific pass to do a second machine dependent reorg
16842 pass after delay branch scheduling. */
16844 static unsigned int
16845 mips_machine_reorg2 (void)
16847 mips_reorg_process_insns ();
16848 if (!TARGET_MIPS16
16849 && TARGET_EXPLICIT_RELOCS
16850 && TUNE_MIPS4130
16851 && TARGET_VR4130_ALIGN)
16852 vr4130_align_insns ();
16853 if (mips_expand_ghost_gp_insns ())
16854 /* The expansion could invalidate some of the VR4130 alignment
16855 optimizations, but this should be an extremely rare case anyhow. */
16856 mips_reorg_process_insns ();
16857 mips16_split_long_branches ();
16858 return 0;
16861 namespace {
16863 const pass_data pass_data_mips_machine_reorg2 =
16865 RTL_PASS, /* type */
16866 "mach2", /* name */
16867 OPTGROUP_NONE, /* optinfo_flags */
16868 TV_MACH_DEP, /* tv_id */
16869 0, /* properties_required */
16870 0, /* properties_provided */
16871 0, /* properties_destroyed */
16872 0, /* todo_flags_start */
16873 0, /* todo_flags_finish */
16876 class pass_mips_machine_reorg2 : public rtl_opt_pass
16878 public:
16879 pass_mips_machine_reorg2(gcc::context *ctxt)
16880 : rtl_opt_pass(pass_data_mips_machine_reorg2, ctxt)
16883 /* opt_pass methods: */
16884 virtual unsigned int execute (function *) { return mips_machine_reorg2 (); }
16886 }; // class pass_mips_machine_reorg2
16888 } // anon namespace
16890 rtl_opt_pass *
16891 make_pass_mips_machine_reorg2 (gcc::context *ctxt)
16893 return new pass_mips_machine_reorg2 (ctxt);
16897 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
16898 in order to avoid duplicating too much logic from elsewhere. */
16900 static void
16901 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
16902 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
16903 tree function)
16905 rtx this_rtx, temp1, temp2, fnaddr;
16906 rtx_insn *insn;
16907 bool use_sibcall_p;
16909 /* Pretend to be a post-reload pass while generating rtl. */
16910 reload_completed = 1;
16912 /* Mark the end of the (empty) prologue. */
16913 emit_note (NOTE_INSN_PROLOGUE_END);
16915 /* Determine if we can use a sibcall to call FUNCTION directly. */
16916 fnaddr = XEXP (DECL_RTL (function), 0);
16917 use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
16918 && const_call_insn_operand (fnaddr, Pmode));
16920 /* Determine if we need to load FNADDR from the GOT. */
16921 if (!use_sibcall_p
16922 && (mips_got_symbol_type_p
16923 (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
16925 /* Pick a global pointer. Use a call-clobbered register if
16926 TARGET_CALL_SAVED_GP. */
16927 cfun->machine->global_pointer
16928 = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
16929 cfun->machine->must_initialize_gp_p = true;
16930 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
16932 /* Set up the global pointer for n32 or n64 abicalls. */
16933 mips_emit_loadgp ();
16936 /* We need two temporary registers in some cases. */
16937 temp1 = gen_rtx_REG (Pmode, 2);
16938 temp2 = gen_rtx_REG (Pmode, 3);
16940 /* Find out which register contains the "this" pointer. */
16941 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
16942 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
16943 else
16944 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
16946 /* Add DELTA to THIS_RTX. */
16947 if (delta != 0)
16949 rtx offset = GEN_INT (delta);
16950 if (!SMALL_OPERAND (delta))
16952 mips_emit_move (temp1, offset);
16953 offset = temp1;
16955 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
16958 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
16959 if (vcall_offset != 0)
16961 rtx addr;
16963 /* Set TEMP1 to *THIS_RTX. */
16964 mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
16966 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
16967 addr = mips_add_offset (temp2, temp1, vcall_offset);
16969 /* Load the offset and add it to THIS_RTX. */
16970 mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
16971 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
16974 /* Jump to the target function. Use a sibcall if direct jumps are
16975 allowed, otherwise load the address into a register first. */
16976 if (use_sibcall_p)
16978 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
16979 SIBLING_CALL_P (insn) = 1;
16981 else
16983 /* This is messy. GAS treats "la $25,foo" as part of a call
16984 sequence and may allow a global "foo" to be lazily bound.
16985 The general move patterns therefore reject this combination.
16987 In this context, lazy binding would actually be OK
16988 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
16989 TARGET_CALL_SAVED_GP; see mips_load_call_address.
16990 We must therefore load the address via a temporary
16991 register if mips_dangerous_for_la25_p.
16993 If we jump to the temporary register rather than $25,
16994 the assembler can use the move insn to fill the jump's
16995 delay slot.
16997 We can use the same technique for MIPS16 code, where $25
16998 is not a valid JR register. */
16999 if (TARGET_USE_PIC_FN_ADDR_REG
17000 && !TARGET_MIPS16
17001 && !mips_dangerous_for_la25_p (fnaddr))
17002 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
17003 mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
17005 if (TARGET_USE_PIC_FN_ADDR_REG
17006 && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
17007 mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
17008 emit_jump_insn (gen_indirect_jump (temp1));
17011 /* Run just enough of rest_of_compilation. This sequence was
17012 "borrowed" from alpha.c. */
17013 insn = get_insns ();
17014 split_all_insns_noflow ();
17015 mips16_lay_out_constants (true);
17016 shorten_branches (insn);
17017 final_start_function (insn, file, 1);
17018 final (insn, file, 1);
17019 final_end_function ();
17021 /* Clean up the vars set above. Note that final_end_function resets
17022 the global pointer for us. */
17023 reload_completed = 0;
17027 /* The last argument passed to mips_set_compression_mode,
17028 or negative if the function hasn't been called yet. */
17029 static unsigned int old_compression_mode = -1;
17031 /* Set up the target-dependent global state for ISA mode COMPRESSION_MODE,
17032 which is either MASK_MIPS16 or MASK_MICROMIPS. */
17034 static void
17035 mips_set_compression_mode (unsigned int compression_mode)
17038 if (compression_mode == old_compression_mode)
17039 return;
17041 /* Restore base settings of various flags. */
17042 target_flags = mips_base_target_flags;
17043 flag_schedule_insns = mips_base_schedule_insns;
17044 flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
17045 flag_move_loop_invariants = mips_base_move_loop_invariants;
17046 align_loops = mips_base_align_loops;
17047 align_jumps = mips_base_align_jumps;
17048 align_functions = mips_base_align_functions;
17049 target_flags &= ~(MASK_MIPS16 | MASK_MICROMIPS);
17050 target_flags |= compression_mode;
17052 if (compression_mode & MASK_MIPS16)
17054 /* Switch to MIPS16 mode. */
17055 target_flags |= MASK_MIPS16;
17057 /* Turn off SYNCI if it was on, MIPS16 doesn't support it. */
17058 target_flags &= ~MASK_SYNCI;
17060 /* Don't run the scheduler before reload, since it tends to
17061 increase register pressure. */
17062 flag_schedule_insns = 0;
17064 /* Don't do hot/cold partitioning. mips16_lay_out_constants expects
17065 the whole function to be in a single section. */
17066 flag_reorder_blocks_and_partition = 0;
17068 /* Don't move loop invariants, because it tends to increase
17069 register pressure. It also introduces an extra move in cases
17070 where the constant is the first operand in a two-operand binary
17071 instruction, or when it forms a register argument to a functon
17072 call. */
17073 flag_move_loop_invariants = 0;
17075 target_flags |= MASK_EXPLICIT_RELOCS;
17077 /* Experiments suggest we get the best overall section-anchor
17078 results from using the range of an unextended LW or SW. Code
17079 that makes heavy use of byte or short accesses can do better
17080 with ranges of 0...31 and 0...63 respectively, but most code is
17081 sensitive to the range of LW and SW instead. */
17082 targetm.min_anchor_offset = 0;
17083 targetm.max_anchor_offset = 127;
17085 targetm.const_anchor = 0;
17087 /* MIPS16 has no BAL instruction. */
17088 target_flags &= ~MASK_RELAX_PIC_CALLS;
17090 /* The R4000 errata don't apply to any known MIPS16 cores.
17091 It's simpler to make the R4000 fixes and MIPS16 mode
17092 mutually exclusive. */
17093 target_flags &= ~MASK_FIX_R4000;
17095 if (flag_pic && !TARGET_OLDABI)
17096 sorry ("MIPS16 PIC for ABIs other than o32 and o64");
17098 if (TARGET_XGOT)
17099 sorry ("MIPS16 -mxgot code");
17101 if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
17102 sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
17104 else
17106 /* Switch to microMIPS or the standard encoding. */
17108 if (TARGET_MICROMIPS)
17109 /* Avoid branch likely. */
17110 target_flags &= ~MASK_BRANCHLIKELY;
17112 /* Provide default values for align_* for 64-bit targets. */
17113 if (TARGET_64BIT)
17115 if (align_loops == 0)
17116 align_loops = 8;
17117 if (align_jumps == 0)
17118 align_jumps = 8;
17119 if (align_functions == 0)
17120 align_functions = 8;
17123 targetm.min_anchor_offset = -32768;
17124 targetm.max_anchor_offset = 32767;
17126 targetm.const_anchor = 0x8000;
17129 /* (Re)initialize MIPS target internals for new ISA. */
17130 mips_init_relocs ();
17132 if (compression_mode & MASK_MIPS16)
17134 if (!mips16_globals)
17135 mips16_globals = save_target_globals_default_opts ();
17136 else
17137 restore_target_globals (mips16_globals);
17139 else
17140 restore_target_globals (&default_target_globals);
17142 old_compression_mode = compression_mode;
17145 /* Implement TARGET_SET_CURRENT_FUNCTION. Decide whether the current
17146 function should use the MIPS16 or microMIPS ISA and switch modes
17147 accordingly. */
17149 static void
17150 mips_set_current_function (tree fndecl)
17152 mips_set_compression_mode (mips_get_compress_mode (fndecl));
17155 /* Allocate a chunk of memory for per-function machine-dependent data. */
17157 static struct machine_function *
17158 mips_init_machine_status (void)
17160 return ggc_cleared_alloc<machine_function> ();
17163 /* Return the processor associated with the given ISA level, or null
17164 if the ISA isn't valid. */
17166 static const struct mips_cpu_info *
17167 mips_cpu_info_from_isa (int isa)
17169 unsigned int i;
17171 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
17172 if (mips_cpu_info_table[i].isa == isa)
17173 return mips_cpu_info_table + i;
17175 return NULL;
17178 /* Return a mips_cpu_info entry determined by an option valued
17179 OPT. */
17181 static const struct mips_cpu_info *
17182 mips_cpu_info_from_opt (int opt)
17184 switch (opt)
17186 case MIPS_ARCH_OPTION_FROM_ABI:
17187 /* 'from-abi' selects the most compatible architecture for the
17188 given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
17189 ABIs. For the EABIs, we have to decide whether we're using
17190 the 32-bit or 64-bit version. */
17191 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
17192 : ABI_NEEDS_64BIT_REGS ? 3
17193 : (TARGET_64BIT ? 3 : 1));
17195 case MIPS_ARCH_OPTION_NATIVE:
17196 gcc_unreachable ();
17198 default:
17199 return &mips_cpu_info_table[opt];
17203 /* Return a default mips_cpu_info entry, given that no -march= option
17204 was explicitly specified. */
17206 static const struct mips_cpu_info *
17207 mips_default_arch (void)
17209 #if defined (MIPS_CPU_STRING_DEFAULT)
17210 unsigned int i;
17211 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
17212 if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
17213 return mips_cpu_info_table + i;
17214 gcc_unreachable ();
17215 #elif defined (MIPS_ISA_DEFAULT)
17216 return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
17217 #else
17218 /* 'from-abi' makes a good default: you get whatever the ABI
17219 requires. */
17220 return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
17221 #endif
17224 /* Set up globals to generate code for the ISA or processor
17225 described by INFO. */
17227 static void
17228 mips_set_architecture (const struct mips_cpu_info *info)
17230 if (info != 0)
17232 mips_arch_info = info;
17233 mips_arch = info->cpu;
17234 mips_isa = info->isa;
17235 if (mips_isa < 32)
17236 mips_isa_rev = 0;
17237 else
17238 mips_isa_rev = (mips_isa & 31) + 1;
17242 /* Likewise for tuning. */
17244 static void
17245 mips_set_tune (const struct mips_cpu_info *info)
17247 if (info != 0)
17249 mips_tune_info = info;
17250 mips_tune = info->cpu;
17254 /* Implement TARGET_OPTION_OVERRIDE. */
17256 static void
17257 mips_option_override (void)
17259 int i, start, regno, mode;
17261 if (global_options_set.x_mips_isa_option)
17262 mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
17264 #ifdef SUBTARGET_OVERRIDE_OPTIONS
17265 SUBTARGET_OVERRIDE_OPTIONS;
17266 #endif
17268 /* MIPS16 and microMIPS cannot coexist. */
17269 if (TARGET_MICROMIPS && TARGET_MIPS16)
17270 error ("unsupported combination: %s", "-mips16 -mmicromips");
17272 /* Save the base compression state and process flags as though we
17273 were generating uncompressed code. */
17274 mips_base_compression_flags = TARGET_COMPRESSION;
17275 target_flags &= ~TARGET_COMPRESSION;
17277 /* -mno-float overrides -mhard-float and -msoft-float. */
17278 if (TARGET_NO_FLOAT)
17280 target_flags |= MASK_SOFT_FLOAT_ABI;
17281 target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
17284 if (TARGET_FLIP_MIPS16)
17285 TARGET_INTERLINK_COMPRESSED = 1;
17287 /* Set the small data limit. */
17288 mips_small_data_threshold = (global_options_set.x_g_switch_value
17289 ? g_switch_value
17290 : MIPS_DEFAULT_GVALUE);
17292 /* The following code determines the architecture and register size.
17293 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
17294 The GAS and GCC code should be kept in sync as much as possible. */
17296 if (global_options_set.x_mips_arch_option)
17297 mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
17299 if (mips_isa_option_info != 0)
17301 if (mips_arch_info == 0)
17302 mips_set_architecture (mips_isa_option_info);
17303 else if (mips_arch_info->isa != mips_isa_option_info->isa)
17304 error ("%<-%s%> conflicts with the other architecture options, "
17305 "which specify a %s processor",
17306 mips_isa_option_info->name,
17307 mips_cpu_info_from_isa (mips_arch_info->isa)->name);
17310 if (mips_arch_info == 0)
17311 mips_set_architecture (mips_default_arch ());
17313 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
17314 error ("%<-march=%s%> is not compatible with the selected ABI",
17315 mips_arch_info->name);
17317 /* Optimize for mips_arch, unless -mtune selects a different processor. */
17318 if (global_options_set.x_mips_tune_option)
17319 mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
17321 if (mips_tune_info == 0)
17322 mips_set_tune (mips_arch_info);
17324 if ((target_flags_explicit & MASK_64BIT) != 0)
17326 /* The user specified the size of the integer registers. Make sure
17327 it agrees with the ABI and ISA. */
17328 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
17329 error ("%<-mgp64%> used with a 32-bit processor");
17330 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
17331 error ("%<-mgp32%> used with a 64-bit ABI");
17332 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
17333 error ("%<-mgp64%> used with a 32-bit ABI");
17335 else
17337 /* Infer the integer register size from the ABI and processor.
17338 Restrict ourselves to 32-bit registers if that's all the
17339 processor has, or if the ABI cannot handle 64-bit registers. */
17340 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
17341 target_flags &= ~MASK_64BIT;
17342 else
17343 target_flags |= MASK_64BIT;
17346 if ((target_flags_explicit & MASK_FLOAT64) != 0)
17348 if (mips_isa_rev >= 6 && !TARGET_FLOAT64)
17349 error ("the %qs architecture does not support %<-mfp32%>",
17350 mips_arch_info->name);
17351 else if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
17352 error ("unsupported combination: %s", "-mfp64 -msingle-float");
17353 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
17354 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
17355 else if (!TARGET_64BIT && TARGET_FLOAT64)
17357 if (!ISA_HAS_MXHC1)
17358 error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
17359 " the target supports the mfhc1 and mthc1 instructions");
17360 else if (mips_abi != ABI_32)
17361 error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
17362 " the o32 ABI");
17365 else
17367 /* -msingle-float selects 32-bit float registers. On r6 and later,
17368 -mdouble-float selects 64-bit float registers, since the old paired
17369 register model is not supported. In other cases the float registers
17370 should be the same size as the integer ones. */
17371 if (mips_isa_rev >= 6 && TARGET_DOUBLE_FLOAT && !TARGET_FLOATXX)
17372 target_flags |= MASK_FLOAT64;
17373 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
17374 target_flags |= MASK_FLOAT64;
17375 else
17376 target_flags &= ~MASK_FLOAT64;
17379 if (mips_abi != ABI_32 && TARGET_FLOATXX)
17380 error ("%<-mfpxx%> can only be used with the o32 ABI");
17381 else if (TARGET_FLOAT64 && TARGET_FLOATXX)
17382 error ("unsupported combination: %s", "-mfp64 -mfpxx");
17383 else if (ISA_MIPS1 && !TARGET_FLOAT32)
17384 error ("%<-march=%s%> requires %<-mfp32%>", mips_arch_info->name);
17385 else if (TARGET_FLOATXX && !mips_lra_flag)
17386 error ("%<-mfpxx%> requires %<-mlra%>");
17388 /* End of code shared with GAS. */
17390 /* The R5900 FPU only supports single precision. */
17391 if (TARGET_MIPS5900 && TARGET_HARD_FLOAT_ABI && TARGET_DOUBLE_FLOAT)
17392 error ("unsupported combination: %s",
17393 "-march=r5900 -mhard-float -mdouble-float");
17395 /* If a -mlong* option was given, check that it matches the ABI,
17396 otherwise infer the -mlong* setting from the other options. */
17397 if ((target_flags_explicit & MASK_LONG64) != 0)
17399 if (TARGET_LONG64)
17401 if (mips_abi == ABI_N32)
17402 error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
17403 else if (mips_abi == ABI_32)
17404 error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
17405 else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
17406 /* We have traditionally allowed non-abicalls code to use
17407 an LP64 form of o64. However, it would take a bit more
17408 effort to support the combination of 32-bit GOT entries
17409 and 64-bit pointers, so we treat the abicalls case as
17410 an error. */
17411 error ("the combination of %qs and %qs is incompatible with %qs",
17412 "-mabi=o64", "-mabicalls", "-mlong64");
17414 else
17416 if (mips_abi == ABI_64)
17417 error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
17420 else
17422 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
17423 target_flags |= MASK_LONG64;
17424 else
17425 target_flags &= ~MASK_LONG64;
17428 if (!TARGET_OLDABI)
17429 flag_pcc_struct_return = 0;
17431 /* Decide which rtx_costs structure to use. */
17432 if (optimize_size)
17433 mips_cost = &mips_rtx_cost_optimize_size;
17434 else
17435 mips_cost = &mips_rtx_cost_data[mips_tune];
17437 /* If the user hasn't specified a branch cost, use the processor's
17438 default. */
17439 if (mips_branch_cost == 0)
17440 mips_branch_cost = mips_cost->branch_cost;
17442 /* If neither -mbranch-likely nor -mno-branch-likely was given
17443 on the command line, set MASK_BRANCHLIKELY based on the target
17444 architecture and tuning flags. Annulled delay slots are a
17445 size win, so we only consider the processor-specific tuning
17446 for !optimize_size. */
17447 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
17449 if (ISA_HAS_BRANCHLIKELY
17450 && (optimize_size
17451 || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
17452 target_flags |= MASK_BRANCHLIKELY;
17453 else
17454 target_flags &= ~MASK_BRANCHLIKELY;
17456 else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
17457 warning (0, "the %qs architecture does not support branch-likely"
17458 " instructions", mips_arch_info->name);
17460 /* If the user hasn't specified -mimadd or -mno-imadd set
17461 MASK_IMADD based on the target architecture and tuning
17462 flags. */
17463 if ((target_flags_explicit & MASK_IMADD) == 0)
17465 if (ISA_HAS_MADD_MSUB &&
17466 (mips_tune_info->tune_flags & PTF_AVOID_IMADD) == 0)
17467 target_flags |= MASK_IMADD;
17468 else
17469 target_flags &= ~MASK_IMADD;
17471 else if (TARGET_IMADD && !ISA_HAS_MADD_MSUB)
17472 warning (0, "the %qs architecture does not support madd or msub"
17473 " instructions", mips_arch_info->name);
17475 /* If neither -modd-spreg nor -mno-odd-spreg was given on the command
17476 line, set MASK_ODD_SPREG based on the ISA and ABI. */
17477 if ((target_flags_explicit & MASK_ODD_SPREG) == 0)
17479 /* Disable TARGET_ODD_SPREG when using the o32 FPXX ABI. */
17480 if (!ISA_HAS_ODD_SPREG || TARGET_FLOATXX)
17481 target_flags &= ~MASK_ODD_SPREG;
17482 else
17483 target_flags |= MASK_ODD_SPREG;
17485 else if (TARGET_ODD_SPREG && !ISA_HAS_ODD_SPREG)
17486 warning (0, "the %qs architecture does not support odd single-precision"
17487 " registers", mips_arch_info->name);
17489 if (!TARGET_ODD_SPREG && TARGET_64BIT)
17491 error ("unsupported combination: %s", "-mgp64 -mno-odd-spreg");
17492 /* Allow compilation to continue further even though invalid output
17493 will be produced. */
17494 target_flags |= MASK_ODD_SPREG;
17497 /* The effect of -mabicalls isn't defined for the EABI. */
17498 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
17500 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
17501 target_flags &= ~MASK_ABICALLS;
17504 /* PIC requires -mabicalls. */
17505 if (flag_pic)
17507 if (mips_abi == ABI_EABI)
17508 error ("cannot generate position-independent code for %qs",
17509 "-mabi=eabi");
17510 else if (!TARGET_ABICALLS)
17511 error ("position-independent code requires %qs", "-mabicalls");
17514 if (TARGET_ABICALLS_PIC2)
17515 /* We need to set flag_pic for executables as well as DSOs
17516 because we may reference symbols that are not defined in
17517 the final executable. (MIPS does not use things like
17518 copy relocs, for example.)
17520 There is a body of code that uses __PIC__ to distinguish
17521 between -mabicalls and -mno-abicalls code. The non-__PIC__
17522 variant is usually appropriate for TARGET_ABICALLS_PIC0, as
17523 long as any indirect jumps use $25. */
17524 flag_pic = 1;
17526 /* -mvr4130-align is a "speed over size" optimization: it usually produces
17527 faster code, but at the expense of more nops. Enable it at -O3 and
17528 above. */
17529 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
17530 target_flags |= MASK_VR4130_ALIGN;
17532 /* Prefer a call to memcpy over inline code when optimizing for size,
17533 though see MOVE_RATIO in mips.h. */
17534 if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
17535 target_flags |= MASK_MEMCPY;
17537 /* If we have a nonzero small-data limit, check that the -mgpopt
17538 setting is consistent with the other target flags. */
17539 if (mips_small_data_threshold > 0)
17541 if (!TARGET_GPOPT)
17543 if (!TARGET_EXPLICIT_RELOCS)
17544 error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
17546 TARGET_LOCAL_SDATA = false;
17547 TARGET_EXTERN_SDATA = false;
17549 else
17551 if (TARGET_VXWORKS_RTP)
17552 warning (0, "cannot use small-data accesses for %qs", "-mrtp");
17554 if (TARGET_ABICALLS)
17555 warning (0, "cannot use small-data accesses for %qs",
17556 "-mabicalls");
17560 /* Set NaN and ABS defaults. */
17561 if (mips_nan == MIPS_IEEE_754_DEFAULT && !ISA_HAS_IEEE_754_LEGACY)
17562 mips_nan = MIPS_IEEE_754_2008;
17563 if (mips_abs == MIPS_IEEE_754_DEFAULT && !ISA_HAS_IEEE_754_LEGACY)
17564 mips_abs = MIPS_IEEE_754_2008;
17566 /* Check for IEEE 754 legacy/2008 support. */
17567 if ((mips_nan == MIPS_IEEE_754_LEGACY
17568 || mips_abs == MIPS_IEEE_754_LEGACY)
17569 && !ISA_HAS_IEEE_754_LEGACY)
17570 warning (0, "the %qs architecture does not support %<-m%s=legacy%>",
17571 mips_arch_info->name,
17572 mips_nan == MIPS_IEEE_754_LEGACY ? "nan" : "abs");
17574 if ((mips_nan == MIPS_IEEE_754_2008
17575 || mips_abs == MIPS_IEEE_754_2008)
17576 && !ISA_HAS_IEEE_754_2008)
17577 warning (0, "the %qs architecture does not support %<-m%s=2008%>",
17578 mips_arch_info->name,
17579 mips_nan == MIPS_IEEE_754_2008 ? "nan" : "abs");
17581 /* Pre-IEEE 754-2008 MIPS hardware has a quirky almost-IEEE format
17582 for all its floating point. */
17583 if (mips_nan != MIPS_IEEE_754_2008)
17585 REAL_MODE_FORMAT (SFmode) = &mips_single_format;
17586 REAL_MODE_FORMAT (DFmode) = &mips_double_format;
17587 REAL_MODE_FORMAT (TFmode) = &mips_quad_format;
17590 /* Make sure that the user didn't turn off paired single support when
17591 MIPS-3D support is requested. */
17592 if (TARGET_MIPS3D
17593 && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
17594 && !TARGET_PAIRED_SINGLE_FLOAT)
17595 error ("%<-mips3d%> requires %<-mpaired-single%>");
17597 /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT. */
17598 if (TARGET_MIPS3D)
17599 target_flags |= MASK_PAIRED_SINGLE_FLOAT;
17601 /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
17602 and TARGET_HARD_FLOAT_ABI are both true. */
17603 if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
17605 error ("%qs must be used with %qs",
17606 TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
17607 TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
17608 target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
17609 TARGET_MIPS3D = 0;
17612 /* Make sure that -mpaired-single is only used on ISAs that support it.
17613 We must disable it otherwise since it relies on other ISA properties
17614 like ISA_HAS_8CC having their normal values. */
17615 if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
17617 error ("the %qs architecture does not support paired-single"
17618 " instructions", mips_arch_info->name);
17619 target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
17620 TARGET_MIPS3D = 0;
17623 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
17624 && !TARGET_CACHE_BUILTIN)
17626 error ("%qs requires a target that provides the %qs instruction",
17627 "-mr10k-cache-barrier", "cache");
17628 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
17631 /* If TARGET_DSPR2, enable TARGET_DSP. */
17632 if (TARGET_DSPR2)
17633 TARGET_DSP = true;
17635 if (TARGET_DSP && mips_isa_rev >= 6)
17637 error ("the %qs architecture does not support DSP instructions",
17638 mips_arch_info->name);
17639 TARGET_DSP = false;
17640 TARGET_DSPR2 = false;
17643 /* .eh_frame addresses should be the same width as a C pointer.
17644 Most MIPS ABIs support only one pointer size, so the assembler
17645 will usually know exactly how big an .eh_frame address is.
17647 Unfortunately, this is not true of the 64-bit EABI. The ABI was
17648 originally defined to use 64-bit pointers (i.e. it is LP64), and
17649 this is still the default mode. However, we also support an n32-like
17650 ILP32 mode, which is selected by -mlong32. The problem is that the
17651 assembler has traditionally not had an -mlong option, so it has
17652 traditionally not known whether we're using the ILP32 or LP64 form.
17654 As it happens, gas versions up to and including 2.19 use _32-bit_
17655 addresses for EABI64 .cfi_* directives. This is wrong for the
17656 default LP64 mode, so we can't use the directives by default.
17657 Moreover, since gas's current behavior is at odds with gcc's
17658 default behavior, it seems unwise to rely on future versions
17659 of gas behaving the same way. We therefore avoid using .cfi
17660 directives for -mlong32 as well. */
17661 if (mips_abi == ABI_EABI && TARGET_64BIT)
17662 flag_dwarf2_cfi_asm = 0;
17664 /* .cfi_* directives generate a read-only section, so fall back on
17665 manual .eh_frame creation if we need the section to be writable. */
17666 if (TARGET_WRITABLE_EH_FRAME)
17667 flag_dwarf2_cfi_asm = 0;
17669 mips_init_print_operand_punct ();
17671 /* Set up array to map GCC register number to debug register number.
17672 Ignore the special purpose register numbers. */
17674 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17676 mips_dbx_regno[i] = IGNORED_DWARF_REGNUM;
17677 if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
17678 mips_dwarf_regno[i] = i;
17679 else
17680 mips_dwarf_regno[i] = INVALID_REGNUM;
17683 start = GP_DBX_FIRST - GP_REG_FIRST;
17684 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
17685 mips_dbx_regno[i] = i + start;
17687 start = FP_DBX_FIRST - FP_REG_FIRST;
17688 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
17689 mips_dbx_regno[i] = i + start;
17691 /* Accumulator debug registers use big-endian ordering. */
17692 mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
17693 mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
17694 mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
17695 mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
17696 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
17698 mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
17699 mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
17702 /* Set up mips_hard_regno_mode_ok. */
17703 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
17704 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
17705 mips_hard_regno_mode_ok[mode][regno]
17706 = mips_hard_regno_mode_ok_p (regno, (machine_mode) mode);
17708 /* Function to allocate machine-dependent function status. */
17709 init_machine_status = &mips_init_machine_status;
17711 /* Default to working around R4000 errata only if the processor
17712 was selected explicitly. */
17713 if ((target_flags_explicit & MASK_FIX_R4000) == 0
17714 && strcmp (mips_arch_info->name, "r4000") == 0)
17715 target_flags |= MASK_FIX_R4000;
17717 /* Default to working around R4400 errata only if the processor
17718 was selected explicitly. */
17719 if ((target_flags_explicit & MASK_FIX_R4400) == 0
17720 && strcmp (mips_arch_info->name, "r4400") == 0)
17721 target_flags |= MASK_FIX_R4400;
17723 /* Default to working around R10000 errata only if the processor
17724 was selected explicitly. */
17725 if ((target_flags_explicit & MASK_FIX_R10000) == 0
17726 && strcmp (mips_arch_info->name, "r10000") == 0)
17727 target_flags |= MASK_FIX_R10000;
17729 /* Make sure that branch-likely instructions available when using
17730 -mfix-r10000. The instructions are not available if either:
17732 1. -mno-branch-likely was passed.
17733 2. The selected ISA does not support branch-likely and
17734 the command line does not include -mbranch-likely. */
17735 if (TARGET_FIX_R10000
17736 && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
17737 ? !ISA_HAS_BRANCHLIKELY
17738 : !TARGET_BRANCHLIKELY))
17739 sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
17741 if (TARGET_SYNCI && !ISA_HAS_SYNCI)
17743 warning (0, "the %qs architecture does not support the synci "
17744 "instruction", mips_arch_info->name);
17745 target_flags &= ~MASK_SYNCI;
17748 /* Only optimize PIC indirect calls if they are actually required. */
17749 if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
17750 target_flags &= ~MASK_RELAX_PIC_CALLS;
17752 /* Save base state of options. */
17753 mips_base_target_flags = target_flags;
17754 mips_base_schedule_insns = flag_schedule_insns;
17755 mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
17756 mips_base_move_loop_invariants = flag_move_loop_invariants;
17757 mips_base_align_loops = align_loops;
17758 mips_base_align_jumps = align_jumps;
17759 mips_base_align_functions = align_functions;
17761 /* Now select the ISA mode.
17763 Do all CPP-sensitive stuff in uncompressed mode; we'll switch modes
17764 later if required. */
17765 mips_set_compression_mode (0);
17767 /* We register a second machine specific reorg pass after delay slot
17768 filling. Registering the pass must be done at start up. It's
17769 convenient to do it here. */
17770 opt_pass *new_pass = make_pass_mips_machine_reorg2 (g);
17771 struct register_pass_info insert_pass_mips_machine_reorg2 =
17773 new_pass, /* pass */
17774 "dbr", /* reference_pass_name */
17775 1, /* ref_pass_instance_number */
17776 PASS_POS_INSERT_AFTER /* po_op */
17778 register_pass (&insert_pass_mips_machine_reorg2);
17780 if (TARGET_HARD_FLOAT_ABI && TARGET_MIPS5900)
17781 REAL_MODE_FORMAT (SFmode) = &spu_single_format;
17784 /* Swap the register information for registers I and I + 1, which
17785 currently have the wrong endianness. Note that the registers'
17786 fixedness and call-clobberedness might have been set on the
17787 command line. */
17789 static void
17790 mips_swap_registers (unsigned int i)
17792 int tmpi;
17793 const char *tmps;
17795 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
17796 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
17798 SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
17799 SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
17800 SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
17801 SWAP_STRING (reg_names[i], reg_names[i + 1]);
17803 #undef SWAP_STRING
17804 #undef SWAP_INT
17807 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
17809 static void
17810 mips_conditional_register_usage (void)
17813 if (ISA_HAS_DSP)
17815 /* These DSP control register fields are global. */
17816 global_regs[CCDSP_PO_REGNUM] = 1;
17817 global_regs[CCDSP_SC_REGNUM] = 1;
17819 else
17820 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17821 reg_class_contents[(int) DSP_ACC_REGS]);
17823 if (!ISA_HAS_HILO)
17824 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17825 reg_class_contents[(int) MD_REGS]);
17827 if (!TARGET_HARD_FLOAT)
17829 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17830 reg_class_contents[(int) FP_REGS]);
17831 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17832 reg_class_contents[(int) ST_REGS]);
17834 else if (!ISA_HAS_8CC)
17836 /* We only have a single condition-code register. We implement
17837 this by fixing all the condition-code registers and generating
17838 RTL that refers directly to ST_REG_FIRST. */
17839 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17840 reg_class_contents[(int) ST_REGS]);
17841 if (!ISA_HAS_CCF)
17842 SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
17843 fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
17845 if (TARGET_MIPS16)
17847 /* In MIPS16 mode, we prohibit the unused $s registers, since they
17848 are call-saved, and saving them via a MIPS16 register would
17849 probably waste more time than just reloading the value.
17851 We permit the $t temporary registers when optimizing for speed
17852 but not when optimizing for space because using them results in
17853 code that is larger (but faster) then not using them. We do
17854 allow $24 (t8) because it is used in CMP and CMPI instructions
17855 and $25 (t9) because it is used as the function call address in
17856 SVR4 PIC code. */
17858 fixed_regs[18] = call_used_regs[18] = 1;
17859 fixed_regs[19] = call_used_regs[19] = 1;
17860 fixed_regs[20] = call_used_regs[20] = 1;
17861 fixed_regs[21] = call_used_regs[21] = 1;
17862 fixed_regs[22] = call_used_regs[22] = 1;
17863 fixed_regs[23] = call_used_regs[23] = 1;
17864 fixed_regs[26] = call_used_regs[26] = 1;
17865 fixed_regs[27] = call_used_regs[27] = 1;
17866 fixed_regs[30] = call_used_regs[30] = 1;
17867 if (optimize_size)
17869 fixed_regs[8] = call_used_regs[8] = 1;
17870 fixed_regs[9] = call_used_regs[9] = 1;
17871 fixed_regs[10] = call_used_regs[10] = 1;
17872 fixed_regs[11] = call_used_regs[11] = 1;
17873 fixed_regs[12] = call_used_regs[12] = 1;
17874 fixed_regs[13] = call_used_regs[13] = 1;
17875 fixed_regs[14] = call_used_regs[14] = 1;
17876 fixed_regs[15] = call_used_regs[15] = 1;
17879 /* Do not allow HI and LO to be treated as register operands.
17880 There are no MTHI or MTLO instructions (or any real need
17881 for them) and one-way registers cannot easily be reloaded. */
17882 AND_COMPL_HARD_REG_SET (operand_reg_set,
17883 reg_class_contents[(int) MD_REGS]);
17885 /* $f20-$f23 are call-clobbered for n64. */
17886 if (mips_abi == ABI_64)
17888 int regno;
17889 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
17890 call_really_used_regs[regno] = call_used_regs[regno] = 1;
17892 /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
17893 for n32 and o32 FP64. */
17894 if (mips_abi == ABI_N32
17895 || (mips_abi == ABI_32
17896 && TARGET_FLOAT64))
17898 int regno;
17899 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
17900 call_really_used_regs[regno] = call_used_regs[regno] = 1;
17902 /* Make sure that double-register accumulator values are correctly
17903 ordered for the current endianness. */
17904 if (TARGET_LITTLE_ENDIAN)
17906 unsigned int regno;
17908 mips_swap_registers (MD_REG_FIRST);
17909 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
17910 mips_swap_registers (regno);
17914 /* Implement EH_USES. */
17916 bool
17917 mips_eh_uses (unsigned int regno)
17919 if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
17921 /* We need to force certain registers to be live in order to handle
17922 PIC long branches correctly. See mips_must_initialize_gp_p for
17923 details. */
17924 if (mips_cfun_has_cprestore_slot_p ())
17926 if (regno == CPRESTORE_SLOT_REGNUM)
17927 return true;
17929 else
17931 if (cfun->machine->global_pointer == regno)
17932 return true;
17936 return false;
17939 /* Implement EPILOGUE_USES. */
17941 bool
17942 mips_epilogue_uses (unsigned int regno)
17944 /* Say that the epilogue uses the return address register. Note that
17945 in the case of sibcalls, the values "used by the epilogue" are
17946 considered live at the start of the called function. */
17947 if (regno == RETURN_ADDR_REGNUM)
17948 return true;
17950 /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
17951 See the comment above load_call<mode> for details. */
17952 if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
17953 return true;
17955 /* An interrupt handler must preserve some registers that are
17956 ordinarily call-clobbered. */
17957 if (cfun->machine->interrupt_handler_p
17958 && mips_interrupt_extra_call_saved_reg_p (regno))
17959 return true;
17961 return false;
17964 /* Return true if INSN needs to be wrapped in ".set noat".
17965 INSN has NOPERANDS operands, stored in OPVEC. */
17967 static bool
17968 mips_need_noat_wrapper_p (rtx_insn *insn, rtx *opvec, int noperands)
17970 if (recog_memoized (insn) >= 0)
17972 subrtx_iterator::array_type array;
17973 for (int i = 0; i < noperands; i++)
17974 FOR_EACH_SUBRTX (iter, array, opvec[i], NONCONST)
17975 if (REG_P (*iter) && REGNO (*iter) == AT_REGNUM)
17976 return true;
17978 return false;
17981 /* Implement FINAL_PRESCAN_INSN. */
17983 void
17984 mips_final_prescan_insn (rtx_insn *insn, rtx *opvec, int noperands)
17986 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17987 mips_push_asm_switch (&mips_noat);
17990 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN. */
17992 static void
17993 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx_insn *insn,
17994 rtx *opvec, int noperands)
17996 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17997 mips_pop_asm_switch (&mips_noat);
18000 /* Return the function that is used to expand the <u>mulsidi3 pattern.
18001 EXT_CODE is the code of the extension used. Return NULL if widening
18002 multiplication shouldn't be used. */
18004 mulsidi3_gen_fn
18005 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
18007 bool signed_p;
18009 signed_p = ext_code == SIGN_EXTEND;
18010 if (TARGET_64BIT)
18012 /* Don't use widening multiplication with MULT when we have DMUL. Even
18013 with the extension of its input operands DMUL is faster. Note that
18014 the extension is not needed for signed multiplication. In order to
18015 ensure that we always remove the redundant sign-extension in this
18016 case we still expand mulsidi3 for DMUL. */
18017 if (ISA_HAS_R6DMUL)
18018 return signed_p ? gen_mulsidi3_64bit_r6dmul : NULL;
18019 if (ISA_HAS_DMUL3)
18020 return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
18021 if (TARGET_MIPS16)
18022 return (signed_p
18023 ? gen_mulsidi3_64bit_mips16
18024 : gen_umulsidi3_64bit_mips16);
18025 if (TARGET_FIX_R4000)
18026 return NULL;
18027 return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
18029 else
18031 if (ISA_HAS_R6MUL)
18032 return (signed_p ? gen_mulsidi3_32bit_r6 : gen_umulsidi3_32bit_r6);
18033 if (TARGET_MIPS16)
18034 return (signed_p
18035 ? gen_mulsidi3_32bit_mips16
18036 : gen_umulsidi3_32bit_mips16);
18037 if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
18038 return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
18039 return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
18043 /* Return true if PATTERN matches the kind of instruction generated by
18044 umips_build_save_restore. SAVE_P is true for store. */
18046 bool
18047 umips_save_restore_pattern_p (bool save_p, rtx pattern)
18049 int n;
18050 unsigned int i;
18051 HOST_WIDE_INT first_offset = 0;
18052 rtx first_base = 0;
18053 unsigned int regmask = 0;
18055 for (n = 0; n < XVECLEN (pattern, 0); n++)
18057 rtx set, reg, mem, this_base;
18058 HOST_WIDE_INT this_offset;
18060 /* Check that we have a SET. */
18061 set = XVECEXP (pattern, 0, n);
18062 if (GET_CODE (set) != SET)
18063 return false;
18065 /* Check that the SET is a load (if restoring) or a store
18066 (if saving). */
18067 mem = save_p ? SET_DEST (set) : SET_SRC (set);
18068 if (!MEM_P (mem) || MEM_VOLATILE_P (mem))
18069 return false;
18071 /* Check that the address is the sum of base and a possibly-zero
18072 constant offset. Determine if the offset is in range. */
18073 mips_split_plus (XEXP (mem, 0), &this_base, &this_offset);
18074 if (!REG_P (this_base))
18075 return false;
18077 if (n == 0)
18079 if (!UMIPS_12BIT_OFFSET_P (this_offset))
18080 return false;
18081 first_base = this_base;
18082 first_offset = this_offset;
18084 else
18086 /* Check that the save slots are consecutive. */
18087 if (REGNO (this_base) != REGNO (first_base)
18088 || this_offset != first_offset + UNITS_PER_WORD * n)
18089 return false;
18092 /* Check that SET's other operand is a register. */
18093 reg = save_p ? SET_SRC (set) : SET_DEST (set);
18094 if (!REG_P (reg))
18095 return false;
18097 regmask |= 1 << REGNO (reg);
18100 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
18101 if (regmask == umips_swm_mask[i])
18102 return true;
18104 return false;
18107 /* Return the assembly instruction for microMIPS LWM or SWM.
18108 SAVE_P and PATTERN are as for umips_save_restore_pattern_p. */
18110 const char *
18111 umips_output_save_restore (bool save_p, rtx pattern)
18113 static char buffer[300];
18114 char *s;
18115 int n;
18116 HOST_WIDE_INT offset;
18117 rtx base, mem, set, last_set, last_reg;
18119 /* Parse the pattern. */
18120 gcc_assert (umips_save_restore_pattern_p (save_p, pattern));
18122 s = strcpy (buffer, save_p ? "swm\t" : "lwm\t");
18123 s += strlen (s);
18124 n = XVECLEN (pattern, 0);
18126 set = XVECEXP (pattern, 0, 0);
18127 mem = save_p ? SET_DEST (set) : SET_SRC (set);
18128 mips_split_plus (XEXP (mem, 0), &base, &offset);
18130 last_set = XVECEXP (pattern, 0, n - 1);
18131 last_reg = save_p ? SET_SRC (last_set) : SET_DEST (last_set);
18133 if (REGNO (last_reg) == 31)
18134 n--;
18136 gcc_assert (n <= 9);
18137 if (n == 0)
18139 else if (n == 1)
18140 s += sprintf (s, "%s,", reg_names[16]);
18141 else if (n < 9)
18142 s += sprintf (s, "%s-%s,", reg_names[16], reg_names[15 + n]);
18143 else if (n == 9)
18144 s += sprintf (s, "%s-%s,%s,", reg_names[16], reg_names[23],
18145 reg_names[30]);
18147 if (REGNO (last_reg) == 31)
18148 s += sprintf (s, "%s,", reg_names[31]);
18150 s += sprintf (s, "%d(%s)", (int)offset, reg_names[REGNO (base)]);
18151 return buffer;
18154 /* Return true if MEM1 and MEM2 use the same base register, and the
18155 offset of MEM2 equals the offset of MEM1 plus 4. FIRST_REG is the
18156 register into (from) which the contents of MEM1 will be loaded
18157 (stored), depending on the value of LOAD_P.
18158 SWAP_P is true when the 1st and 2nd instructions are swapped. */
18160 static bool
18161 umips_load_store_pair_p_1 (bool load_p, bool swap_p,
18162 rtx first_reg, rtx mem1, rtx mem2)
18164 rtx base1, base2;
18165 HOST_WIDE_INT offset1, offset2;
18167 if (!MEM_P (mem1) || !MEM_P (mem2))
18168 return false;
18170 mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
18171 mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
18173 if (!REG_P (base1) || !rtx_equal_p (base1, base2))
18174 return false;
18176 /* Avoid invalid load pair instructions. */
18177 if (load_p && REGNO (first_reg) == REGNO (base1))
18178 return false;
18180 /* We must avoid this case for anti-dependence.
18181 Ex: lw $3, 4($3)
18182 lw $2, 0($3)
18183 first_reg is $2, but the base is $3. */
18184 if (load_p
18185 && swap_p
18186 && REGNO (first_reg) + 1 == REGNO (base1))
18187 return false;
18189 if (offset2 != offset1 + 4)
18190 return false;
18192 if (!UMIPS_12BIT_OFFSET_P (offset1))
18193 return false;
18195 return true;
18198 /* OPERANDS describes the operands to a pair of SETs, in the order
18199 dest1, src1, dest2, src2. Return true if the operands can be used
18200 in an LWP or SWP instruction; LOAD_P says which. */
18202 bool
18203 umips_load_store_pair_p (bool load_p, rtx *operands)
18205 rtx reg1, reg2, mem1, mem2;
18207 if (load_p)
18209 reg1 = operands[0];
18210 reg2 = operands[2];
18211 mem1 = operands[1];
18212 mem2 = operands[3];
18214 else
18216 reg1 = operands[1];
18217 reg2 = operands[3];
18218 mem1 = operands[0];
18219 mem2 = operands[2];
18222 if (REGNO (reg2) == REGNO (reg1) + 1)
18223 return umips_load_store_pair_p_1 (load_p, false, reg1, mem1, mem2);
18225 if (REGNO (reg1) == REGNO (reg2) + 1)
18226 return umips_load_store_pair_p_1 (load_p, true, reg2, mem2, mem1);
18228 return false;
18231 /* Return the assembly instruction for a microMIPS LWP or SWP in which
18232 the first register is REG and the first memory slot is MEM.
18233 LOAD_P is true for LWP. */
18235 static void
18236 umips_output_load_store_pair_1 (bool load_p, rtx reg, rtx mem)
18238 rtx ops[] = {reg, mem};
18240 if (load_p)
18241 output_asm_insn ("lwp\t%0,%1", ops);
18242 else
18243 output_asm_insn ("swp\t%0,%1", ops);
18246 /* Output the assembly instruction for a microMIPS LWP or SWP instruction.
18247 LOAD_P and OPERANDS are as for umips_load_store_pair_p. */
18249 void
18250 umips_output_load_store_pair (bool load_p, rtx *operands)
18252 rtx reg1, reg2, mem1, mem2;
18253 if (load_p)
18255 reg1 = operands[0];
18256 reg2 = operands[2];
18257 mem1 = operands[1];
18258 mem2 = operands[3];
18260 else
18262 reg1 = operands[1];
18263 reg2 = operands[3];
18264 mem1 = operands[0];
18265 mem2 = operands[2];
18268 if (REGNO (reg2) == REGNO (reg1) + 1)
18270 umips_output_load_store_pair_1 (load_p, reg1, mem1);
18271 return;
18274 gcc_assert (REGNO (reg1) == REGNO (reg2) + 1);
18275 umips_output_load_store_pair_1 (load_p, reg2, mem2);
18278 /* Return true if REG1 and REG2 match the criteria for a movep insn. */
18280 bool
18281 umips_movep_target_p (rtx reg1, rtx reg2)
18283 int regno1, regno2, pair;
18284 unsigned int i;
18285 static const int match[8] = {
18286 0x00000060, /* 5, 6 */
18287 0x000000a0, /* 5, 7 */
18288 0x000000c0, /* 6, 7 */
18289 0x00200010, /* 4, 21 */
18290 0x00400010, /* 4, 22 */
18291 0x00000030, /* 4, 5 */
18292 0x00000050, /* 4, 6 */
18293 0x00000090 /* 4, 7 */
18296 if (!REG_P (reg1) || !REG_P (reg2))
18297 return false;
18299 regno1 = REGNO (reg1);
18300 regno2 = REGNO (reg2);
18302 if (!GP_REG_P (regno1) || !GP_REG_P (regno2))
18303 return false;
18305 pair = (1 << regno1) | (1 << regno2);
18307 for (i = 0; i < ARRAY_SIZE (match); i++)
18308 if (pair == match[i])
18309 return true;
18311 return false;
18314 /* Return the size in bytes of the trampoline code, padded to
18315 TRAMPOLINE_ALIGNMENT bits. The static chain pointer and target
18316 function address immediately follow. */
18319 mips_trampoline_code_size (void)
18321 if (TARGET_USE_PIC_FN_ADDR_REG)
18322 return 4 * 4;
18323 else if (ptr_mode == DImode)
18324 return 8 * 4;
18325 else if (ISA_HAS_LOAD_DELAY)
18326 return 6 * 4;
18327 else
18328 return 4 * 4;
18331 /* Implement TARGET_TRAMPOLINE_INIT. */
18333 static void
18334 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
18336 rtx addr, end_addr, high, low, opcode, mem;
18337 rtx trampoline[8];
18338 unsigned int i, j;
18339 HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
18341 /* Work out the offsets of the pointers from the start of the
18342 trampoline code. */
18343 end_addr_offset = mips_trampoline_code_size ();
18344 static_chain_offset = end_addr_offset;
18345 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
18347 /* Get pointers to the beginning and end of the code block. */
18348 addr = force_reg (Pmode, XEXP (m_tramp, 0));
18349 end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
18351 #define OP(X) gen_int_mode (X, SImode)
18353 /* Build up the code in TRAMPOLINE. */
18354 i = 0;
18355 if (TARGET_USE_PIC_FN_ADDR_REG)
18357 /* $25 contains the address of the trampoline. Emit code of the form:
18359 l[wd] $1, target_function_offset($25)
18360 l[wd] $static_chain, static_chain_offset($25)
18361 jr $1
18362 move $25,$1. */
18363 trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
18364 target_function_offset,
18365 PIC_FUNCTION_ADDR_REGNUM));
18366 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
18367 static_chain_offset,
18368 PIC_FUNCTION_ADDR_REGNUM));
18369 trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
18370 trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
18372 else if (ptr_mode == DImode)
18374 /* It's too cumbersome to create the full 64-bit address, so let's
18375 instead use:
18377 move $1, $31
18378 bal 1f
18380 1: l[wd] $25, target_function_offset - 12($31)
18381 l[wd] $static_chain, static_chain_offset - 12($31)
18382 jr $25
18383 move $31, $1
18385 where 12 is the offset of "1:" from the start of the code block. */
18386 trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
18387 trampoline[i++] = OP (MIPS_BAL (1));
18388 trampoline[i++] = OP (MIPS_NOP);
18389 trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
18390 target_function_offset - 12,
18391 RETURN_ADDR_REGNUM));
18392 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
18393 static_chain_offset - 12,
18394 RETURN_ADDR_REGNUM));
18395 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18396 trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
18398 else
18400 /* If the target has load delays, emit:
18402 lui $1, %hi(end_addr)
18403 lw $25, %lo(end_addr + ...)($1)
18404 lw $static_chain, %lo(end_addr + ...)($1)
18405 jr $25
18408 Otherwise emit:
18410 lui $1, %hi(end_addr)
18411 lw $25, %lo(end_addr + ...)($1)
18412 jr $25
18413 lw $static_chain, %lo(end_addr + ...)($1). */
18415 /* Split END_ADDR into %hi and %lo values. Trampolines are aligned
18416 to 64 bits, so the %lo value will have the bottom 3 bits clear. */
18417 high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
18418 NULL, false, OPTAB_WIDEN);
18419 high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
18420 NULL, false, OPTAB_WIDEN);
18421 low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
18423 /* Emit the LUI. */
18424 opcode = OP (MIPS_LUI (AT_REGNUM, 0));
18425 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
18426 NULL, false, OPTAB_WIDEN);
18428 /* Emit the load of the target function. */
18429 opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
18430 target_function_offset - end_addr_offset,
18431 AT_REGNUM));
18432 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
18433 NULL, false, OPTAB_WIDEN);
18435 /* Emit the JR here, if we can. */
18436 if (!ISA_HAS_LOAD_DELAY)
18437 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18439 /* Emit the load of the static chain register. */
18440 opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
18441 static_chain_offset - end_addr_offset,
18442 AT_REGNUM));
18443 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
18444 NULL, false, OPTAB_WIDEN);
18446 /* Emit the JR, if we couldn't above. */
18447 if (ISA_HAS_LOAD_DELAY)
18449 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18450 trampoline[i++] = OP (MIPS_NOP);
18454 #undef OP
18456 /* Copy the trampoline code. Leave any padding uninitialized. */
18457 for (j = 0; j < i; j++)
18459 mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
18460 mips_emit_move (mem, trampoline[j]);
18463 /* Set up the static chain pointer field. */
18464 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
18465 mips_emit_move (mem, chain_value);
18467 /* Set up the target function field. */
18468 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
18469 mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
18471 /* Flush the code part of the trampoline. */
18472 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
18473 emit_insn (gen_clear_cache (addr, end_addr));
18476 /* Implement FUNCTION_PROFILER. */
18478 void mips_function_profiler (FILE *file)
18480 if (TARGET_MIPS16)
18481 sorry ("mips16 function profiling");
18482 if (TARGET_LONG_CALLS)
18484 /* For TARGET_LONG_CALLS use $3 for the address of _mcount. */
18485 if (Pmode == DImode)
18486 fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
18487 else
18488 fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
18490 mips_push_asm_switch (&mips_noat);
18491 fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
18492 reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
18493 /* _mcount treats $2 as the static chain register. */
18494 if (cfun->static_chain_decl != NULL)
18495 fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
18496 reg_names[STATIC_CHAIN_REGNUM]);
18497 if (TARGET_MCOUNT_RA_ADDRESS)
18499 /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
18500 ra save location. */
18501 if (cfun->machine->frame.ra_fp_offset == 0)
18502 /* ra not saved, pass zero. */
18503 fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
18504 else
18505 fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
18506 Pmode == DImode ? "dla" : "la", reg_names[12],
18507 cfun->machine->frame.ra_fp_offset,
18508 reg_names[STACK_POINTER_REGNUM]);
18510 if (!TARGET_NEWABI)
18511 fprintf (file,
18512 "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from stack\n",
18513 TARGET_64BIT ? "dsubu" : "subu",
18514 reg_names[STACK_POINTER_REGNUM],
18515 reg_names[STACK_POINTER_REGNUM],
18516 Pmode == DImode ? 16 : 8);
18518 if (TARGET_LONG_CALLS)
18519 fprintf (file, "\tjalr\t%s\n", reg_names[3]);
18520 else
18521 fprintf (file, "\tjal\t_mcount\n");
18522 mips_pop_asm_switch (&mips_noat);
18523 /* _mcount treats $2 as the static chain register. */
18524 if (cfun->static_chain_decl != NULL)
18525 fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
18526 reg_names[2]);
18529 /* Implement TARGET_SHIFT_TRUNCATION_MASK. We want to keep the default
18530 behaviour of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
18531 when TARGET_LOONGSON_VECTORS is true. */
18533 static unsigned HOST_WIDE_INT
18534 mips_shift_truncation_mask (machine_mode mode)
18536 if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
18537 return 0;
18539 return GET_MODE_BITSIZE (mode) - 1;
18542 /* Implement TARGET_PREPARE_PCH_SAVE. */
18544 static void
18545 mips_prepare_pch_save (void)
18547 /* We are called in a context where the current MIPS16 vs. non-MIPS16
18548 setting should be irrelevant. The question then is: which setting
18549 makes most sense at load time?
18551 The PCH is loaded before the first token is read. We should never
18552 have switched into MIPS16 mode by that point, and thus should not
18553 have populated mips16_globals. Nor can we load the entire contents
18554 of mips16_globals from the PCH file, because mips16_globals contains
18555 a combination of GGC and non-GGC data.
18557 There is therefore no point in trying save the GGC part of
18558 mips16_globals to the PCH file, or to preserve MIPS16ness across
18559 the PCH save and load. The loading compiler would not have access
18560 to the non-GGC parts of mips16_globals (either from the PCH file,
18561 or from a copy that the loading compiler generated itself) and would
18562 have to call target_reinit anyway.
18564 It therefore seems best to switch back to non-MIPS16 mode at
18565 save time, and to ensure that mips16_globals remains null after
18566 a PCH load. */
18567 mips_set_compression_mode (0);
18568 mips16_globals = 0;
18571 /* Generate or test for an insn that supports a constant permutation. */
18573 #define MAX_VECT_LEN 8
18575 struct expand_vec_perm_d
18577 rtx target, op0, op1;
18578 unsigned char perm[MAX_VECT_LEN];
18579 machine_mode vmode;
18580 unsigned char nelt;
18581 bool one_vector_p;
18582 bool testing_p;
18585 /* Construct (set target (vec_select op0 (parallel perm))) and
18586 return true if that's a valid instruction in the active ISA. */
18588 static bool
18589 mips_expand_vselect (rtx target, rtx op0,
18590 const unsigned char *perm, unsigned nelt)
18592 rtx rperm[MAX_VECT_LEN], x;
18593 rtx_insn *insn;
18594 unsigned i;
18596 for (i = 0; i < nelt; ++i)
18597 rperm[i] = GEN_INT (perm[i]);
18599 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
18600 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
18601 x = gen_rtx_SET (VOIDmode, target, x);
18603 insn = emit_insn (x);
18604 if (recog_memoized (insn) < 0)
18606 remove_insn (insn);
18607 return false;
18609 return true;
18612 /* Similar, but generate a vec_concat from op0 and op1 as well. */
18614 static bool
18615 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
18616 const unsigned char *perm, unsigned nelt)
18618 machine_mode v2mode;
18619 rtx x;
18621 v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
18622 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
18623 return mips_expand_vselect (target, x, perm, nelt);
18626 /* Recognize patterns for even-odd extraction. */
18628 static bool
18629 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
18631 unsigned i, odd, nelt = d->nelt;
18632 rtx t0, t1, t2, t3;
18634 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18635 return false;
18636 /* Even-odd for V2SI/V2SFmode is matched by interleave directly. */
18637 if (nelt < 4)
18638 return false;
18640 odd = d->perm[0];
18641 if (odd > 1)
18642 return false;
18643 for (i = 1; i < nelt; ++i)
18644 if (d->perm[i] != i * 2 + odd)
18645 return false;
18647 if (d->testing_p)
18648 return true;
18650 /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
18651 t0 = gen_reg_rtx (d->vmode);
18652 t1 = gen_reg_rtx (d->vmode);
18653 switch (d->vmode)
18655 case V4HImode:
18656 emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
18657 emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
18658 if (odd)
18659 emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
18660 else
18661 emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
18662 break;
18664 case V8QImode:
18665 t2 = gen_reg_rtx (d->vmode);
18666 t3 = gen_reg_rtx (d->vmode);
18667 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
18668 emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
18669 emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
18670 emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
18671 if (odd)
18672 emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
18673 else
18674 emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
18675 break;
18677 default:
18678 gcc_unreachable ();
18680 return true;
18683 /* Recognize patterns for the Loongson PSHUFH instruction. */
18685 static bool
18686 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
18688 unsigned i, mask;
18689 rtx rmask;
18691 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18692 return false;
18693 if (d->vmode != V4HImode)
18694 return false;
18695 if (d->testing_p)
18696 return true;
18698 /* Convert the selector into the packed 8-bit form for pshufh. */
18699 /* Recall that loongson is little-endian only. No big-endian
18700 adjustment required. */
18701 for (i = mask = 0; i < 4; i++)
18702 mask |= (d->perm[i] & 3) << (i * 2);
18703 rmask = force_reg (SImode, GEN_INT (mask));
18705 if (d->one_vector_p)
18706 emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
18707 else
18709 rtx t0, t1, x, merge, rmerge[4];
18711 t0 = gen_reg_rtx (V4HImode);
18712 t1 = gen_reg_rtx (V4HImode);
18713 emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
18714 emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
18716 for (i = 0; i < 4; ++i)
18717 rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
18718 merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
18719 merge = force_reg (V4HImode, merge);
18721 x = gen_rtx_AND (V4HImode, merge, t1);
18722 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18724 x = gen_rtx_NOT (V4HImode, merge);
18725 x = gen_rtx_AND (V4HImode, x, t0);
18726 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18728 x = gen_rtx_IOR (V4HImode, t0, t1);
18729 emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
18732 return true;
18735 /* Recognize broadcast patterns for the Loongson. */
18737 static bool
18738 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
18740 unsigned i, elt;
18741 rtx t0, t1;
18743 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18744 return false;
18745 /* Note that we've already matched V2SI via punpck and V4HI via pshufh. */
18746 if (d->vmode != V8QImode)
18747 return false;
18748 if (!d->one_vector_p)
18749 return false;
18751 elt = d->perm[0];
18752 for (i = 1; i < 8; ++i)
18753 if (d->perm[i] != elt)
18754 return false;
18756 if (d->testing_p)
18757 return true;
18759 /* With one interleave we put two of the desired element adjacent. */
18760 t0 = gen_reg_rtx (V8QImode);
18761 if (elt < 4)
18762 emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
18763 else
18764 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
18766 /* Shuffle that one HImode element into all locations. */
18767 elt &= 3;
18768 elt *= 0x55;
18769 t1 = gen_reg_rtx (V4HImode);
18770 emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
18771 force_reg (SImode, GEN_INT (elt))));
18773 emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
18774 return true;
18777 static bool
18778 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
18780 unsigned int i, nelt = d->nelt;
18781 unsigned char perm2[MAX_VECT_LEN];
18783 if (d->one_vector_p)
18785 /* Try interleave with alternating operands. */
18786 memcpy (perm2, d->perm, sizeof(perm2));
18787 for (i = 1; i < nelt; i += 2)
18788 perm2[i] += nelt;
18789 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
18790 return true;
18792 else
18794 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
18795 d->perm, nelt))
18796 return true;
18798 /* Try again with swapped operands. */
18799 for (i = 0; i < nelt; ++i)
18800 perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
18801 if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
18802 return true;
18805 if (mips_expand_vpc_loongson_even_odd (d))
18806 return true;
18807 if (mips_expand_vpc_loongson_pshufh (d))
18808 return true;
18809 if (mips_expand_vpc_loongson_bcast (d))
18810 return true;
18811 return false;
18814 /* Expand a vec_perm_const pattern. */
18816 bool
18817 mips_expand_vec_perm_const (rtx operands[4])
18819 struct expand_vec_perm_d d;
18820 int i, nelt, which;
18821 unsigned char orig_perm[MAX_VECT_LEN];
18822 rtx sel;
18823 bool ok;
18825 d.target = operands[0];
18826 d.op0 = operands[1];
18827 d.op1 = operands[2];
18828 sel = operands[3];
18830 d.vmode = GET_MODE (d.target);
18831 gcc_assert (VECTOR_MODE_P (d.vmode));
18832 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18833 d.testing_p = false;
18835 for (i = which = 0; i < nelt; ++i)
18837 rtx e = XVECEXP (sel, 0, i);
18838 int ei = INTVAL (e) & (2 * nelt - 1);
18839 which |= (ei < nelt ? 1 : 2);
18840 orig_perm[i] = ei;
18842 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18844 switch (which)
18846 default:
18847 gcc_unreachable();
18849 case 3:
18850 d.one_vector_p = false;
18851 if (!rtx_equal_p (d.op0, d.op1))
18852 break;
18853 /* FALLTHRU */
18855 case 2:
18856 for (i = 0; i < nelt; ++i)
18857 d.perm[i] &= nelt - 1;
18858 d.op0 = d.op1;
18859 d.one_vector_p = true;
18860 break;
18862 case 1:
18863 d.op1 = d.op0;
18864 d.one_vector_p = true;
18865 break;
18868 ok = mips_expand_vec_perm_const_1 (&d);
18870 /* If we were given a two-vector permutation which just happened to
18871 have both input vectors equal, we folded this into a one-vector
18872 permutation. There are several loongson patterns that are matched
18873 via direct vec_select+vec_concat expansion, but we do not have
18874 support in mips_expand_vec_perm_const_1 to guess the adjustment
18875 that should be made for a single operand. Just try again with
18876 the original permutation. */
18877 if (!ok && which == 3)
18879 d.op0 = operands[1];
18880 d.op1 = operands[2];
18881 d.one_vector_p = false;
18882 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18883 ok = mips_expand_vec_perm_const_1 (&d);
18886 return ok;
18889 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK. */
18891 static bool
18892 mips_vectorize_vec_perm_const_ok (machine_mode vmode,
18893 const unsigned char *sel)
18895 struct expand_vec_perm_d d;
18896 unsigned int i, nelt, which;
18897 bool ret;
18899 d.vmode = vmode;
18900 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18901 d.testing_p = true;
18902 memcpy (d.perm, sel, nelt);
18904 /* Categorize the set of elements in the selector. */
18905 for (i = which = 0; i < nelt; ++i)
18907 unsigned char e = d.perm[i];
18908 gcc_assert (e < 2 * nelt);
18909 which |= (e < nelt ? 1 : 2);
18912 /* For all elements from second vector, fold the elements to first. */
18913 if (which == 2)
18914 for (i = 0; i < nelt; ++i)
18915 d.perm[i] -= nelt;
18917 /* Check whether the mask can be applied to the vector type. */
18918 d.one_vector_p = (which != 3);
18920 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
18921 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
18922 if (!d.one_vector_p)
18923 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
18925 start_sequence ();
18926 ret = mips_expand_vec_perm_const_1 (&d);
18927 end_sequence ();
18929 return ret;
18932 /* Expand an integral vector unpack operation. */
18934 void
18935 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18937 machine_mode imode = GET_MODE (operands[1]);
18938 rtx (*unpack) (rtx, rtx, rtx);
18939 rtx (*cmpgt) (rtx, rtx, rtx);
18940 rtx tmp, dest, zero;
18942 switch (imode)
18944 case V8QImode:
18945 if (high_p)
18946 unpack = gen_loongson_punpckhbh;
18947 else
18948 unpack = gen_loongson_punpcklbh;
18949 cmpgt = gen_loongson_pcmpgtb;
18950 break;
18951 case V4HImode:
18952 if (high_p)
18953 unpack = gen_loongson_punpckhhw;
18954 else
18955 unpack = gen_loongson_punpcklhw;
18956 cmpgt = gen_loongson_pcmpgth;
18957 break;
18958 default:
18959 gcc_unreachable ();
18962 zero = force_reg (imode, CONST0_RTX (imode));
18963 if (unsigned_p)
18964 tmp = zero;
18965 else
18967 tmp = gen_reg_rtx (imode);
18968 emit_insn (cmpgt (tmp, zero, operands[1]));
18971 dest = gen_reg_rtx (imode);
18972 emit_insn (unpack (dest, operands[1], tmp));
18974 emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
18977 /* A subroutine of mips_expand_vec_init, match constant vector elements. */
18979 static inline bool
18980 mips_constant_elt_p (rtx x)
18982 return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
18985 /* A subroutine of mips_expand_vec_init, expand via broadcast. */
18987 static void
18988 mips_expand_vi_broadcast (machine_mode vmode, rtx target, rtx elt)
18990 struct expand_vec_perm_d d;
18991 rtx t1;
18992 bool ok;
18994 if (elt != const0_rtx)
18995 elt = force_reg (GET_MODE_INNER (vmode), elt);
18996 if (REG_P (elt))
18997 elt = gen_lowpart (DImode, elt);
18999 t1 = gen_reg_rtx (vmode);
19000 switch (vmode)
19002 case V8QImode:
19003 emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
19004 break;
19005 case V4HImode:
19006 emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
19007 break;
19008 default:
19009 gcc_unreachable ();
19012 memset (&d, 0, sizeof (d));
19013 d.target = target;
19014 d.op0 = t1;
19015 d.op1 = t1;
19016 d.vmode = vmode;
19017 d.nelt = GET_MODE_NUNITS (vmode);
19018 d.one_vector_p = true;
19020 ok = mips_expand_vec_perm_const_1 (&d);
19021 gcc_assert (ok);
19024 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
19025 elements of VALS with zeros, copy the constant vector to TARGET. */
19027 static void
19028 mips_expand_vi_constant (machine_mode vmode, unsigned nelt,
19029 rtx target, rtx vals)
19031 rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
19032 unsigned i;
19034 for (i = 0; i < nelt; ++i)
19036 if (!mips_constant_elt_p (RTVEC_ELT (vec, i)))
19037 RTVEC_ELT (vec, i) = const0_rtx;
19040 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
19044 /* A subroutine of mips_expand_vec_init, expand via pinsrh. */
19046 static void
19047 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
19049 mips_expand_vi_constant (V4HImode, 4, target, vals);
19051 emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
19052 GEN_INT (one_var)));
19055 /* A subroutine of mips_expand_vec_init, expand anything via memory. */
19057 static void
19058 mips_expand_vi_general (machine_mode vmode, machine_mode imode,
19059 unsigned nelt, unsigned nvar, rtx target, rtx vals)
19061 rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
19062 unsigned int i, isize = GET_MODE_SIZE (imode);
19064 if (nvar < nelt)
19065 mips_expand_vi_constant (vmode, nelt, mem, vals);
19067 for (i = 0; i < nelt; ++i)
19069 rtx x = XVECEXP (vals, 0, i);
19070 if (!mips_constant_elt_p (x))
19071 emit_move_insn (adjust_address (mem, imode, i * isize), x);
19074 emit_move_insn (target, mem);
19077 /* Expand a vector initialization. */
19079 void
19080 mips_expand_vector_init (rtx target, rtx vals)
19082 machine_mode vmode = GET_MODE (target);
19083 machine_mode imode = GET_MODE_INNER (vmode);
19084 unsigned i, nelt = GET_MODE_NUNITS (vmode);
19085 unsigned nvar = 0, one_var = -1u;
19086 bool all_same = true;
19087 rtx x;
19089 for (i = 0; i < nelt; ++i)
19091 x = XVECEXP (vals, 0, i);
19092 if (!mips_constant_elt_p (x))
19093 nvar++, one_var = i;
19094 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
19095 all_same = false;
19098 /* Load constants from the pool, or whatever's handy. */
19099 if (nvar == 0)
19101 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
19102 return;
19105 /* For two-part initialization, always use CONCAT. */
19106 if (nelt == 2)
19108 rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
19109 rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
19110 x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
19111 emit_insn (gen_rtx_SET (VOIDmode, target, x));
19112 return;
19115 /* Loongson is the only cpu with vectors with more elements. */
19116 gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
19118 /* If all values are identical, broadcast the value. */
19119 if (all_same)
19121 mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
19122 return;
19125 /* If we've only got one non-variable V4HImode, use PINSRH. */
19126 if (nvar == 1 && vmode == V4HImode)
19128 mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
19129 return;
19132 mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
19135 /* Expand a vector reduction. */
19137 void
19138 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
19140 machine_mode vmode = GET_MODE (in);
19141 unsigned char perm2[2];
19142 rtx last, next, fold, x;
19143 bool ok;
19145 last = in;
19146 fold = gen_reg_rtx (vmode);
19147 switch (vmode)
19149 case V2SFmode:
19150 /* Use PUL/PLU to produce { L, H } op { H, L }.
19151 By reversing the pair order, rather than a pure interleave high,
19152 we avoid erroneous exceptional conditions that we might otherwise
19153 produce from the computation of H op H. */
19154 perm2[0] = 1;
19155 perm2[1] = 2;
19156 ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
19157 gcc_assert (ok);
19158 break;
19160 case V2SImode:
19161 /* Use interleave to produce { H, L } op { H, H }. */
19162 emit_insn (gen_loongson_punpckhwd (fold, last, last));
19163 break;
19165 case V4HImode:
19166 /* Perform the first reduction with interleave,
19167 and subsequent reductions with shifts. */
19168 emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
19170 next = gen_reg_rtx (vmode);
19171 emit_insn (gen (next, last, fold));
19172 last = next;
19174 fold = gen_reg_rtx (vmode);
19175 x = force_reg (SImode, GEN_INT (16));
19176 emit_insn (gen_vec_shr_v4hi (fold, last, x));
19177 break;
19179 case V8QImode:
19180 emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
19182 next = gen_reg_rtx (vmode);
19183 emit_insn (gen (next, last, fold));
19184 last = next;
19186 fold = gen_reg_rtx (vmode);
19187 x = force_reg (SImode, GEN_INT (16));
19188 emit_insn (gen_vec_shr_v8qi (fold, last, x));
19190 next = gen_reg_rtx (vmode);
19191 emit_insn (gen (next, last, fold));
19192 last = next;
19194 fold = gen_reg_rtx (vmode);
19195 x = force_reg (SImode, GEN_INT (8));
19196 emit_insn (gen_vec_shr_v8qi (fold, last, x));
19197 break;
19199 default:
19200 gcc_unreachable ();
19203 emit_insn (gen (target, last, fold));
19206 /* Expand a vector minimum/maximum. */
19208 void
19209 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
19210 rtx (*cmp) (rtx, rtx, rtx), bool min_p)
19212 machine_mode vmode = GET_MODE (target);
19213 rtx tc, t0, t1, x;
19215 tc = gen_reg_rtx (vmode);
19216 t0 = gen_reg_rtx (vmode);
19217 t1 = gen_reg_rtx (vmode);
19219 /* op0 > op1 */
19220 emit_insn (cmp (tc, op0, op1));
19222 x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
19223 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
19225 x = gen_rtx_NOT (vmode, tc);
19226 x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
19227 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
19229 x = gen_rtx_IOR (vmode, t0, t1);
19230 emit_insn (gen_rtx_SET (VOIDmode, target, x));
19233 /* Implement HARD_REGNO_CALLER_SAVE_MODE. */
19235 machine_mode
19236 mips_hard_regno_caller_save_mode (unsigned int regno,
19237 unsigned int nregs,
19238 machine_mode mode)
19240 /* For performance, avoid saving/restoring upper parts of a register
19241 by returning MODE as save mode when the mode is known. */
19242 if (mode == VOIDmode)
19243 return choose_hard_reg_mode (regno, nregs, false);
19244 else
19245 return mode;
19248 /* Implement TARGET_CASE_VALUES_THRESHOLD. */
19250 unsigned int
19251 mips_case_values_threshold (void)
19253 /* In MIPS16 mode using a larger case threshold generates smaller code. */
19254 if (TARGET_MIPS16 && optimize_size)
19255 return 10;
19256 else
19257 return default_case_values_threshold ();
19260 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
19262 static void
19263 mips_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
19265 if (!TARGET_HARD_FLOAT_ABI)
19266 return;
19267 tree exceptions_var = create_tmp_var (MIPS_ATYPE_USI);
19268 tree fcsr_orig_var = create_tmp_var (MIPS_ATYPE_USI);
19269 tree fcsr_mod_var = create_tmp_var (MIPS_ATYPE_USI);
19270 tree get_fcsr = mips_builtin_decls[MIPS_GET_FCSR];
19271 tree set_fcsr = mips_builtin_decls[MIPS_SET_FCSR];
19272 tree get_fcsr_hold_call = build_call_expr (get_fcsr, 0);
19273 tree hold_assign_orig = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
19274 fcsr_orig_var, get_fcsr_hold_call);
19275 tree hold_mod_val = build2 (BIT_AND_EXPR, MIPS_ATYPE_USI, fcsr_orig_var,
19276 build_int_cst (MIPS_ATYPE_USI, 0xfffff003));
19277 tree hold_assign_mod = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
19278 fcsr_mod_var, hold_mod_val);
19279 tree set_fcsr_hold_call = build_call_expr (set_fcsr, 1, fcsr_mod_var);
19280 tree hold_all = build2 (COMPOUND_EXPR, MIPS_ATYPE_USI,
19281 hold_assign_orig, hold_assign_mod);
19282 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_all,
19283 set_fcsr_hold_call);
19285 *clear = build_call_expr (set_fcsr, 1, fcsr_mod_var);
19287 tree get_fcsr_update_call = build_call_expr (get_fcsr, 0);
19288 *update = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
19289 exceptions_var, get_fcsr_update_call);
19290 tree set_fcsr_update_call = build_call_expr (set_fcsr, 1, fcsr_orig_var);
19291 *update = build2 (COMPOUND_EXPR, void_type_node, *update,
19292 set_fcsr_update_call);
19293 tree atomic_feraiseexcept
19294 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
19295 tree int_exceptions_var = fold_convert (integer_type_node,
19296 exceptions_var);
19297 tree atomic_feraiseexcept_call = build_call_expr (atomic_feraiseexcept,
19298 1, int_exceptions_var);
19299 *update = build2 (COMPOUND_EXPR, void_type_node, *update,
19300 atomic_feraiseexcept_call);
19303 /* Implement TARGET_SPILL_CLASS. */
19305 static reg_class_t
19306 mips_spill_class (reg_class_t rclass ATTRIBUTE_UNUSED,
19307 machine_mode mode ATTRIBUTE_UNUSED)
19309 if (TARGET_MIPS16)
19310 return SPILL_REGS;
19311 return NO_REGS;
19314 /* Implement TARGET_LRA_P. */
19316 static bool
19317 mips_lra_p (void)
19319 return mips_lra_flag;
19322 /* Initialize the GCC target structure. */
19323 #undef TARGET_ASM_ALIGNED_HI_OP
19324 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
19325 #undef TARGET_ASM_ALIGNED_SI_OP
19326 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
19327 #undef TARGET_ASM_ALIGNED_DI_OP
19328 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
19330 #undef TARGET_OPTION_OVERRIDE
19331 #define TARGET_OPTION_OVERRIDE mips_option_override
19333 #undef TARGET_LEGITIMIZE_ADDRESS
19334 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
19336 #undef TARGET_ASM_FUNCTION_PROLOGUE
19337 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
19338 #undef TARGET_ASM_FUNCTION_EPILOGUE
19339 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
19340 #undef TARGET_ASM_SELECT_RTX_SECTION
19341 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
19342 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
19343 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
19345 #undef TARGET_SCHED_INIT
19346 #define TARGET_SCHED_INIT mips_sched_init
19347 #undef TARGET_SCHED_REORDER
19348 #define TARGET_SCHED_REORDER mips_sched_reorder
19349 #undef TARGET_SCHED_REORDER2
19350 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
19351 #undef TARGET_SCHED_VARIABLE_ISSUE
19352 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
19353 #undef TARGET_SCHED_ADJUST_COST
19354 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
19355 #undef TARGET_SCHED_ISSUE_RATE
19356 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
19357 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
19358 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
19359 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
19360 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
19361 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
19362 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
19363 mips_multipass_dfa_lookahead
19364 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
19365 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
19366 mips_small_register_classes_for_mode_p
19368 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
19369 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
19371 #undef TARGET_INSERT_ATTRIBUTES
19372 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
19373 #undef TARGET_MERGE_DECL_ATTRIBUTES
19374 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
19375 #undef TARGET_CAN_INLINE_P
19376 #define TARGET_CAN_INLINE_P mips_can_inline_p
19377 #undef TARGET_SET_CURRENT_FUNCTION
19378 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
19380 #undef TARGET_VALID_POINTER_MODE
19381 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
19382 #undef TARGET_REGISTER_MOVE_COST
19383 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
19384 #undef TARGET_REGISTER_PRIORITY
19385 #define TARGET_REGISTER_PRIORITY mips_register_priority
19386 #undef TARGET_MEMORY_MOVE_COST
19387 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
19388 #undef TARGET_RTX_COSTS
19389 #define TARGET_RTX_COSTS mips_rtx_costs
19390 #undef TARGET_ADDRESS_COST
19391 #define TARGET_ADDRESS_COST mips_address_cost
19393 #undef TARGET_IN_SMALL_DATA_P
19394 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
19396 #undef TARGET_MACHINE_DEPENDENT_REORG
19397 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
19399 #undef TARGET_PREFERRED_RELOAD_CLASS
19400 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
19402 #undef TARGET_EXPAND_TO_RTL_HOOK
19403 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
19404 #undef TARGET_ASM_FILE_START
19405 #define TARGET_ASM_FILE_START mips_file_start
19406 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
19407 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
19408 #undef TARGET_ASM_CODE_END
19409 #define TARGET_ASM_CODE_END mips_code_end
19411 #undef TARGET_INIT_LIBFUNCS
19412 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
19414 #undef TARGET_BUILD_BUILTIN_VA_LIST
19415 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
19416 #undef TARGET_EXPAND_BUILTIN_VA_START
19417 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
19418 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
19419 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
19421 #undef TARGET_PROMOTE_FUNCTION_MODE
19422 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
19423 #undef TARGET_PROMOTE_PROTOTYPES
19424 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
19426 #undef TARGET_FUNCTION_VALUE
19427 #define TARGET_FUNCTION_VALUE mips_function_value
19428 #undef TARGET_LIBCALL_VALUE
19429 #define TARGET_LIBCALL_VALUE mips_libcall_value
19430 #undef TARGET_FUNCTION_VALUE_REGNO_P
19431 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
19432 #undef TARGET_RETURN_IN_MEMORY
19433 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
19434 #undef TARGET_RETURN_IN_MSB
19435 #define TARGET_RETURN_IN_MSB mips_return_in_msb
19437 #undef TARGET_ASM_OUTPUT_MI_THUNK
19438 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
19439 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
19440 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
19442 #undef TARGET_PRINT_OPERAND
19443 #define TARGET_PRINT_OPERAND mips_print_operand
19444 #undef TARGET_PRINT_OPERAND_ADDRESS
19445 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
19446 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
19447 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
19449 #undef TARGET_SETUP_INCOMING_VARARGS
19450 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
19451 #undef TARGET_STRICT_ARGUMENT_NAMING
19452 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
19453 #undef TARGET_MUST_PASS_IN_STACK
19454 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
19455 #undef TARGET_PASS_BY_REFERENCE
19456 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
19457 #undef TARGET_CALLEE_COPIES
19458 #define TARGET_CALLEE_COPIES mips_callee_copies
19459 #undef TARGET_ARG_PARTIAL_BYTES
19460 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
19461 #undef TARGET_FUNCTION_ARG
19462 #define TARGET_FUNCTION_ARG mips_function_arg
19463 #undef TARGET_FUNCTION_ARG_ADVANCE
19464 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
19465 #undef TARGET_FUNCTION_ARG_BOUNDARY
19466 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
19467 #undef TARGET_GET_RAW_RESULT_MODE
19468 #define TARGET_GET_RAW_RESULT_MODE mips_get_reg_raw_mode
19469 #undef TARGET_GET_RAW_ARG_MODE
19470 #define TARGET_GET_RAW_ARG_MODE mips_get_reg_raw_mode
19472 #undef TARGET_MODE_REP_EXTENDED
19473 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
19475 #undef TARGET_VECTOR_MODE_SUPPORTED_P
19476 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
19478 #undef TARGET_SCALAR_MODE_SUPPORTED_P
19479 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
19481 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
19482 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
19484 #undef TARGET_INIT_BUILTINS
19485 #define TARGET_INIT_BUILTINS mips_init_builtins
19486 #undef TARGET_BUILTIN_DECL
19487 #define TARGET_BUILTIN_DECL mips_builtin_decl
19488 #undef TARGET_EXPAND_BUILTIN
19489 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
19491 #undef TARGET_HAVE_TLS
19492 #define TARGET_HAVE_TLS HAVE_AS_TLS
19494 #undef TARGET_CANNOT_FORCE_CONST_MEM
19495 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
19497 #undef TARGET_LEGITIMATE_CONSTANT_P
19498 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
19500 #undef TARGET_ENCODE_SECTION_INFO
19501 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
19503 #undef TARGET_ATTRIBUTE_TABLE
19504 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
19505 /* All our function attributes are related to how out-of-line copies should
19506 be compiled or called. They don't in themselves prevent inlining. */
19507 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
19508 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
19510 #undef TARGET_EXTRA_LIVE_ON_ENTRY
19511 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
19513 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
19514 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
19515 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
19516 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
19518 #undef TARGET_COMP_TYPE_ATTRIBUTES
19519 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
19521 #ifdef HAVE_AS_DTPRELWORD
19522 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
19523 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
19524 #endif
19525 #undef TARGET_DWARF_REGISTER_SPAN
19526 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
19527 #undef TARGET_DWARF_FRAME_REG_MODE
19528 #define TARGET_DWARF_FRAME_REG_MODE mips_dwarf_frame_reg_mode
19530 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
19531 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
19533 #undef TARGET_LEGITIMATE_ADDRESS_P
19534 #define TARGET_LEGITIMATE_ADDRESS_P mips_legitimate_address_p
19536 #undef TARGET_FRAME_POINTER_REQUIRED
19537 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
19539 #undef TARGET_CAN_ELIMINATE
19540 #define TARGET_CAN_ELIMINATE mips_can_eliminate
19542 #undef TARGET_CONDITIONAL_REGISTER_USAGE
19543 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
19545 #undef TARGET_TRAMPOLINE_INIT
19546 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
19548 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
19549 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
19551 #undef TARGET_SHIFT_TRUNCATION_MASK
19552 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
19554 #undef TARGET_PREPARE_PCH_SAVE
19555 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
19557 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
19558 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK mips_vectorize_vec_perm_const_ok
19560 #undef TARGET_CASE_VALUES_THRESHOLD
19561 #define TARGET_CASE_VALUES_THRESHOLD mips_case_values_threshold
19563 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
19564 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV mips_atomic_assign_expand_fenv
19566 #undef TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS
19567 #define TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS true
19569 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
19570 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
19571 mips_use_by_pieces_infrastructure_p
19573 #undef TARGET_SPILL_CLASS
19574 #define TARGET_SPILL_CLASS mips_spill_class
19575 #undef TARGET_LRA_P
19576 #define TARGET_LRA_P mips_lra_p
19578 struct gcc_target targetm = TARGET_INITIALIZER;
19580 #include "gt-mips.h"