1 /* Subroutines used for MIPS code generation.
2 Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Contributed by A. Lichnewsky, lich@inria.inria.fr.
6 Changes by Michael Meissner, meissner@osf.org.
7 64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
8 Brendan Eich, brendan@microunity.com.
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
17 GCC is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
28 #include "coretypes.h"
33 #include "hard-reg-set.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "insn-attr.h"
53 #include "target-def.h"
54 #include "integrate.h"
55 #include "langhooks.h"
56 #include "cfglayout.h"
57 #include "sched-int.h"
58 #include "tree-gimple.h"
60 #include "diagnostic.h"
62 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
63 #define UNSPEC_ADDRESS_P(X) \
64 (GET_CODE (X) == UNSPEC \
65 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
66 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
68 /* Extract the symbol or label from UNSPEC wrapper X. */
69 #define UNSPEC_ADDRESS(X) \
72 /* Extract the symbol type from UNSPEC wrapper X. */
73 #define UNSPEC_ADDRESS_TYPE(X) \
74 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
76 /* The maximum distance between the top of the stack frame and the
77 value $sp has when we save and restore registers.
79 The value for normal-mode code must be a SMALL_OPERAND and must
80 preserve the maximum stack alignment. We therefore use a value
81 of 0x7ff0 in this case.
83 MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
84 up to 0x7f8 bytes and can usually save or restore all the registers
85 that we need to save or restore. (Note that we can only use these
86 instructions for o32, for which the stack alignment is 8 bytes.)
88 We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
89 RESTORE are not available. We can then use unextended instructions
90 to save and restore registers, and to allocate and deallocate the top
92 #define MIPS_MAX_FIRST_STACK_STEP \
93 (!TARGET_MIPS16 ? 0x7ff0 \
94 : GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8 \
95 : TARGET_64BIT ? 0x100 : 0x400)
97 /* True if INSN is a mips.md pattern or asm statement. */
98 #define USEFUL_INSN_P(INSN) \
100 && GET_CODE (PATTERN (INSN)) != USE \
101 && GET_CODE (PATTERN (INSN)) != CLOBBER \
102 && GET_CODE (PATTERN (INSN)) != ADDR_VEC \
103 && GET_CODE (PATTERN (INSN)) != ADDR_DIFF_VEC)
105 /* If INSN is a delayed branch sequence, return the first instruction
106 in the sequence, otherwise return INSN itself. */
107 #define SEQ_BEGIN(INSN) \
108 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
109 ? XVECEXP (PATTERN (INSN), 0, 0) \
112 /* Likewise for the last instruction in a delayed branch sequence. */
113 #define SEQ_END(INSN) \
114 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
115 ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1) \
118 /* Execute the following loop body with SUBINSN set to each instruction
119 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
120 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
121 for ((SUBINSN) = SEQ_BEGIN (INSN); \
122 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
123 (SUBINSN) = NEXT_INSN (SUBINSN))
125 /* True if bit BIT is set in VALUE. */
126 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
128 /* Classifies an address.
131 A natural register + offset address. The register satisfies
132 mips_valid_base_register_p and the offset is a const_arith_operand.
135 A LO_SUM rtx. The first operand is a valid base register and
136 the second operand is a symbolic address.
139 A signed 16-bit constant address.
142 A constant symbolic address. */
143 enum mips_address_type {
150 /* Macros to create an enumeration identifier for a function prototype. */
151 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
152 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
153 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
154 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
156 /* Classifies the prototype of a built-in function. */
157 enum mips_function_type {
158 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
159 #include "config/mips/mips-ftypes.def"
160 #undef DEF_MIPS_FTYPE
164 /* Specifies how a built-in function should be converted into rtl. */
165 enum mips_builtin_type {
166 /* The function corresponds directly to an .md pattern. The return
167 value is mapped to operand 0 and the arguments are mapped to
168 operands 1 and above. */
171 /* The function corresponds directly to an .md pattern. There is no return
172 value and the arguments are mapped to operands 0 and above. */
173 MIPS_BUILTIN_DIRECT_NO_TARGET,
175 /* The function corresponds to a comparison instruction followed by
176 a mips_cond_move_tf_ps pattern. The first two arguments are the
177 values to compare and the second two arguments are the vector
178 operands for the movt.ps or movf.ps instruction (in assembly order). */
182 /* The function corresponds to a V2SF comparison instruction. Operand 0
183 of this instruction is the result of the comparison, which has mode
184 CCV2 or CCV4. The function arguments are mapped to operands 1 and
185 above. The function's return value is an SImode boolean that is
186 true under the following conditions:
188 MIPS_BUILTIN_CMP_ANY: one of the registers is true
189 MIPS_BUILTIN_CMP_ALL: all of the registers are true
190 MIPS_BUILTIN_CMP_LOWER: the first register is true
191 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
192 MIPS_BUILTIN_CMP_ANY,
193 MIPS_BUILTIN_CMP_ALL,
194 MIPS_BUILTIN_CMP_UPPER,
195 MIPS_BUILTIN_CMP_LOWER,
197 /* As above, but the instruction only sets a single $fcc register. */
198 MIPS_BUILTIN_CMP_SINGLE,
200 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
201 MIPS_BUILTIN_BPOSGE32
204 /* Invoke MACRO (COND) for each C.cond.fmt condition. */
205 #define MIPS_FP_CONDITIONS(MACRO) \
223 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
224 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
225 enum mips_fp_condition {
226 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
229 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
230 #define STRINGIFY(X) #X
231 static const char *const mips_fp_conditions[] = {
232 MIPS_FP_CONDITIONS (STRINGIFY)
235 /* Information about a function's frame layout. */
236 struct mips_frame_info GTY(()) {
237 /* The size of the frame in bytes. */
238 HOST_WIDE_INT total_size;
240 /* The number of bytes allocated to variables. */
241 HOST_WIDE_INT var_size;
243 /* The number of bytes allocated to outgoing function arguments. */
244 HOST_WIDE_INT args_size;
246 /* The number of bytes allocated to the .cprestore slot, or 0 if there
248 HOST_WIDE_INT cprestore_size;
250 /* Bit X is set if the function saves or restores GPR X. */
253 /* Likewise FPR X. */
256 /* The number of GPRs and FPRs saved. */
260 /* The offset of the topmost GPR and FPR save slots from the top of
261 the frame, or zero if no such slots are needed. */
262 HOST_WIDE_INT gp_save_offset;
263 HOST_WIDE_INT fp_save_offset;
265 /* Likewise, but giving offsets from the bottom of the frame. */
266 HOST_WIDE_INT gp_sp_offset;
267 HOST_WIDE_INT fp_sp_offset;
269 /* The offset of arg_pointer_rtx from frame_pointer_rtx. */
270 HOST_WIDE_INT arg_pointer_offset;
272 /* The offset of hard_frame_pointer_rtx from frame_pointer_rtx. */
273 HOST_WIDE_INT hard_frame_pointer_offset;
276 struct machine_function GTY(()) {
277 /* The register returned by mips16_gp_pseudo_reg; see there for details. */
278 rtx mips16_gp_pseudo_rtx;
280 /* The number of extra stack bytes taken up by register varargs.
281 This area is allocated by the callee at the very top of the frame. */
284 /* The current frame information, calculated by mips_compute_frame_info. */
285 struct mips_frame_info frame;
287 /* The register to use as the function's global pointer. */
288 unsigned int global_pointer;
290 /* True if mips_adjust_insn_length should ignore an instruction's
292 bool ignore_hazard_length_p;
294 /* True if the whole function is suitable for .set noreorder and
296 bool all_noreorder_p;
298 /* True if the function is known to have an instruction that needs $gp. */
301 /* True if we have emitted an instruction to initialize
302 mips16_gp_pseudo_rtx. */
303 bool initialized_mips16_gp_pseudo_p;
306 /* Information about a single argument. */
307 struct mips_arg_info {
308 /* True if the argument is passed in a floating-point register, or
309 would have been if we hadn't run out of registers. */
312 /* The number of words passed in registers, rounded up. */
313 unsigned int reg_words;
315 /* For EABI, the offset of the first register from GP_ARG_FIRST or
316 FP_ARG_FIRST. For other ABIs, the offset of the first register from
317 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
318 comment for details).
320 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
322 unsigned int reg_offset;
324 /* The number of words that must be passed on the stack, rounded up. */
325 unsigned int stack_words;
327 /* The offset from the start of the stack overflow area of the argument's
328 first stack word. Only meaningful when STACK_WORDS is nonzero. */
329 unsigned int stack_offset;
332 /* Information about an address described by mips_address_type.
338 REG is the base register and OFFSET is the constant offset.
341 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
342 is the type of symbol it references.
345 SYMBOL_TYPE is the type of symbol that the address references. */
346 struct mips_address_info {
347 enum mips_address_type type;
350 enum mips_symbol_type symbol_type;
353 /* One stage in a constant building sequence. These sequences have
357 A = A CODE[1] VALUE[1]
358 A = A CODE[2] VALUE[2]
361 where A is an accumulator, each CODE[i] is a binary rtl operation
362 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
363 struct mips_integer_op {
365 unsigned HOST_WIDE_INT value;
368 /* The largest number of operations needed to load an integer constant.
369 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
370 When the lowest bit is clear, we can try, but reject a sequence with
371 an extra SLL at the end. */
372 #define MIPS_MAX_INTEGER_OPS 7
374 /* Information about a MIPS16e SAVE or RESTORE instruction. */
375 struct mips16e_save_restore_info {
376 /* The number of argument registers saved by a SAVE instruction.
377 0 for RESTORE instructions. */
380 /* Bit X is set if the instruction saves or restores GPR X. */
383 /* The total number of bytes to allocate. */
387 /* Global variables for machine-dependent things. */
389 /* The -G setting, or the configuration's default small-data limit if
390 no -G option is given. */
391 static unsigned int mips_small_data_threshold;
393 /* The number of file directives written by mips_output_filename. */
394 int num_source_filenames;
396 /* The name that appeared in the last .file directive written by
397 mips_output_filename, or "" if mips_output_filename hasn't
398 written anything yet. */
399 const char *current_function_file = "";
401 /* A label counter used by PUT_SDB_BLOCK_START and PUT_SDB_BLOCK_END. */
404 /* Arrays that map GCC register numbers to debugger register numbers. */
405 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
406 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
408 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs. */
413 /* True if we're writing out a branch-likely instruction rather than a
415 static bool mips_branch_likely;
417 /* The operands passed to the last cmpMM expander. */
420 /* The current instruction-set architecture. */
421 enum processor_type mips_arch;
422 const struct mips_cpu_info *mips_arch_info;
424 /* The processor that we should tune the code for. */
425 enum processor_type mips_tune;
426 const struct mips_cpu_info *mips_tune_info;
428 /* The ISA level associated with mips_arch. */
431 /* The architecture selected by -mipsN, or null if -mipsN wasn't used. */
432 static const struct mips_cpu_info *mips_isa_option_info;
434 /* Which ABI to use. */
435 int mips_abi = MIPS_ABI_DEFAULT;
437 /* Which cost information to use. */
438 const struct mips_rtx_cost_data *mips_cost;
440 /* The ambient target flags, excluding MASK_MIPS16. */
441 static int mips_base_target_flags;
443 /* True if MIPS16 is the default mode. */
444 static bool mips_base_mips16;
446 /* The ambient values of other global variables. */
447 static int mips_base_delayed_branch; /* flag_delayed_branch */
448 static int mips_base_schedule_insns; /* flag_schedule_insns */
449 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
450 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
451 static int mips_base_align_loops; /* align_loops */
452 static int mips_base_align_jumps; /* align_jumps */
453 static int mips_base_align_functions; /* align_functions */
455 /* The -mcode-readable setting. */
456 enum mips_code_readable_setting mips_code_readable = CODE_READABLE_YES;
458 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
459 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
461 /* Index C is true if character C is a valid PRINT_OPERAND punctation
463 bool mips_print_operand_punct[256];
465 static GTY (()) int mips_output_filename_first_time = 1;
467 /* mips_split_p[X] is true if symbols of type X can be split by
468 mips_split_symbol. */
469 bool mips_split_p[NUM_SYMBOL_TYPES];
471 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
472 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
473 if they are matched by a special .md file pattern. */
474 static const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
476 /* Likewise for HIGHs. */
477 static const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
479 /* Index R is the smallest register class that contains register R. */
480 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
481 LEA_REGS, LEA_REGS, M16_NA_REGS, V1_REG,
482 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
483 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
484 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
485 M16_NA_REGS, M16_NA_REGS, LEA_REGS, LEA_REGS,
486 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
487 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
488 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
489 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
490 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
491 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
492 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
493 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
494 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
495 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
496 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
497 MD0_REG, MD1_REG, NO_REGS, ST_REGS,
498 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
499 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
500 NO_REGS, ALL_REGS, ALL_REGS, NO_REGS,
501 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
502 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
503 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
504 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
505 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
506 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
507 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
508 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
509 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
510 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
511 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
512 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
513 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
514 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
515 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
516 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
517 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
518 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
519 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
520 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
521 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
522 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
523 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
524 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
525 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
526 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
527 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
530 /* The value of TARGET_ATTRIBUTE_TABLE. */
531 const struct attribute_spec mips_attribute_table[] = {
532 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
533 { "long_call", 0, 0, false, true, true, NULL },
534 { "far", 0, 0, false, true, true, NULL },
535 { "near", 0, 0, false, true, true, NULL },
536 /* We would really like to treat "mips16" and "nomips16" as type
537 attributes, but GCC doesn't provide the hooks we need to support
538 the right conversion rules. As declaration attributes, they affect
539 code generation but don't carry other semantics. */
540 { "mips16", 0, 0, true, false, false, NULL },
541 { "nomips16", 0, 0, true, false, false, NULL },
542 { NULL, 0, 0, false, false, false, NULL }
545 /* A table describing all the processors GCC knows about. Names are
546 matched in the order listed. The first mention of an ISA level is
547 taken as the canonical name for that ISA.
549 To ease comparison, please keep this table in the same order
550 as GAS's mips_cpu_info_table. Please also make sure that
551 MIPS_ISA_LEVEL_SPEC and MIPS_ARCH_FLOAT_SPEC handle all -march
552 options correctly. */
553 static const struct mips_cpu_info mips_cpu_info_table[] = {
554 /* Entries for generic ISAs. */
555 { "mips1", PROCESSOR_R3000, 1, 0 },
556 { "mips2", PROCESSOR_R6000, 2, 0 },
557 { "mips3", PROCESSOR_R4000, 3, 0 },
558 { "mips4", PROCESSOR_R8000, 4, 0 },
559 /* Prefer not to use branch-likely instructions for generic MIPS32rX
560 and MIPS64rX code. The instructions were officially deprecated
561 in revisions 2 and earlier, but revision 3 is likely to downgrade
562 that to a recommendation to avoid the instructions in code that
563 isn't tuned to a specific processor. */
564 { "mips32", PROCESSOR_4KC, 32, PTF_AVOID_BRANCHLIKELY },
565 { "mips32r2", PROCESSOR_M4K, 33, PTF_AVOID_BRANCHLIKELY },
566 { "mips64", PROCESSOR_5KC, 64, PTF_AVOID_BRANCHLIKELY },
568 /* MIPS I processors. */
569 { "r3000", PROCESSOR_R3000, 1, 0 },
570 { "r2000", PROCESSOR_R3000, 1, 0 },
571 { "r3900", PROCESSOR_R3900, 1, 0 },
573 /* MIPS II processors. */
574 { "r6000", PROCESSOR_R6000, 2, 0 },
576 /* MIPS III processors. */
577 { "r4000", PROCESSOR_R4000, 3, 0 },
578 { "vr4100", PROCESSOR_R4100, 3, 0 },
579 { "vr4111", PROCESSOR_R4111, 3, 0 },
580 { "vr4120", PROCESSOR_R4120, 3, 0 },
581 { "vr4130", PROCESSOR_R4130, 3, 0 },
582 { "vr4300", PROCESSOR_R4300, 3, 0 },
583 { "r4400", PROCESSOR_R4000, 3, 0 },
584 { "r4600", PROCESSOR_R4600, 3, 0 },
585 { "orion", PROCESSOR_R4600, 3, 0 },
586 { "r4650", PROCESSOR_R4650, 3, 0 },
588 /* MIPS IV processors. */
589 { "r8000", PROCESSOR_R8000, 4, 0 },
590 { "vr5000", PROCESSOR_R5000, 4, 0 },
591 { "vr5400", PROCESSOR_R5400, 4, 0 },
592 { "vr5500", PROCESSOR_R5500, 4, PTF_AVOID_BRANCHLIKELY },
593 { "rm7000", PROCESSOR_R7000, 4, 0 },
594 { "rm9000", PROCESSOR_R9000, 4, 0 },
596 /* MIPS32 processors. */
597 { "4kc", PROCESSOR_4KC, 32, 0 },
598 { "4km", PROCESSOR_4KC, 32, 0 },
599 { "4kp", PROCESSOR_4KP, 32, 0 },
600 { "4ksc", PROCESSOR_4KC, 32, 0 },
602 /* MIPS32 Release 2 processors. */
603 { "m4k", PROCESSOR_M4K, 33, 0 },
604 { "4kec", PROCESSOR_4KC, 33, 0 },
605 { "4kem", PROCESSOR_4KC, 33, 0 },
606 { "4kep", PROCESSOR_4KP, 33, 0 },
607 { "4ksd", PROCESSOR_4KC, 33, 0 },
609 { "24kc", PROCESSOR_24KC, 33, 0 },
610 { "24kf2_1", PROCESSOR_24KF2_1, 33, 0 },
611 { "24kf", PROCESSOR_24KF2_1, 33, 0 },
612 { "24kf1_1", PROCESSOR_24KF1_1, 33, 0 },
613 { "24kfx", PROCESSOR_24KF1_1, 33, 0 },
614 { "24kx", PROCESSOR_24KF1_1, 33, 0 },
616 { "24kec", PROCESSOR_24KC, 33, 0 }, /* 24K with DSP. */
617 { "24kef2_1", PROCESSOR_24KF2_1, 33, 0 },
618 { "24kef", PROCESSOR_24KF2_1, 33, 0 },
619 { "24kef1_1", PROCESSOR_24KF1_1, 33, 0 },
620 { "24kefx", PROCESSOR_24KF1_1, 33, 0 },
621 { "24kex", PROCESSOR_24KF1_1, 33, 0 },
623 { "34kc", PROCESSOR_24KC, 33, 0 }, /* 34K with MT/DSP. */
624 { "34kf2_1", PROCESSOR_24KF2_1, 33, 0 },
625 { "34kf", PROCESSOR_24KF2_1, 33, 0 },
626 { "34kf1_1", PROCESSOR_24KF1_1, 33, 0 },
627 { "34kfx", PROCESSOR_24KF1_1, 33, 0 },
628 { "34kx", PROCESSOR_24KF1_1, 33, 0 },
630 { "74kc", PROCESSOR_74KC, 33, 0 }, /* 74K with DSPr2. */
631 { "74kf2_1", PROCESSOR_74KF2_1, 33, 0 },
632 { "74kf", PROCESSOR_74KF2_1, 33, 0 },
633 { "74kf1_1", PROCESSOR_74KF1_1, 33, 0 },
634 { "74kfx", PROCESSOR_74KF1_1, 33, 0 },
635 { "74kx", PROCESSOR_74KF1_1, 33, 0 },
636 { "74kf3_2", PROCESSOR_74KF3_2, 33, 0 },
638 /* MIPS64 processors. */
639 { "5kc", PROCESSOR_5KC, 64, 0 },
640 { "5kf", PROCESSOR_5KF, 64, 0 },
641 { "20kc", PROCESSOR_20KC, 64, PTF_AVOID_BRANCHLIKELY },
642 { "sb1", PROCESSOR_SB1, 64, PTF_AVOID_BRANCHLIKELY },
643 { "sb1a", PROCESSOR_SB1A, 64, PTF_AVOID_BRANCHLIKELY },
644 { "sr71000", PROCESSOR_SR71000, 64, PTF_AVOID_BRANCHLIKELY },
647 /* Default costs. If these are used for a processor we should look
648 up the actual costs. */
649 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
650 COSTS_N_INSNS (7), /* fp_mult_sf */ \
651 COSTS_N_INSNS (8), /* fp_mult_df */ \
652 COSTS_N_INSNS (23), /* fp_div_sf */ \
653 COSTS_N_INSNS (36), /* fp_div_df */ \
654 COSTS_N_INSNS (10), /* int_mult_si */ \
655 COSTS_N_INSNS (10), /* int_mult_di */ \
656 COSTS_N_INSNS (69), /* int_div_si */ \
657 COSTS_N_INSNS (69), /* int_div_di */ \
658 2, /* branch_cost */ \
659 4 /* memory_latency */
661 /* Floating-point costs for processors without an FPU. Just assume that
662 all floating-point libcalls are very expensive. */
663 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
664 COSTS_N_INSNS (256), /* fp_mult_sf */ \
665 COSTS_N_INSNS (256), /* fp_mult_df */ \
666 COSTS_N_INSNS (256), /* fp_div_sf */ \
667 COSTS_N_INSNS (256) /* fp_div_df */
669 /* Costs to use when optimizing for size. */
670 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
671 COSTS_N_INSNS (1), /* fp_add */
672 COSTS_N_INSNS (1), /* fp_mult_sf */
673 COSTS_N_INSNS (1), /* fp_mult_df */
674 COSTS_N_INSNS (1), /* fp_div_sf */
675 COSTS_N_INSNS (1), /* fp_div_df */
676 COSTS_N_INSNS (1), /* int_mult_si */
677 COSTS_N_INSNS (1), /* int_mult_di */
678 COSTS_N_INSNS (1), /* int_div_si */
679 COSTS_N_INSNS (1), /* int_div_di */
681 4 /* memory_latency */
684 /* Costs to use when optimizing for speed, indexed by processor. */
685 static const struct mips_rtx_cost_data mips_rtx_cost_data[PROCESSOR_MAX] = {
687 COSTS_N_INSNS (2), /* fp_add */
688 COSTS_N_INSNS (4), /* fp_mult_sf */
689 COSTS_N_INSNS (5), /* fp_mult_df */
690 COSTS_N_INSNS (12), /* fp_div_sf */
691 COSTS_N_INSNS (19), /* fp_div_df */
692 COSTS_N_INSNS (12), /* int_mult_si */
693 COSTS_N_INSNS (12), /* int_mult_di */
694 COSTS_N_INSNS (35), /* int_div_si */
695 COSTS_N_INSNS (35), /* int_div_di */
697 4 /* memory_latency */
701 COSTS_N_INSNS (6), /* int_mult_si */
702 COSTS_N_INSNS (6), /* int_mult_di */
703 COSTS_N_INSNS (36), /* int_div_si */
704 COSTS_N_INSNS (36), /* int_div_di */
706 4 /* memory_latency */
710 COSTS_N_INSNS (36), /* int_mult_si */
711 COSTS_N_INSNS (36), /* int_mult_di */
712 COSTS_N_INSNS (37), /* int_div_si */
713 COSTS_N_INSNS (37), /* int_div_di */
715 4 /* memory_latency */
719 COSTS_N_INSNS (4), /* int_mult_si */
720 COSTS_N_INSNS (11), /* int_mult_di */
721 COSTS_N_INSNS (36), /* int_div_si */
722 COSTS_N_INSNS (68), /* int_div_di */
724 4 /* memory_latency */
727 COSTS_N_INSNS (4), /* fp_add */
728 COSTS_N_INSNS (4), /* fp_mult_sf */
729 COSTS_N_INSNS (5), /* fp_mult_df */
730 COSTS_N_INSNS (17), /* fp_div_sf */
731 COSTS_N_INSNS (32), /* fp_div_df */
732 COSTS_N_INSNS (4), /* int_mult_si */
733 COSTS_N_INSNS (11), /* int_mult_di */
734 COSTS_N_INSNS (36), /* int_div_si */
735 COSTS_N_INSNS (68), /* int_div_di */
737 4 /* memory_latency */
740 COSTS_N_INSNS (4), /* fp_add */
741 COSTS_N_INSNS (4), /* fp_mult_sf */
742 COSTS_N_INSNS (5), /* fp_mult_df */
743 COSTS_N_INSNS (17), /* fp_div_sf */
744 COSTS_N_INSNS (32), /* fp_div_df */
745 COSTS_N_INSNS (4), /* int_mult_si */
746 COSTS_N_INSNS (7), /* int_mult_di */
747 COSTS_N_INSNS (42), /* int_div_si */
748 COSTS_N_INSNS (72), /* int_div_di */
750 4 /* memory_latency */
754 COSTS_N_INSNS (5), /* int_mult_si */
755 COSTS_N_INSNS (5), /* int_mult_di */
756 COSTS_N_INSNS (41), /* int_div_si */
757 COSTS_N_INSNS (41), /* int_div_di */
759 4 /* memory_latency */
762 COSTS_N_INSNS (8), /* fp_add */
763 COSTS_N_INSNS (8), /* fp_mult_sf */
764 COSTS_N_INSNS (10), /* fp_mult_df */
765 COSTS_N_INSNS (34), /* fp_div_sf */
766 COSTS_N_INSNS (64), /* fp_div_df */
767 COSTS_N_INSNS (5), /* int_mult_si */
768 COSTS_N_INSNS (5), /* int_mult_di */
769 COSTS_N_INSNS (41), /* int_div_si */
770 COSTS_N_INSNS (41), /* int_div_di */
772 4 /* memory_latency */
775 COSTS_N_INSNS (4), /* fp_add */
776 COSTS_N_INSNS (4), /* fp_mult_sf */
777 COSTS_N_INSNS (5), /* fp_mult_df */
778 COSTS_N_INSNS (17), /* fp_div_sf */
779 COSTS_N_INSNS (32), /* fp_div_df */
780 COSTS_N_INSNS (5), /* int_mult_si */
781 COSTS_N_INSNS (5), /* int_mult_di */
782 COSTS_N_INSNS (41), /* int_div_si */
783 COSTS_N_INSNS (41), /* int_div_di */
785 4 /* memory_latency */
789 COSTS_N_INSNS (5), /* int_mult_si */
790 COSTS_N_INSNS (5), /* int_mult_di */
791 COSTS_N_INSNS (41), /* int_div_si */
792 COSTS_N_INSNS (41), /* int_div_di */
794 4 /* memory_latency */
797 COSTS_N_INSNS (8), /* fp_add */
798 COSTS_N_INSNS (8), /* fp_mult_sf */
799 COSTS_N_INSNS (10), /* fp_mult_df */
800 COSTS_N_INSNS (34), /* fp_div_sf */
801 COSTS_N_INSNS (64), /* fp_div_df */
802 COSTS_N_INSNS (5), /* int_mult_si */
803 COSTS_N_INSNS (5), /* int_mult_di */
804 COSTS_N_INSNS (41), /* int_div_si */
805 COSTS_N_INSNS (41), /* int_div_di */
807 4 /* memory_latency */
810 COSTS_N_INSNS (4), /* fp_add */
811 COSTS_N_INSNS (4), /* fp_mult_sf */
812 COSTS_N_INSNS (5), /* fp_mult_df */
813 COSTS_N_INSNS (17), /* fp_div_sf */
814 COSTS_N_INSNS (32), /* fp_div_df */
815 COSTS_N_INSNS (5), /* int_mult_si */
816 COSTS_N_INSNS (5), /* int_mult_di */
817 COSTS_N_INSNS (41), /* int_div_si */
818 COSTS_N_INSNS (41), /* int_div_di */
820 4 /* memory_latency */
823 COSTS_N_INSNS (6), /* fp_add */
824 COSTS_N_INSNS (6), /* fp_mult_sf */
825 COSTS_N_INSNS (7), /* fp_mult_df */
826 COSTS_N_INSNS (25), /* fp_div_sf */
827 COSTS_N_INSNS (48), /* fp_div_df */
828 COSTS_N_INSNS (5), /* int_mult_si */
829 COSTS_N_INSNS (5), /* int_mult_di */
830 COSTS_N_INSNS (41), /* int_div_si */
831 COSTS_N_INSNS (41), /* int_div_di */
833 4 /* memory_latency */
839 COSTS_N_INSNS (2), /* fp_add */
840 COSTS_N_INSNS (4), /* fp_mult_sf */
841 COSTS_N_INSNS (5), /* fp_mult_df */
842 COSTS_N_INSNS (12), /* fp_div_sf */
843 COSTS_N_INSNS (19), /* fp_div_df */
844 COSTS_N_INSNS (2), /* int_mult_si */
845 COSTS_N_INSNS (2), /* int_mult_di */
846 COSTS_N_INSNS (35), /* int_div_si */
847 COSTS_N_INSNS (35), /* int_div_di */
849 4 /* memory_latency */
852 COSTS_N_INSNS (3), /* fp_add */
853 COSTS_N_INSNS (5), /* fp_mult_sf */
854 COSTS_N_INSNS (6), /* fp_mult_df */
855 COSTS_N_INSNS (15), /* fp_div_sf */
856 COSTS_N_INSNS (16), /* fp_div_df */
857 COSTS_N_INSNS (17), /* int_mult_si */
858 COSTS_N_INSNS (17), /* int_mult_di */
859 COSTS_N_INSNS (38), /* int_div_si */
860 COSTS_N_INSNS (38), /* int_div_di */
862 6 /* memory_latency */
865 COSTS_N_INSNS (6), /* fp_add */
866 COSTS_N_INSNS (7), /* fp_mult_sf */
867 COSTS_N_INSNS (8), /* fp_mult_df */
868 COSTS_N_INSNS (23), /* fp_div_sf */
869 COSTS_N_INSNS (36), /* fp_div_df */
870 COSTS_N_INSNS (10), /* int_mult_si */
871 COSTS_N_INSNS (10), /* int_mult_di */
872 COSTS_N_INSNS (69), /* int_div_si */
873 COSTS_N_INSNS (69), /* int_div_di */
875 6 /* memory_latency */
887 /* The only costs that appear to be updated here are
888 integer multiplication. */
890 COSTS_N_INSNS (4), /* int_mult_si */
891 COSTS_N_INSNS (6), /* int_mult_di */
892 COSTS_N_INSNS (69), /* int_div_si */
893 COSTS_N_INSNS (69), /* int_div_di */
895 4 /* memory_latency */
907 COSTS_N_INSNS (6), /* fp_add */
908 COSTS_N_INSNS (4), /* fp_mult_sf */
909 COSTS_N_INSNS (5), /* fp_mult_df */
910 COSTS_N_INSNS (23), /* fp_div_sf */
911 COSTS_N_INSNS (36), /* fp_div_df */
912 COSTS_N_INSNS (5), /* int_mult_si */
913 COSTS_N_INSNS (5), /* int_mult_di */
914 COSTS_N_INSNS (36), /* int_div_si */
915 COSTS_N_INSNS (36), /* int_div_di */
917 4 /* memory_latency */
920 COSTS_N_INSNS (6), /* fp_add */
921 COSTS_N_INSNS (5), /* fp_mult_sf */
922 COSTS_N_INSNS (6), /* fp_mult_df */
923 COSTS_N_INSNS (30), /* fp_div_sf */
924 COSTS_N_INSNS (59), /* fp_div_df */
925 COSTS_N_INSNS (3), /* int_mult_si */
926 COSTS_N_INSNS (4), /* int_mult_di */
927 COSTS_N_INSNS (42), /* int_div_si */
928 COSTS_N_INSNS (74), /* int_div_di */
930 4 /* memory_latency */
933 COSTS_N_INSNS (6), /* fp_add */
934 COSTS_N_INSNS (5), /* fp_mult_sf */
935 COSTS_N_INSNS (6), /* fp_mult_df */
936 COSTS_N_INSNS (30), /* fp_div_sf */
937 COSTS_N_INSNS (59), /* fp_div_df */
938 COSTS_N_INSNS (5), /* int_mult_si */
939 COSTS_N_INSNS (9), /* int_mult_di */
940 COSTS_N_INSNS (42), /* int_div_si */
941 COSTS_N_INSNS (74), /* int_div_di */
943 4 /* memory_latency */
946 /* The only costs that are changed here are
947 integer multiplication. */
948 COSTS_N_INSNS (6), /* fp_add */
949 COSTS_N_INSNS (7), /* fp_mult_sf */
950 COSTS_N_INSNS (8), /* fp_mult_df */
951 COSTS_N_INSNS (23), /* fp_div_sf */
952 COSTS_N_INSNS (36), /* fp_div_df */
953 COSTS_N_INSNS (5), /* int_mult_si */
954 COSTS_N_INSNS (9), /* int_mult_di */
955 COSTS_N_INSNS (69), /* int_div_si */
956 COSTS_N_INSNS (69), /* int_div_di */
958 4 /* memory_latency */
964 /* The only costs that are changed here are
965 integer multiplication. */
966 COSTS_N_INSNS (6), /* fp_add */
967 COSTS_N_INSNS (7), /* fp_mult_sf */
968 COSTS_N_INSNS (8), /* fp_mult_df */
969 COSTS_N_INSNS (23), /* fp_div_sf */
970 COSTS_N_INSNS (36), /* fp_div_df */
971 COSTS_N_INSNS (3), /* int_mult_si */
972 COSTS_N_INSNS (8), /* int_mult_di */
973 COSTS_N_INSNS (69), /* int_div_si */
974 COSTS_N_INSNS (69), /* int_div_di */
976 4 /* memory_latency */
979 /* These costs are the same as the SB-1A below. */
980 COSTS_N_INSNS (4), /* fp_add */
981 COSTS_N_INSNS (4), /* fp_mult_sf */
982 COSTS_N_INSNS (4), /* fp_mult_df */
983 COSTS_N_INSNS (24), /* fp_div_sf */
984 COSTS_N_INSNS (32), /* fp_div_df */
985 COSTS_N_INSNS (3), /* int_mult_si */
986 COSTS_N_INSNS (4), /* int_mult_di */
987 COSTS_N_INSNS (36), /* int_div_si */
988 COSTS_N_INSNS (68), /* int_div_di */
990 4 /* memory_latency */
993 /* These costs are the same as the SB-1 above. */
994 COSTS_N_INSNS (4), /* fp_add */
995 COSTS_N_INSNS (4), /* fp_mult_sf */
996 COSTS_N_INSNS (4), /* fp_mult_df */
997 COSTS_N_INSNS (24), /* fp_div_sf */
998 COSTS_N_INSNS (32), /* fp_div_df */
999 COSTS_N_INSNS (3), /* int_mult_si */
1000 COSTS_N_INSNS (4), /* int_mult_di */
1001 COSTS_N_INSNS (36), /* int_div_si */
1002 COSTS_N_INSNS (68), /* int_div_di */
1003 1, /* branch_cost */
1004 4 /* memory_latency */
1011 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1012 for -mflip_mips16. It maps decl names onto a boolean mode setting. */
1013 struct mflip_mips16_entry GTY (()) {
1017 static GTY ((param_is (struct mflip_mips16_entry))) htab_t mflip_mips16_htab;
1019 /* Hash table callbacks for mflip_mips16_htab. */
1022 mflip_mips16_htab_hash (const void *entry)
1024 return htab_hash_string (((const struct mflip_mips16_entry *) entry)->name);
1028 mflip_mips16_htab_eq (const void *entry, const void *name)
1030 return strcmp (((const struct mflip_mips16_entry *) entry)->name,
1031 (const char *) name) == 0;
1034 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1035 mode, false if it should next add an attribute for the opposite mode. */
1036 static GTY(()) bool mips16_flipper;
1038 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1039 for -mflip-mips16. Return true if it should use "mips16" and false if
1040 it should use "nomips16". */
1043 mflip_mips16_use_mips16_p (tree decl)
1045 struct mflip_mips16_entry *entry;
1050 /* Use the opposite of the command-line setting for anonymous decls. */
1051 if (!DECL_NAME (decl))
1052 return !mips_base_mips16;
1054 if (!mflip_mips16_htab)
1055 mflip_mips16_htab = htab_create_ggc (37, mflip_mips16_htab_hash,
1056 mflip_mips16_htab_eq, NULL);
1058 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1059 hash = htab_hash_string (name);
1060 slot = htab_find_slot_with_hash (mflip_mips16_htab, name, hash, INSERT);
1061 entry = (struct mflip_mips16_entry *) *slot;
1064 mips16_flipper = !mips16_flipper;
1065 entry = GGC_NEW (struct mflip_mips16_entry);
1067 entry->mips16_p = mips16_flipper ? !mips_base_mips16 : mips_base_mips16;
1070 return entry->mips16_p;
1073 /* Predicates to test for presence of "near" and "far"/"long_call"
1074 attributes on the given TYPE. */
1077 mips_near_type_p (const_tree type)
1079 return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1083 mips_far_type_p (const_tree type)
1085 return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1086 || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1089 /* Similar predicates for "mips16"/"nomips16" function attributes. */
1092 mips_mips16_decl_p (const_tree decl)
1094 return lookup_attribute ("mips16", DECL_ATTRIBUTES (decl)) != NULL;
1098 mips_nomips16_decl_p (const_tree decl)
1100 return lookup_attribute ("nomips16", DECL_ATTRIBUTES (decl)) != NULL;
1103 /* Return true if function DECL is a MIPS16 function. Return the ambient
1104 setting if DECL is null. */
1107 mips_use_mips16_mode_p (tree decl)
1111 /* Nested functions must use the same frame pointer as their
1112 parent and must therefore use the same ISA mode. */
1113 tree parent = decl_function_context (decl);
1116 if (mips_mips16_decl_p (decl))
1118 if (mips_nomips16_decl_p (decl))
1121 return mips_base_mips16;
1124 /* Implement TARGET_COMP_TYPE_ATTRIBUTES. */
1127 mips_comp_type_attributes (const_tree type1, const_tree type2)
1129 /* Disallow mixed near/far attributes. */
1130 if (mips_far_type_p (type1) && mips_near_type_p (type2))
1132 if (mips_near_type_p (type1) && mips_far_type_p (type2))
1137 /* Implement TARGET_INSERT_ATTRIBUTES. */
1140 mips_insert_attributes (tree decl, tree *attributes)
1143 bool mips16_p, nomips16_p;
1145 /* Check for "mips16" and "nomips16" attributes. */
1146 mips16_p = lookup_attribute ("mips16", *attributes) != NULL;
1147 nomips16_p = lookup_attribute ("nomips16", *attributes) != NULL;
1148 if (TREE_CODE (decl) != FUNCTION_DECL)
1151 error ("%qs attribute only applies to functions", "mips16");
1153 error ("%qs attribute only applies to functions", "nomips16");
1157 mips16_p |= mips_mips16_decl_p (decl);
1158 nomips16_p |= mips_nomips16_decl_p (decl);
1159 if (mips16_p || nomips16_p)
1161 /* DECL cannot be simultaneously "mips16" and "nomips16". */
1162 if (mips16_p && nomips16_p)
1163 error ("%qs cannot have both %<mips16%> and "
1164 "%<nomips16%> attributes",
1165 IDENTIFIER_POINTER (DECL_NAME (decl)));
1167 else if (TARGET_FLIP_MIPS16 && !DECL_ARTIFICIAL (decl))
1169 /* Implement -mflip-mips16. If DECL has neither a "nomips16" nor a
1170 "mips16" attribute, arbitrarily pick one. We must pick the same
1171 setting for duplicate declarations of a function. */
1172 name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1173 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1178 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
1181 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1183 /* The decls' "mips16" and "nomips16" attributes must match exactly. */
1184 if (mips_mips16_decl_p (olddecl) != mips_mips16_decl_p (newdecl))
1185 error ("%qs redeclared with conflicting %qs attributes",
1186 IDENTIFIER_POINTER (DECL_NAME (newdecl)), "mips16");
1187 if (mips_nomips16_decl_p (olddecl) != mips_nomips16_decl_p (newdecl))
1188 error ("%qs redeclared with conflicting %qs attributes",
1189 IDENTIFIER_POINTER (DECL_NAME (newdecl)), "nomips16");
1191 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1192 DECL_ATTRIBUTES (newdecl));
1195 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1196 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
1199 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1201 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
1203 *base_ptr = XEXP (x, 0);
1204 *offset_ptr = INTVAL (XEXP (x, 1));
1213 static unsigned int mips_build_integer (struct mips_integer_op *,
1214 unsigned HOST_WIDE_INT);
1216 /* A subroutine of mips_build_integer, with the same interface.
1217 Assume that the final action in the sequence should be a left shift. */
1220 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1222 unsigned int i, shift;
1224 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1225 since signed numbers are easier to load than unsigned ones. */
1227 while ((value & 1) == 0)
1228 value /= 2, shift++;
1230 i = mips_build_integer (codes, value);
1231 codes[i].code = ASHIFT;
1232 codes[i].value = shift;
1236 /* As for mips_build_shift, but assume that the final action will be
1237 an IOR or PLUS operation. */
1240 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1242 unsigned HOST_WIDE_INT high;
1245 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1246 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1248 /* The constant is too complex to load with a simple LUI/ORI pair,
1249 so we want to give the recursive call as many trailing zeros as
1250 possible. In this case, we know bit 16 is set and that the
1251 low 16 bits form a negative number. If we subtract that number
1252 from VALUE, we will clear at least the lowest 17 bits, maybe more. */
1253 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1254 codes[i].code = PLUS;
1255 codes[i].value = CONST_LOW_PART (value);
1259 /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1260 bits gives a value with at least 17 trailing zeros. */
1261 i = mips_build_integer (codes, high);
1262 codes[i].code = IOR;
1263 codes[i].value = value & 0xffff;
1268 /* Fill CODES with a sequence of rtl operations to load VALUE.
1269 Return the number of operations needed. */
1272 mips_build_integer (struct mips_integer_op *codes,
1273 unsigned HOST_WIDE_INT value)
1275 if (SMALL_OPERAND (value)
1276 || SMALL_OPERAND_UNSIGNED (value)
1277 || LUI_OPERAND (value))
1279 /* The value can be loaded with a single instruction. */
1280 codes[0].code = UNKNOWN;
1281 codes[0].value = value;
1284 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1286 /* Either the constant is a simple LUI/ORI combination or its
1287 lowest bit is set. We don't want to shift in this case. */
1288 return mips_build_lower (codes, value);
1290 else if ((value & 0xffff) == 0)
1292 /* The constant will need at least three actions. The lowest
1293 16 bits are clear, so the final action will be a shift. */
1294 return mips_build_shift (codes, value);
1298 /* The final action could be a shift, add or inclusive OR.
1299 Rather than use a complex condition to select the best
1300 approach, try both mips_build_shift and mips_build_lower
1301 and pick the one that gives the shortest sequence.
1302 Note that this case is only used once per constant. */
1303 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1304 unsigned int cost, alt_cost;
1306 cost = mips_build_shift (codes, value);
1307 alt_cost = mips_build_lower (alt_codes, value);
1308 if (alt_cost < cost)
1310 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1317 /* Return true if X is a thread-local symbol. */
1320 mips_tls_symbol_p (rtx x)
1322 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1325 /* Return true if SYMBOL_REF X is associated with a global symbol
1326 (in the STB_GLOBAL sense). */
1329 mips_global_symbol_p (const_rtx x)
1331 const_tree decl = SYMBOL_REF_DECL (x);
1334 return !SYMBOL_REF_LOCAL_P (x);
1336 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1337 or weak symbols. Relocations in the object file will be against
1338 the target symbol, so it's that symbol's binding that matters here. */
1339 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1342 /* Return true if SYMBOL_REF X binds locally. */
1345 mips_symbol_binds_local_p (const_rtx x)
1347 return (SYMBOL_REF_DECL (x)
1348 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1349 : SYMBOL_REF_LOCAL_P (x));
1352 /* Return true if rtx constants of mode MODE should be put into a small
1356 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1358 return (!TARGET_EMBEDDED_DATA
1359 && TARGET_LOCAL_SDATA
1360 && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1363 /* Return true if X should not be moved directly into register $25.
1364 We need this because many versions of GAS will treat "la $25,foo" as
1365 part of a call sequence and so allow a global "foo" to be lazily bound. */
1368 mips_dangerous_for_la25_p (rtx x)
1370 return (!TARGET_EXPLICIT_RELOCS
1372 && GET_CODE (x) == SYMBOL_REF
1373 && mips_global_symbol_p (x));
1376 /* Return the method that should be used to access SYMBOL_REF or
1377 LABEL_REF X in context CONTEXT. */
1379 static enum mips_symbol_type
1380 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1383 return SYMBOL_GOT_DISP;
1385 if (GET_CODE (x) == LABEL_REF)
1387 /* LABEL_REFs are used for jump tables as well as text labels.
1388 Only return SYMBOL_PC_RELATIVE if we know the label is in
1389 the text section. */
1390 if (TARGET_MIPS16_SHORT_JUMP_TABLES)
1391 return SYMBOL_PC_RELATIVE;
1393 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1394 return SYMBOL_GOT_PAGE_OFST;
1396 return SYMBOL_ABSOLUTE;
1399 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1401 if (SYMBOL_REF_TLS_MODEL (x))
1404 if (CONSTANT_POOL_ADDRESS_P (x))
1406 if (TARGET_MIPS16_TEXT_LOADS)
1407 return SYMBOL_PC_RELATIVE;
1409 if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1410 return SYMBOL_PC_RELATIVE;
1412 if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1413 return SYMBOL_GP_RELATIVE;
1416 /* Do not use small-data accesses for weak symbols; they may end up
1418 if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1419 return SYMBOL_GP_RELATIVE;
1421 /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1424 && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1426 /* There are three cases to consider:
1428 - o32 PIC (either with or without explicit relocs)
1429 - n32/n64 PIC without explicit relocs
1430 - n32/n64 PIC with explicit relocs
1432 In the first case, both local and global accesses will use an
1433 R_MIPS_GOT16 relocation. We must correctly predict which of
1434 the two semantics (local or global) the assembler and linker
1435 will apply. The choice depends on the symbol's binding rather
1436 than its visibility.
1438 In the second case, the assembler will not use R_MIPS_GOT16
1439 relocations, but it chooses between local and global accesses
1440 in the same way as for o32 PIC.
1442 In the third case we have more freedom since both forms of
1443 access will work for any kind of symbol. However, there seems
1444 little point in doing things differently. */
1445 if (mips_global_symbol_p (x))
1446 return SYMBOL_GOT_DISP;
1448 return SYMBOL_GOT_PAGE_OFST;
1451 if (TARGET_MIPS16_PCREL_LOADS && context != SYMBOL_CONTEXT_CALL)
1452 return SYMBOL_FORCE_TO_MEM;
1454 return SYMBOL_ABSOLUTE;
1457 /* Classify the base of symbolic expression X, given that X appears in
1460 static enum mips_symbol_type
1461 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1465 split_const (x, &x, &offset);
1466 if (UNSPEC_ADDRESS_P (x))
1467 return UNSPEC_ADDRESS_TYPE (x);
1469 return mips_classify_symbol (x, context);
1472 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1473 is the alignment in bytes of SYMBOL_REF X. */
1476 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1478 HOST_WIDE_INT align;
1480 align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1481 return IN_RANGE (offset, 0, align - 1);
1484 /* Return true if X is a symbolic constant that can be used in context
1485 CONTEXT. If it is, store the type of the symbol in *SYMBOL_TYPE. */
1488 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1489 enum mips_symbol_type *symbol_type)
1493 split_const (x, &x, &offset);
1494 if (UNSPEC_ADDRESS_P (x))
1496 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1497 x = UNSPEC_ADDRESS (x);
1499 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1501 *symbol_type = mips_classify_symbol (x, context);
1502 if (*symbol_type == SYMBOL_TLS)
1508 if (offset == const0_rtx)
1511 /* Check whether a nonzero offset is valid for the underlying
1513 switch (*symbol_type)
1515 case SYMBOL_ABSOLUTE:
1516 case SYMBOL_FORCE_TO_MEM:
1517 case SYMBOL_32_HIGH:
1518 case SYMBOL_64_HIGH:
1521 /* If the target has 64-bit pointers and the object file only
1522 supports 32-bit symbols, the values of those symbols will be
1523 sign-extended. In this case we can't allow an arbitrary offset
1524 in case the 32-bit value X + OFFSET has a different sign from X. */
1525 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1526 return offset_within_block_p (x, INTVAL (offset));
1528 /* In other cases the relocations can handle any offset. */
1531 case SYMBOL_PC_RELATIVE:
1532 /* Allow constant pool references to be converted to LABEL+CONSTANT.
1533 In this case, we no longer have access to the underlying constant,
1534 but the original symbol-based access was known to be valid. */
1535 if (GET_CODE (x) == LABEL_REF)
1540 case SYMBOL_GP_RELATIVE:
1541 /* Make sure that the offset refers to something within the
1542 same object block. This should guarantee that the final
1543 PC- or GP-relative offset is within the 16-bit limit. */
1544 return offset_within_block_p (x, INTVAL (offset));
1546 case SYMBOL_GOT_PAGE_OFST:
1547 case SYMBOL_GOTOFF_PAGE:
1548 /* If the symbol is global, the GOT entry will contain the symbol's
1549 address, and we will apply a 16-bit offset after loading it.
1550 If the symbol is local, the linker should provide enough local
1551 GOT entries for a 16-bit offset, but larger offsets may lead
1553 return SMALL_INT (offset);
1557 /* There is no carry between the HI and LO REL relocations, so the
1558 offset is only valid if we know it won't lead to such a carry. */
1559 return mips_offset_within_alignment_p (x, INTVAL (offset));
1561 case SYMBOL_GOT_DISP:
1562 case SYMBOL_GOTOFF_DISP:
1563 case SYMBOL_GOTOFF_CALL:
1564 case SYMBOL_GOTOFF_LOADGP:
1567 case SYMBOL_GOTTPREL:
1575 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
1576 single instruction. We rely on the fact that, in the worst case,
1577 all instructions involved in a MIPS16 address calculation are usually
1581 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
1585 case SYMBOL_ABSOLUTE:
1586 /* When using 64-bit symbols, we need 5 preparatory instructions,
1589 lui $at,%highest(symbol)
1590 daddiu $at,$at,%higher(symbol)
1592 daddiu $at,$at,%hi(symbol)
1595 The final address is then $at + %lo(symbol). With 32-bit
1596 symbols we just need a preparatory LUI for normal mode and
1597 a preparatory LI and SLL for MIPS16. */
1598 return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
1600 case SYMBOL_GP_RELATIVE:
1601 /* Treat GP-relative accesses as taking a single instruction on
1602 MIPS16 too; the copy of $gp can often be shared. */
1605 case SYMBOL_PC_RELATIVE:
1606 /* PC-relative constants can be only be used with ADDIUPC,
1607 DADDIUPC, LWPC and LDPC. */
1608 if (mode == MAX_MACHINE_MODE
1609 || GET_MODE_SIZE (mode) == 4
1610 || GET_MODE_SIZE (mode) == 8)
1613 /* The constant must be loaded using ADDIUPC or DADDIUPC first. */
1616 case SYMBOL_FORCE_TO_MEM:
1617 /* LEAs will be converted into constant-pool references by
1619 if (mode == MAX_MACHINE_MODE)
1622 /* The constant must be loaded and then dereferenced. */
1625 case SYMBOL_GOT_DISP:
1626 /* The constant will have to be loaded from the GOT before it
1627 is used in an address. */
1628 if (mode != MAX_MACHINE_MODE)
1633 case SYMBOL_GOT_PAGE_OFST:
1634 /* Unless -funit-at-a-time is in effect, we can't be sure whether the
1635 local/global classification is accurate. The worst cases are:
1637 (1) For local symbols when generating o32 or o64 code. The assembler
1643 ...and the final address will be $at + %lo(symbol).
1645 (2) For global symbols when -mxgot. The assembler will use:
1647 lui $at,%got_hi(symbol)
1650 ...and the final address will be $at + %got_lo(symbol). */
1653 case SYMBOL_GOTOFF_PAGE:
1654 case SYMBOL_GOTOFF_DISP:
1655 case SYMBOL_GOTOFF_CALL:
1656 case SYMBOL_GOTOFF_LOADGP:
1657 case SYMBOL_32_HIGH:
1658 case SYMBOL_64_HIGH:
1664 case SYMBOL_GOTTPREL:
1667 /* A 16-bit constant formed by a single relocation, or a 32-bit
1668 constant formed from a high 16-bit relocation and a low 16-bit
1669 relocation. Use mips_split_p to determine which. 32-bit
1670 constants need an "lui; addiu" sequence for normal mode and
1671 an "li; sll; addiu" sequence for MIPS16 mode. */
1672 return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
1675 /* We don't treat a bare TLS symbol as a constant. */
1681 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
1682 to load symbols of type TYPE into a register. Return 0 if the given
1683 type of symbol cannot be used as an immediate operand.
1685 Otherwise, return the number of instructions needed to load or store
1686 values of mode MODE to or from addresses of type TYPE. Return 0 if
1687 the given type of symbol is not valid in addresses.
1689 In both cases, treat extended MIPS16 instructions as two instructions. */
1692 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
1694 return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
1697 /* A for_each_rtx callback. Stop the search if *X references a
1698 thread-local symbol. */
1701 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1703 return mips_tls_symbol_p (*x);
1706 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
1709 mips_cannot_force_const_mem (rtx x)
1715 /* As an optimization, reject constants that mips_legitimize_move
1718 Suppose we have a multi-instruction sequence that loads constant C
1719 into register R. If R does not get allocated a hard register, and
1720 R is used in an operand that allows both registers and memory
1721 references, reload will consider forcing C into memory and using
1722 one of the instruction's memory alternatives. Returning false
1723 here will force it to use an input reload instead. */
1724 if (GET_CODE (x) == CONST_INT)
1727 split_const (x, &base, &offset);
1728 if (symbolic_operand (base, VOIDmode) && SMALL_INT (offset))
1732 /* TLS symbols must be computed by mips_legitimize_move. */
1733 if (for_each_rtx (&x, &mips_tls_symbol_ref_1, NULL))
1739 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. We can't use blocks for
1740 constants when we're using a per-function constant pool. */
1743 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
1744 const_rtx x ATTRIBUTE_UNUSED)
1746 return !TARGET_MIPS16_PCREL_LOADS;
1749 /* Return true if register REGNO is a valid base register for mode MODE.
1750 STRICT_P is true if REG_OK_STRICT is in effect. */
1753 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
1756 if (!HARD_REGISTER_NUM_P (regno))
1760 regno = reg_renumber[regno];
1763 /* These fake registers will be eliminated to either the stack or
1764 hard frame pointer, both of which are usually valid base registers.
1765 Reload deals with the cases where the eliminated form isn't valid. */
1766 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
1769 /* In MIPS16 mode, the stack pointer can only address word and doubleword
1770 values, nothing smaller. There are two problems here:
1772 (a) Instantiating virtual registers can introduce new uses of the
1773 stack pointer. If these virtual registers are valid addresses,
1774 the stack pointer should be too.
1776 (b) Most uses of the stack pointer are not made explicit until
1777 FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
1778 We don't know until that stage whether we'll be eliminating to the
1779 stack pointer (which needs the restriction) or the hard frame
1780 pointer (which doesn't).
1782 All in all, it seems more consistent to only enforce this restriction
1783 during and after reload. */
1784 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
1785 return !strict_p || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1787 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
1790 /* Return true if X is a valid base register for mode MODE.
1791 STRICT_P is true if REG_OK_STRICT is in effect. */
1794 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
1796 if (!strict_p && GET_CODE (x) == SUBREG)
1800 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
1803 /* Return true if X is a valid address for machine mode MODE. If it is,
1804 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
1808 mips_classify_address (struct mips_address_info *info, rtx x,
1809 enum machine_mode mode, bool strict_p)
1811 switch (GET_CODE (x))
1815 info->type = ADDRESS_REG;
1817 info->offset = const0_rtx;
1818 return mips_valid_base_register_p (info->reg, mode, strict_p);
1821 info->type = ADDRESS_REG;
1822 info->reg = XEXP (x, 0);
1823 info->offset = XEXP (x, 1);
1824 return (mips_valid_base_register_p (info->reg, mode, strict_p)
1825 && const_arith_operand (info->offset, VOIDmode));
1828 info->type = ADDRESS_LO_SUM;
1829 info->reg = XEXP (x, 0);
1830 info->offset = XEXP (x, 1);
1831 /* We have to trust the creator of the LO_SUM to do something vaguely
1832 sane. Target-independent code that creates a LO_SUM should also
1833 create and verify the matching HIGH. Target-independent code that
1834 adds an offset to a LO_SUM must prove that the offset will not
1835 induce a carry. Failure to do either of these things would be
1836 a bug, and we are not required to check for it here. The MIPS
1837 backend itself should only create LO_SUMs for valid symbolic
1838 constants, with the high part being either a HIGH or a copy
1841 = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
1842 return (mips_valid_base_register_p (info->reg, mode, strict_p)
1843 && mips_symbol_insns (info->symbol_type, mode) > 0
1844 && mips_lo_relocs[info->symbol_type] != 0);
1847 /* Small-integer addresses don't occur very often, but they
1848 are legitimate if $0 is a valid base register. */
1849 info->type = ADDRESS_CONST_INT;
1850 return !TARGET_MIPS16 && SMALL_INT (x);
1855 info->type = ADDRESS_SYMBOLIC;
1856 return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
1858 && mips_symbol_insns (info->symbol_type, mode) > 0
1859 && !mips_split_p[info->symbol_type]);
1866 /* Return true if X is a legitimate address for a memory operand of mode
1867 MODE. STRICT_P is true if REG_OK_STRICT is in effect. */
1870 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
1872 struct mips_address_info addr;
1874 return mips_classify_address (&addr, x, mode, strict_p);
1877 /* Return true if X is a legitimate $sp-based address for mode MDOE. */
1880 mips_stack_address_p (rtx x, enum machine_mode mode)
1882 struct mips_address_info addr;
1884 return (mips_classify_address (&addr, x, mode, false)
1885 && addr.type == ADDRESS_REG
1886 && addr.reg == stack_pointer_rtx);
1889 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
1890 address instruction. Note that such addresses are not considered
1891 legitimate in the GO_IF_LEGITIMATE_ADDRESS sense, because their use
1892 is so restricted. */
1895 mips_lwxs_address_p (rtx addr)
1898 && GET_CODE (addr) == PLUS
1899 && REG_P (XEXP (addr, 1)))
1901 rtx offset = XEXP (addr, 0);
1902 if (GET_CODE (offset) == MULT
1903 && REG_P (XEXP (offset, 0))
1904 && GET_CODE (XEXP (offset, 1)) == CONST_INT
1905 && INTVAL (XEXP (offset, 1)) == 4)
1911 /* Return true if a value at OFFSET bytes from base register BASE can be
1912 accessed using an unextended MIPS16 instruction. MODE is the mode of
1915 Usually the offset in an unextended instruction is a 5-bit field.
1916 The offset is unsigned and shifted left once for LH and SH, twice
1917 for LW and SW, and so on. An exception is LWSP and SWSP, which have
1918 an 8-bit immediate field that's shifted left twice. */
1921 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
1922 unsigned HOST_WIDE_INT offset)
1924 if (offset % GET_MODE_SIZE (mode) == 0)
1926 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
1927 return offset < 256U * GET_MODE_SIZE (mode);
1928 return offset < 32U * GET_MODE_SIZE (mode);
1933 /* Return the number of instructions needed to load or store a value
1934 of mode MODE at address X. Return 0 if X isn't valid for MODE.
1935 Assume that multiword moves may need to be split into word moves
1936 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
1939 For MIPS16 code, count extended instructions as two instructions. */
1942 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
1944 struct mips_address_info addr;
1947 /* BLKmode is used for single unaligned loads and stores and should
1948 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
1949 meaningless, so we have to single it out as a special case one way
1951 if (mode != BLKmode && might_split_p)
1952 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1956 if (mips_classify_address (&addr, x, mode, false))
1961 && !mips16_unextended_reference_p (mode, addr.reg,
1962 UINTVAL (addr.offset)))
1966 case ADDRESS_LO_SUM:
1967 return TARGET_MIPS16 ? factor * 2 : factor;
1969 case ADDRESS_CONST_INT:
1972 case ADDRESS_SYMBOLIC:
1973 return factor * mips_symbol_insns (addr.symbol_type, mode);
1978 /* Return the number of instructions needed to load constant X.
1979 Return 0 if X isn't a valid constant. */
1982 mips_const_insns (rtx x)
1984 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
1985 enum mips_symbol_type symbol_type;
1988 switch (GET_CODE (x))
1991 if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
1993 || !mips_split_p[symbol_type])
1996 /* This is simply an LUI for normal mode. It is an extended
1997 LI followed by an extended SLL for MIPS16. */
1998 return TARGET_MIPS16 ? 4 : 1;
2002 /* Unsigned 8-bit constants can be loaded using an unextended
2003 LI instruction. Unsigned 16-bit constants can be loaded
2004 using an extended LI. Negative constants must be loaded
2005 using LI and then negated. */
2006 return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2007 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2008 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2009 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2012 return mips_build_integer (codes, INTVAL (x));
2016 /* Allow zeros for normal mode, where we can use $0. */
2017 return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2023 /* See if we can refer to X directly. */
2024 if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2025 return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2027 /* Otherwise try splitting the constant into a base and offset.
2028 16-bit offsets can be added using an extra ADDIU. Larger offsets
2029 must be calculated separately and then added to the base. */
2030 split_const (x, &x, &offset);
2033 int n = mips_const_insns (x);
2036 if (SMALL_INT (offset))
2039 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2046 return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2054 /* Return the number of instructions needed to implement INSN,
2055 given that it loads from or stores to MEM. Count extended
2056 MIPS16 instructions as two instructions. */
2059 mips_load_store_insns (rtx mem, rtx insn)
2061 enum machine_mode mode;
2065 gcc_assert (MEM_P (mem));
2066 mode = GET_MODE (mem);
2068 /* Try to prove that INSN does not need to be split. */
2069 might_split_p = true;
2070 if (GET_MODE_BITSIZE (mode) == 64)
2072 set = single_set (insn);
2073 if (set && !mips_split_64bit_move_p (SET_DEST (set), SET_SRC (set)))
2074 might_split_p = false;
2077 return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2080 /* Return the number of instructions needed for an integer division. */
2083 mips_idiv_insns (void)
2088 if (TARGET_CHECK_ZERO_DIV)
2090 if (GENERATE_DIVIDE_TRAPS)
2096 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2101 /* Emit a move from SRC to DEST. Assume that the move expanders can
2102 handle all moves if !can_create_pseudo_p (). The distinction is
2103 important because, unlike emit_move_insn, the move expanders know
2104 how to force Pmode objects into the constant pool even when the
2105 constant pool address is not itself legitimate. */
2108 mips_emit_move (rtx dest, rtx src)
2110 return (can_create_pseudo_p ()
2111 ? emit_move_insn (dest, src)
2112 : emit_move_insn_1 (dest, src));
2115 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2118 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2120 emit_insn (gen_rtx_SET (VOIDmode, target,
2121 gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2124 /* Copy VALUE to a register and return that register. If new pseudos
2125 are allowed, copy it into a new register, otherwise use DEST. */
2128 mips_force_temporary (rtx dest, rtx value)
2130 if (can_create_pseudo_p ())
2131 return force_reg (Pmode, value);
2134 mips_emit_move (dest, value);
2139 /* Emit a call sequence with call pattern PATTERN and return the call
2140 instruction itself (which is not necessarily the last instruction
2141 emitted). LAZY_P is true if the call address is lazily-bound. */
2144 mips_emit_call_insn (rtx pattern, bool lazy_p)
2148 insn = emit_call_insn (pattern);
2150 /* Lazy-binding stubs require $gp to be valid on entry. */
2152 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2156 /* See the comment above load_call<mode> for details. */
2157 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2158 gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2159 emit_insn (gen_update_got_version ());
2164 /* Return an instruction that copies $gp into register REG. We want
2165 GCC to treat the register's value as constant, so that its value
2166 can be rematerialized on demand. */
2169 gen_load_const_gp (rtx reg)
2171 return (Pmode == SImode
2172 ? gen_load_const_gp_si (reg)
2173 : gen_load_const_gp_di (reg));
2176 /* Return a pseudo register that contains the value of $gp throughout
2177 the current function. Such registers are needed by MIPS16 functions,
2178 for which $gp itself is not a valid base register or addition operand. */
2181 mips16_gp_pseudo_reg (void)
2183 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2184 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2186 /* Don't emit an instruction to initialize the pseudo register if
2187 we are being called from the tree optimizers' cost-calculation
2189 if (!cfun->machine->initialized_mips16_gp_pseudo_p
2190 && (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl))
2192 rtx insn, scan, after;
2194 insn = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2196 push_topmost_sequence ();
2197 /* We need to emit the initialization after the FUNCTION_BEG
2198 note, so that it will be integrated. */
2199 after = get_insns ();
2200 for (scan = after; scan != NULL_RTX; scan = NEXT_INSN (scan))
2201 if (NOTE_P (scan) && NOTE_KIND (scan) == NOTE_INSN_FUNCTION_BEG)
2206 insn = emit_insn_after (insn, after);
2207 pop_topmost_sequence ();
2209 cfun->machine->initialized_mips16_gp_pseudo_p = true;
2212 return cfun->machine->mips16_gp_pseudo_rtx;
2215 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
2216 it appears in a MEM of that mode. Return true if ADDR is a legitimate
2217 constant in that context and can be split into a high part and a LO_SUM.
2218 If so, and if LO_SUM_OUT is nonnull, emit the high part and return
2219 the LO_SUM in *LO_SUM_OUT. Leave *LO_SUM_OUT unchanged otherwise.
2221 TEMP is as for mips_force_temporary and is used to load the high
2222 part into a register. */
2225 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *lo_sum_out)
2227 enum mips_symbol_context context;
2228 enum mips_symbol_type symbol_type;
2231 context = (mode == MAX_MACHINE_MODE
2232 ? SYMBOL_CONTEXT_LEA
2233 : SYMBOL_CONTEXT_MEM);
2234 if (!mips_symbolic_constant_p (addr, context, &symbol_type)
2235 || mips_symbol_insns (symbol_type, mode) == 0
2236 || !mips_split_p[symbol_type])
2241 if (symbol_type == SYMBOL_GP_RELATIVE)
2243 if (!can_create_pseudo_p ())
2245 emit_insn (gen_load_const_gp (temp));
2249 high = mips16_gp_pseudo_reg ();
2253 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
2254 high = mips_force_temporary (temp, high);
2256 *lo_sum_out = gen_rtx_LO_SUM (Pmode, high, addr);
2261 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2262 then add CONST_INT OFFSET to the result. */
2265 mips_unspec_address_offset (rtx base, rtx offset,
2266 enum mips_symbol_type symbol_type)
2268 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2269 UNSPEC_ADDRESS_FIRST + symbol_type);
2270 if (offset != const0_rtx)
2271 base = gen_rtx_PLUS (Pmode, base, offset);
2272 return gen_rtx_CONST (Pmode, base);
2275 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2276 type SYMBOL_TYPE. */
2279 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2283 split_const (address, &base, &offset);
2284 return mips_unspec_address_offset (base, offset, symbol_type);
2287 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2288 high part to BASE and return the result. Just return BASE otherwise.
2289 TEMP is as for mips_force_temporary.
2291 The returned expression can be used as the first operand to a LO_SUM. */
2294 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2295 enum mips_symbol_type symbol_type)
2297 if (mips_split_p[symbol_type])
2299 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2300 addr = mips_force_temporary (temp, addr);
2301 base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2306 /* Return a legitimate address for REG + OFFSET. TEMP is as for
2307 mips_force_temporary; it is only needed when OFFSET is not a
2311 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2313 if (!SMALL_OPERAND (offset))
2319 /* Load the full offset into a register so that we can use
2320 an unextended instruction for the address itself. */
2321 high = GEN_INT (offset);
2326 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH. */
2327 high = GEN_INT (CONST_HIGH_PART (offset));
2328 offset = CONST_LOW_PART (offset);
2330 high = mips_force_temporary (temp, high);
2331 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
2333 return plus_constant (reg, offset);
2336 /* The __tls_get_attr symbol. */
2337 static GTY(()) rtx mips_tls_symbol;
2339 /* Return an instruction sequence that calls __tls_get_addr. SYM is
2340 the TLS symbol we are referencing and TYPE is the symbol type to use
2341 (either global dynamic or local dynamic). V0 is an RTX for the
2342 return value location. */
2345 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
2349 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
2351 if (!mips_tls_symbol)
2352 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
2354 loc = mips_unspec_address (sym, type);
2358 emit_insn (gen_rtx_SET (Pmode, a0,
2359 gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
2360 insn = mips_expand_call (v0, mips_tls_symbol, const0_rtx, const0_rtx, false);
2361 CONST_OR_PURE_CALL_P (insn) = 1;
2362 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
2363 insn = get_insns ();
2370 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
2371 its address. The return value will be both a valid address and a valid
2372 SET_SRC (either a REG or a LO_SUM). */
2375 mips_legitimize_tls_address (rtx loc)
2377 rtx dest, insn, v0, v1, tmp1, tmp2, eqv;
2378 enum tls_model model;
2382 sorry ("MIPS16 TLS");
2383 return gen_reg_rtx (Pmode);
2386 model = SYMBOL_REF_TLS_MODEL (loc);
2387 /* Only TARGET_ABICALLS code can have more than one module; other
2388 code must be be static and should not use a GOT. All TLS models
2389 reduce to local exec in this situation. */
2390 if (!TARGET_ABICALLS)
2391 model = TLS_MODEL_LOCAL_EXEC;
2395 case TLS_MODEL_GLOBAL_DYNAMIC:
2396 v0 = gen_rtx_REG (Pmode, GP_RETURN);
2397 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
2398 dest = gen_reg_rtx (Pmode);
2399 emit_libcall_block (insn, dest, v0, loc);
2402 case TLS_MODEL_LOCAL_DYNAMIC:
2403 v0 = gen_rtx_REG (Pmode, GP_RETURN);
2404 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
2405 tmp1 = gen_reg_rtx (Pmode);
2407 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2408 share the LDM result with other LD model accesses. */
2409 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2411 emit_libcall_block (insn, tmp1, v0, eqv);
2413 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
2414 dest = gen_rtx_LO_SUM (Pmode, tmp2,
2415 mips_unspec_address (loc, SYMBOL_DTPREL));
2418 case TLS_MODEL_INITIAL_EXEC:
2419 v1 = gen_rtx_REG (Pmode, GP_RETURN + 1);
2420 tmp1 = gen_reg_rtx (Pmode);
2421 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
2422 if (Pmode == DImode)
2424 emit_insn (gen_tls_get_tp_di (v1));
2425 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
2429 emit_insn (gen_tls_get_tp_si (v1));
2430 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
2432 dest = gen_reg_rtx (Pmode);
2433 emit_insn (gen_add3_insn (dest, tmp1, v1));
2436 case TLS_MODEL_LOCAL_EXEC:
2437 v1 = gen_rtx_REG (Pmode, GP_RETURN + 1);
2438 if (Pmode == DImode)
2439 emit_insn (gen_tls_get_tp_di (v1));
2441 emit_insn (gen_tls_get_tp_si (v1));
2443 tmp1 = mips_unspec_offset_high (NULL, v1, loc, SYMBOL_TPREL);
2444 dest = gen_rtx_LO_SUM (Pmode, tmp1,
2445 mips_unspec_address (loc, SYMBOL_TPREL));
2454 /* This function is used to implement LEGITIMIZE_ADDRESS. If *XLOC can
2455 be legitimized in a way that the generic machinery might not expect,
2456 put the new address in *XLOC and return true. MODE is the mode of
2457 the memory being accessed. */
2460 mips_legitimize_address (rtx *xloc, enum machine_mode mode)
2463 HOST_WIDE_INT offset;
2465 if (mips_tls_symbol_p (*xloc))
2467 *xloc = mips_legitimize_tls_address (*xloc);
2471 /* See if the address can split into a high part and a LO_SUM. */
2472 if (mips_split_symbol (NULL, *xloc, mode, xloc))
2475 /* Handle BASE + OFFSET using mips_add_offset. */
2476 mips_split_plus (*xloc, &base, &offset);
2479 if (!mips_valid_base_register_p (base, mode, false))
2480 base = copy_to_mode_reg (Pmode, base);
2481 *xloc = mips_add_offset (NULL, base, offset);
2487 /* Load VALUE into DEST. TEMP is as for mips_force_temporary. */
2490 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
2492 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2493 enum machine_mode mode;
2494 unsigned int i, num_ops;
2497 mode = GET_MODE (dest);
2498 num_ops = mips_build_integer (codes, value);
2500 /* Apply each binary operation to X. Invariant: X is a legitimate
2501 source operand for a SET pattern. */
2502 x = GEN_INT (codes[0].value);
2503 for (i = 1; i < num_ops; i++)
2505 if (!can_create_pseudo_p ())
2507 emit_insn (gen_rtx_SET (VOIDmode, temp, x));
2511 x = force_reg (mode, x);
2512 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
2515 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
2518 /* Subroutine of mips_legitimize_move. Move constant SRC into register
2519 DEST given that SRC satisfies immediate_operand but doesn't satisfy
2523 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
2527 /* Split moves of big integers into smaller pieces. */
2528 if (splittable_const_int_operand (src, mode))
2530 mips_move_integer (dest, dest, INTVAL (src));
2534 /* Split moves of symbolic constants into high/low pairs. */
2535 if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
2537 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
2541 /* Generate the appropriate access sequences for TLS symbols. */
2542 if (mips_tls_symbol_p (src))
2544 mips_emit_move (dest, mips_legitimize_tls_address (src));
2548 /* If we have (const (plus symbol offset)), and that expression cannot
2549 be forced into memory, load the symbol first and add in the offset.
2550 In non-MIPS16 mode, prefer to do this even if the constant _can_ be
2551 forced into memory, as it usually produces better code. */
2552 split_const (src, &base, &offset);
2553 if (offset != const0_rtx
2554 && (targetm.cannot_force_const_mem (src)
2555 || (!TARGET_MIPS16 && can_create_pseudo_p ())))
2557 base = mips_force_temporary (dest, base);
2558 mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
2562 src = force_const_mem (mode, src);
2564 /* When using explicit relocs, constant pool references are sometimes
2565 not legitimate addresses. */
2566 mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
2567 mips_emit_move (dest, src);
2570 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
2571 sequence that is valid. */
2574 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
2576 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
2578 mips_emit_move (dest, force_reg (mode, src));
2582 /* Check for individual, fully-reloaded mflo and mfhi instructions. */
2583 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
2584 && REG_P (src) && MD_REG_P (REGNO (src))
2585 && REG_P (dest) && GP_REG_P (REGNO (dest)))
2587 int other_regno = REGNO (src) == HI_REGNUM ? LO_REGNUM : HI_REGNUM;
2588 if (GET_MODE_SIZE (mode) <= 4)
2589 emit_insn (gen_mfhilo_si (gen_lowpart (SImode, dest),
2590 gen_lowpart (SImode, src),
2591 gen_rtx_REG (SImode, other_regno)));
2593 emit_insn (gen_mfhilo_di (gen_lowpart (DImode, dest),
2594 gen_lowpart (DImode, src),
2595 gen_rtx_REG (DImode, other_regno)));
2599 /* We need to deal with constants that would be legitimate
2600 immediate_operands but aren't legitimate move_operands. */
2601 if (CONSTANT_P (src) && !move_operand (src, mode))
2603 mips_legitimize_const_move (mode, dest, src);
2604 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
2610 /* Return true if value X in context CONTEXT is a small-data address
2611 that can be rewritten as a LO_SUM. */
2614 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
2616 enum mips_symbol_type symbol_type;
2618 return (TARGET_EXPLICIT_RELOCS
2619 && mips_symbolic_constant_p (x, context, &symbol_type)
2620 && symbol_type == SYMBOL_GP_RELATIVE);
2623 /* A for_each_rtx callback for mips_small_data_pattern_p. DATA is the
2624 containing MEM, or null if none. */
2627 mips_small_data_pattern_1 (rtx *loc, void *data)
2629 enum mips_symbol_context context;
2631 if (GET_CODE (*loc) == LO_SUM)
2636 if (for_each_rtx (&XEXP (*loc, 0), mips_small_data_pattern_1, *loc))
2641 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
2642 return mips_rewrite_small_data_p (*loc, context);
2645 /* Return true if OP refers to small data symbols directly, not through
2649 mips_small_data_pattern_p (rtx op)
2651 return for_each_rtx (&op, mips_small_data_pattern_1, NULL);
2654 /* A for_each_rtx callback, used by mips_rewrite_small_data.
2655 DATA is the containing MEM, or null if none. */
2658 mips_rewrite_small_data_1 (rtx *loc, void *data)
2660 enum mips_symbol_context context;
2664 for_each_rtx (&XEXP (*loc, 0), mips_rewrite_small_data_1, *loc);
2668 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
2669 if (mips_rewrite_small_data_p (*loc, context))
2670 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
2672 if (GET_CODE (*loc) == LO_SUM)
2678 /* Rewrite instruction pattern PATTERN so that it refers to small data
2679 using explicit relocations. */
2682 mips_rewrite_small_data (rtx pattern)
2684 pattern = copy_insn (pattern);
2685 for_each_rtx (&pattern, mips_rewrite_small_data_1, NULL);
2689 /* We need a lot of little routines to check the range of MIPS16 immediate
2693 m16_check_op (rtx op, int low, int high, int mask)
2695 return (GET_CODE (op) == CONST_INT
2696 && IN_RANGE (INTVAL (op), low, high)
2697 && (INTVAL (op) & mask) == 0);
2701 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2703 return m16_check_op (op, 0x1, 0x8, 0);
2707 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2709 return m16_check_op (op, -0x8, 0x7, 0);
2713 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2715 return m16_check_op (op, -0x7, 0x8, 0);
2719 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2721 return m16_check_op (op, -0x10, 0xf, 0);
2725 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2727 return m16_check_op (op, -0xf, 0x10, 0);
2731 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2733 return m16_check_op (op, -0x10 << 2, 0xf << 2, 3);
2737 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2739 return m16_check_op (op, -0xf << 2, 0x10 << 2, 3);
2743 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2745 return m16_check_op (op, -0x80, 0x7f, 0);
2749 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2751 return m16_check_op (op, -0x7f, 0x80, 0);
2755 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2757 return m16_check_op (op, 0x0, 0xff, 0);
2761 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2763 return m16_check_op (op, -0xff, 0x0, 0);
2767 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2769 return m16_check_op (op, -0x1, 0xfe, 0);
2773 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2775 return m16_check_op (op, 0x0, 0xff << 2, 3);
2779 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2781 return m16_check_op (op, -0xff << 2, 0x0, 3);
2785 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2787 return m16_check_op (op, -0x80 << 3, 0x7f << 3, 7);
2791 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2793 return m16_check_op (op, -0x7f << 3, 0x80 << 3, 7);
2796 /* The cost of loading values from the constant pool. It should be
2797 larger than the cost of any constant we want to synthesize inline. */
2798 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
2800 /* Return the cost of X when used as an operand to the MIPS16 instruction
2801 that implements CODE. Return -1 if there is no such instruction, or if
2802 X is not a valid immediate operand for it. */
2805 mips16_constant_cost (int code, HOST_WIDE_INT x)
2812 /* Shifts by between 1 and 8 bits (inclusive) are unextended,
2813 other shifts are extended. The shift patterns truncate the shift
2814 count to the right size, so there are no out-of-range values. */
2815 if (IN_RANGE (x, 1, 8))
2817 return COSTS_N_INSNS (1);
2820 if (IN_RANGE (x, -128, 127))
2822 if (SMALL_OPERAND (x))
2823 return COSTS_N_INSNS (1);
2827 /* Like LE, but reject the always-true case. */
2831 /* We add 1 to the immediate and use SLT. */
2834 /* We can use CMPI for an xor with an unsigned 16-bit X. */
2837 if (IN_RANGE (x, 0, 255))
2839 if (SMALL_OPERAND_UNSIGNED (x))
2840 return COSTS_N_INSNS (1);
2845 /* Equality comparisons with 0 are cheap. */
2855 /* Return true if there is a non-MIPS16 instruction that implements CODE
2856 and if that instruction accepts X as an immediate operand. */
2859 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
2866 /* All shift counts are truncated to a valid constant. */
2871 /* Likewise rotates, if the target supports rotates at all. */
2877 /* These instructions take 16-bit unsigned immediates. */
2878 return SMALL_OPERAND_UNSIGNED (x);
2883 /* These instructions take 16-bit signed immediates. */
2884 return SMALL_OPERAND (x);
2890 /* The "immediate" forms of these instructions are really
2891 implemented as comparisons with register 0. */
2896 /* Likewise, meaning that the only valid immediate operand is 1. */
2900 /* We add 1 to the immediate and use SLT. */
2901 return SMALL_OPERAND (x + 1);
2904 /* Likewise SLTU, but reject the always-true case. */
2905 return SMALL_OPERAND (x + 1) && x + 1 != 0;
2909 /* The bit position and size are immediate operands. */
2910 return ISA_HAS_EXT_INS;
2913 /* By default assume that $0 can be used for 0. */
2918 /* Return the cost of binary operation X, given that the instruction
2919 sequence for a word-sized or smaller operation has cost SINGLE_COST
2920 and that the sequence of a double-word operation has cost DOUBLE_COST. */
2923 mips_binary_cost (rtx x, int single_cost, int double_cost)
2927 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
2932 + rtx_cost (XEXP (x, 0), 0)
2933 + rtx_cost (XEXP (x, 1), GET_CODE (x)));
2936 /* Return the cost of floating-point multiplications of mode MODE. */
2939 mips_fp_mult_cost (enum machine_mode mode)
2941 return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
2944 /* Return the cost of floating-point divisions of mode MODE. */
2947 mips_fp_div_cost (enum machine_mode mode)
2949 return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
2952 /* Return the cost of sign-extending OP to mode MODE, not including the
2953 cost of OP itself. */
2956 mips_sign_extend_cost (enum machine_mode mode, rtx op)
2959 /* Extended loads are as cheap as unextended ones. */
2962 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
2963 /* A sign extension from SImode to DImode in 64-bit mode is free. */
2966 if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
2967 /* We can use SEB or SEH. */
2968 return COSTS_N_INSNS (1);
2970 /* We need to use a shift left and a shift right. */
2971 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
2974 /* Return the cost of zero-extending OP to mode MODE, not including the
2975 cost of OP itself. */
2978 mips_zero_extend_cost (enum machine_mode mode, rtx op)
2981 /* Extended loads are as cheap as unextended ones. */
2984 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
2985 /* We need a shift left by 32 bits and a shift right by 32 bits. */
2986 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
2988 if (GENERATE_MIPS16E)
2989 /* We can use ZEB or ZEH. */
2990 return COSTS_N_INSNS (1);
2993 /* We need to load 0xff or 0xffff into a register and use AND. */
2994 return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
2996 /* We can use ANDI. */
2997 return COSTS_N_INSNS (1);
3000 /* Implement TARGET_RTX_COSTS. */
3003 mips_rtx_costs (rtx x, int code, int outer_code, int *total)
3005 enum machine_mode mode = GET_MODE (x);
3006 bool float_mode_p = FLOAT_MODE_P (mode);
3010 /* The cost of a COMPARE is hard to define for MIPS. COMPAREs don't
3011 appear in the instruction stream, and the cost of a comparison is
3012 really the cost of the branch or scc condition. At the time of
3013 writing, GCC only uses an explicit outer COMPARE code when optabs
3014 is testing whether a constant is expensive enough to force into a
3015 register. We want optabs to pass such constants through the MIPS
3016 expanders instead, so make all constants very cheap here. */
3017 if (outer_code == COMPARE)
3019 gcc_assert (CONSTANT_P (x));
3027 /* Treat *clear_upper32-style ANDs as having zero cost in the
3028 second operand. The cost is entirely in the first operand.
3030 ??? This is needed because we would otherwise try to CSE
3031 the constant operand. Although that's the right thing for
3032 instructions that continue to be a register operation throughout
3033 compilation, it is disastrous for instructions that could
3034 later be converted into a memory operation. */
3036 && outer_code == AND
3037 && UINTVAL (x) == 0xffffffff)
3045 cost = mips16_constant_cost (outer_code, INTVAL (x));
3054 /* When not optimizing for size, we care more about the cost
3055 of hot code, and hot code is often in a loop. If a constant
3056 operand needs to be forced into a register, we will often be
3057 able to hoist the constant load out of the loop, so the load
3058 should not contribute to the cost. */
3060 || mips_immediate_operand_p (outer_code, INTVAL (x)))
3072 if (force_to_mem_operand (x, VOIDmode))
3074 *total = COSTS_N_INSNS (1);
3077 cost = mips_const_insns (x);
3080 /* If the constant is likely to be stored in a GPR, SETs of
3081 single-insn constants are as cheap as register sets; we
3082 never want to CSE them.
3084 Don't reduce the cost of storing a floating-point zero in
3085 FPRs. If we have a zero in an FPR for other reasons, we
3086 can get better cfg-cleanup and delayed-branch results by
3087 using it consistently, rather than using $0 sometimes and
3088 an FPR at other times. Also, moves between floating-point
3089 registers are sometimes cheaper than (D)MTC1 $0. */
3091 && outer_code == SET
3092 && !(float_mode_p && TARGET_HARD_FLOAT))
3094 /* When non-MIPS16 code loads a constant N>1 times, we rarely
3095 want to CSE the constant itself. It is usually better to
3096 have N copies of the last operation in the sequence and one
3097 shared copy of the other operations. (Note that this is
3098 not true for MIPS16 code, where the final operation in the
3099 sequence is often an extended instruction.)
3101 Also, if we have a CONST_INT, we don't know whether it is
3102 for a word or doubleword operation, so we cannot rely on
3103 the result of mips_build_integer. */
3104 else if (!TARGET_MIPS16
3105 && (outer_code == SET || mode == VOIDmode))
3107 *total = COSTS_N_INSNS (cost);
3110 /* The value will need to be fetched from the constant pool. */
3111 *total = CONSTANT_POOL_COST;
3115 /* If the address is legitimate, return the number of
3116 instructions it needs. */
3118 cost = mips_address_insns (addr, mode, true);
3121 *total = COSTS_N_INSNS (cost + 1);
3124 /* Check for a scaled indexed address. */
3125 if (mips_lwxs_address_p (addr))
3127 *total = COSTS_N_INSNS (2);
3130 /* Otherwise use the default handling. */
3134 *total = COSTS_N_INSNS (6);
3138 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3142 /* Check for a *clear_upper32 pattern and treat it like a zero
3143 extension. See the pattern's comment for details. */
3146 && CONST_INT_P (XEXP (x, 1))
3147 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3149 *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3150 + rtx_cost (XEXP (x, 0), 0));
3157 /* Double-word operations use two single-word operations. */
3158 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2));
3166 if (CONSTANT_P (XEXP (x, 1)))
3167 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4));
3169 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12));
3174 *total = mips_cost->fp_add;
3176 *total = COSTS_N_INSNS (4);
3180 /* Low-part immediates need an extended MIPS16 instruction. */
3181 *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3182 + rtx_cost (XEXP (x, 0), 0));
3197 /* Branch comparisons have VOIDmode, so use the first operand's
3199 mode = GET_MODE (XEXP (x, 0));
3200 if (FLOAT_MODE_P (mode))
3202 *total = mips_cost->fp_add;
3205 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4));
3210 && ISA_HAS_NMADD_NMSUB (mode)
3211 && TARGET_FUSED_MADD
3212 && !HONOR_NANS (mode)
3213 && !HONOR_SIGNED_ZEROS (mode))
3215 /* See if we can use NMADD or NMSUB. See mips.md for the
3216 associated patterns. */
3217 rtx op0 = XEXP (x, 0);
3218 rtx op1 = XEXP (x, 1);
3219 if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
3221 *total = (mips_fp_mult_cost (mode)
3222 + rtx_cost (XEXP (XEXP (op0, 0), 0), 0)
3223 + rtx_cost (XEXP (op0, 1), 0)
3224 + rtx_cost (op1, 0));
3227 if (GET_CODE (op1) == MULT)
3229 *total = (mips_fp_mult_cost (mode)
3231 + rtx_cost (XEXP (op1, 0), 0)
3232 + rtx_cost (XEXP (op1, 1), 0));
3241 /* If this is part of a MADD or MSUB, treat the PLUS as
3244 && TARGET_FUSED_MADD
3245 && GET_CODE (XEXP (x, 0)) == MULT)
3248 *total = mips_cost->fp_add;
3252 /* Double-word operations require three single-word operations and
3253 an SLTU. The MIPS16 version then needs to move the result of
3254 the SLTU from $24 to a MIPS16 register. */
3255 *total = mips_binary_cost (x, COSTS_N_INSNS (1),
3256 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4));
3261 && ISA_HAS_NMADD_NMSUB (mode)
3262 && TARGET_FUSED_MADD
3263 && !HONOR_NANS (mode)
3264 && HONOR_SIGNED_ZEROS (mode))
3266 /* See if we can use NMADD or NMSUB. See mips.md for the
3267 associated patterns. */
3268 rtx op = XEXP (x, 0);
3269 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3270 && GET_CODE (XEXP (op, 0)) == MULT)
3272 *total = (mips_fp_mult_cost (mode)
3273 + rtx_cost (XEXP (XEXP (op, 0), 0), 0)
3274 + rtx_cost (XEXP (XEXP (op, 0), 1), 0)
3275 + rtx_cost (XEXP (op, 1), 0));
3281 *total = mips_cost->fp_add;
3283 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
3288 *total = mips_fp_mult_cost (mode);
3289 else if (mode == DImode && !TARGET_64BIT)
3290 /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
3291 where the mulsidi3 always includes an MFHI and an MFLO. */
3292 *total = (optimize_size
3293 ? COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9)
3294 : mips_cost->int_mult_si * 3 + 6);
3295 else if (optimize_size)
3296 *total = (ISA_HAS_MUL3 ? 1 : 2);
3297 else if (mode == DImode)
3298 *total = mips_cost->int_mult_di;
3300 *total = mips_cost->int_mult_si;
3304 /* Check for a reciprocal. */
3307 && flag_unsafe_math_optimizations
3308 && XEXP (x, 0) == CONST1_RTX (mode))
3310 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
3311 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
3312 division as being free. */
3313 *total = rtx_cost (XEXP (x, 1), 0);
3315 *total = mips_fp_div_cost (mode) + rtx_cost (XEXP (x, 1), 0);
3324 *total = mips_fp_div_cost (mode);
3333 /* It is our responsibility to make division by a power of 2
3334 as cheap as 2 register additions if we want the division
3335 expanders to be used for such operations; see the setting
3336 of sdiv_pow2_cheap in optabs.c. Using (D)DIV for MIPS16
3337 should always produce shorter code than using
3338 expand_sdiv2_pow2. */
3340 && CONST_INT_P (XEXP (x, 1))
3341 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
3343 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), 0);
3346 *total = COSTS_N_INSNS (mips_idiv_insns ());
3348 else if (mode == DImode)
3349 *total = mips_cost->int_div_di;
3351 *total = mips_cost->int_div_si;
3355 *total = mips_sign_extend_cost (mode, XEXP (x, 0));
3359 *total = mips_zero_extend_cost (mode, XEXP (x, 0));
3363 case UNSIGNED_FLOAT:
3366 case FLOAT_TRUNCATE:
3367 *total = mips_cost->fp_add;
3375 /* Implement TARGET_ADDRESS_COST. */
3378 mips_address_cost (rtx addr)
3380 return mips_address_insns (addr, SImode, false);
3383 /* Return one word of double-word value OP, taking into account the fixed
3384 endianness of certain registers. HIGH_P is true to select the high part,
3385 false to select the low part. */
3388 mips_subword (rtx op, bool high_p)
3390 unsigned int byte, offset;
3391 enum machine_mode mode;
3393 mode = GET_MODE (op);
3394 if (mode == VOIDmode)
3397 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
3398 byte = UNITS_PER_WORD;
3402 if (FP_REG_RTX_P (op))
3404 /* Paired FPRs are always ordered little-endian. */
3405 offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
3406 return gen_rtx_REG (word_mode, REGNO (op) + offset);
3410 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
3412 return simplify_gen_subreg (word_mode, op, mode, byte);
3415 /* Return true if a 64-bit move from SRC to DEST should be split into two. */
3418 mips_split_64bit_move_p (rtx dest, rtx src)
3423 /* FPR-to-FPR moves can be done in a single instruction, if they're
3425 if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
3428 /* Check for floating-point loads and stores. */
3429 if (ISA_HAS_LDC1_SDC1)
3431 if (FP_REG_RTX_P (dest) && MEM_P (src))
3433 if (FP_REG_RTX_P (src) && MEM_P (dest))
3439 /* Split a doubleword move from SRC to DEST. On 32-bit targets,
3440 this function handles 64-bit moves for which mips_split_64bit_move_p
3441 holds. For 64-bit targets, this function handles 128-bit moves. */
3444 mips_split_doubleword_move (rtx dest, rtx src)