* integrate.c: Remove.
[official-gcc.git] / gcc / config / mips / mips.c
blob122bc98efa0017fd0163a326c76e7b9d6fedccae
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, 2008, 2009, 2010,
4 2011, 2012
5 Free Software Foundation, Inc.
6 Contributed by A. Lichnewsky, lich@inria.inria.fr.
7 Changes by Michael Meissner, meissner@osf.org.
8 64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
9 Brendan Eich, brendan@microunity.com.
11 This file is part of GCC.
13 GCC is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 3, or (at your option)
16 any later version.
18 GCC is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GCC; see the file COPYING3. If not see
25 <http://www.gnu.org/licenses/>. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "rtl.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "insn-attr.h"
37 #include "recog.h"
38 #include "output.h"
39 #include "tree.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "libfuncs.h"
44 #include "flags.h"
45 #include "reload.h"
46 #include "tm_p.h"
47 #include "ggc.h"
48 #include "gstab.h"
49 #include "hashtab.h"
50 #include "debug.h"
51 #include "target.h"
52 #include "target-def.h"
53 #include "langhooks.h"
54 #include "cfglayout.h"
55 #include "sched-int.h"
56 #include "gimple.h"
57 #include "bitmap.h"
58 #include "diagnostic.h"
59 #include "target-globals.h"
60 #include "opts.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) \
70 XVECEXP (X, 0, 0)
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
91 part of the frame. */
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) \
99 (NONDEBUG_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) \
110 : (INSN))
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) \
116 : (INSN))
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 /* Return the opcode for a ptr_mode load of the form:
130 l[wd] DEST, OFFSET(BASE). */
131 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE) \
132 (((ptr_mode == DImode ? 0x37 : 0x23) << 26) \
133 | ((BASE) << 21) \
134 | ((DEST) << 16) \
135 | (OFFSET))
137 /* Return the opcode to move register SRC into register DEST. */
138 #define MIPS_MOVE(DEST, SRC) \
139 ((TARGET_64BIT ? 0x2d : 0x21) \
140 | ((DEST) << 11) \
141 | ((SRC) << 21))
143 /* Return the opcode for:
145 lui DEST, VALUE. */
146 #define MIPS_LUI(DEST, VALUE) \
147 ((0xf << 26) | ((DEST) << 16) | (VALUE))
149 /* Return the opcode to jump to register DEST. */
150 #define MIPS_JR(DEST) \
151 (((DEST) << 21) | 0x8)
153 /* Return the opcode for:
155 bal . + (1 + OFFSET) * 4. */
156 #define MIPS_BAL(OFFSET) \
157 ((0x1 << 26) | (0x11 << 16) | (OFFSET))
159 /* Return the usual opcode for a nop. */
160 #define MIPS_NOP 0
162 /* Classifies an address.
164 ADDRESS_REG
165 A natural register + offset address. The register satisfies
166 mips_valid_base_register_p and the offset is a const_arith_operand.
168 ADDRESS_LO_SUM
169 A LO_SUM rtx. The first operand is a valid base register and
170 the second operand is a symbolic address.
172 ADDRESS_CONST_INT
173 A signed 16-bit constant address.
175 ADDRESS_SYMBOLIC:
176 A constant symbolic address. */
177 enum mips_address_type {
178 ADDRESS_REG,
179 ADDRESS_LO_SUM,
180 ADDRESS_CONST_INT,
181 ADDRESS_SYMBOLIC
184 /* Macros to create an enumeration identifier for a function prototype. */
185 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
186 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
187 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
188 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
190 /* Classifies the prototype of a built-in function. */
191 enum mips_function_type {
192 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
193 #include "config/mips/mips-ftypes.def"
194 #undef DEF_MIPS_FTYPE
195 MIPS_MAX_FTYPE_MAX
198 /* Specifies how a built-in function should be converted into rtl. */
199 enum mips_builtin_type {
200 /* The function corresponds directly to an .md pattern. The return
201 value is mapped to operand 0 and the arguments are mapped to
202 operands 1 and above. */
203 MIPS_BUILTIN_DIRECT,
205 /* The function corresponds directly to an .md pattern. There is no return
206 value and the arguments are mapped to operands 0 and above. */
207 MIPS_BUILTIN_DIRECT_NO_TARGET,
209 /* The function corresponds to a comparison instruction followed by
210 a mips_cond_move_tf_ps pattern. The first two arguments are the
211 values to compare and the second two arguments are the vector
212 operands for the movt.ps or movf.ps instruction (in assembly order). */
213 MIPS_BUILTIN_MOVF,
214 MIPS_BUILTIN_MOVT,
216 /* The function corresponds to a V2SF comparison instruction. Operand 0
217 of this instruction is the result of the comparison, which has mode
218 CCV2 or CCV4. The function arguments are mapped to operands 1 and
219 above. The function's return value is an SImode boolean that is
220 true under the following conditions:
222 MIPS_BUILTIN_CMP_ANY: one of the registers is true
223 MIPS_BUILTIN_CMP_ALL: all of the registers are true
224 MIPS_BUILTIN_CMP_LOWER: the first register is true
225 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
226 MIPS_BUILTIN_CMP_ANY,
227 MIPS_BUILTIN_CMP_ALL,
228 MIPS_BUILTIN_CMP_UPPER,
229 MIPS_BUILTIN_CMP_LOWER,
231 /* As above, but the instruction only sets a single $fcc register. */
232 MIPS_BUILTIN_CMP_SINGLE,
234 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
235 MIPS_BUILTIN_BPOSGE32
238 /* Invoke MACRO (COND) for each C.cond.fmt condition. */
239 #define MIPS_FP_CONDITIONS(MACRO) \
240 MACRO (f), \
241 MACRO (un), \
242 MACRO (eq), \
243 MACRO (ueq), \
244 MACRO (olt), \
245 MACRO (ult), \
246 MACRO (ole), \
247 MACRO (ule), \
248 MACRO (sf), \
249 MACRO (ngle), \
250 MACRO (seq), \
251 MACRO (ngl), \
252 MACRO (lt), \
253 MACRO (nge), \
254 MACRO (le), \
255 MACRO (ngt)
257 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
258 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
259 enum mips_fp_condition {
260 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
263 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
264 #define STRINGIFY(X) #X
265 static const char *const mips_fp_conditions[] = {
266 MIPS_FP_CONDITIONS (STRINGIFY)
269 /* Information about a function's frame layout. */
270 struct GTY(()) mips_frame_info {
271 /* The size of the frame in bytes. */
272 HOST_WIDE_INT total_size;
274 /* The number of bytes allocated to variables. */
275 HOST_WIDE_INT var_size;
277 /* The number of bytes allocated to outgoing function arguments. */
278 HOST_WIDE_INT args_size;
280 /* The number of bytes allocated to the .cprestore slot, or 0 if there
281 is no such slot. */
282 HOST_WIDE_INT cprestore_size;
284 /* Bit X is set if the function saves or restores GPR X. */
285 unsigned int mask;
287 /* Likewise FPR X. */
288 unsigned int fmask;
290 /* Likewise doubleword accumulator X ($acX). */
291 unsigned int acc_mask;
293 /* The number of GPRs, FPRs, doubleword accumulators and COP0
294 registers saved. */
295 unsigned int num_gp;
296 unsigned int num_fp;
297 unsigned int num_acc;
298 unsigned int num_cop0_regs;
300 /* The offset of the topmost GPR, FPR, accumulator and COP0-register
301 save slots from the top of the frame, or zero if no such slots are
302 needed. */
303 HOST_WIDE_INT gp_save_offset;
304 HOST_WIDE_INT fp_save_offset;
305 HOST_WIDE_INT acc_save_offset;
306 HOST_WIDE_INT cop0_save_offset;
308 /* Likewise, but giving offsets from the bottom of the frame. */
309 HOST_WIDE_INT gp_sp_offset;
310 HOST_WIDE_INT fp_sp_offset;
311 HOST_WIDE_INT acc_sp_offset;
312 HOST_WIDE_INT cop0_sp_offset;
314 /* Similar, but the value passed to _mcount. */
315 HOST_WIDE_INT ra_fp_offset;
317 /* The offset of arg_pointer_rtx from the bottom of the frame. */
318 HOST_WIDE_INT arg_pointer_offset;
320 /* The offset of hard_frame_pointer_rtx from the bottom of the frame. */
321 HOST_WIDE_INT hard_frame_pointer_offset;
324 struct GTY(()) machine_function {
325 /* The register returned by mips16_gp_pseudo_reg; see there for details. */
326 rtx mips16_gp_pseudo_rtx;
328 /* The number of extra stack bytes taken up by register varargs.
329 This area is allocated by the callee at the very top of the frame. */
330 int varargs_size;
332 /* The current frame information, calculated by mips_compute_frame_info. */
333 struct mips_frame_info frame;
335 /* The register to use as the function's global pointer, or INVALID_REGNUM
336 if the function doesn't need one. */
337 unsigned int global_pointer;
339 /* How many instructions it takes to load a label into $AT, or 0 if
340 this property hasn't yet been calculated. */
341 unsigned int load_label_num_insns;
343 /* True if mips_adjust_insn_length should ignore an instruction's
344 hazard attribute. */
345 bool ignore_hazard_length_p;
347 /* True if the whole function is suitable for .set noreorder and
348 .set nomacro. */
349 bool all_noreorder_p;
351 /* True if the function has "inflexible" and "flexible" references
352 to the global pointer. See mips_cfun_has_inflexible_gp_ref_p
353 and mips_cfun_has_flexible_gp_ref_p for details. */
354 bool has_inflexible_gp_insn_p;
355 bool has_flexible_gp_insn_p;
357 /* True if the function's prologue must load the global pointer
358 value into pic_offset_table_rtx and store the same value in
359 the function's cprestore slot (if any). Even if this value
360 is currently false, we may decide to set it to true later;
361 see mips_must_initialize_gp_p () for details. */
362 bool must_initialize_gp_p;
364 /* True if the current function must restore $gp after any potential
365 clobber. This value is only meaningful during the first post-epilogue
366 split_insns pass; see mips_must_initialize_gp_p () for details. */
367 bool must_restore_gp_when_clobbered_p;
369 /* True if this is an interrupt handler. */
370 bool interrupt_handler_p;
372 /* True if this is an interrupt handler that uses shadow registers. */
373 bool use_shadow_register_set_p;
375 /* True if this is an interrupt handler that should keep interrupts
376 masked. */
377 bool keep_interrupts_masked_p;
379 /* True if this is an interrupt handler that should use DERET
380 instead of ERET. */
381 bool use_debug_exception_return_p;
384 /* Information about a single argument. */
385 struct mips_arg_info {
386 /* True if the argument is passed in a floating-point register, or
387 would have been if we hadn't run out of registers. */
388 bool fpr_p;
390 /* The number of words passed in registers, rounded up. */
391 unsigned int reg_words;
393 /* For EABI, the offset of the first register from GP_ARG_FIRST or
394 FP_ARG_FIRST. For other ABIs, the offset of the first register from
395 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
396 comment for details).
398 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
399 on the stack. */
400 unsigned int reg_offset;
402 /* The number of words that must be passed on the stack, rounded up. */
403 unsigned int stack_words;
405 /* The offset from the start of the stack overflow area of the argument's
406 first stack word. Only meaningful when STACK_WORDS is nonzero. */
407 unsigned int stack_offset;
410 /* Information about an address described by mips_address_type.
412 ADDRESS_CONST_INT
413 No fields are used.
415 ADDRESS_REG
416 REG is the base register and OFFSET is the constant offset.
418 ADDRESS_LO_SUM
419 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
420 is the type of symbol it references.
422 ADDRESS_SYMBOLIC
423 SYMBOL_TYPE is the type of symbol that the address references. */
424 struct mips_address_info {
425 enum mips_address_type type;
426 rtx reg;
427 rtx offset;
428 enum mips_symbol_type symbol_type;
431 /* One stage in a constant building sequence. These sequences have
432 the form:
434 A = VALUE[0]
435 A = A CODE[1] VALUE[1]
436 A = A CODE[2] VALUE[2]
439 where A is an accumulator, each CODE[i] is a binary rtl operation
440 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
441 struct mips_integer_op {
442 enum rtx_code code;
443 unsigned HOST_WIDE_INT value;
446 /* The largest number of operations needed to load an integer constant.
447 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
448 When the lowest bit is clear, we can try, but reject a sequence with
449 an extra SLL at the end. */
450 #define MIPS_MAX_INTEGER_OPS 7
452 /* Information about a MIPS16e SAVE or RESTORE instruction. */
453 struct mips16e_save_restore_info {
454 /* The number of argument registers saved by a SAVE instruction.
455 0 for RESTORE instructions. */
456 unsigned int nargs;
458 /* Bit X is set if the instruction saves or restores GPR X. */
459 unsigned int mask;
461 /* The total number of bytes to allocate. */
462 HOST_WIDE_INT size;
465 /* Costs of various operations on the different architectures. */
467 struct mips_rtx_cost_data
469 unsigned short fp_add;
470 unsigned short fp_mult_sf;
471 unsigned short fp_mult_df;
472 unsigned short fp_div_sf;
473 unsigned short fp_div_df;
474 unsigned short int_mult_si;
475 unsigned short int_mult_di;
476 unsigned short int_div_si;
477 unsigned short int_div_di;
478 unsigned short branch_cost;
479 unsigned short memory_latency;
482 /* Global variables for machine-dependent things. */
484 /* The -G setting, or the configuration's default small-data limit if
485 no -G option is given. */
486 static unsigned int mips_small_data_threshold;
488 /* The number of file directives written by mips_output_filename. */
489 int num_source_filenames;
491 /* The name that appeared in the last .file directive written by
492 mips_output_filename, or "" if mips_output_filename hasn't
493 written anything yet. */
494 const char *current_function_file = "";
496 /* Arrays that map GCC register numbers to debugger register numbers. */
497 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
498 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
500 /* Information about the current function's epilogue, used only while
501 expanding it. */
502 static struct {
503 /* A list of queued REG_CFA_RESTORE notes. */
504 rtx cfa_restores;
506 /* The CFA is currently defined as CFA_REG + CFA_OFFSET. */
507 rtx cfa_reg;
508 HOST_WIDE_INT cfa_offset;
510 /* The offset of the CFA from the stack pointer while restoring
511 registers. */
512 HOST_WIDE_INT cfa_restore_sp_offset;
513 } mips_epilogue;
515 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs. */
516 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
517 struct mips_asm_switch mips_nomacro = { "macro", 0 };
518 struct mips_asm_switch mips_noat = { "at", 0 };
520 /* True if we're writing out a branch-likely instruction rather than a
521 normal branch. */
522 static bool mips_branch_likely;
524 /* The current instruction-set architecture. */
525 enum processor mips_arch;
526 const struct mips_cpu_info *mips_arch_info;
528 /* The processor that we should tune the code for. */
529 enum processor mips_tune;
530 const struct mips_cpu_info *mips_tune_info;
532 /* The ISA level associated with mips_arch. */
533 int mips_isa;
535 /* The architecture selected by -mipsN, or null if -mipsN wasn't used. */
536 static const struct mips_cpu_info *mips_isa_option_info;
538 /* Which cost information to use. */
539 static const struct mips_rtx_cost_data *mips_cost;
541 /* The ambient target flags, excluding MASK_MIPS16. */
542 static int mips_base_target_flags;
544 /* True if MIPS16 is the default mode. */
545 bool mips_base_mips16;
547 /* The ambient values of other global variables. */
548 static int mips_base_schedule_insns; /* flag_schedule_insns */
549 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
550 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
551 static int mips_base_align_loops; /* align_loops */
552 static int mips_base_align_jumps; /* align_jumps */
553 static int mips_base_align_functions; /* align_functions */
555 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
556 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
558 /* Index C is true if character C is a valid PRINT_OPERAND punctation
559 character. */
560 static bool mips_print_operand_punct[256];
562 static GTY (()) int mips_output_filename_first_time = 1;
564 /* mips_split_p[X] is true if symbols of type X can be split by
565 mips_split_symbol. */
566 bool mips_split_p[NUM_SYMBOL_TYPES];
568 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
569 can be split by mips_split_symbol. */
570 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
572 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
573 forced into a PC-relative constant pool. */
574 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
576 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
577 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
578 if they are matched by a special .md file pattern. */
579 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
581 /* Likewise for HIGHs. */
582 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
584 /* Target state for MIPS16. */
585 struct target_globals *mips16_globals;
587 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
588 and returned from mips_sched_reorder2. */
589 static int cached_can_issue_more;
591 /* True if the output uses __mips16_rdhwr. */
592 static bool mips_need_mips16_rdhwr_p;
594 /* Index R is the smallest register class that contains register R. */
595 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
596 LEA_REGS, LEA_REGS, M16_REGS, V1_REG,
597 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
598 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
599 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
600 M16_REGS, M16_REGS, LEA_REGS, LEA_REGS,
601 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
602 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
603 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
604 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
605 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
606 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
607 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
608 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
609 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
610 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
611 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
612 MD0_REG, MD1_REG, NO_REGS, ST_REGS,
613 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
614 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
615 NO_REGS, FRAME_REGS, FRAME_REGS, NO_REGS,
616 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
617 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
618 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
619 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
620 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
621 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
622 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
623 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
624 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
625 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
626 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
627 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
628 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
629 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
630 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
631 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
632 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
633 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
634 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
635 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
636 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
637 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
638 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
639 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
640 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
641 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
642 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
645 /* The value of TARGET_ATTRIBUTE_TABLE. */
646 static const struct attribute_spec mips_attribute_table[] = {
647 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
648 om_diagnostic } */
649 { "long_call", 0, 0, false, true, true, NULL, false },
650 { "far", 0, 0, false, true, true, NULL, false },
651 { "near", 0, 0, false, true, true, NULL, false },
652 /* We would really like to treat "mips16" and "nomips16" as type
653 attributes, but GCC doesn't provide the hooks we need to support
654 the right conversion rules. As declaration attributes, they affect
655 code generation but don't carry other semantics. */
656 { "mips16", 0, 0, true, false, false, NULL, false },
657 { "nomips16", 0, 0, true, false, false, NULL, false },
658 /* Allow functions to be specified as interrupt handlers */
659 { "interrupt", 0, 0, false, true, true, NULL, false },
660 { "use_shadow_register_set", 0, 0, false, true, true, NULL, false },
661 { "keep_interrupts_masked", 0, 0, false, true, true, NULL, false },
662 { "use_debug_exception_return", 0, 0, false, true, true, NULL, false },
663 { NULL, 0, 0, false, false, false, NULL, false }
666 /* A table describing all the processors GCC knows about; see
667 mips-cpus.def for details. */
668 static const struct mips_cpu_info mips_cpu_info_table[] = {
669 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
670 { NAME, CPU, ISA, FLAGS },
671 #include "mips-cpus.def"
672 #undef MIPS_CPU
675 /* Default costs. If these are used for a processor we should look
676 up the actual costs. */
677 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
678 COSTS_N_INSNS (7), /* fp_mult_sf */ \
679 COSTS_N_INSNS (8), /* fp_mult_df */ \
680 COSTS_N_INSNS (23), /* fp_div_sf */ \
681 COSTS_N_INSNS (36), /* fp_div_df */ \
682 COSTS_N_INSNS (10), /* int_mult_si */ \
683 COSTS_N_INSNS (10), /* int_mult_di */ \
684 COSTS_N_INSNS (69), /* int_div_si */ \
685 COSTS_N_INSNS (69), /* int_div_di */ \
686 2, /* branch_cost */ \
687 4 /* memory_latency */
689 /* Floating-point costs for processors without an FPU. Just assume that
690 all floating-point libcalls are very expensive. */
691 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
692 COSTS_N_INSNS (256), /* fp_mult_sf */ \
693 COSTS_N_INSNS (256), /* fp_mult_df */ \
694 COSTS_N_INSNS (256), /* fp_div_sf */ \
695 COSTS_N_INSNS (256) /* fp_div_df */
697 /* Costs to use when optimizing for size. */
698 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
699 COSTS_N_INSNS (1), /* fp_add */
700 COSTS_N_INSNS (1), /* fp_mult_sf */
701 COSTS_N_INSNS (1), /* fp_mult_df */
702 COSTS_N_INSNS (1), /* fp_div_sf */
703 COSTS_N_INSNS (1), /* fp_div_df */
704 COSTS_N_INSNS (1), /* int_mult_si */
705 COSTS_N_INSNS (1), /* int_mult_di */
706 COSTS_N_INSNS (1), /* int_div_si */
707 COSTS_N_INSNS (1), /* int_div_di */
708 2, /* branch_cost */
709 4 /* memory_latency */
712 /* Costs to use when optimizing for speed, indexed by processor. */
713 static const struct mips_rtx_cost_data
714 mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
715 { /* R3000 */
716 COSTS_N_INSNS (2), /* fp_add */
717 COSTS_N_INSNS (4), /* fp_mult_sf */
718 COSTS_N_INSNS (5), /* fp_mult_df */
719 COSTS_N_INSNS (12), /* fp_div_sf */
720 COSTS_N_INSNS (19), /* fp_div_df */
721 COSTS_N_INSNS (12), /* int_mult_si */
722 COSTS_N_INSNS (12), /* int_mult_di */
723 COSTS_N_INSNS (35), /* int_div_si */
724 COSTS_N_INSNS (35), /* int_div_di */
725 1, /* branch_cost */
726 4 /* memory_latency */
728 { /* 4KC */
729 SOFT_FP_COSTS,
730 COSTS_N_INSNS (6), /* int_mult_si */
731 COSTS_N_INSNS (6), /* int_mult_di */
732 COSTS_N_INSNS (36), /* int_div_si */
733 COSTS_N_INSNS (36), /* int_div_di */
734 1, /* branch_cost */
735 4 /* memory_latency */
737 { /* 4KP */
738 SOFT_FP_COSTS,
739 COSTS_N_INSNS (36), /* int_mult_si */
740 COSTS_N_INSNS (36), /* int_mult_di */
741 COSTS_N_INSNS (37), /* int_div_si */
742 COSTS_N_INSNS (37), /* int_div_di */
743 1, /* branch_cost */
744 4 /* memory_latency */
746 { /* 5KC */
747 SOFT_FP_COSTS,
748 COSTS_N_INSNS (4), /* int_mult_si */
749 COSTS_N_INSNS (11), /* int_mult_di */
750 COSTS_N_INSNS (36), /* int_div_si */
751 COSTS_N_INSNS (68), /* int_div_di */
752 1, /* branch_cost */
753 4 /* memory_latency */
755 { /* 5KF */
756 COSTS_N_INSNS (4), /* fp_add */
757 COSTS_N_INSNS (4), /* fp_mult_sf */
758 COSTS_N_INSNS (5), /* fp_mult_df */
759 COSTS_N_INSNS (17), /* fp_div_sf */
760 COSTS_N_INSNS (32), /* fp_div_df */
761 COSTS_N_INSNS (4), /* int_mult_si */
762 COSTS_N_INSNS (11), /* int_mult_di */
763 COSTS_N_INSNS (36), /* int_div_si */
764 COSTS_N_INSNS (68), /* int_div_di */
765 1, /* branch_cost */
766 4 /* memory_latency */
768 { /* 20KC */
769 COSTS_N_INSNS (4), /* fp_add */
770 COSTS_N_INSNS (4), /* fp_mult_sf */
771 COSTS_N_INSNS (5), /* fp_mult_df */
772 COSTS_N_INSNS (17), /* fp_div_sf */
773 COSTS_N_INSNS (32), /* fp_div_df */
774 COSTS_N_INSNS (4), /* int_mult_si */
775 COSTS_N_INSNS (7), /* int_mult_di */
776 COSTS_N_INSNS (42), /* int_div_si */
777 COSTS_N_INSNS (72), /* int_div_di */
778 1, /* branch_cost */
779 4 /* memory_latency */
781 { /* 24KC */
782 SOFT_FP_COSTS,
783 COSTS_N_INSNS (5), /* int_mult_si */
784 COSTS_N_INSNS (5), /* int_mult_di */
785 COSTS_N_INSNS (41), /* int_div_si */
786 COSTS_N_INSNS (41), /* int_div_di */
787 1, /* branch_cost */
788 4 /* memory_latency */
790 { /* 24KF2_1 */
791 COSTS_N_INSNS (8), /* fp_add */
792 COSTS_N_INSNS (8), /* fp_mult_sf */
793 COSTS_N_INSNS (10), /* fp_mult_df */
794 COSTS_N_INSNS (34), /* fp_div_sf */
795 COSTS_N_INSNS (64), /* fp_div_df */
796 COSTS_N_INSNS (5), /* int_mult_si */
797 COSTS_N_INSNS (5), /* int_mult_di */
798 COSTS_N_INSNS (41), /* int_div_si */
799 COSTS_N_INSNS (41), /* int_div_di */
800 1, /* branch_cost */
801 4 /* memory_latency */
803 { /* 24KF1_1 */
804 COSTS_N_INSNS (4), /* fp_add */
805 COSTS_N_INSNS (4), /* fp_mult_sf */
806 COSTS_N_INSNS (5), /* fp_mult_df */
807 COSTS_N_INSNS (17), /* fp_div_sf */
808 COSTS_N_INSNS (32), /* fp_div_df */
809 COSTS_N_INSNS (5), /* int_mult_si */
810 COSTS_N_INSNS (5), /* int_mult_di */
811 COSTS_N_INSNS (41), /* int_div_si */
812 COSTS_N_INSNS (41), /* int_div_di */
813 1, /* branch_cost */
814 4 /* memory_latency */
816 { /* 74KC */
817 SOFT_FP_COSTS,
818 COSTS_N_INSNS (5), /* int_mult_si */
819 COSTS_N_INSNS (5), /* int_mult_di */
820 COSTS_N_INSNS (41), /* int_div_si */
821 COSTS_N_INSNS (41), /* int_div_di */
822 1, /* branch_cost */
823 4 /* memory_latency */
825 { /* 74KF2_1 */
826 COSTS_N_INSNS (8), /* fp_add */
827 COSTS_N_INSNS (8), /* fp_mult_sf */
828 COSTS_N_INSNS (10), /* fp_mult_df */
829 COSTS_N_INSNS (34), /* fp_div_sf */
830 COSTS_N_INSNS (64), /* fp_div_df */
831 COSTS_N_INSNS (5), /* int_mult_si */
832 COSTS_N_INSNS (5), /* int_mult_di */
833 COSTS_N_INSNS (41), /* int_div_si */
834 COSTS_N_INSNS (41), /* int_div_di */
835 1, /* branch_cost */
836 4 /* memory_latency */
838 { /* 74KF1_1 */
839 COSTS_N_INSNS (4), /* fp_add */
840 COSTS_N_INSNS (4), /* fp_mult_sf */
841 COSTS_N_INSNS (5), /* fp_mult_df */
842 COSTS_N_INSNS (17), /* fp_div_sf */
843 COSTS_N_INSNS (32), /* fp_div_df */
844 COSTS_N_INSNS (5), /* int_mult_si */
845 COSTS_N_INSNS (5), /* int_mult_di */
846 COSTS_N_INSNS (41), /* int_div_si */
847 COSTS_N_INSNS (41), /* int_div_di */
848 1, /* branch_cost */
849 4 /* memory_latency */
851 { /* 74KF3_2 */
852 COSTS_N_INSNS (6), /* fp_add */
853 COSTS_N_INSNS (6), /* fp_mult_sf */
854 COSTS_N_INSNS (7), /* fp_mult_df */
855 COSTS_N_INSNS (25), /* fp_div_sf */
856 COSTS_N_INSNS (48), /* fp_div_df */
857 COSTS_N_INSNS (5), /* int_mult_si */
858 COSTS_N_INSNS (5), /* int_mult_di */
859 COSTS_N_INSNS (41), /* int_div_si */
860 COSTS_N_INSNS (41), /* int_div_di */
861 1, /* branch_cost */
862 4 /* memory_latency */
864 { /* Loongson-2E */
865 DEFAULT_COSTS
867 { /* Loongson-2F */
868 DEFAULT_COSTS
870 { /* Loongson-3A */
871 DEFAULT_COSTS
873 { /* M4k */
874 DEFAULT_COSTS
876 /* Octeon */
878 SOFT_FP_COSTS,
879 COSTS_N_INSNS (5), /* int_mult_si */
880 COSTS_N_INSNS (5), /* int_mult_di */
881 COSTS_N_INSNS (72), /* int_div_si */
882 COSTS_N_INSNS (72), /* int_div_di */
883 1, /* branch_cost */
884 4 /* memory_latency */
886 /* Octeon II */
888 SOFT_FP_COSTS,
889 COSTS_N_INSNS (6), /* int_mult_si */
890 COSTS_N_INSNS (6), /* int_mult_di */
891 COSTS_N_INSNS (18), /* int_div_si */
892 COSTS_N_INSNS (35), /* int_div_di */
893 4, /* branch_cost */
894 4 /* memory_latency */
896 { /* R3900 */
897 COSTS_N_INSNS (2), /* fp_add */
898 COSTS_N_INSNS (4), /* fp_mult_sf */
899 COSTS_N_INSNS (5), /* fp_mult_df */
900 COSTS_N_INSNS (12), /* fp_div_sf */
901 COSTS_N_INSNS (19), /* fp_div_df */
902 COSTS_N_INSNS (2), /* int_mult_si */
903 COSTS_N_INSNS (2), /* int_mult_di */
904 COSTS_N_INSNS (35), /* int_div_si */
905 COSTS_N_INSNS (35), /* int_div_di */
906 1, /* branch_cost */
907 4 /* memory_latency */
909 { /* R6000 */
910 COSTS_N_INSNS (3), /* fp_add */
911 COSTS_N_INSNS (5), /* fp_mult_sf */
912 COSTS_N_INSNS (6), /* fp_mult_df */
913 COSTS_N_INSNS (15), /* fp_div_sf */
914 COSTS_N_INSNS (16), /* fp_div_df */
915 COSTS_N_INSNS (17), /* int_mult_si */
916 COSTS_N_INSNS (17), /* int_mult_di */
917 COSTS_N_INSNS (38), /* int_div_si */
918 COSTS_N_INSNS (38), /* int_div_di */
919 2, /* branch_cost */
920 6 /* memory_latency */
922 { /* R4000 */
923 COSTS_N_INSNS (6), /* fp_add */
924 COSTS_N_INSNS (7), /* fp_mult_sf */
925 COSTS_N_INSNS (8), /* fp_mult_df */
926 COSTS_N_INSNS (23), /* fp_div_sf */
927 COSTS_N_INSNS (36), /* fp_div_df */
928 COSTS_N_INSNS (10), /* int_mult_si */
929 COSTS_N_INSNS (10), /* int_mult_di */
930 COSTS_N_INSNS (69), /* int_div_si */
931 COSTS_N_INSNS (69), /* int_div_di */
932 2, /* branch_cost */
933 6 /* memory_latency */
935 { /* R4100 */
936 DEFAULT_COSTS
938 { /* R4111 */
939 DEFAULT_COSTS
941 { /* R4120 */
942 DEFAULT_COSTS
944 { /* R4130 */
945 /* The only costs that appear to be updated here are
946 integer multiplication. */
947 SOFT_FP_COSTS,
948 COSTS_N_INSNS (4), /* int_mult_si */
949 COSTS_N_INSNS (6), /* int_mult_di */
950 COSTS_N_INSNS (69), /* int_div_si */
951 COSTS_N_INSNS (69), /* int_div_di */
952 1, /* branch_cost */
953 4 /* memory_latency */
955 { /* R4300 */
956 DEFAULT_COSTS
958 { /* R4600 */
959 DEFAULT_COSTS
961 { /* R4650 */
962 DEFAULT_COSTS
964 { /* R5000 */
965 COSTS_N_INSNS (6), /* fp_add */
966 COSTS_N_INSNS (4), /* fp_mult_sf */
967 COSTS_N_INSNS (5), /* fp_mult_df */
968 COSTS_N_INSNS (23), /* fp_div_sf */
969 COSTS_N_INSNS (36), /* fp_div_df */
970 COSTS_N_INSNS (5), /* int_mult_si */
971 COSTS_N_INSNS (5), /* int_mult_di */
972 COSTS_N_INSNS (36), /* int_div_si */
973 COSTS_N_INSNS (36), /* int_div_di */
974 1, /* branch_cost */
975 4 /* memory_latency */
977 { /* R5400 */
978 COSTS_N_INSNS (6), /* fp_add */
979 COSTS_N_INSNS (5), /* fp_mult_sf */
980 COSTS_N_INSNS (6), /* fp_mult_df */
981 COSTS_N_INSNS (30), /* fp_div_sf */
982 COSTS_N_INSNS (59), /* fp_div_df */
983 COSTS_N_INSNS (3), /* int_mult_si */
984 COSTS_N_INSNS (4), /* int_mult_di */
985 COSTS_N_INSNS (42), /* int_div_si */
986 COSTS_N_INSNS (74), /* int_div_di */
987 1, /* branch_cost */
988 4 /* memory_latency */
990 { /* R5500 */
991 COSTS_N_INSNS (6), /* fp_add */
992 COSTS_N_INSNS (5), /* fp_mult_sf */
993 COSTS_N_INSNS (6), /* fp_mult_df */
994 COSTS_N_INSNS (30), /* fp_div_sf */
995 COSTS_N_INSNS (59), /* fp_div_df */
996 COSTS_N_INSNS (5), /* int_mult_si */
997 COSTS_N_INSNS (9), /* int_mult_di */
998 COSTS_N_INSNS (42), /* int_div_si */
999 COSTS_N_INSNS (74), /* int_div_di */
1000 1, /* branch_cost */
1001 4 /* memory_latency */
1003 { /* R7000 */
1004 /* The only costs that are changed here are
1005 integer multiplication. */
1006 COSTS_N_INSNS (6), /* fp_add */
1007 COSTS_N_INSNS (7), /* fp_mult_sf */
1008 COSTS_N_INSNS (8), /* fp_mult_df */
1009 COSTS_N_INSNS (23), /* fp_div_sf */
1010 COSTS_N_INSNS (36), /* fp_div_df */
1011 COSTS_N_INSNS (5), /* int_mult_si */
1012 COSTS_N_INSNS (9), /* int_mult_di */
1013 COSTS_N_INSNS (69), /* int_div_si */
1014 COSTS_N_INSNS (69), /* int_div_di */
1015 1, /* branch_cost */
1016 4 /* memory_latency */
1018 { /* R8000 */
1019 DEFAULT_COSTS
1021 { /* R9000 */
1022 /* The only costs that are changed here are
1023 integer multiplication. */
1024 COSTS_N_INSNS (6), /* fp_add */
1025 COSTS_N_INSNS (7), /* fp_mult_sf */
1026 COSTS_N_INSNS (8), /* fp_mult_df */
1027 COSTS_N_INSNS (23), /* fp_div_sf */
1028 COSTS_N_INSNS (36), /* fp_div_df */
1029 COSTS_N_INSNS (3), /* int_mult_si */
1030 COSTS_N_INSNS (8), /* int_mult_di */
1031 COSTS_N_INSNS (69), /* int_div_si */
1032 COSTS_N_INSNS (69), /* int_div_di */
1033 1, /* branch_cost */
1034 4 /* memory_latency */
1036 { /* R1x000 */
1037 COSTS_N_INSNS (2), /* fp_add */
1038 COSTS_N_INSNS (2), /* fp_mult_sf */
1039 COSTS_N_INSNS (2), /* fp_mult_df */
1040 COSTS_N_INSNS (12), /* fp_div_sf */
1041 COSTS_N_INSNS (19), /* fp_div_df */
1042 COSTS_N_INSNS (5), /* int_mult_si */
1043 COSTS_N_INSNS (9), /* int_mult_di */
1044 COSTS_N_INSNS (34), /* int_div_si */
1045 COSTS_N_INSNS (66), /* int_div_di */
1046 1, /* branch_cost */
1047 4 /* memory_latency */
1049 { /* SB1 */
1050 /* These costs are the same as the SB-1A below. */
1051 COSTS_N_INSNS (4), /* fp_add */
1052 COSTS_N_INSNS (4), /* fp_mult_sf */
1053 COSTS_N_INSNS (4), /* fp_mult_df */
1054 COSTS_N_INSNS (24), /* fp_div_sf */
1055 COSTS_N_INSNS (32), /* fp_div_df */
1056 COSTS_N_INSNS (3), /* int_mult_si */
1057 COSTS_N_INSNS (4), /* int_mult_di */
1058 COSTS_N_INSNS (36), /* int_div_si */
1059 COSTS_N_INSNS (68), /* int_div_di */
1060 1, /* branch_cost */
1061 4 /* memory_latency */
1063 { /* SB1-A */
1064 /* These costs are the same as the SB-1 above. */
1065 COSTS_N_INSNS (4), /* fp_add */
1066 COSTS_N_INSNS (4), /* fp_mult_sf */
1067 COSTS_N_INSNS (4), /* fp_mult_df */
1068 COSTS_N_INSNS (24), /* fp_div_sf */
1069 COSTS_N_INSNS (32), /* fp_div_df */
1070 COSTS_N_INSNS (3), /* int_mult_si */
1071 COSTS_N_INSNS (4), /* int_mult_di */
1072 COSTS_N_INSNS (36), /* int_div_si */
1073 COSTS_N_INSNS (68), /* int_div_di */
1074 1, /* branch_cost */
1075 4 /* memory_latency */
1077 { /* SR71000 */
1078 DEFAULT_COSTS
1080 { /* XLR */
1081 SOFT_FP_COSTS,
1082 COSTS_N_INSNS (8), /* int_mult_si */
1083 COSTS_N_INSNS (8), /* int_mult_di */
1084 COSTS_N_INSNS (72), /* int_div_si */
1085 COSTS_N_INSNS (72), /* int_div_di */
1086 1, /* branch_cost */
1087 4 /* memory_latency */
1089 { /* XLP */
1090 /* These costs are the same as 5KF above. */
1091 COSTS_N_INSNS (4), /* fp_add */
1092 COSTS_N_INSNS (4), /* fp_mult_sf */
1093 COSTS_N_INSNS (5), /* fp_mult_df */
1094 COSTS_N_INSNS (17), /* fp_div_sf */
1095 COSTS_N_INSNS (32), /* fp_div_df */
1096 COSTS_N_INSNS (4), /* int_mult_si */
1097 COSTS_N_INSNS (11), /* int_mult_di */
1098 COSTS_N_INSNS (36), /* int_div_si */
1099 COSTS_N_INSNS (68), /* int_div_di */
1100 1, /* branch_cost */
1101 4 /* memory_latency */
1105 static rtx mips_find_pic_call_symbol (rtx, rtx, bool);
1106 static int mips_register_move_cost (enum machine_mode, reg_class_t,
1107 reg_class_t);
1108 static unsigned int mips_function_arg_boundary (enum machine_mode, const_tree);
1110 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1111 for -mflip_mips16. It maps decl names onto a boolean mode setting. */
1112 struct GTY (()) mflip_mips16_entry {
1113 const char *name;
1114 bool mips16_p;
1116 static GTY ((param_is (struct mflip_mips16_entry))) htab_t mflip_mips16_htab;
1118 /* Hash table callbacks for mflip_mips16_htab. */
1120 static hashval_t
1121 mflip_mips16_htab_hash (const void *entry)
1123 return htab_hash_string (((const struct mflip_mips16_entry *) entry)->name);
1126 static int
1127 mflip_mips16_htab_eq (const void *entry, const void *name)
1129 return strcmp (((const struct mflip_mips16_entry *) entry)->name,
1130 (const char *) name) == 0;
1133 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1134 mode, false if it should next add an attribute for the opposite mode. */
1135 static GTY(()) bool mips16_flipper;
1137 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1138 for -mflip-mips16. Return true if it should use "mips16" and false if
1139 it should use "nomips16". */
1141 static bool
1142 mflip_mips16_use_mips16_p (tree decl)
1144 struct mflip_mips16_entry *entry;
1145 const char *name;
1146 hashval_t hash;
1147 void **slot;
1149 /* Use the opposite of the command-line setting for anonymous decls. */
1150 if (!DECL_NAME (decl))
1151 return !mips_base_mips16;
1153 if (!mflip_mips16_htab)
1154 mflip_mips16_htab = htab_create_ggc (37, mflip_mips16_htab_hash,
1155 mflip_mips16_htab_eq, NULL);
1157 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1158 hash = htab_hash_string (name);
1159 slot = htab_find_slot_with_hash (mflip_mips16_htab, name, hash, INSERT);
1160 entry = (struct mflip_mips16_entry *) *slot;
1161 if (!entry)
1163 mips16_flipper = !mips16_flipper;
1164 entry = ggc_alloc_mflip_mips16_entry ();
1165 entry->name = name;
1166 entry->mips16_p = mips16_flipper ? !mips_base_mips16 : mips_base_mips16;
1167 *slot = entry;
1169 return entry->mips16_p;
1172 /* Predicates to test for presence of "near" and "far"/"long_call"
1173 attributes on the given TYPE. */
1175 static bool
1176 mips_near_type_p (const_tree type)
1178 return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1181 static bool
1182 mips_far_type_p (const_tree type)
1184 return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1185 || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1188 /* Similar predicates for "mips16"/"nomips16" function attributes. */
1190 static bool
1191 mips_mips16_decl_p (const_tree decl)
1193 return lookup_attribute ("mips16", DECL_ATTRIBUTES (decl)) != NULL;
1196 static bool
1197 mips_nomips16_decl_p (const_tree decl)
1199 return lookup_attribute ("nomips16", DECL_ATTRIBUTES (decl)) != NULL;
1202 /* Check if the interrupt attribute is set for a function. */
1204 static bool
1205 mips_interrupt_type_p (tree type)
1207 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1210 /* Check if the attribute to use shadow register set is set for a function. */
1212 static bool
1213 mips_use_shadow_register_set_p (tree type)
1215 return lookup_attribute ("use_shadow_register_set",
1216 TYPE_ATTRIBUTES (type)) != NULL;
1219 /* Check if the attribute to keep interrupts masked is set for a function. */
1221 static bool
1222 mips_keep_interrupts_masked_p (tree type)
1224 return lookup_attribute ("keep_interrupts_masked",
1225 TYPE_ATTRIBUTES (type)) != NULL;
1228 /* Check if the attribute to use debug exception return is set for
1229 a function. */
1231 static bool
1232 mips_use_debug_exception_return_p (tree type)
1234 return lookup_attribute ("use_debug_exception_return",
1235 TYPE_ATTRIBUTES (type)) != NULL;
1238 /* Return true if function DECL is a MIPS16 function. Return the ambient
1239 setting if DECL is null. */
1241 static bool
1242 mips_use_mips16_mode_p (tree decl)
1244 if (decl)
1246 /* Nested functions must use the same frame pointer as their
1247 parent and must therefore use the same ISA mode. */
1248 tree parent = decl_function_context (decl);
1249 if (parent)
1250 decl = parent;
1251 if (mips_mips16_decl_p (decl))
1252 return true;
1253 if (mips_nomips16_decl_p (decl))
1254 return false;
1256 return mips_base_mips16;
1259 /* Implement TARGET_COMP_TYPE_ATTRIBUTES. */
1261 static int
1262 mips_comp_type_attributes (const_tree type1, const_tree type2)
1264 /* Disallow mixed near/far attributes. */
1265 if (mips_far_type_p (type1) && mips_near_type_p (type2))
1266 return 0;
1267 if (mips_near_type_p (type1) && mips_far_type_p (type2))
1268 return 0;
1269 return 1;
1272 /* Implement TARGET_INSERT_ATTRIBUTES. */
1274 static void
1275 mips_insert_attributes (tree decl, tree *attributes)
1277 const char *name;
1278 bool mips16_p, nomips16_p;
1280 /* Check for "mips16" and "nomips16" attributes. */
1281 mips16_p = lookup_attribute ("mips16", *attributes) != NULL;
1282 nomips16_p = lookup_attribute ("nomips16", *attributes) != NULL;
1283 if (TREE_CODE (decl) != FUNCTION_DECL)
1285 if (mips16_p)
1286 error ("%qs attribute only applies to functions", "mips16");
1287 if (nomips16_p)
1288 error ("%qs attribute only applies to functions", "nomips16");
1290 else
1292 mips16_p |= mips_mips16_decl_p (decl);
1293 nomips16_p |= mips_nomips16_decl_p (decl);
1294 if (mips16_p || nomips16_p)
1296 /* DECL cannot be simultaneously "mips16" and "nomips16". */
1297 if (mips16_p && nomips16_p)
1298 error ("%qE cannot have both %<mips16%> and "
1299 "%<nomips16%> attributes",
1300 DECL_NAME (decl));
1302 else if (TARGET_FLIP_MIPS16 && !DECL_ARTIFICIAL (decl))
1304 /* Implement -mflip-mips16. If DECL has neither a "nomips16" nor a
1305 "mips16" attribute, arbitrarily pick one. We must pick the same
1306 setting for duplicate declarations of a function. */
1307 name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1308 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1313 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
1315 static tree
1316 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1318 /* The decls' "mips16" and "nomips16" attributes must match exactly. */
1319 if (mips_mips16_decl_p (olddecl) != mips_mips16_decl_p (newdecl))
1320 error ("%qE redeclared with conflicting %qs attributes",
1321 DECL_NAME (newdecl), "mips16");
1322 if (mips_nomips16_decl_p (olddecl) != mips_nomips16_decl_p (newdecl))
1323 error ("%qE redeclared with conflicting %qs attributes",
1324 DECL_NAME (newdecl), "nomips16");
1326 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1327 DECL_ATTRIBUTES (newdecl));
1330 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1331 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
1333 static void
1334 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1336 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1338 *base_ptr = XEXP (x, 0);
1339 *offset_ptr = INTVAL (XEXP (x, 1));
1341 else
1343 *base_ptr = x;
1344 *offset_ptr = 0;
1348 static unsigned int mips_build_integer (struct mips_integer_op *,
1349 unsigned HOST_WIDE_INT);
1351 /* A subroutine of mips_build_integer, with the same interface.
1352 Assume that the final action in the sequence should be a left shift. */
1354 static unsigned int
1355 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1357 unsigned int i, shift;
1359 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1360 since signed numbers are easier to load than unsigned ones. */
1361 shift = 0;
1362 while ((value & 1) == 0)
1363 value /= 2, shift++;
1365 i = mips_build_integer (codes, value);
1366 codes[i].code = ASHIFT;
1367 codes[i].value = shift;
1368 return i + 1;
1371 /* As for mips_build_shift, but assume that the final action will be
1372 an IOR or PLUS operation. */
1374 static unsigned int
1375 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1377 unsigned HOST_WIDE_INT high;
1378 unsigned int i;
1380 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1381 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1383 /* The constant is too complex to load with a simple LUI/ORI pair,
1384 so we want to give the recursive call as many trailing zeros as
1385 possible. In this case, we know bit 16 is set and that the
1386 low 16 bits form a negative number. If we subtract that number
1387 from VALUE, we will clear at least the lowest 17 bits, maybe more. */
1388 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1389 codes[i].code = PLUS;
1390 codes[i].value = CONST_LOW_PART (value);
1392 else
1394 /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1395 bits gives a value with at least 17 trailing zeros. */
1396 i = mips_build_integer (codes, high);
1397 codes[i].code = IOR;
1398 codes[i].value = value & 0xffff;
1400 return i + 1;
1403 /* Fill CODES with a sequence of rtl operations to load VALUE.
1404 Return the number of operations needed. */
1406 static unsigned int
1407 mips_build_integer (struct mips_integer_op *codes,
1408 unsigned HOST_WIDE_INT value)
1410 if (SMALL_OPERAND (value)
1411 || SMALL_OPERAND_UNSIGNED (value)
1412 || LUI_OPERAND (value))
1414 /* The value can be loaded with a single instruction. */
1415 codes[0].code = UNKNOWN;
1416 codes[0].value = value;
1417 return 1;
1419 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1421 /* Either the constant is a simple LUI/ORI combination or its
1422 lowest bit is set. We don't want to shift in this case. */
1423 return mips_build_lower (codes, value);
1425 else if ((value & 0xffff) == 0)
1427 /* The constant will need at least three actions. The lowest
1428 16 bits are clear, so the final action will be a shift. */
1429 return mips_build_shift (codes, value);
1431 else
1433 /* The final action could be a shift, add or inclusive OR.
1434 Rather than use a complex condition to select the best
1435 approach, try both mips_build_shift and mips_build_lower
1436 and pick the one that gives the shortest sequence.
1437 Note that this case is only used once per constant. */
1438 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1439 unsigned int cost, alt_cost;
1441 cost = mips_build_shift (codes, value);
1442 alt_cost = mips_build_lower (alt_codes, value);
1443 if (alt_cost < cost)
1445 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1446 cost = alt_cost;
1448 return cost;
1452 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
1454 static bool
1455 mips_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1457 return mips_const_insns (x) > 0;
1460 /* Return a SYMBOL_REF for a MIPS16 function called NAME. */
1462 static rtx
1463 mips16_stub_function (const char *name)
1465 rtx x;
1467 x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1468 SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1469 return x;
1472 /* Return true if symbols of type TYPE require a GOT access. */
1474 static bool
1475 mips_got_symbol_type_p (enum mips_symbol_type type)
1477 switch (type)
1479 case SYMBOL_GOT_PAGE_OFST:
1480 case SYMBOL_GOT_DISP:
1481 return true;
1483 default:
1484 return false;
1488 /* Return true if X is a thread-local symbol. */
1490 static bool
1491 mips_tls_symbol_p (rtx x)
1493 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1496 /* Return true if SYMBOL_REF X is associated with a global symbol
1497 (in the STB_GLOBAL sense). */
1499 static bool
1500 mips_global_symbol_p (const_rtx x)
1502 const_tree decl = SYMBOL_REF_DECL (x);
1504 if (!decl)
1505 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1507 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1508 or weak symbols. Relocations in the object file will be against
1509 the target symbol, so it's that symbol's binding that matters here. */
1510 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1513 /* Return true if function X is a libgcc MIPS16 stub function. */
1515 static bool
1516 mips16_stub_function_p (const_rtx x)
1518 return (GET_CODE (x) == SYMBOL_REF
1519 && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1522 /* Return true if function X is a locally-defined and locally-binding
1523 MIPS16 function. */
1525 static bool
1526 mips16_local_function_p (const_rtx x)
1528 return (GET_CODE (x) == SYMBOL_REF
1529 && SYMBOL_REF_LOCAL_P (x)
1530 && !SYMBOL_REF_EXTERNAL_P (x)
1531 && mips_use_mips16_mode_p (SYMBOL_REF_DECL (x)));
1534 /* Return true if SYMBOL_REF X binds locally. */
1536 static bool
1537 mips_symbol_binds_local_p (const_rtx x)
1539 return (SYMBOL_REF_DECL (x)
1540 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1541 : SYMBOL_REF_LOCAL_P (x));
1544 /* Return true if rtx constants of mode MODE should be put into a small
1545 data section. */
1547 static bool
1548 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1550 return (!TARGET_EMBEDDED_DATA
1551 && TARGET_LOCAL_SDATA
1552 && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1555 /* Return true if X should not be moved directly into register $25.
1556 We need this because many versions of GAS will treat "la $25,foo" as
1557 part of a call sequence and so allow a global "foo" to be lazily bound. */
1559 bool
1560 mips_dangerous_for_la25_p (rtx x)
1562 return (!TARGET_EXPLICIT_RELOCS
1563 && TARGET_USE_GOT
1564 && GET_CODE (x) == SYMBOL_REF
1565 && mips_global_symbol_p (x));
1568 /* Return true if calls to X might need $25 to be valid on entry. */
1570 bool
1571 mips_use_pic_fn_addr_reg_p (const_rtx x)
1573 if (!TARGET_USE_PIC_FN_ADDR_REG)
1574 return false;
1576 /* MIPS16 stub functions are guaranteed not to use $25. */
1577 if (mips16_stub_function_p (x))
1578 return false;
1580 if (GET_CODE (x) == SYMBOL_REF)
1582 /* If PLTs and copy relocations are available, the static linker
1583 will make sure that $25 is valid on entry to the target function. */
1584 if (TARGET_ABICALLS_PIC0)
1585 return false;
1587 /* Locally-defined functions use absolute accesses to set up
1588 the global pointer. */
1589 if (TARGET_ABSOLUTE_ABICALLS
1590 && mips_symbol_binds_local_p (x)
1591 && !SYMBOL_REF_EXTERNAL_P (x))
1592 return false;
1595 return true;
1598 /* Return the method that should be used to access SYMBOL_REF or
1599 LABEL_REF X in context CONTEXT. */
1601 static enum mips_symbol_type
1602 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1604 if (TARGET_RTP_PIC)
1605 return SYMBOL_GOT_DISP;
1607 if (GET_CODE (x) == LABEL_REF)
1609 /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
1610 code and if we know that the label is in the current function's
1611 text section. LABEL_REFs are used for jump tables as well as
1612 text labels, so we must check whether jump tables live in the
1613 text section. */
1614 if (TARGET_MIPS16_SHORT_JUMP_TABLES
1615 && !LABEL_REF_NONLOCAL_P (x))
1616 return SYMBOL_PC_RELATIVE;
1618 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1619 return SYMBOL_GOT_PAGE_OFST;
1621 return SYMBOL_ABSOLUTE;
1624 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1626 if (SYMBOL_REF_TLS_MODEL (x))
1627 return SYMBOL_TLS;
1629 if (CONSTANT_POOL_ADDRESS_P (x))
1631 if (TARGET_MIPS16_TEXT_LOADS)
1632 return SYMBOL_PC_RELATIVE;
1634 if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1635 return SYMBOL_PC_RELATIVE;
1637 if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1638 return SYMBOL_GP_RELATIVE;
1641 /* Do not use small-data accesses for weak symbols; they may end up
1642 being zero. */
1643 if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1644 return SYMBOL_GP_RELATIVE;
1646 /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1647 is in effect. */
1648 if (TARGET_ABICALLS_PIC2
1649 && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1651 /* There are three cases to consider:
1653 - o32 PIC (either with or without explicit relocs)
1654 - n32/n64 PIC without explicit relocs
1655 - n32/n64 PIC with explicit relocs
1657 In the first case, both local and global accesses will use an
1658 R_MIPS_GOT16 relocation. We must correctly predict which of
1659 the two semantics (local or global) the assembler and linker
1660 will apply. The choice depends on the symbol's binding rather
1661 than its visibility.
1663 In the second case, the assembler will not use R_MIPS_GOT16
1664 relocations, but it chooses between local and global accesses
1665 in the same way as for o32 PIC.
1667 In the third case we have more freedom since both forms of
1668 access will work for any kind of symbol. However, there seems
1669 little point in doing things differently. */
1670 if (mips_global_symbol_p (x))
1671 return SYMBOL_GOT_DISP;
1673 return SYMBOL_GOT_PAGE_OFST;
1676 return SYMBOL_ABSOLUTE;
1679 /* Classify the base of symbolic expression X, given that X appears in
1680 context CONTEXT. */
1682 static enum mips_symbol_type
1683 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1685 rtx offset;
1687 split_const (x, &x, &offset);
1688 if (UNSPEC_ADDRESS_P (x))
1689 return UNSPEC_ADDRESS_TYPE (x);
1691 return mips_classify_symbol (x, context);
1694 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1695 is the alignment in bytes of SYMBOL_REF X. */
1697 static bool
1698 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1700 HOST_WIDE_INT align;
1702 align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1703 return IN_RANGE (offset, 0, align - 1);
1706 /* Return true if X is a symbolic constant that can be used in context
1707 CONTEXT. If it is, store the type of the symbol in *SYMBOL_TYPE. */
1709 bool
1710 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1711 enum mips_symbol_type *symbol_type)
1713 rtx offset;
1715 split_const (x, &x, &offset);
1716 if (UNSPEC_ADDRESS_P (x))
1718 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1719 x = UNSPEC_ADDRESS (x);
1721 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1723 *symbol_type = mips_classify_symbol (x, context);
1724 if (*symbol_type == SYMBOL_TLS)
1725 return false;
1727 else
1728 return false;
1730 if (offset == const0_rtx)
1731 return true;
1733 /* Check whether a nonzero offset is valid for the underlying
1734 relocations. */
1735 switch (*symbol_type)
1737 case SYMBOL_ABSOLUTE:
1738 case SYMBOL_64_HIGH:
1739 case SYMBOL_64_MID:
1740 case SYMBOL_64_LOW:
1741 /* If the target has 64-bit pointers and the object file only
1742 supports 32-bit symbols, the values of those symbols will be
1743 sign-extended. In this case we can't allow an arbitrary offset
1744 in case the 32-bit value X + OFFSET has a different sign from X. */
1745 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1746 return offset_within_block_p (x, INTVAL (offset));
1748 /* In other cases the relocations can handle any offset. */
1749 return true;
1751 case SYMBOL_PC_RELATIVE:
1752 /* Allow constant pool references to be converted to LABEL+CONSTANT.
1753 In this case, we no longer have access to the underlying constant,
1754 but the original symbol-based access was known to be valid. */
1755 if (GET_CODE (x) == LABEL_REF)
1756 return true;
1758 /* Fall through. */
1760 case SYMBOL_GP_RELATIVE:
1761 /* Make sure that the offset refers to something within the
1762 same object block. This should guarantee that the final
1763 PC- or GP-relative offset is within the 16-bit limit. */
1764 return offset_within_block_p (x, INTVAL (offset));
1766 case SYMBOL_GOT_PAGE_OFST:
1767 case SYMBOL_GOTOFF_PAGE:
1768 /* If the symbol is global, the GOT entry will contain the symbol's
1769 address, and we will apply a 16-bit offset after loading it.
1770 If the symbol is local, the linker should provide enough local
1771 GOT entries for a 16-bit offset, but larger offsets may lead
1772 to GOT overflow. */
1773 return SMALL_INT (offset);
1775 case SYMBOL_TPREL:
1776 case SYMBOL_DTPREL:
1777 /* There is no carry between the HI and LO REL relocations, so the
1778 offset is only valid if we know it won't lead to such a carry. */
1779 return mips_offset_within_alignment_p (x, INTVAL (offset));
1781 case SYMBOL_GOT_DISP:
1782 case SYMBOL_GOTOFF_DISP:
1783 case SYMBOL_GOTOFF_CALL:
1784 case SYMBOL_GOTOFF_LOADGP:
1785 case SYMBOL_TLSGD:
1786 case SYMBOL_TLSLDM:
1787 case SYMBOL_GOTTPREL:
1788 case SYMBOL_TLS:
1789 case SYMBOL_HALF:
1790 return false;
1792 gcc_unreachable ();
1795 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
1796 single instruction. We rely on the fact that, in the worst case,
1797 all instructions involved in a MIPS16 address calculation are usually
1798 extended ones. */
1800 static int
1801 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
1803 if (mips_use_pcrel_pool_p[(int) type])
1805 if (mode == MAX_MACHINE_MODE)
1806 /* LEAs will be converted into constant-pool references by
1807 mips_reorg. */
1808 type = SYMBOL_PC_RELATIVE;
1809 else
1810 /* The constant must be loaded and then dereferenced. */
1811 return 0;
1814 switch (type)
1816 case SYMBOL_ABSOLUTE:
1817 /* When using 64-bit symbols, we need 5 preparatory instructions,
1818 such as:
1820 lui $at,%highest(symbol)
1821 daddiu $at,$at,%higher(symbol)
1822 dsll $at,$at,16
1823 daddiu $at,$at,%hi(symbol)
1824 dsll $at,$at,16
1826 The final address is then $at + %lo(symbol). With 32-bit
1827 symbols we just need a preparatory LUI for normal mode and
1828 a preparatory LI and SLL for MIPS16. */
1829 return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
1831 case SYMBOL_GP_RELATIVE:
1832 /* Treat GP-relative accesses as taking a single instruction on
1833 MIPS16 too; the copy of $gp can often be shared. */
1834 return 1;
1836 case SYMBOL_PC_RELATIVE:
1837 /* PC-relative constants can be only be used with ADDIUPC,
1838 DADDIUPC, LWPC and LDPC. */
1839 if (mode == MAX_MACHINE_MODE
1840 || GET_MODE_SIZE (mode) == 4
1841 || GET_MODE_SIZE (mode) == 8)
1842 return 1;
1844 /* The constant must be loaded using ADDIUPC or DADDIUPC first. */
1845 return 0;
1847 case SYMBOL_GOT_DISP:
1848 /* The constant will have to be loaded from the GOT before it
1849 is used in an address. */
1850 if (mode != MAX_MACHINE_MODE)
1851 return 0;
1853 /* Fall through. */
1855 case SYMBOL_GOT_PAGE_OFST:
1856 /* Unless -funit-at-a-time is in effect, we can't be sure whether the
1857 local/global classification is accurate. The worst cases are:
1859 (1) For local symbols when generating o32 or o64 code. The assembler
1860 will use:
1862 lw $at,%got(symbol)
1865 ...and the final address will be $at + %lo(symbol).
1867 (2) For global symbols when -mxgot. The assembler will use:
1869 lui $at,%got_hi(symbol)
1870 (d)addu $at,$at,$gp
1872 ...and the final address will be $at + %got_lo(symbol). */
1873 return 3;
1875 case SYMBOL_GOTOFF_PAGE:
1876 case SYMBOL_GOTOFF_DISP:
1877 case SYMBOL_GOTOFF_CALL:
1878 case SYMBOL_GOTOFF_LOADGP:
1879 case SYMBOL_64_HIGH:
1880 case SYMBOL_64_MID:
1881 case SYMBOL_64_LOW:
1882 case SYMBOL_TLSGD:
1883 case SYMBOL_TLSLDM:
1884 case SYMBOL_DTPREL:
1885 case SYMBOL_GOTTPREL:
1886 case SYMBOL_TPREL:
1887 case SYMBOL_HALF:
1888 /* A 16-bit constant formed by a single relocation, or a 32-bit
1889 constant formed from a high 16-bit relocation and a low 16-bit
1890 relocation. Use mips_split_p to determine which. 32-bit
1891 constants need an "lui; addiu" sequence for normal mode and
1892 an "li; sll; addiu" sequence for MIPS16 mode. */
1893 return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
1895 case SYMBOL_TLS:
1896 /* We don't treat a bare TLS symbol as a constant. */
1897 return 0;
1899 gcc_unreachable ();
1902 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
1903 to load symbols of type TYPE into a register. Return 0 if the given
1904 type of symbol cannot be used as an immediate operand.
1906 Otherwise, return the number of instructions needed to load or store
1907 values of mode MODE to or from addresses of type TYPE. Return 0 if
1908 the given type of symbol is not valid in addresses.
1910 In both cases, treat extended MIPS16 instructions as two instructions. */
1912 static int
1913 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
1915 return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
1918 /* A for_each_rtx callback. Stop the search if *X references a
1919 thread-local symbol. */
1921 static int
1922 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1924 return mips_tls_symbol_p (*x);
1927 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
1929 static bool
1930 mips_cannot_force_const_mem (enum machine_mode mode, rtx x)
1932 enum mips_symbol_type type;
1933 rtx base, offset;
1935 /* There is no assembler syntax for expressing an address-sized
1936 high part. */
1937 if (GET_CODE (x) == HIGH)
1938 return true;
1940 /* As an optimization, reject constants that mips_legitimize_move
1941 can expand inline.
1943 Suppose we have a multi-instruction sequence that loads constant C
1944 into register R. If R does not get allocated a hard register, and
1945 R is used in an operand that allows both registers and memory
1946 references, reload will consider forcing C into memory and using
1947 one of the instruction's memory alternatives. Returning false
1948 here will force it to use an input reload instead. */
1949 if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
1950 return true;
1952 split_const (x, &base, &offset);
1953 if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
1955 /* See whether we explicitly want these symbols in the pool. */
1956 if (mips_use_pcrel_pool_p[(int) type])
1957 return false;
1959 /* The same optimization as for CONST_INT. */
1960 if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
1961 return true;
1963 /* If MIPS16 constant pools live in the text section, they should
1964 not refer to anything that might need run-time relocation. */
1965 if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
1966 return true;
1969 /* TLS symbols must be computed by mips_legitimize_move. */
1970 if (for_each_rtx (&x, &mips_tls_symbol_ref_1, NULL))
1971 return true;
1973 return false;
1976 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. We can't use blocks for
1977 constants when we're using a per-function constant pool. */
1979 static bool
1980 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
1981 const_rtx x ATTRIBUTE_UNUSED)
1983 return !TARGET_MIPS16_PCREL_LOADS;
1986 /* Return true if register REGNO is a valid base register for mode MODE.
1987 STRICT_P is true if REG_OK_STRICT is in effect. */
1990 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
1991 bool strict_p)
1993 if (!HARD_REGISTER_NUM_P (regno))
1995 if (!strict_p)
1996 return true;
1997 regno = reg_renumber[regno];
2000 /* These fake registers will be eliminated to either the stack or
2001 hard frame pointer, both of which are usually valid base registers.
2002 Reload deals with the cases where the eliminated form isn't valid. */
2003 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2004 return true;
2006 /* In MIPS16 mode, the stack pointer can only address word and doubleword
2007 values, nothing smaller. There are two problems here:
2009 (a) Instantiating virtual registers can introduce new uses of the
2010 stack pointer. If these virtual registers are valid addresses,
2011 the stack pointer should be too.
2013 (b) Most uses of the stack pointer are not made explicit until
2014 FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
2015 We don't know until that stage whether we'll be eliminating to the
2016 stack pointer (which needs the restriction) or the hard frame
2017 pointer (which doesn't).
2019 All in all, it seems more consistent to only enforce this restriction
2020 during and after reload. */
2021 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2022 return !strict_p || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2024 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2027 /* Return true if X is a valid base register for mode MODE.
2028 STRICT_P is true if REG_OK_STRICT is in effect. */
2030 static bool
2031 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
2033 if (!strict_p && GET_CODE (x) == SUBREG)
2034 x = SUBREG_REG (x);
2036 return (REG_P (x)
2037 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2040 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2041 can address a value of mode MODE. */
2043 static bool
2044 mips_valid_offset_p (rtx x, enum machine_mode mode)
2046 /* Check that X is a signed 16-bit number. */
2047 if (!const_arith_operand (x, Pmode))
2048 return false;
2050 /* We may need to split multiword moves, so make sure that every word
2051 is accessible. */
2052 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2053 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2054 return false;
2056 return true;
2059 /* Return true if a LO_SUM can address a value of mode MODE when the
2060 LO_SUM symbol has type SYMBOL_TYPE. */
2062 static bool
2063 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, enum machine_mode mode)
2065 /* Check that symbols of type SYMBOL_TYPE can be used to access values
2066 of mode MODE. */
2067 if (mips_symbol_insns (symbol_type, mode) == 0)
2068 return false;
2070 /* Check that there is a known low-part relocation. */
2071 if (mips_lo_relocs[symbol_type] == NULL)
2072 return false;
2074 /* We may need to split multiword moves, so make sure that each word
2075 can be accessed without inducing a carry. This is mainly needed
2076 for o64, which has historically only guaranteed 64-bit alignment
2077 for 128-bit types. */
2078 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2079 && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2080 return false;
2082 return true;
2085 /* Return true if X is a valid address for machine mode MODE. If it is,
2086 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
2087 effect. */
2089 static bool
2090 mips_classify_address (struct mips_address_info *info, rtx x,
2091 enum machine_mode mode, bool strict_p)
2093 switch (GET_CODE (x))
2095 case REG:
2096 case SUBREG:
2097 info->type = ADDRESS_REG;
2098 info->reg = x;
2099 info->offset = const0_rtx;
2100 return mips_valid_base_register_p (info->reg, mode, strict_p);
2102 case PLUS:
2103 info->type = ADDRESS_REG;
2104 info->reg = XEXP (x, 0);
2105 info->offset = XEXP (x, 1);
2106 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2107 && mips_valid_offset_p (info->offset, mode));
2109 case LO_SUM:
2110 info->type = ADDRESS_LO_SUM;
2111 info->reg = XEXP (x, 0);
2112 info->offset = XEXP (x, 1);
2113 /* We have to trust the creator of the LO_SUM to do something vaguely
2114 sane. Target-independent code that creates a LO_SUM should also
2115 create and verify the matching HIGH. Target-independent code that
2116 adds an offset to a LO_SUM must prove that the offset will not
2117 induce a carry. Failure to do either of these things would be
2118 a bug, and we are not required to check for it here. The MIPS
2119 backend itself should only create LO_SUMs for valid symbolic
2120 constants, with the high part being either a HIGH or a copy
2121 of _gp. */
2122 info->symbol_type
2123 = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2124 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2125 && mips_valid_lo_sum_p (info->symbol_type, mode));
2127 case CONST_INT:
2128 /* Small-integer addresses don't occur very often, but they
2129 are legitimate if $0 is a valid base register. */
2130 info->type = ADDRESS_CONST_INT;
2131 return !TARGET_MIPS16 && SMALL_INT (x);
2133 case CONST:
2134 case LABEL_REF:
2135 case SYMBOL_REF:
2136 info->type = ADDRESS_SYMBOLIC;
2137 return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2138 &info->symbol_type)
2139 && mips_symbol_insns (info->symbol_type, mode) > 0
2140 && !mips_split_p[info->symbol_type]);
2142 default:
2143 return false;
2147 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2149 static bool
2150 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2152 struct mips_address_info addr;
2154 return mips_classify_address (&addr, x, mode, strict_p);
2157 /* Return true if X is a legitimate $sp-based address for mode MDOE. */
2159 bool
2160 mips_stack_address_p (rtx x, enum machine_mode mode)
2162 struct mips_address_info addr;
2164 return (mips_classify_address (&addr, x, mode, false)
2165 && addr.type == ADDRESS_REG
2166 && addr.reg == stack_pointer_rtx);
2169 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2170 address instruction. Note that such addresses are not considered
2171 legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2172 is so restricted. */
2174 static bool
2175 mips_lwxs_address_p (rtx addr)
2177 if (ISA_HAS_LWXS
2178 && GET_CODE (addr) == PLUS
2179 && REG_P (XEXP (addr, 1)))
2181 rtx offset = XEXP (addr, 0);
2182 if (GET_CODE (offset) == MULT
2183 && REG_P (XEXP (offset, 0))
2184 && CONST_INT_P (XEXP (offset, 1))
2185 && INTVAL (XEXP (offset, 1)) == 4)
2186 return true;
2188 return false;
2191 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load
2192 indexed address instruction. Note that such addresses are
2193 not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2194 sense, because their use is so restricted. */
2196 static bool
2197 mips_lx_address_p (rtx addr, enum machine_mode mode)
2199 if (GET_CODE (addr) != PLUS
2200 || !REG_P (XEXP (addr, 0))
2201 || !REG_P (XEXP (addr, 1)))
2202 return false;
2203 if (ISA_HAS_LBX && mode == QImode)
2204 return true;
2205 if (ISA_HAS_LHX && mode == HImode)
2206 return true;
2207 if (ISA_HAS_LWX && mode == SImode)
2208 return true;
2209 if (ISA_HAS_LDX && mode == DImode)
2210 return true;
2211 return false;
2214 /* Return true if a value at OFFSET bytes from base register BASE can be
2215 accessed using an unextended MIPS16 instruction. MODE is the mode of
2216 the value.
2218 Usually the offset in an unextended instruction is a 5-bit field.
2219 The offset is unsigned and shifted left once for LH and SH, twice
2220 for LW and SW, and so on. An exception is LWSP and SWSP, which have
2221 an 8-bit immediate field that's shifted left twice. */
2223 static bool
2224 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
2225 unsigned HOST_WIDE_INT offset)
2227 if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2229 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2230 return offset < 256U * GET_MODE_SIZE (mode);
2231 return offset < 32U * GET_MODE_SIZE (mode);
2233 return false;
2236 /* Return the number of instructions needed to load or store a value
2237 of mode MODE at address X. Return 0 if X isn't valid for MODE.
2238 Assume that multiword moves may need to be split into word moves
2239 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2240 enough.
2242 For MIPS16 code, count extended instructions as two instructions. */
2245 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
2247 struct mips_address_info addr;
2248 int factor;
2250 /* BLKmode is used for single unaligned loads and stores and should
2251 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
2252 meaningless, so we have to single it out as a special case one way
2253 or the other.) */
2254 if (mode != BLKmode && might_split_p)
2255 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2256 else
2257 factor = 1;
2259 if (mips_classify_address (&addr, x, mode, false))
2260 switch (addr.type)
2262 case ADDRESS_REG:
2263 if (TARGET_MIPS16
2264 && !mips16_unextended_reference_p (mode, addr.reg,
2265 UINTVAL (addr.offset)))
2266 return factor * 2;
2267 return factor;
2269 case ADDRESS_LO_SUM:
2270 return TARGET_MIPS16 ? factor * 2 : factor;
2272 case ADDRESS_CONST_INT:
2273 return factor;
2275 case ADDRESS_SYMBOLIC:
2276 return factor * mips_symbol_insns (addr.symbol_type, mode);
2278 return 0;
2281 /* Return the number of instructions needed to load constant X.
2282 Return 0 if X isn't a valid constant. */
2285 mips_const_insns (rtx x)
2287 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2288 enum mips_symbol_type symbol_type;
2289 rtx offset;
2291 switch (GET_CODE (x))
2293 case HIGH:
2294 if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2295 &symbol_type)
2296 || !mips_split_p[symbol_type])
2297 return 0;
2299 /* This is simply an LUI for normal mode. It is an extended
2300 LI followed by an extended SLL for MIPS16. */
2301 return TARGET_MIPS16 ? 4 : 1;
2303 case CONST_INT:
2304 if (TARGET_MIPS16)
2305 /* Unsigned 8-bit constants can be loaded using an unextended
2306 LI instruction. Unsigned 16-bit constants can be loaded
2307 using an extended LI. Negative constants must be loaded
2308 using LI and then negated. */
2309 return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2310 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2311 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2312 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2313 : 0);
2315 return mips_build_integer (codes, INTVAL (x));
2317 case CONST_DOUBLE:
2318 case CONST_VECTOR:
2319 /* Allow zeros for normal mode, where we can use $0. */
2320 return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2322 case CONST:
2323 if (CONST_GP_P (x))
2324 return 1;
2326 /* See if we can refer to X directly. */
2327 if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2328 return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2330 /* Otherwise try splitting the constant into a base and offset.
2331 If the offset is a 16-bit value, we can load the base address
2332 into a register and then use (D)ADDIU to add in the offset.
2333 If the offset is larger, we can load the base and offset
2334 into separate registers and add them together with (D)ADDU.
2335 However, the latter is only possible before reload; during
2336 and after reload, we must have the option of forcing the
2337 constant into the pool instead. */
2338 split_const (x, &x, &offset);
2339 if (offset != 0)
2341 int n = mips_const_insns (x);
2342 if (n != 0)
2344 if (SMALL_INT (offset))
2345 return n + 1;
2346 else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2347 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2350 return 0;
2352 case SYMBOL_REF:
2353 case LABEL_REF:
2354 return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2355 MAX_MACHINE_MODE);
2357 default:
2358 return 0;
2362 /* X is a doubleword constant that can be handled by splitting it into
2363 two words and loading each word separately. Return the number of
2364 instructions required to do this. */
2367 mips_split_const_insns (rtx x)
2369 unsigned int low, high;
2371 low = mips_const_insns (mips_subword (x, false));
2372 high = mips_const_insns (mips_subword (x, true));
2373 gcc_assert (low > 0 && high > 0);
2374 return low + high;
2377 /* Return the number of instructions needed to implement INSN,
2378 given that it loads from or stores to MEM. Count extended
2379 MIPS16 instructions as two instructions. */
2382 mips_load_store_insns (rtx mem, rtx insn)
2384 enum machine_mode mode;
2385 bool might_split_p;
2386 rtx set;
2388 gcc_assert (MEM_P (mem));
2389 mode = GET_MODE (mem);
2391 /* Try to prove that INSN does not need to be split. */
2392 might_split_p = true;
2393 if (GET_MODE_BITSIZE (mode) == 64)
2395 set = single_set (insn);
2396 if (set && !mips_split_64bit_move_p (SET_DEST (set), SET_SRC (set)))
2397 might_split_p = false;
2400 return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2403 /* Return the number of instructions needed for an integer division. */
2406 mips_idiv_insns (void)
2408 int count;
2410 count = 1;
2411 if (TARGET_CHECK_ZERO_DIV)
2413 if (GENERATE_DIVIDE_TRAPS)
2414 count++;
2415 else
2416 count += 2;
2419 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2420 count++;
2421 return count;
2424 /* Emit a move from SRC to DEST. Assume that the move expanders can
2425 handle all moves if !can_create_pseudo_p (). The distinction is
2426 important because, unlike emit_move_insn, the move expanders know
2427 how to force Pmode objects into the constant pool even when the
2428 constant pool address is not itself legitimate. */
2431 mips_emit_move (rtx dest, rtx src)
2433 return (can_create_pseudo_p ()
2434 ? emit_move_insn (dest, src)
2435 : emit_move_insn_1 (dest, src));
2438 /* Emit an instruction of the form (set TARGET (CODE OP0)). */
2440 static void
2441 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2443 emit_insn (gen_rtx_SET (VOIDmode, target,
2444 gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2447 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2448 Return that new register. */
2450 static rtx
2451 mips_force_unary (enum machine_mode mode, enum rtx_code code, rtx op0)
2453 rtx reg;
2455 reg = gen_reg_rtx (mode);
2456 mips_emit_unary (code, reg, op0);
2457 return reg;
2460 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2462 void
2463 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2465 emit_insn (gen_rtx_SET (VOIDmode, target,
2466 gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2469 /* Compute (CODE OP0 OP1) and store the result in a new register
2470 of mode MODE. Return that new register. */
2472 static rtx
2473 mips_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2475 rtx reg;
2477 reg = gen_reg_rtx (mode);
2478 mips_emit_binary (code, reg, op0, op1);
2479 return reg;
2482 /* Copy VALUE to a register and return that register. If new pseudos
2483 are allowed, copy it into a new register, otherwise use DEST. */
2485 static rtx
2486 mips_force_temporary (rtx dest, rtx value)
2488 if (can_create_pseudo_p ())
2489 return force_reg (Pmode, value);
2490 else
2492 mips_emit_move (dest, value);
2493 return dest;
2497 /* Emit a call sequence with call pattern PATTERN and return the call
2498 instruction itself (which is not necessarily the last instruction
2499 emitted). ORIG_ADDR is the original, unlegitimized address,
2500 ADDR is the legitimized form, and LAZY_P is true if the call
2501 address is lazily-bound. */
2503 static rtx
2504 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2506 rtx insn, reg;
2508 insn = emit_call_insn (pattern);
2510 if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2512 /* MIPS16 JALRs only take MIPS16 registers. If the target
2513 function requires $25 to be valid on entry, we must copy it
2514 there separately. The move instruction can be put in the
2515 call's delay slot. */
2516 reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2517 emit_insn_before (gen_move_insn (reg, addr), insn);
2518 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2521 if (lazy_p)
2522 /* Lazy-binding stubs require $gp to be valid on entry. */
2523 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2525 if (TARGET_USE_GOT)
2527 /* See the comment above load_call<mode> for details. */
2528 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2529 gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2530 emit_insn (gen_update_got_version ());
2532 return insn;
2535 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2536 then add CONST_INT OFFSET to the result. */
2538 static rtx
2539 mips_unspec_address_offset (rtx base, rtx offset,
2540 enum mips_symbol_type symbol_type)
2542 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2543 UNSPEC_ADDRESS_FIRST + symbol_type);
2544 if (offset != const0_rtx)
2545 base = gen_rtx_PLUS (Pmode, base, offset);
2546 return gen_rtx_CONST (Pmode, base);
2549 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2550 type SYMBOL_TYPE. */
2553 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2555 rtx base, offset;
2557 split_const (address, &base, &offset);
2558 return mips_unspec_address_offset (base, offset, symbol_type);
2561 /* If OP is an UNSPEC address, return the address to which it refers,
2562 otherwise return OP itself. */
2564 static rtx
2565 mips_strip_unspec_address (rtx op)
2567 rtx base, offset;
2569 split_const (op, &base, &offset);
2570 if (UNSPEC_ADDRESS_P (base))
2571 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2572 return op;
2575 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2576 high part to BASE and return the result. Just return BASE otherwise.
2577 TEMP is as for mips_force_temporary.
2579 The returned expression can be used as the first operand to a LO_SUM. */
2581 static rtx
2582 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2583 enum mips_symbol_type symbol_type)
2585 if (mips_split_p[symbol_type])
2587 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2588 addr = mips_force_temporary (temp, addr);
2589 base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2591 return base;
2594 /* Return an instruction that copies $gp into register REG. We want
2595 GCC to treat the register's value as constant, so that its value
2596 can be rematerialized on demand. */
2598 static rtx
2599 gen_load_const_gp (rtx reg)
2601 return PMODE_INSN (gen_load_const_gp, (reg));
2604 /* Return a pseudo register that contains the value of $gp throughout
2605 the current function. Such registers are needed by MIPS16 functions,
2606 for which $gp itself is not a valid base register or addition operand. */
2608 static rtx
2609 mips16_gp_pseudo_reg (void)
2611 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2613 rtx insn, scan;
2615 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2617 push_topmost_sequence ();
2619 scan = get_insns ();
2620 while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2621 scan = NEXT_INSN (scan);
2623 insn = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2624 insn = emit_insn_after (insn, scan);
2625 INSN_LOCATOR (insn) = 0;
2627 pop_topmost_sequence ();
2630 return cfun->machine->mips16_gp_pseudo_rtx;
2633 /* Return a base register that holds pic_offset_table_rtx.
2634 TEMP, if nonnull, is a scratch Pmode base register. */
2637 mips_pic_base_register (rtx temp)
2639 if (!TARGET_MIPS16)
2640 return pic_offset_table_rtx;
2642 if (currently_expanding_to_rtl)
2643 return mips16_gp_pseudo_reg ();
2645 if (can_create_pseudo_p ())
2646 temp = gen_reg_rtx (Pmode);
2648 if (TARGET_USE_GOT)
2649 /* The first post-reload split exposes all references to $gp
2650 (both uses and definitions). All references must remain
2651 explicit after that point.
2653 It is safe to introduce uses of $gp at any time, so for
2654 simplicity, we do that before the split too. */
2655 mips_emit_move (temp, pic_offset_table_rtx);
2656 else
2657 emit_insn (gen_load_const_gp (temp));
2658 return temp;
2661 /* Return the RHS of a load_call<mode> insn. */
2663 static rtx
2664 mips_unspec_call (rtx reg, rtx symbol)
2666 rtvec vec;
2668 vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2669 return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2672 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2673 reference. Return NULL_RTX otherwise. */
2675 static rtx
2676 mips_strip_unspec_call (rtx src)
2678 if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
2679 return mips_strip_unspec_address (XVECEXP (src, 0, 1));
2680 return NULL_RTX;
2683 /* Create and return a GOT reference of type TYPE for address ADDR.
2684 TEMP, if nonnull, is a scratch Pmode base register. */
2687 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
2689 rtx base, high, lo_sum_symbol;
2691 base = mips_pic_base_register (temp);
2693 /* If we used the temporary register to load $gp, we can't use
2694 it for the high part as well. */
2695 if (temp != NULL && reg_overlap_mentioned_p (base, temp))
2696 temp = NULL;
2698 high = mips_unspec_offset_high (temp, base, addr, type);
2699 lo_sum_symbol = mips_unspec_address (addr, type);
2701 if (type == SYMBOL_GOTOFF_CALL)
2702 return mips_unspec_call (high, lo_sum_symbol);
2703 else
2704 return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
2707 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
2708 it appears in a MEM of that mode. Return true if ADDR is a legitimate
2709 constant in that context and can be split into high and low parts.
2710 If so, and if LOW_OUT is nonnull, emit the high part and store the
2711 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
2713 TEMP is as for mips_force_temporary and is used to load the high
2714 part into a register.
2716 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
2717 a legitimize SET_SRC for an .md pattern, otherwise the low part
2718 is guaranteed to be a legitimate address for mode MODE. */
2720 bool
2721 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
2723 enum mips_symbol_context context;
2724 enum mips_symbol_type symbol_type;
2725 rtx high;
2727 context = (mode == MAX_MACHINE_MODE
2728 ? SYMBOL_CONTEXT_LEA
2729 : SYMBOL_CONTEXT_MEM);
2730 if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
2732 addr = XEXP (addr, 0);
2733 if (mips_symbolic_constant_p (addr, context, &symbol_type)
2734 && mips_symbol_insns (symbol_type, mode) > 0
2735 && mips_split_hi_p[symbol_type])
2737 if (low_out)
2738 switch (symbol_type)
2740 case SYMBOL_GOT_PAGE_OFST:
2741 /* The high part of a page/ofst pair is loaded from the GOT. */
2742 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
2743 break;
2745 default:
2746 gcc_unreachable ();
2748 return true;
2751 else
2753 if (mips_symbolic_constant_p (addr, context, &symbol_type)
2754 && mips_symbol_insns (symbol_type, mode) > 0
2755 && mips_split_p[symbol_type])
2757 if (low_out)
2758 switch (symbol_type)
2760 case SYMBOL_GOT_DISP:
2761 /* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
2762 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
2763 break;
2765 case SYMBOL_GP_RELATIVE:
2766 high = mips_pic_base_register (temp);
2767 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2768 break;
2770 default:
2771 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
2772 high = mips_force_temporary (temp, high);
2773 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2774 break;
2776 return true;
2779 return false;
2782 /* Return a legitimate address for REG + OFFSET. TEMP is as for
2783 mips_force_temporary; it is only needed when OFFSET is not a
2784 SMALL_OPERAND. */
2786 static rtx
2787 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2789 if (!SMALL_OPERAND (offset))
2791 rtx high;
2793 if (TARGET_MIPS16)
2795 /* Load the full offset into a register so that we can use
2796 an unextended instruction for the address itself. */
2797 high = GEN_INT (offset);
2798 offset = 0;
2800 else
2802 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
2803 The addition inside the macro CONST_HIGH_PART may cause an
2804 overflow, so we need to force a sign-extension check. */
2805 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
2806 offset = CONST_LOW_PART (offset);
2808 high = mips_force_temporary (temp, high);
2809 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
2811 return plus_constant (Pmode, reg, offset);
2814 /* The __tls_get_attr symbol. */
2815 static GTY(()) rtx mips_tls_symbol;
2817 /* Return an instruction sequence that calls __tls_get_addr. SYM is
2818 the TLS symbol we are referencing and TYPE is the symbol type to use
2819 (either global dynamic or local dynamic). V0 is an RTX for the
2820 return value location. */
2822 static rtx
2823 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
2825 rtx insn, loc, a0;
2827 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
2829 if (!mips_tls_symbol)
2830 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
2832 loc = mips_unspec_address (sym, type);
2834 start_sequence ();
2836 emit_insn (gen_rtx_SET (Pmode, a0,
2837 gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
2838 insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
2839 const0_rtx, NULL_RTX, false);
2840 RTL_CONST_CALL_P (insn) = 1;
2841 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
2842 insn = get_insns ();
2844 end_sequence ();
2846 return insn;
2849 /* Return a pseudo register that contains the current thread pointer. */
2851 static rtx
2852 mips_get_tp (void)
2854 rtx tp, fn;
2856 tp = gen_reg_rtx (Pmode);
2857 if (TARGET_MIPS16)
2859 mips_need_mips16_rdhwr_p = true;
2860 fn = mips16_stub_function ("__mips16_rdhwr");
2861 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
2862 if (!call_insn_operand (fn, VOIDmode))
2863 fn = force_reg (Pmode, fn);
2864 emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
2866 else
2867 emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
2868 return tp;
2871 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
2872 its address. The return value will be both a valid address and a valid
2873 SET_SRC (either a REG or a LO_SUM). */
2875 static rtx
2876 mips_legitimize_tls_address (rtx loc)
2878 rtx dest, insn, v0, tp, tmp1, tmp2, eqv, offset;
2879 enum tls_model model;
2881 model = SYMBOL_REF_TLS_MODEL (loc);
2882 /* Only TARGET_ABICALLS code can have more than one module; other
2883 code must be be static and should not use a GOT. All TLS models
2884 reduce to local exec in this situation. */
2885 if (!TARGET_ABICALLS)
2886 model = TLS_MODEL_LOCAL_EXEC;
2888 switch (model)
2890 case TLS_MODEL_GLOBAL_DYNAMIC:
2891 v0 = gen_rtx_REG (Pmode, GP_RETURN);
2892 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
2893 dest = gen_reg_rtx (Pmode);
2894 emit_libcall_block (insn, dest, v0, loc);
2895 break;
2897 case TLS_MODEL_LOCAL_DYNAMIC:
2898 v0 = gen_rtx_REG (Pmode, GP_RETURN);
2899 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
2900 tmp1 = gen_reg_rtx (Pmode);
2902 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2903 share the LDM result with other LD model accesses. */
2904 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2905 UNSPEC_TLS_LDM);
2906 emit_libcall_block (insn, tmp1, v0, eqv);
2908 offset = mips_unspec_address (loc, SYMBOL_DTPREL);
2909 if (mips_split_p[SYMBOL_DTPREL])
2911 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
2912 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
2914 else
2915 dest = expand_binop (Pmode, add_optab, tmp1, offset,
2916 0, 0, OPTAB_DIRECT);
2917 break;
2919 case TLS_MODEL_INITIAL_EXEC:
2920 tp = mips_get_tp ();
2921 tmp1 = gen_reg_rtx (Pmode);
2922 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
2923 if (Pmode == DImode)
2924 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
2925 else
2926 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
2927 dest = gen_reg_rtx (Pmode);
2928 emit_insn (gen_add3_insn (dest, tmp1, tp));
2929 break;
2931 case TLS_MODEL_LOCAL_EXEC:
2932 tmp1 = mips_get_tp ();
2933 offset = mips_unspec_address (loc, SYMBOL_TPREL);
2934 if (mips_split_p[SYMBOL_TPREL])
2936 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
2937 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
2939 else
2940 dest = expand_binop (Pmode, add_optab, tmp1, offset,
2941 0, 0, OPTAB_DIRECT);
2942 break;
2944 default:
2945 gcc_unreachable ();
2947 return dest;
2950 /* If X is not a valid address for mode MODE, force it into a register. */
2952 static rtx
2953 mips_force_address (rtx x, enum machine_mode mode)
2955 if (!mips_legitimate_address_p (mode, x, false))
2956 x = force_reg (Pmode, x);
2957 return x;
2960 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
2961 be legitimized in a way that the generic machinery might not expect,
2962 return a new address, otherwise return NULL. MODE is the mode of
2963 the memory being accessed. */
2965 static rtx
2966 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
2967 enum machine_mode mode)
2969 rtx base, addr;
2970 HOST_WIDE_INT offset;
2972 if (mips_tls_symbol_p (x))
2973 return mips_legitimize_tls_address (x);
2975 /* See if the address can split into a high part and a LO_SUM. */
2976 if (mips_split_symbol (NULL, x, mode, &addr))
2977 return mips_force_address (addr, mode);
2979 /* Handle BASE + OFFSET using mips_add_offset. */
2980 mips_split_plus (x, &base, &offset);
2981 if (offset != 0)
2983 if (!mips_valid_base_register_p (base, mode, false))
2984 base = copy_to_mode_reg (Pmode, base);
2985 addr = mips_add_offset (NULL, base, offset);
2986 return mips_force_address (addr, mode);
2989 return x;
2992 /* Load VALUE into DEST. TEMP is as for mips_force_temporary. */
2994 void
2995 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
2997 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2998 enum machine_mode mode;
2999 unsigned int i, num_ops;
3000 rtx x;
3002 mode = GET_MODE (dest);
3003 num_ops = mips_build_integer (codes, value);
3005 /* Apply each binary operation to X. Invariant: X is a legitimate
3006 source operand for a SET pattern. */
3007 x = GEN_INT (codes[0].value);
3008 for (i = 1; i < num_ops; i++)
3010 if (!can_create_pseudo_p ())
3012 emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3013 x = temp;
3015 else
3016 x = force_reg (mode, x);
3017 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3020 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3023 /* Subroutine of mips_legitimize_move. Move constant SRC into register
3024 DEST given that SRC satisfies immediate_operand but doesn't satisfy
3025 move_operand. */
3027 static void
3028 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
3030 rtx base, offset;
3032 /* Split moves of big integers into smaller pieces. */
3033 if (splittable_const_int_operand (src, mode))
3035 mips_move_integer (dest, dest, INTVAL (src));
3036 return;
3039 /* Split moves of symbolic constants into high/low pairs. */
3040 if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3042 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3043 return;
3046 /* Generate the appropriate access sequences for TLS symbols. */
3047 if (mips_tls_symbol_p (src))
3049 mips_emit_move (dest, mips_legitimize_tls_address (src));
3050 return;
3053 /* If we have (const (plus symbol offset)), and that expression cannot
3054 be forced into memory, load the symbol first and add in the offset.
3055 In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3056 forced into memory, as it usually produces better code. */
3057 split_const (src, &base, &offset);
3058 if (offset != const0_rtx
3059 && (targetm.cannot_force_const_mem (mode, src)
3060 || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3062 base = mips_force_temporary (dest, base);
3063 mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3064 return;
3067 src = force_const_mem (mode, src);
3069 /* When using explicit relocs, constant pool references are sometimes
3070 not legitimate addresses. */
3071 mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3072 mips_emit_move (dest, src);
3075 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3076 sequence that is valid. */
3078 bool
3079 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
3081 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3083 mips_emit_move (dest, force_reg (mode, src));
3084 return true;
3087 /* We need to deal with constants that would be legitimate
3088 immediate_operands but aren't legitimate move_operands. */
3089 if (CONSTANT_P (src) && !move_operand (src, mode))
3091 mips_legitimize_const_move (mode, dest, src);
3092 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3093 return true;
3095 return false;
3098 /* Return true if value X in context CONTEXT is a small-data address
3099 that can be rewritten as a LO_SUM. */
3101 static bool
3102 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3104 enum mips_symbol_type symbol_type;
3106 return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3107 && !mips_split_p[SYMBOL_GP_RELATIVE]
3108 && mips_symbolic_constant_p (x, context, &symbol_type)
3109 && symbol_type == SYMBOL_GP_RELATIVE);
3112 /* A for_each_rtx callback for mips_small_data_pattern_p. DATA is the
3113 containing MEM, or null if none. */
3115 static int
3116 mips_small_data_pattern_1 (rtx *loc, void *data)
3118 enum mips_symbol_context context;
3120 /* Ignore things like "g" constraints in asms. We make no particular
3121 guarantee about which symbolic constants are acceptable as asm operands
3122 versus which must be forced into a GPR. */
3123 if (GET_CODE (*loc) == LO_SUM || GET_CODE (*loc) == ASM_OPERANDS)
3124 return -1;
3126 if (MEM_P (*loc))
3128 if (for_each_rtx (&XEXP (*loc, 0), mips_small_data_pattern_1, *loc))
3129 return 1;
3130 return -1;
3133 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3134 return mips_rewrite_small_data_p (*loc, context);
3137 /* Return true if OP refers to small data symbols directly, not through
3138 a LO_SUM. */
3140 bool
3141 mips_small_data_pattern_p (rtx op)
3143 return for_each_rtx (&op, mips_small_data_pattern_1, NULL);
3146 /* A for_each_rtx callback, used by mips_rewrite_small_data.
3147 DATA is the containing MEM, or null if none. */
3149 static int
3150 mips_rewrite_small_data_1 (rtx *loc, void *data)
3152 enum mips_symbol_context context;
3154 if (MEM_P (*loc))
3156 for_each_rtx (&XEXP (*loc, 0), mips_rewrite_small_data_1, *loc);
3157 return -1;
3160 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3161 if (mips_rewrite_small_data_p (*loc, context))
3162 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3164 if (GET_CODE (*loc) == LO_SUM)
3165 return -1;
3167 return 0;
3170 /* Rewrite instruction pattern PATTERN so that it refers to small data
3171 using explicit relocations. */
3174 mips_rewrite_small_data (rtx pattern)
3176 pattern = copy_insn (pattern);
3177 for_each_rtx (&pattern, mips_rewrite_small_data_1, NULL);
3178 return pattern;
3181 /* We need a lot of little routines to check the range of MIPS16 immediate
3182 operands. */
3184 static int
3185 m16_check_op (rtx op, int low, int high, int mask)
3187 return (CONST_INT_P (op)
3188 && IN_RANGE (INTVAL (op), low, high)
3189 && (INTVAL (op) & mask) == 0);
3193 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3195 return m16_check_op (op, 0x1, 0x8, 0);
3199 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3201 return m16_check_op (op, -0x8, 0x7, 0);
3205 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3207 return m16_check_op (op, -0x7, 0x8, 0);
3211 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3213 return m16_check_op (op, -0x10, 0xf, 0);
3217 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3219 return m16_check_op (op, -0xf, 0x10, 0);
3223 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3225 return m16_check_op (op, -0x10 << 2, 0xf << 2, 3);
3229 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3231 return m16_check_op (op, -0xf << 2, 0x10 << 2, 3);
3235 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3237 return m16_check_op (op, -0x80, 0x7f, 0);
3241 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3243 return m16_check_op (op, -0x7f, 0x80, 0);
3247 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3249 return m16_check_op (op, 0x0, 0xff, 0);
3253 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3255 return m16_check_op (op, -0xff, 0x0, 0);
3259 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3261 return m16_check_op (op, -0x1, 0xfe, 0);
3265 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3267 return m16_check_op (op, 0x0, 0xff << 2, 3);
3271 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3273 return m16_check_op (op, -0xff << 2, 0x0, 3);
3277 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3279 return m16_check_op (op, -0x80 << 3, 0x7f << 3, 7);
3283 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3285 return m16_check_op (op, -0x7f << 3, 0x80 << 3, 7);
3288 /* The cost of loading values from the constant pool. It should be
3289 larger than the cost of any constant we want to synthesize inline. */
3290 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3292 /* Return the cost of X when used as an operand to the MIPS16 instruction
3293 that implements CODE. Return -1 if there is no such instruction, or if
3294 X is not a valid immediate operand for it. */
3296 static int
3297 mips16_constant_cost (int code, HOST_WIDE_INT x)
3299 switch (code)
3301 case ASHIFT:
3302 case ASHIFTRT:
3303 case LSHIFTRT:
3304 /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3305 other shifts are extended. The shift patterns truncate the shift
3306 count to the right size, so there are no out-of-range values. */
3307 if (IN_RANGE (x, 1, 8))
3308 return 0;
3309 return COSTS_N_INSNS (1);
3311 case PLUS:
3312 if (IN_RANGE (x, -128, 127))
3313 return 0;
3314 if (SMALL_OPERAND (x))
3315 return COSTS_N_INSNS (1);
3316 return -1;
3318 case LEU:
3319 /* Like LE, but reject the always-true case. */
3320 if (x == -1)
3321 return -1;
3322 case LE:
3323 /* We add 1 to the immediate and use SLT. */
3324 x += 1;
3325 case XOR:
3326 /* We can use CMPI for an xor with an unsigned 16-bit X. */
3327 case LT:
3328 case LTU:
3329 if (IN_RANGE (x, 0, 255))
3330 return 0;
3331 if (SMALL_OPERAND_UNSIGNED (x))
3332 return COSTS_N_INSNS (1);
3333 return -1;
3335 case EQ:
3336 case NE:
3337 /* Equality comparisons with 0 are cheap. */
3338 if (x == 0)
3339 return 0;
3340 return -1;
3342 default:
3343 return -1;
3347 /* Return true if there is a non-MIPS16 instruction that implements CODE
3348 and if that instruction accepts X as an immediate operand. */
3350 static int
3351 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3353 switch (code)
3355 case ASHIFT:
3356 case ASHIFTRT:
3357 case LSHIFTRT:
3358 /* All shift counts are truncated to a valid constant. */
3359 return true;
3361 case ROTATE:
3362 case ROTATERT:
3363 /* Likewise rotates, if the target supports rotates at all. */
3364 return ISA_HAS_ROR;
3366 case AND:
3367 case IOR:
3368 case XOR:
3369 /* These instructions take 16-bit unsigned immediates. */
3370 return SMALL_OPERAND_UNSIGNED (x);
3372 case PLUS:
3373 case LT:
3374 case LTU:
3375 /* These instructions take 16-bit signed immediates. */
3376 return SMALL_OPERAND (x);
3378 case EQ:
3379 case NE:
3380 case GT:
3381 case GTU:
3382 /* The "immediate" forms of these instructions are really
3383 implemented as comparisons with register 0. */
3384 return x == 0;
3386 case GE:
3387 case GEU:
3388 /* Likewise, meaning that the only valid immediate operand is 1. */
3389 return x == 1;
3391 case LE:
3392 /* We add 1 to the immediate and use SLT. */
3393 return SMALL_OPERAND (x + 1);
3395 case LEU:
3396 /* Likewise SLTU, but reject the always-true case. */
3397 return SMALL_OPERAND (x + 1) && x + 1 != 0;
3399 case SIGN_EXTRACT:
3400 case ZERO_EXTRACT:
3401 /* The bit position and size are immediate operands. */
3402 return ISA_HAS_EXT_INS;
3404 default:
3405 /* By default assume that $0 can be used for 0. */
3406 return x == 0;
3410 /* Return the cost of binary operation X, given that the instruction
3411 sequence for a word-sized or smaller operation has cost SINGLE_COST
3412 and that the sequence of a double-word operation has cost DOUBLE_COST.
3413 If SPEED is true, optimize for speed otherwise optimize for size. */
3415 static int
3416 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3418 int cost;
3420 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3421 cost = double_cost;
3422 else
3423 cost = single_cost;
3424 return (cost
3425 + set_src_cost (XEXP (x, 0), speed)
3426 + rtx_cost (XEXP (x, 1), GET_CODE (x), 1, speed));
3429 /* Return the cost of floating-point multiplications of mode MODE. */
3431 static int
3432 mips_fp_mult_cost (enum machine_mode mode)
3434 return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3437 /* Return the cost of floating-point divisions of mode MODE. */
3439 static int
3440 mips_fp_div_cost (enum machine_mode mode)
3442 return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3445 /* Return the cost of sign-extending OP to mode MODE, not including the
3446 cost of OP itself. */
3448 static int
3449 mips_sign_extend_cost (enum machine_mode mode, rtx op)
3451 if (MEM_P (op))
3452 /* Extended loads are as cheap as unextended ones. */
3453 return 0;
3455 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3456 /* A sign extension from SImode to DImode in 64-bit mode is free. */
3457 return 0;
3459 if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3460 /* We can use SEB or SEH. */
3461 return COSTS_N_INSNS (1);
3463 /* We need to use a shift left and a shift right. */
3464 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3467 /* Return the cost of zero-extending OP to mode MODE, not including the
3468 cost of OP itself. */
3470 static int
3471 mips_zero_extend_cost (enum machine_mode mode, rtx op)
3473 if (MEM_P (op))
3474 /* Extended loads are as cheap as unextended ones. */
3475 return 0;
3477 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3478 /* We need a shift left by 32 bits and a shift right by 32 bits. */
3479 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3481 if (GENERATE_MIPS16E)
3482 /* We can use ZEB or ZEH. */
3483 return COSTS_N_INSNS (1);
3485 if (TARGET_MIPS16)
3486 /* We need to load 0xff or 0xffff into a register and use AND. */
3487 return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3489 /* We can use ANDI. */
3490 return COSTS_N_INSNS (1);
3493 /* Return the cost of moving between two registers of mode MODE,
3494 assuming that the move will be in pieces of at most UNITS bytes. */
3496 static int
3497 mips_set_reg_reg_piece_cost (enum machine_mode mode, unsigned int units)
3499 return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3502 /* Return the cost of moving between two registers of mode MODE. */
3504 static int
3505 mips_set_reg_reg_cost (enum machine_mode mode)
3507 switch (GET_MODE_CLASS (mode))
3509 case MODE_CC:
3510 return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3512 case MODE_FLOAT:
3513 case MODE_COMPLEX_FLOAT:
3514 case MODE_VECTOR_FLOAT:
3515 if (TARGET_HARD_FLOAT)
3516 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3517 /* Fall through */
3519 default:
3520 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3524 /* Implement TARGET_RTX_COSTS. */
3526 static bool
3527 mips_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3528 int *total, bool speed)
3530 enum machine_mode mode = GET_MODE (x);
3531 bool float_mode_p = FLOAT_MODE_P (mode);
3532 int cost;
3533 rtx addr;
3535 /* The cost of a COMPARE is hard to define for MIPS. COMPAREs don't
3536 appear in the instruction stream, and the cost of a comparison is
3537 really the cost of the branch or scc condition. At the time of
3538 writing, GCC only uses an explicit outer COMPARE code when optabs
3539 is testing whether a constant is expensive enough to force into a
3540 register. We want optabs to pass such constants through the MIPS
3541 expanders instead, so make all constants very cheap here. */
3542 if (outer_code == COMPARE)
3544 gcc_assert (CONSTANT_P (x));
3545 *total = 0;
3546 return true;
3549 switch (code)
3551 case CONST_INT:
3552 /* Treat *clear_upper32-style ANDs as having zero cost in the
3553 second operand. The cost is entirely in the first operand.
3555 ??? This is needed because we would otherwise try to CSE
3556 the constant operand. Although that's the right thing for
3557 instructions that continue to be a register operation throughout
3558 compilation, it is disastrous for instructions that could
3559 later be converted into a memory operation. */
3560 if (TARGET_64BIT
3561 && outer_code == AND
3562 && UINTVAL (x) == 0xffffffff)
3564 *total = 0;
3565 return true;
3568 if (TARGET_MIPS16)
3570 cost = mips16_constant_cost (outer_code, INTVAL (x));
3571 if (cost >= 0)
3573 *total = cost;
3574 return true;
3577 else
3579 /* When not optimizing for size, we care more about the cost
3580 of hot code, and hot code is often in a loop. If a constant
3581 operand needs to be forced into a register, we will often be
3582 able to hoist the constant load out of the loop, so the load
3583 should not contribute to the cost. */
3584 if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3586 *total = 0;
3587 return true;
3590 /* Fall through. */
3592 case CONST:
3593 case SYMBOL_REF:
3594 case LABEL_REF:
3595 case CONST_DOUBLE:
3596 if (force_to_mem_operand (x, VOIDmode))
3598 *total = COSTS_N_INSNS (1);
3599 return true;
3601 cost = mips_const_insns (x);
3602 if (cost > 0)
3604 /* If the constant is likely to be stored in a GPR, SETs of
3605 single-insn constants are as cheap as register sets; we
3606 never want to CSE them.
3608 Don't reduce the cost of storing a floating-point zero in
3609 FPRs. If we have a zero in an FPR for other reasons, we
3610 can get better cfg-cleanup and delayed-branch results by
3611 using it consistently, rather than using $0 sometimes and
3612 an FPR at other times. Also, moves between floating-point
3613 registers are sometimes cheaper than (D)MTC1 $0. */
3614 if (cost == 1
3615 && outer_code == SET
3616 && !(float_mode_p && TARGET_HARD_FLOAT))
3617 cost = 0;
3618 /* When non-MIPS16 code loads a constant N>1 times, we rarely
3619 want to CSE the constant itself. It is usually better to
3620 have N copies of the last operation in the sequence and one
3621 shared copy of the other operations. (Note that this is
3622 not true for MIPS16 code, where the final operation in the
3623 sequence is often an extended instruction.)
3625 Also, if we have a CONST_INT, we don't know whether it is
3626 for a word or doubleword operation, so we cannot rely on
3627 the result of mips_build_integer. */
3628 else if (!TARGET_MIPS16
3629 && (outer_code == SET || mode == VOIDmode))
3630 cost = 1;
3631 *total = COSTS_N_INSNS (cost);
3632 return true;
3634 /* The value will need to be fetched from the constant pool. */
3635 *total = CONSTANT_POOL_COST;
3636 return true;
3638 case MEM:
3639 /* If the address is legitimate, return the number of
3640 instructions it needs. */
3641 addr = XEXP (x, 0);
3642 cost = mips_address_insns (addr, mode, true);
3643 if (cost > 0)
3645 *total = COSTS_N_INSNS (cost + 1);
3646 return true;
3648 /* Check for a scaled indexed address. */
3649 if (mips_lwxs_address_p (addr)
3650 || mips_lx_address_p (addr, mode))
3652 *total = COSTS_N_INSNS (2);
3653 return true;
3655 /* Otherwise use the default handling. */
3656 return false;
3658 case FFS:
3659 *total = COSTS_N_INSNS (6);
3660 return false;
3662 case NOT:
3663 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3664 return false;
3666 case AND:
3667 /* Check for a *clear_upper32 pattern and treat it like a zero
3668 extension. See the pattern's comment for details. */
3669 if (TARGET_64BIT
3670 && mode == DImode
3671 && CONST_INT_P (XEXP (x, 1))
3672 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3674 *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3675 + set_src_cost (XEXP (x, 0), speed));
3676 return true;
3678 /* Fall through. */
3680 case IOR:
3681 case XOR:
3682 /* Double-word operations use two single-word operations. */
3683 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3684 speed);
3685 return true;
3687 case ASHIFT:
3688 case ASHIFTRT:
3689 case LSHIFTRT:
3690 case ROTATE:
3691 case ROTATERT:
3692 if (CONSTANT_P (XEXP (x, 1)))
3693 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3694 speed);
3695 else
3696 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
3697 speed);
3698 return true;
3700 case ABS:
3701 if (float_mode_p)
3702 *total = mips_cost->fp_add;
3703 else
3704 *total = COSTS_N_INSNS (4);
3705 return false;
3707 case LO_SUM:
3708 /* Low-part immediates need an extended MIPS16 instruction. */
3709 *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3710 + set_src_cost (XEXP (x, 0), speed));
3711 return true;
3713 case LT:
3714 case LTU:
3715 case LE:
3716 case LEU:
3717 case GT:
3718 case GTU:
3719 case GE:
3720 case GEU:
3721 case EQ:
3722 case NE:
3723 case UNORDERED:
3724 case LTGT:
3725 /* Branch comparisons have VOIDmode, so use the first operand's
3726 mode instead. */
3727 mode = GET_MODE (XEXP (x, 0));
3728 if (FLOAT_MODE_P (mode))
3730 *total = mips_cost->fp_add;
3731 return false;
3733 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3734 speed);
3735 return true;
3737 case MINUS:
3738 if (float_mode_p
3739 && (ISA_HAS_NMADD4_NMSUB4 (mode) || ISA_HAS_NMADD3_NMSUB3 (mode))
3740 && TARGET_FUSED_MADD
3741 && !HONOR_NANS (mode)
3742 && !HONOR_SIGNED_ZEROS (mode))
3744 /* See if we can use NMADD or NMSUB. See mips.md for the
3745 associated patterns. */
3746 rtx op0 = XEXP (x, 0);
3747 rtx op1 = XEXP (x, 1);
3748 if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
3750 *total = (mips_fp_mult_cost (mode)
3751 + set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
3752 + set_src_cost (XEXP (op0, 1), speed)
3753 + set_src_cost (op1, speed));
3754 return true;
3756 if (GET_CODE (op1) == MULT)
3758 *total = (mips_fp_mult_cost (mode)
3759 + set_src_cost (op0, speed)
3760 + set_src_cost (XEXP (op1, 0), speed)
3761 + set_src_cost (XEXP (op1, 1), speed));
3762 return true;
3765 /* Fall through. */
3767 case PLUS:
3768 if (float_mode_p)
3770 /* If this is part of a MADD or MSUB, treat the PLUS as
3771 being free. */
3772 if (ISA_HAS_FP4
3773 && TARGET_FUSED_MADD
3774 && GET_CODE (XEXP (x, 0)) == MULT)
3775 *total = 0;
3776 else
3777 *total = mips_cost->fp_add;
3778 return false;
3781 /* Double-word operations require three single-word operations and
3782 an SLTU. The MIPS16 version then needs to move the result of
3783 the SLTU from $24 to a MIPS16 register. */
3784 *total = mips_binary_cost (x, COSTS_N_INSNS (1),
3785 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
3786 speed);
3787 return true;
3789 case NEG:
3790 if (float_mode_p
3791 && (ISA_HAS_NMADD4_NMSUB4 (mode) || ISA_HAS_NMADD3_NMSUB3 (mode))
3792 && TARGET_FUSED_MADD
3793 && !HONOR_NANS (mode)
3794 && HONOR_SIGNED_ZEROS (mode))
3796 /* See if we can use NMADD or NMSUB. See mips.md for the
3797 associated patterns. */
3798 rtx op = XEXP (x, 0);
3799 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3800 && GET_CODE (XEXP (op, 0)) == MULT)
3802 *total = (mips_fp_mult_cost (mode)
3803 + set_src_cost (XEXP (XEXP (op, 0), 0), speed)
3804 + set_src_cost (XEXP (XEXP (op, 0), 1), speed)
3805 + set_src_cost (XEXP (op, 1), speed));
3806 return true;
3810 if (float_mode_p)
3811 *total = mips_cost->fp_add;
3812 else
3813 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
3814 return false;
3816 case MULT:
3817 if (float_mode_p)
3818 *total = mips_fp_mult_cost (mode);
3819 else if (mode == DImode && !TARGET_64BIT)
3820 /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
3821 where the mulsidi3 always includes an MFHI and an MFLO. */
3822 *total = (speed
3823 ? mips_cost->int_mult_si * 3 + 6
3824 : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
3825 else if (!speed)
3826 *total = (ISA_HAS_MUL3 ? 1 : 2);
3827 else if (mode == DImode)
3828 *total = mips_cost->int_mult_di;
3829 else
3830 *total = mips_cost->int_mult_si;
3831 return false;
3833 case DIV:
3834 /* Check for a reciprocal. */
3835 if (float_mode_p
3836 && ISA_HAS_FP4
3837 && flag_unsafe_math_optimizations
3838 && XEXP (x, 0) == CONST1_RTX (mode))
3840 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
3841 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
3842 division as being free. */
3843 *total = set_src_cost (XEXP (x, 1), speed);
3844 else
3845 *total = (mips_fp_div_cost (mode)
3846 + set_src_cost (XEXP (x, 1), speed));
3847 return true;
3849 /* Fall through. */
3851 case SQRT:
3852 case MOD:
3853 if (float_mode_p)
3855 *total = mips_fp_div_cost (mode);
3856 return false;
3858 /* Fall through. */
3860 case UDIV:
3861 case UMOD:
3862 if (!speed)
3864 /* It is our responsibility to make division by a power of 2
3865 as cheap as 2 register additions if we want the division
3866 expanders to be used for such operations; see the setting
3867 of sdiv_pow2_cheap in optabs.c. Using (D)DIV for MIPS16
3868 should always produce shorter code than using
3869 expand_sdiv2_pow2. */
3870 if (TARGET_MIPS16
3871 && CONST_INT_P (XEXP (x, 1))
3872 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
3874 *total = COSTS_N_INSNS (2) + set_src_cost (XEXP (x, 0), speed);
3875 return true;
3877 *total = COSTS_N_INSNS (mips_idiv_insns ());
3879 else if (mode == DImode)
3880 *total = mips_cost->int_div_di;
3881 else
3882 *total = mips_cost->int_div_si;
3883 return false;
3885 case SIGN_EXTEND:
3886 *total = mips_sign_extend_cost (mode, XEXP (x, 0));
3887 return false;
3889 case ZERO_EXTEND:
3890 if (outer_code == SET
3891 && ISA_HAS_BADDU
3892 && (GET_CODE (XEXP (x, 0)) == TRUNCATE
3893 || GET_CODE (XEXP (x, 0)) == SUBREG)
3894 && GET_MODE (XEXP (x, 0)) == QImode
3895 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
3897 *total = set_src_cost (XEXP (XEXP (x, 0), 0), speed);
3898 return true;
3900 *total = mips_zero_extend_cost (mode, XEXP (x, 0));
3901 return false;
3903 case FLOAT:
3904 case UNSIGNED_FLOAT:
3905 case FIX:
3906 case FLOAT_EXTEND:
3907 case FLOAT_TRUNCATE:
3908 *total = mips_cost->fp_add;
3909 return false;
3911 case SET:
3912 if (register_operand (SET_DEST (x), VOIDmode)
3913 && reg_or_0_operand (SET_SRC (x), VOIDmode))
3915 *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
3916 return true;
3918 return false;
3920 default:
3921 return false;
3925 /* Implement TARGET_ADDRESS_COST. */
3927 static int
3928 mips_address_cost (rtx addr, bool speed ATTRIBUTE_UNUSED)
3930 return mips_address_insns (addr, SImode, false);
3933 /* Information about a single instruction in a multi-instruction
3934 asm sequence. */
3935 struct mips_multi_member {
3936 /* True if this is a label, false if it is code. */
3937 bool is_label_p;
3939 /* The output_asm_insn format of the instruction. */
3940 const char *format;
3942 /* The operands to the instruction. */
3943 rtx operands[MAX_RECOG_OPERANDS];
3945 typedef struct mips_multi_member mips_multi_member;
3947 /* Vector definitions for the above. */
3948 DEF_VEC_O(mips_multi_member);
3949 DEF_VEC_ALLOC_O(mips_multi_member, heap);
3951 /* The instructions that make up the current multi-insn sequence. */
3952 static VEC (mips_multi_member, heap) *mips_multi_members;
3954 /* How many instructions (as opposed to labels) are in the current
3955 multi-insn sequence. */
3956 static unsigned int mips_multi_num_insns;
3958 /* Start a new multi-insn sequence. */
3960 static void
3961 mips_multi_start (void)
3963 VEC_truncate (mips_multi_member, mips_multi_members, 0);
3964 mips_multi_num_insns = 0;
3967 /* Add a new, uninitialized member to the current multi-insn sequence. */
3969 static struct mips_multi_member *
3970 mips_multi_add (void)
3972 return VEC_safe_push (mips_multi_member, heap, mips_multi_members, 0);
3975 /* Add a normal insn with the given asm format to the current multi-insn
3976 sequence. The other arguments are a null-terminated list of operands. */
3978 static void
3979 mips_multi_add_insn (const char *format, ...)
3981 struct mips_multi_member *member;
3982 va_list ap;
3983 unsigned int i;
3984 rtx op;
3986 member = mips_multi_add ();
3987 member->is_label_p = false;
3988 member->format = format;
3989 va_start (ap, format);
3990 i = 0;
3991 while ((op = va_arg (ap, rtx)))
3992 member->operands[i++] = op;
3993 va_end (ap);
3994 mips_multi_num_insns++;
3997 /* Add the given label definition to the current multi-insn sequence.
3998 The definition should include the colon. */
4000 static void
4001 mips_multi_add_label (const char *label)
4003 struct mips_multi_member *member;
4005 member = mips_multi_add ();
4006 member->is_label_p = true;
4007 member->format = label;
4010 /* Return the index of the last member of the current multi-insn sequence. */
4012 static unsigned int
4013 mips_multi_last_index (void)
4015 return VEC_length (mips_multi_member, mips_multi_members) - 1;
4018 /* Add a copy of an existing instruction to the current multi-insn
4019 sequence. I is the index of the instruction that should be copied. */
4021 static void
4022 mips_multi_copy_insn (unsigned int i)
4024 struct mips_multi_member *member;
4026 member = mips_multi_add ();
4027 memcpy (member, VEC_index (mips_multi_member, mips_multi_members, i),
4028 sizeof (*member));
4029 gcc_assert (!member->is_label_p);
4032 /* Change the operand of an existing instruction in the current
4033 multi-insn sequence. I is the index of the instruction,
4034 OP is the index of the operand, and X is the new value. */
4036 static void
4037 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4039 VEC_index (mips_multi_member, mips_multi_members, i)->operands[op] = x;
4042 /* Write out the asm code for the current multi-insn sequence. */
4044 static void
4045 mips_multi_write (void)
4047 struct mips_multi_member *member;
4048 unsigned int i;
4050 FOR_EACH_VEC_ELT (mips_multi_member, mips_multi_members, i, member)
4051 if (member->is_label_p)
4052 fprintf (asm_out_file, "%s\n", member->format);
4053 else
4054 output_asm_insn (member->format, member->operands);
4057 /* Return one word of double-word value OP, taking into account the fixed
4058 endianness of certain registers. HIGH_P is true to select the high part,
4059 false to select the low part. */
4062 mips_subword (rtx op, bool high_p)
4064 unsigned int byte, offset;
4065 enum machine_mode mode;
4067 mode = GET_MODE (op);
4068 if (mode == VOIDmode)
4069 mode = TARGET_64BIT ? TImode : DImode;
4071 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4072 byte = UNITS_PER_WORD;
4073 else
4074 byte = 0;
4076 if (FP_REG_RTX_P (op))
4078 /* Paired FPRs are always ordered little-endian. */
4079 offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4080 return gen_rtx_REG (word_mode, REGNO (op) + offset);
4083 if (MEM_P (op))
4084 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4086 return simplify_gen_subreg (word_mode, op, mode, byte);
4089 /* Return true if a 64-bit move from SRC to DEST should be split into two. */
4091 bool
4092 mips_split_64bit_move_p (rtx dest, rtx src)
4094 if (TARGET_64BIT)
4095 return false;
4097 /* FPR-to-FPR moves can be done in a single instruction, if they're
4098 allowed at all. */
4099 if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4100 return false;
4102 /* Check for floating-point loads and stores. */
4103 if (ISA_HAS_LDC1_SDC1)
4105 if (FP_REG_RTX_P (dest) && MEM_P (src))
4106 return false;
4107 if (FP_REG_RTX_P (src) && MEM_P (dest))
4108 return false;
4110 return true;
4113 /* Split a doubleword move from SRC to DEST. On 32-bit targets,
4114 this function handles 64-bit moves for which mips_split_64bit_move_p
4115 holds. For 64-bit targets, this function handles 128-bit moves. */
4117 void
4118 mips_split_doubleword_move (rtx dest, rtx src)
4120 rtx low_dest;
4122 if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4124 if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4125 emit_insn (gen_move_doubleword_fprdi (dest, src));
4126 else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4127 emit_insn (gen_move_doubleword_fprdf (dest, src));
4128 else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4129 emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4130 else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4131 emit_insn (gen_move_doubleword_fprv2si (dest, src));
4132 else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4133 emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4134 else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4135 emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4136 else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4137 emit_insn (gen_move_doubleword_fprtf (dest, src));
4138 else
4139 gcc_unreachable ();
4141 else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4143 low_dest = mips_subword (dest, false);
4144 mips_emit_move (low_dest, mips_subword (src, false));
4145 if (TARGET_64BIT)
4146 emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4147 else
4148 emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4150 else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4152 mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4153 if (TARGET_64BIT)
4154 emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4155 else
4156 emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4158 else
4160 /* The operation can be split into two normal moves. Decide in
4161 which order to do them. */
4162 low_dest = mips_subword (dest, false);
4163 if (REG_P (low_dest)
4164 && reg_overlap_mentioned_p (low_dest, src))
4166 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4167 mips_emit_move (low_dest, mips_subword (src, false));
4169 else
4171 mips_emit_move (low_dest, mips_subword (src, false));
4172 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4177 /* Return the appropriate instructions to move SRC into DEST. Assume
4178 that SRC is operand 1 and DEST is operand 0. */
4180 const char *
4181 mips_output_move (rtx dest, rtx src)
4183 enum rtx_code dest_code, src_code;
4184 enum machine_mode mode;
4185 enum mips_symbol_type symbol_type;
4186 bool dbl_p;
4188 dest_code = GET_CODE (dest);
4189 src_code = GET_CODE (src);
4190 mode = GET_MODE (dest);
4191 dbl_p = (GET_MODE_SIZE (mode) == 8);
4193 if (dbl_p && mips_split_64bit_move_p (dest, src))
4194 return "#";
4196 if ((src_code == REG && GP_REG_P (REGNO (src)))
4197 || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4199 if (dest_code == REG)
4201 if (GP_REG_P (REGNO (dest)))
4202 return "move\t%0,%z1";
4204 /* Moves to HI are handled by special .md insns. */
4205 if (REGNO (dest) == LO_REGNUM)
4206 return "mtlo\t%z1";
4208 if (DSP_ACC_REG_P (REGNO (dest)))
4210 static char retval[] = "mt__\t%z1,%q0";
4212 retval[2] = reg_names[REGNO (dest)][4];
4213 retval[3] = reg_names[REGNO (dest)][5];
4214 return retval;
4217 if (FP_REG_P (REGNO (dest)))
4218 return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4220 if (ALL_COP_REG_P (REGNO (dest)))
4222 static char retval[] = "dmtc_\t%z1,%0";
4224 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4225 return dbl_p ? retval : retval + 1;
4228 if (dest_code == MEM)
4229 switch (GET_MODE_SIZE (mode))
4231 case 1: return "sb\t%z1,%0";
4232 case 2: return "sh\t%z1,%0";
4233 case 4: return "sw\t%z1,%0";
4234 case 8: return "sd\t%z1,%0";
4237 if (dest_code == REG && GP_REG_P (REGNO (dest)))
4239 if (src_code == REG)
4241 /* Moves from HI are handled by special .md insns. */
4242 if (REGNO (src) == LO_REGNUM)
4244 /* When generating VR4120 or VR4130 code, we use MACC and
4245 DMACC instead of MFLO. This avoids both the normal
4246 MIPS III HI/LO hazards and the errata related to
4247 -mfix-vr4130. */
4248 if (ISA_HAS_MACCHI)
4249 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4250 return "mflo\t%0";
4253 if (DSP_ACC_REG_P (REGNO (src)))
4255 static char retval[] = "mf__\t%0,%q1";
4257 retval[2] = reg_names[REGNO (src)][4];
4258 retval[3] = reg_names[REGNO (src)][5];
4259 return retval;
4262 if (FP_REG_P (REGNO (src)))
4263 return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4265 if (ALL_COP_REG_P (REGNO (src)))
4267 static char retval[] = "dmfc_\t%0,%1";
4269 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4270 return dbl_p ? retval : retval + 1;
4273 if (ST_REG_P (REGNO (src)) && ISA_HAS_8CC)
4274 return "lui\t%0,0x3f80\n\tmovf\t%0,%.,%1";
4277 if (src_code == MEM)
4278 switch (GET_MODE_SIZE (mode))
4280 case 1: return "lbu\t%0,%1";
4281 case 2: return "lhu\t%0,%1";
4282 case 4: return "lw\t%0,%1";
4283 case 8: return "ld\t%0,%1";
4286 if (src_code == CONST_INT)
4288 /* Don't use the X format for the operand itself, because that
4289 will give out-of-range numbers for 64-bit hosts and 32-bit
4290 targets. */
4291 if (!TARGET_MIPS16)
4292 return "li\t%0,%1\t\t\t# %X1";
4294 if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4295 return "li\t%0,%1";
4297 if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4298 return "#";
4301 if (src_code == HIGH)
4302 return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4304 if (CONST_GP_P (src))
4305 return "move\t%0,%1";
4307 if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4308 && mips_lo_relocs[symbol_type] != 0)
4310 /* A signed 16-bit constant formed by applying a relocation
4311 operator to a symbolic address. */
4312 gcc_assert (!mips_split_p[symbol_type]);
4313 return "li\t%0,%R1";
4316 if (symbolic_operand (src, VOIDmode))
4318 gcc_assert (TARGET_MIPS16
4319 ? TARGET_MIPS16_TEXT_LOADS
4320 : !TARGET_EXPLICIT_RELOCS);
4321 return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4324 if (src_code == REG && FP_REG_P (REGNO (src)))
4326 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4328 if (GET_MODE (dest) == V2SFmode)
4329 return "mov.ps\t%0,%1";
4330 else
4331 return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4334 if (dest_code == MEM)
4335 return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4337 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4339 if (src_code == MEM)
4340 return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4342 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4344 static char retval[] = "l_c_\t%0,%1";
4346 retval[1] = (dbl_p ? 'd' : 'w');
4347 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4348 return retval;
4350 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4352 static char retval[] = "s_c_\t%1,%0";
4354 retval[1] = (dbl_p ? 'd' : 'w');
4355 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4356 return retval;
4358 gcc_unreachable ();
4361 /* Return true if CMP1 is a suitable second operand for integer ordering
4362 test CODE. See also the *sCC patterns in mips.md. */
4364 static bool
4365 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4367 switch (code)
4369 case GT:
4370 case GTU:
4371 return reg_or_0_operand (cmp1, VOIDmode);
4373 case GE:
4374 case GEU:
4375 return !TARGET_MIPS16 && cmp1 == const1_rtx;
4377 case LT:
4378 case LTU:
4379 return arith_operand (cmp1, VOIDmode);
4381 case LE:
4382 return sle_operand (cmp1, VOIDmode);
4384 case LEU:
4385 return sleu_operand (cmp1, VOIDmode);
4387 default:
4388 gcc_unreachable ();
4392 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4393 integer ordering test *CODE, or if an equivalent combination can
4394 be formed by adjusting *CODE and *CMP1. When returning true, update
4395 *CODE and *CMP1 with the chosen code and operand, otherwise leave
4396 them alone. */
4398 static bool
4399 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4400 enum machine_mode mode)
4402 HOST_WIDE_INT plus_one;
4404 if (mips_int_order_operand_ok_p (*code, *cmp1))
4405 return true;
4407 if (CONST_INT_P (*cmp1))
4408 switch (*code)
4410 case LE:
4411 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4412 if (INTVAL (*cmp1) < plus_one)
4414 *code = LT;
4415 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4416 return true;
4418 break;
4420 case LEU:
4421 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4422 if (plus_one != 0)
4424 *code = LTU;
4425 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4426 return true;
4428 break;
4430 default:
4431 break;
4433 return false;
4436 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4437 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
4438 is nonnull, it's OK to set TARGET to the inverse of the result and
4439 flip *INVERT_PTR instead. */
4441 static void
4442 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4443 rtx target, rtx cmp0, rtx cmp1)
4445 enum machine_mode mode;
4447 /* First see if there is a MIPS instruction that can do this operation.
4448 If not, try doing the same for the inverse operation. If that also
4449 fails, force CMP1 into a register and try again. */
4450 mode = GET_MODE (cmp0);
4451 if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4452 mips_emit_binary (code, target, cmp0, cmp1);
4453 else
4455 enum rtx_code inv_code = reverse_condition (code);
4456 if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4458 cmp1 = force_reg (mode, cmp1);
4459 mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4461 else if (invert_ptr == 0)
4463 rtx inv_target;
4465 inv_target = mips_force_binary (GET_MODE (target),
4466 inv_code, cmp0, cmp1);
4467 mips_emit_binary (XOR, target, inv_target, const1_rtx);
4469 else
4471 *invert_ptr = !*invert_ptr;
4472 mips_emit_binary (inv_code, target, cmp0, cmp1);
4477 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4478 The register will have the same mode as CMP0. */
4480 static rtx
4481 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4483 if (cmp1 == const0_rtx)
4484 return cmp0;
4486 if (uns_arith_operand (cmp1, VOIDmode))
4487 return expand_binop (GET_MODE (cmp0), xor_optab,
4488 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4490 return expand_binop (GET_MODE (cmp0), sub_optab,
4491 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4494 /* Convert *CODE into a code that can be used in a floating-point
4495 scc instruction (C.cond.fmt). Return true if the values of
4496 the condition code registers will be inverted, with 0 indicating
4497 that the condition holds. */
4499 static bool
4500 mips_reversed_fp_cond (enum rtx_code *code)
4502 switch (*code)
4504 case NE:
4505 case LTGT:
4506 case ORDERED:
4507 *code = reverse_condition_maybe_unordered (*code);
4508 return true;
4510 default:
4511 return false;
4515 /* Convert a comparison into something that can be used in a branch or
4516 conditional move. On entry, *OP0 and *OP1 are the values being
4517 compared and *CODE is the code used to compare them.
4519 Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
4520 If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
4521 otherwise any standard branch condition can be used. The standard branch
4522 conditions are:
4524 - EQ or NE between two registers.
4525 - any comparison between a register and zero. */
4527 static void
4528 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
4530 rtx cmp_op0 = *op0;
4531 rtx cmp_op1 = *op1;
4533 if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
4535 if (!need_eq_ne_p && *op1 == const0_rtx)
4537 else if (*code == EQ || *code == NE)
4539 if (need_eq_ne_p)
4541 *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
4542 *op1 = const0_rtx;
4544 else
4545 *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
4547 else
4549 /* The comparison needs a separate scc instruction. Store the
4550 result of the scc in *OP0 and compare it against zero. */
4551 bool invert = false;
4552 *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
4553 mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
4554 *code = (invert ? EQ : NE);
4555 *op1 = const0_rtx;
4558 else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
4560 *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
4561 mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
4562 *code = NE;
4563 *op1 = const0_rtx;
4565 else
4567 enum rtx_code cmp_code;
4569 /* Floating-point tests use a separate C.cond.fmt comparison to
4570 set a condition code register. The branch or conditional move
4571 will then compare that register against zero.
4573 Set CMP_CODE to the code of the comparison instruction and
4574 *CODE to the code that the branch or move should use. */
4575 cmp_code = *code;
4576 *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
4577 *op0 = (ISA_HAS_8CC
4578 ? gen_reg_rtx (CCmode)
4579 : gen_rtx_REG (CCmode, FPSW_REGNUM));
4580 *op1 = const0_rtx;
4581 mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
4585 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
4586 and OPERAND[3]. Store the result in OPERANDS[0].
4588 On 64-bit targets, the mode of the comparison and target will always be
4589 SImode, thus possibly narrower than that of the comparison's operands. */
4591 void
4592 mips_expand_scc (rtx operands[])
4594 rtx target = operands[0];
4595 enum rtx_code code = GET_CODE (operands[1]);
4596 rtx op0 = operands[2];
4597 rtx op1 = operands[3];
4599 gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
4601 if (code == EQ || code == NE)
4603 if (ISA_HAS_SEQ_SNE
4604 && reg_imm10_operand (op1, GET_MODE (op1)))
4605 mips_emit_binary (code, target, op0, op1);
4606 else
4608 rtx zie = mips_zero_if_equal (op0, op1);
4609 mips_emit_binary (code, target, zie, const0_rtx);
4612 else
4613 mips_emit_int_order_test (code, 0, target, op0, op1);
4616 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
4617 CODE and jump to OPERANDS[3] if the condition holds. */
4619 void
4620 mips_expand_conditional_branch (rtx *operands)
4622 enum rtx_code code = GET_CODE (operands[0]);
4623 rtx op0 = operands[1];
4624 rtx op1 = operands[2];
4625 rtx condition;
4627 mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
4628 condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
4629 emit_jump_insn (gen_condjump (condition, operands[3]));
4632 /* Implement:
4634 (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
4635 (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS)) */
4637 void
4638 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
4639 enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
4641 rtx cmp_result;
4642 bool reversed_p;
4644 reversed_p = mips_reversed_fp_cond (&cond);
4645 cmp_result = gen_reg_rtx (CCV2mode);
4646 emit_insn (gen_scc_ps (cmp_result,
4647 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
4648 if (reversed_p)
4649 emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
4650 cmp_result));
4651 else
4652 emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
4653 cmp_result));
4656 /* Perform the comparison in OPERANDS[1]. Move OPERANDS[2] into OPERANDS[0]
4657 if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0]. */
4659 void
4660 mips_expand_conditional_move (rtx *operands)
4662 rtx cond;
4663 enum rtx_code code = GET_CODE (operands[1]);
4664 rtx op0 = XEXP (operands[1], 0);
4665 rtx op1 = XEXP (operands[1], 1);
4667 mips_emit_compare (&code, &op0, &op1, true);
4668 cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
4669 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
4670 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
4671 operands[2], operands[3])));
4674 /* Perform the comparison in COMPARISON, then trap if the condition holds. */
4676 void
4677 mips_expand_conditional_trap (rtx comparison)
4679 rtx op0, op1;
4680 enum machine_mode mode;
4681 enum rtx_code code;
4683 /* MIPS conditional trap instructions don't have GT or LE flavors,
4684 so we must swap the operands and convert to LT and GE respectively. */
4685 code = GET_CODE (comparison);
4686 switch (code)
4688 case GT:
4689 case LE:
4690 case GTU:
4691 case LEU:
4692 code = swap_condition (code);
4693 op0 = XEXP (comparison, 1);
4694 op1 = XEXP (comparison, 0);
4695 break;
4697 default:
4698 op0 = XEXP (comparison, 0);
4699 op1 = XEXP (comparison, 1);
4700 break;
4703 mode = GET_MODE (XEXP (comparison, 0));
4704 op0 = force_reg (mode, op0);
4705 if (!arith_operand (op1, mode))
4706 op1 = force_reg (mode, op1);
4708 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
4709 gen_rtx_fmt_ee (code, mode, op0, op1),
4710 const0_rtx));
4713 /* Initialize *CUM for a call to a function of type FNTYPE. */
4715 void
4716 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
4718 memset (cum, 0, sizeof (*cum));
4719 cum->prototype = (fntype && prototype_p (fntype));
4720 cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
4723 /* Fill INFO with information about a single argument. CUM is the
4724 cumulative state for earlier arguments. MODE is the mode of this
4725 argument and TYPE is its type (if known). NAMED is true if this
4726 is a named (fixed) argument rather than a variable one. */
4728 static void
4729 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
4730 enum machine_mode mode, const_tree type, bool named)
4732 bool doubleword_aligned_p;
4733 unsigned int num_bytes, num_words, max_regs;
4735 /* Work out the size of the argument. */
4736 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
4737 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4739 /* Decide whether it should go in a floating-point register, assuming
4740 one is free. Later code checks for availability.
4742 The checks against UNITS_PER_FPVALUE handle the soft-float and
4743 single-float cases. */
4744 switch (mips_abi)
4746 case ABI_EABI:
4747 /* The EABI conventions have traditionally been defined in terms
4748 of TYPE_MODE, regardless of the actual type. */
4749 info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
4750 || mode == V2SFmode)
4751 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
4752 break;
4754 case ABI_32:
4755 case ABI_O64:
4756 /* Only leading floating-point scalars are passed in
4757 floating-point registers. We also handle vector floats the same
4758 say, which is OK because they are not covered by the standard ABI. */
4759 info->fpr_p = (!cum->gp_reg_found
4760 && cum->arg_number < 2
4761 && (type == 0
4762 || SCALAR_FLOAT_TYPE_P (type)
4763 || VECTOR_FLOAT_TYPE_P (type))
4764 && (GET_MODE_CLASS (mode) == MODE_FLOAT
4765 || mode == V2SFmode)
4766 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
4767 break;
4769 case ABI_N32:
4770 case ABI_64:
4771 /* Scalar, complex and vector floating-point types are passed in
4772 floating-point registers, as long as this is a named rather
4773 than a variable argument. */
4774 info->fpr_p = (named
4775 && (type == 0 || FLOAT_TYPE_P (type))
4776 && (GET_MODE_CLASS (mode) == MODE_FLOAT
4777 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4778 || mode == V2SFmode)
4779 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
4781 /* ??? According to the ABI documentation, the real and imaginary
4782 parts of complex floats should be passed in individual registers.
4783 The real and imaginary parts of stack arguments are supposed
4784 to be contiguous and there should be an extra word of padding
4785 at the end.
4787 This has two problems. First, it makes it impossible to use a
4788 single "void *" va_list type, since register and stack arguments
4789 are passed differently. (At the time of writing, MIPSpro cannot
4790 handle complex float varargs correctly.) Second, it's unclear
4791 what should happen when there is only one register free.
4793 For now, we assume that named complex floats should go into FPRs
4794 if there are two FPRs free, otherwise they should be passed in the
4795 same way as a struct containing two floats. */
4796 if (info->fpr_p
4797 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4798 && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
4800 if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
4801 info->fpr_p = false;
4802 else
4803 num_words = 2;
4805 break;
4807 default:
4808 gcc_unreachable ();
4811 /* See whether the argument has doubleword alignment. */
4812 doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
4813 > BITS_PER_WORD);
4815 /* Set REG_OFFSET to the register count we're interested in.
4816 The EABI allocates the floating-point registers separately,
4817 but the other ABIs allocate them like integer registers. */
4818 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
4819 ? cum->num_fprs
4820 : cum->num_gprs);
4822 /* Advance to an even register if the argument is doubleword-aligned. */
4823 if (doubleword_aligned_p)
4824 info->reg_offset += info->reg_offset & 1;
4826 /* Work out the offset of a stack argument. */
4827 info->stack_offset = cum->stack_words;
4828 if (doubleword_aligned_p)
4829 info->stack_offset += info->stack_offset & 1;
4831 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
4833 /* Partition the argument between registers and stack. */
4834 info->reg_words = MIN (num_words, max_regs);
4835 info->stack_words = num_words - info->reg_words;
4838 /* INFO describes a register argument that has the normal format for the
4839 argument's mode. Return the register it uses, assuming that FPRs are
4840 available if HARD_FLOAT_P. */
4842 static unsigned int
4843 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
4845 if (!info->fpr_p || !hard_float_p)
4846 return GP_ARG_FIRST + info->reg_offset;
4847 else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
4848 /* In o32, the second argument is always passed in $f14
4849 for TARGET_DOUBLE_FLOAT, regardless of whether the
4850 first argument was a word or doubleword. */
4851 return FP_ARG_FIRST + 2;
4852 else
4853 return FP_ARG_FIRST + info->reg_offset;
4856 /* Implement TARGET_STRICT_ARGUMENT_NAMING. */
4858 static bool
4859 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
4861 return !TARGET_OLDABI;
4864 /* Implement TARGET_FUNCTION_ARG. */
4866 static rtx
4867 mips_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
4868 const_tree type, bool named)
4870 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
4871 struct mips_arg_info info;
4873 /* We will be called with a mode of VOIDmode after the last argument
4874 has been seen. Whatever we return will be passed to the call expander.
4875 If we need a MIPS16 fp_code, return a REG with the code stored as
4876 the mode. */
4877 if (mode == VOIDmode)
4879 if (TARGET_MIPS16 && cum->fp_code != 0)
4880 return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
4881 else
4882 return NULL;
4885 mips_get_arg_info (&info, cum, mode, type, named);
4887 /* Return straight away if the whole argument is passed on the stack. */
4888 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
4889 return NULL;
4891 /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
4892 contains a double in its entirety, then that 64-bit chunk is passed
4893 in a floating-point register. */
4894 if (TARGET_NEWABI
4895 && TARGET_HARD_FLOAT
4896 && named
4897 && type != 0
4898 && TREE_CODE (type) == RECORD_TYPE
4899 && TYPE_SIZE_UNIT (type)
4900 && host_integerp (TYPE_SIZE_UNIT (type), 1))
4902 tree field;
4904 /* First check to see if there is any such field. */
4905 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4906 if (TREE_CODE (field) == FIELD_DECL
4907 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
4908 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
4909 && host_integerp (bit_position (field), 0)
4910 && int_bit_position (field) % BITS_PER_WORD == 0)
4911 break;
4913 if (field != 0)
4915 /* Now handle the special case by returning a PARALLEL
4916 indicating where each 64-bit chunk goes. INFO.REG_WORDS
4917 chunks are passed in registers. */
4918 unsigned int i;
4919 HOST_WIDE_INT bitpos;
4920 rtx ret;
4922 /* assign_parms checks the mode of ENTRY_PARM, so we must
4923 use the actual mode here. */
4924 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
4926 bitpos = 0;
4927 field = TYPE_FIELDS (type);
4928 for (i = 0; i < info.reg_words; i++)
4930 rtx reg;
4932 for (; field; field = DECL_CHAIN (field))
4933 if (TREE_CODE (field) == FIELD_DECL
4934 && int_bit_position (field) >= bitpos)
4935 break;
4937 if (field
4938 && int_bit_position (field) == bitpos
4939 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
4940 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
4941 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
4942 else
4943 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
4945 XVECEXP (ret, 0, i)
4946 = gen_rtx_EXPR_LIST (VOIDmode, reg,
4947 GEN_INT (bitpos / BITS_PER_UNIT));
4949 bitpos += BITS_PER_WORD;
4951 return ret;
4955 /* Handle the n32/n64 conventions for passing complex floating-point
4956 arguments in FPR pairs. The real part goes in the lower register
4957 and the imaginary part goes in the upper register. */
4958 if (TARGET_NEWABI
4959 && info.fpr_p
4960 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4962 rtx real, imag;
4963 enum machine_mode inner;
4964 unsigned int regno;
4966 inner = GET_MODE_INNER (mode);
4967 regno = FP_ARG_FIRST + info.reg_offset;
4968 if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
4970 /* Real part in registers, imaginary part on stack. */
4971 gcc_assert (info.stack_words == info.reg_words);
4972 return gen_rtx_REG (inner, regno);
4974 else
4976 gcc_assert (info.stack_words == 0);
4977 real = gen_rtx_EXPR_LIST (VOIDmode,
4978 gen_rtx_REG (inner, regno),
4979 const0_rtx);
4980 imag = gen_rtx_EXPR_LIST (VOIDmode,
4981 gen_rtx_REG (inner,
4982 regno + info.reg_words / 2),
4983 GEN_INT (GET_MODE_SIZE (inner)));
4984 return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
4988 return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
4991 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
4993 static void
4994 mips_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
4995 const_tree type, bool named)
4997 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
4998 struct mips_arg_info info;
5000 mips_get_arg_info (&info, cum, mode, type, named);
5002 if (!info.fpr_p)
5003 cum->gp_reg_found = true;
5005 /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
5006 an explanation of what this code does. It assumes that we're using
5007 either the o32 or the o64 ABI, both of which pass at most 2 arguments
5008 in FPRs. */
5009 if (cum->arg_number < 2 && info.fpr_p)
5010 cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
5012 /* Advance the register count. This has the effect of setting
5013 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
5014 argument required us to skip the final GPR and pass the whole
5015 argument on the stack. */
5016 if (mips_abi != ABI_EABI || !info.fpr_p)
5017 cum->num_gprs = info.reg_offset + info.reg_words;
5018 else if (info.reg_words > 0)
5019 cum->num_fprs += MAX_FPRS_PER_FMT;
5021 /* Advance the stack word count. */
5022 if (info.stack_words > 0)
5023 cum->stack_words = info.stack_offset + info.stack_words;
5025 cum->arg_number++;
5028 /* Implement TARGET_ARG_PARTIAL_BYTES. */
5030 static int
5031 mips_arg_partial_bytes (cumulative_args_t cum,
5032 enum machine_mode mode, tree type, bool named)
5034 struct mips_arg_info info;
5036 mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
5037 return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
5040 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
5041 least PARM_BOUNDARY bits of alignment, but will be given anything up
5042 to STACK_BOUNDARY bits if the type requires it. */
5044 static unsigned int
5045 mips_function_arg_boundary (enum machine_mode mode, const_tree type)
5047 unsigned int alignment;
5049 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5050 if (alignment < PARM_BOUNDARY)
5051 alignment = PARM_BOUNDARY;
5052 if (alignment > STACK_BOUNDARY)
5053 alignment = STACK_BOUNDARY;
5054 return alignment;
5057 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5058 upward rather than downward. In other words, return true if the
5059 first byte of the stack slot has useful data, false if the last
5060 byte does. */
5062 bool
5063 mips_pad_arg_upward (enum machine_mode mode, const_tree type)
5065 /* On little-endian targets, the first byte of every stack argument
5066 is passed in the first byte of the stack slot. */
5067 if (!BYTES_BIG_ENDIAN)
5068 return true;
5070 /* Otherwise, integral types are padded downward: the last byte of a
5071 stack argument is passed in the last byte of the stack slot. */
5072 if (type != 0
5073 ? (INTEGRAL_TYPE_P (type)
5074 || POINTER_TYPE_P (type)
5075 || FIXED_POINT_TYPE_P (type))
5076 : (SCALAR_INT_MODE_P (mode)
5077 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5078 return false;
5080 /* Big-endian o64 pads floating-point arguments downward. */
5081 if (mips_abi == ABI_O64)
5082 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5083 return false;
5085 /* Other types are padded upward for o32, o64, n32 and n64. */
5086 if (mips_abi != ABI_EABI)
5087 return true;
5089 /* Arguments smaller than a stack slot are padded downward. */
5090 if (mode != BLKmode)
5091 return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5092 else
5093 return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5096 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
5097 if the least significant byte of the register has useful data. Return
5098 the opposite if the most significant byte does. */
5100 bool
5101 mips_pad_reg_upward (enum machine_mode mode, tree type)
5103 /* No shifting is required for floating-point arguments. */
5104 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5105 return !BYTES_BIG_ENDIAN;
5107 /* Otherwise, apply the same padding to register arguments as we do
5108 to stack arguments. */
5109 return mips_pad_arg_upward (mode, type);
5112 /* Return nonzero when an argument must be passed by reference. */
5114 static bool
5115 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5116 enum machine_mode mode, const_tree type,
5117 bool named ATTRIBUTE_UNUSED)
5119 if (mips_abi == ABI_EABI)
5121 int size;
5123 /* ??? How should SCmode be handled? */
5124 if (mode == DImode || mode == DFmode
5125 || mode == DQmode || mode == UDQmode
5126 || mode == DAmode || mode == UDAmode)
5127 return 0;
5129 size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5130 return size == -1 || size > UNITS_PER_WORD;
5132 else
5134 /* If we have a variable-sized parameter, we have no choice. */
5135 return targetm.calls.must_pass_in_stack (mode, type);
5139 /* Implement TARGET_CALLEE_COPIES. */
5141 static bool
5142 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
5143 enum machine_mode mode ATTRIBUTE_UNUSED,
5144 const_tree type ATTRIBUTE_UNUSED, bool named)
5146 return mips_abi == ABI_EABI && named;
5149 /* See whether VALTYPE is a record whose fields should be returned in
5150 floating-point registers. If so, return the number of fields and
5151 list them in FIELDS (which should have two elements). Return 0
5152 otherwise.
5154 For n32 & n64, a structure with one or two fields is returned in
5155 floating-point registers as long as every field has a floating-point
5156 type. */
5158 static int
5159 mips_fpr_return_fields (const_tree valtype, tree *fields)
5161 tree field;
5162 int i;
5164 if (!TARGET_NEWABI)
5165 return 0;
5167 if (TREE_CODE (valtype) != RECORD_TYPE)
5168 return 0;
5170 i = 0;
5171 for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5173 if (TREE_CODE (field) != FIELD_DECL)
5174 continue;
5176 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5177 return 0;
5179 if (i == 2)
5180 return 0;
5182 fields[i++] = field;
5184 return i;
5187 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
5188 a value in the most significant part of $2/$3 if:
5190 - the target is big-endian;
5192 - the value has a structure or union type (we generalize this to
5193 cover aggregates from other languages too); and
5195 - the structure is not returned in floating-point registers. */
5197 static bool
5198 mips_return_in_msb (const_tree valtype)
5200 tree fields[2];
5202 return (TARGET_NEWABI
5203 && TARGET_BIG_ENDIAN
5204 && AGGREGATE_TYPE_P (valtype)
5205 && mips_fpr_return_fields (valtype, fields) == 0);
5208 /* Return true if the function return value MODE will get returned in a
5209 floating-point register. */
5211 static bool
5212 mips_return_mode_in_fpr_p (enum machine_mode mode)
5214 return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5215 || mode == V2SFmode
5216 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5217 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5220 /* Return the representation of an FPR return register when the
5221 value being returned in FP_RETURN has mode VALUE_MODE and the
5222 return type itself has mode TYPE_MODE. On NewABI targets,
5223 the two modes may be different for structures like:
5225 struct __attribute__((packed)) foo { float f; }
5227 where we return the SFmode value of "f" in FP_RETURN, but where
5228 the structure itself has mode BLKmode. */
5230 static rtx
5231 mips_return_fpr_single (enum machine_mode type_mode,
5232 enum machine_mode value_mode)
5234 rtx x;
5236 x = gen_rtx_REG (value_mode, FP_RETURN);
5237 if (type_mode != value_mode)
5239 x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5240 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5242 return x;
5245 /* Return a composite value in a pair of floating-point registers.
5246 MODE1 and OFFSET1 are the mode and byte offset for the first value,
5247 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
5248 complete value.
5250 For n32 & n64, $f0 always holds the first value and $f2 the second.
5251 Otherwise the values are packed together as closely as possible. */
5253 static rtx
5254 mips_return_fpr_pair (enum machine_mode mode,
5255 enum machine_mode mode1, HOST_WIDE_INT offset1,
5256 enum machine_mode mode2, HOST_WIDE_INT offset2)
5258 int inc;
5260 inc = (TARGET_NEWABI ? 2 : MAX_FPRS_PER_FMT);
5261 return gen_rtx_PARALLEL
5262 (mode,
5263 gen_rtvec (2,
5264 gen_rtx_EXPR_LIST (VOIDmode,
5265 gen_rtx_REG (mode1, FP_RETURN),
5266 GEN_INT (offset1)),
5267 gen_rtx_EXPR_LIST (VOIDmode,
5268 gen_rtx_REG (mode2, FP_RETURN + inc),
5269 GEN_INT (offset2))));
5273 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
5274 For normal calls, VALTYPE is the return type and MODE is VOIDmode.
5275 For libcalls, VALTYPE is null and MODE is the mode of the return value. */
5277 static rtx
5278 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
5279 enum machine_mode mode)
5281 if (valtype)
5283 tree fields[2];
5284 int unsigned_p;
5285 const_tree func;
5287 if (fn_decl_or_type && DECL_P (fn_decl_or_type))
5288 func = fn_decl_or_type;
5289 else
5290 func = NULL;
5292 mode = TYPE_MODE (valtype);
5293 unsigned_p = TYPE_UNSIGNED (valtype);
5295 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5296 return values, promote the mode here too. */
5297 mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5299 /* Handle structures whose fields are returned in $f0/$f2. */
5300 switch (mips_fpr_return_fields (valtype, fields))
5302 case 1:
5303 return mips_return_fpr_single (mode,
5304 TYPE_MODE (TREE_TYPE (fields[0])));
5306 case 2:
5307 return mips_return_fpr_pair (mode,
5308 TYPE_MODE (TREE_TYPE (fields[0])),
5309 int_byte_position (fields[0]),
5310 TYPE_MODE (TREE_TYPE (fields[1])),
5311 int_byte_position (fields[1]));
5314 /* If a value is passed in the most significant part of a register, see
5315 whether we have to round the mode up to a whole number of words. */
5316 if (mips_return_in_msb (valtype))
5318 HOST_WIDE_INT size = int_size_in_bytes (valtype);
5319 if (size % UNITS_PER_WORD != 0)
5321 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5322 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5326 /* For EABI, the class of return register depends entirely on MODE.
5327 For example, "struct { some_type x; }" and "union { some_type x; }"
5328 are returned in the same way as a bare "some_type" would be.
5329 Other ABIs only use FPRs for scalar, complex or vector types. */
5330 if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5331 return gen_rtx_REG (mode, GP_RETURN);
5334 if (!TARGET_MIPS16)
5336 /* Handle long doubles for n32 & n64. */
5337 if (mode == TFmode)
5338 return mips_return_fpr_pair (mode,
5339 DImode, 0,
5340 DImode, GET_MODE_SIZE (mode) / 2);
5342 if (mips_return_mode_in_fpr_p (mode))
5344 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5345 return mips_return_fpr_pair (mode,
5346 GET_MODE_INNER (mode), 0,
5347 GET_MODE_INNER (mode),
5348 GET_MODE_SIZE (mode) / 2);
5349 else
5350 return gen_rtx_REG (mode, FP_RETURN);
5354 return gen_rtx_REG (mode, GP_RETURN);
5357 /* Implement TARGET_FUNCTION_VALUE. */
5359 static rtx
5360 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
5361 bool outgoing ATTRIBUTE_UNUSED)
5363 return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5366 /* Implement TARGET_LIBCALL_VALUE. */
5368 static rtx
5369 mips_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5371 return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
5374 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5376 On the MIPS, R2 R3 and F0 F2 are the only register thus used.
5377 Currently, R2 and F0 are only implemented here (C has no complex type). */
5379 static bool
5380 mips_function_value_regno_p (const unsigned int regno)
5382 if (regno == GP_RETURN
5383 || regno == FP_RETURN
5384 || (LONG_DOUBLE_TYPE_SIZE == 128
5385 && FP_RETURN != GP_RETURN
5386 && regno == FP_RETURN + 2))
5387 return true;
5389 return false;
5392 /* Implement TARGET_RETURN_IN_MEMORY. Under the o32 and o64 ABIs,
5393 all BLKmode objects are returned in memory. Under the n32, n64
5394 and embedded ABIs, small structures are returned in a register.
5395 Objects with varying size must still be returned in memory, of
5396 course. */
5398 static bool
5399 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5401 return (TARGET_OLDABI
5402 ? TYPE_MODE (type) == BLKmode
5403 : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5406 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
5408 static void
5409 mips_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
5410 tree type, int *pretend_size ATTRIBUTE_UNUSED,
5411 int no_rtl)
5413 CUMULATIVE_ARGS local_cum;
5414 int gp_saved, fp_saved;
5416 /* The caller has advanced CUM up to, but not beyond, the last named
5417 argument. Advance a local copy of CUM past the last "real" named
5418 argument, to find out how many registers are left over. */
5419 local_cum = *get_cumulative_args (cum);
5420 mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
5421 true);
5423 /* Found out how many registers we need to save. */
5424 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
5425 fp_saved = (EABI_FLOAT_VARARGS_P
5426 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
5427 : 0);
5429 if (!no_rtl)
5431 if (gp_saved > 0)
5433 rtx ptr, mem;
5435 ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
5436 REG_PARM_STACK_SPACE (cfun->decl)
5437 - gp_saved * UNITS_PER_WORD);
5438 mem = gen_frame_mem (BLKmode, ptr);
5439 set_mem_alias_set (mem, get_varargs_alias_set ());
5441 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
5442 mem, gp_saved);
5444 if (fp_saved > 0)
5446 /* We can't use move_block_from_reg, because it will use
5447 the wrong mode. */
5448 enum machine_mode mode;
5449 int off, i;
5451 /* Set OFF to the offset from virtual_incoming_args_rtx of
5452 the first float register. The FP save area lies below
5453 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
5454 off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
5455 off -= fp_saved * UNITS_PER_FPREG;
5457 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
5459 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
5460 i += MAX_FPRS_PER_FMT)
5462 rtx ptr, mem;
5464 ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
5465 mem = gen_frame_mem (mode, ptr);
5466 set_mem_alias_set (mem, get_varargs_alias_set ());
5467 mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
5468 off += UNITS_PER_HWFPVALUE;
5472 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
5473 cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
5474 + fp_saved * UNITS_PER_FPREG);
5477 /* Implement TARGET_BUILTIN_VA_LIST. */
5479 static tree
5480 mips_build_builtin_va_list (void)
5482 if (EABI_FLOAT_VARARGS_P)
5484 /* We keep 3 pointers, and two offsets.
5486 Two pointers are to the overflow area, which starts at the CFA.
5487 One of these is constant, for addressing into the GPR save area
5488 below it. The other is advanced up the stack through the
5489 overflow region.
5491 The third pointer is to the bottom of the GPR save area.
5492 Since the FPR save area is just below it, we can address
5493 FPR slots off this pointer.
5495 We also keep two one-byte offsets, which are to be subtracted
5496 from the constant pointers to yield addresses in the GPR and
5497 FPR save areas. These are downcounted as float or non-float
5498 arguments are used, and when they get to zero, the argument
5499 must be obtained from the overflow region. */
5500 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
5501 tree array, index;
5503 record = lang_hooks.types.make_type (RECORD_TYPE);
5505 f_ovfl = build_decl (BUILTINS_LOCATION,
5506 FIELD_DECL, get_identifier ("__overflow_argptr"),
5507 ptr_type_node);
5508 f_gtop = build_decl (BUILTINS_LOCATION,
5509 FIELD_DECL, get_identifier ("__gpr_top"),
5510 ptr_type_node);
5511 f_ftop = build_decl (BUILTINS_LOCATION,
5512 FIELD_DECL, get_identifier ("__fpr_top"),
5513 ptr_type_node);
5514 f_goff = build_decl (BUILTINS_LOCATION,
5515 FIELD_DECL, get_identifier ("__gpr_offset"),
5516 unsigned_char_type_node);
5517 f_foff = build_decl (BUILTINS_LOCATION,
5518 FIELD_DECL, get_identifier ("__fpr_offset"),
5519 unsigned_char_type_node);
5520 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
5521 warn on every user file. */
5522 index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
5523 array = build_array_type (unsigned_char_type_node,
5524 build_index_type (index));
5525 f_res = build_decl (BUILTINS_LOCATION,
5526 FIELD_DECL, get_identifier ("__reserved"), array);
5528 DECL_FIELD_CONTEXT (f_ovfl) = record;
5529 DECL_FIELD_CONTEXT (f_gtop) = record;
5530 DECL_FIELD_CONTEXT (f_ftop) = record;
5531 DECL_FIELD_CONTEXT (f_goff) = record;
5532 DECL_FIELD_CONTEXT (f_foff) = record;
5533 DECL_FIELD_CONTEXT (f_res) = record;
5535 TYPE_FIELDS (record) = f_ovfl;
5536 DECL_CHAIN (f_ovfl) = f_gtop;
5537 DECL_CHAIN (f_gtop) = f_ftop;
5538 DECL_CHAIN (f_ftop) = f_goff;
5539 DECL_CHAIN (f_goff) = f_foff;
5540 DECL_CHAIN (f_foff) = f_res;
5542 layout_type (record);
5543 return record;
5545 else
5546 /* Otherwise, we use 'void *'. */
5547 return ptr_type_node;
5550 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
5552 static void
5553 mips_va_start (tree valist, rtx nextarg)
5555 if (EABI_FLOAT_VARARGS_P)
5557 const CUMULATIVE_ARGS *cum;
5558 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5559 tree ovfl, gtop, ftop, goff, foff;
5560 tree t;
5561 int gpr_save_area_size;
5562 int fpr_save_area_size;
5563 int fpr_offset;
5565 cum = &crtl->args.info;
5566 gpr_save_area_size
5567 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5568 fpr_save_area_size
5569 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5571 f_ovfl = TYPE_FIELDS (va_list_type_node);
5572 f_gtop = DECL_CHAIN (f_ovfl);
5573 f_ftop = DECL_CHAIN (f_gtop);
5574 f_goff = DECL_CHAIN (f_ftop);
5575 f_foff = DECL_CHAIN (f_goff);
5577 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5578 NULL_TREE);
5579 gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
5580 NULL_TREE);
5581 ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
5582 NULL_TREE);
5583 goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
5584 NULL_TREE);
5585 foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
5586 NULL_TREE);
5588 /* Emit code to initialize OVFL, which points to the next varargs
5589 stack argument. CUM->STACK_WORDS gives the number of stack
5590 words used by named arguments. */
5591 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5592 if (cum->stack_words > 0)
5593 t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
5594 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5595 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5597 /* Emit code to initialize GTOP, the top of the GPR save area. */
5598 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5599 t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5600 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5602 /* Emit code to initialize FTOP, the top of the FPR save area.
5603 This address is gpr_save_area_bytes below GTOP, rounded
5604 down to the next fp-aligned boundary. */
5605 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5606 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5607 fpr_offset &= -UNITS_PER_FPVALUE;
5608 if (fpr_offset)
5609 t = fold_build_pointer_plus_hwi (t, -fpr_offset);
5610 t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5611 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5613 /* Emit code to initialize GOFF, the offset from GTOP of the
5614 next GPR argument. */
5615 t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
5616 build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
5617 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5619 /* Likewise emit code to initialize FOFF, the offset from FTOP
5620 of the next FPR argument. */
5621 t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
5622 build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
5623 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5625 else
5627 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
5628 std_expand_builtin_va_start (valist, nextarg);
5632 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
5633 types as well. */
5635 static tree
5636 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5637 gimple_seq *post_p)
5639 tree addr, t, type_size, rounded_size, valist_tmp;
5640 unsigned HOST_WIDE_INT align, boundary;
5641 bool indirect;
5643 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
5644 if (indirect)
5645 type = build_pointer_type (type);
5647 align = PARM_BOUNDARY / BITS_PER_UNIT;
5648 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
5650 /* When we align parameter on stack for caller, if the parameter
5651 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
5652 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
5653 here with caller. */
5654 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
5655 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
5657 boundary /= BITS_PER_UNIT;
5659 /* Hoist the valist value into a temporary for the moment. */
5660 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
5662 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
5663 requires greater alignment, we must perform dynamic alignment. */
5664 if (boundary > align)
5666 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
5667 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
5668 gimplify_and_add (t, pre_p);
5670 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
5671 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
5672 valist_tmp,
5673 build_int_cst (TREE_TYPE (valist), -boundary)));
5674 gimplify_and_add (t, pre_p);
5676 else
5677 boundary = align;
5679 /* If the actual alignment is less than the alignment of the type,
5680 adjust the type accordingly so that we don't assume strict alignment
5681 when dereferencing the pointer. */
5682 boundary *= BITS_PER_UNIT;
5683 if (boundary < TYPE_ALIGN (type))
5685 type = build_variant_type_copy (type);
5686 TYPE_ALIGN (type) = boundary;
5689 /* Compute the rounded size of the type. */
5690 type_size = size_in_bytes (type);
5691 rounded_size = round_up (type_size, align);
5693 /* Reduce rounded_size so it's sharable with the postqueue. */
5694 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
5696 /* Get AP. */
5697 addr = valist_tmp;
5698 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
5700 /* Small args are padded downward. */
5701 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
5702 rounded_size, size_int (align));
5703 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
5704 size_binop (MINUS_EXPR, rounded_size, type_size));
5705 addr = fold_build_pointer_plus (addr, t);
5708 /* Compute new value for AP. */
5709 t = fold_build_pointer_plus (valist_tmp, rounded_size);
5710 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
5711 gimplify_and_add (t, pre_p);
5713 addr = fold_convert (build_pointer_type (type), addr);
5715 if (indirect)
5716 addr = build_va_arg_indirect_ref (addr);
5718 return build_va_arg_indirect_ref (addr);
5721 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
5723 static tree
5724 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5725 gimple_seq *post_p)
5727 tree addr;
5728 bool indirect_p;
5730 indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
5731 if (indirect_p)
5732 type = build_pointer_type (type);
5734 if (!EABI_FLOAT_VARARGS_P)
5735 addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5736 else
5738 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5739 tree ovfl, top, off, align;
5740 HOST_WIDE_INT size, rsize, osize;
5741 tree t, u;
5743 f_ovfl = TYPE_FIELDS (va_list_type_node);
5744 f_gtop = DECL_CHAIN (f_ovfl);
5745 f_ftop = DECL_CHAIN (f_gtop);
5746 f_goff = DECL_CHAIN (f_ftop);
5747 f_foff = DECL_CHAIN (f_goff);
5749 /* Let:
5751 TOP be the top of the GPR or FPR save area;
5752 OFF be the offset from TOP of the next register;
5753 ADDR_RTX be the address of the argument;
5754 SIZE be the number of bytes in the argument type;
5755 RSIZE be the number of bytes used to store the argument
5756 when it's in the register save area; and
5757 OSIZE be the number of bytes used to store it when it's
5758 in the stack overflow area.
5760 The code we want is:
5762 1: off &= -rsize; // round down
5763 2: if (off != 0)
5764 3: {
5765 4: addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
5766 5: off -= rsize;
5767 6: }
5768 7: else
5769 8: {
5770 9: ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
5771 10: addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
5772 11: ovfl += osize;
5773 14: }
5775 [1] and [9] can sometimes be optimized away. */
5777 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5778 NULL_TREE);
5779 size = int_size_in_bytes (type);
5781 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
5782 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
5784 top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
5785 unshare_expr (valist), f_ftop, NULL_TREE);
5786 off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
5787 unshare_expr (valist), f_foff, NULL_TREE);
5789 /* When va_start saves FPR arguments to the stack, each slot
5790 takes up UNITS_PER_HWFPVALUE bytes, regardless of the
5791 argument's precision. */
5792 rsize = UNITS_PER_HWFPVALUE;
5794 /* Overflow arguments are padded to UNITS_PER_WORD bytes
5795 (= PARM_BOUNDARY bits). This can be different from RSIZE
5796 in two cases:
5798 (1) On 32-bit targets when TYPE is a structure such as:
5800 struct s { float f; };
5802 Such structures are passed in paired FPRs, so RSIZE
5803 will be 8 bytes. However, the structure only takes
5804 up 4 bytes of memory, so OSIZE will only be 4.
5806 (2) In combinations such as -mgp64 -msingle-float
5807 -fshort-double. Doubles passed in registers will then take
5808 up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
5809 stack take up UNITS_PER_WORD bytes. */
5810 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
5812 else
5814 top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
5815 unshare_expr (valist), f_gtop, NULL_TREE);
5816 off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
5817 unshare_expr (valist), f_goff, NULL_TREE);
5818 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
5819 if (rsize > UNITS_PER_WORD)
5821 /* [1] Emit code for: off &= -rsize. */
5822 t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
5823 build_int_cst (TREE_TYPE (off), -rsize));
5824 gimplify_assign (unshare_expr (off), t, pre_p);
5826 osize = rsize;
5829 /* [2] Emit code to branch if off == 0. */
5830 t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
5831 build_int_cst (TREE_TYPE (off), 0));
5832 addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
5834 /* [5] Emit code for: off -= rsize. We do this as a form of
5835 post-decrement not available to C. */
5836 t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
5837 t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
5839 /* [4] Emit code for:
5840 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0). */
5841 t = fold_convert (sizetype, t);
5842 t = fold_build1 (NEGATE_EXPR, sizetype, t);
5843 t = fold_build_pointer_plus (top, t);
5844 if (BYTES_BIG_ENDIAN && rsize > size)
5845 t = fold_build_pointer_plus_hwi (t, rsize - size);
5846 COND_EXPR_THEN (addr) = t;
5848 if (osize > UNITS_PER_WORD)
5850 /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize. */
5851 t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
5852 u = build_int_cst (TREE_TYPE (t), -osize);
5853 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
5854 align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
5855 unshare_expr (ovfl), t);
5857 else
5858 align = NULL;
5860 /* [10, 11] Emit code for:
5861 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
5862 ovfl += osize. */
5863 u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
5864 t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
5865 if (BYTES_BIG_ENDIAN && osize > size)
5866 t = fold_build_pointer_plus_hwi (t, osize - size);
5868 /* String [9] and [10, 11] together. */
5869 if (align)
5870 t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
5871 COND_EXPR_ELSE (addr) = t;
5873 addr = fold_convert (build_pointer_type (type), addr);
5874 addr = build_va_arg_indirect_ref (addr);
5877 if (indirect_p)
5878 addr = build_va_arg_indirect_ref (addr);
5880 return addr;
5883 /* Declare a unique, locally-binding function called NAME, then start
5884 its definition. */
5886 static void
5887 mips_start_unique_function (const char *name)
5889 tree decl;
5891 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
5892 get_identifier (name),
5893 build_function_type_list (void_type_node, NULL_TREE));
5894 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
5895 NULL_TREE, void_type_node);
5896 TREE_PUBLIC (decl) = 1;
5897 TREE_STATIC (decl) = 1;
5899 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
5901 targetm.asm_out.unique_section (decl, 0);
5902 switch_to_section (get_named_section (decl, NULL, 0));
5904 targetm.asm_out.globalize_label (asm_out_file, name);
5905 fputs ("\t.hidden\t", asm_out_file);
5906 assemble_name (asm_out_file, name);
5907 putc ('\n', asm_out_file);
5910 /* Start a definition of function NAME. MIPS16_P indicates whether the
5911 function contains MIPS16 code. */
5913 static void
5914 mips_start_function_definition (const char *name, bool mips16_p)
5916 if (mips16_p)
5917 fprintf (asm_out_file, "\t.set\tmips16\n");
5918 else
5919 fprintf (asm_out_file, "\t.set\tnomips16\n");
5921 if (!flag_inhibit_size_directive)
5923 fputs ("\t.ent\t", asm_out_file);
5924 assemble_name (asm_out_file, name);
5925 fputs ("\n", asm_out_file);
5928 ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
5930 /* Start the definition proper. */
5931 assemble_name (asm_out_file, name);
5932 fputs (":\n", asm_out_file);
5935 /* End a function definition started by mips_start_function_definition. */
5937 static void
5938 mips_end_function_definition (const char *name)
5940 if (!flag_inhibit_size_directive)
5942 fputs ("\t.end\t", asm_out_file);
5943 assemble_name (asm_out_file, name);
5944 fputs ("\n", asm_out_file);
5948 /* Output a definition of the __mips16_rdhwr function. */
5950 static void
5951 mips_output_mips16_rdhwr (void)
5953 const char *name;
5955 name = "__mips16_rdhwr";
5956 mips_start_unique_function (name);
5957 mips_start_function_definition (name, false);
5958 fprintf (asm_out_file,
5959 "\t.set\tpush\n"
5960 "\t.set\tmips32r2\n"
5961 "\t.set\tnoreorder\n"
5962 "\trdhwr\t$3,$29\n"
5963 "\t.set\tpop\n"
5964 "\tj\t$31\n");
5965 mips_end_function_definition (name);
5968 /* Return true if calls to X can use R_MIPS_CALL* relocations. */
5970 static bool
5971 mips_ok_for_lazy_binding_p (rtx x)
5973 return (TARGET_USE_GOT
5974 && GET_CODE (x) == SYMBOL_REF
5975 && !SYMBOL_REF_BIND_NOW_P (x)
5976 && !mips_symbol_binds_local_p (x));
5979 /* Load function address ADDR into register DEST. TYPE is as for
5980 mips_expand_call. Return true if we used an explicit lazy-binding
5981 sequence. */
5983 static bool
5984 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
5986 /* If we're generating PIC, and this call is to a global function,
5987 try to allow its address to be resolved lazily. This isn't
5988 possible for sibcalls when $gp is call-saved because the value
5989 of $gp on entry to the stub would be our caller's gp, not ours. */
5990 if (TARGET_EXPLICIT_RELOCS
5991 && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
5992 && mips_ok_for_lazy_binding_p (addr))
5994 addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
5995 emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
5996 return true;
5998 else
6000 mips_emit_move (dest, addr);
6001 return false;
6005 /* Each locally-defined hard-float MIPS16 function has a local symbol
6006 associated with it. This hash table maps the function symbol (FUNC)
6007 to the local symbol (LOCAL). */
6008 struct GTY(()) mips16_local_alias {
6009 rtx func;
6010 rtx local;
6012 static GTY ((param_is (struct mips16_local_alias))) htab_t mips16_local_aliases;
6014 /* Hash table callbacks for mips16_local_aliases. */
6016 static hashval_t
6017 mips16_local_aliases_hash (const void *entry)
6019 const struct mips16_local_alias *alias;
6021 alias = (const struct mips16_local_alias *) entry;
6022 return htab_hash_string (XSTR (alias->func, 0));
6025 static int
6026 mips16_local_aliases_eq (const void *entry1, const void *entry2)
6028 const struct mips16_local_alias *alias1, *alias2;
6030 alias1 = (const struct mips16_local_alias *) entry1;
6031 alias2 = (const struct mips16_local_alias *) entry2;
6032 return rtx_equal_p (alias1->func, alias2->func);
6035 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
6036 Return a local alias for it, creating a new one if necessary. */
6038 static rtx
6039 mips16_local_alias (rtx func)
6041 struct mips16_local_alias *alias, tmp_alias;
6042 void **slot;
6044 /* Create the hash table if this is the first call. */
6045 if (mips16_local_aliases == NULL)
6046 mips16_local_aliases = htab_create_ggc (37, mips16_local_aliases_hash,
6047 mips16_local_aliases_eq, NULL);
6049 /* Look up the function symbol, creating a new entry if need be. */
6050 tmp_alias.func = func;
6051 slot = htab_find_slot (mips16_local_aliases, &tmp_alias, INSERT);
6052 gcc_assert (slot != NULL);
6054 alias = (struct mips16_local_alias *) *slot;
6055 if (alias == NULL)
6057 const char *func_name, *local_name;
6058 rtx local;
6060 /* Create a new SYMBOL_REF for the local symbol. The choice of
6061 __fn_local_* is based on the __fn_stub_* names that we've
6062 traditionally used for the non-MIPS16 stub. */
6063 func_name = targetm.strip_name_encoding (XSTR (func, 0));
6064 local_name = ACONCAT (("__fn_local_", func_name, NULL));
6065 local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
6066 SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
6068 /* Create a new structure to represent the mapping. */
6069 alias = ggc_alloc_mips16_local_alias ();
6070 alias->func = func;
6071 alias->local = local;
6072 *slot = alias;
6074 return alias->local;
6077 /* A chained list of functions for which mips16_build_call_stub has already
6078 generated a stub. NAME is the name of the function and FP_RET_P is true
6079 if the function returns a value in floating-point registers. */
6080 struct mips16_stub {
6081 struct mips16_stub *next;
6082 char *name;
6083 bool fp_ret_p;
6085 static struct mips16_stub *mips16_stubs;
6087 /* Return the two-character string that identifies floating-point
6088 return mode MODE in the name of a MIPS16 function stub. */
6090 static const char *
6091 mips16_call_stub_mode_suffix (enum machine_mode mode)
6093 if (mode == SFmode)
6094 return "sf";
6095 else if (mode == DFmode)
6096 return "df";
6097 else if (mode == SCmode)
6098 return "sc";
6099 else if (mode == DCmode)
6100 return "dc";
6101 else if (mode == V2SFmode)
6102 return "df";
6103 else
6104 gcc_unreachable ();
6107 /* Write instructions to move a 32-bit value between general register
6108 GPREG and floating-point register FPREG. DIRECTION is 't' to move
6109 from GPREG to FPREG and 'f' to move in the opposite direction. */
6111 static void
6112 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6114 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6115 reg_names[gpreg], reg_names[fpreg]);
6118 /* Likewise for 64-bit values. */
6120 static void
6121 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6123 if (TARGET_64BIT)
6124 fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
6125 reg_names[gpreg], reg_names[fpreg]);
6126 else if (TARGET_FLOAT64)
6128 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6129 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6130 fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
6131 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
6133 else
6135 /* Move the least-significant word. */
6136 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6137 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6138 /* ...then the most significant word. */
6139 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6140 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
6144 /* Write out code to move floating-point arguments into or out of
6145 general registers. FP_CODE is the code describing which arguments
6146 are present (see the comment above the definition of CUMULATIVE_ARGS
6147 in mips.h). DIRECTION is as for mips_output_32bit_xfer. */
6149 static void
6150 mips_output_args_xfer (int fp_code, char direction)
6152 unsigned int gparg, fparg, f;
6153 CUMULATIVE_ARGS cum;
6155 /* This code only works for o32 and o64. */
6156 gcc_assert (TARGET_OLDABI);
6158 mips_init_cumulative_args (&cum, NULL);
6160 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6162 enum machine_mode mode;
6163 struct mips_arg_info info;
6165 if ((f & 3) == 1)
6166 mode = SFmode;
6167 else if ((f & 3) == 2)
6168 mode = DFmode;
6169 else
6170 gcc_unreachable ();
6172 mips_get_arg_info (&info, &cum, mode, NULL, true);
6173 gparg = mips_arg_regno (&info, false);
6174 fparg = mips_arg_regno (&info, true);
6176 if (mode == SFmode)
6177 mips_output_32bit_xfer (direction, gparg, fparg);
6178 else
6179 mips_output_64bit_xfer (direction, gparg, fparg);
6181 mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
6185 /* Write a MIPS16 stub for the current function. This stub is used
6186 for functions which take arguments in the floating-point registers.
6187 It is normal-mode code that moves the floating-point arguments
6188 into the general registers and then jumps to the MIPS16 code. */
6190 static void
6191 mips16_build_function_stub (void)
6193 const char *fnname, *alias_name, *separator;
6194 char *secname, *stubname;
6195 tree stubdecl;
6196 unsigned int f;
6197 rtx symbol, alias;
6199 /* Create the name of the stub, and its unique section. */
6200 symbol = XEXP (DECL_RTL (current_function_decl), 0);
6201 alias = mips16_local_alias (symbol);
6203 fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6204 alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6205 secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6206 stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6208 /* Build a decl for the stub. */
6209 stubdecl = build_decl (BUILTINS_LOCATION,
6210 FUNCTION_DECL, get_identifier (stubname),
6211 build_function_type_list (void_type_node, NULL_TREE));
6212 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6213 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6214 RESULT_DECL, NULL_TREE, void_type_node);
6216 /* Output a comment. */
6217 fprintf (asm_out_file, "\t# Stub function for %s (",
6218 current_function_name ());
6219 separator = "";
6220 for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6222 fprintf (asm_out_file, "%s%s", separator,
6223 (f & 3) == 1 ? "float" : "double");
6224 separator = ", ";
6226 fprintf (asm_out_file, ")\n");
6228 /* Start the function definition. */
6229 assemble_start_function (stubdecl, stubname);
6230 mips_start_function_definition (stubname, false);
6232 /* If generating pic2 code, either set up the global pointer or
6233 switch to pic0. */
6234 if (TARGET_ABICALLS_PIC2)
6236 if (TARGET_ABSOLUTE_ABICALLS)
6237 fprintf (asm_out_file, "\t.option\tpic0\n");
6238 else
6240 output_asm_insn ("%(.cpload\t%^%)", NULL);
6241 /* Emit an R_MIPS_NONE relocation to tell the linker what the
6242 target function is. Use a local GOT access when loading the
6243 symbol, to cut down on the number of unnecessary GOT entries
6244 for stubs that aren't needed. */
6245 output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6246 symbol = alias;
6250 /* Load the address of the MIPS16 function into $25. Do this first so
6251 that targets with coprocessor interlocks can use an MFC1 to fill the
6252 delay slot. */
6253 output_asm_insn ("la\t%^,%0", &symbol);
6255 /* Move the arguments from floating-point registers to general registers. */
6256 mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6258 /* Jump to the MIPS16 function. */
6259 output_asm_insn ("jr\t%^", NULL);
6261 if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6262 fprintf (asm_out_file, "\t.option\tpic2\n");
6264 mips_end_function_definition (stubname);
6266 /* If the linker needs to create a dynamic symbol for the target
6267 function, it will associate the symbol with the stub (which,
6268 unlike the target function, follows the proper calling conventions).
6269 It is therefore useful to have a local alias for the target function,
6270 so that it can still be identified as MIPS16 code. As an optimization,
6271 this symbol can also be used for indirect MIPS16 references from
6272 within this file. */
6273 ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6275 switch_to_section (function_section (current_function_decl));
6278 /* The current function is a MIPS16 function that returns a value in an FPR.
6279 Copy the return value from its soft-float to its hard-float location.
6280 libgcc2 has special non-MIPS16 helper functions for each case. */
6282 static void
6283 mips16_copy_fpr_return_value (void)
6285 rtx fn, insn, retval;
6286 tree return_type;
6287 enum machine_mode return_mode;
6288 const char *name;
6290 return_type = DECL_RESULT (current_function_decl);
6291 return_mode = DECL_MODE (return_type);
6293 name = ACONCAT (("__mips16_ret_",
6294 mips16_call_stub_mode_suffix (return_mode),
6295 NULL));
6296 fn = mips16_stub_function (name);
6298 /* The function takes arguments in $2 (and possibly $3), so calls
6299 to it cannot be lazily bound. */
6300 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6302 /* Model the call as something that takes the GPR return value as
6303 argument and returns an "updated" value. */
6304 retval = gen_rtx_REG (return_mode, GP_RETURN);
6305 insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6306 const0_rtx, NULL_RTX, false);
6307 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6310 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6311 RETVAL is the location of the return value, or null if this is
6312 a "call" rather than a "call_value". ARGS_SIZE is the size of the
6313 arguments and FP_CODE is the code built by mips_function_arg;
6314 see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6316 There are three alternatives:
6318 - If a stub was needed, emit the call and return the call insn itself.
6320 - If we can avoid using a stub by redirecting the call, set *FN_PTR
6321 to the new target and return null.
6323 - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6324 unmodified.
6326 A stub is needed for calls to functions that, in normal mode,
6327 receive arguments in FPRs or return values in FPRs. The stub
6328 copies the arguments from their soft-float positions to their
6329 hard-float positions, calls the real function, then copies the
6330 return value from its hard-float position to its soft-float
6331 position.
6333 We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6334 If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6335 automatically redirects the JAL to the stub, otherwise the JAL
6336 continues to call FN directly. */
6338 static rtx
6339 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6341 const char *fnname;
6342 bool fp_ret_p;
6343 struct mips16_stub *l;
6344 rtx insn, fn;
6346 /* We don't need to do anything if we aren't in MIPS16 mode, or if
6347 we were invoked with the -msoft-float option. */
6348 if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
6349 return NULL_RTX;
6351 /* Figure out whether the value might come back in a floating-point
6352 register. */
6353 fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
6355 /* We don't need to do anything if there were no floating-point
6356 arguments and the value will not be returned in a floating-point
6357 register. */
6358 if (fp_code == 0 && !fp_ret_p)
6359 return NULL_RTX;
6361 /* We don't need to do anything if this is a call to a special
6362 MIPS16 support function. */
6363 fn = *fn_ptr;
6364 if (mips16_stub_function_p (fn))
6365 return NULL_RTX;
6367 /* If we're calling a locally-defined MIPS16 function, we know that
6368 it will return values in both the "soft-float" and "hard-float"
6369 registers. There is no need to use a stub to move the latter
6370 to the former. */
6371 if (fp_code == 0 && mips16_local_function_p (fn))
6372 return NULL_RTX;
6374 /* This code will only work for o32 and o64 abis. The other ABI's
6375 require more sophisticated support. */
6376 gcc_assert (TARGET_OLDABI);
6378 /* If we're calling via a function pointer, use one of the magic
6379 libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
6380 Each stub expects the function address to arrive in register $2. */
6381 if (GET_CODE (fn) != SYMBOL_REF
6382 || !call_insn_operand (fn, VOIDmode))
6384 char buf[30];
6385 rtx stub_fn, insn, addr;
6386 bool lazy_p;
6388 /* If this is a locally-defined and locally-binding function,
6389 avoid the stub by calling the local alias directly. */
6390 if (mips16_local_function_p (fn))
6392 *fn_ptr = mips16_local_alias (fn);
6393 return NULL_RTX;
6396 /* Create a SYMBOL_REF for the libgcc.a function. */
6397 if (fp_ret_p)
6398 sprintf (buf, "__mips16_call_stub_%s_%d",
6399 mips16_call_stub_mode_suffix (GET_MODE (retval)),
6400 fp_code);
6401 else
6402 sprintf (buf, "__mips16_call_stub_%d", fp_code);
6403 stub_fn = mips16_stub_function (buf);
6405 /* The function uses $2 as an argument, so calls to it
6406 cannot be lazily bound. */
6407 SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6409 /* Load the target function into $2. */
6410 addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6411 lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
6413 /* Emit the call. */
6414 insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
6415 args_size, NULL_RTX, lazy_p);
6417 /* Tell GCC that this call does indeed use the value of $2. */
6418 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
6420 /* If we are handling a floating-point return value, we need to
6421 save $18 in the function prologue. Putting a note on the
6422 call will mean that df_regs_ever_live_p ($18) will be true if the
6423 call is not eliminated, and we can check that in the prologue
6424 code. */
6425 if (fp_ret_p)
6426 CALL_INSN_FUNCTION_USAGE (insn) =
6427 gen_rtx_EXPR_LIST (VOIDmode,
6428 gen_rtx_CLOBBER (VOIDmode,
6429 gen_rtx_REG (word_mode, 18)),
6430 CALL_INSN_FUNCTION_USAGE (insn));
6432 return insn;
6435 /* We know the function we are going to call. If we have already
6436 built a stub, we don't need to do anything further. */
6437 fnname = targetm.strip_name_encoding (XSTR (fn, 0));
6438 for (l = mips16_stubs; l != NULL; l = l->next)
6439 if (strcmp (l->name, fnname) == 0)
6440 break;
6442 if (l == NULL)
6444 const char *separator;
6445 char *secname, *stubname;
6446 tree stubid, stubdecl;
6447 unsigned int f;
6449 /* If the function does not return in FPRs, the special stub
6450 section is named
6451 .mips16.call.FNNAME
6453 If the function does return in FPRs, the stub section is named
6454 .mips16.call.fp.FNNAME
6456 Build a decl for the stub. */
6457 secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
6458 fnname, NULL));
6459 stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
6460 fnname, NULL));
6461 stubid = get_identifier (stubname);
6462 stubdecl = build_decl (BUILTINS_LOCATION,
6463 FUNCTION_DECL, stubid,
6464 build_function_type_list (void_type_node,
6465 NULL_TREE));
6466 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6467 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6468 RESULT_DECL, NULL_TREE,
6469 void_type_node);
6471 /* Output a comment. */
6472 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
6473 (fp_ret_p
6474 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
6475 : ""),
6476 fnname);
6477 separator = "";
6478 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6480 fprintf (asm_out_file, "%s%s", separator,
6481 (f & 3) == 1 ? "float" : "double");
6482 separator = ", ";
6484 fprintf (asm_out_file, ")\n");
6486 /* Start the function definition. */
6487 assemble_start_function (stubdecl, stubname);
6488 mips_start_function_definition (stubname, false);
6490 if (fp_ret_p)
6492 fprintf (asm_out_file, "\t.cfi_startproc\n");
6494 /* Create a fake CFA 4 bytes below the stack pointer.
6495 This works around unwinders (like libgcc's) that expect
6496 the CFA for non-signal frames to be unique. */
6497 fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
6499 /* "Save" $sp in itself so we don't use the fake CFA.
6500 This is: DW_CFA_val_expression r29, { DW_OP_reg29 }. */
6501 fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
6503 else
6505 /* Load the address of the MIPS16 function into $25. Do this
6506 first so that targets with coprocessor interlocks can use
6507 an MFC1 to fill the delay slot. */
6508 if (TARGET_EXPLICIT_RELOCS)
6510 output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
6511 output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
6513 else
6514 output_asm_insn ("la\t%^,%0", &fn);
6517 /* Move the arguments from general registers to floating-point
6518 registers. */
6519 mips_output_args_xfer (fp_code, 't');
6521 if (fp_ret_p)
6523 /* Save the return address in $18 and call the non-MIPS16 function.
6524 The stub's caller knows that $18 might be clobbered, even though
6525 $18 is usually a call-saved register. */
6526 fprintf (asm_out_file, "\tmove\t%s,%s\n",
6527 reg_names[GP_REG_FIRST + 18], reg_names[RETURN_ADDR_REGNUM]);
6528 output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
6529 fprintf (asm_out_file, "\t.cfi_register 31,18\n");
6531 /* Move the result from floating-point registers to
6532 general registers. */
6533 switch (GET_MODE (retval))
6535 case SCmode:
6536 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
6537 TARGET_BIG_ENDIAN
6538 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6539 : FP_REG_FIRST);
6540 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
6541 TARGET_LITTLE_ENDIAN
6542 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6543 : FP_REG_FIRST);
6544 if (GET_MODE (retval) == SCmode && TARGET_64BIT)
6546 /* On 64-bit targets, complex floats are returned in
6547 a single GPR, such that "sd" on a suitably-aligned
6548 target would store the value correctly. */
6549 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6550 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6551 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6552 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6553 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
6554 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
6555 fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
6556 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6557 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6558 fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
6559 reg_names[GP_RETURN],
6560 reg_names[GP_RETURN],
6561 reg_names[GP_RETURN + 1]);
6563 break;
6565 case SFmode:
6566 mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6567 break;
6569 case DCmode:
6570 mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
6571 FP_REG_FIRST + MAX_FPRS_PER_FMT);
6572 /* Fall though. */
6573 case DFmode:
6574 case V2SFmode:
6575 mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6576 break;
6578 default:
6579 gcc_unreachable ();
6581 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
6582 fprintf (asm_out_file, "\t.cfi_endproc\n");
6584 else
6586 /* Jump to the previously-loaded address. */
6587 output_asm_insn ("jr\t%^", NULL);
6590 #ifdef ASM_DECLARE_FUNCTION_SIZE
6591 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
6592 #endif
6594 mips_end_function_definition (stubname);
6596 /* Record this stub. */
6597 l = XNEW (struct mips16_stub);
6598 l->name = xstrdup (fnname);
6599 l->fp_ret_p = fp_ret_p;
6600 l->next = mips16_stubs;
6601 mips16_stubs = l;
6604 /* If we expect a floating-point return value, but we've built a
6605 stub which does not expect one, then we're in trouble. We can't
6606 use the existing stub, because it won't handle the floating-point
6607 value. We can't build a new stub, because the linker won't know
6608 which stub to use for the various calls in this object file.
6609 Fortunately, this case is illegal, since it means that a function
6610 was declared in two different ways in a single compilation. */
6611 if (fp_ret_p && !l->fp_ret_p)
6612 error ("cannot handle inconsistent calls to %qs", fnname);
6614 if (retval == NULL_RTX)
6615 insn = gen_call_internal_direct (fn, args_size);
6616 else
6617 insn = gen_call_value_internal_direct (retval, fn, args_size);
6618 insn = mips_emit_call_insn (insn, fn, fn, false);
6620 /* If we are calling a stub which handles a floating-point return
6621 value, we need to arrange to save $18 in the prologue. We do this
6622 by marking the function call as using the register. The prologue
6623 will later see that it is used, and emit code to save it. */
6624 if (fp_ret_p)
6625 CALL_INSN_FUNCTION_USAGE (insn) =
6626 gen_rtx_EXPR_LIST (VOIDmode,
6627 gen_rtx_CLOBBER (VOIDmode,
6628 gen_rtx_REG (word_mode, 18)),
6629 CALL_INSN_FUNCTION_USAGE (insn));
6631 return insn;
6634 /* Expand a call of type TYPE. RESULT is where the result will go (null
6635 for "call"s and "sibcall"s), ADDR is the address of the function,
6636 ARGS_SIZE is the size of the arguments and AUX is the value passed
6637 to us by mips_function_arg. LAZY_P is true if this call already
6638 involves a lazily-bound function address (such as when calling
6639 functions through a MIPS16 hard-float stub).
6641 Return the call itself. */
6644 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
6645 rtx args_size, rtx aux, bool lazy_p)
6647 rtx orig_addr, pattern, insn;
6648 int fp_code;
6650 fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
6651 insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
6652 if (insn)
6654 gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
6655 return insn;
6658 orig_addr = addr;
6659 if (!call_insn_operand (addr, VOIDmode))
6661 if (type == MIPS_CALL_EPILOGUE)
6662 addr = MIPS_EPILOGUE_TEMP (Pmode);
6663 else
6664 addr = gen_reg_rtx (Pmode);
6665 lazy_p |= mips_load_call_address (type, addr, orig_addr);
6668 if (result == 0)
6670 rtx (*fn) (rtx, rtx);
6672 if (type == MIPS_CALL_SIBCALL)
6673 fn = gen_sibcall_internal;
6674 else
6675 fn = gen_call_internal;
6677 pattern = fn (addr, args_size);
6679 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
6681 /* Handle return values created by mips_return_fpr_pair. */
6682 rtx (*fn) (rtx, rtx, rtx, rtx);
6683 rtx reg1, reg2;
6685 if (type == MIPS_CALL_SIBCALL)
6686 fn = gen_sibcall_value_multiple_internal;
6687 else
6688 fn = gen_call_value_multiple_internal;
6690 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
6691 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
6692 pattern = fn (reg1, addr, args_size, reg2);
6694 else
6696 rtx (*fn) (rtx, rtx, rtx);
6698 if (type == MIPS_CALL_SIBCALL)
6699 fn = gen_sibcall_value_internal;
6700 else
6701 fn = gen_call_value_internal;
6703 /* Handle return values created by mips_return_fpr_single. */
6704 if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
6705 result = XEXP (XVECEXP (result, 0, 0), 0);
6706 pattern = fn (result, addr, args_size);
6709 return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
6712 /* Split call instruction INSN into a $gp-clobbering call and
6713 (where necessary) an instruction to restore $gp from its save slot.
6714 CALL_PATTERN is the pattern of the new call. */
6716 void
6717 mips_split_call (rtx insn, rtx call_pattern)
6719 emit_call_insn (call_pattern);
6720 if (!find_reg_note (insn, REG_NORETURN, 0))
6721 /* Pick a temporary register that is suitable for both MIPS16 and
6722 non-MIPS16 code. $4 and $5 are used for returning complex double
6723 values in soft-float code, so $6 is the first suitable candidate. */
6724 mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode, GP_ARG_FIRST + 2));
6727 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
6729 static bool
6730 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
6732 if (!TARGET_SIBCALLS)
6733 return false;
6735 /* Interrupt handlers need special epilogue code and therefore can't
6736 use sibcalls. */
6737 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
6738 return false;
6740 /* We can't do a sibcall if the called function is a MIPS16 function
6741 because there is no direct "jx" instruction equivalent to "jalx" to
6742 switch the ISA mode. We only care about cases where the sibling
6743 and normal calls would both be direct. */
6744 if (decl
6745 && mips_use_mips16_mode_p (decl)
6746 && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode))
6747 return false;
6749 /* When -minterlink-mips16 is in effect, assume that non-locally-binding
6750 functions could be MIPS16 ones unless an attribute explicitly tells
6751 us otherwise. */
6752 if (TARGET_INTERLINK_MIPS16
6753 && decl
6754 && (DECL_EXTERNAL (decl) || !targetm.binds_local_p (decl))
6755 && !mips_nomips16_decl_p (decl)
6756 && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode))
6757 return false;
6759 /* Otherwise OK. */
6760 return true;
6763 /* Emit code to move general operand SRC into condition-code
6764 register DEST given that SCRATCH is a scratch TFmode FPR.
6765 The sequence is:
6767 FP1 = SRC
6768 FP2 = 0.0f
6769 DEST = FP2 < FP1
6771 where FP1 and FP2 are single-precision FPRs taken from SCRATCH. */
6773 void
6774 mips_expand_fcc_reload (rtx dest, rtx src, rtx scratch)
6776 rtx fp1, fp2;
6778 /* Change the source to SFmode. */
6779 if (MEM_P (src))
6780 src = adjust_address (src, SFmode, 0);
6781 else if (REG_P (src) || GET_CODE (src) == SUBREG)
6782 src = gen_rtx_REG (SFmode, true_regnum (src));
6784 fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
6785 fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + MAX_FPRS_PER_FMT);
6787 mips_emit_move (copy_rtx (fp1), src);
6788 mips_emit_move (copy_rtx (fp2), CONST0_RTX (SFmode));
6789 emit_insn (gen_slt_sf (dest, fp2, fp1));
6792 /* Implement MOVE_BY_PIECES_P. */
6794 bool
6795 mips_move_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
6797 if (HAVE_movmemsi)
6799 /* movmemsi is meant to generate code that is at least as good as
6800 move_by_pieces. However, movmemsi effectively uses a by-pieces
6801 implementation both for moves smaller than a word and for
6802 word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
6803 bytes. We should allow the tree-level optimisers to do such
6804 moves by pieces, as it often exposes other optimization
6805 opportunities. We might as well continue to use movmemsi at
6806 the rtl level though, as it produces better code when
6807 scheduling is disabled (such as at -O). */
6808 if (currently_expanding_to_rtl)
6809 return false;
6810 if (align < BITS_PER_WORD)
6811 return size < UNITS_PER_WORD;
6812 return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
6814 /* The default value. If this becomes a target hook, we should
6815 call the default definition instead. */
6816 return (move_by_pieces_ninsns (size, align, MOVE_MAX_PIECES + 1)
6817 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()));
6820 /* Implement STORE_BY_PIECES_P. */
6822 bool
6823 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
6825 /* Storing by pieces involves moving constants into registers
6826 of size MIN (ALIGN, BITS_PER_WORD), then storing them.
6827 We need to decide whether it is cheaper to load the address of
6828 constant data into a register and use a block move instead. */
6830 /* If the data is only byte aligned, then:
6832 (a1) A block move of less than 4 bytes would involve three 3 LBs and
6833 3 SBs. We might as well use 3 single-instruction LIs and 3 SBs
6834 instead.
6836 (a2) A block move of 4 bytes from aligned source data can use an
6837 LW/SWL/SWR sequence. This is often better than the 4 LIs and
6838 4 SBs that we would generate when storing by pieces. */
6839 if (align <= BITS_PER_UNIT)
6840 return size < 4;
6842 /* If the data is 2-byte aligned, then:
6844 (b1) A block move of less than 4 bytes would use a combination of LBs,
6845 LHs, SBs and SHs. We get better code by using single-instruction
6846 LIs, SBs and SHs instead.
6848 (b2) A block move of 4 bytes from aligned source data would again use
6849 an LW/SWL/SWR sequence. In most cases, loading the address of
6850 the source data would require at least one extra instruction.
6851 It is often more efficient to use 2 single-instruction LIs and
6852 2 SHs instead.
6854 (b3) A block move of up to 3 additional bytes would be like (b1).
6856 (b4) A block move of 8 bytes from aligned source data can use two
6857 LW/SWL/SWR sequences or a single LD/SDL/SDR sequence. Both
6858 sequences are better than the 4 LIs and 4 SHs that we'd generate
6859 when storing by pieces.
6861 The reasoning for higher alignments is similar:
6863 (c1) A block move of less than 4 bytes would be the same as (b1).
6865 (c2) A block move of 4 bytes would use an LW/SW sequence. Again,
6866 loading the address of the source data would typically require
6867 at least one extra instruction. It is generally better to use
6868 LUI/ORI/SW instead.
6870 (c3) A block move of up to 3 additional bytes would be like (b1).
6872 (c4) A block move of 8 bytes can use two LW/SW sequences or a single
6873 LD/SD sequence, and in these cases we've traditionally preferred
6874 the memory copy over the more bulky constant moves. */
6875 return size < 8;
6878 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
6879 Assume that the areas do not overlap. */
6881 static void
6882 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
6884 HOST_WIDE_INT offset, delta;
6885 unsigned HOST_WIDE_INT bits;
6886 int i;
6887 enum machine_mode mode;
6888 rtx *regs;
6890 /* Work out how many bits to move at a time. If both operands have
6891 half-word alignment, it is usually better to move in half words.
6892 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
6893 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
6894 Otherwise move word-sized chunks. */
6895 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
6896 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
6897 bits = BITS_PER_WORD / 2;
6898 else
6899 bits = BITS_PER_WORD;
6901 mode = mode_for_size (bits, MODE_INT, 0);
6902 delta = bits / BITS_PER_UNIT;
6904 /* Allocate a buffer for the temporary registers. */
6905 regs = XALLOCAVEC (rtx, length / delta);
6907 /* Load as many BITS-sized chunks as possible. Use a normal load if
6908 the source has enough alignment, otherwise use left/right pairs. */
6909 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
6911 regs[i] = gen_reg_rtx (mode);
6912 if (MEM_ALIGN (src) >= bits)
6913 mips_emit_move (regs[i], adjust_address (src, mode, offset));
6914 else
6916 rtx part = adjust_address (src, BLKmode, offset);
6917 if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0))
6918 gcc_unreachable ();
6922 /* Copy the chunks to the destination. */
6923 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
6924 if (MEM_ALIGN (dest) >= bits)
6925 mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
6926 else
6928 rtx part = adjust_address (dest, BLKmode, offset);
6929 if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
6930 gcc_unreachable ();
6933 /* Mop up any left-over bytes. */
6934 if (offset < length)
6936 src = adjust_address (src, BLKmode, offset);
6937 dest = adjust_address (dest, BLKmode, offset);
6938 move_by_pieces (dest, src, length - offset,
6939 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
6943 /* Helper function for doing a loop-based block operation on memory
6944 reference MEM. Each iteration of the loop will operate on LENGTH
6945 bytes of MEM.
6947 Create a new base register for use within the loop and point it to
6948 the start of MEM. Create a new memory reference that uses this
6949 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
6951 static void
6952 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
6953 rtx *loop_reg, rtx *loop_mem)
6955 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
6957 /* Although the new mem does not refer to a known location,
6958 it does keep up to LENGTH bytes of alignment. */
6959 *loop_mem = change_address (mem, BLKmode, *loop_reg);
6960 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
6963 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
6964 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
6965 the memory regions do not overlap. */
6967 static void
6968 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
6969 HOST_WIDE_INT bytes_per_iter)
6971 rtx label, src_reg, dest_reg, final_src, test;
6972 HOST_WIDE_INT leftover;
6974 leftover = length % bytes_per_iter;
6975 length -= leftover;
6977 /* Create registers and memory references for use within the loop. */
6978 mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
6979 mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
6981 /* Calculate the value that SRC_REG should have after the last iteration
6982 of the loop. */
6983 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
6984 0, 0, OPTAB_WIDEN);
6986 /* Emit the start of the loop. */
6987 label = gen_label_rtx ();
6988 emit_label (label);
6990 /* Emit the loop body. */
6991 mips_block_move_straight (dest, src, bytes_per_iter);
6993 /* Move on to the next block. */
6994 mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
6995 mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
6997 /* Emit the loop condition. */
6998 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
6999 if (Pmode == DImode)
7000 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
7001 else
7002 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
7004 /* Mop up any left-over bytes. */
7005 if (leftover)
7006 mips_block_move_straight (dest, src, leftover);
7009 /* Expand a movmemsi instruction, which copies LENGTH bytes from
7010 memory reference SRC to memory reference DEST. */
7012 bool
7013 mips_expand_block_move (rtx dest, rtx src, rtx length)
7015 if (CONST_INT_P (length))
7017 if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
7019 mips_block_move_straight (dest, src, INTVAL (length));
7020 return true;
7022 else if (optimize)
7024 mips_block_move_loop (dest, src, INTVAL (length),
7025 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
7026 return true;
7029 return false;
7032 /* Expand a loop of synci insns for the address range [BEGIN, END). */
7034 void
7035 mips_expand_synci_loop (rtx begin, rtx end)
7037 rtx inc, label, end_label, cmp_result, mask, length;
7039 /* Create end_label. */
7040 end_label = gen_label_rtx ();
7042 /* Check if begin equals end. */
7043 cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
7044 emit_jump_insn (gen_condjump (cmp_result, end_label));
7046 /* Load INC with the cache line size (rdhwr INC,$1). */
7047 inc = gen_reg_rtx (Pmode);
7048 emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
7050 /* Check if inc is 0. */
7051 cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
7052 emit_jump_insn (gen_condjump (cmp_result, end_label));
7054 /* Calculate mask. */
7055 mask = mips_force_unary (Pmode, NEG, inc);
7057 /* Mask out begin by mask. */
7058 begin = mips_force_binary (Pmode, AND, begin, mask);
7060 /* Calculate length. */
7061 length = mips_force_binary (Pmode, MINUS, end, begin);
7063 /* Loop back to here. */
7064 label = gen_label_rtx ();
7065 emit_label (label);
7067 emit_insn (gen_synci (begin));
7069 /* Update length. */
7070 mips_emit_binary (MINUS, length, length, inc);
7072 /* Update begin. */
7073 mips_emit_binary (PLUS, begin, begin, inc);
7075 /* Check if length is greater than 0. */
7076 cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
7077 emit_jump_insn (gen_condjump (cmp_result, label));
7079 emit_label (end_label);
7082 /* Expand a QI or HI mode atomic memory operation.
7084 GENERATOR contains a pointer to the gen_* function that generates
7085 the SI mode underlying atomic operation using masks that we
7086 calculate.
7088 RESULT is the return register for the operation. Its value is NULL
7089 if unused.
7091 MEM is the location of the atomic access.
7093 OLDVAL is the first operand for the operation.
7095 NEWVAL is the optional second operand for the operation. Its value
7096 is NULL if unused. */
7098 void
7099 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
7100 rtx result, rtx mem, rtx oldval, rtx newval)
7102 rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
7103 rtx unshifted_mask_reg, mask, inverted_mask, si_op;
7104 rtx res = NULL;
7105 enum machine_mode mode;
7107 mode = GET_MODE (mem);
7109 /* Compute the address of the containing SImode value. */
7110 orig_addr = force_reg (Pmode, XEXP (mem, 0));
7111 memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
7112 force_reg (Pmode, GEN_INT (-4)));
7114 /* Create a memory reference for it. */
7115 memsi = gen_rtx_MEM (SImode, memsi_addr);
7116 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
7117 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
7119 /* Work out the byte offset of the QImode or HImode value,
7120 counting from the least significant byte. */
7121 shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
7122 if (TARGET_BIG_ENDIAN)
7123 mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
7125 /* Multiply by eight to convert the shift value from bytes to bits. */
7126 mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
7128 /* Make the final shift an SImode value, so that it can be used in
7129 SImode operations. */
7130 shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
7132 /* Set MASK to an inclusive mask of the QImode or HImode value. */
7133 unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
7134 unshifted_mask_reg = force_reg (SImode, unshifted_mask);
7135 mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
7137 /* Compute the equivalent exclusive mask. */
7138 inverted_mask = gen_reg_rtx (SImode);
7139 emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
7140 gen_rtx_NOT (SImode, mask)));
7142 /* Shift the old value into place. */
7143 if (oldval != const0_rtx)
7145 oldval = convert_modes (SImode, mode, oldval, true);
7146 oldval = force_reg (SImode, oldval);
7147 oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
7150 /* Do the same for the new value. */
7151 if (newval && newval != const0_rtx)
7153 newval = convert_modes (SImode, mode, newval, true);
7154 newval = force_reg (SImode, newval);
7155 newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
7158 /* Do the SImode atomic access. */
7159 if (result)
7160 res = gen_reg_rtx (SImode);
7161 if (newval)
7162 si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
7163 else if (result)
7164 si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
7165 else
7166 si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
7168 emit_insn (si_op);
7170 if (result)
7172 /* Shift and convert the result. */
7173 mips_emit_binary (AND, res, res, mask);
7174 mips_emit_binary (LSHIFTRT, res, res, shiftsi);
7175 mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
7179 /* Return true if it is possible to use left/right accesses for a
7180 bitfield of WIDTH bits starting BITPOS bits into *OP. When
7181 returning true, update *OP, *LEFT and *RIGHT as follows:
7183 *OP is a BLKmode reference to the whole field.
7185 *LEFT is a QImode reference to the first byte if big endian or
7186 the last byte if little endian. This address can be used in the
7187 left-side instructions (LWL, SWL, LDL, SDL).
7189 *RIGHT is a QImode reference to the opposite end of the field and
7190 can be used in the patterning right-side instruction. */
7192 static bool
7193 mips_get_unaligned_mem (rtx *op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
7194 rtx *left, rtx *right)
7196 rtx first, last;
7198 /* Check that the operand really is a MEM. Not all the extv and
7199 extzv predicates are checked. */
7200 if (!MEM_P (*op))
7201 return false;
7203 /* Check that the size is valid. */
7204 if (width != 32 && (!TARGET_64BIT || width != 64))
7205 return false;
7207 /* We can only access byte-aligned values. Since we are always passed
7208 a reference to the first byte of the field, it is not necessary to
7209 do anything with BITPOS after this check. */
7210 if (bitpos % BITS_PER_UNIT != 0)
7211 return false;
7213 /* Reject aligned bitfields: we want to use a normal load or store
7214 instead of a left/right pair. */
7215 if (MEM_ALIGN (*op) >= width)
7216 return false;
7218 /* Adjust *OP to refer to the whole field. This also has the effect
7219 of legitimizing *OP's address for BLKmode, possibly simplifying it. */
7220 *op = adjust_address (*op, BLKmode, 0);
7221 set_mem_size (*op, width / BITS_PER_UNIT);
7223 /* Get references to both ends of the field. We deliberately don't
7224 use the original QImode *OP for FIRST since the new BLKmode one
7225 might have a simpler address. */
7226 first = adjust_address (*op, QImode, 0);
7227 last = adjust_address (*op, QImode, width / BITS_PER_UNIT - 1);
7229 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
7230 correspond to the MSB and RIGHT to the LSB. */
7231 if (TARGET_BIG_ENDIAN)
7232 *left = first, *right = last;
7233 else
7234 *left = last, *right = first;
7236 return true;
7239 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
7240 DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
7241 the operation is the equivalent of:
7243 (set DEST (*_extract SRC WIDTH BITPOS))
7245 Return true on success. */
7247 bool
7248 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
7249 HOST_WIDE_INT bitpos)
7251 rtx left, right, temp;
7253 /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
7254 be a paradoxical word_mode subreg. This is the only case in which
7255 we allow the destination to be larger than the source. */
7256 if (GET_CODE (dest) == SUBREG
7257 && GET_MODE (dest) == DImode
7258 && GET_MODE (SUBREG_REG (dest)) == SImode)
7259 dest = SUBREG_REG (dest);
7261 /* After the above adjustment, the destination must be the same
7262 width as the source. */
7263 if (GET_MODE_BITSIZE (GET_MODE (dest)) != width)
7264 return false;
7266 if (!mips_get_unaligned_mem (&src, width, bitpos, &left, &right))
7267 return false;
7269 temp = gen_reg_rtx (GET_MODE (dest));
7270 if (GET_MODE (dest) == DImode)
7272 emit_insn (gen_mov_ldl (temp, src, left));
7273 emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
7275 else
7277 emit_insn (gen_mov_lwl (temp, src, left));
7278 emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
7280 return true;
7283 /* Try to use left/right stores to expand an "ins" pattern. DEST, WIDTH,
7284 BITPOS and SRC are the operands passed to the expander; the operation
7285 is the equivalent of:
7287 (set (zero_extract DEST WIDTH BITPOS) SRC)
7289 Return true on success. */
7291 bool
7292 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
7293 HOST_WIDE_INT bitpos)
7295 rtx left, right;
7296 enum machine_mode mode;
7298 if (!mips_get_unaligned_mem (&dest, width, bitpos, &left, &right))
7299 return false;
7301 mode = mode_for_size (width, MODE_INT, 0);
7302 src = gen_lowpart (mode, src);
7303 if (mode == DImode)
7305 emit_insn (gen_mov_sdl (dest, src, left));
7306 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7308 else
7310 emit_insn (gen_mov_swl (dest, src, left));
7311 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7313 return true;
7316 /* Return true if X is a MEM with the same size as MODE. */
7318 bool
7319 mips_mem_fits_mode_p (enum machine_mode mode, rtx x)
7321 return (MEM_P (x)
7322 && MEM_SIZE_KNOWN_P (x)
7323 && MEM_SIZE (x) == GET_MODE_SIZE (mode));
7326 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7327 source of an "ext" instruction or the destination of an "ins"
7328 instruction. OP must be a register operand and the following
7329 conditions must hold:
7331 0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7332 0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7333 0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7335 Also reject lengths equal to a word as they are better handled
7336 by the move patterns. */
7338 bool
7339 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7341 if (!ISA_HAS_EXT_INS
7342 || !register_operand (op, VOIDmode)
7343 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7344 return false;
7346 if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7347 return false;
7349 if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7350 return false;
7352 return true;
7355 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7356 operation if MAXLEN is the maxium length of consecutive bits that
7357 can make up MASK. MODE is the mode of the operation. See
7358 mask_low_and_shift_len for the actual definition. */
7360 bool
7361 mask_low_and_shift_p (enum machine_mode mode, rtx mask, rtx shift, int maxlen)
7363 return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7366 /* Return true iff OP1 and OP2 are valid operands together for the
7367 *and<MODE>3 and *and<MODE>3_mips16 patterns. For the cases to consider,
7368 see the table in the comment before the pattern. */
7370 bool
7371 and_operands_ok (enum machine_mode mode, rtx op1, rtx op2)
7373 return (memory_operand (op1, mode)
7374 ? and_load_operand (op2, mode)
7375 : and_reg_operand (op2, mode));
7378 /* The canonical form of a mask-low-and-shift-left operation is
7379 (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7380 cleared. Thus we need to shift MASK to the right before checking if it
7381 is a valid mask value. MODE is the mode of the operation. If true
7382 return the length of the mask, otherwise return -1. */
7385 mask_low_and_shift_len (enum machine_mode mode, rtx mask, rtx shift)
7387 HOST_WIDE_INT shval;
7389 shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7390 return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7393 /* Return true if -msplit-addresses is selected and should be honored.
7395 -msplit-addresses is a half-way house between explicit relocations
7396 and the traditional assembler macros. It can split absolute 32-bit
7397 symbolic constants into a high/lo_sum pair but uses macros for other
7398 sorts of access.
7400 Like explicit relocation support for REL targets, it relies
7401 on GNU extensions in the assembler and the linker.
7403 Although this code should work for -O0, it has traditionally
7404 been treated as an optimization. */
7406 static bool
7407 mips_split_addresses_p (void)
7409 return (TARGET_SPLIT_ADDRESSES
7410 && optimize
7411 && !TARGET_MIPS16
7412 && !flag_pic
7413 && !ABI_HAS_64BIT_SYMBOLS);
7416 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs. */
7418 static void
7419 mips_init_relocs (void)
7421 memset (mips_split_p, '\0', sizeof (mips_split_p));
7422 memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
7423 memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
7424 memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
7425 memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
7427 if (TARGET_MIPS16_PCREL_LOADS)
7428 mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
7429 else
7431 if (ABI_HAS_64BIT_SYMBOLS)
7433 if (TARGET_EXPLICIT_RELOCS)
7435 mips_split_p[SYMBOL_64_HIGH] = true;
7436 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
7437 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
7439 mips_split_p[SYMBOL_64_MID] = true;
7440 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
7441 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
7443 mips_split_p[SYMBOL_64_LOW] = true;
7444 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
7445 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
7447 mips_split_p[SYMBOL_ABSOLUTE] = true;
7448 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7451 else
7453 if (TARGET_EXPLICIT_RELOCS
7454 || mips_split_addresses_p ()
7455 || TARGET_MIPS16)
7457 mips_split_p[SYMBOL_ABSOLUTE] = true;
7458 mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
7459 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7464 if (TARGET_MIPS16)
7466 /* The high part is provided by a pseudo copy of $gp. */
7467 mips_split_p[SYMBOL_GP_RELATIVE] = true;
7468 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
7470 else if (TARGET_EXPLICIT_RELOCS)
7471 /* Small data constants are kept whole until after reload,
7472 then lowered by mips_rewrite_small_data. */
7473 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
7475 if (TARGET_EXPLICIT_RELOCS)
7477 mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
7478 if (TARGET_NEWABI)
7480 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
7481 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
7483 else
7485 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
7486 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
7488 if (TARGET_MIPS16)
7489 /* Expose the use of $28 as soon as possible. */
7490 mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
7492 if (TARGET_XGOT)
7494 /* The HIGH and LO_SUM are matched by special .md patterns. */
7495 mips_split_p[SYMBOL_GOT_DISP] = true;
7497 mips_split_p[SYMBOL_GOTOFF_DISP] = true;
7498 mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
7499 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
7501 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
7502 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
7503 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
7505 else
7507 if (TARGET_NEWABI)
7508 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
7509 else
7510 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
7511 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
7512 if (TARGET_MIPS16)
7513 /* Expose the use of $28 as soon as possible. */
7514 mips_split_p[SYMBOL_GOT_DISP] = true;
7518 if (TARGET_NEWABI)
7520 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
7521 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
7522 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
7525 mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
7526 mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
7528 if (TARGET_MIPS16_PCREL_LOADS)
7530 mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
7531 mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
7533 else
7535 mips_split_p[SYMBOL_DTPREL] = true;
7536 mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
7537 mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
7539 mips_split_p[SYMBOL_TPREL] = true;
7540 mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
7541 mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
7544 mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
7545 mips_lo_relocs[SYMBOL_HALF] = "%half(";
7548 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
7549 in context CONTEXT. RELOCS is the array of relocations to use. */
7551 static void
7552 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
7553 const char **relocs)
7555 enum mips_symbol_type symbol_type;
7556 const char *p;
7558 symbol_type = mips_classify_symbolic_expression (op, context);
7559 gcc_assert (relocs[symbol_type]);
7561 fputs (relocs[symbol_type], file);
7562 output_addr_const (file, mips_strip_unspec_address (op));
7563 for (p = relocs[symbol_type]; *p != 0; p++)
7564 if (*p == '(')
7565 fputc (')', file);
7568 /* Start a new block with the given asm switch enabled. If we need
7569 to print a directive, emit PREFIX before it and SUFFIX after it. */
7571 static void
7572 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
7573 const char *prefix, const char *suffix)
7575 if (asm_switch->nesting_level == 0)
7576 fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
7577 asm_switch->nesting_level++;
7580 /* Likewise, but end a block. */
7582 static void
7583 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
7584 const char *prefix, const char *suffix)
7586 gcc_assert (asm_switch->nesting_level);
7587 asm_switch->nesting_level--;
7588 if (asm_switch->nesting_level == 0)
7589 fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
7592 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
7593 that either print a complete line or print nothing. */
7595 void
7596 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
7598 mips_push_asm_switch_1 (asm_switch, "\t", "\n");
7601 void
7602 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
7604 mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
7607 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
7608 The punctuation characters are:
7610 '(' Start a nested ".set noreorder" block.
7611 ')' End a nested ".set noreorder" block.
7612 '[' Start a nested ".set noat" block.
7613 ']' End a nested ".set noat" block.
7614 '<' Start a nested ".set nomacro" block.
7615 '>' End a nested ".set nomacro" block.
7616 '*' Behave like %(%< if generating a delayed-branch sequence.
7617 '#' Print a nop if in a ".set noreorder" block.
7618 '/' Like '#', but do nothing within a delayed-branch sequence.
7619 '?' Print "l" if mips_branch_likely is true
7620 '~' Print a nop if mips_branch_likely is true
7621 '.' Print the name of the register with a hard-wired zero (zero or $0).
7622 '@' Print the name of the assembler temporary register (at or $1).
7623 '^' Print the name of the pic call-through register (t9 or $25).
7624 '+' Print the name of the gp register (usually gp or $28).
7625 '$' Print the name of the stack pointer register (sp or $29).
7627 See also mips_init_print_operand_pucnt. */
7629 static void
7630 mips_print_operand_punctuation (FILE *file, int ch)
7632 switch (ch)
7634 case '(':
7635 mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
7636 break;
7638 case ')':
7639 mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
7640 break;
7642 case '[':
7643 mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
7644 break;
7646 case ']':
7647 mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
7648 break;
7650 case '<':
7651 mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
7652 break;
7654 case '>':
7655 mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
7656 break;
7658 case '*':
7659 if (final_sequence != 0)
7661 mips_print_operand_punctuation (file, '(');
7662 mips_print_operand_punctuation (file, '<');
7664 break;
7666 case '#':
7667 if (mips_noreorder.nesting_level > 0)
7668 fputs ("\n\tnop", file);
7669 break;
7671 case '/':
7672 /* Print an extra newline so that the delayed insn is separated
7673 from the following ones. This looks neater and is consistent
7674 with non-nop delayed sequences. */
7675 if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
7676 fputs ("\n\tnop\n", file);
7677 break;
7679 case '?':
7680 if (mips_branch_likely)
7681 putc ('l', file);
7682 break;
7684 case '~':
7685 if (mips_branch_likely)
7686 fputs ("\n\tnop", file);
7687 break;
7689 case '.':
7690 fputs (reg_names[GP_REG_FIRST + 0], file);
7691 break;
7693 case '@':
7694 fputs (reg_names[AT_REGNUM], file);
7695 break;
7697 case '^':
7698 fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
7699 break;
7701 case '+':
7702 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
7703 break;
7705 case '$':
7706 fputs (reg_names[STACK_POINTER_REGNUM], file);
7707 break;
7709 default:
7710 gcc_unreachable ();
7711 break;
7715 /* Initialize mips_print_operand_punct. */
7717 static void
7718 mips_init_print_operand_punct (void)
7720 const char *p;
7722 for (p = "()[]<>*#/?~.@^+$"; *p; p++)
7723 mips_print_operand_punct[(unsigned char) *p] = true;
7726 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
7727 associated with condition CODE. Print the condition part of the
7728 opcode to FILE. */
7730 static void
7731 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
7733 switch (code)
7735 case EQ:
7736 case NE:
7737 case GT:
7738 case GE:
7739 case LT:
7740 case LE:
7741 case GTU:
7742 case GEU:
7743 case LTU:
7744 case LEU:
7745 /* Conveniently, the MIPS names for these conditions are the same
7746 as their RTL equivalents. */
7747 fputs (GET_RTX_NAME (code), file);
7748 break;
7750 default:
7751 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
7752 break;
7756 /* Likewise floating-point branches. */
7758 static void
7759 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
7761 switch (code)
7763 case EQ:
7764 fputs ("c1f", file);
7765 break;
7767 case NE:
7768 fputs ("c1t", file);
7769 break;
7771 default:
7772 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
7773 break;
7777 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
7779 static bool
7780 mips_print_operand_punct_valid_p (unsigned char code)
7782 return mips_print_operand_punct[code];
7785 /* Implement TARGET_PRINT_OPERAND. The MIPS-specific operand codes are:
7787 'X' Print CONST_INT OP in hexadecimal format.
7788 'x' Print the low 16 bits of CONST_INT OP in hexadecimal format.
7789 'd' Print CONST_INT OP in decimal.
7790 'm' Print one less than CONST_INT OP in decimal.
7791 'h' Print the high-part relocation associated with OP, after stripping
7792 any outermost HIGH.
7793 'R' Print the low-part relocation associated with OP.
7794 'C' Print the integer branch condition for comparison OP.
7795 'N' Print the inverse of the integer branch condition for comparison OP.
7796 'F' Print the FPU branch condition for comparison OP.
7797 'W' Print the inverse of the FPU branch condition for comparison OP.
7798 'T' Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
7799 'z' for (eq:?I ...), 'n' for (ne:?I ...).
7800 't' Like 'T', but with the EQ/NE cases reversed
7801 'Y' Print mips_fp_conditions[INTVAL (OP)]
7802 'Z' Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
7803 'q' Print a DSP accumulator register.
7804 'D' Print the second part of a double-word register or memory operand.
7805 'L' Print the low-order register in a double-word register operand.
7806 'M' Print high-order register in a double-word register operand.
7807 'z' Print $0 if OP is zero, otherwise print OP normally. */
7809 static void
7810 mips_print_operand (FILE *file, rtx op, int letter)
7812 enum rtx_code code;
7814 if (mips_print_operand_punct_valid_p (letter))
7816 mips_print_operand_punctuation (file, letter);
7817 return;
7820 gcc_assert (op);
7821 code = GET_CODE (op);
7823 switch (letter)
7825 case 'X':
7826 if (CONST_INT_P (op))
7827 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
7828 else
7829 output_operand_lossage ("invalid use of '%%%c'", letter);
7830 break;
7832 case 'x':
7833 if (CONST_INT_P (op))
7834 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
7835 else
7836 output_operand_lossage ("invalid use of '%%%c'", letter);
7837 break;
7839 case 'd':
7840 if (CONST_INT_P (op))
7841 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
7842 else
7843 output_operand_lossage ("invalid use of '%%%c'", letter);
7844 break;
7846 case 'm':
7847 if (CONST_INT_P (op))
7848 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
7849 else
7850 output_operand_lossage ("invalid use of '%%%c'", letter);
7851 break;
7853 case 'h':
7854 if (code == HIGH)
7855 op = XEXP (op, 0);
7856 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
7857 break;
7859 case 'R':
7860 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
7861 break;
7863 case 'C':
7864 mips_print_int_branch_condition (file, code, letter);
7865 break;
7867 case 'N':
7868 mips_print_int_branch_condition (file, reverse_condition (code), letter);
7869 break;
7871 case 'F':
7872 mips_print_float_branch_condition (file, code, letter);
7873 break;
7875 case 'W':
7876 mips_print_float_branch_condition (file, reverse_condition (code),
7877 letter);
7878 break;
7880 case 'T':
7881 case 't':
7883 int truth = (code == NE) == (letter == 'T');
7884 fputc ("zfnt"[truth * 2 + (GET_MODE (op) == CCmode)], file);
7886 break;
7888 case 'Y':
7889 if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
7890 fputs (mips_fp_conditions[UINTVAL (op)], file);
7891 else
7892 output_operand_lossage ("'%%%c' is not a valid operand prefix",
7893 letter);
7894 break;
7896 case 'Z':
7897 if (ISA_HAS_8CC)
7899 mips_print_operand (file, op, 0);
7900 fputc (',', file);
7902 break;
7904 case 'q':
7905 if (code == REG && MD_REG_P (REGNO (op)))
7906 fprintf (file, "$ac0");
7907 else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
7908 fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
7909 else
7910 output_operand_lossage ("invalid use of '%%%c'", letter);
7911 break;
7913 default:
7914 switch (code)
7916 case REG:
7918 unsigned int regno = REGNO (op);
7919 if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
7920 || (letter == 'L' && TARGET_BIG_ENDIAN)
7921 || letter == 'D')
7922 regno++;
7923 else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
7924 output_operand_lossage ("invalid use of '%%%c'", letter);
7925 /* We need to print $0 .. $31 for COP0 registers. */
7926 if (COP0_REG_P (regno))
7927 fprintf (file, "$%s", &reg_names[regno][4]);
7928 else
7929 fprintf (file, "%s", reg_names[regno]);
7931 break;
7933 case MEM:
7934 if (letter == 'D')
7935 output_address (plus_constant (Pmode, XEXP (op, 0), 4));
7936 else if (letter && letter != 'z')
7937 output_operand_lossage ("invalid use of '%%%c'", letter);
7938 else
7939 output_address (XEXP (op, 0));
7940 break;
7942 default:
7943 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
7944 fputs (reg_names[GP_REG_FIRST], file);
7945 else if (letter && letter != 'z')
7946 output_operand_lossage ("invalid use of '%%%c'", letter);
7947 else if (CONST_GP_P (op))
7948 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
7949 else
7950 output_addr_const (file, mips_strip_unspec_address (op));
7951 break;
7956 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
7958 static void
7959 mips_print_operand_address (FILE *file, rtx x)
7961 struct mips_address_info addr;
7963 if (mips_classify_address (&addr, x, word_mode, true))
7964 switch (addr.type)
7966 case ADDRESS_REG:
7967 mips_print_operand (file, addr.offset, 0);
7968 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
7969 return;
7971 case ADDRESS_LO_SUM:
7972 mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
7973 mips_lo_relocs);
7974 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
7975 return;
7977 case ADDRESS_CONST_INT:
7978 output_addr_const (file, x);
7979 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
7980 return;
7982 case ADDRESS_SYMBOLIC:
7983 output_addr_const (file, mips_strip_unspec_address (x));
7984 return;
7986 gcc_unreachable ();
7989 /* Implement TARGET_ENCODE_SECTION_INFO. */
7991 static void
7992 mips_encode_section_info (tree decl, rtx rtl, int first)
7994 default_encode_section_info (decl, rtl, first);
7996 if (TREE_CODE (decl) == FUNCTION_DECL)
7998 rtx symbol = XEXP (rtl, 0);
7999 tree type = TREE_TYPE (decl);
8001 /* Encode whether the symbol is short or long. */
8002 if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
8003 || mips_far_type_p (type))
8004 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
8008 /* Implement TARGET_SELECT_RTX_SECTION. */
8010 static section *
8011 mips_select_rtx_section (enum machine_mode mode, rtx x,
8012 unsigned HOST_WIDE_INT align)
8014 /* ??? Consider using mergeable small data sections. */
8015 if (mips_rtx_constant_in_small_data_p (mode))
8016 return get_named_section (NULL, ".sdata", 0);
8018 return default_elf_select_rtx_section (mode, x, align);
8021 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
8023 The complication here is that, with the combination TARGET_ABICALLS
8024 && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
8025 absolute addresses, and should therefore not be included in the
8026 read-only part of a DSO. Handle such cases by selecting a normal
8027 data section instead of a read-only one. The logic apes that in
8028 default_function_rodata_section. */
8030 static section *
8031 mips_function_rodata_section (tree decl)
8033 if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
8034 return default_function_rodata_section (decl);
8036 if (decl && DECL_SECTION_NAME (decl))
8038 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8039 if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
8041 char *rname = ASTRDUP (name);
8042 rname[14] = 'd';
8043 return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
8045 else if (flag_function_sections
8046 && flag_data_sections
8047 && strncmp (name, ".text.", 6) == 0)
8049 char *rname = ASTRDUP (name);
8050 memcpy (rname + 1, "data", 4);
8051 return get_section (rname, SECTION_WRITE, decl);
8054 return data_section;
8057 /* Implement TARGET_IN_SMALL_DATA_P. */
8059 static bool
8060 mips_in_small_data_p (const_tree decl)
8062 unsigned HOST_WIDE_INT size;
8064 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
8065 return false;
8067 /* We don't yet generate small-data references for -mabicalls
8068 or VxWorks RTP code. See the related -G handling in
8069 mips_option_override. */
8070 if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
8071 return false;
8073 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
8075 const char *name;
8077 /* Reject anything that isn't in a known small-data section. */
8078 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8079 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
8080 return false;
8082 /* If a symbol is defined externally, the assembler will use the
8083 usual -G rules when deciding how to implement macros. */
8084 if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
8085 return true;
8087 else if (TARGET_EMBEDDED_DATA)
8089 /* Don't put constants into the small data section: we want them
8090 to be in ROM rather than RAM. */
8091 if (TREE_CODE (decl) != VAR_DECL)
8092 return false;
8094 if (TREE_READONLY (decl)
8095 && !TREE_SIDE_EFFECTS (decl)
8096 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
8097 return false;
8100 /* Enforce -mlocal-sdata. */
8101 if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
8102 return false;
8104 /* Enforce -mextern-sdata. */
8105 if (!TARGET_EXTERN_SDATA && DECL_P (decl))
8107 if (DECL_EXTERNAL (decl))
8108 return false;
8109 if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
8110 return false;
8113 /* We have traditionally not treated zero-sized objects as small data,
8114 so this is now effectively part of the ABI. */
8115 size = int_size_in_bytes (TREE_TYPE (decl));
8116 return size > 0 && size <= mips_small_data_threshold;
8119 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P. We don't want to use
8120 anchors for small data: the GP register acts as an anchor in that
8121 case. We also don't want to use them for PC-relative accesses,
8122 where the PC acts as an anchor. */
8124 static bool
8125 mips_use_anchors_for_symbol_p (const_rtx symbol)
8127 switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
8129 case SYMBOL_PC_RELATIVE:
8130 case SYMBOL_GP_RELATIVE:
8131 return false;
8133 default:
8134 return default_use_anchors_for_symbol_p (symbol);
8138 /* The MIPS debug format wants all automatic variables and arguments
8139 to be in terms of the virtual frame pointer (stack pointer before
8140 any adjustment in the function), while the MIPS 3.0 linker wants
8141 the frame pointer to be the stack pointer after the initial
8142 adjustment. So, we do the adjustment here. The arg pointer (which
8143 is eliminated) points to the virtual frame pointer, while the frame
8144 pointer (which may be eliminated) points to the stack pointer after
8145 the initial adjustments. */
8147 HOST_WIDE_INT
8148 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
8150 rtx offset2 = const0_rtx;
8151 rtx reg = eliminate_constant_term (addr, &offset2);
8153 if (offset == 0)
8154 offset = INTVAL (offset2);
8156 if (reg == stack_pointer_rtx
8157 || reg == frame_pointer_rtx
8158 || reg == hard_frame_pointer_rtx)
8160 offset -= cfun->machine->frame.total_size;
8161 if (reg == hard_frame_pointer_rtx)
8162 offset += cfun->machine->frame.hard_frame_pointer_offset;
8165 return offset;
8168 /* Implement ASM_OUTPUT_EXTERNAL. */
8170 void
8171 mips_output_external (FILE *file, tree decl, const char *name)
8173 default_elf_asm_output_external (file, decl, name);
8175 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8176 set in order to avoid putting out names that are never really
8177 used. */
8178 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
8180 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
8182 /* When using assembler macros, emit .extern directives for
8183 all small-data externs so that the assembler knows how
8184 big they are.
8186 In most cases it would be safe (though pointless) to emit
8187 .externs for other symbols too. One exception is when an
8188 object is within the -G limit but declared by the user to
8189 be in a section other than .sbss or .sdata. */
8190 fputs ("\t.extern\t", file);
8191 assemble_name (file, name);
8192 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
8193 int_size_in_bytes (TREE_TYPE (decl)));
8198 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8200 static void
8201 mips_output_filename (FILE *stream, const char *name)
8203 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
8204 directives. */
8205 if (write_symbols == DWARF2_DEBUG)
8206 return;
8207 else if (mips_output_filename_first_time)
8209 mips_output_filename_first_time = 0;
8210 num_source_filenames += 1;
8211 current_function_file = name;
8212 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8213 output_quoted_string (stream, name);
8214 putc ('\n', stream);
8216 /* If we are emitting stabs, let dbxout.c handle this (except for
8217 the mips_output_filename_first_time case). */
8218 else if (write_symbols == DBX_DEBUG)
8219 return;
8220 else if (name != current_function_file
8221 && strcmp (name, current_function_file) != 0)
8223 num_source_filenames += 1;
8224 current_function_file = name;
8225 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8226 output_quoted_string (stream, name);
8227 putc ('\n', stream);
8231 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL. */
8233 static void ATTRIBUTE_UNUSED
8234 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
8236 switch (size)
8238 case 4:
8239 fputs ("\t.dtprelword\t", file);
8240 break;
8242 case 8:
8243 fputs ("\t.dtpreldword\t", file);
8244 break;
8246 default:
8247 gcc_unreachable ();
8249 output_addr_const (file, x);
8250 fputs ("+0x8000", file);
8253 /* Implement TARGET_DWARF_REGISTER_SPAN. */
8255 static rtx
8256 mips_dwarf_register_span (rtx reg)
8258 rtx high, low;
8259 enum machine_mode mode;
8261 /* By default, GCC maps increasing register numbers to increasing
8262 memory locations, but paired FPRs are always little-endian,
8263 regardless of the prevailing endianness. */
8264 mode = GET_MODE (reg);
8265 if (FP_REG_P (REGNO (reg))
8266 && TARGET_BIG_ENDIAN
8267 && MAX_FPRS_PER_FMT > 1
8268 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8270 gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
8271 high = mips_subword (reg, true);
8272 low = mips_subword (reg, false);
8273 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
8276 return NULL_RTX;
8279 /* Implement ASM_OUTPUT_ASCII. */
8281 void
8282 mips_output_ascii (FILE *stream, const char *string, size_t len)
8284 size_t i;
8285 int cur_pos;
8287 cur_pos = 17;
8288 fprintf (stream, "\t.ascii\t\"");
8289 for (i = 0; i < len; i++)
8291 int c;
8293 c = (unsigned char) string[i];
8294 if (ISPRINT (c))
8296 if (c == '\\' || c == '\"')
8298 putc ('\\', stream);
8299 cur_pos++;
8301 putc (c, stream);
8302 cur_pos++;
8304 else
8306 fprintf (stream, "\\%03o", c);
8307 cur_pos += 4;
8310 if (cur_pos > 72 && i+1 < len)
8312 cur_pos = 17;
8313 fprintf (stream, "\"\n\t.ascii\t\"");
8316 fprintf (stream, "\"\n");
8319 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
8320 Update *ADDR with the operand that should be printed. */
8322 const char *
8323 mips_output_tls_reloc_directive (rtx *addr)
8325 enum mips_symbol_type type;
8327 type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
8328 *addr = mips_strip_unspec_address (*addr);
8329 switch (type)
8331 case SYMBOL_DTPREL:
8332 return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
8334 case SYMBOL_TPREL:
8335 return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
8337 default:
8338 gcc_unreachable ();
8342 /* Emit either a label, .comm, or .lcomm directive. When using assembler
8343 macros, mark the symbol as written so that mips_asm_output_external
8344 won't emit an .extern for it. STREAM is the output file, NAME is the
8345 name of the symbol, INIT_STRING is the string that should be written
8346 before the symbol and FINAL_STRING is the string that should be
8347 written after it. FINAL_STRING is a printf format that consumes the
8348 remaining arguments. */
8350 void
8351 mips_declare_object (FILE *stream, const char *name, const char *init_string,
8352 const char *final_string, ...)
8354 va_list ap;
8356 fputs (init_string, stream);
8357 assemble_name (stream, name);
8358 va_start (ap, final_string);
8359 vfprintf (stream, final_string, ap);
8360 va_end (ap);
8362 if (!TARGET_EXPLICIT_RELOCS)
8364 tree name_tree = get_identifier (name);
8365 TREE_ASM_WRITTEN (name_tree) = 1;
8369 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
8370 NAME is the name of the object and ALIGN is the required alignment
8371 in bytes. TAKES_ALIGNMENT_P is true if the directive takes a third
8372 alignment argument. */
8374 void
8375 mips_declare_common_object (FILE *stream, const char *name,
8376 const char *init_string,
8377 unsigned HOST_WIDE_INT size,
8378 unsigned int align, bool takes_alignment_p)
8380 if (!takes_alignment_p)
8382 size += (align / BITS_PER_UNIT) - 1;
8383 size -= size % (align / BITS_PER_UNIT);
8384 mips_declare_object (stream, name, init_string,
8385 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
8387 else
8388 mips_declare_object (stream, name, init_string,
8389 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
8390 size, align / BITS_PER_UNIT);
8393 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as the
8394 elfos.h version, but we also need to handle -muninit-const-in-rodata. */
8396 void
8397 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
8398 unsigned HOST_WIDE_INT size,
8399 unsigned int align)
8401 /* If the target wants uninitialized const declarations in
8402 .rdata then don't put them in .comm. */
8403 if (TARGET_EMBEDDED_DATA
8404 && TARGET_UNINIT_CONST_IN_RODATA
8405 && TREE_CODE (decl) == VAR_DECL
8406 && TREE_READONLY (decl)
8407 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
8409 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
8410 targetm.asm_out.globalize_label (stream, name);
8412 switch_to_section (readonly_data_section);
8413 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
8414 mips_declare_object (stream, name, "",
8415 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
8416 size);
8418 else
8419 mips_declare_common_object (stream, name, "\n\t.comm\t",
8420 size, align, true);
8423 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
8424 extern int size_directive_output;
8426 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
8427 definitions except that it uses mips_declare_object to emit the label. */
8429 void
8430 mips_declare_object_name (FILE *stream, const char *name,
8431 tree decl ATTRIBUTE_UNUSED)
8433 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
8434 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
8435 #endif
8437 size_directive_output = 0;
8438 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
8440 HOST_WIDE_INT size;
8442 size_directive_output = 1;
8443 size = int_size_in_bytes (TREE_TYPE (decl));
8444 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8447 mips_declare_object (stream, name, "", ":\n");
8450 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
8452 void
8453 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
8455 const char *name;
8457 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8458 if (!flag_inhibit_size_directive
8459 && DECL_SIZE (decl) != 0
8460 && !at_end
8461 && top_level
8462 && DECL_INITIAL (decl) == error_mark_node
8463 && !size_directive_output)
8465 HOST_WIDE_INT size;
8467 size_directive_output = 1;
8468 size = int_size_in_bytes (TREE_TYPE (decl));
8469 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8472 #endif
8474 /* Return the FOO in the name of the ".mdebug.FOO" section associated
8475 with the current ABI. */
8477 static const char *
8478 mips_mdebug_abi_name (void)
8480 switch (mips_abi)
8482 case ABI_32:
8483 return "abi32";
8484 case ABI_O64:
8485 return "abiO64";
8486 case ABI_N32:
8487 return "abiN32";
8488 case ABI_64:
8489 return "abi64";
8490 case ABI_EABI:
8491 return TARGET_64BIT ? "eabi64" : "eabi32";
8492 default:
8493 gcc_unreachable ();
8497 /* Implement TARGET_ASM_FILE_START. */
8499 static void
8500 mips_file_start (void)
8502 default_file_start ();
8504 /* Generate a special section to describe the ABI switches used to
8505 produce the resultant binary. */
8507 /* Record the ABI itself. Modern versions of binutils encode
8508 this information in the ELF header flags, but GDB needs the
8509 information in order to correctly debug binaries produced by
8510 older binutils. See the function mips_gdbarch_init in
8511 gdb/mips-tdep.c. */
8512 fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
8513 mips_mdebug_abi_name ());
8515 /* There is no ELF header flag to distinguish long32 forms of the
8516 EABI from long64 forms. Emit a special section to help tools
8517 such as GDB. Do the same for o64, which is sometimes used with
8518 -mlong64. */
8519 if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
8520 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
8521 "\t.previous\n", TARGET_LONG64 ? 64 : 32);
8523 #ifdef HAVE_AS_GNU_ATTRIBUTE
8525 int attr;
8527 /* No floating-point operations, -mno-float. */
8528 if (TARGET_NO_FLOAT)
8529 attr = 0;
8530 /* Soft-float code, -msoft-float. */
8531 else if (!TARGET_HARD_FLOAT_ABI)
8532 attr = 3;
8533 /* Single-float code, -msingle-float. */
8534 else if (!TARGET_DOUBLE_FLOAT)
8535 attr = 2;
8536 /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64. */
8537 else if (!TARGET_64BIT && TARGET_FLOAT64)
8538 attr = 4;
8539 /* Regular FP code, FP regs same size as GP regs, -mdouble-float. */
8540 else
8541 attr = 1;
8543 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
8545 #endif
8547 /* If TARGET_ABICALLS, tell GAS to generate -KPIC code. */
8548 if (TARGET_ABICALLS)
8550 fprintf (asm_out_file, "\t.abicalls\n");
8551 if (TARGET_ABICALLS_PIC0)
8552 fprintf (asm_out_file, "\t.option\tpic0\n");
8555 if (flag_verbose_asm)
8556 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
8557 ASM_COMMENT_START,
8558 mips_small_data_threshold, mips_arch_info->name, mips_isa);
8561 /* Implement TARGET_ASM_CODE_END. */
8563 static void
8564 mips_code_end (void)
8566 if (mips_need_mips16_rdhwr_p)
8567 mips_output_mips16_rdhwr ();
8570 /* Make the last instruction frame-related and note that it performs
8571 the operation described by FRAME_PATTERN. */
8573 static void
8574 mips_set_frame_expr (rtx frame_pattern)
8576 rtx insn;
8578 insn = get_last_insn ();
8579 RTX_FRAME_RELATED_P (insn) = 1;
8580 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8581 frame_pattern,
8582 REG_NOTES (insn));
8585 /* Return a frame-related rtx that stores REG at MEM.
8586 REG must be a single register. */
8588 static rtx
8589 mips_frame_set (rtx mem, rtx reg)
8591 rtx set;
8593 set = gen_rtx_SET (VOIDmode, mem, reg);
8594 RTX_FRAME_RELATED_P (set) = 1;
8596 return set;
8599 /* Record that the epilogue has restored call-saved register REG. */
8601 static void
8602 mips_add_cfa_restore (rtx reg)
8604 mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
8605 mips_epilogue.cfa_restores);
8608 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
8609 mips16e_s2_s8_regs[X], it must also save the registers in indexes
8610 X + 1 onwards. Likewise mips16e_a0_a3_regs. */
8611 static const unsigned char mips16e_s2_s8_regs[] = {
8612 30, 23, 22, 21, 20, 19, 18
8614 static const unsigned char mips16e_a0_a3_regs[] = {
8615 4, 5, 6, 7
8618 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
8619 ordered from the uppermost in memory to the lowest in memory. */
8620 static const unsigned char mips16e_save_restore_regs[] = {
8621 31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
8624 /* Return the index of the lowest X in the range [0, SIZE) for which
8625 bit REGS[X] is set in MASK. Return SIZE if there is no such X. */
8627 static unsigned int
8628 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
8629 unsigned int size)
8631 unsigned int i;
8633 for (i = 0; i < size; i++)
8634 if (BITSET_P (mask, regs[i]))
8635 break;
8637 return i;
8640 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
8641 is the number of set bits. If *MASK_PTR contains REGS[X] for some X
8642 in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
8643 is true for all indexes (X, SIZE). */
8645 static void
8646 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
8647 unsigned int size, unsigned int *num_regs_ptr)
8649 unsigned int i;
8651 i = mips16e_find_first_register (*mask_ptr, regs, size);
8652 for (i++; i < size; i++)
8653 if (!BITSET_P (*mask_ptr, regs[i]))
8655 *num_regs_ptr += 1;
8656 *mask_ptr |= 1 << regs[i];
8660 /* Return a simplified form of X using the register values in REG_VALUES.
8661 REG_VALUES[R] is the last value assigned to hard register R, or null
8662 if R has not been modified.
8664 This function is rather limited, but is good enough for our purposes. */
8666 static rtx
8667 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
8669 x = avoid_constant_pool_reference (x);
8671 if (UNARY_P (x))
8673 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
8674 return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
8675 x0, GET_MODE (XEXP (x, 0)));
8678 if (ARITHMETIC_P (x))
8680 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
8681 rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
8682 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
8685 if (REG_P (x)
8686 && reg_values[REGNO (x)]
8687 && !rtx_unstable_p (reg_values[REGNO (x)]))
8688 return reg_values[REGNO (x)];
8690 return x;
8693 /* Return true if (set DEST SRC) stores an argument register into its
8694 caller-allocated save slot, storing the number of that argument
8695 register in *REGNO_PTR if so. REG_VALUES is as for
8696 mips16e_collect_propagate_value. */
8698 static bool
8699 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
8700 unsigned int *regno_ptr)
8702 unsigned int argno, regno;
8703 HOST_WIDE_INT offset, required_offset;
8704 rtx addr, base;
8706 /* Check that this is a word-mode store. */
8707 if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
8708 return false;
8710 /* Check that the register being saved is an unmodified argument
8711 register. */
8712 regno = REGNO (src);
8713 if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
8714 return false;
8715 argno = regno - GP_ARG_FIRST;
8717 /* Check whether the address is an appropriate stack-pointer or
8718 frame-pointer access. */
8719 addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
8720 mips_split_plus (addr, &base, &offset);
8721 required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
8722 if (base == hard_frame_pointer_rtx)
8723 required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
8724 else if (base != stack_pointer_rtx)
8725 return false;
8726 if (offset != required_offset)
8727 return false;
8729 *regno_ptr = regno;
8730 return true;
8733 /* A subroutine of mips_expand_prologue, called only when generating
8734 MIPS16e SAVE instructions. Search the start of the function for any
8735 instructions that save argument registers into their caller-allocated
8736 save slots. Delete such instructions and return a value N such that
8737 saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
8738 instructions redundant. */
8740 static unsigned int
8741 mips16e_collect_argument_saves (void)
8743 rtx reg_values[FIRST_PSEUDO_REGISTER];
8744 rtx insn, next, set, dest, src;
8745 unsigned int nargs, regno;
8747 push_topmost_sequence ();
8748 nargs = 0;
8749 memset (reg_values, 0, sizeof (reg_values));
8750 for (insn = get_insns (); insn; insn = next)
8752 next = NEXT_INSN (insn);
8753 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
8754 continue;
8756 if (!INSN_P (insn))
8757 break;
8759 set = PATTERN (insn);
8760 if (GET_CODE (set) != SET)
8761 break;
8763 dest = SET_DEST (set);
8764 src = SET_SRC (set);
8765 if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
8767 if (!BITSET_P (cfun->machine->frame.mask, regno))
8769 delete_insn (insn);
8770 nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
8773 else if (REG_P (dest) && GET_MODE (dest) == word_mode)
8774 reg_values[REGNO (dest)]
8775 = mips16e_collect_propagate_value (src, reg_values);
8776 else
8777 break;
8779 pop_topmost_sequence ();
8781 return nargs;
8784 /* Return a move between register REGNO and memory location SP + OFFSET.
8785 REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
8786 Make the move a load if RESTORE_P, otherwise make it a store. */
8788 static rtx
8789 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
8790 HOST_WIDE_INT offset, unsigned int regno)
8792 rtx reg, mem;
8794 mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
8795 offset));
8796 reg = gen_rtx_REG (SImode, regno);
8797 if (restore_p)
8799 mips_add_cfa_restore (reg);
8800 return gen_rtx_SET (VOIDmode, reg, mem);
8802 if (reg_parm_p)
8803 return gen_rtx_SET (VOIDmode, mem, reg);
8804 return mips_frame_set (mem, reg);
8807 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
8808 The instruction must:
8810 - Allocate or deallocate SIZE bytes in total; SIZE is known
8811 to be nonzero.
8813 - Save or restore as many registers in *MASK_PTR as possible.
8814 The instruction saves the first registers at the top of the
8815 allocated area, with the other registers below it.
8817 - Save NARGS argument registers above the allocated area.
8819 (NARGS is always zero if RESTORE_P.)
8821 The SAVE and RESTORE instructions cannot save and restore all general
8822 registers, so there may be some registers left over for the caller to
8823 handle. Destructively modify *MASK_PTR so that it contains the registers
8824 that still need to be saved or restored. The caller can save these
8825 registers in the memory immediately below *OFFSET_PTR, which is a
8826 byte offset from the bottom of the allocated stack area. */
8828 static rtx
8829 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
8830 HOST_WIDE_INT *offset_ptr, unsigned int nargs,
8831 HOST_WIDE_INT size)
8833 rtx pattern, set;
8834 HOST_WIDE_INT offset, top_offset;
8835 unsigned int i, regno;
8836 int n;
8838 gcc_assert (cfun->machine->frame.num_fp == 0);
8840 /* Calculate the number of elements in the PARALLEL. We need one element
8841 for the stack adjustment, one for each argument register save, and one
8842 for each additional register move. */
8843 n = 1 + nargs;
8844 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
8845 if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
8846 n++;
8848 /* Create the final PARALLEL. */
8849 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
8850 n = 0;
8852 /* Add the stack pointer adjustment. */
8853 set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8854 plus_constant (Pmode, stack_pointer_rtx,
8855 restore_p ? size : -size));
8856 RTX_FRAME_RELATED_P (set) = 1;
8857 XVECEXP (pattern, 0, n++) = set;
8859 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
8860 top_offset = restore_p ? size : 0;
8862 /* Save the arguments. */
8863 for (i = 0; i < nargs; i++)
8865 offset = top_offset + i * UNITS_PER_WORD;
8866 set = mips16e_save_restore_reg (restore_p, true, offset,
8867 GP_ARG_FIRST + i);
8868 XVECEXP (pattern, 0, n++) = set;
8871 /* Then fill in the other register moves. */
8872 offset = top_offset;
8873 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
8875 regno = mips16e_save_restore_regs[i];
8876 if (BITSET_P (*mask_ptr, regno))
8878 offset -= UNITS_PER_WORD;
8879 set = mips16e_save_restore_reg (restore_p, false, offset, regno);
8880 XVECEXP (pattern, 0, n++) = set;
8881 *mask_ptr &= ~(1 << regno);
8885 /* Tell the caller what offset it should use for the remaining registers. */
8886 *offset_ptr = size + (offset - top_offset);
8888 gcc_assert (n == XVECLEN (pattern, 0));
8890 return pattern;
8893 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
8894 pointer. Return true if PATTERN matches the kind of instruction
8895 generated by mips16e_build_save_restore. If INFO is nonnull,
8896 initialize it when returning true. */
8898 bool
8899 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
8900 struct mips16e_save_restore_info *info)
8902 unsigned int i, nargs, mask, extra;
8903 HOST_WIDE_INT top_offset, save_offset, offset;
8904 rtx set, reg, mem, base;
8905 int n;
8907 if (!GENERATE_MIPS16E_SAVE_RESTORE)
8908 return false;
8910 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
8911 top_offset = adjust > 0 ? adjust : 0;
8913 /* Interpret all other members of the PARALLEL. */
8914 save_offset = top_offset - UNITS_PER_WORD;
8915 mask = 0;
8916 nargs = 0;
8917 i = 0;
8918 for (n = 1; n < XVECLEN (pattern, 0); n++)
8920 /* Check that we have a SET. */
8921 set = XVECEXP (pattern, 0, n);
8922 if (GET_CODE (set) != SET)
8923 return false;
8925 /* Check that the SET is a load (if restoring) or a store
8926 (if saving). */
8927 mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
8928 if (!MEM_P (mem))
8929 return false;
8931 /* Check that the address is the sum of the stack pointer and a
8932 possibly-zero constant offset. */
8933 mips_split_plus (XEXP (mem, 0), &base, &offset);
8934 if (base != stack_pointer_rtx)
8935 return false;
8937 /* Check that SET's other operand is a register. */
8938 reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
8939 if (!REG_P (reg))
8940 return false;
8942 /* Check for argument saves. */
8943 if (offset == top_offset + nargs * UNITS_PER_WORD
8944 && REGNO (reg) == GP_ARG_FIRST + nargs)
8945 nargs++;
8946 else if (offset == save_offset)
8948 while (mips16e_save_restore_regs[i++] != REGNO (reg))
8949 if (i == ARRAY_SIZE (mips16e_save_restore_regs))
8950 return false;
8952 mask |= 1 << REGNO (reg);
8953 save_offset -= UNITS_PER_WORD;
8955 else
8956 return false;
8959 /* Check that the restrictions on register ranges are met. */
8960 extra = 0;
8961 mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
8962 ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
8963 mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
8964 ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
8965 if (extra != 0)
8966 return false;
8968 /* Make sure that the topmost argument register is not saved twice.
8969 The checks above ensure that the same is then true for the other
8970 argument registers. */
8971 if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
8972 return false;
8974 /* Pass back information, if requested. */
8975 if (info)
8977 info->nargs = nargs;
8978 info->mask = mask;
8979 info->size = (adjust > 0 ? adjust : -adjust);
8982 return true;
8985 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
8986 for the register range [MIN_REG, MAX_REG]. Return a pointer to
8987 the null terminator. */
8989 static char *
8990 mips16e_add_register_range (char *s, unsigned int min_reg,
8991 unsigned int max_reg)
8993 if (min_reg != max_reg)
8994 s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
8995 else
8996 s += sprintf (s, ",%s", reg_names[min_reg]);
8997 return s;
9000 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
9001 PATTERN and ADJUST are as for mips16e_save_restore_pattern_p. */
9003 const char *
9004 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
9006 static char buffer[300];
9008 struct mips16e_save_restore_info info;
9009 unsigned int i, end;
9010 char *s;
9012 /* Parse the pattern. */
9013 if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
9014 gcc_unreachable ();
9016 /* Add the mnemonic. */
9017 s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
9018 s += strlen (s);
9020 /* Save the arguments. */
9021 if (info.nargs > 1)
9022 s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
9023 reg_names[GP_ARG_FIRST + info.nargs - 1]);
9024 else if (info.nargs == 1)
9025 s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
9027 /* Emit the amount of stack space to allocate or deallocate. */
9028 s += sprintf (s, "%d", (int) info.size);
9030 /* Save or restore $16. */
9031 if (BITSET_P (info.mask, 16))
9032 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
9034 /* Save or restore $17. */
9035 if (BITSET_P (info.mask, 17))
9036 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
9038 /* Save or restore registers in the range $s2...$s8, which
9039 mips16e_s2_s8_regs lists in decreasing order. Note that this
9040 is a software register range; the hardware registers are not
9041 numbered consecutively. */
9042 end = ARRAY_SIZE (mips16e_s2_s8_regs);
9043 i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
9044 if (i < end)
9045 s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
9046 mips16e_s2_s8_regs[i]);
9048 /* Save or restore registers in the range $a0...$a3. */
9049 end = ARRAY_SIZE (mips16e_a0_a3_regs);
9050 i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
9051 if (i < end)
9052 s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
9053 mips16e_a0_a3_regs[end - 1]);
9055 /* Save or restore $31. */
9056 if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
9057 s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
9059 return buffer;
9062 /* Return true if the current function returns its value in a floating-point
9063 register in MIPS16 mode. */
9065 static bool
9066 mips16_cfun_returns_in_fpr_p (void)
9068 tree return_type = DECL_RESULT (current_function_decl);
9069 return (TARGET_MIPS16
9070 && TARGET_HARD_FLOAT_ABI
9071 && !aggregate_value_p (return_type, current_function_decl)
9072 && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
9075 /* Return true if predicate PRED is true for at least one instruction.
9076 Cache the result in *CACHE, and assume that the result is true
9077 if *CACHE is already true. */
9079 static bool
9080 mips_find_gp_ref (bool *cache, bool (*pred) (rtx))
9082 rtx insn;
9084 if (!*cache)
9086 push_topmost_sequence ();
9087 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9088 if (USEFUL_INSN_P (insn) && pred (insn))
9090 *cache = true;
9091 break;
9093 pop_topmost_sequence ();
9095 return *cache;
9098 /* Return true if INSN refers to the global pointer in an "inflexible" way.
9099 See mips_cfun_has_inflexible_gp_ref_p for details. */
9101 static bool
9102 mips_insn_has_inflexible_gp_ref_p (rtx insn)
9104 /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
9105 indicate that the target could be a traditional MIPS
9106 lazily-binding stub. */
9107 return find_reg_fusage (insn, USE, pic_offset_table_rtx);
9110 /* Return true if the current function refers to the global pointer
9111 in a way that forces $28 to be valid. This means that we can't
9112 change the choice of global pointer, even for NewABI code.
9114 One example of this (and one which needs several checks) is that
9115 $28 must be valid when calling traditional MIPS lazy-binding stubs.
9116 (This restriction does not apply to PLTs.) */
9118 static bool
9119 mips_cfun_has_inflexible_gp_ref_p (void)
9121 /* If the function has a nonlocal goto, $28 must hold the correct
9122 global pointer for the target function. That is, the target
9123 of the goto implicitly uses $28. */
9124 if (crtl->has_nonlocal_goto)
9125 return true;
9127 if (TARGET_ABICALLS_PIC2)
9129 /* Symbolic accesses implicitly use the global pointer unless
9130 -mexplicit-relocs is in effect. JAL macros to symbolic addresses
9131 might go to traditional MIPS lazy-binding stubs. */
9132 if (!TARGET_EXPLICIT_RELOCS)
9133 return true;
9135 /* FUNCTION_PROFILER includes a JAL to _mcount, which again
9136 can be lazily-bound. */
9137 if (crtl->profile)
9138 return true;
9140 /* MIPS16 functions that return in FPRs need to call an
9141 external libgcc routine. This call is only made explict
9142 during mips_expand_epilogue, and it too might be lazily bound. */
9143 if (mips16_cfun_returns_in_fpr_p ())
9144 return true;
9147 return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
9148 mips_insn_has_inflexible_gp_ref_p);
9151 /* Return true if INSN refers to the global pointer in a "flexible" way.
9152 See mips_cfun_has_flexible_gp_ref_p for details. */
9154 static bool
9155 mips_insn_has_flexible_gp_ref_p (rtx insn)
9157 return (get_attr_got (insn) != GOT_UNSET
9158 || mips_small_data_pattern_p (PATTERN (insn))
9159 || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
9162 /* Return true if the current function references the global pointer,
9163 but if those references do not inherently require the global pointer
9164 to be $28. Assume !mips_cfun_has_inflexible_gp_ref_p (). */
9166 static bool
9167 mips_cfun_has_flexible_gp_ref_p (void)
9169 /* Reload can sometimes introduce constant pool references
9170 into a function that otherwise didn't need them. For example,
9171 suppose we have an instruction like:
9173 (set (reg:DF R1) (float:DF (reg:SI R2)))
9175 If R2 turns out to be a constant such as 1, the instruction may
9176 have a REG_EQUAL note saying that R1 == 1.0. Reload then has
9177 the option of using this constant if R2 doesn't get allocated
9178 to a register.
9180 In cases like these, reload will have added the constant to the
9181 pool but no instruction will yet refer to it. */
9182 if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
9183 return true;
9185 return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
9186 mips_insn_has_flexible_gp_ref_p);
9189 /* Return the register that should be used as the global pointer
9190 within this function. Return INVALID_REGNUM if the function
9191 doesn't need a global pointer. */
9193 static unsigned int
9194 mips_global_pointer (void)
9196 unsigned int regno;
9198 /* $gp is always available unless we're using a GOT. */
9199 if (!TARGET_USE_GOT)
9200 return GLOBAL_POINTER_REGNUM;
9202 /* If there are inflexible references to $gp, we must use the
9203 standard register. */
9204 if (mips_cfun_has_inflexible_gp_ref_p ())
9205 return GLOBAL_POINTER_REGNUM;
9207 /* If there are no current references to $gp, then the only uses
9208 we can introduce later are those involved in long branches. */
9209 if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
9210 return INVALID_REGNUM;
9212 /* If the global pointer is call-saved, try to use a call-clobbered
9213 alternative. */
9214 if (TARGET_CALL_SAVED_GP && current_function_is_leaf)
9215 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9216 if (!df_regs_ever_live_p (regno)
9217 && call_really_used_regs[regno]
9218 && !fixed_regs[regno]
9219 && regno != PIC_FUNCTION_ADDR_REGNUM)
9220 return regno;
9222 return GLOBAL_POINTER_REGNUM;
9225 /* Return true if the current function's prologue must load the global
9226 pointer value into pic_offset_table_rtx and store the same value in
9227 the function's cprestore slot (if any).
9229 One problem we have to deal with is that, when emitting GOT-based
9230 position independent code, long-branch sequences will need to load
9231 the address of the branch target from the GOT. We don't know until
9232 the very end of compilation whether (and where) the function needs
9233 long branches, so we must ensure that _any_ branch can access the
9234 global pointer in some form. However, we do not want to pessimize
9235 the usual case in which all branches are short.
9237 We handle this as follows:
9239 (1) During reload, we set cfun->machine->global_pointer to
9240 INVALID_REGNUM if we _know_ that the current function
9241 doesn't need a global pointer. This is only valid if
9242 long branches don't need the GOT.
9244 Otherwise, we assume that we might need a global pointer
9245 and pick an appropriate register.
9247 (2) If cfun->machine->global_pointer != INVALID_REGNUM,
9248 we ensure that the global pointer is available at every
9249 block boundary bar entry and exit. We do this in one of two ways:
9251 - If the function has a cprestore slot, we ensure that this
9252 slot is valid at every branch. However, as explained in
9253 point (6) below, there is no guarantee that pic_offset_table_rtx
9254 itself is valid if new uses of the global pointer are introduced
9255 after the first post-epilogue split.
9257 We guarantee that the cprestore slot is valid by loading it
9258 into a fake register, CPRESTORE_SLOT_REGNUM. We then make
9259 this register live at every block boundary bar function entry
9260 and exit. It is then invalid to move the load (and thus the
9261 preceding store) across a block boundary.
9263 - If the function has no cprestore slot, we guarantee that
9264 pic_offset_table_rtx itself is valid at every branch.
9266 See mips_eh_uses for the handling of the register liveness.
9268 (3) During prologue and epilogue generation, we emit "ghost"
9269 placeholder instructions to manipulate the global pointer.
9271 (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
9272 and cfun->machine->must_restore_gp_when_clobbered_p if we already know
9273 that the function needs a global pointer. (There is no need to set
9274 them earlier than this, and doing it as late as possible leads to
9275 fewer false positives.)
9277 (5) If cfun->machine->must_initialize_gp_p is true during a
9278 split_insns pass, we split the ghost instructions into real
9279 instructions. These split instructions can then be optimized in
9280 the usual way. Otherwise, we keep the ghost instructions intact,
9281 and optimize for the case where they aren't needed. We still
9282 have the option of splitting them later, if we need to introduce
9283 new uses of the global pointer.
9285 For example, the scheduler ignores a ghost instruction that
9286 stores $28 to the stack, but it handles the split form of
9287 the ghost instruction as an ordinary store.
9289 (6) [OldABI only.] If cfun->machine->must_restore_gp_when_clobbered_p
9290 is true during the first post-epilogue split_insns pass, we split
9291 calls and restore_gp patterns into instructions that explicitly
9292 load pic_offset_table_rtx from the cprestore slot. Otherwise,
9293 we split these patterns into instructions that _don't_ load from
9294 the cprestore slot.
9296 If cfun->machine->must_restore_gp_when_clobbered_p is true at the
9297 time of the split, then any instructions that exist at that time
9298 can make free use of pic_offset_table_rtx. However, if we want
9299 to introduce new uses of the global pointer after the split,
9300 we must explicitly load the value from the cprestore slot, since
9301 pic_offset_table_rtx itself might not be valid at a given point
9302 in the function.
9304 The idea is that we want to be able to delete redundant
9305 loads from the cprestore slot in the usual case where no
9306 long branches are needed.
9308 (7) If cfun->machine->must_initialize_gp_p is still false at the end
9309 of md_reorg, we decide whether the global pointer is needed for
9310 long branches. If so, we set cfun->machine->must_initialize_gp_p
9311 to true and split the ghost instructions into real instructions
9312 at that stage.
9314 Note that the ghost instructions must have a zero length for three reasons:
9316 - Giving the length of the underlying $gp sequence might cause
9317 us to use long branches in cases where they aren't really needed.
9319 - They would perturb things like alignment calculations.
9321 - More importantly, the hazard detection in md_reorg relies on
9322 empty instructions having a zero length.
9324 If we find a long branch and split the ghost instructions at the
9325 end of md_reorg, the split could introduce more long branches.
9326 That isn't a problem though, because we still do the split before
9327 the final shorten_branches pass.
9329 This is extremely ugly, but it seems like the best compromise between
9330 correctness and efficiency. */
9332 bool
9333 mips_must_initialize_gp_p (void)
9335 return cfun->machine->must_initialize_gp_p;
9338 /* Return true if REGNO is a register that is ordinarily call-clobbered
9339 but must nevertheless be preserved by an interrupt handler. */
9341 static bool
9342 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
9344 if (MD_REG_P (regno))
9345 return true;
9347 if (TARGET_DSP && DSP_ACC_REG_P (regno))
9348 return true;
9350 if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
9352 /* $0 is hard-wired. */
9353 if (regno == GP_REG_FIRST)
9354 return false;
9356 /* The interrupt handler can treat kernel registers as
9357 scratch registers. */
9358 if (KERNEL_REG_P (regno))
9359 return false;
9361 /* The function will return the stack pointer to its original value
9362 anyway. */
9363 if (regno == STACK_POINTER_REGNUM)
9364 return false;
9366 /* Otherwise, return true for registers that aren't ordinarily
9367 call-clobbered. */
9368 return call_really_used_regs[regno];
9371 return false;
9374 /* Return true if the current function should treat register REGNO
9375 as call-saved. */
9377 static bool
9378 mips_cfun_call_saved_reg_p (unsigned int regno)
9380 /* If the user makes an ordinarily-call-saved register global,
9381 that register is no longer call-saved. */
9382 if (global_regs[regno])
9383 return false;
9385 /* Interrupt handlers need to save extra registers. */
9386 if (cfun->machine->interrupt_handler_p
9387 && mips_interrupt_extra_call_saved_reg_p (regno))
9388 return true;
9390 /* call_insns preserve $28 unless they explicitly say otherwise,
9391 so call_really_used_regs[] treats $28 as call-saved. However,
9392 we want the ABI property rather than the default call_insn
9393 property here. */
9394 return (regno == GLOBAL_POINTER_REGNUM
9395 ? TARGET_CALL_SAVED_GP
9396 : !call_really_used_regs[regno]);
9399 /* Return true if the function body might clobber register REGNO.
9400 We know that REGNO is call-saved. */
9402 static bool
9403 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
9405 /* Some functions should be treated as clobbering all call-saved
9406 registers. */
9407 if (crtl->saves_all_registers)
9408 return true;
9410 /* DF handles cases where a register is explicitly referenced in
9411 the rtl. Incoming values are passed in call-clobbered registers,
9412 so we can assume that any live call-saved register is set within
9413 the function. */
9414 if (df_regs_ever_live_p (regno))
9415 return true;
9417 /* Check for registers that are clobbered by FUNCTION_PROFILER.
9418 These clobbers are not explicit in the rtl. */
9419 if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
9420 return true;
9422 /* If we're using a call-saved global pointer, the function's
9423 prologue will need to set it up. */
9424 if (cfun->machine->global_pointer == regno)
9425 return true;
9427 /* The function's prologue will need to set the frame pointer if
9428 frame_pointer_needed. */
9429 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
9430 return true;
9432 /* If a MIPS16 function returns a value in FPRs, its epilogue
9433 will need to call an external libgcc routine. This yet-to-be
9434 generated call_insn will clobber $31. */
9435 if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
9436 return true;
9438 /* If REGNO is ordinarily call-clobbered, we must assume that any
9439 called function could modify it. */
9440 if (cfun->machine->interrupt_handler_p
9441 && !current_function_is_leaf
9442 && mips_interrupt_extra_call_saved_reg_p (regno))
9443 return true;
9445 return false;
9448 /* Return true if the current function must save register REGNO. */
9450 static bool
9451 mips_save_reg_p (unsigned int regno)
9453 if (mips_cfun_call_saved_reg_p (regno))
9455 if (mips_cfun_might_clobber_call_saved_reg_p (regno))
9456 return true;
9458 /* Save both registers in an FPR pair if either one is used. This is
9459 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
9460 register to be used without the even register. */
9461 if (FP_REG_P (regno)
9462 && MAX_FPRS_PER_FMT == 2
9463 && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
9464 return true;
9467 /* We need to save the incoming return address if __builtin_eh_return
9468 is being used to set a different return address. */
9469 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
9470 return true;
9472 return false;
9475 /* Populate the current function's mips_frame_info structure.
9477 MIPS stack frames look like:
9479 +-------------------------------+
9481 | incoming stack arguments |
9483 +-------------------------------+
9485 | caller-allocated save area |
9486 A | for register arguments |
9488 +-------------------------------+ <-- incoming stack pointer
9490 | callee-allocated save area |
9491 B | for arguments that are |
9492 | split between registers and |
9493 | the stack |
9495 +-------------------------------+ <-- arg_pointer_rtx
9497 C | callee-allocated save area |
9498 | for register varargs |
9500 +-------------------------------+ <-- frame_pointer_rtx
9501 | | + cop0_sp_offset
9502 | COP0 reg save area | + UNITS_PER_WORD
9504 +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
9505 | | + UNITS_PER_WORD
9506 | accumulator save area |
9508 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
9509 | | + UNITS_PER_HWFPVALUE
9510 | FPR save area |
9512 +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
9513 | | + UNITS_PER_WORD
9514 | GPR save area |
9516 +-------------------------------+ <-- frame_pointer_rtx with
9517 | | \ -fstack-protector
9518 | local variables | | var_size
9519 | | /
9520 +-------------------------------+
9521 | | \
9522 | $gp save area | | cprestore_size
9523 | | /
9524 P +-------------------------------+ <-- hard_frame_pointer_rtx for
9525 | | \ MIPS16 code
9526 | outgoing stack arguments | |
9527 | | |
9528 +-------------------------------+ | args_size
9529 | | |
9530 | caller-allocated save area | |
9531 | for register arguments | |
9532 | | /
9533 +-------------------------------+ <-- stack_pointer_rtx
9534 frame_pointer_rtx without
9535 -fstack-protector
9536 hard_frame_pointer_rtx for
9537 non-MIPS16 code.
9539 At least two of A, B and C will be empty.
9541 Dynamic stack allocations such as alloca insert data at point P.
9542 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
9543 hard_frame_pointer_rtx unchanged. */
9545 static void
9546 mips_compute_frame_info (void)
9548 struct mips_frame_info *frame;
9549 HOST_WIDE_INT offset, size;
9550 unsigned int regno, i;
9552 /* Set this function's interrupt properties. */
9553 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
9555 if (!ISA_MIPS32R2)
9556 error ("the %<interrupt%> attribute requires a MIPS32r2 processor");
9557 else if (TARGET_HARD_FLOAT)
9558 error ("the %<interrupt%> attribute requires %<-msoft-float%>");
9559 else if (TARGET_MIPS16)
9560 error ("interrupt handlers cannot be MIPS16 functions");
9561 else
9563 cfun->machine->interrupt_handler_p = true;
9564 cfun->machine->use_shadow_register_set_p =
9565 mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
9566 cfun->machine->keep_interrupts_masked_p =
9567 mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
9568 cfun->machine->use_debug_exception_return_p =
9569 mips_use_debug_exception_return_p (TREE_TYPE
9570 (current_function_decl));
9574 frame = &cfun->machine->frame;
9575 memset (frame, 0, sizeof (*frame));
9576 size = get_frame_size ();
9578 cfun->machine->global_pointer = mips_global_pointer ();
9580 /* The first two blocks contain the outgoing argument area and the $gp save
9581 slot. This area isn't needed in leaf functions, but if the
9582 target-independent frame size is nonzero, we have already committed to
9583 allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD. */
9584 if ((size == 0 || FRAME_GROWS_DOWNWARD) && current_function_is_leaf)
9586 /* The MIPS 3.0 linker does not like functions that dynamically
9587 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
9588 looks like we are trying to create a second frame pointer to the
9589 function, so allocate some stack space to make it happy. */
9590 if (cfun->calls_alloca)
9591 frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
9592 else
9593 frame->args_size = 0;
9594 frame->cprestore_size = 0;
9596 else
9598 frame->args_size = crtl->outgoing_args_size;
9599 frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
9601 offset = frame->args_size + frame->cprestore_size;
9603 /* Move above the local variables. */
9604 frame->var_size = MIPS_STACK_ALIGN (size);
9605 offset += frame->var_size;
9607 /* Find out which GPRs we need to save. */
9608 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9609 if (mips_save_reg_p (regno))
9611 frame->num_gp++;
9612 frame->mask |= 1 << (regno - GP_REG_FIRST);
9615 /* If this function calls eh_return, we must also save and restore the
9616 EH data registers. */
9617 if (crtl->calls_eh_return)
9618 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
9620 frame->num_gp++;
9621 frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
9624 /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
9625 $a3-$a0 and $s2-$s8. If we save one register in the range, we must
9626 save all later registers too. */
9627 if (GENERATE_MIPS16E_SAVE_RESTORE)
9629 mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
9630 ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
9631 mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
9632 ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
9635 /* Move above the GPR save area. */
9636 if (frame->num_gp > 0)
9638 offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
9639 frame->gp_sp_offset = offset - UNITS_PER_WORD;
9642 /* Find out which FPRs we need to save. This loop must iterate over
9643 the same space as its companion in mips_for_each_saved_gpr_and_fpr. */
9644 if (TARGET_HARD_FLOAT)
9645 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
9646 if (mips_save_reg_p (regno))
9648 frame->num_fp += MAX_FPRS_PER_FMT;
9649 frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
9652 /* Move above the FPR save area. */
9653 if (frame->num_fp > 0)
9655 offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
9656 frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
9659 /* Add in space for the interrupt context information. */
9660 if (cfun->machine->interrupt_handler_p)
9662 /* Check HI/LO. */
9663 if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
9665 frame->num_acc++;
9666 frame->acc_mask |= (1 << 0);
9669 /* Check accumulators 1, 2, 3. */
9670 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
9671 if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
9673 frame->num_acc++;
9674 frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
9677 /* All interrupt context functions need space to preserve STATUS. */
9678 frame->num_cop0_regs++;
9680 /* If we don't keep interrupts masked, we need to save EPC. */
9681 if (!cfun->machine->keep_interrupts_masked_p)
9682 frame->num_cop0_regs++;
9685 /* Move above the accumulator save area. */
9686 if (frame->num_acc > 0)
9688 /* Each accumulator needs 2 words. */
9689 offset += frame->num_acc * 2 * UNITS_PER_WORD;
9690 frame->acc_sp_offset = offset - UNITS_PER_WORD;
9693 /* Move above the COP0 register save area. */
9694 if (frame->num_cop0_regs > 0)
9696 offset += frame->num_cop0_regs * UNITS_PER_WORD;
9697 frame->cop0_sp_offset = offset - UNITS_PER_WORD;
9700 /* Move above the callee-allocated varargs save area. */
9701 offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
9702 frame->arg_pointer_offset = offset;
9704 /* Move above the callee-allocated area for pretend stack arguments. */
9705 offset += crtl->args.pretend_args_size;
9706 frame->total_size = offset;
9708 /* Work out the offsets of the save areas from the top of the frame. */
9709 if (frame->gp_sp_offset > 0)
9710 frame->gp_save_offset = frame->gp_sp_offset - offset;
9711 if (frame->fp_sp_offset > 0)
9712 frame->fp_save_offset = frame->fp_sp_offset - offset;
9713 if (frame->acc_sp_offset > 0)
9714 frame->acc_save_offset = frame->acc_sp_offset - offset;
9715 if (frame->num_cop0_regs > 0)
9716 frame->cop0_save_offset = frame->cop0_sp_offset - offset;
9718 /* MIPS16 code offsets the frame pointer by the size of the outgoing
9719 arguments. This tends to increase the chances of using unextended
9720 instructions for local variables and incoming arguments. */
9721 if (TARGET_MIPS16)
9722 frame->hard_frame_pointer_offset = frame->args_size;
9725 /* Return the style of GP load sequence that is being used for the
9726 current function. */
9728 enum mips_loadgp_style
9729 mips_current_loadgp_style (void)
9731 if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
9732 return LOADGP_NONE;
9734 if (TARGET_RTP_PIC)
9735 return LOADGP_RTP;
9737 if (TARGET_ABSOLUTE_ABICALLS)
9738 return LOADGP_ABSOLUTE;
9740 return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
9743 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
9745 static bool
9746 mips_frame_pointer_required (void)
9748 /* If the function contains dynamic stack allocations, we need to
9749 use the frame pointer to access the static parts of the frame. */
9750 if (cfun->calls_alloca)
9751 return true;
9753 /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
9754 reload may be unable to compute the address of a local variable,
9755 since there is no way to add a large constant to the stack pointer
9756 without using a second temporary register. */
9757 if (TARGET_MIPS16)
9759 mips_compute_frame_info ();
9760 if (!SMALL_OPERAND (cfun->machine->frame.total_size))
9761 return true;
9764 return false;
9767 /* Make sure that we're not trying to eliminate to the wrong hard frame
9768 pointer. */
9770 static bool
9771 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
9773 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
9776 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
9777 or argument pointer. TO is either the stack pointer or hard frame
9778 pointer. */
9780 HOST_WIDE_INT
9781 mips_initial_elimination_offset (int from, int to)
9783 HOST_WIDE_INT offset;
9785 mips_compute_frame_info ();
9787 /* Set OFFSET to the offset from the end-of-prologue stack pointer. */
9788 switch (from)
9790 case FRAME_POINTER_REGNUM:
9791 if (FRAME_GROWS_DOWNWARD)
9792 offset = (cfun->machine->frame.args_size
9793 + cfun->machine->frame.cprestore_size
9794 + cfun->machine->frame.var_size);
9795 else
9796 offset = 0;
9797 break;
9799 case ARG_POINTER_REGNUM:
9800 offset = cfun->machine->frame.arg_pointer_offset;
9801 break;
9803 default:
9804 gcc_unreachable ();
9807 if (to == HARD_FRAME_POINTER_REGNUM)
9808 offset -= cfun->machine->frame.hard_frame_pointer_offset;
9810 return offset;
9813 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY. */
9815 static void
9816 mips_extra_live_on_entry (bitmap regs)
9818 if (TARGET_USE_GOT)
9820 /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
9821 the global pointer. */
9822 if (!TARGET_ABSOLUTE_ABICALLS)
9823 bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
9825 /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
9826 the global pointer. */
9827 if (TARGET_MIPS16)
9828 bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
9830 /* See the comment above load_call<mode> for details. */
9831 bitmap_set_bit (regs, GOT_VERSION_REGNUM);
9835 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
9836 previous frame. */
9839 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
9841 if (count != 0)
9842 return const0_rtx;
9844 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
9847 /* Emit code to change the current function's return address to
9848 ADDRESS. SCRATCH is available as a scratch register, if needed.
9849 ADDRESS and SCRATCH are both word-mode GPRs. */
9851 void
9852 mips_set_return_address (rtx address, rtx scratch)
9854 rtx slot_address;
9856 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
9857 slot_address = mips_add_offset (scratch, stack_pointer_rtx,
9858 cfun->machine->frame.gp_sp_offset);
9859 mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
9862 /* Return true if the current function has a cprestore slot. */
9864 bool
9865 mips_cfun_has_cprestore_slot_p (void)
9867 return (cfun->machine->global_pointer != INVALID_REGNUM
9868 && cfun->machine->frame.cprestore_size > 0);
9871 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
9872 cprestore slot. LOAD_P is true if the caller wants to load from
9873 the cprestore slot; it is false if the caller wants to store to
9874 the slot. */
9876 static void
9877 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
9878 bool load_p)
9880 const struct mips_frame_info *frame;
9882 frame = &cfun->machine->frame;
9883 /* .cprestore always uses the stack pointer instead of the frame pointer.
9884 We have a free choice for direct stores for non-MIPS16 functions,
9885 and for MIPS16 functions whose cprestore slot is in range of the
9886 stack pointer. Using the stack pointer would sometimes give more
9887 (early) scheduling freedom, but using the frame pointer would
9888 sometimes give more (late) scheduling freedom. It's hard to
9889 predict which applies to a given function, so let's keep things
9890 simple.
9892 Loads must always use the frame pointer in functions that call
9893 alloca, and there's little benefit to using the stack pointer
9894 otherwise. */
9895 if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
9897 *base = hard_frame_pointer_rtx;
9898 *offset = frame->args_size - frame->hard_frame_pointer_offset;
9900 else
9902 *base = stack_pointer_rtx;
9903 *offset = frame->args_size;
9907 /* Return true if X is the load or store address of the cprestore slot;
9908 LOAD_P says which. */
9910 bool
9911 mips_cprestore_address_p (rtx x, bool load_p)
9913 rtx given_base, required_base;
9914 HOST_WIDE_INT given_offset, required_offset;
9916 mips_split_plus (x, &given_base, &given_offset);
9917 mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
9918 return given_base == required_base && given_offset == required_offset;
9921 /* Return a MEM rtx for the cprestore slot. LOAD_P is true if we are
9922 going to load from it, false if we are going to store to it.
9923 Use TEMP as a temporary register if need be. */
9925 static rtx
9926 mips_cprestore_slot (rtx temp, bool load_p)
9928 rtx base;
9929 HOST_WIDE_INT offset;
9931 mips_get_cprestore_base_and_offset (&base, &offset, load_p);
9932 return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
9935 /* Emit instructions to save global pointer value GP into cprestore
9936 slot MEM. OFFSET is the offset that MEM applies to the base register.
9938 MEM may not be a legitimate address. If it isn't, TEMP is a
9939 temporary register that can be used, otherwise it is a SCRATCH. */
9941 void
9942 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
9944 if (TARGET_CPRESTORE_DIRECTIVE)
9946 gcc_assert (gp == pic_offset_table_rtx);
9947 emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
9949 else
9950 mips_emit_move (mips_cprestore_slot (temp, false), gp);
9953 /* Restore $gp from its save slot, using TEMP as a temporary base register
9954 if need be. This function is for o32 and o64 abicalls only.
9956 See mips_must_initialize_gp_p for details about how we manage the
9957 global pointer. */
9959 void
9960 mips_restore_gp_from_cprestore_slot (rtx temp)
9962 gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
9964 if (!cfun->machine->must_restore_gp_when_clobbered_p)
9966 emit_note (NOTE_INSN_DELETED);
9967 return;
9970 if (TARGET_MIPS16)
9972 mips_emit_move (temp, mips_cprestore_slot (temp, true));
9973 mips_emit_move (pic_offset_table_rtx, temp);
9975 else
9976 mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
9977 if (!TARGET_EXPLICIT_RELOCS)
9978 emit_insn (gen_blockage ());
9981 /* A function to save or store a register. The first argument is the
9982 register and the second is the stack slot. */
9983 typedef void (*mips_save_restore_fn) (rtx, rtx);
9985 /* Use FN to save or restore register REGNO. MODE is the register's
9986 mode and OFFSET is the offset of its save slot from the current
9987 stack pointer. */
9989 static void
9990 mips_save_restore_reg (enum machine_mode mode, int regno,
9991 HOST_WIDE_INT offset, mips_save_restore_fn fn)
9993 rtx mem;
9995 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
9996 offset));
9997 fn (gen_rtx_REG (mode, regno), mem);
10000 /* Call FN for each accumlator that is saved by the current function.
10001 SP_OFFSET is the offset of the current stack pointer from the start
10002 of the frame. */
10004 static void
10005 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
10007 HOST_WIDE_INT offset;
10008 int regno;
10010 offset = cfun->machine->frame.acc_sp_offset - sp_offset;
10011 if (BITSET_P (cfun->machine->frame.acc_mask, 0))
10013 mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
10014 offset -= UNITS_PER_WORD;
10015 mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
10016 offset -= UNITS_PER_WORD;
10019 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
10020 if (BITSET_P (cfun->machine->frame.acc_mask,
10021 ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
10023 mips_save_restore_reg (word_mode, regno, offset, fn);
10024 offset -= UNITS_PER_WORD;
10028 /* Call FN for each register that is saved by the current function.
10029 SP_OFFSET is the offset of the current stack pointer from the start
10030 of the frame. */
10032 static void
10033 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
10034 mips_save_restore_fn fn)
10036 enum machine_mode fpr_mode;
10037 HOST_WIDE_INT offset;
10038 int regno;
10040 /* Save registers starting from high to low. The debuggers prefer at least
10041 the return register be stored at func+4, and also it allows us not to
10042 need a nop in the epilogue if at least one register is reloaded in
10043 addition to return address. */
10044 offset = cfun->machine->frame.gp_sp_offset - sp_offset;
10045 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
10046 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
10048 /* Record the ra offset for use by mips_function_profiler. */
10049 if (regno == RETURN_ADDR_REGNUM)
10050 cfun->machine->frame.ra_fp_offset = offset + sp_offset;
10051 mips_save_restore_reg (word_mode, regno, offset, fn);
10052 offset -= UNITS_PER_WORD;
10055 /* This loop must iterate over the same space as its companion in
10056 mips_compute_frame_info. */
10057 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
10058 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
10059 for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
10060 regno >= FP_REG_FIRST;
10061 regno -= MAX_FPRS_PER_FMT)
10062 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
10064 mips_save_restore_reg (fpr_mode, regno, offset, fn);
10065 offset -= GET_MODE_SIZE (fpr_mode);
10069 /* Return true if a move between register REGNO and its save slot (MEM)
10070 can be done in a single move. LOAD_P is true if we are loading
10071 from the slot, false if we are storing to it. */
10073 static bool
10074 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
10076 /* There is a specific MIPS16 instruction for saving $31 to the stack. */
10077 if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
10078 return false;
10080 return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
10081 GET_MODE (mem), mem, load_p) == NO_REGS;
10084 /* Emit a move from SRC to DEST, given that one of them is a register
10085 save slot and that the other is a register. TEMP is a temporary
10086 GPR of the same mode that is available if need be. */
10088 void
10089 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
10091 unsigned int regno;
10092 rtx mem;
10094 if (REG_P (src))
10096 regno = REGNO (src);
10097 mem = dest;
10099 else
10101 regno = REGNO (dest);
10102 mem = src;
10105 if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
10107 /* We don't yet know whether we'll need this instruction or not.
10108 Postpone the decision by emitting a ghost move. This move
10109 is specifically not frame-related; only the split version is. */
10110 if (TARGET_64BIT)
10111 emit_insn (gen_move_gpdi (dest, src));
10112 else
10113 emit_insn (gen_move_gpsi (dest, src));
10114 return;
10117 if (regno == HI_REGNUM)
10119 if (REG_P (dest))
10121 mips_emit_move (temp, src);
10122 if (TARGET_64BIT)
10123 emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
10124 temp, gen_rtx_REG (DImode, LO_REGNUM)));
10125 else
10126 emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
10127 temp, gen_rtx_REG (SImode, LO_REGNUM)));
10129 else
10131 if (TARGET_64BIT)
10132 emit_insn (gen_mfhidi_ti (temp,
10133 gen_rtx_REG (TImode, MD_REG_FIRST)));
10134 else
10135 emit_insn (gen_mfhisi_di (temp,
10136 gen_rtx_REG (DImode, MD_REG_FIRST)));
10137 mips_emit_move (dest, temp);
10140 else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
10141 mips_emit_move (dest, src);
10142 else
10144 gcc_assert (!reg_overlap_mentioned_p (dest, temp));
10145 mips_emit_move (temp, src);
10146 mips_emit_move (dest, temp);
10148 if (MEM_P (dest))
10149 mips_set_frame_expr (mips_frame_set (dest, src));
10152 /* If we're generating n32 or n64 abicalls, and the current function
10153 does not use $28 as its global pointer, emit a cplocal directive.
10154 Use pic_offset_table_rtx as the argument to the directive. */
10156 static void
10157 mips_output_cplocal (void)
10159 if (!TARGET_EXPLICIT_RELOCS
10160 && mips_must_initialize_gp_p ()
10161 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
10162 output_asm_insn (".cplocal %+", 0);
10165 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE. */
10167 static void
10168 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10170 const char *fnname;
10172 /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
10173 floating-point arguments. */
10174 if (TARGET_MIPS16
10175 && TARGET_HARD_FLOAT_ABI
10176 && crtl->args.info.fp_code != 0)
10177 mips16_build_function_stub ();
10179 /* Get the function name the same way that toplev.c does before calling
10180 assemble_start_function. This is needed so that the name used here
10181 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
10182 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10183 mips_start_function_definition (fnname, TARGET_MIPS16);
10185 /* Output MIPS-specific frame information. */
10186 if (!flag_inhibit_size_directive)
10188 const struct mips_frame_info *frame;
10190 frame = &cfun->machine->frame;
10192 /* .frame FRAMEREG, FRAMESIZE, RETREG. */
10193 fprintf (file,
10194 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
10195 "# vars= " HOST_WIDE_INT_PRINT_DEC
10196 ", regs= %d/%d"
10197 ", args= " HOST_WIDE_INT_PRINT_DEC
10198 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
10199 reg_names[frame_pointer_needed
10200 ? HARD_FRAME_POINTER_REGNUM
10201 : STACK_POINTER_REGNUM],
10202 (frame_pointer_needed
10203 ? frame->total_size - frame->hard_frame_pointer_offset
10204 : frame->total_size),
10205 reg_names[RETURN_ADDR_REGNUM],
10206 frame->var_size,
10207 frame->num_gp, frame->num_fp,
10208 frame->args_size,
10209 frame->cprestore_size);
10211 /* .mask MASK, OFFSET. */
10212 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10213 frame->mask, frame->gp_save_offset);
10215 /* .fmask MASK, OFFSET. */
10216 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10217 frame->fmask, frame->fp_save_offset);
10220 /* Handle the initialization of $gp for SVR4 PIC, if applicable.
10221 Also emit the ".set noreorder; .set nomacro" sequence for functions
10222 that need it. */
10223 if (mips_must_initialize_gp_p ()
10224 && mips_current_loadgp_style () == LOADGP_OLDABI)
10226 if (TARGET_MIPS16)
10228 /* This is a fixed-form sequence. The position of the
10229 first two instructions is important because of the
10230 way _gp_disp is defined. */
10231 output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
10232 output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
10233 output_asm_insn ("sll\t$2,16", 0);
10234 output_asm_insn ("addu\t$2,$3", 0);
10236 else
10238 /* .cpload must be in a .set noreorder but not a
10239 .set nomacro block. */
10240 mips_push_asm_switch (&mips_noreorder);
10241 output_asm_insn (".cpload\t%^", 0);
10242 if (!cfun->machine->all_noreorder_p)
10243 mips_pop_asm_switch (&mips_noreorder);
10244 else
10245 mips_push_asm_switch (&mips_nomacro);
10248 else if (cfun->machine->all_noreorder_p)
10250 mips_push_asm_switch (&mips_noreorder);
10251 mips_push_asm_switch (&mips_nomacro);
10254 /* Tell the assembler which register we're using as the global
10255 pointer. This is needed for thunks, since they can use either
10256 explicit relocs or assembler macros. */
10257 mips_output_cplocal ();
10260 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE. */
10262 static void
10263 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10264 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10266 const char *fnname;
10268 /* Reinstate the normal $gp. */
10269 SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
10270 mips_output_cplocal ();
10272 if (cfun->machine->all_noreorder_p)
10274 mips_pop_asm_switch (&mips_nomacro);
10275 mips_pop_asm_switch (&mips_noreorder);
10278 /* Get the function name the same way that toplev.c does before calling
10279 assemble_start_function. This is needed so that the name used here
10280 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
10281 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10282 mips_end_function_definition (fnname);
10285 /* Emit an optimisation barrier for accesses to the current frame. */
10287 static void
10288 mips_frame_barrier (void)
10290 emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
10293 /* Save register REG to MEM. Make the instruction frame-related. */
10295 static void
10296 mips_save_reg (rtx reg, rtx mem)
10298 if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
10300 rtx x1, x2;
10302 if (mips_split_64bit_move_p (mem, reg))
10303 mips_split_doubleword_move (mem, reg);
10304 else
10305 mips_emit_move (mem, reg);
10307 x1 = mips_frame_set (mips_subword (mem, false),
10308 mips_subword (reg, false));
10309 x2 = mips_frame_set (mips_subword (mem, true),
10310 mips_subword (reg, true));
10311 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
10313 else
10314 mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
10317 /* The __gnu_local_gp symbol. */
10319 static GTY(()) rtx mips_gnu_local_gp;
10321 /* If we're generating n32 or n64 abicalls, emit instructions
10322 to set up the global pointer. */
10324 static void
10325 mips_emit_loadgp (void)
10327 rtx addr, offset, incoming_address, base, index, pic_reg;
10329 pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10330 switch (mips_current_loadgp_style ())
10332 case LOADGP_ABSOLUTE:
10333 if (mips_gnu_local_gp == NULL)
10335 mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
10336 SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
10338 emit_insn (PMODE_INSN (gen_loadgp_absolute,
10339 (pic_reg, mips_gnu_local_gp)));
10340 break;
10342 case LOADGP_OLDABI:
10343 /* Added by mips_output_function_prologue. */
10344 break;
10346 case LOADGP_NEWABI:
10347 addr = XEXP (DECL_RTL (current_function_decl), 0);
10348 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
10349 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
10350 emit_insn (PMODE_INSN (gen_loadgp_newabi,
10351 (pic_reg, offset, incoming_address)));
10352 break;
10354 case LOADGP_RTP:
10355 base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
10356 index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
10357 emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
10358 break;
10360 default:
10361 return;
10364 if (TARGET_MIPS16)
10365 emit_insn (PMODE_INSN (gen_copygp_mips16,
10366 (pic_offset_table_rtx, pic_reg)));
10368 /* Emit a blockage if there are implicit uses of the GP register.
10369 This includes profiled functions, because FUNCTION_PROFILE uses
10370 a jal macro. */
10371 if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
10372 emit_insn (gen_loadgp_blockage ());
10375 /* A for_each_rtx callback. Stop the search if *X is a kernel register. */
10377 static int
10378 mips_kernel_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
10380 return REG_P (*x) && KERNEL_REG_P (REGNO (*x));
10383 /* Expand the "prologue" pattern. */
10385 void
10386 mips_expand_prologue (void)
10388 const struct mips_frame_info *frame;
10389 HOST_WIDE_INT size;
10390 unsigned int nargs;
10391 rtx insn;
10393 if (cfun->machine->global_pointer != INVALID_REGNUM)
10395 /* Check whether an insn uses pic_offset_table_rtx, either explicitly
10396 or implicitly. If so, we can commit to using a global pointer
10397 straight away, otherwise we need to defer the decision. */
10398 if (mips_cfun_has_inflexible_gp_ref_p ()
10399 || mips_cfun_has_flexible_gp_ref_p ())
10401 cfun->machine->must_initialize_gp_p = true;
10402 cfun->machine->must_restore_gp_when_clobbered_p = true;
10405 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
10408 frame = &cfun->machine->frame;
10409 size = frame->total_size;
10411 if (flag_stack_usage_info)
10412 current_function_static_stack_size = size;
10414 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
10415 bytes beforehand; this is enough to cover the register save area
10416 without going out of range. */
10417 if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
10418 || frame->num_cop0_regs > 0)
10420 HOST_WIDE_INT step1;
10422 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
10423 if (GENERATE_MIPS16E_SAVE_RESTORE)
10425 HOST_WIDE_INT offset;
10426 unsigned int mask, regno;
10428 /* Try to merge argument stores into the save instruction. */
10429 nargs = mips16e_collect_argument_saves ();
10431 /* Build the save instruction. */
10432 mask = frame->mask;
10433 insn = mips16e_build_save_restore (false, &mask, &offset,
10434 nargs, step1);
10435 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10436 mips_frame_barrier ();
10437 size -= step1;
10439 /* Check if we need to save other registers. */
10440 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
10441 if (BITSET_P (mask, regno - GP_REG_FIRST))
10443 offset -= UNITS_PER_WORD;
10444 mips_save_restore_reg (word_mode, regno,
10445 offset, mips_save_reg);
10448 else
10450 if (cfun->machine->interrupt_handler_p)
10452 HOST_WIDE_INT offset;
10453 rtx mem;
10455 /* If this interrupt is using a shadow register set, we need to
10456 get the stack pointer from the previous register set. */
10457 if (cfun->machine->use_shadow_register_set_p)
10458 emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
10459 stack_pointer_rtx));
10461 if (!cfun->machine->keep_interrupts_masked_p)
10463 /* Move from COP0 Cause to K0. */
10464 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
10465 gen_rtx_REG (SImode,
10466 COP0_CAUSE_REG_NUM)));
10467 /* Move from COP0 EPC to K1. */
10468 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
10469 gen_rtx_REG (SImode,
10470 COP0_EPC_REG_NUM)));
10473 /* Allocate the first part of the frame. */
10474 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
10475 GEN_INT (-step1));
10476 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10477 mips_frame_barrier ();
10478 size -= step1;
10480 /* Start at the uppermost location for saving. */
10481 offset = frame->cop0_sp_offset - size;
10482 if (!cfun->machine->keep_interrupts_masked_p)
10484 /* Push EPC into its stack slot. */
10485 mem = gen_frame_mem (word_mode,
10486 plus_constant (Pmode, stack_pointer_rtx,
10487 offset));
10488 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
10489 offset -= UNITS_PER_WORD;
10492 /* Move from COP0 Status to K1. */
10493 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
10494 gen_rtx_REG (SImode,
10495 COP0_STATUS_REG_NUM)));
10497 /* Right justify the RIPL in k0. */
10498 if (!cfun->machine->keep_interrupts_masked_p)
10499 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
10500 gen_rtx_REG (SImode, K0_REG_NUM),
10501 GEN_INT (CAUSE_IPL)));
10503 /* Push Status into its stack slot. */
10504 mem = gen_frame_mem (word_mode,
10505 plus_constant (Pmode, stack_pointer_rtx,
10506 offset));
10507 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
10508 offset -= UNITS_PER_WORD;
10510 /* Insert the RIPL into our copy of SR (k1) as the new IPL. */
10511 if (!cfun->machine->keep_interrupts_masked_p)
10512 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10513 GEN_INT (6),
10514 GEN_INT (SR_IPL),
10515 gen_rtx_REG (SImode, K0_REG_NUM)));
10517 if (!cfun->machine->keep_interrupts_masked_p)
10518 /* Enable interrupts by clearing the KSU ERL and EXL bits.
10519 IE is already the correct value, so we don't have to do
10520 anything explicit. */
10521 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10522 GEN_INT (4),
10523 GEN_INT (SR_EXL),
10524 gen_rtx_REG (SImode, GP_REG_FIRST)));
10525 else
10526 /* Disable interrupts by clearing the KSU, ERL, EXL,
10527 and IE bits. */
10528 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10529 GEN_INT (5),
10530 GEN_INT (SR_IE),
10531 gen_rtx_REG (SImode, GP_REG_FIRST)));
10533 else
10535 insn = gen_add3_insn (stack_pointer_rtx,
10536 stack_pointer_rtx,
10537 GEN_INT (-step1));
10538 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10539 mips_frame_barrier ();
10540 size -= step1;
10542 mips_for_each_saved_acc (size, mips_save_reg);
10543 mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
10547 /* Allocate the rest of the frame. */
10548 if (size > 0)
10550 if (SMALL_OPERAND (-size))
10551 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
10552 stack_pointer_rtx,
10553 GEN_INT (-size)))) = 1;
10554 else
10556 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
10557 if (TARGET_MIPS16)
10559 /* There are no instructions to add or subtract registers
10560 from the stack pointer, so use the frame pointer as a
10561 temporary. We should always be using a frame pointer
10562 in this case anyway. */
10563 gcc_assert (frame_pointer_needed);
10564 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10565 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
10566 hard_frame_pointer_rtx,
10567 MIPS_PROLOGUE_TEMP (Pmode)));
10568 mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
10570 else
10571 emit_insn (gen_sub3_insn (stack_pointer_rtx,
10572 stack_pointer_rtx,
10573 MIPS_PROLOGUE_TEMP (Pmode)));
10575 /* Describe the combined effect of the previous instructions. */
10576 mips_set_frame_expr
10577 (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10578 plus_constant (Pmode, stack_pointer_rtx, -size)));
10580 mips_frame_barrier ();
10583 /* Set up the frame pointer, if we're using one. */
10584 if (frame_pointer_needed)
10586 HOST_WIDE_INT offset;
10588 offset = frame->hard_frame_pointer_offset;
10589 if (offset == 0)
10591 insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10592 RTX_FRAME_RELATED_P (insn) = 1;
10594 else if (SMALL_OPERAND (offset))
10596 insn = gen_add3_insn (hard_frame_pointer_rtx,
10597 stack_pointer_rtx, GEN_INT (offset));
10598 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10600 else
10602 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
10603 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10604 emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
10605 hard_frame_pointer_rtx,
10606 MIPS_PROLOGUE_TEMP (Pmode)));
10607 mips_set_frame_expr
10608 (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
10609 plus_constant (Pmode, stack_pointer_rtx, offset)));
10613 mips_emit_loadgp ();
10615 /* Initialize the $gp save slot. */
10616 if (mips_cfun_has_cprestore_slot_p ())
10618 rtx base, mem, gp, temp;
10619 HOST_WIDE_INT offset;
10621 mips_get_cprestore_base_and_offset (&base, &offset, false);
10622 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
10623 gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10624 temp = (SMALL_OPERAND (offset)
10625 ? gen_rtx_SCRATCH (Pmode)
10626 : MIPS_PROLOGUE_TEMP (Pmode));
10627 emit_insn (PMODE_INSN (gen_potential_cprestore,
10628 (mem, GEN_INT (offset), gp, temp)));
10630 mips_get_cprestore_base_and_offset (&base, &offset, true);
10631 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
10632 emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
10635 /* We need to search back to the last use of K0 or K1. */
10636 if (cfun->machine->interrupt_handler_p)
10638 for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
10639 if (INSN_P (insn)
10640 && for_each_rtx (&PATTERN (insn), mips_kernel_reg_p, NULL))
10641 break;
10642 /* Emit a move from K1 to COP0 Status after insn. */
10643 gcc_assert (insn != NULL_RTX);
10644 emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
10645 gen_rtx_REG (SImode, K1_REG_NUM)),
10646 insn);
10649 /* If we are profiling, make sure no instructions are scheduled before
10650 the call to mcount. */
10651 if (crtl->profile)
10652 emit_insn (gen_blockage ());
10655 /* Attach all pending register saves to the previous instruction.
10656 Return that instruction. */
10658 static rtx
10659 mips_epilogue_emit_cfa_restores (void)
10661 rtx insn;
10663 insn = get_last_insn ();
10664 gcc_assert (insn && !REG_NOTES (insn));
10665 if (mips_epilogue.cfa_restores)
10667 RTX_FRAME_RELATED_P (insn) = 1;
10668 REG_NOTES (insn) = mips_epilogue.cfa_restores;
10669 mips_epilogue.cfa_restores = 0;
10671 return insn;
10674 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
10675 now at REG + OFFSET. */
10677 static void
10678 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
10680 rtx insn;
10682 insn = mips_epilogue_emit_cfa_restores ();
10683 if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
10685 RTX_FRAME_RELATED_P (insn) = 1;
10686 REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
10687 plus_constant (Pmode, reg, offset),
10688 REG_NOTES (insn));
10689 mips_epilogue.cfa_reg = reg;
10690 mips_epilogue.cfa_offset = offset;
10694 /* Emit instructions to restore register REG from slot MEM. Also update
10695 the cfa_restores list. */
10697 static void
10698 mips_restore_reg (rtx reg, rtx mem)
10700 /* There's no MIPS16 instruction to load $31 directly. Load into
10701 $7 instead and adjust the return insn appropriately. */
10702 if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
10703 reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
10704 else if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
10706 mips_add_cfa_restore (mips_subword (reg, true));
10707 mips_add_cfa_restore (mips_subword (reg, false));
10709 else
10710 mips_add_cfa_restore (reg);
10712 mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
10713 if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
10714 /* The CFA is currently defined in terms of the register whose
10715 value we have just restored. Redefine the CFA in terms of
10716 the stack pointer. */
10717 mips_epilogue_set_cfa (stack_pointer_rtx,
10718 mips_epilogue.cfa_restore_sp_offset);
10721 /* Emit code to set the stack pointer to BASE + OFFSET, given that
10722 BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
10723 BASE, if not the stack pointer, is available as a temporary. */
10725 static void
10726 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
10728 if (base == stack_pointer_rtx && offset == const0_rtx)
10729 return;
10731 mips_frame_barrier ();
10732 if (offset == const0_rtx)
10734 emit_move_insn (stack_pointer_rtx, base);
10735 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
10737 else if (TARGET_MIPS16 && base != stack_pointer_rtx)
10739 emit_insn (gen_add3_insn (base, base, offset));
10740 mips_epilogue_set_cfa (base, new_frame_size);
10741 emit_move_insn (stack_pointer_rtx, base);
10743 else
10745 emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
10746 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
10750 /* Emit any instructions needed before a return. */
10752 void
10753 mips_expand_before_return (void)
10755 /* When using a call-clobbered gp, we start out with unified call
10756 insns that include instructions to restore the gp. We then split
10757 these unified calls after reload. These split calls explicitly
10758 clobber gp, so there is no need to define
10759 PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
10761 For consistency, we should also insert an explicit clobber of $28
10762 before return insns, so that the post-reload optimizers know that
10763 the register is not live on exit. */
10764 if (TARGET_CALL_CLOBBERED_GP)
10765 emit_clobber (pic_offset_table_rtx);
10768 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
10769 says which. */
10771 void
10772 mips_expand_epilogue (bool sibcall_p)
10774 const struct mips_frame_info *frame;
10775 HOST_WIDE_INT step1, step2;
10776 rtx base, adjust, insn;
10778 if (!sibcall_p && mips_can_use_return_insn ())
10780 emit_jump_insn (gen_return ());
10781 return;
10784 /* In MIPS16 mode, if the return value should go into a floating-point
10785 register, we need to call a helper routine to copy it over. */
10786 if (mips16_cfun_returns_in_fpr_p ())
10787 mips16_copy_fpr_return_value ();
10789 /* Split the frame into two. STEP1 is the amount of stack we should
10790 deallocate before restoring the registers. STEP2 is the amount we
10791 should deallocate afterwards.
10793 Start off by assuming that no registers need to be restored. */
10794 frame = &cfun->machine->frame;
10795 step1 = frame->total_size;
10796 step2 = 0;
10798 /* Work out which register holds the frame address. */
10799 if (!frame_pointer_needed)
10800 base = stack_pointer_rtx;
10801 else
10803 base = hard_frame_pointer_rtx;
10804 step1 -= frame->hard_frame_pointer_offset;
10806 mips_epilogue.cfa_reg = base;
10807 mips_epilogue.cfa_offset = step1;
10808 mips_epilogue.cfa_restores = NULL_RTX;
10810 /* If we need to restore registers, deallocate as much stack as
10811 possible in the second step without going out of range. */
10812 if ((frame->mask | frame->fmask | frame->acc_mask) != 0
10813 || frame->num_cop0_regs > 0)
10815 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
10816 step1 -= step2;
10819 /* Get an rtx for STEP1 that we can add to BASE. */
10820 adjust = GEN_INT (step1);
10821 if (!SMALL_OPERAND (step1))
10823 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
10824 adjust = MIPS_EPILOGUE_TEMP (Pmode);
10826 mips_deallocate_stack (base, adjust, step2);
10828 /* If we're using addressing macros, $gp is implicitly used by all
10829 SYMBOL_REFs. We must emit a blockage insn before restoring $gp
10830 from the stack. */
10831 if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
10832 emit_insn (gen_blockage ());
10834 mips_epilogue.cfa_restore_sp_offset = step2;
10835 if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
10837 unsigned int regno, mask;
10838 HOST_WIDE_INT offset;
10839 rtx restore;
10841 /* Generate the restore instruction. */
10842 mask = frame->mask;
10843 restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
10845 /* Restore any other registers manually. */
10846 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
10847 if (BITSET_P (mask, regno - GP_REG_FIRST))
10849 offset -= UNITS_PER_WORD;
10850 mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
10853 /* Restore the remaining registers and deallocate the final bit
10854 of the frame. */
10855 mips_frame_barrier ();
10856 emit_insn (restore);
10857 mips_epilogue_set_cfa (stack_pointer_rtx, 0);
10859 else
10861 /* Restore the registers. */
10862 mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
10863 mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
10864 mips_restore_reg);
10866 if (cfun->machine->interrupt_handler_p)
10868 HOST_WIDE_INT offset;
10869 rtx mem;
10871 offset = frame->cop0_sp_offset - (frame->total_size - step2);
10872 if (!cfun->machine->keep_interrupts_masked_p)
10874 /* Restore the original EPC. */
10875 mem = gen_frame_mem (word_mode,
10876 plus_constant (Pmode, stack_pointer_rtx,
10877 offset));
10878 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
10879 offset -= UNITS_PER_WORD;
10881 /* Move to COP0 EPC. */
10882 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
10883 gen_rtx_REG (SImode, K0_REG_NUM)));
10886 /* Restore the original Status. */
10887 mem = gen_frame_mem (word_mode,
10888 plus_constant (Pmode, stack_pointer_rtx,
10889 offset));
10890 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
10891 offset -= UNITS_PER_WORD;
10893 /* If we don't use shoadow register set, we need to update SP. */
10894 if (!cfun->machine->use_shadow_register_set_p)
10895 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
10896 else
10897 /* The choice of position is somewhat arbitrary in this case. */
10898 mips_epilogue_emit_cfa_restores ();
10900 /* Move to COP0 Status. */
10901 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
10902 gen_rtx_REG (SImode, K0_REG_NUM)));
10904 else
10905 /* Deallocate the final bit of the frame. */
10906 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
10908 gcc_assert (!mips_epilogue.cfa_restores);
10910 /* Add in the __builtin_eh_return stack adjustment. We need to
10911 use a temporary in MIPS16 code. */
10912 if (crtl->calls_eh_return)
10914 if (TARGET_MIPS16)
10916 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
10917 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
10918 MIPS_EPILOGUE_TEMP (Pmode),
10919 EH_RETURN_STACKADJ_RTX));
10920 mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
10922 else
10923 emit_insn (gen_add3_insn (stack_pointer_rtx,
10924 stack_pointer_rtx,
10925 EH_RETURN_STACKADJ_RTX));
10928 if (!sibcall_p)
10930 mips_expand_before_return ();
10931 if (cfun->machine->interrupt_handler_p)
10933 /* Interrupt handlers generate eret or deret. */
10934 if (cfun->machine->use_debug_exception_return_p)
10935 emit_jump_insn (gen_mips_deret ());
10936 else
10937 emit_jump_insn (gen_mips_eret ());
10939 else
10941 rtx pat;
10943 /* When generating MIPS16 code, the normal
10944 mips_for_each_saved_gpr_and_fpr path will restore the return
10945 address into $7 rather than $31. */
10946 if (TARGET_MIPS16
10947 && !GENERATE_MIPS16E_SAVE_RESTORE
10948 && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
10950 /* simple_returns cannot rely on values that are only available
10951 on paths through the epilogue (because return paths that do
10952 not pass through the epilogue may nevertheless reuse a
10953 simple_return that occurs at the end of the epilogue).
10954 Use a normal return here instead. */
10955 rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
10956 pat = gen_return_internal (reg);
10958 else
10960 rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
10961 pat = gen_simple_return_internal (reg);
10963 emit_jump_insn (pat);
10967 /* Search from the beginning to the first use of K0 or K1. */
10968 if (cfun->machine->interrupt_handler_p
10969 && !cfun->machine->keep_interrupts_masked_p)
10971 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
10972 if (INSN_P (insn)
10973 && for_each_rtx (&PATTERN(insn), mips_kernel_reg_p, NULL))
10974 break;
10975 gcc_assert (insn != NULL_RTX);
10976 /* Insert disable interrupts before the first use of K0 or K1. */
10977 emit_insn_before (gen_mips_di (), insn);
10978 emit_insn_before (gen_mips_ehb (), insn);
10982 /* Return nonzero if this function is known to have a null epilogue.
10983 This allows the optimizer to omit jumps to jumps if no stack
10984 was created. */
10986 bool
10987 mips_can_use_return_insn (void)
10989 /* Interrupt handlers need to go through the epilogue. */
10990 if (cfun->machine->interrupt_handler_p)
10991 return false;
10993 if (!reload_completed)
10994 return false;
10996 if (crtl->profile)
10997 return false;
10999 /* In MIPS16 mode, a function that returns a floating-point value
11000 needs to arrange to copy the return value into the floating-point
11001 registers. */
11002 if (mips16_cfun_returns_in_fpr_p ())
11003 return false;
11005 return cfun->machine->frame.total_size == 0;
11008 /* Return true if register REGNO can store a value of mode MODE.
11009 The result of this function is cached in mips_hard_regno_mode_ok. */
11011 static bool
11012 mips_hard_regno_mode_ok_p (unsigned int regno, enum machine_mode mode)
11014 unsigned int size;
11015 enum mode_class mclass;
11017 if (mode == CCV2mode)
11018 return (ISA_HAS_8CC
11019 && ST_REG_P (regno)
11020 && (regno - ST_REG_FIRST) % 2 == 0);
11022 if (mode == CCV4mode)
11023 return (ISA_HAS_8CC
11024 && ST_REG_P (regno)
11025 && (regno - ST_REG_FIRST) % 4 == 0);
11027 if (mode == CCmode)
11029 if (!ISA_HAS_8CC)
11030 return regno == FPSW_REGNUM;
11032 return (ST_REG_P (regno)
11033 || GP_REG_P (regno)
11034 || FP_REG_P (regno));
11037 size = GET_MODE_SIZE (mode);
11038 mclass = GET_MODE_CLASS (mode);
11040 if (GP_REG_P (regno))
11041 return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
11043 if (FP_REG_P (regno)
11044 && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
11045 || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
11047 /* Allow TFmode for CCmode reloads. */
11048 if (mode == TFmode && ISA_HAS_8CC)
11049 return true;
11051 /* Allow 64-bit vector modes for Loongson-2E/2F. */
11052 if (TARGET_LOONGSON_VECTORS
11053 && (mode == V2SImode
11054 || mode == V4HImode
11055 || mode == V8QImode
11056 || mode == DImode))
11057 return true;
11059 if (mclass == MODE_FLOAT
11060 || mclass == MODE_COMPLEX_FLOAT
11061 || mclass == MODE_VECTOR_FLOAT)
11062 return size <= UNITS_PER_FPVALUE;
11064 /* Allow integer modes that fit into a single register. We need
11065 to put integers into FPRs when using instructions like CVT
11066 and TRUNC. There's no point allowing sizes smaller than a word,
11067 because the FPU has no appropriate load/store instructions. */
11068 if (mclass == MODE_INT)
11069 return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
11072 if (ACC_REG_P (regno)
11073 && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
11075 if (MD_REG_P (regno))
11077 /* After a multiplication or division, clobbering HI makes
11078 the value of LO unpredictable, and vice versa. This means
11079 that, for all interesting cases, HI and LO are effectively
11080 a single register.
11082 We model this by requiring that any value that uses HI
11083 also uses LO. */
11084 if (size <= UNITS_PER_WORD * 2)
11085 return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
11087 else
11089 /* DSP accumulators do not have the same restrictions as
11090 HI and LO, so we can treat them as normal doubleword
11091 registers. */
11092 if (size <= UNITS_PER_WORD)
11093 return true;
11095 if (size <= UNITS_PER_WORD * 2
11096 && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
11097 return true;
11101 if (ALL_COP_REG_P (regno))
11102 return mclass == MODE_INT && size <= UNITS_PER_WORD;
11104 if (regno == GOT_VERSION_REGNUM)
11105 return mode == SImode;
11107 return false;
11110 /* Implement HARD_REGNO_NREGS. */
11112 unsigned int
11113 mips_hard_regno_nregs (int regno, enum machine_mode mode)
11115 if (ST_REG_P (regno))
11116 /* The size of FP status registers is always 4, because they only hold
11117 CCmode values, and CCmode is always considered to be 4 bytes wide. */
11118 return (GET_MODE_SIZE (mode) + 3) / 4;
11120 if (FP_REG_P (regno))
11121 return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
11123 /* All other registers are word-sized. */
11124 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
11127 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
11128 in mips_hard_regno_nregs. */
11131 mips_class_max_nregs (enum reg_class rclass, enum machine_mode mode)
11133 int size;
11134 HARD_REG_SET left;
11136 size = 0x8000;
11137 COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
11138 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
11140 if (HARD_REGNO_MODE_OK (ST_REG_FIRST, mode))
11141 size = MIN (size, 4);
11142 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
11144 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
11146 if (HARD_REGNO_MODE_OK (FP_REG_FIRST, mode))
11147 size = MIN (size, UNITS_PER_FPREG);
11148 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
11150 if (!hard_reg_set_empty_p (left))
11151 size = MIN (size, UNITS_PER_WORD);
11152 return (GET_MODE_SIZE (mode) + size - 1) / size;
11155 /* Implement CANNOT_CHANGE_MODE_CLASS. */
11157 bool
11158 mips_cannot_change_mode_class (enum machine_mode from,
11159 enum machine_mode to,
11160 enum reg_class rclass)
11162 /* Allow conversions between different Loongson integer vectors,
11163 and between those vectors and DImode. */
11164 if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
11165 && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
11166 return false;
11168 /* Otherwise, there are several problems with changing the modes of
11169 values in floating-point registers:
11171 - When a multi-word value is stored in paired floating-point
11172 registers, the first register always holds the low word. We
11173 therefore can't allow FPRs to change between single-word and
11174 multi-word modes on big-endian targets.
11176 - GCC assumes that each word of a multiword register can be
11177 accessed individually using SUBREGs. This is not true for
11178 floating-point registers if they are bigger than a word.
11180 - Loading a 32-bit value into a 64-bit floating-point register
11181 will not sign-extend the value, despite what LOAD_EXTEND_OP
11182 says. We can't allow FPRs to change from SImode to a wider
11183 mode on 64-bit targets.
11185 - If the FPU has already interpreted a value in one format, we
11186 must not ask it to treat the value as having a different
11187 format.
11189 We therefore disallow all mode changes involving FPRs. */
11191 return reg_classes_intersect_p (FP_REGS, rclass);
11194 /* Implement target hook small_register_classes_for_mode_p. */
11196 static bool
11197 mips_small_register_classes_for_mode_p (enum machine_mode mode
11198 ATTRIBUTE_UNUSED)
11200 return TARGET_MIPS16;
11203 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction. */
11205 static bool
11206 mips_mode_ok_for_mov_fmt_p (enum machine_mode mode)
11208 switch (mode)
11210 case SFmode:
11211 return TARGET_HARD_FLOAT;
11213 case DFmode:
11214 return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
11216 case V2SFmode:
11217 return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
11219 default:
11220 return false;
11224 /* Implement MODES_TIEABLE_P. */
11226 bool
11227 mips_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
11229 /* FPRs allow no mode punning, so it's not worth tying modes if we'd
11230 prefer to put one of them in FPRs. */
11231 return (mode1 == mode2
11232 || (!mips_mode_ok_for_mov_fmt_p (mode1)
11233 && !mips_mode_ok_for_mov_fmt_p (mode2)));
11236 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
11238 static reg_class_t
11239 mips_preferred_reload_class (rtx x, reg_class_t rclass)
11241 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
11242 return LEA_REGS;
11244 if (reg_class_subset_p (FP_REGS, rclass)
11245 && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
11246 return FP_REGS;
11248 if (reg_class_subset_p (GR_REGS, rclass))
11249 rclass = GR_REGS;
11251 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
11252 rclass = M16_REGS;
11254 return rclass;
11257 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
11258 Return a "canonical" class to represent it in later calculations. */
11260 static reg_class_t
11261 mips_canonicalize_move_class (reg_class_t rclass)
11263 /* All moves involving accumulator registers have the same cost. */
11264 if (reg_class_subset_p (rclass, ACC_REGS))
11265 rclass = ACC_REGS;
11267 /* Likewise promote subclasses of general registers to the most
11268 interesting containing class. */
11269 if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
11270 rclass = M16_REGS;
11271 else if (reg_class_subset_p (rclass, GENERAL_REGS))
11272 rclass = GENERAL_REGS;
11274 return rclass;
11277 /* Return the cost of moving a value of mode MODE from a register of
11278 class FROM to a GPR. Return 0 for classes that are unions of other
11279 classes handled by this function. */
11281 static int
11282 mips_move_to_gpr_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
11283 reg_class_t from)
11285 switch (from)
11287 case GENERAL_REGS:
11288 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
11289 return 2;
11291 case ACC_REGS:
11292 /* MFLO and MFHI. */
11293 return 6;
11295 case FP_REGS:
11296 /* MFC1, etc. */
11297 return 4;
11299 case ST_REGS:
11300 /* LUI followed by MOVF. */
11301 return 4;
11303 case COP0_REGS:
11304 case COP2_REGS:
11305 case COP3_REGS:
11306 /* This choice of value is historical. */
11307 return 5;
11309 default:
11310 return 0;
11314 /* Return the cost of moving a value of mode MODE from a GPR to a
11315 register of class TO. Return 0 for classes that are unions of
11316 other classes handled by this function. */
11318 static int
11319 mips_move_from_gpr_cost (enum machine_mode mode, reg_class_t to)
11321 switch (to)
11323 case GENERAL_REGS:
11324 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
11325 return 2;
11327 case ACC_REGS:
11328 /* MTLO and MTHI. */
11329 return 6;
11331 case FP_REGS:
11332 /* MTC1, etc. */
11333 return 4;
11335 case ST_REGS:
11336 /* A secondary reload through an FPR scratch. */
11337 return (mips_register_move_cost (mode, GENERAL_REGS, FP_REGS)
11338 + mips_register_move_cost (mode, FP_REGS, ST_REGS));
11340 case COP0_REGS:
11341 case COP2_REGS:
11342 case COP3_REGS:
11343 /* This choice of value is historical. */
11344 return 5;
11346 default:
11347 return 0;
11351 /* Implement TARGET_REGISTER_MOVE_COST. Return 0 for classes that are the
11352 maximum of the move costs for subclasses; regclass will work out
11353 the maximum for us. */
11355 static int
11356 mips_register_move_cost (enum machine_mode mode,
11357 reg_class_t from, reg_class_t to)
11359 reg_class_t dregs;
11360 int cost1, cost2;
11362 from = mips_canonicalize_move_class (from);
11363 to = mips_canonicalize_move_class (to);
11365 /* Handle moves that can be done without using general-purpose registers. */
11366 if (from == FP_REGS)
11368 if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
11369 /* MOV.FMT. */
11370 return 4;
11371 if (to == ST_REGS)
11372 /* The sequence generated by mips_expand_fcc_reload. */
11373 return 8;
11376 /* Handle cases in which only one class deviates from the ideal. */
11377 dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
11378 if (from == dregs)
11379 return mips_move_from_gpr_cost (mode, to);
11380 if (to == dregs)
11381 return mips_move_to_gpr_cost (mode, from);
11383 /* Handles cases that require a GPR temporary. */
11384 cost1 = mips_move_to_gpr_cost (mode, from);
11385 if (cost1 != 0)
11387 cost2 = mips_move_from_gpr_cost (mode, to);
11388 if (cost2 != 0)
11389 return cost1 + cost2;
11392 return 0;
11395 /* Implement TARGET_MEMORY_MOVE_COST. */
11397 static int
11398 mips_memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
11400 return (mips_cost->memory_latency
11401 + memory_move_secondary_cost (mode, rclass, in));
11404 /* Return the register class required for a secondary register when
11405 copying between one of the registers in RCLASS and value X, which
11406 has mode MODE. X is the source of the move if IN_P, otherwise it
11407 is the destination. Return NO_REGS if no secondary register is
11408 needed. */
11410 enum reg_class
11411 mips_secondary_reload_class (enum reg_class rclass,
11412 enum machine_mode mode, rtx x, bool in_p)
11414 int regno;
11416 /* If X is a constant that cannot be loaded into $25, it must be loaded
11417 into some other GPR. No other register class allows a direct move. */
11418 if (mips_dangerous_for_la25_p (x))
11419 return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
11421 regno = true_regnum (x);
11422 if (TARGET_MIPS16)
11424 /* In MIPS16 mode, every move must involve a member of M16_REGS. */
11425 if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
11426 return M16_REGS;
11428 return NO_REGS;
11431 /* Copying from accumulator registers to anywhere other than a general
11432 register requires a temporary general register. */
11433 if (reg_class_subset_p (rclass, ACC_REGS))
11434 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
11435 if (ACC_REG_P (regno))
11436 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11438 /* We can only copy a value to a condition code register from a
11439 floating-point register, and even then we require a scratch
11440 floating-point register. We can only copy a value out of a
11441 condition-code register into a general register. */
11442 if (reg_class_subset_p (rclass, ST_REGS))
11444 if (in_p)
11445 return FP_REGS;
11446 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
11448 if (ST_REG_P (regno))
11450 if (!in_p)
11451 return FP_REGS;
11452 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11455 if (reg_class_subset_p (rclass, FP_REGS))
11457 if (MEM_P (x)
11458 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8))
11459 /* In this case we can use lwc1, swc1, ldc1 or sdc1. We'll use
11460 pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported. */
11461 return NO_REGS;
11463 if (GP_REG_P (regno) || x == CONST0_RTX (mode))
11464 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
11465 return NO_REGS;
11467 if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
11468 /* We can force the constant to memory and use lwc1
11469 and ldc1. As above, we will use pairs of lwc1s if
11470 ldc1 is not supported. */
11471 return NO_REGS;
11473 if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
11474 /* In this case we can use mov.fmt. */
11475 return NO_REGS;
11477 /* Otherwise, we need to reload through an integer register. */
11478 return GR_REGS;
11480 if (FP_REG_P (regno))
11481 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11483 return NO_REGS;
11486 /* Implement TARGET_MODE_REP_EXTENDED. */
11488 static int
11489 mips_mode_rep_extended (enum machine_mode mode, enum machine_mode mode_rep)
11491 /* On 64-bit targets, SImode register values are sign-extended to DImode. */
11492 if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
11493 return SIGN_EXTEND;
11495 return UNKNOWN;
11498 /* Implement TARGET_VALID_POINTER_MODE. */
11500 static bool
11501 mips_valid_pointer_mode (enum machine_mode mode)
11503 return mode == SImode || (TARGET_64BIT && mode == DImode);
11506 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P. */
11508 static bool
11509 mips_vector_mode_supported_p (enum machine_mode mode)
11511 switch (mode)
11513 case V2SFmode:
11514 return TARGET_PAIRED_SINGLE_FLOAT;
11516 case V2HImode:
11517 case V4QImode:
11518 case V2HQmode:
11519 case V2UHQmode:
11520 case V2HAmode:
11521 case V2UHAmode:
11522 case V4QQmode:
11523 case V4UQQmode:
11524 return TARGET_DSP;
11526 case V2SImode:
11527 case V4HImode:
11528 case V8QImode:
11529 return TARGET_LOONGSON_VECTORS;
11531 default:
11532 return false;
11536 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
11538 static bool
11539 mips_scalar_mode_supported_p (enum machine_mode mode)
11541 if (ALL_FIXED_POINT_MODE_P (mode)
11542 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
11543 return true;
11545 return default_scalar_mode_supported_p (mode);
11548 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
11550 static enum machine_mode
11551 mips_preferred_simd_mode (enum machine_mode mode ATTRIBUTE_UNUSED)
11553 if (TARGET_PAIRED_SINGLE_FLOAT
11554 && mode == SFmode)
11555 return V2SFmode;
11556 return word_mode;
11559 /* Implement TARGET_INIT_LIBFUNCS. */
11561 static void
11562 mips_init_libfuncs (void)
11564 if (TARGET_FIX_VR4120)
11566 /* Register the special divsi3 and modsi3 functions needed to work
11567 around VR4120 division errata. */
11568 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
11569 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
11572 if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
11574 /* Register the MIPS16 -mhard-float stubs. */
11575 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
11576 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
11577 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
11578 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
11580 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
11581 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
11582 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
11583 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
11584 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
11585 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
11586 set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
11588 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
11589 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
11590 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
11592 if (TARGET_DOUBLE_FLOAT)
11594 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
11595 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
11596 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
11597 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
11599 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
11600 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
11601 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
11602 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
11603 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
11604 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
11605 set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
11607 set_conv_libfunc (sext_optab, DFmode, SFmode,
11608 "__mips16_extendsfdf2");
11609 set_conv_libfunc (trunc_optab, SFmode, DFmode,
11610 "__mips16_truncdfsf2");
11611 set_conv_libfunc (sfix_optab, SImode, DFmode,
11612 "__mips16_fix_truncdfsi");
11613 set_conv_libfunc (sfloat_optab, DFmode, SImode,
11614 "__mips16_floatsidf");
11615 set_conv_libfunc (ufloat_optab, DFmode, SImode,
11616 "__mips16_floatunsidf");
11620 /* The MIPS16 ISA does not have an encoding for "sync", so we rely
11621 on an external non-MIPS16 routine to implement __sync_synchronize.
11622 Similarly for the rest of the ll/sc libfuncs. */
11623 if (TARGET_MIPS16)
11625 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
11626 init_sync_libfuncs (UNITS_PER_WORD);
11630 /* Build up a multi-insn sequence that loads label TARGET into $AT. */
11632 static void
11633 mips_process_load_label (rtx target)
11635 rtx base, gp, intop;
11636 HOST_WIDE_INT offset;
11638 mips_multi_start ();
11639 switch (mips_abi)
11641 case ABI_N32:
11642 mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
11643 mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
11644 break;
11646 case ABI_64:
11647 mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
11648 mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
11649 break;
11651 default:
11652 gp = pic_offset_table_rtx;
11653 if (mips_cfun_has_cprestore_slot_p ())
11655 gp = gen_rtx_REG (Pmode, AT_REGNUM);
11656 mips_get_cprestore_base_and_offset (&base, &offset, true);
11657 if (!SMALL_OPERAND (offset))
11659 intop = GEN_INT (CONST_HIGH_PART (offset));
11660 mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
11661 mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
11663 base = gp;
11664 offset = CONST_LOW_PART (offset);
11666 intop = GEN_INT (offset);
11667 if (ISA_HAS_LOAD_DELAY)
11668 mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
11669 else
11670 mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
11672 if (ISA_HAS_LOAD_DELAY)
11673 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
11674 else
11675 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
11676 mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
11677 break;
11681 /* Return the number of instructions needed to load a label into $AT. */
11683 static unsigned int
11684 mips_load_label_num_insns (void)
11686 if (cfun->machine->load_label_num_insns == 0)
11688 mips_process_load_label (pc_rtx);
11689 cfun->machine->load_label_num_insns = mips_multi_num_insns;
11691 return cfun->machine->load_label_num_insns;
11694 /* Emit an asm sequence to start a noat block and load the address
11695 of a label into $1. */
11697 void
11698 mips_output_load_label (rtx target)
11700 mips_push_asm_switch (&mips_noat);
11701 if (TARGET_EXPLICIT_RELOCS)
11703 mips_process_load_label (target);
11704 mips_multi_write ();
11706 else
11708 if (Pmode == DImode)
11709 output_asm_insn ("dla\t%@,%0", &target);
11710 else
11711 output_asm_insn ("la\t%@,%0", &target);
11715 /* Return the length of INSN. LENGTH is the initial length computed by
11716 attributes in the machine-description file. */
11719 mips_adjust_insn_length (rtx insn, int length)
11721 /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
11722 of a PIC long-branch sequence. Substitute the correct value. */
11723 if (length == MAX_PIC_BRANCH_LENGTH
11724 && INSN_CODE (insn) >= 0
11725 && get_attr_type (insn) == TYPE_BRANCH)
11727 /* Add the branch-over instruction and its delay slot, if this
11728 is a conditional branch. */
11729 length = simplejump_p (insn) ? 0 : 8;
11731 /* Load the label into $AT and jump to it. Ignore the delay
11732 slot of the jump. */
11733 length += 4 * mips_load_label_num_insns() + 4;
11736 /* A unconditional jump has an unfilled delay slot if it is not part
11737 of a sequence. A conditional jump normally has a delay slot, but
11738 does not on MIPS16. */
11739 if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
11740 length += 4;
11742 /* See how many nops might be needed to avoid hardware hazards. */
11743 if (!cfun->machine->ignore_hazard_length_p && INSN_CODE (insn) >= 0)
11744 switch (get_attr_hazard (insn))
11746 case HAZARD_NONE:
11747 break;
11749 case HAZARD_DELAY:
11750 length += 4;
11751 break;
11753 case HAZARD_HILO:
11754 length += 8;
11755 break;
11758 /* In order to make it easier to share MIPS16 and non-MIPS16 patterns,
11759 the .md file length attributes are 4-based for both modes.
11760 Adjust the MIPS16 ones here. */
11761 if (TARGET_MIPS16)
11762 length /= 2;
11764 return length;
11767 /* Return the assembly code for INSN, which has the operands given by
11768 OPERANDS, and which branches to OPERANDS[0] if some condition is true.
11769 BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
11770 is in range of a direct branch. BRANCH_IF_FALSE is an inverted
11771 version of BRANCH_IF_TRUE. */
11773 const char *
11774 mips_output_conditional_branch (rtx insn, rtx *operands,
11775 const char *branch_if_true,
11776 const char *branch_if_false)
11778 unsigned int length;
11779 rtx taken, not_taken;
11781 gcc_assert (LABEL_P (operands[0]));
11783 length = get_attr_length (insn);
11784 if (length <= 8)
11786 /* Just a simple conditional branch. */
11787 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
11788 return branch_if_true;
11791 /* Generate a reversed branch around a direct jump. This fallback does
11792 not use branch-likely instructions. */
11793 mips_branch_likely = false;
11794 not_taken = gen_label_rtx ();
11795 taken = operands[0];
11797 /* Generate the reversed branch to NOT_TAKEN. */
11798 operands[0] = not_taken;
11799 output_asm_insn (branch_if_false, operands);
11801 /* If INSN has a delay slot, we must provide delay slots for both the
11802 branch to NOT_TAKEN and the conditional jump. We must also ensure
11803 that INSN's delay slot is executed in the appropriate cases. */
11804 if (final_sequence)
11806 /* This first delay slot will always be executed, so use INSN's
11807 delay slot if is not annulled. */
11808 if (!INSN_ANNULLED_BRANCH_P (insn))
11810 final_scan_insn (XVECEXP (final_sequence, 0, 1),
11811 asm_out_file, optimize, 1, NULL);
11812 INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
11814 else
11815 output_asm_insn ("nop", 0);
11816 fprintf (asm_out_file, "\n");
11819 /* Output the unconditional branch to TAKEN. */
11820 if (TARGET_ABSOLUTE_JUMPS)
11821 output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
11822 else
11824 mips_output_load_label (taken);
11825 output_asm_insn ("jr\t%@%]%/", 0);
11828 /* Now deal with its delay slot; see above. */
11829 if (final_sequence)
11831 /* This delay slot will only be executed if the branch is taken.
11832 Use INSN's delay slot if is annulled. */
11833 if (INSN_ANNULLED_BRANCH_P (insn))
11835 final_scan_insn (XVECEXP (final_sequence, 0, 1),
11836 asm_out_file, optimize, 1, NULL);
11837 INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
11839 else
11840 output_asm_insn ("nop", 0);
11841 fprintf (asm_out_file, "\n");
11844 /* Output NOT_TAKEN. */
11845 targetm.asm_out.internal_label (asm_out_file, "L",
11846 CODE_LABEL_NUMBER (not_taken));
11847 return "";
11850 /* Return the assembly code for INSN, which branches to OPERANDS[0]
11851 if some ordering condition is true. The condition is given by
11852 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
11853 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
11854 its second is always zero. */
11856 const char *
11857 mips_output_order_conditional_branch (rtx insn, rtx *operands, bool inverted_p)
11859 const char *branch[2];
11861 /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
11862 Make BRANCH[0] branch on the inverse condition. */
11863 switch (GET_CODE (operands[1]))
11865 /* These cases are equivalent to comparisons against zero. */
11866 case LEU:
11867 inverted_p = !inverted_p;
11868 /* Fall through. */
11869 case GTU:
11870 branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
11871 branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
11872 break;
11874 /* These cases are always true or always false. */
11875 case LTU:
11876 inverted_p = !inverted_p;
11877 /* Fall through. */
11878 case GEU:
11879 branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
11880 branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
11881 break;
11883 default:
11884 branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
11885 branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
11886 break;
11888 return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
11891 /* Start a block of code that needs access to the LL, SC and SYNC
11892 instructions. */
11894 static void
11895 mips_start_ll_sc_sync_block (void)
11897 if (!ISA_HAS_LL_SC)
11899 output_asm_insn (".set\tpush", 0);
11900 output_asm_insn (".set\tmips2", 0);
11904 /* End a block started by mips_start_ll_sc_sync_block. */
11906 static void
11907 mips_end_ll_sc_sync_block (void)
11909 if (!ISA_HAS_LL_SC)
11910 output_asm_insn (".set\tpop", 0);
11913 /* Output and/or return the asm template for a sync instruction. */
11915 const char *
11916 mips_output_sync (void)
11918 mips_start_ll_sc_sync_block ();
11919 output_asm_insn ("sync", 0);
11920 mips_end_ll_sc_sync_block ();
11921 return "";
11924 /* Return the asm template associated with sync_insn1 value TYPE.
11925 IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation. */
11927 static const char *
11928 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
11930 switch (type)
11932 case SYNC_INSN1_MOVE:
11933 return "move\t%0,%z2";
11934 case SYNC_INSN1_LI:
11935 return "li\t%0,%2";
11936 case SYNC_INSN1_ADDU:
11937 return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
11938 case SYNC_INSN1_ADDIU:
11939 return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
11940 case SYNC_INSN1_SUBU:
11941 return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
11942 case SYNC_INSN1_AND:
11943 return "and\t%0,%1,%z2";
11944 case SYNC_INSN1_ANDI:
11945 return "andi\t%0,%1,%2";
11946 case SYNC_INSN1_OR:
11947 return "or\t%0,%1,%z2";
11948 case SYNC_INSN1_ORI:
11949 return "ori\t%0,%1,%2";
11950 case SYNC_INSN1_XOR:
11951 return "xor\t%0,%1,%z2";
11952 case SYNC_INSN1_XORI:
11953 return "xori\t%0,%1,%2";
11955 gcc_unreachable ();
11958 /* Return the asm template associated with sync_insn2 value TYPE. */
11960 static const char *
11961 mips_sync_insn2_template (enum attr_sync_insn2 type)
11963 switch (type)
11965 case SYNC_INSN2_NOP:
11966 gcc_unreachable ();
11967 case SYNC_INSN2_AND:
11968 return "and\t%0,%1,%z2";
11969 case SYNC_INSN2_XOR:
11970 return "xor\t%0,%1,%z2";
11971 case SYNC_INSN2_NOT:
11972 return "nor\t%0,%1,%.";
11974 gcc_unreachable ();
11977 /* OPERANDS are the operands to a sync loop instruction and INDEX is
11978 the value of the one of the sync_* attributes. Return the operand
11979 referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
11980 have the associated attribute. */
11982 static rtx
11983 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
11985 if (index > 0)
11986 default_value = operands[index - 1];
11987 return default_value;
11990 /* INSN is a sync loop with operands OPERANDS. Build up a multi-insn
11991 sequence for it. */
11993 static void
11994 mips_process_sync_loop (rtx insn, rtx *operands)
11996 rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
11997 rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3;
11998 unsigned int tmp3_insn;
11999 enum attr_sync_insn1 insn1;
12000 enum attr_sync_insn2 insn2;
12001 bool is_64bit_p;
12003 /* Read an operand from the sync_WHAT attribute and store it in
12004 variable WHAT. DEFAULT is the default value if no attribute
12005 is specified. */
12006 #define READ_OPERAND(WHAT, DEFAULT) \
12007 WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
12008 DEFAULT)
12010 /* Read the memory. */
12011 READ_OPERAND (mem, 0);
12012 gcc_assert (mem);
12013 is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
12015 /* Read the other attributes. */
12016 at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
12017 READ_OPERAND (oldval, at);
12018 READ_OPERAND (newval, at);
12019 READ_OPERAND (inclusive_mask, 0);
12020 READ_OPERAND (exclusive_mask, 0);
12021 READ_OPERAND (required_oldval, 0);
12022 READ_OPERAND (insn1_op2, 0);
12023 insn1 = get_attr_sync_insn1 (insn);
12024 insn2 = get_attr_sync_insn2 (insn);
12026 mips_multi_start ();
12028 /* Output the release side of the memory barrier. */
12029 if (get_attr_sync_release_barrier (insn) == SYNC_RELEASE_BARRIER_YES)
12031 if (required_oldval == 0 && TARGET_OCTEON)
12033 /* Octeon doesn't reorder reads, so a full barrier can be
12034 created by using SYNCW to order writes combined with the
12035 write from the following SC. When the SC successfully
12036 completes, we know that all preceding writes are also
12037 committed to the coherent memory system. It is possible
12038 for a single SYNCW to fail, but a pair of them will never
12039 fail, so we use two. */
12040 mips_multi_add_insn ("syncw", NULL);
12041 mips_multi_add_insn ("syncw", NULL);
12043 else
12044 mips_multi_add_insn ("sync", NULL);
12047 /* Output the branch-back label. */
12048 mips_multi_add_label ("1:");
12050 /* OLDVAL = *MEM. */
12051 mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
12052 oldval, mem, NULL);
12054 /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2. */
12055 if (required_oldval)
12057 if (inclusive_mask == 0)
12058 tmp1 = oldval;
12059 else
12061 gcc_assert (oldval != at);
12062 mips_multi_add_insn ("and\t%0,%1,%2",
12063 at, oldval, inclusive_mask, NULL);
12064 tmp1 = at;
12066 mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
12069 /* $TMP1 = OLDVAL & EXCLUSIVE_MASK. */
12070 if (exclusive_mask == 0)
12071 tmp1 = const0_rtx;
12072 else
12074 gcc_assert (oldval != at);
12075 mips_multi_add_insn ("and\t%0,%1,%z2",
12076 at, oldval, exclusive_mask, NULL);
12077 tmp1 = at;
12080 /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
12082 We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
12083 at least one instruction in that case. */
12084 if (insn1 == SYNC_INSN1_MOVE
12085 && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
12086 tmp2 = insn1_op2;
12087 else
12089 mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
12090 newval, oldval, insn1_op2, NULL);
12091 tmp2 = newval;
12094 /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK). */
12095 if (insn2 == SYNC_INSN2_NOP)
12096 tmp3 = tmp2;
12097 else
12099 mips_multi_add_insn (mips_sync_insn2_template (insn2),
12100 newval, tmp2, inclusive_mask, NULL);
12101 tmp3 = newval;
12103 tmp3_insn = mips_multi_last_index ();
12105 /* $AT = $TMP1 | $TMP3. */
12106 if (tmp1 == const0_rtx || tmp3 == const0_rtx)
12108 mips_multi_set_operand (tmp3_insn, 0, at);
12109 tmp3 = at;
12111 else
12113 gcc_assert (tmp1 != tmp3);
12114 mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
12117 /* if (!commit (*MEM = $AT)) goto 1.
12119 This will sometimes be a delayed branch; see the write code below
12120 for details. */
12121 mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
12122 mips_multi_add_insn ("beq%?\t%0,%.,1b", at, NULL);
12124 /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot]. */
12125 if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
12127 mips_multi_copy_insn (tmp3_insn);
12128 mips_multi_set_operand (mips_multi_last_index (), 0, newval);
12130 else
12131 mips_multi_add_insn ("nop", NULL);
12133 /* Output the acquire side of the memory barrier. */
12134 if (TARGET_SYNC_AFTER_SC)
12135 mips_multi_add_insn ("sync", NULL);
12137 /* Output the exit label, if needed. */
12138 if (required_oldval)
12139 mips_multi_add_label ("2:");
12141 #undef READ_OPERAND
12144 /* Output and/or return the asm template for sync loop INSN, which has
12145 the operands given by OPERANDS. */
12147 const char *
12148 mips_output_sync_loop (rtx insn, rtx *operands)
12150 mips_process_sync_loop (insn, operands);
12152 /* Use branch-likely instructions to work around the LL/SC R10000
12153 errata. */
12154 mips_branch_likely = TARGET_FIX_R10000;
12156 mips_push_asm_switch (&mips_noreorder);
12157 mips_push_asm_switch (&mips_nomacro);
12158 mips_push_asm_switch (&mips_noat);
12159 mips_start_ll_sc_sync_block ();
12161 mips_multi_write ();
12163 mips_end_ll_sc_sync_block ();
12164 mips_pop_asm_switch (&mips_noat);
12165 mips_pop_asm_switch (&mips_nomacro);
12166 mips_pop_asm_switch (&mips_noreorder);
12168 return "";
12171 /* Return the number of individual instructions in sync loop INSN,
12172 which has the operands given by OPERANDS. */
12174 unsigned int
12175 mips_sync_loop_insns (rtx insn, rtx *operands)
12177 mips_process_sync_loop (insn, operands);
12178 return mips_multi_num_insns;
12181 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
12182 the operands given by OPERANDS. Add in a divide-by-zero check if needed.
12184 When working around R4000 and R4400 errata, we need to make sure that
12185 the division is not immediately followed by a shift[1][2]. We also
12186 need to stop the division from being put into a branch delay slot[3].
12187 The easiest way to avoid both problems is to add a nop after the
12188 division. When a divide-by-zero check is needed, this nop can be
12189 used to fill the branch delay slot.
12191 [1] If a double-word or a variable shift executes immediately
12192 after starting an integer division, the shift may give an
12193 incorrect result. See quotations of errata #16 and #28 from
12194 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12195 in mips.md for details.
12197 [2] A similar bug to [1] exists for all revisions of the
12198 R4000 and the R4400 when run in an MC configuration.
12199 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
12201 "19. In this following sequence:
12203 ddiv (or ddivu or div or divu)
12204 dsll32 (or dsrl32, dsra32)
12206 if an MPT stall occurs, while the divide is slipping the cpu
12207 pipeline, then the following double shift would end up with an
12208 incorrect result.
12210 Workaround: The compiler needs to avoid generating any
12211 sequence with divide followed by extended double shift."
12213 This erratum is also present in "MIPS R4400MC Errata, Processor
12214 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
12215 & 3.0" as errata #10 and #4, respectively.
12217 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12218 (also valid for MIPS R4000MC processors):
12220 "52. R4000SC: This bug does not apply for the R4000PC.
12222 There are two flavors of this bug:
12224 1) If the instruction just after divide takes an RF exception
12225 (tlb-refill, tlb-invalid) and gets an instruction cache
12226 miss (both primary and secondary) and the line which is
12227 currently in secondary cache at this index had the first
12228 data word, where the bits 5..2 are set, then R4000 would
12229 get a wrong result for the div.
12233 div r8, r9
12234 ------------------- # end-of page. -tlb-refill
12238 div r8, r9
12239 ------------------- # end-of page. -tlb-invalid
12242 2) If the divide is in the taken branch delay slot, where the
12243 target takes RF exception and gets an I-cache miss for the
12244 exception vector or where I-cache miss occurs for the
12245 target address, under the above mentioned scenarios, the
12246 div would get wrong results.
12249 j r2 # to next page mapped or unmapped
12250 div r8,r9 # this bug would be there as long
12251 # as there is an ICache miss and
12252 nop # the "data pattern" is present
12255 beq r0, r0, NextPage # to Next page
12256 div r8,r9
12259 This bug is present for div, divu, ddiv, and ddivu
12260 instructions.
12262 Workaround: For item 1), OS could make sure that the next page
12263 after the divide instruction is also mapped. For item 2), the
12264 compiler could make sure that the divide instruction is not in
12265 the branch delay slot."
12267 These processors have PRId values of 0x00004220 and 0x00004300 for
12268 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
12270 const char *
12271 mips_output_division (const char *division, rtx *operands)
12273 const char *s;
12275 s = division;
12276 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
12278 output_asm_insn (s, operands);
12279 s = "nop";
12281 if (TARGET_CHECK_ZERO_DIV)
12283 if (TARGET_MIPS16)
12285 output_asm_insn (s, operands);
12286 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
12288 else if (GENERATE_DIVIDE_TRAPS)
12290 /* Avoid long replay penalty on load miss by putting the trap before
12291 the divide. */
12292 if (TUNE_74K)
12293 output_asm_insn ("teq\t%2,%.,7", operands);
12294 else
12296 output_asm_insn (s, operands);
12297 s = "teq\t%2,%.,7";
12300 else
12302 output_asm_insn ("%(bne\t%2,%.,1f", operands);
12303 output_asm_insn (s, operands);
12304 s = "break\t7%)\n1:";
12307 return s;
12310 /* Return true if IN_INSN is a multiply-add or multiply-subtract
12311 instruction and if OUT_INSN assigns to the accumulator operand. */
12313 bool
12314 mips_linked_madd_p (rtx out_insn, rtx in_insn)
12316 rtx x;
12318 x = single_set (in_insn);
12319 if (x == 0)
12320 return false;
12322 x = SET_SRC (x);
12324 if (GET_CODE (x) == PLUS
12325 && GET_CODE (XEXP (x, 0)) == MULT
12326 && reg_set_p (XEXP (x, 1), out_insn))
12327 return true;
12329 if (GET_CODE (x) == MINUS
12330 && GET_CODE (XEXP (x, 1)) == MULT
12331 && reg_set_p (XEXP (x, 0), out_insn))
12332 return true;
12334 return false;
12337 /* True if the dependency between OUT_INSN and IN_INSN is on the store
12338 data rather than the address. We need this because the cprestore
12339 pattern is type "store", but is defined using an UNSPEC_VOLATILE,
12340 which causes the default routine to abort. We just return false
12341 for that case. */
12343 bool
12344 mips_store_data_bypass_p (rtx out_insn, rtx in_insn)
12346 if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
12347 return false;
12349 return !store_data_bypass_p (out_insn, in_insn);
12353 /* Variables and flags used in scheduler hooks when tuning for
12354 Loongson 2E/2F. */
12355 static struct
12357 /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
12358 strategy. */
12360 /* If true, then next ALU1/2 instruction will go to ALU1. */
12361 bool alu1_turn_p;
12363 /* If true, then next FALU1/2 unstruction will go to FALU1. */
12364 bool falu1_turn_p;
12366 /* Codes to query if [f]alu{1,2}_core units are subscribed or not. */
12367 int alu1_core_unit_code;
12368 int alu2_core_unit_code;
12369 int falu1_core_unit_code;
12370 int falu2_core_unit_code;
12372 /* True if current cycle has a multi instruction.
12373 This flag is used in mips_ls2_dfa_post_advance_cycle. */
12374 bool cycle_has_multi_p;
12376 /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
12377 These are used in mips_ls2_dfa_post_advance_cycle to initialize
12378 DFA state.
12379 E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
12380 instruction to go ALU1. */
12381 rtx alu1_turn_enabled_insn;
12382 rtx alu2_turn_enabled_insn;
12383 rtx falu1_turn_enabled_insn;
12384 rtx falu2_turn_enabled_insn;
12385 } mips_ls2;
12387 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
12388 dependencies have no cost, except on the 20Kc where output-dependence
12389 is treated like input-dependence. */
12391 static int
12392 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
12393 rtx dep ATTRIBUTE_UNUSED, int cost)
12395 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
12396 && TUNE_20KC)
12397 return cost;
12398 if (REG_NOTE_KIND (link) != 0)
12399 return 0;
12400 return cost;
12403 /* Return the number of instructions that can be issued per cycle. */
12405 static int
12406 mips_issue_rate (void)
12408 switch (mips_tune)
12410 case PROCESSOR_74KC:
12411 case PROCESSOR_74KF2_1:
12412 case PROCESSOR_74KF1_1:
12413 case PROCESSOR_74KF3_2:
12414 /* The 74k is not strictly quad-issue cpu, but can be seen as one
12415 by the scheduler. It can issue 1 ALU, 1 AGEN and 2 FPU insns,
12416 but in reality only a maximum of 3 insns can be issued as
12417 floating-point loads and stores also require a slot in the
12418 AGEN pipe. */
12419 case PROCESSOR_R10000:
12420 /* All R10K Processors are quad-issue (being the first MIPS
12421 processors to support this feature). */
12422 return 4;
12424 case PROCESSOR_20KC:
12425 case PROCESSOR_R4130:
12426 case PROCESSOR_R5400:
12427 case PROCESSOR_R5500:
12428 case PROCESSOR_R7000:
12429 case PROCESSOR_R9000:
12430 case PROCESSOR_OCTEON:
12431 case PROCESSOR_OCTEON2:
12432 return 2;
12434 case PROCESSOR_SB1:
12435 case PROCESSOR_SB1A:
12436 /* This is actually 4, but we get better performance if we claim 3.
12437 This is partly because of unwanted speculative code motion with the
12438 larger number, and partly because in most common cases we can't
12439 reach the theoretical max of 4. */
12440 return 3;
12442 case PROCESSOR_LOONGSON_2E:
12443 case PROCESSOR_LOONGSON_2F:
12444 case PROCESSOR_LOONGSON_3A:
12445 return 4;
12447 default:
12448 return 1;
12452 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2. */
12454 static void
12455 mips_ls2_init_dfa_post_cycle_insn (void)
12457 start_sequence ();
12458 emit_insn (gen_ls2_alu1_turn_enabled_insn ());
12459 mips_ls2.alu1_turn_enabled_insn = get_insns ();
12460 end_sequence ();
12462 start_sequence ();
12463 emit_insn (gen_ls2_alu2_turn_enabled_insn ());
12464 mips_ls2.alu2_turn_enabled_insn = get_insns ();
12465 end_sequence ();
12467 start_sequence ();
12468 emit_insn (gen_ls2_falu1_turn_enabled_insn ());
12469 mips_ls2.falu1_turn_enabled_insn = get_insns ();
12470 end_sequence ();
12472 start_sequence ();
12473 emit_insn (gen_ls2_falu2_turn_enabled_insn ());
12474 mips_ls2.falu2_turn_enabled_insn = get_insns ();
12475 end_sequence ();
12477 mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
12478 mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
12479 mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
12480 mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
12483 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
12484 Init data used in mips_dfa_post_advance_cycle. */
12486 static void
12487 mips_init_dfa_post_cycle_insn (void)
12489 if (TUNE_LOONGSON_2EF)
12490 mips_ls2_init_dfa_post_cycle_insn ();
12493 /* Initialize STATE when scheduling for Loongson 2E/2F.
12494 Support round-robin dispatch scheme by enabling only one of
12495 ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
12496 respectively. */
12498 static void
12499 mips_ls2_dfa_post_advance_cycle (state_t state)
12501 if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
12503 /* Though there are no non-pipelined ALU1 insns,
12504 we can get an instruction of type 'multi' before reload. */
12505 gcc_assert (mips_ls2.cycle_has_multi_p);
12506 mips_ls2.alu1_turn_p = false;
12509 mips_ls2.cycle_has_multi_p = false;
12511 if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
12512 /* We have a non-pipelined alu instruction in the core,
12513 adjust round-robin counter. */
12514 mips_ls2.alu1_turn_p = true;
12516 if (mips_ls2.alu1_turn_p)
12518 if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
12519 gcc_unreachable ();
12521 else
12523 if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
12524 gcc_unreachable ();
12527 if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
12529 /* There are no non-pipelined FALU1 insns. */
12530 gcc_unreachable ();
12531 mips_ls2.falu1_turn_p = false;
12534 if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
12535 /* We have a non-pipelined falu instruction in the core,
12536 adjust round-robin counter. */
12537 mips_ls2.falu1_turn_p = true;
12539 if (mips_ls2.falu1_turn_p)
12541 if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
12542 gcc_unreachable ();
12544 else
12546 if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
12547 gcc_unreachable ();
12551 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
12552 This hook is being called at the start of each cycle. */
12554 static void
12555 mips_dfa_post_advance_cycle (void)
12557 if (TUNE_LOONGSON_2EF)
12558 mips_ls2_dfa_post_advance_cycle (curr_state);
12561 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
12562 be as wide as the scheduling freedom in the DFA. */
12564 static int
12565 mips_multipass_dfa_lookahead (void)
12567 /* Can schedule up to 4 of the 6 function units in any one cycle. */
12568 if (TUNE_SB1)
12569 return 4;
12571 if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
12572 return 4;
12574 if (TUNE_OCTEON)
12575 return 2;
12577 return 0;
12580 /* Remove the instruction at index LOWER from ready queue READY and
12581 reinsert it in front of the instruction at index HIGHER. LOWER must
12582 be <= HIGHER. */
12584 static void
12585 mips_promote_ready (rtx *ready, int lower, int higher)
12587 rtx new_head;
12588 int i;
12590 new_head = ready[lower];
12591 for (i = lower; i < higher; i++)
12592 ready[i] = ready[i + 1];
12593 ready[i] = new_head;
12596 /* If the priority of the instruction at POS2 in the ready queue READY
12597 is within LIMIT units of that of the instruction at POS1, swap the
12598 instructions if POS2 is not already less than POS1. */
12600 static void
12601 mips_maybe_swap_ready (rtx *ready, int pos1, int pos2, int limit)
12603 if (pos1 < pos2
12604 && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
12606 rtx temp;
12608 temp = ready[pos1];
12609 ready[pos1] = ready[pos2];
12610 ready[pos2] = temp;
12614 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
12615 that may clobber hi or lo. */
12616 static rtx mips_macc_chains_last_hilo;
12618 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
12619 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
12621 static void
12622 mips_macc_chains_record (rtx insn)
12624 if (get_attr_may_clobber_hilo (insn))
12625 mips_macc_chains_last_hilo = insn;
12628 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
12629 has NREADY elements, looking for a multiply-add or multiply-subtract
12630 instruction that is cumulative with mips_macc_chains_last_hilo.
12631 If there is one, promote it ahead of anything else that might
12632 clobber hi or lo. */
12634 static void
12635 mips_macc_chains_reorder (rtx *ready, int nready)
12637 int i, j;
12639 if (mips_macc_chains_last_hilo != 0)
12640 for (i = nready - 1; i >= 0; i--)
12641 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
12643 for (j = nready - 1; j > i; j--)
12644 if (recog_memoized (ready[j]) >= 0
12645 && get_attr_may_clobber_hilo (ready[j]))
12647 mips_promote_ready (ready, i, j);
12648 break;
12650 break;
12654 /* The last instruction to be scheduled. */
12655 static rtx vr4130_last_insn;
12657 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
12658 points to an rtx that is initially an instruction. Nullify the rtx
12659 if the instruction uses the value of register X. */
12661 static void
12662 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
12663 void *data)
12665 rtx *insn_ptr;
12667 insn_ptr = (rtx *) data;
12668 if (REG_P (x)
12669 && *insn_ptr != 0
12670 && reg_referenced_p (x, PATTERN (*insn_ptr)))
12671 *insn_ptr = 0;
12674 /* Return true if there is true register dependence between vr4130_last_insn
12675 and INSN. */
12677 static bool
12678 vr4130_true_reg_dependence_p (rtx insn)
12680 note_stores (PATTERN (vr4130_last_insn),
12681 vr4130_true_reg_dependence_p_1, &insn);
12682 return insn == 0;
12685 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
12686 the ready queue and that INSN2 is the instruction after it, return
12687 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
12688 in which INSN1 and INSN2 can probably issue in parallel, but for
12689 which (INSN2, INSN1) should be less sensitive to instruction
12690 alignment than (INSN1, INSN2). See 4130.md for more details. */
12692 static bool
12693 vr4130_swap_insns_p (rtx insn1, rtx insn2)
12695 sd_iterator_def sd_it;
12696 dep_t dep;
12698 /* Check for the following case:
12700 1) there is some other instruction X with an anti dependence on INSN1;
12701 2) X has a higher priority than INSN2; and
12702 3) X is an arithmetic instruction (and thus has no unit restrictions).
12704 If INSN1 is the last instruction blocking X, it would better to
12705 choose (INSN1, X) over (INSN2, INSN1). */
12706 FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
12707 if (DEP_TYPE (dep) == REG_DEP_ANTI
12708 && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
12709 && recog_memoized (DEP_CON (dep)) >= 0
12710 && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
12711 return false;
12713 if (vr4130_last_insn != 0
12714 && recog_memoized (insn1) >= 0
12715 && recog_memoized (insn2) >= 0)
12717 /* See whether INSN1 and INSN2 use different execution units,
12718 or if they are both ALU-type instructions. If so, they can
12719 probably execute in parallel. */
12720 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
12721 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
12722 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
12724 /* If only one of the instructions has a dependence on
12725 vr4130_last_insn, prefer to schedule the other one first. */
12726 bool dep1_p = vr4130_true_reg_dependence_p (insn1);
12727 bool dep2_p = vr4130_true_reg_dependence_p (insn2);
12728 if (dep1_p != dep2_p)
12729 return dep1_p;
12731 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
12732 is not an ALU-type instruction and if INSN1 uses the same
12733 execution unit. (Note that if this condition holds, we already
12734 know that INSN2 uses a different execution unit.) */
12735 if (class1 != VR4130_CLASS_ALU
12736 && recog_memoized (vr4130_last_insn) >= 0
12737 && class1 == get_attr_vr4130_class (vr4130_last_insn))
12738 return true;
12741 return false;
12744 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
12745 queue with at least two instructions. Swap the first two if
12746 vr4130_swap_insns_p says that it could be worthwhile. */
12748 static void
12749 vr4130_reorder (rtx *ready, int nready)
12751 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
12752 mips_promote_ready (ready, nready - 2, nready - 1);
12755 /* Record whether last 74k AGEN instruction was a load or store. */
12756 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
12758 /* Initialize mips_last_74k_agen_insn from INSN. A null argument
12759 resets to TYPE_UNKNOWN state. */
12761 static void
12762 mips_74k_agen_init (rtx insn)
12764 if (!insn || CALL_P (insn) || JUMP_P (insn))
12765 mips_last_74k_agen_insn = TYPE_UNKNOWN;
12766 else
12768 enum attr_type type = get_attr_type (insn);
12769 if (type == TYPE_LOAD || type == TYPE_STORE)
12770 mips_last_74k_agen_insn = type;
12774 /* A TUNE_74K helper function. The 74K AGEN pipeline likes multiple
12775 loads to be grouped together, and multiple stores to be grouped
12776 together. Swap things around in the ready queue to make this happen. */
12778 static void
12779 mips_74k_agen_reorder (rtx *ready, int nready)
12781 int i;
12782 int store_pos, load_pos;
12784 store_pos = -1;
12785 load_pos = -1;
12787 for (i = nready - 1; i >= 0; i--)
12789 rtx insn = ready[i];
12790 if (USEFUL_INSN_P (insn))
12791 switch (get_attr_type (insn))
12793 case TYPE_STORE:
12794 if (store_pos == -1)
12795 store_pos = i;
12796 break;
12798 case TYPE_LOAD:
12799 if (load_pos == -1)
12800 load_pos = i;
12801 break;
12803 default:
12804 break;
12808 if (load_pos == -1 || store_pos == -1)
12809 return;
12811 switch (mips_last_74k_agen_insn)
12813 case TYPE_UNKNOWN:
12814 /* Prefer to schedule loads since they have a higher latency. */
12815 case TYPE_LOAD:
12816 /* Swap loads to the front of the queue. */
12817 mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
12818 break;
12819 case TYPE_STORE:
12820 /* Swap stores to the front of the queue. */
12821 mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
12822 break;
12823 default:
12824 break;
12828 /* Implement TARGET_SCHED_INIT. */
12830 static void
12831 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
12832 int max_ready ATTRIBUTE_UNUSED)
12834 mips_macc_chains_last_hilo = 0;
12835 vr4130_last_insn = 0;
12836 mips_74k_agen_init (NULL_RTX);
12838 /* When scheduling for Loongson2, branch instructions go to ALU1,
12839 therefore basic block is most likely to start with round-robin counter
12840 pointed to ALU2. */
12841 mips_ls2.alu1_turn_p = false;
12842 mips_ls2.falu1_turn_p = true;
12845 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2. */
12847 static void
12848 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
12849 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
12851 if (!reload_completed
12852 && TUNE_MACC_CHAINS
12853 && *nreadyp > 0)
12854 mips_macc_chains_reorder (ready, *nreadyp);
12856 if (reload_completed
12857 && TUNE_MIPS4130
12858 && !TARGET_VR4130_ALIGN
12859 && *nreadyp > 1)
12860 vr4130_reorder (ready, *nreadyp);
12862 if (TUNE_74K)
12863 mips_74k_agen_reorder (ready, *nreadyp);
12866 /* Implement TARGET_SCHED_REORDER. */
12868 static int
12869 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
12870 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
12872 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
12873 return mips_issue_rate ();
12876 /* Implement TARGET_SCHED_REORDER2. */
12878 static int
12879 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
12880 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
12882 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
12883 return cached_can_issue_more;
12886 /* Update round-robin counters for ALU1/2 and FALU1/2. */
12888 static void
12889 mips_ls2_variable_issue (rtx insn)
12891 if (mips_ls2.alu1_turn_p)
12893 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
12894 mips_ls2.alu1_turn_p = false;
12896 else
12898 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
12899 mips_ls2.alu1_turn_p = true;
12902 if (mips_ls2.falu1_turn_p)
12904 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
12905 mips_ls2.falu1_turn_p = false;
12907 else
12909 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
12910 mips_ls2.falu1_turn_p = true;
12913 if (recog_memoized (insn) >= 0)
12914 mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
12917 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
12919 static int
12920 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
12921 rtx insn, int more)
12923 /* Ignore USEs and CLOBBERs; don't count them against the issue rate. */
12924 if (USEFUL_INSN_P (insn))
12926 if (get_attr_type (insn) != TYPE_GHOST)
12927 more--;
12928 if (!reload_completed && TUNE_MACC_CHAINS)
12929 mips_macc_chains_record (insn);
12930 vr4130_last_insn = insn;
12931 if (TUNE_74K)
12932 mips_74k_agen_init (insn);
12933 else if (TUNE_LOONGSON_2EF)
12934 mips_ls2_variable_issue (insn);
12937 /* Instructions of type 'multi' should all be split before
12938 the second scheduling pass. */
12939 gcc_assert (!reload_completed
12940 || recog_memoized (insn) < 0
12941 || get_attr_type (insn) != TYPE_MULTI);
12943 cached_can_issue_more = more;
12944 return more;
12947 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
12948 return the first operand of the associated PREF or PREFX insn. */
12951 mips_prefetch_cookie (rtx write, rtx locality)
12953 /* store_streamed / load_streamed. */
12954 if (INTVAL (locality) <= 0)
12955 return GEN_INT (INTVAL (write) + 4);
12957 /* store / load. */
12958 if (INTVAL (locality) <= 2)
12959 return write;
12961 /* store_retained / load_retained. */
12962 return GEN_INT (INTVAL (write) + 6);
12965 /* Flags that indicate when a built-in function is available.
12967 BUILTIN_AVAIL_NON_MIPS16
12968 The function is available on the current target, but only
12969 in non-MIPS16 mode. */
12970 #define BUILTIN_AVAIL_NON_MIPS16 1
12972 /* Declare an availability predicate for built-in functions that
12973 require non-MIPS16 mode and also require COND to be true.
12974 NAME is the main part of the predicate's name. */
12975 #define AVAIL_NON_MIPS16(NAME, COND) \
12976 static unsigned int \
12977 mips_builtin_avail_##NAME (void) \
12979 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0; \
12982 /* This structure describes a single built-in function. */
12983 struct mips_builtin_description {
12984 /* The code of the main .md file instruction. See mips_builtin_type
12985 for more information. */
12986 enum insn_code icode;
12988 /* The floating-point comparison code to use with ICODE, if any. */
12989 enum mips_fp_condition cond;
12991 /* The name of the built-in function. */
12992 const char *name;
12994 /* Specifies how the function should be expanded. */
12995 enum mips_builtin_type builtin_type;
12997 /* The function's prototype. */
12998 enum mips_function_type function_type;
13000 /* Whether the function is available. */
13001 unsigned int (*avail) (void);
13004 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
13005 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
13006 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
13007 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
13008 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
13009 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
13010 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
13011 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
13012 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
13013 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
13015 /* Construct a mips_builtin_description from the given arguments.
13017 INSN is the name of the associated instruction pattern, without the
13018 leading CODE_FOR_mips_.
13020 CODE is the floating-point condition code associated with the
13021 function. It can be 'f' if the field is not applicable.
13023 NAME is the name of the function itself, without the leading
13024 "__builtin_mips_".
13026 BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
13028 AVAIL is the name of the availability predicate, without the leading
13029 mips_builtin_avail_. */
13030 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE, \
13031 FUNCTION_TYPE, AVAIL) \
13032 { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND, \
13033 "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE, \
13034 mips_builtin_avail_ ## AVAIL }
13036 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
13037 mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE and AVAIL
13038 are as for MIPS_BUILTIN. */
13039 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
13040 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
13042 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
13043 are subject to mips_builtin_avail_<AVAIL>. */
13044 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL) \
13045 MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s", \
13046 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL), \
13047 MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d", \
13048 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
13050 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
13051 The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
13052 while the any and all forms are subject to mips_builtin_avail_mips3d. */
13053 #define CMP_PS_BUILTINS(INSN, COND, AVAIL) \
13054 MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps", \
13055 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF, \
13056 mips3d), \
13057 MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps", \
13058 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF, \
13059 mips3d), \
13060 MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
13061 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF, \
13062 AVAIL), \
13063 MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
13064 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF, \
13065 AVAIL)
13067 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s. The functions
13068 are subject to mips_builtin_avail_mips3d. */
13069 #define CMP_4S_BUILTINS(INSN, COND) \
13070 MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s", \
13071 MIPS_BUILTIN_CMP_ANY, \
13072 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d), \
13073 MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s", \
13074 MIPS_BUILTIN_CMP_ALL, \
13075 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
13077 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps. The comparison
13078 instruction requires mips_builtin_avail_<AVAIL>. */
13079 #define MOVTF_BUILTINS(INSN, COND, AVAIL) \
13080 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps", \
13081 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13082 AVAIL), \
13083 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps", \
13084 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13085 AVAIL)
13087 /* Define all the built-in functions related to C.cond.fmt condition COND. */
13088 #define CMP_BUILTINS(COND) \
13089 MOVTF_BUILTINS (c, COND, paired_single), \
13090 MOVTF_BUILTINS (cabs, COND, mips3d), \
13091 CMP_SCALAR_BUILTINS (cabs, COND, mips3d), \
13092 CMP_PS_BUILTINS (c, COND, paired_single), \
13093 CMP_PS_BUILTINS (cabs, COND, mips3d), \
13094 CMP_4S_BUILTINS (c, COND), \
13095 CMP_4S_BUILTINS (cabs, COND)
13097 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
13098 function mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE
13099 and AVAIL are as for MIPS_BUILTIN. */
13100 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
13101 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET, \
13102 FUNCTION_TYPE, AVAIL)
13104 /* Define __builtin_mips_bposge<VALUE>. <VALUE> is 32 for the MIPS32 DSP
13105 branch instruction. AVAIL is as for MIPS_BUILTIN. */
13106 #define BPOSGE_BUILTIN(VALUE, AVAIL) \
13107 MIPS_BUILTIN (bposge, f, "bposge" #VALUE, \
13108 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
13110 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
13111 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
13112 builtin_description field. */
13113 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE) \
13114 { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f, \
13115 "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT, \
13116 FUNCTION_TYPE, mips_builtin_avail_loongson }
13118 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
13119 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
13120 builtin_description field. */
13121 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE) \
13122 LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
13124 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
13125 We use functions of this form when the same insn can be usefully applied
13126 to more than one datatype. */
13127 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE) \
13128 LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
13130 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
13131 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
13132 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
13133 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
13134 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
13135 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
13136 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
13137 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
13139 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
13140 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
13141 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
13142 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
13143 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
13144 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
13145 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
13146 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
13147 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
13148 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
13149 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
13150 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
13151 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
13152 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
13153 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
13154 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
13155 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
13156 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
13157 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
13158 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
13159 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
13160 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
13161 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
13162 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
13163 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
13164 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
13165 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
13166 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
13167 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
13168 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
13170 static const struct mips_builtin_description mips_builtins[] = {
13171 DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13172 DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13173 DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13174 DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13175 DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
13176 DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
13177 DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
13178 DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
13180 DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
13181 DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13182 DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13183 DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13184 DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
13186 DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
13187 DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
13188 DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13189 DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13190 DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13191 DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13193 DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
13194 DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
13195 DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13196 DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13197 DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13198 DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13200 MIPS_FP_CONDITIONS (CMP_BUILTINS),
13202 /* Built-in functions for the SB-1 processor. */
13203 DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
13205 /* Built-in functions for the DSP ASE (32-bit and 64-bit). */
13206 DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13207 DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13208 DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13209 DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13210 DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13211 DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13212 DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13213 DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13214 DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13215 DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13216 DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
13217 DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
13218 DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
13219 DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
13220 DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
13221 DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
13222 DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13223 DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13224 DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13225 DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13226 DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
13227 DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
13228 DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13229 DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13230 DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13231 DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13232 DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13233 DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13234 DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13235 DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13236 DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13237 DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13238 DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13239 DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13240 DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13241 DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13242 DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13243 DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
13244 DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
13245 DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
13246 DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13247 DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
13248 DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
13249 DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
13250 DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
13251 DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
13252 DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
13253 DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13254 DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13255 DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13256 DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13257 DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13258 DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13259 DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13260 DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13261 DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13262 DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13263 DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13264 DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13265 DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
13266 DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
13267 DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
13268 DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
13269 DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
13270 BPOSGE_BUILTIN (32, dsp),
13272 /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit). */
13273 DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
13274 DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13275 DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13276 DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13277 DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13278 DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13279 DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13280 DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13281 DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13282 DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13283 DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13284 DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13285 DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13286 DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13287 DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13288 DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
13289 DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
13290 DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
13291 DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13292 DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
13293 DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
13294 DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
13295 DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13296 DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13297 DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13298 DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13299 DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13300 DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13301 DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13302 DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13303 DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13304 DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13305 DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13306 DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13308 /* Built-in functions for the DSP ASE (32-bit only). */
13309 DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13310 DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13311 DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13312 DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13313 DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13314 DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13315 DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13316 DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13317 DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13318 DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13319 DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13320 DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13321 DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13322 DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13323 DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13324 DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13325 DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
13326 DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
13327 DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
13328 DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
13329 DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
13330 DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13331 DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
13332 DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13333 DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
13334 DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
13335 DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
13337 /* Built-in functions for the DSP ASE (64-bit only). */
13338 DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
13340 /* The following are for the MIPS DSP ASE REV 2 (32-bit only). */
13341 DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13342 DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13343 DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13344 DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13345 DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13346 DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13347 DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13348 DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13349 DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13351 /* Builtin functions for ST Microelectronics Loongson-2E/2F cores. */
13352 LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
13353 LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
13354 LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
13355 LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13356 LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13357 LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13358 LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13359 LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13360 LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13361 LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
13362 LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
13363 LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13364 LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
13365 LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13366 LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13367 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
13368 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13369 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13370 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13371 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
13372 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
13373 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13374 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
13375 LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13376 LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13377 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13378 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13379 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13380 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13381 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13382 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13383 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13384 LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13385 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13386 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13387 LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13388 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13389 LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
13390 LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
13391 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13392 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13393 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13394 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13395 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13396 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13397 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13398 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13399 LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
13400 LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13401 LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13402 LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13403 LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13404 LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
13405 LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
13406 LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13407 LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13408 LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13409 LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
13410 LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13411 LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
13412 LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
13413 LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
13414 LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
13415 LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
13416 LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
13417 LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
13418 LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
13419 LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
13420 LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
13421 LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
13422 LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
13423 LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
13424 LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
13425 LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
13426 LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
13427 LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13428 LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13429 LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13430 LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13431 LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13432 LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13433 LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
13434 LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
13435 LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13436 LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
13437 LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13438 LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13439 LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13440 LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13441 LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13442 LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13443 LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13444 LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13445 LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13446 LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13447 LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13448 LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13449 LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13450 LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13452 /* Sundry other built-in functions. */
13453 DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
13456 /* Index I is the function declaration for mips_builtins[I], or null if the
13457 function isn't defined on this target. */
13458 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
13460 /* MODE is a vector mode whose elements have type TYPE. Return the type
13461 of the vector itself. */
13463 static tree
13464 mips_builtin_vector_type (tree type, enum machine_mode mode)
13466 static tree types[2 * (int) MAX_MACHINE_MODE];
13467 int mode_index;
13469 mode_index = (int) mode;
13471 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
13472 mode_index += MAX_MACHINE_MODE;
13474 if (types[mode_index] == NULL_TREE)
13475 types[mode_index] = build_vector_type_for_mode (type, mode);
13476 return types[mode_index];
13479 /* Return a type for 'const volatile void *'. */
13481 static tree
13482 mips_build_cvpointer_type (void)
13484 static tree cache;
13486 if (cache == NULL_TREE)
13487 cache = build_pointer_type (build_qualified_type
13488 (void_type_node,
13489 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
13490 return cache;
13493 /* Source-level argument types. */
13494 #define MIPS_ATYPE_VOID void_type_node
13495 #define MIPS_ATYPE_INT integer_type_node
13496 #define MIPS_ATYPE_POINTER ptr_type_node
13497 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
13499 /* Standard mode-based argument types. */
13500 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
13501 #define MIPS_ATYPE_SI intSI_type_node
13502 #define MIPS_ATYPE_USI unsigned_intSI_type_node
13503 #define MIPS_ATYPE_DI intDI_type_node
13504 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
13505 #define MIPS_ATYPE_SF float_type_node
13506 #define MIPS_ATYPE_DF double_type_node
13508 /* Vector argument types. */
13509 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
13510 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
13511 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
13512 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
13513 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
13514 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
13515 #define MIPS_ATYPE_UV2SI \
13516 mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
13517 #define MIPS_ATYPE_UV4HI \
13518 mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
13519 #define MIPS_ATYPE_UV8QI \
13520 mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
13522 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
13523 their associated MIPS_ATYPEs. */
13524 #define MIPS_FTYPE_ATYPES1(A, B) \
13525 MIPS_ATYPE_##A, MIPS_ATYPE_##B
13527 #define MIPS_FTYPE_ATYPES2(A, B, C) \
13528 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
13530 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
13531 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
13533 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
13534 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
13535 MIPS_ATYPE_##E
13537 /* Return the function type associated with function prototype TYPE. */
13539 static tree
13540 mips_build_function_type (enum mips_function_type type)
13542 static tree types[(int) MIPS_MAX_FTYPE_MAX];
13544 if (types[(int) type] == NULL_TREE)
13545 switch (type)
13547 #define DEF_MIPS_FTYPE(NUM, ARGS) \
13548 case MIPS_FTYPE_NAME##NUM ARGS: \
13549 types[(int) type] \
13550 = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS, \
13551 NULL_TREE); \
13552 break;
13553 #include "config/mips/mips-ftypes.def"
13554 #undef DEF_MIPS_FTYPE
13555 default:
13556 gcc_unreachable ();
13559 return types[(int) type];
13562 /* Implement TARGET_INIT_BUILTINS. */
13564 static void
13565 mips_init_builtins (void)
13567 const struct mips_builtin_description *d;
13568 unsigned int i;
13570 /* Iterate through all of the bdesc arrays, initializing all of the
13571 builtin functions. */
13572 for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
13574 d = &mips_builtins[i];
13575 if (d->avail ())
13576 mips_builtin_decls[i]
13577 = add_builtin_function (d->name,
13578 mips_build_function_type (d->function_type),
13579 i, BUILT_IN_MD, NULL, NULL);
13583 /* Implement TARGET_BUILTIN_DECL. */
13585 static tree
13586 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
13588 if (code >= ARRAY_SIZE (mips_builtins))
13589 return error_mark_node;
13590 return mips_builtin_decls[code];
13593 /* Take argument ARGNO from EXP's argument list and convert it into
13594 an expand operand. Store the operand in *OP. */
13596 static void
13597 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
13598 unsigned int argno)
13600 tree arg;
13601 rtx value;
13603 arg = CALL_EXPR_ARG (exp, argno);
13604 value = expand_normal (arg);
13605 create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
13608 /* Expand instruction ICODE as part of a built-in function sequence.
13609 Use the first NOPS elements of OPS as the instruction's operands.
13610 HAS_TARGET_P is true if operand 0 is a target; it is false if the
13611 instruction has no target.
13613 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
13615 static rtx
13616 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
13617 struct expand_operand *ops, bool has_target_p)
13619 if (!maybe_expand_insn (icode, nops, ops))
13621 error ("invalid argument to built-in function");
13622 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
13624 return has_target_p ? ops[0].value : const0_rtx;
13627 /* Expand a floating-point comparison for built-in function call EXP.
13628 The first NARGS arguments are the values to be compared. ICODE is
13629 the .md pattern that does the comparison and COND is the condition
13630 that is being tested. Return an rtx for the result. */
13632 static rtx
13633 mips_expand_builtin_compare_1 (enum insn_code icode,
13634 enum mips_fp_condition cond,
13635 tree exp, int nargs)
13637 struct expand_operand ops[MAX_RECOG_OPERANDS];
13638 int opno, argno;
13640 /* The instruction should have a target operand, an operand for each
13641 argument, and an operand for COND. */
13642 gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
13644 opno = 0;
13645 create_output_operand (&ops[opno++], NULL_RTX,
13646 insn_data[(int) icode].operand[0].mode);
13647 for (argno = 0; argno < nargs; argno++)
13648 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
13649 create_integer_operand (&ops[opno++], (int) cond);
13650 return mips_expand_builtin_insn (icode, opno, ops, true);
13653 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
13654 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
13655 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
13656 suggests a good place to put the result. */
13658 static rtx
13659 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
13660 bool has_target_p)
13662 struct expand_operand ops[MAX_RECOG_OPERANDS];
13663 int opno, argno;
13665 /* Map any target to operand 0. */
13666 opno = 0;
13667 if (has_target_p)
13668 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
13670 /* Map the arguments to the other operands. */
13671 gcc_assert (opno + call_expr_nargs (exp)
13672 == insn_data[icode].n_generator_args);
13673 for (argno = 0; argno < call_expr_nargs (exp); argno++)
13674 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
13676 return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
13679 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
13680 function; TYPE says which. EXP is the CALL_EXPR that calls the
13681 function, ICODE is the instruction that should be used to compare
13682 the first two arguments, and COND is the condition it should test.
13683 TARGET, if nonnull, suggests a good place to put the result. */
13685 static rtx
13686 mips_expand_builtin_movtf (enum mips_builtin_type type,
13687 enum insn_code icode, enum mips_fp_condition cond,
13688 rtx target, tree exp)
13690 struct expand_operand ops[4];
13691 rtx cmp_result;
13693 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
13694 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
13695 if (type == MIPS_BUILTIN_MOVT)
13697 mips_prepare_builtin_arg (&ops[2], exp, 2);
13698 mips_prepare_builtin_arg (&ops[1], exp, 3);
13700 else
13702 mips_prepare_builtin_arg (&ops[1], exp, 2);
13703 mips_prepare_builtin_arg (&ops[2], exp, 3);
13705 create_fixed_operand (&ops[3], cmp_result);
13706 return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
13707 4, ops, true);
13710 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
13711 into TARGET otherwise. Return TARGET. */
13713 static rtx
13714 mips_builtin_branch_and_move (rtx condition, rtx target,
13715 rtx value_if_true, rtx value_if_false)
13717 rtx true_label, done_label;
13719 true_label = gen_label_rtx ();
13720 done_label = gen_label_rtx ();
13722 /* First assume that CONDITION is false. */
13723 mips_emit_move (target, value_if_false);
13725 /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise. */
13726 emit_jump_insn (gen_condjump (condition, true_label));
13727 emit_jump_insn (gen_jump (done_label));
13728 emit_barrier ();
13730 /* Fix TARGET if CONDITION is true. */
13731 emit_label (true_label);
13732 mips_emit_move (target, value_if_true);
13734 emit_label (done_label);
13735 return target;
13738 /* Expand a comparison built-in function of type BUILTIN_TYPE. EXP is
13739 the CALL_EXPR that calls the function, ICODE is the code of the
13740 comparison instruction, and COND is the condition it should test.
13741 TARGET, if nonnull, suggests a good place to put the boolean result. */
13743 static rtx
13744 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
13745 enum insn_code icode, enum mips_fp_condition cond,
13746 rtx target, tree exp)
13748 rtx offset, condition, cmp_result;
13750 if (target == 0 || GET_MODE (target) != SImode)
13751 target = gen_reg_rtx (SImode);
13752 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
13753 call_expr_nargs (exp));
13755 /* If the comparison sets more than one register, we define the result
13756 to be 0 if all registers are false and -1 if all registers are true.
13757 The value of the complete result is indeterminate otherwise. */
13758 switch (builtin_type)
13760 case MIPS_BUILTIN_CMP_ALL:
13761 condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
13762 return mips_builtin_branch_and_move (condition, target,
13763 const0_rtx, const1_rtx);
13765 case MIPS_BUILTIN_CMP_UPPER:
13766 case MIPS_BUILTIN_CMP_LOWER:
13767 offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
13768 condition = gen_single_cc (cmp_result, offset);
13769 return mips_builtin_branch_and_move (condition, target,
13770 const1_rtx, const0_rtx);
13772 default:
13773 condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
13774 return mips_builtin_branch_and_move (condition, target,
13775 const1_rtx, const0_rtx);
13779 /* Expand a bposge built-in function of type BUILTIN_TYPE. TARGET,
13780 if nonnull, suggests a good place to put the boolean result. */
13782 static rtx
13783 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
13785 rtx condition, cmp_result;
13786 int cmp_value;
13788 if (target == 0 || GET_MODE (target) != SImode)
13789 target = gen_reg_rtx (SImode);
13791 cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
13793 if (builtin_type == MIPS_BUILTIN_BPOSGE32)
13794 cmp_value = 32;
13795 else
13796 gcc_assert (0);
13798 condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
13799 return mips_builtin_branch_and_move (condition, target,
13800 const1_rtx, const0_rtx);
13803 /* Implement TARGET_EXPAND_BUILTIN. */
13805 static rtx
13806 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
13807 enum machine_mode mode, int ignore)
13809 tree fndecl;
13810 unsigned int fcode, avail;
13811 const struct mips_builtin_description *d;
13813 fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13814 fcode = DECL_FUNCTION_CODE (fndecl);
13815 gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
13816 d = &mips_builtins[fcode];
13817 avail = d->avail ();
13818 gcc_assert (avail != 0);
13819 if (TARGET_MIPS16)
13821 error ("built-in function %qE not supported for MIPS16",
13822 DECL_NAME (fndecl));
13823 return ignore ? const0_rtx : CONST0_RTX (mode);
13825 switch (d->builtin_type)
13827 case MIPS_BUILTIN_DIRECT:
13828 return mips_expand_builtin_direct (d->icode, target, exp, true);
13830 case MIPS_BUILTIN_DIRECT_NO_TARGET:
13831 return mips_expand_builtin_direct (d->icode, target, exp, false);
13833 case MIPS_BUILTIN_MOVT:
13834 case MIPS_BUILTIN_MOVF:
13835 return mips_expand_builtin_movtf (d->builtin_type, d->icode,
13836 d->cond, target, exp);
13838 case MIPS_BUILTIN_CMP_ANY:
13839 case MIPS_BUILTIN_CMP_ALL:
13840 case MIPS_BUILTIN_CMP_UPPER:
13841 case MIPS_BUILTIN_CMP_LOWER:
13842 case MIPS_BUILTIN_CMP_SINGLE:
13843 return mips_expand_builtin_compare (d->builtin_type, d->icode,
13844 d->cond, target, exp);
13846 case MIPS_BUILTIN_BPOSGE32:
13847 return mips_expand_builtin_bposge (d->builtin_type, target);
13849 gcc_unreachable ();
13852 /* An entry in the MIPS16 constant pool. VALUE is the pool constant,
13853 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
13854 struct mips16_constant {
13855 struct mips16_constant *next;
13856 rtx value;
13857 rtx label;
13858 enum machine_mode mode;
13861 /* Information about an incomplete MIPS16 constant pool. FIRST is the
13862 first constant, HIGHEST_ADDRESS is the highest address that the first
13863 byte of the pool can have, and INSN_ADDRESS is the current instruction
13864 address. */
13865 struct mips16_constant_pool {
13866 struct mips16_constant *first;
13867 int highest_address;
13868 int insn_address;
13871 /* Add constant VALUE to POOL and return its label. MODE is the
13872 value's mode (used for CONST_INTs, etc.). */
13874 static rtx
13875 mips16_add_constant (struct mips16_constant_pool *pool,
13876 rtx value, enum machine_mode mode)
13878 struct mips16_constant **p, *c;
13879 bool first_of_size_p;
13881 /* See whether the constant is already in the pool. If so, return the
13882 existing label, otherwise leave P pointing to the place where the
13883 constant should be added.
13885 Keep the pool sorted in increasing order of mode size so that we can
13886 reduce the number of alignments needed. */
13887 first_of_size_p = true;
13888 for (p = &pool->first; *p != 0; p = &(*p)->next)
13890 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
13891 return (*p)->label;
13892 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
13893 break;
13894 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
13895 first_of_size_p = false;
13898 /* In the worst case, the constant needed by the earliest instruction
13899 will end up at the end of the pool. The entire pool must then be
13900 accessible from that instruction.
13902 When adding the first constant, set the pool's highest address to
13903 the address of the first out-of-range byte. Adjust this address
13904 downwards each time a new constant is added. */
13905 if (pool->first == 0)
13906 /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
13907 of the instruction with the lowest two bits clear. The base PC
13908 value for LDPC has the lowest three bits clear. Assume the worst
13909 case here; namely that the PC-relative instruction occupies the
13910 last 2 bytes in an aligned word. */
13911 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
13912 pool->highest_address -= GET_MODE_SIZE (mode);
13913 if (first_of_size_p)
13914 /* Take into account the worst possible padding due to alignment. */
13915 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
13917 /* Create a new entry. */
13918 c = XNEW (struct mips16_constant);
13919 c->value = value;
13920 c->mode = mode;
13921 c->label = gen_label_rtx ();
13922 c->next = *p;
13923 *p = c;
13925 return c->label;
13928 /* Output constant VALUE after instruction INSN and return the last
13929 instruction emitted. MODE is the mode of the constant. */
13931 static rtx
13932 mips16_emit_constants_1 (enum machine_mode mode, rtx value, rtx insn)
13934 if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
13936 rtx size = GEN_INT (GET_MODE_SIZE (mode));
13937 return emit_insn_after (gen_consttable_int (value, size), insn);
13940 if (SCALAR_FLOAT_MODE_P (mode))
13941 return emit_insn_after (gen_consttable_float (value), insn);
13943 if (VECTOR_MODE_P (mode))
13945 int i;
13947 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
13948 insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
13949 CONST_VECTOR_ELT (value, i), insn);
13950 return insn;
13953 gcc_unreachable ();
13956 /* Dump out the constants in CONSTANTS after INSN. */
13958 static void
13959 mips16_emit_constants (struct mips16_constant *constants, rtx insn)
13961 struct mips16_constant *c, *next;
13962 int align;
13964 align = 0;
13965 for (c = constants; c != NULL; c = next)
13967 /* If necessary, increase the alignment of PC. */
13968 if (align < GET_MODE_SIZE (c->mode))
13970 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
13971 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
13973 align = GET_MODE_SIZE (c->mode);
13975 insn = emit_label_after (c->label, insn);
13976 insn = mips16_emit_constants_1 (c->mode, c->value, insn);
13978 next = c->next;
13979 free (c);
13982 emit_barrier_after (insn);
13985 /* Return the length of instruction INSN. */
13987 static int
13988 mips16_insn_length (rtx insn)
13990 if (JUMP_P (insn))
13992 rtx body = PATTERN (insn);
13993 if (GET_CODE (body) == ADDR_VEC)
13994 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
13995 if (GET_CODE (body) == ADDR_DIFF_VEC)
13996 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
13998 return get_attr_length (insn);
14001 /* If *X is a symbolic constant that refers to the constant pool, add
14002 the constant to POOL and rewrite *X to use the constant's label. */
14004 static void
14005 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
14007 rtx base, offset, label;
14009 split_const (*x, &base, &offset);
14010 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
14012 label = mips16_add_constant (pool, get_pool_constant (base),
14013 get_pool_mode (base));
14014 base = gen_rtx_LABEL_REF (Pmode, label);
14015 *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
14019 /* This structure is used to communicate with mips16_rewrite_pool_refs.
14020 INSN is the instruction we're rewriting and POOL points to the current
14021 constant pool. */
14022 struct mips16_rewrite_pool_refs_info {
14023 rtx insn;
14024 struct mips16_constant_pool *pool;
14027 /* Rewrite *X so that constant pool references refer to the constant's
14028 label instead. DATA points to a mips16_rewrite_pool_refs_info
14029 structure. */
14031 static int
14032 mips16_rewrite_pool_refs (rtx *x, void *data)
14034 struct mips16_rewrite_pool_refs_info *info =
14035 (struct mips16_rewrite_pool_refs_info *) data;
14037 if (force_to_mem_operand (*x, Pmode))
14039 rtx mem = force_const_mem (GET_MODE (*x), *x);
14040 validate_change (info->insn, x, mem, false);
14043 if (MEM_P (*x))
14045 mips16_rewrite_pool_constant (info->pool, &XEXP (*x, 0));
14046 return -1;
14049 /* Don't rewrite the __mips16_rdwr symbol. */
14050 if (GET_CODE (*x) == UNSPEC && XINT (*x, 1) == UNSPEC_TLS_GET_TP)
14051 return -1;
14053 if (TARGET_MIPS16_TEXT_LOADS)
14054 mips16_rewrite_pool_constant (info->pool, x);
14056 return GET_CODE (*x) == CONST ? -1 : 0;
14059 /* Return whether CFG is used in mips_reorg. */
14061 static bool
14062 mips_cfg_in_reorg (void)
14064 return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
14065 || TARGET_RELAX_PIC_CALLS);
14068 /* Build MIPS16 constant pools. */
14070 static void
14071 mips16_lay_out_constants (void)
14073 struct mips16_constant_pool pool;
14074 struct mips16_rewrite_pool_refs_info info;
14075 rtx insn, barrier;
14077 if (!TARGET_MIPS16_PCREL_LOADS)
14078 return;
14080 if (mips_cfg_in_reorg ())
14081 split_all_insns ();
14082 else
14083 split_all_insns_noflow ();
14084 barrier = 0;
14085 memset (&pool, 0, sizeof (pool));
14086 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14088 /* Rewrite constant pool references in INSN. */
14089 if (USEFUL_INSN_P (insn))
14091 info.insn = insn;
14092 info.pool = &pool;
14093 for_each_rtx (&PATTERN (insn), mips16_rewrite_pool_refs, &info);
14096 pool.insn_address += mips16_insn_length (insn);
14098 if (pool.first != NULL)
14100 /* If there are no natural barriers between the first user of
14101 the pool and the highest acceptable address, we'll need to
14102 create a new instruction to jump around the constant pool.
14103 In the worst case, this instruction will be 4 bytes long.
14105 If it's too late to do this transformation after INSN,
14106 do it immediately before INSN. */
14107 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
14109 rtx label, jump;
14111 label = gen_label_rtx ();
14113 jump = emit_jump_insn_before (gen_jump (label), insn);
14114 JUMP_LABEL (jump) = label;
14115 LABEL_NUSES (label) = 1;
14116 barrier = emit_barrier_after (jump);
14118 emit_label_after (label, barrier);
14119 pool.insn_address += 4;
14122 /* See whether the constant pool is now out of range of the first
14123 user. If so, output the constants after the previous barrier.
14124 Note that any instructions between BARRIER and INSN (inclusive)
14125 will use negative offsets to refer to the pool. */
14126 if (pool.insn_address > pool.highest_address)
14128 mips16_emit_constants (pool.first, barrier);
14129 pool.first = NULL;
14130 barrier = 0;
14132 else if (BARRIER_P (insn))
14133 barrier = insn;
14136 mips16_emit_constants (pool.first, get_last_insn ());
14139 /* Return true if it is worth r10k_simplify_address's while replacing
14140 an address with X. We are looking for constants, and for addresses
14141 at a known offset from the incoming stack pointer. */
14143 static bool
14144 r10k_simplified_address_p (rtx x)
14146 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
14147 x = XEXP (x, 0);
14148 return x == virtual_incoming_args_rtx || CONSTANT_P (x);
14151 /* X is an expression that appears in INSN. Try to use the UD chains
14152 to simplify it, returning the simplified form on success and the
14153 original form otherwise. Replace the incoming value of $sp with
14154 virtual_incoming_args_rtx (which should never occur in X otherwise). */
14156 static rtx
14157 r10k_simplify_address (rtx x, rtx insn)
14159 rtx newx, op0, op1, set, def_insn, note;
14160 df_ref use, def;
14161 struct df_link *defs;
14163 newx = NULL_RTX;
14164 if (UNARY_P (x))
14166 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14167 if (op0 != XEXP (x, 0))
14168 newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
14169 op0, GET_MODE (XEXP (x, 0)));
14171 else if (BINARY_P (x))
14173 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14174 op1 = r10k_simplify_address (XEXP (x, 1), insn);
14175 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
14176 newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
14178 else if (GET_CODE (x) == LO_SUM)
14180 /* LO_SUMs can be offset from HIGHs, if we know they won't
14181 overflow. See mips_classify_address for the rationale behind
14182 the lax check. */
14183 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14184 if (GET_CODE (op0) == HIGH)
14185 newx = XEXP (x, 1);
14187 else if (REG_P (x))
14189 /* Uses are recorded by regno_reg_rtx, not X itself. */
14190 use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
14191 gcc_assert (use);
14192 defs = DF_REF_CHAIN (use);
14194 /* Require a single definition. */
14195 if (defs && defs->next == NULL)
14197 def = defs->ref;
14198 if (DF_REF_IS_ARTIFICIAL (def))
14200 /* Replace the incoming value of $sp with
14201 virtual_incoming_args_rtx. */
14202 if (x == stack_pointer_rtx
14203 && DF_REF_BB (def) == ENTRY_BLOCK_PTR)
14204 newx = virtual_incoming_args_rtx;
14206 else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
14207 DF_REF_BB (def)))
14209 /* Make sure that DEF_INSN is a single set of REG. */
14210 def_insn = DF_REF_INSN (def);
14211 if (NONJUMP_INSN_P (def_insn))
14213 set = single_set (def_insn);
14214 if (set && rtx_equal_p (SET_DEST (set), x))
14216 /* Prefer to use notes, since the def-use chains
14217 are often shorter. */
14218 note = find_reg_equal_equiv_note (def_insn);
14219 if (note)
14220 newx = XEXP (note, 0);
14221 else
14222 newx = SET_SRC (set);
14223 newx = r10k_simplify_address (newx, def_insn);
14229 if (newx && r10k_simplified_address_p (newx))
14230 return newx;
14231 return x;
14234 /* Return true if ADDRESS is known to be an uncached address
14235 on R10K systems. */
14237 static bool
14238 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
14240 unsigned HOST_WIDE_INT upper;
14242 /* Check for KSEG1. */
14243 if (address + 0x60000000 < 0x20000000)
14244 return true;
14246 /* Check for uncached XKPHYS addresses. */
14247 if (Pmode == DImode)
14249 upper = (address >> 40) & 0xf9ffff;
14250 if (upper == 0x900000 || upper == 0xb80000)
14251 return true;
14253 return false;
14256 /* Return true if we can prove that an access to address X in instruction
14257 INSN would be safe from R10K speculation. This X is a general
14258 expression; it might not be a legitimate address. */
14260 static bool
14261 r10k_safe_address_p (rtx x, rtx insn)
14263 rtx base, offset;
14264 HOST_WIDE_INT offset_val;
14266 x = r10k_simplify_address (x, insn);
14268 /* Check for references to the stack frame. It doesn't really matter
14269 how much of the frame has been allocated at INSN; -mr10k-cache-barrier
14270 allows us to assume that accesses to any part of the eventual frame
14271 is safe from speculation at any point in the function. */
14272 mips_split_plus (x, &base, &offset_val);
14273 if (base == virtual_incoming_args_rtx
14274 && offset_val >= -cfun->machine->frame.total_size
14275 && offset_val < cfun->machine->frame.args_size)
14276 return true;
14278 /* Check for uncached addresses. */
14279 if (CONST_INT_P (x))
14280 return r10k_uncached_address_p (INTVAL (x));
14282 /* Check for accesses to a static object. */
14283 split_const (x, &base, &offset);
14284 return offset_within_block_p (base, INTVAL (offset));
14287 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
14288 an in-range access to an automatic variable, or to an object with
14289 a link-time-constant address. */
14291 static bool
14292 r10k_safe_mem_expr_p (tree expr, HOST_WIDE_INT offset)
14294 if (offset < 0 || offset >= int_size_in_bytes (TREE_TYPE (expr)))
14295 return false;
14297 while (TREE_CODE (expr) == COMPONENT_REF)
14299 expr = TREE_OPERAND (expr, 0);
14300 if (expr == NULL_TREE)
14301 return false;
14304 return DECL_P (expr);
14307 /* A for_each_rtx callback for which DATA points to the instruction
14308 containing *X. Stop the search if we find a MEM that is not safe
14309 from R10K speculation. */
14311 static int
14312 r10k_needs_protection_p_1 (rtx *loc, void *data)
14314 rtx mem;
14316 mem = *loc;
14317 if (!MEM_P (mem))
14318 return 0;
14320 if (MEM_EXPR (mem)
14321 && MEM_OFFSET_KNOWN_P (mem)
14322 && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
14323 return -1;
14325 if (r10k_safe_address_p (XEXP (mem, 0), (rtx) data))
14326 return -1;
14328 return 1;
14331 /* A note_stores callback for which DATA points to an instruction pointer.
14332 If *DATA is nonnull, make it null if it X contains a MEM that is not
14333 safe from R10K speculation. */
14335 static void
14336 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
14337 void *data)
14339 rtx *insn_ptr;
14341 insn_ptr = (rtx *) data;
14342 if (*insn_ptr && for_each_rtx (&x, r10k_needs_protection_p_1, *insn_ptr))
14343 *insn_ptr = NULL_RTX;
14346 /* A for_each_rtx callback that iterates over the pattern of a CALL_INSN.
14347 Return nonzero if the call is not to a declared function. */
14349 static int
14350 r10k_needs_protection_p_call (rtx *loc, void *data ATTRIBUTE_UNUSED)
14352 rtx x;
14354 x = *loc;
14355 if (!MEM_P (x))
14356 return 0;
14358 x = XEXP (x, 0);
14359 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DECL (x))
14360 return -1;
14362 return 1;
14365 /* Return true if instruction INSN needs to be protected by an R10K
14366 cache barrier. */
14368 static bool
14369 r10k_needs_protection_p (rtx insn)
14371 if (CALL_P (insn))
14372 return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_call, NULL);
14374 if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
14376 note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
14377 return insn == NULL_RTX;
14380 return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_1, insn);
14383 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
14384 edge is unconditional. */
14386 static bool
14387 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
14389 edge_iterator ei;
14390 edge e;
14392 FOR_EACH_EDGE (e, ei, bb->preds)
14393 if (!single_succ_p (e->src)
14394 || !TEST_BIT (protected_bbs, e->src->index)
14395 || (e->flags & EDGE_COMPLEX) != 0)
14396 return false;
14397 return true;
14400 /* Implement -mr10k-cache-barrier= for the current function. */
14402 static void
14403 r10k_insert_cache_barriers (void)
14405 int *rev_post_order;
14406 unsigned int i, n;
14407 basic_block bb;
14408 sbitmap protected_bbs;
14409 rtx insn, end, unprotected_region;
14411 if (TARGET_MIPS16)
14413 sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
14414 return;
14417 /* Calculate dominators. */
14418 calculate_dominance_info (CDI_DOMINATORS);
14420 /* Bit X of PROTECTED_BBS is set if the last operation in basic block
14421 X is protected by a cache barrier. */
14422 protected_bbs = sbitmap_alloc (last_basic_block);
14423 sbitmap_zero (protected_bbs);
14425 /* Iterate over the basic blocks in reverse post-order. */
14426 rev_post_order = XNEWVEC (int, last_basic_block);
14427 n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
14428 for (i = 0; i < n; i++)
14430 bb = BASIC_BLOCK (rev_post_order[i]);
14432 /* If this block is only reached by unconditional edges, and if the
14433 source of every edge is protected, the beginning of the block is
14434 also protected. */
14435 if (r10k_protected_bb_p (bb, protected_bbs))
14436 unprotected_region = NULL_RTX;
14437 else
14438 unprotected_region = pc_rtx;
14439 end = NEXT_INSN (BB_END (bb));
14441 /* UNPROTECTED_REGION is:
14443 - null if we are processing a protected region,
14444 - pc_rtx if we are processing an unprotected region but have
14445 not yet found the first instruction in it
14446 - the first instruction in an unprotected region otherwise. */
14447 for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
14449 if (unprotected_region && USEFUL_INSN_P (insn))
14451 if (recog_memoized (insn) == CODE_FOR_mips_cache)
14452 /* This CACHE instruction protects the following code. */
14453 unprotected_region = NULL_RTX;
14454 else
14456 /* See if INSN is the first instruction in this
14457 unprotected region. */
14458 if (unprotected_region == pc_rtx)
14459 unprotected_region = insn;
14461 /* See if INSN needs to be protected. If so,
14462 we must insert a cache barrier somewhere between
14463 PREV_INSN (UNPROTECTED_REGION) and INSN. It isn't
14464 clear which position is better performance-wise,
14465 but as a tie-breaker, we assume that it is better
14466 to allow delay slots to be back-filled where
14467 possible, and that it is better not to insert
14468 barriers in the middle of already-scheduled code.
14469 We therefore insert the barrier at the beginning
14470 of the region. */
14471 if (r10k_needs_protection_p (insn))
14473 emit_insn_before (gen_r10k_cache_barrier (),
14474 unprotected_region);
14475 unprotected_region = NULL_RTX;
14480 if (CALL_P (insn))
14481 /* The called function is not required to protect the exit path.
14482 The code that follows a call is therefore unprotected. */
14483 unprotected_region = pc_rtx;
14486 /* Record whether the end of this block is protected. */
14487 if (unprotected_region == NULL_RTX)
14488 SET_BIT (protected_bbs, bb->index);
14490 XDELETEVEC (rev_post_order);
14492 sbitmap_free (protected_bbs);
14494 free_dominance_info (CDI_DOMINATORS);
14497 /* If INSN is a call, return the underlying CALL expr. Return NULL_RTX
14498 otherwise. If INSN has two call rtx, then store the second one in
14499 SECOND_CALL. */
14501 static rtx
14502 mips_call_expr_from_insn (rtx insn, rtx *second_call)
14504 rtx x;
14505 rtx x2;
14507 if (!CALL_P (insn))
14508 return NULL_RTX;
14510 x = PATTERN (insn);
14511 if (GET_CODE (x) == PARALLEL)
14513 /* Calls returning complex values have two CALL rtx. Look for the second
14514 one here, and return it via the SECOND_CALL arg. */
14515 x2 = XVECEXP (x, 0, 1);
14516 if (GET_CODE (x2) == SET)
14517 x2 = XEXP (x2, 1);
14518 if (GET_CODE (x2) == CALL)
14519 *second_call = x2;
14521 x = XVECEXP (x, 0, 0);
14523 if (GET_CODE (x) == SET)
14524 x = XEXP (x, 1);
14525 gcc_assert (GET_CODE (x) == CALL);
14527 return x;
14530 /* REG is set in DEF. See if the definition is one of the ways we load a
14531 register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
14532 If it is, return the symbol reference of the function, otherwise return
14533 NULL_RTX.
14535 If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
14536 the values of source registers, otherwise treat such registers as
14537 having an unknown value. */
14539 static rtx
14540 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
14542 rtx def_insn, set;
14544 if (DF_REF_IS_ARTIFICIAL (def))
14545 return NULL_RTX;
14547 def_insn = DF_REF_INSN (def);
14548 set = single_set (def_insn);
14549 if (set && rtx_equal_p (SET_DEST (set), reg))
14551 rtx note, src, symbol;
14553 /* First, look at REG_EQUAL/EQUIV notes. */
14554 note = find_reg_equal_equiv_note (def_insn);
14555 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
14556 return XEXP (note, 0);
14558 /* For %call16 references we don't have REG_EQUAL. */
14559 src = SET_SRC (set);
14560 symbol = mips_strip_unspec_call (src);
14561 if (symbol)
14563 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
14564 return symbol;
14567 /* Follow at most one simple register copy. Such copies are
14568 interesting in cases like:
14570 for (...)
14572 locally_binding_fn (...);
14575 and:
14577 locally_binding_fn (...);
14579 locally_binding_fn (...);
14581 where the load of locally_binding_fn can legitimately be
14582 hoisted or shared. However, we do not expect to see complex
14583 chains of copies, so a full worklist solution to the problem
14584 would probably be overkill. */
14585 if (recurse_p && REG_P (src))
14586 return mips_find_pic_call_symbol (def_insn, src, false);
14589 return NULL_RTX;
14592 /* Find the definition of the use of REG in INSN. See if the definition
14593 is one of the ways we load a register with a symbol address for a
14594 mips_use_pic_fn_addr_reg_p call. If it is return the symbol reference
14595 of the function, otherwise return NULL_RTX. RECURSE_P is as for
14596 mips_pic_call_symbol_from_set. */
14598 static rtx
14599 mips_find_pic_call_symbol (rtx insn, rtx reg, bool recurse_p)
14601 df_ref use;
14602 struct df_link *defs;
14603 rtx symbol;
14605 use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
14606 if (!use)
14607 return NULL_RTX;
14608 defs = DF_REF_CHAIN (use);
14609 if (!defs)
14610 return NULL_RTX;
14611 symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
14612 if (!symbol)
14613 return NULL_RTX;
14615 /* If we have more than one definition, they need to be identical. */
14616 for (defs = defs->next; defs; defs = defs->next)
14618 rtx other;
14620 other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
14621 if (!rtx_equal_p (symbol, other))
14622 return NULL_RTX;
14625 return symbol;
14628 /* Replace the args_size operand of the call expression CALL with the
14629 call-attribute UNSPEC and fill in SYMBOL as the function symbol. */
14631 static void
14632 mips_annotate_pic_call_expr (rtx call, rtx symbol)
14634 rtx args_size;
14636 args_size = XEXP (call, 1);
14637 XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
14638 gen_rtvec (2, args_size, symbol),
14639 UNSPEC_CALL_ATTR);
14642 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression. See
14643 if instead of the arg_size argument it contains the call attributes. If
14644 yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
14645 symbol from the call attributes. Also return false if ARGS_SIZE_OPNO is
14646 -1. */
14648 bool
14649 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
14651 rtx args_size, symbol;
14653 if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
14654 return false;
14656 args_size = operands[args_size_opno];
14657 if (GET_CODE (args_size) != UNSPEC)
14658 return false;
14659 gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
14661 symbol = XVECEXP (args_size, 0, 1);
14662 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
14664 operands[args_size_opno] = symbol;
14665 return true;
14668 /* Use DF to annotate PIC indirect calls with the function symbol they
14669 dispatch to. */
14671 static void
14672 mips_annotate_pic_calls (void)
14674 basic_block bb;
14675 rtx insn;
14677 FOR_EACH_BB (bb)
14678 FOR_BB_INSNS (bb, insn)
14680 rtx call, reg, symbol, second_call;
14682 second_call = 0;
14683 call = mips_call_expr_from_insn (insn, &second_call);
14684 if (!call)
14685 continue;
14686 gcc_assert (MEM_P (XEXP (call, 0)));
14687 reg = XEXP (XEXP (call, 0), 0);
14688 if (!REG_P (reg))
14689 continue;
14691 symbol = mips_find_pic_call_symbol (insn, reg, true);
14692 if (symbol)
14694 mips_annotate_pic_call_expr (call, symbol);
14695 if (second_call)
14696 mips_annotate_pic_call_expr (second_call, symbol);
14701 /* A temporary variable used by for_each_rtx callbacks, etc. */
14702 static rtx mips_sim_insn;
14704 /* A structure representing the state of the processor pipeline.
14705 Used by the mips_sim_* family of functions. */
14706 struct mips_sim {
14707 /* The maximum number of instructions that can be issued in a cycle.
14708 (Caches mips_issue_rate.) */
14709 unsigned int issue_rate;
14711 /* The current simulation time. */
14712 unsigned int time;
14714 /* How many more instructions can be issued in the current cycle. */
14715 unsigned int insns_left;
14717 /* LAST_SET[X].INSN is the last instruction to set register X.
14718 LAST_SET[X].TIME is the time at which that instruction was issued.
14719 INSN is null if no instruction has yet set register X. */
14720 struct {
14721 rtx insn;
14722 unsigned int time;
14723 } last_set[FIRST_PSEUDO_REGISTER];
14725 /* The pipeline's current DFA state. */
14726 state_t dfa_state;
14729 /* Reset STATE to the initial simulation state. */
14731 static void
14732 mips_sim_reset (struct mips_sim *state)
14734 state->time = 0;
14735 state->insns_left = state->issue_rate;
14736 memset (&state->last_set, 0, sizeof (state->last_set));
14737 state_reset (state->dfa_state);
14740 /* Initialize STATE before its first use. DFA_STATE points to an
14741 allocated but uninitialized DFA state. */
14743 static void
14744 mips_sim_init (struct mips_sim *state, state_t dfa_state)
14746 state->issue_rate = mips_issue_rate ();
14747 state->dfa_state = dfa_state;
14748 mips_sim_reset (state);
14751 /* Advance STATE by one clock cycle. */
14753 static void
14754 mips_sim_next_cycle (struct mips_sim *state)
14756 state->time++;
14757 state->insns_left = state->issue_rate;
14758 state_transition (state->dfa_state, 0);
14761 /* Advance simulation state STATE until instruction INSN can read
14762 register REG. */
14764 static void
14765 mips_sim_wait_reg (struct mips_sim *state, rtx insn, rtx reg)
14767 unsigned int regno, end_regno;
14769 end_regno = END_REGNO (reg);
14770 for (regno = REGNO (reg); regno < end_regno; regno++)
14771 if (state->last_set[regno].insn != 0)
14773 unsigned int t;
14775 t = (state->last_set[regno].time
14776 + insn_latency (state->last_set[regno].insn, insn));
14777 while (state->time < t)
14778 mips_sim_next_cycle (state);
14782 /* A for_each_rtx callback. If *X is a register, advance simulation state
14783 DATA until mips_sim_insn can read the register's value. */
14785 static int
14786 mips_sim_wait_regs_2 (rtx *x, void *data)
14788 if (REG_P (*x))
14789 mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *x);
14790 return 0;
14793 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X. */
14795 static void
14796 mips_sim_wait_regs_1 (rtx *x, void *data)
14798 for_each_rtx (x, mips_sim_wait_regs_2, data);
14801 /* Advance simulation state STATE until all of INSN's register
14802 dependencies are satisfied. */
14804 static void
14805 mips_sim_wait_regs (struct mips_sim *state, rtx insn)
14807 mips_sim_insn = insn;
14808 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
14811 /* Advance simulation state STATE until the units required by
14812 instruction INSN are available. */
14814 static void
14815 mips_sim_wait_units (struct mips_sim *state, rtx insn)
14817 state_t tmp_state;
14819 tmp_state = alloca (state_size ());
14820 while (state->insns_left == 0
14821 || (memcpy (tmp_state, state->dfa_state, state_size ()),
14822 state_transition (tmp_state, insn) >= 0))
14823 mips_sim_next_cycle (state);
14826 /* Advance simulation state STATE until INSN is ready to issue. */
14828 static void
14829 mips_sim_wait_insn (struct mips_sim *state, rtx insn)
14831 mips_sim_wait_regs (state, insn);
14832 mips_sim_wait_units (state, insn);
14835 /* mips_sim_insn has just set X. Update the LAST_SET array
14836 in simulation state DATA. */
14838 static void
14839 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
14841 struct mips_sim *state;
14843 state = (struct mips_sim *) data;
14844 if (REG_P (x))
14846 unsigned int regno, end_regno;
14848 end_regno = END_REGNO (x);
14849 for (regno = REGNO (x); regno < end_regno; regno++)
14851 state->last_set[regno].insn = mips_sim_insn;
14852 state->last_set[regno].time = state->time;
14857 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
14858 can issue immediately (i.e., that mips_sim_wait_insn has already
14859 been called). */
14861 static void
14862 mips_sim_issue_insn (struct mips_sim *state, rtx insn)
14864 state_transition (state->dfa_state, insn);
14865 state->insns_left--;
14867 mips_sim_insn = insn;
14868 note_stores (PATTERN (insn), mips_sim_record_set, state);
14871 /* Simulate issuing a NOP in state STATE. */
14873 static void
14874 mips_sim_issue_nop (struct mips_sim *state)
14876 if (state->insns_left == 0)
14877 mips_sim_next_cycle (state);
14878 state->insns_left--;
14881 /* Update simulation state STATE so that it's ready to accept the instruction
14882 after INSN. INSN should be part of the main rtl chain, not a member of a
14883 SEQUENCE. */
14885 static void
14886 mips_sim_finish_insn (struct mips_sim *state, rtx insn)
14888 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
14889 if (JUMP_P (insn))
14890 mips_sim_issue_nop (state);
14892 switch (GET_CODE (SEQ_BEGIN (insn)))
14894 case CODE_LABEL:
14895 case CALL_INSN:
14896 /* We can't predict the processor state after a call or label. */
14897 mips_sim_reset (state);
14898 break;
14900 case JUMP_INSN:
14901 /* The delay slots of branch likely instructions are only executed
14902 when the branch is taken. Therefore, if the caller has simulated
14903 the delay slot instruction, STATE does not really reflect the state
14904 of the pipeline for the instruction after the delay slot. Also,
14905 branch likely instructions tend to incur a penalty when not taken,
14906 so there will probably be an extra delay between the branch and
14907 the instruction after the delay slot. */
14908 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
14909 mips_sim_reset (state);
14910 break;
14912 default:
14913 break;
14917 /* The VR4130 pipeline issues aligned pairs of instructions together,
14918 but it stalls the second instruction if it depends on the first.
14919 In order to cut down the amount of logic required, this dependence
14920 check is not based on a full instruction decode. Instead, any non-SPECIAL
14921 instruction is assumed to modify the register specified by bits 20-16
14922 (which is usually the "rt" field).
14924 In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
14925 input, so we can end up with a false dependence between the branch
14926 and its delay slot. If this situation occurs in instruction INSN,
14927 try to avoid it by swapping rs and rt. */
14929 static void
14930 vr4130_avoid_branch_rt_conflict (rtx insn)
14932 rtx first, second;
14934 first = SEQ_BEGIN (insn);
14935 second = SEQ_END (insn);
14936 if (JUMP_P (first)
14937 && NONJUMP_INSN_P (second)
14938 && GET_CODE (PATTERN (first)) == SET
14939 && GET_CODE (SET_DEST (PATTERN (first))) == PC
14940 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
14942 /* Check for the right kind of condition. */
14943 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
14944 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
14945 && REG_P (XEXP (cond, 0))
14946 && REG_P (XEXP (cond, 1))
14947 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
14948 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
14950 /* SECOND mentions the rt register but not the rs register. */
14951 rtx tmp = XEXP (cond, 0);
14952 XEXP (cond, 0) = XEXP (cond, 1);
14953 XEXP (cond, 1) = tmp;
14958 /* Implement -mvr4130-align. Go through each basic block and simulate the
14959 processor pipeline. If we find that a pair of instructions could execute
14960 in parallel, and the first of those instructions is not 8-byte aligned,
14961 insert a nop to make it aligned. */
14963 static void
14964 vr4130_align_insns (void)
14966 struct mips_sim state;
14967 rtx insn, subinsn, last, last2, next;
14968 bool aligned_p;
14970 dfa_start ();
14972 /* LAST is the last instruction before INSN to have a nonzero length.
14973 LAST2 is the last such instruction before LAST. */
14974 last = 0;
14975 last2 = 0;
14977 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
14978 aligned_p = true;
14980 mips_sim_init (&state, alloca (state_size ()));
14981 for (insn = get_insns (); insn != 0; insn = next)
14983 unsigned int length;
14985 next = NEXT_INSN (insn);
14987 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
14988 This isn't really related to the alignment pass, but we do it on
14989 the fly to avoid a separate instruction walk. */
14990 vr4130_avoid_branch_rt_conflict (insn);
14992 if (USEFUL_INSN_P (insn))
14993 FOR_EACH_SUBINSN (subinsn, insn)
14995 mips_sim_wait_insn (&state, subinsn);
14997 /* If we want this instruction to issue in parallel with the
14998 previous one, make sure that the previous instruction is
14999 aligned. There are several reasons why this isn't worthwhile
15000 when the second instruction is a call:
15002 - Calls are less likely to be performance critical,
15003 - There's a good chance that the delay slot can execute
15004 in parallel with the call.
15005 - The return address would then be unaligned.
15007 In general, if we're going to insert a nop between instructions
15008 X and Y, it's better to insert it immediately after X. That
15009 way, if the nop makes Y aligned, it will also align any labels
15010 between X and Y. */
15011 if (state.insns_left != state.issue_rate
15012 && !CALL_P (subinsn))
15014 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
15016 /* SUBINSN is the first instruction in INSN and INSN is
15017 aligned. We want to align the previous instruction
15018 instead, so insert a nop between LAST2 and LAST.
15020 Note that LAST could be either a single instruction
15021 or a branch with a delay slot. In the latter case,
15022 LAST, like INSN, is already aligned, but the delay
15023 slot must have some extra delay that stops it from
15024 issuing at the same time as the branch. We therefore
15025 insert a nop before the branch in order to align its
15026 delay slot. */
15027 emit_insn_after (gen_nop (), last2);
15028 aligned_p = false;
15030 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
15032 /* SUBINSN is the delay slot of INSN, but INSN is
15033 currently unaligned. Insert a nop between
15034 LAST and INSN to align it. */
15035 emit_insn_after (gen_nop (), last);
15036 aligned_p = true;
15039 mips_sim_issue_insn (&state, subinsn);
15041 mips_sim_finish_insn (&state, insn);
15043 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
15044 length = get_attr_length (insn);
15045 if (length > 0)
15047 /* If the instruction is an asm statement or multi-instruction
15048 mips.md patern, the length is only an estimate. Insert an
15049 8 byte alignment after it so that the following instructions
15050 can be handled correctly. */
15051 if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
15052 && (recog_memoized (insn) < 0 || length >= 8))
15054 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
15055 next = NEXT_INSN (next);
15056 mips_sim_next_cycle (&state);
15057 aligned_p = true;
15059 else if (length & 4)
15060 aligned_p = !aligned_p;
15061 last2 = last;
15062 last = insn;
15065 /* See whether INSN is an aligned label. */
15066 if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
15067 aligned_p = true;
15069 dfa_finish ();
15072 /* This structure records that the current function has a LO_SUM
15073 involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
15074 the largest offset applied to BASE by all such LO_SUMs. */
15075 struct mips_lo_sum_offset {
15076 rtx base;
15077 HOST_WIDE_INT offset;
15080 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE. */
15082 static hashval_t
15083 mips_hash_base (rtx base)
15085 int do_not_record_p;
15087 return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
15090 /* Hash-table callbacks for mips_lo_sum_offsets. */
15092 static hashval_t
15093 mips_lo_sum_offset_hash (const void *entry)
15095 return mips_hash_base (((const struct mips_lo_sum_offset *) entry)->base);
15098 static int
15099 mips_lo_sum_offset_eq (const void *entry, const void *value)
15101 return rtx_equal_p (((const struct mips_lo_sum_offset *) entry)->base,
15102 (const_rtx) value);
15105 /* Look up symbolic constant X in HTAB, which is a hash table of
15106 mips_lo_sum_offsets. If OPTION is NO_INSERT, return true if X can be
15107 paired with a recorded LO_SUM, otherwise record X in the table. */
15109 static bool
15110 mips_lo_sum_offset_lookup (htab_t htab, rtx x, enum insert_option option)
15112 rtx base, offset;
15113 void **slot;
15114 struct mips_lo_sum_offset *entry;
15116 /* Split X into a base and offset. */
15117 split_const (x, &base, &offset);
15118 if (UNSPEC_ADDRESS_P (base))
15119 base = UNSPEC_ADDRESS (base);
15121 /* Look up the base in the hash table. */
15122 slot = htab_find_slot_with_hash (htab, base, mips_hash_base (base), option);
15123 if (slot == NULL)
15124 return false;
15126 entry = (struct mips_lo_sum_offset *) *slot;
15127 if (option == INSERT)
15129 if (entry == NULL)
15131 entry = XNEW (struct mips_lo_sum_offset);
15132 entry->base = base;
15133 entry->offset = INTVAL (offset);
15134 *slot = entry;
15136 else
15138 if (INTVAL (offset) > entry->offset)
15139 entry->offset = INTVAL (offset);
15142 return INTVAL (offset) <= entry->offset;
15145 /* A for_each_rtx callback for which DATA is a mips_lo_sum_offset hash table.
15146 Record every LO_SUM in *LOC. */
15148 static int
15149 mips_record_lo_sum (rtx *loc, void *data)
15151 if (GET_CODE (*loc) == LO_SUM)
15152 mips_lo_sum_offset_lookup ((htab_t) data, XEXP (*loc, 1), INSERT);
15153 return 0;
15156 /* Return true if INSN is a SET of an orphaned high-part relocation.
15157 HTAB is a hash table of mips_lo_sum_offsets that describes all the
15158 LO_SUMs in the current function. */
15160 static bool
15161 mips_orphaned_high_part_p (htab_t htab, rtx insn)
15163 enum mips_symbol_type type;
15164 rtx x, set;
15166 set = single_set (insn);
15167 if (set)
15169 /* Check for %his. */
15170 x = SET_SRC (set);
15171 if (GET_CODE (x) == HIGH
15172 && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
15173 return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
15175 /* Check for local %gots (and %got_pages, which is redundant but OK). */
15176 if (GET_CODE (x) == UNSPEC
15177 && XINT (x, 1) == UNSPEC_LOAD_GOT
15178 && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
15179 SYMBOL_CONTEXT_LEA, &type)
15180 && type == SYMBOL_GOTOFF_PAGE)
15181 return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
15183 return false;
15186 /* Subroutine of mips_reorg_process_insns. If there is a hazard between
15187 INSN and a previous instruction, avoid it by inserting nops after
15188 instruction AFTER.
15190 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
15191 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
15192 before using the value of that register. *HILO_DELAY counts the
15193 number of instructions since the last hilo hazard (that is,
15194 the number of instructions since the last MFLO or MFHI).
15196 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
15197 for the next instruction.
15199 LO_REG is an rtx for the LO register, used in dependence checking. */
15201 static void
15202 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
15203 rtx *delayed_reg, rtx lo_reg)
15205 rtx pattern, set;
15206 int nops, ninsns;
15208 pattern = PATTERN (insn);
15210 /* Do not put the whole function in .set noreorder if it contains
15211 an asm statement. We don't know whether there will be hazards
15212 between the asm statement and the gcc-generated code. */
15213 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
15214 cfun->machine->all_noreorder_p = false;
15216 /* Ignore zero-length instructions (barriers and the like). */
15217 ninsns = get_attr_length (insn) / 4;
15218 if (ninsns == 0)
15219 return;
15221 /* Work out how many nops are needed. Note that we only care about
15222 registers that are explicitly mentioned in the instruction's pattern.
15223 It doesn't matter that calls use the argument registers or that they
15224 clobber hi and lo. */
15225 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
15226 nops = 2 - *hilo_delay;
15227 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
15228 nops = 1;
15229 else
15230 nops = 0;
15232 /* Insert the nops between this instruction and the previous one.
15233 Each new nop takes us further from the last hilo hazard. */
15234 *hilo_delay += nops;
15235 while (nops-- > 0)
15236 emit_insn_after (gen_hazard_nop (), after);
15238 /* Set up the state for the next instruction. */
15239 *hilo_delay += ninsns;
15240 *delayed_reg = 0;
15241 if (INSN_CODE (insn) >= 0)
15242 switch (get_attr_hazard (insn))
15244 case HAZARD_NONE:
15245 break;
15247 case HAZARD_HILO:
15248 *hilo_delay = 0;
15249 break;
15251 case HAZARD_DELAY:
15252 set = single_set (insn);
15253 gcc_assert (set);
15254 *delayed_reg = SET_DEST (set);
15255 break;
15259 /* Go through the instruction stream and insert nops where necessary.
15260 Also delete any high-part relocations whose partnering low parts
15261 are now all dead. See if the whole function can then be put into
15262 .set noreorder and .set nomacro. */
15264 static void
15265 mips_reorg_process_insns (void)
15267 rtx insn, last_insn, subinsn, next_insn, lo_reg, delayed_reg;
15268 int hilo_delay;
15269 htab_t htab;
15271 /* Force all instructions to be split into their final form. */
15272 split_all_insns_noflow ();
15274 /* Recalculate instruction lengths without taking nops into account. */
15275 cfun->machine->ignore_hazard_length_p = true;
15276 shorten_branches (get_insns ());
15278 cfun->machine->all_noreorder_p = true;
15280 /* We don't track MIPS16 PC-relative offsets closely enough to make
15281 a good job of "set .noreorder" code in MIPS16 mode. */
15282 if (TARGET_MIPS16)
15283 cfun->machine->all_noreorder_p = false;
15285 /* Code that doesn't use explicit relocs can't be ".set nomacro". */
15286 if (!TARGET_EXPLICIT_RELOCS)
15287 cfun->machine->all_noreorder_p = false;
15289 /* Profiled functions can't be all noreorder because the profiler
15290 support uses assembler macros. */
15291 if (crtl->profile)
15292 cfun->machine->all_noreorder_p = false;
15294 /* Code compiled with -mfix-vr4120 or -mfix-24k can't be all noreorder
15295 because we rely on the assembler to work around some errata. */
15296 if (TARGET_FIX_VR4120 || TARGET_FIX_24K)
15297 cfun->machine->all_noreorder_p = false;
15299 /* The same is true for -mfix-vr4130 if we might generate MFLO or
15300 MFHI instructions. Note that we avoid using MFLO and MFHI if
15301 the VR4130 MACC and DMACC instructions are available instead;
15302 see the *mfhilo_{si,di}_macc patterns. */
15303 if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
15304 cfun->machine->all_noreorder_p = false;
15306 htab = htab_create (37, mips_lo_sum_offset_hash,
15307 mips_lo_sum_offset_eq, free);
15309 /* Make a first pass over the instructions, recording all the LO_SUMs. */
15310 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
15311 FOR_EACH_SUBINSN (subinsn, insn)
15312 if (USEFUL_INSN_P (subinsn))
15313 for_each_rtx (&PATTERN (subinsn), mips_record_lo_sum, htab);
15315 last_insn = 0;
15316 hilo_delay = 2;
15317 delayed_reg = 0;
15318 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
15320 /* Make a second pass over the instructions. Delete orphaned
15321 high-part relocations or turn them into NOPs. Avoid hazards
15322 by inserting NOPs. */
15323 for (insn = get_insns (); insn != 0; insn = next_insn)
15325 next_insn = NEXT_INSN (insn);
15326 if (USEFUL_INSN_P (insn))
15328 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
15330 /* If we find an orphaned high-part relocation in a delay
15331 slot, it's easier to turn that instruction into a NOP than
15332 to delete it. The delay slot will be a NOP either way. */
15333 FOR_EACH_SUBINSN (subinsn, insn)
15334 if (INSN_P (subinsn))
15336 if (mips_orphaned_high_part_p (htab, subinsn))
15338 PATTERN (subinsn) = gen_nop ();
15339 INSN_CODE (subinsn) = CODE_FOR_nop;
15341 mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
15342 &delayed_reg, lo_reg);
15344 last_insn = insn;
15346 else
15348 /* INSN is a single instruction. Delete it if it's an
15349 orphaned high-part relocation. */
15350 if (mips_orphaned_high_part_p (htab, insn))
15351 delete_insn (insn);
15352 /* Also delete cache barriers if the last instruction
15353 was an annulled branch. INSN will not be speculatively
15354 executed. */
15355 else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
15356 && last_insn
15357 && JUMP_P (SEQ_BEGIN (last_insn))
15358 && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
15359 delete_insn (insn);
15360 else
15362 mips_avoid_hazard (last_insn, insn, &hilo_delay,
15363 &delayed_reg, lo_reg);
15364 last_insn = insn;
15370 htab_delete (htab);
15373 /* If we are using a GOT, but have not decided to use a global pointer yet,
15374 see whether we need one to implement long branches. Convert the ghost
15375 global-pointer instructions into real ones if so. */
15377 static bool
15378 mips_expand_ghost_gp_insns (void)
15380 rtx insn;
15381 int normal_length;
15383 /* Quick exit if we already know that we will or won't need a
15384 global pointer. */
15385 if (!TARGET_USE_GOT
15386 || cfun->machine->global_pointer == INVALID_REGNUM
15387 || mips_must_initialize_gp_p ())
15388 return false;
15390 shorten_branches (get_insns ());
15392 /* Look for a branch that is longer than normal. The normal length for
15393 non-MIPS16 branches is 8, because the length includes the delay slot.
15394 It is 4 for MIPS16, because MIPS16 branches are extended instructions,
15395 but they have no delay slot. */
15396 normal_length = (TARGET_MIPS16 ? 4 : 8);
15397 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15398 if (JUMP_P (insn)
15399 && USEFUL_INSN_P (insn)
15400 && get_attr_length (insn) > normal_length)
15401 break;
15403 if (insn == NULL_RTX)
15404 return false;
15406 /* We've now established that we need $gp. */
15407 cfun->machine->must_initialize_gp_p = true;
15408 split_all_insns_noflow ();
15410 return true;
15413 /* Subroutine of mips_reorg to manage passes that require DF. */
15415 static void
15416 mips_df_reorg (void)
15418 /* Create def-use chains. */
15419 df_set_flags (DF_EQ_NOTES);
15420 df_chain_add_problem (DF_UD_CHAIN);
15421 df_analyze ();
15423 if (TARGET_RELAX_PIC_CALLS)
15424 mips_annotate_pic_calls ();
15426 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
15427 r10k_insert_cache_barriers ();
15429 df_finish_pass (false);
15432 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
15434 static void
15435 mips_reorg (void)
15437 /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF. Also during
15438 insn splitting in mips16_lay_out_constants, DF insn info is only kept up
15439 to date if the CFG is available. */
15440 if (mips_cfg_in_reorg ())
15441 compute_bb_for_insn ();
15442 mips16_lay_out_constants ();
15443 if (mips_cfg_in_reorg ())
15445 mips_df_reorg ();
15446 free_bb_for_insn ();
15449 if (optimize > 0 && flag_delayed_branch)
15450 dbr_schedule (get_insns ());
15451 mips_reorg_process_insns ();
15452 if (!TARGET_MIPS16
15453 && TARGET_EXPLICIT_RELOCS
15454 && TUNE_MIPS4130
15455 && TARGET_VR4130_ALIGN)
15456 vr4130_align_insns ();
15457 if (mips_expand_ghost_gp_insns ())
15458 /* The expansion could invalidate some of the VR4130 alignment
15459 optimizations, but this should be an extremely rare case anyhow. */
15460 mips_reorg_process_insns ();
15463 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
15464 in order to avoid duplicating too much logic from elsewhere. */
15466 static void
15467 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
15468 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
15469 tree function)
15471 rtx this_rtx, temp1, temp2, insn, fnaddr;
15472 bool use_sibcall_p;
15474 /* Pretend to be a post-reload pass while generating rtl. */
15475 reload_completed = 1;
15477 /* Mark the end of the (empty) prologue. */
15478 emit_note (NOTE_INSN_PROLOGUE_END);
15480 /* Determine if we can use a sibcall to call FUNCTION directly. */
15481 fnaddr = XEXP (DECL_RTL (function), 0);
15482 use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
15483 && const_call_insn_operand (fnaddr, Pmode));
15485 /* Determine if we need to load FNADDR from the GOT. */
15486 if (!use_sibcall_p
15487 && (mips_got_symbol_type_p
15488 (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
15490 /* Pick a global pointer. Use a call-clobbered register if
15491 TARGET_CALL_SAVED_GP. */
15492 cfun->machine->global_pointer
15493 = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
15494 cfun->machine->must_initialize_gp_p = true;
15495 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
15497 /* Set up the global pointer for n32 or n64 abicalls. */
15498 mips_emit_loadgp ();
15501 /* We need two temporary registers in some cases. */
15502 temp1 = gen_rtx_REG (Pmode, 2);
15503 temp2 = gen_rtx_REG (Pmode, 3);
15505 /* Find out which register contains the "this" pointer. */
15506 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
15507 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
15508 else
15509 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
15511 /* Add DELTA to THIS_RTX. */
15512 if (delta != 0)
15514 rtx offset = GEN_INT (delta);
15515 if (!SMALL_OPERAND (delta))
15517 mips_emit_move (temp1, offset);
15518 offset = temp1;
15520 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
15523 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
15524 if (vcall_offset != 0)
15526 rtx addr;
15528 /* Set TEMP1 to *THIS_RTX. */
15529 mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
15531 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
15532 addr = mips_add_offset (temp2, temp1, vcall_offset);
15534 /* Load the offset and add it to THIS_RTX. */
15535 mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
15536 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
15539 /* Jump to the target function. Use a sibcall if direct jumps are
15540 allowed, otherwise load the address into a register first. */
15541 if (use_sibcall_p)
15543 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
15544 SIBLING_CALL_P (insn) = 1;
15546 else
15548 /* This is messy. GAS treats "la $25,foo" as part of a call
15549 sequence and may allow a global "foo" to be lazily bound.
15550 The general move patterns therefore reject this combination.
15552 In this context, lazy binding would actually be OK
15553 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
15554 TARGET_CALL_SAVED_GP; see mips_load_call_address.
15555 We must therefore load the address via a temporary
15556 register if mips_dangerous_for_la25_p.
15558 If we jump to the temporary register rather than $25,
15559 the assembler can use the move insn to fill the jump's
15560 delay slot.
15562 We can use the same technique for MIPS16 code, where $25
15563 is not a valid JR register. */
15564 if (TARGET_USE_PIC_FN_ADDR_REG
15565 && !TARGET_MIPS16
15566 && !mips_dangerous_for_la25_p (fnaddr))
15567 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
15568 mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
15570 if (TARGET_USE_PIC_FN_ADDR_REG
15571 && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
15572 mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
15573 emit_jump_insn (gen_indirect_jump (temp1));
15576 /* Run just enough of rest_of_compilation. This sequence was
15577 "borrowed" from alpha.c. */
15578 insn = get_insns ();
15579 insn_locators_alloc ();
15580 split_all_insns_noflow ();
15581 mips16_lay_out_constants ();
15582 shorten_branches (insn);
15583 final_start_function (insn, file, 1);
15584 final (insn, file, 1);
15585 final_end_function ();
15587 /* Clean up the vars set above. Note that final_end_function resets
15588 the global pointer for us. */
15589 reload_completed = 0;
15592 /* The last argument passed to mips_set_mips16_mode, or negative if the
15593 function hasn't been called yet. */
15594 static int was_mips16_p = -1;
15596 /* Set up the target-dependent global state so that it matches the
15597 current function's ISA mode. */
15599 static void
15600 mips_set_mips16_mode (int mips16_p)
15602 if (mips16_p == was_mips16_p)
15603 return;
15605 /* Restore base settings of various flags. */
15606 target_flags = mips_base_target_flags;
15607 flag_schedule_insns = mips_base_schedule_insns;
15608 flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
15609 flag_move_loop_invariants = mips_base_move_loop_invariants;
15610 align_loops = mips_base_align_loops;
15611 align_jumps = mips_base_align_jumps;
15612 align_functions = mips_base_align_functions;
15614 if (mips16_p)
15616 /* Switch to MIPS16 mode. */
15617 target_flags |= MASK_MIPS16;
15619 /* Don't run the scheduler before reload, since it tends to
15620 increase register pressure. */
15621 flag_schedule_insns = 0;
15623 /* Don't do hot/cold partitioning. mips16_lay_out_constants expects
15624 the whole function to be in a single section. */
15625 flag_reorder_blocks_and_partition = 0;
15627 /* Don't move loop invariants, because it tends to increase
15628 register pressure. It also introduces an extra move in cases
15629 where the constant is the first operand in a two-operand binary
15630 instruction, or when it forms a register argument to a functon
15631 call. */
15632 flag_move_loop_invariants = 0;
15634 target_flags |= MASK_EXPLICIT_RELOCS;
15636 /* Experiments suggest we get the best overall section-anchor
15637 results from using the range of an unextended LW or SW. Code
15638 that makes heavy use of byte or short accesses can do better
15639 with ranges of 0...31 and 0...63 respectively, but most code is
15640 sensitive to the range of LW and SW instead. */
15641 targetm.min_anchor_offset = 0;
15642 targetm.max_anchor_offset = 127;
15644 targetm.const_anchor = 0;
15646 /* MIPS16 has no BAL instruction. */
15647 target_flags &= ~MASK_RELAX_PIC_CALLS;
15649 /* The R4000 errata don't apply to any known MIPS16 cores.
15650 It's simpler to make the R4000 fixes and MIPS16 mode
15651 mutually exclusive. */
15652 target_flags &= ~MASK_FIX_R4000;
15654 if (flag_pic && !TARGET_OLDABI)
15655 sorry ("MIPS16 PIC for ABIs other than o32 and o64");
15657 if (TARGET_XGOT)
15658 sorry ("MIPS16 -mxgot code");
15660 if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
15661 sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
15663 else
15665 /* Switch to normal (non-MIPS16) mode. */
15666 target_flags &= ~MASK_MIPS16;
15668 /* Provide default values for align_* for 64-bit targets. */
15669 if (TARGET_64BIT)
15671 if (align_loops == 0)
15672 align_loops = 8;
15673 if (align_jumps == 0)
15674 align_jumps = 8;
15675 if (align_functions == 0)
15676 align_functions = 8;
15679 targetm.min_anchor_offset = -32768;
15680 targetm.max_anchor_offset = 32767;
15682 targetm.const_anchor = 0x8000;
15685 /* (Re)initialize MIPS target internals for new ISA. */
15686 mips_init_relocs ();
15688 if (mips16_p)
15690 if (!mips16_globals)
15691 mips16_globals = save_target_globals ();
15692 else
15693 restore_target_globals (mips16_globals);
15695 else
15696 restore_target_globals (&default_target_globals);
15698 was_mips16_p = mips16_p;
15701 /* Implement TARGET_SET_CURRENT_FUNCTION. Decide whether the current
15702 function should use the MIPS16 ISA and switch modes accordingly. */
15704 static void
15705 mips_set_current_function (tree fndecl)
15707 mips_set_mips16_mode (mips_use_mips16_mode_p (fndecl));
15710 /* Allocate a chunk of memory for per-function machine-dependent data. */
15712 static struct machine_function *
15713 mips_init_machine_status (void)
15715 return ggc_alloc_cleared_machine_function ();
15718 /* Return the processor associated with the given ISA level, or null
15719 if the ISA isn't valid. */
15721 static const struct mips_cpu_info *
15722 mips_cpu_info_from_isa (int isa)
15724 unsigned int i;
15726 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
15727 if (mips_cpu_info_table[i].isa == isa)
15728 return mips_cpu_info_table + i;
15730 return NULL;
15733 /* Return a mips_cpu_info entry determined by an option valued
15734 OPT. */
15736 static const struct mips_cpu_info *
15737 mips_cpu_info_from_opt (int opt)
15739 switch (opt)
15741 case MIPS_ARCH_OPTION_FROM_ABI:
15742 /* 'from-abi' selects the most compatible architecture for the
15743 given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
15744 ABIs. For the EABIs, we have to decide whether we're using
15745 the 32-bit or 64-bit version. */
15746 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
15747 : ABI_NEEDS_64BIT_REGS ? 3
15748 : (TARGET_64BIT ? 3 : 1));
15750 case MIPS_ARCH_OPTION_NATIVE:
15751 gcc_unreachable ();
15753 default:
15754 return &mips_cpu_info_table[opt];
15758 /* Return a default mips_cpu_info entry, given that no -march= option
15759 was explicitly specified. */
15761 static const struct mips_cpu_info *
15762 mips_default_arch (void)
15764 #if defined (MIPS_CPU_STRING_DEFAULT)
15765 unsigned int i;
15766 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
15767 if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
15768 return mips_cpu_info_table + i;
15769 gcc_unreachable ();
15770 #elif defined (MIPS_ISA_DEFAULT)
15771 return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
15772 #else
15773 /* 'from-abi' makes a good default: you get whatever the ABI
15774 requires. */
15775 return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
15776 #endif
15779 /* Set up globals to generate code for the ISA or processor
15780 described by INFO. */
15782 static void
15783 mips_set_architecture (const struct mips_cpu_info *info)
15785 if (info != 0)
15787 mips_arch_info = info;
15788 mips_arch = info->cpu;
15789 mips_isa = info->isa;
15793 /* Likewise for tuning. */
15795 static void
15796 mips_set_tune (const struct mips_cpu_info *info)
15798 if (info != 0)
15800 mips_tune_info = info;
15801 mips_tune = info->cpu;
15805 /* Implement TARGET_OPTION_OVERRIDE. */
15807 static void
15808 mips_option_override (void)
15810 int i, start, regno, mode;
15812 if (global_options_set.x_mips_isa_option)
15813 mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
15815 /* Process flags as though we were generating non-MIPS16 code. */
15816 mips_base_mips16 = TARGET_MIPS16;
15817 target_flags &= ~MASK_MIPS16;
15819 #ifdef SUBTARGET_OVERRIDE_OPTIONS
15820 SUBTARGET_OVERRIDE_OPTIONS;
15821 #endif
15823 /* -mno-float overrides -mhard-float and -msoft-float. */
15824 if (TARGET_NO_FLOAT)
15826 target_flags |= MASK_SOFT_FLOAT_ABI;
15827 target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
15830 if (TARGET_FLIP_MIPS16)
15831 TARGET_INTERLINK_MIPS16 = 1;
15833 /* Set the small data limit. */
15834 mips_small_data_threshold = (global_options_set.x_g_switch_value
15835 ? g_switch_value
15836 : MIPS_DEFAULT_GVALUE);
15838 /* The following code determines the architecture and register size.
15839 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
15840 The GAS and GCC code should be kept in sync as much as possible. */
15842 if (global_options_set.x_mips_arch_option)
15843 mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
15845 if (mips_isa_option_info != 0)
15847 if (mips_arch_info == 0)
15848 mips_set_architecture (mips_isa_option_info);
15849 else if (mips_arch_info->isa != mips_isa_option_info->isa)
15850 error ("%<-%s%> conflicts with the other architecture options, "
15851 "which specify a %s processor",
15852 mips_isa_option_info->name,
15853 mips_cpu_info_from_isa (mips_arch_info->isa)->name);
15856 if (mips_arch_info == 0)
15857 mips_set_architecture (mips_default_arch ());
15859 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
15860 error ("%<-march=%s%> is not compatible with the selected ABI",
15861 mips_arch_info->name);
15863 /* Optimize for mips_arch, unless -mtune selects a different processor. */
15864 if (global_options_set.x_mips_tune_option)
15865 mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
15867 if (mips_tune_info == 0)
15868 mips_set_tune (mips_arch_info);
15870 if ((target_flags_explicit & MASK_64BIT) != 0)
15872 /* The user specified the size of the integer registers. Make sure
15873 it agrees with the ABI and ISA. */
15874 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
15875 error ("%<-mgp64%> used with a 32-bit processor");
15876 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
15877 error ("%<-mgp32%> used with a 64-bit ABI");
15878 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
15879 error ("%<-mgp64%> used with a 32-bit ABI");
15881 else
15883 /* Infer the integer register size from the ABI and processor.
15884 Restrict ourselves to 32-bit registers if that's all the
15885 processor has, or if the ABI cannot handle 64-bit registers. */
15886 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
15887 target_flags &= ~MASK_64BIT;
15888 else
15889 target_flags |= MASK_64BIT;
15892 if ((target_flags_explicit & MASK_FLOAT64) != 0)
15894 if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
15895 error ("unsupported combination: %s", "-mfp64 -msingle-float");
15896 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
15897 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
15898 else if (!TARGET_64BIT && TARGET_FLOAT64)
15900 if (!ISA_HAS_MXHC1)
15901 error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
15902 " the target supports the mfhc1 and mthc1 instructions");
15903 else if (mips_abi != ABI_32)
15904 error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
15905 " the o32 ABI");
15908 else
15910 /* -msingle-float selects 32-bit float registers. Otherwise the
15911 float registers should be the same size as the integer ones. */
15912 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
15913 target_flags |= MASK_FLOAT64;
15914 else
15915 target_flags &= ~MASK_FLOAT64;
15918 /* End of code shared with GAS. */
15920 /* If a -mlong* option was given, check that it matches the ABI,
15921 otherwise infer the -mlong* setting from the other options. */
15922 if ((target_flags_explicit & MASK_LONG64) != 0)
15924 if (TARGET_LONG64)
15926 if (mips_abi == ABI_N32)
15927 error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
15928 else if (mips_abi == ABI_32)
15929 error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
15930 else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
15931 /* We have traditionally allowed non-abicalls code to use
15932 an LP64 form of o64. However, it would take a bit more
15933 effort to support the combination of 32-bit GOT entries
15934 and 64-bit pointers, so we treat the abicalls case as
15935 an error. */
15936 error ("the combination of %qs and %qs is incompatible with %qs",
15937 "-mabi=o64", "-mabicalls", "-mlong64");
15939 else
15941 if (mips_abi == ABI_64)
15942 error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
15945 else
15947 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
15948 target_flags |= MASK_LONG64;
15949 else
15950 target_flags &= ~MASK_LONG64;
15953 if (!TARGET_OLDABI)
15954 flag_pcc_struct_return = 0;
15956 /* Decide which rtx_costs structure to use. */
15957 if (optimize_size)
15958 mips_cost = &mips_rtx_cost_optimize_size;
15959 else
15960 mips_cost = &mips_rtx_cost_data[mips_tune];
15962 /* If the user hasn't specified a branch cost, use the processor's
15963 default. */
15964 if (mips_branch_cost == 0)
15965 mips_branch_cost = mips_cost->branch_cost;
15967 /* If neither -mbranch-likely nor -mno-branch-likely was given
15968 on the command line, set MASK_BRANCHLIKELY based on the target
15969 architecture and tuning flags. Annulled delay slots are a
15970 size win, so we only consider the processor-specific tuning
15971 for !optimize_size. */
15972 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
15974 if (ISA_HAS_BRANCHLIKELY
15975 && (optimize_size
15976 || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
15977 target_flags |= MASK_BRANCHLIKELY;
15978 else
15979 target_flags &= ~MASK_BRANCHLIKELY;
15981 else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
15982 warning (0, "the %qs architecture does not support branch-likely"
15983 " instructions", mips_arch_info->name);
15985 /* The effect of -mabicalls isn't defined for the EABI. */
15986 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
15988 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
15989 target_flags &= ~MASK_ABICALLS;
15992 if (TARGET_ABICALLS_PIC2)
15993 /* We need to set flag_pic for executables as well as DSOs
15994 because we may reference symbols that are not defined in
15995 the final executable. (MIPS does not use things like
15996 copy relocs, for example.)
15998 There is a body of code that uses __PIC__ to distinguish
15999 between -mabicalls and -mno-abicalls code. The non-__PIC__
16000 variant is usually appropriate for TARGET_ABICALLS_PIC0, as
16001 long as any indirect jumps use $25. */
16002 flag_pic = 1;
16004 /* -mvr4130-align is a "speed over size" optimization: it usually produces
16005 faster code, but at the expense of more nops. Enable it at -O3 and
16006 above. */
16007 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
16008 target_flags |= MASK_VR4130_ALIGN;
16010 /* Prefer a call to memcpy over inline code when optimizing for size,
16011 though see MOVE_RATIO in mips.h. */
16012 if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
16013 target_flags |= MASK_MEMCPY;
16015 /* If we have a nonzero small-data limit, check that the -mgpopt
16016 setting is consistent with the other target flags. */
16017 if (mips_small_data_threshold > 0)
16019 if (!TARGET_GPOPT)
16021 if (!TARGET_EXPLICIT_RELOCS)
16022 error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
16024 TARGET_LOCAL_SDATA = false;
16025 TARGET_EXTERN_SDATA = false;
16027 else
16029 if (TARGET_VXWORKS_RTP)
16030 warning (0, "cannot use small-data accesses for %qs", "-mrtp");
16032 if (TARGET_ABICALLS)
16033 warning (0, "cannot use small-data accesses for %qs",
16034 "-mabicalls");
16038 /* Make sure that the user didn't turn off paired single support when
16039 MIPS-3D support is requested. */
16040 if (TARGET_MIPS3D
16041 && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
16042 && !TARGET_PAIRED_SINGLE_FLOAT)
16043 error ("%<-mips3d%> requires %<-mpaired-single%>");
16045 /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT. */
16046 if (TARGET_MIPS3D)
16047 target_flags |= MASK_PAIRED_SINGLE_FLOAT;
16049 /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
16050 and TARGET_HARD_FLOAT_ABI are both true. */
16051 if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
16052 error ("%qs must be used with %qs",
16053 TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
16054 TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
16056 /* Make sure that the ISA supports TARGET_PAIRED_SINGLE_FLOAT when it is
16057 enabled. */
16058 if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
16059 warning (0, "the %qs architecture does not support paired-single"
16060 " instructions", mips_arch_info->name);
16062 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
16063 && !TARGET_CACHE_BUILTIN)
16065 error ("%qs requires a target that provides the %qs instruction",
16066 "-mr10k-cache-barrier", "cache");
16067 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
16070 /* If TARGET_DSPR2, enable MASK_DSP. */
16071 if (TARGET_DSPR2)
16072 target_flags |= MASK_DSP;
16074 /* .eh_frame addresses should be the same width as a C pointer.
16075 Most MIPS ABIs support only one pointer size, so the assembler
16076 will usually know exactly how big an .eh_frame address is.
16078 Unfortunately, this is not true of the 64-bit EABI. The ABI was
16079 originally defined to use 64-bit pointers (i.e. it is LP64), and
16080 this is still the default mode. However, we also support an n32-like
16081 ILP32 mode, which is selected by -mlong32. The problem is that the
16082 assembler has traditionally not had an -mlong option, so it has
16083 traditionally not known whether we're using the ILP32 or LP64 form.
16085 As it happens, gas versions up to and including 2.19 use _32-bit_
16086 addresses for EABI64 .cfi_* directives. This is wrong for the
16087 default LP64 mode, so we can't use the directives by default.
16088 Moreover, since gas's current behavior is at odds with gcc's
16089 default behavior, it seems unwise to rely on future versions
16090 of gas behaving the same way. We therefore avoid using .cfi
16091 directives for -mlong32 as well. */
16092 if (mips_abi == ABI_EABI && TARGET_64BIT)
16093 flag_dwarf2_cfi_asm = 0;
16095 /* .cfi_* directives generate a read-only section, so fall back on
16096 manual .eh_frame creation if we need the section to be writable. */
16097 if (TARGET_WRITABLE_EH_FRAME)
16098 flag_dwarf2_cfi_asm = 0;
16100 mips_init_print_operand_punct ();
16102 /* Set up array to map GCC register number to debug register number.
16103 Ignore the special purpose register numbers. */
16105 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
16107 mips_dbx_regno[i] = INVALID_REGNUM;
16108 if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
16109 mips_dwarf_regno[i] = i;
16110 else
16111 mips_dwarf_regno[i] = INVALID_REGNUM;
16114 start = GP_DBX_FIRST - GP_REG_FIRST;
16115 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
16116 mips_dbx_regno[i] = i + start;
16118 start = FP_DBX_FIRST - FP_REG_FIRST;
16119 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
16120 mips_dbx_regno[i] = i + start;
16122 /* Accumulator debug registers use big-endian ordering. */
16123 mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
16124 mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
16125 mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
16126 mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
16127 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
16129 mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
16130 mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
16133 /* Set up mips_hard_regno_mode_ok. */
16134 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
16135 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
16136 mips_hard_regno_mode_ok[mode][regno]
16137 = mips_hard_regno_mode_ok_p (regno, (enum machine_mode) mode);
16139 /* Function to allocate machine-dependent function status. */
16140 init_machine_status = &mips_init_machine_status;
16142 /* Default to working around R4000 errata only if the processor
16143 was selected explicitly. */
16144 if ((target_flags_explicit & MASK_FIX_R4000) == 0
16145 && strcmp (mips_arch_info->name, "r4000") == 0)
16146 target_flags |= MASK_FIX_R4000;
16148 /* Default to working around R4400 errata only if the processor
16149 was selected explicitly. */
16150 if ((target_flags_explicit & MASK_FIX_R4400) == 0
16151 && strcmp (mips_arch_info->name, "r4400") == 0)
16152 target_flags |= MASK_FIX_R4400;
16154 /* Default to working around R10000 errata only if the processor
16155 was selected explicitly. */
16156 if ((target_flags_explicit & MASK_FIX_R10000) == 0
16157 && strcmp (mips_arch_info->name, "r10000") == 0)
16158 target_flags |= MASK_FIX_R10000;
16160 /* Make sure that branch-likely instructions available when using
16161 -mfix-r10000. The instructions are not available if either:
16163 1. -mno-branch-likely was passed.
16164 2. The selected ISA does not support branch-likely and
16165 the command line does not include -mbranch-likely. */
16166 if (TARGET_FIX_R10000
16167 && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
16168 ? !ISA_HAS_BRANCHLIKELY
16169 : !TARGET_BRANCHLIKELY))
16170 sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
16172 if (TARGET_SYNCI && !ISA_HAS_SYNCI)
16174 warning (0, "the %qs architecture does not support the synci "
16175 "instruction", mips_arch_info->name);
16176 target_flags &= ~MASK_SYNCI;
16179 /* Only optimize PIC indirect calls if they are actually required. */
16180 if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
16181 target_flags &= ~MASK_RELAX_PIC_CALLS;
16183 /* Save base state of options. */
16184 mips_base_target_flags = target_flags;
16185 mips_base_schedule_insns = flag_schedule_insns;
16186 mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
16187 mips_base_move_loop_invariants = flag_move_loop_invariants;
16188 mips_base_align_loops = align_loops;
16189 mips_base_align_jumps = align_jumps;
16190 mips_base_align_functions = align_functions;
16192 /* Now select the ISA mode.
16194 Do all CPP-sensitive stuff in non-MIPS16 mode; we'll switch to
16195 MIPS16 mode afterwards if need be. */
16196 mips_set_mips16_mode (false);
16199 /* Swap the register information for registers I and I + 1, which
16200 currently have the wrong endianness. Note that the registers'
16201 fixedness and call-clobberedness might have been set on the
16202 command line. */
16204 static void
16205 mips_swap_registers (unsigned int i)
16207 int tmpi;
16208 const char *tmps;
16210 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
16211 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
16213 SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
16214 SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
16215 SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
16216 SWAP_STRING (reg_names[i], reg_names[i + 1]);
16218 #undef SWAP_STRING
16219 #undef SWAP_INT
16222 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
16224 static void
16225 mips_conditional_register_usage (void)
16228 if (ISA_HAS_DSP)
16230 /* These DSP control register fields are global. */
16231 global_regs[CCDSP_PO_REGNUM] = 1;
16232 global_regs[CCDSP_SC_REGNUM] = 1;
16234 else
16235 AND_COMPL_HARD_REG_SET (accessible_reg_set,
16236 reg_class_contents[(int) DSP_ACC_REGS]);
16238 if (!TARGET_HARD_FLOAT)
16240 AND_COMPL_HARD_REG_SET (accessible_reg_set,
16241 reg_class_contents[(int) FP_REGS]);
16242 AND_COMPL_HARD_REG_SET (accessible_reg_set,
16243 reg_class_contents[(int) ST_REGS]);
16245 else if (!ISA_HAS_8CC)
16247 /* We only have a single condition-code register. We implement
16248 this by fixing all the condition-code registers and generating
16249 RTL that refers directly to ST_REG_FIRST. */
16250 AND_COMPL_HARD_REG_SET (accessible_reg_set,
16251 reg_class_contents[(int) ST_REGS]);
16252 SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
16253 fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
16255 if (TARGET_MIPS16)
16257 /* In MIPS16 mode, we permit the $t temporary registers to be used
16258 for reload. We prohibit the unused $s registers, since they
16259 are call-saved, and saving them via a MIPS16 register would
16260 probably waste more time than just reloading the value. */
16261 fixed_regs[18] = call_used_regs[18] = 1;
16262 fixed_regs[19] = call_used_regs[19] = 1;
16263 fixed_regs[20] = call_used_regs[20] = 1;
16264 fixed_regs[21] = call_used_regs[21] = 1;
16265 fixed_regs[22] = call_used_regs[22] = 1;
16266 fixed_regs[23] = call_used_regs[23] = 1;
16267 fixed_regs[26] = call_used_regs[26] = 1;
16268 fixed_regs[27] = call_used_regs[27] = 1;
16269 fixed_regs[30] = call_used_regs[30] = 1;
16271 /* Do not allow HI and LO to be treated as register operands.
16272 There are no MTHI or MTLO instructions (or any real need
16273 for them) and one-way registers cannot easily be reloaded. */
16274 AND_COMPL_HARD_REG_SET (operand_reg_set,
16275 reg_class_contents[(int) MD_REGS]);
16277 /* $f20-$f23 are call-clobbered for n64. */
16278 if (mips_abi == ABI_64)
16280 int regno;
16281 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
16282 call_really_used_regs[regno] = call_used_regs[regno] = 1;
16284 /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
16285 for n32. */
16286 if (mips_abi == ABI_N32)
16288 int regno;
16289 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
16290 call_really_used_regs[regno] = call_used_regs[regno] = 1;
16292 /* Make sure that double-register accumulator values are correctly
16293 ordered for the current endianness. */
16294 if (TARGET_LITTLE_ENDIAN)
16296 unsigned int regno;
16298 mips_swap_registers (MD_REG_FIRST);
16299 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
16300 mips_swap_registers (regno);
16304 /* When generating MIPS16 code, we want to allocate $24 (T_REG) before
16305 other registers for instructions for which it is possible. This
16306 encourages the compiler to use CMP in cases where an XOR would
16307 require some register shuffling. */
16309 void
16310 mips_order_regs_for_local_alloc (void)
16312 int i;
16314 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
16315 reg_alloc_order[i] = i;
16317 if (TARGET_MIPS16)
16319 /* It really doesn't matter where we put register 0, since it is
16320 a fixed register anyhow. */
16321 reg_alloc_order[0] = 24;
16322 reg_alloc_order[24] = 0;
16326 /* Implement EH_USES. */
16328 bool
16329 mips_eh_uses (unsigned int regno)
16331 if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
16333 /* We need to force certain registers to be live in order to handle
16334 PIC long branches correctly. See mips_must_initialize_gp_p for
16335 details. */
16336 if (mips_cfun_has_cprestore_slot_p ())
16338 if (regno == CPRESTORE_SLOT_REGNUM)
16339 return true;
16341 else
16343 if (cfun->machine->global_pointer == regno)
16344 return true;
16348 return false;
16351 /* Implement EPILOGUE_USES. */
16353 bool
16354 mips_epilogue_uses (unsigned int regno)
16356 /* Say that the epilogue uses the return address register. Note that
16357 in the case of sibcalls, the values "used by the epilogue" are
16358 considered live at the start of the called function. */
16359 if (regno == RETURN_ADDR_REGNUM)
16360 return true;
16362 /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
16363 See the comment above load_call<mode> for details. */
16364 if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
16365 return true;
16367 /* An interrupt handler must preserve some registers that are
16368 ordinarily call-clobbered. */
16369 if (cfun->machine->interrupt_handler_p
16370 && mips_interrupt_extra_call_saved_reg_p (regno))
16371 return true;
16373 return false;
16376 /* A for_each_rtx callback. Stop the search if *X is an AT register. */
16378 static int
16379 mips_at_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
16381 return REG_P (*x) && REGNO (*x) == AT_REGNUM;
16384 /* Return true if INSN needs to be wrapped in ".set noat".
16385 INSN has NOPERANDS operands, stored in OPVEC. */
16387 static bool
16388 mips_need_noat_wrapper_p (rtx insn, rtx *opvec, int noperands)
16390 int i;
16392 if (recog_memoized (insn) >= 0)
16393 for (i = 0; i < noperands; i++)
16394 if (for_each_rtx (&opvec[i], mips_at_reg_p, NULL))
16395 return true;
16396 return false;
16399 /* Implement FINAL_PRESCAN_INSN. */
16401 void
16402 mips_final_prescan_insn (rtx insn, rtx *opvec, int noperands)
16404 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
16405 mips_push_asm_switch (&mips_noat);
16408 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN. */
16410 static void
16411 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx insn,
16412 rtx *opvec, int noperands)
16414 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
16415 mips_pop_asm_switch (&mips_noat);
16418 /* Return the function that is used to expand the <u>mulsidi3 pattern.
16419 EXT_CODE is the code of the extension used. Return NULL if widening
16420 multiplication shouldn't be used. */
16422 mulsidi3_gen_fn
16423 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
16425 bool signed_p;
16427 signed_p = ext_code == SIGN_EXTEND;
16428 if (TARGET_64BIT)
16430 /* Don't use widening multiplication with MULT when we have DMUL. Even
16431 with the extension of its input operands DMUL is faster. Note that
16432 the extension is not needed for signed multiplication. In order to
16433 ensure that we always remove the redundant sign-extension in this
16434 case we still expand mulsidi3 for DMUL. */
16435 if (ISA_HAS_DMUL3)
16436 return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
16437 if (TARGET_MIPS16)
16438 return (signed_p
16439 ? gen_mulsidi3_64bit_mips16
16440 : gen_umulsidi3_64bit_mips16);
16441 if (TARGET_FIX_R4000)
16442 return NULL;
16443 return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
16445 else
16447 if (TARGET_MIPS16)
16448 return (signed_p
16449 ? gen_mulsidi3_32bit_mips16
16450 : gen_umulsidi3_32bit_mips16);
16451 if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
16452 return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
16453 return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
16457 /* Return the size in bytes of the trampoline code, padded to
16458 TRAMPOLINE_ALIGNMENT bits. The static chain pointer and target
16459 function address immediately follow. */
16462 mips_trampoline_code_size (void)
16464 if (TARGET_USE_PIC_FN_ADDR_REG)
16465 return 4 * 4;
16466 else if (ptr_mode == DImode)
16467 return 8 * 4;
16468 else if (ISA_HAS_LOAD_DELAY)
16469 return 6 * 4;
16470 else
16471 return 4 * 4;
16474 /* Implement TARGET_TRAMPOLINE_INIT. */
16476 static void
16477 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
16479 rtx addr, end_addr, high, low, opcode, mem;
16480 rtx trampoline[8];
16481 unsigned int i, j;
16482 HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
16484 /* Work out the offsets of the pointers from the start of the
16485 trampoline code. */
16486 end_addr_offset = mips_trampoline_code_size ();
16487 static_chain_offset = end_addr_offset;
16488 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
16490 /* Get pointers to the beginning and end of the code block. */
16491 addr = force_reg (Pmode, XEXP (m_tramp, 0));
16492 end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
16494 #define OP(X) gen_int_mode (X, SImode)
16496 /* Build up the code in TRAMPOLINE. */
16497 i = 0;
16498 if (TARGET_USE_PIC_FN_ADDR_REG)
16500 /* $25 contains the address of the trampoline. Emit code of the form:
16502 l[wd] $1, target_function_offset($25)
16503 l[wd] $static_chain, static_chain_offset($25)
16504 jr $1
16505 move $25,$1. */
16506 trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
16507 target_function_offset,
16508 PIC_FUNCTION_ADDR_REGNUM));
16509 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
16510 static_chain_offset,
16511 PIC_FUNCTION_ADDR_REGNUM));
16512 trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
16513 trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
16515 else if (ptr_mode == DImode)
16517 /* It's too cumbersome to create the full 64-bit address, so let's
16518 instead use:
16520 move $1, $31
16521 bal 1f
16523 1: l[wd] $25, target_function_offset - 12($31)
16524 l[wd] $static_chain, static_chain_offset - 12($31)
16525 jr $25
16526 move $31, $1
16528 where 12 is the offset of "1:" from the start of the code block. */
16529 trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
16530 trampoline[i++] = OP (MIPS_BAL (1));
16531 trampoline[i++] = OP (MIPS_NOP);
16532 trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
16533 target_function_offset - 12,
16534 RETURN_ADDR_REGNUM));
16535 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
16536 static_chain_offset - 12,
16537 RETURN_ADDR_REGNUM));
16538 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
16539 trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
16541 else
16543 /* If the target has load delays, emit:
16545 lui $1, %hi(end_addr)
16546 lw $25, %lo(end_addr + ...)($1)
16547 lw $static_chain, %lo(end_addr + ...)($1)
16548 jr $25
16551 Otherwise emit:
16553 lui $1, %hi(end_addr)
16554 lw $25, %lo(end_addr + ...)($1)
16555 jr $25
16556 lw $static_chain, %lo(end_addr + ...)($1). */
16558 /* Split END_ADDR into %hi and %lo values. Trampolines are aligned
16559 to 64 bits, so the %lo value will have the bottom 3 bits clear. */
16560 high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
16561 NULL, false, OPTAB_WIDEN);
16562 high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
16563 NULL, false, OPTAB_WIDEN);
16564 low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
16566 /* Emit the LUI. */
16567 opcode = OP (MIPS_LUI (AT_REGNUM, 0));
16568 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
16569 NULL, false, OPTAB_WIDEN);
16571 /* Emit the load of the target function. */
16572 opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
16573 target_function_offset - end_addr_offset,
16574 AT_REGNUM));
16575 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
16576 NULL, false, OPTAB_WIDEN);
16578 /* Emit the JR here, if we can. */
16579 if (!ISA_HAS_LOAD_DELAY)
16580 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
16582 /* Emit the load of the static chain register. */
16583 opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
16584 static_chain_offset - end_addr_offset,
16585 AT_REGNUM));
16586 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
16587 NULL, false, OPTAB_WIDEN);
16589 /* Emit the JR, if we couldn't above. */
16590 if (ISA_HAS_LOAD_DELAY)
16592 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
16593 trampoline[i++] = OP (MIPS_NOP);
16597 #undef OP
16599 /* Copy the trampoline code. Leave any padding uninitialized. */
16600 for (j = 0; j < i; j++)
16602 mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
16603 mips_emit_move (mem, trampoline[j]);
16606 /* Set up the static chain pointer field. */
16607 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
16608 mips_emit_move (mem, chain_value);
16610 /* Set up the target function field. */
16611 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
16612 mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
16614 /* Flush the code part of the trampoline. */
16615 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
16616 emit_insn (gen_clear_cache (addr, end_addr));
16619 /* Implement FUNCTION_PROFILER. */
16621 void mips_function_profiler (FILE *file)
16623 if (TARGET_MIPS16)
16624 sorry ("mips16 function profiling");
16625 if (TARGET_LONG_CALLS)
16627 /* For TARGET_LONG_CALLS use $3 for the address of _mcount. */
16628 if (Pmode == DImode)
16629 fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
16630 else
16631 fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
16633 mips_push_asm_switch (&mips_noat);
16634 fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
16635 reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
16636 /* _mcount treats $2 as the static chain register. */
16637 if (cfun->static_chain_decl != NULL)
16638 fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
16639 reg_names[STATIC_CHAIN_REGNUM]);
16640 if (TARGET_MCOUNT_RA_ADDRESS)
16642 /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
16643 ra save location. */
16644 if (cfun->machine->frame.ra_fp_offset == 0)
16645 /* ra not saved, pass zero. */
16646 fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
16647 else
16648 fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
16649 Pmode == DImode ? "dla" : "la", reg_names[12],
16650 cfun->machine->frame.ra_fp_offset,
16651 reg_names[STACK_POINTER_REGNUM]);
16653 if (!TARGET_NEWABI)
16654 fprintf (file,
16655 "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from stack\n",
16656 TARGET_64BIT ? "dsubu" : "subu",
16657 reg_names[STACK_POINTER_REGNUM],
16658 reg_names[STACK_POINTER_REGNUM],
16659 Pmode == DImode ? 16 : 8);
16661 if (TARGET_LONG_CALLS)
16662 fprintf (file, "\tjalr\t%s\n", reg_names[3]);
16663 else
16664 fprintf (file, "\tjal\t_mcount\n");
16665 mips_pop_asm_switch (&mips_noat);
16666 /* _mcount treats $2 as the static chain register. */
16667 if (cfun->static_chain_decl != NULL)
16668 fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
16669 reg_names[2]);
16672 /* Implement TARGET_SHIFT_TRUNCATION_MASK. We want to keep the default
16673 behaviour of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
16674 when TARGET_LOONGSON_VECTORS is true. */
16676 static unsigned HOST_WIDE_INT
16677 mips_shift_truncation_mask (enum machine_mode mode)
16679 if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
16680 return 0;
16682 return GET_MODE_BITSIZE (mode) - 1;
16685 /* Implement TARGET_PREPARE_PCH_SAVE. */
16687 static void
16688 mips_prepare_pch_save (void)
16690 /* We are called in a context where the current MIPS16 vs. non-MIPS16
16691 setting should be irrelevant. The question then is: which setting
16692 makes most sense at load time?
16694 The PCH is loaded before the first token is read. We should never
16695 have switched into MIPS16 mode by that point, and thus should not
16696 have populated mips16_globals. Nor can we load the entire contents
16697 of mips16_globals from the PCH file, because mips16_globals contains
16698 a combination of GGC and non-GGC data.
16700 There is therefore no point in trying save the GGC part of
16701 mips16_globals to the PCH file, or to preserve MIPS16ness across
16702 the PCH save and load. The loading compiler would not have access
16703 to the non-GGC parts of mips16_globals (either from the PCH file,
16704 or from a copy that the loading compiler generated itself) and would
16705 have to call target_reinit anyway.
16707 It therefore seems best to switch back to non-MIPS16 mode at
16708 save time, and to ensure that mips16_globals remains null after
16709 a PCH load. */
16710 mips_set_mips16_mode (false);
16711 mips16_globals = 0;
16714 /* Generate or test for an insn that supports a constant permutation. */
16716 #define MAX_VECT_LEN 8
16718 struct expand_vec_perm_d
16720 rtx target, op0, op1;
16721 unsigned char perm[MAX_VECT_LEN];
16722 enum machine_mode vmode;
16723 unsigned char nelt;
16724 bool one_vector_p;
16725 bool testing_p;
16728 /* Construct (set target (vec_select op0 (parallel perm))) and
16729 return true if that's a valid instruction in the active ISA. */
16731 static bool
16732 mips_expand_vselect (rtx target, rtx op0,
16733 const unsigned char *perm, unsigned nelt)
16735 rtx rperm[MAX_VECT_LEN], x;
16736 unsigned i;
16738 for (i = 0; i < nelt; ++i)
16739 rperm[i] = GEN_INT (perm[i]);
16741 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
16742 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
16743 x = gen_rtx_SET (VOIDmode, target, x);
16745 x = emit_insn (x);
16746 if (recog_memoized (x) < 0)
16748 remove_insn (x);
16749 return false;
16751 return true;
16754 /* Similar, but generate a vec_concat from op0 and op1 as well. */
16756 static bool
16757 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
16758 const unsigned char *perm, unsigned nelt)
16760 enum machine_mode v2mode;
16761 rtx x;
16763 v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
16764 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
16765 return mips_expand_vselect (target, x, perm, nelt);
16768 /* Recognize patterns for even-odd extraction. */
16770 static bool
16771 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
16773 unsigned i, odd, nelt = d->nelt;
16774 rtx t0, t1, t2, t3;
16776 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
16777 return false;
16778 /* Even-odd for V2SI/V2SFmode is matched by interleave directly. */
16779 if (nelt < 4)
16780 return false;
16782 odd = d->perm[0];
16783 if (odd > 1)
16784 return false;
16785 for (i = 1; i < nelt; ++i)
16786 if (d->perm[i] != i * 2 + odd)
16787 return false;
16789 if (d->testing_p)
16790 return true;
16792 /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
16793 t0 = gen_reg_rtx (d->vmode);
16794 t1 = gen_reg_rtx (d->vmode);
16795 switch (d->vmode)
16797 case V4HImode:
16798 emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
16799 emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
16800 if (odd)
16801 emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
16802 else
16803 emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
16804 break;
16806 case V8QImode:
16807 t2 = gen_reg_rtx (d->vmode);
16808 t3 = gen_reg_rtx (d->vmode);
16809 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
16810 emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
16811 emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
16812 emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
16813 if (odd)
16814 emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
16815 else
16816 emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
16817 break;
16819 default:
16820 gcc_unreachable ();
16822 return true;
16825 /* Recognize patterns for the Loongson PSHUFH instruction. */
16827 static bool
16828 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
16830 unsigned i, mask;
16831 rtx rmask;
16833 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
16834 return false;
16835 if (d->vmode != V4HImode)
16836 return false;
16837 if (d->testing_p)
16838 return true;
16840 /* Convert the selector into the packed 8-bit form for pshufh. */
16841 /* Recall that loongson is little-endian only. No big-endian
16842 adjustment required. */
16843 for (i = mask = 0; i < 4; i++)
16844 mask |= (d->perm[i] & 3) << (i * 2);
16845 rmask = force_reg (SImode, GEN_INT (mask));
16847 if (d->one_vector_p)
16848 emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
16849 else
16851 rtx t0, t1, x, merge, rmerge[4];
16853 t0 = gen_reg_rtx (V4HImode);
16854 t1 = gen_reg_rtx (V4HImode);
16855 emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
16856 emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
16858 for (i = 0; i < 4; ++i)
16859 rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
16860 merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
16861 merge = force_reg (V4HImode, merge);
16863 x = gen_rtx_AND (V4HImode, merge, t1);
16864 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
16866 x = gen_rtx_NOT (V4HImode, merge);
16867 x = gen_rtx_AND (V4HImode, x, t0);
16868 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
16870 x = gen_rtx_IOR (V4HImode, t0, t1);
16871 emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
16874 return true;
16877 /* Recognize broadcast patterns for the Loongson. */
16879 static bool
16880 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
16882 unsigned i, elt;
16883 rtx t0, t1;
16885 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
16886 return false;
16887 /* Note that we've already matched V2SI via punpck and V4HI via pshufh. */
16888 if (d->vmode != V8QImode)
16889 return false;
16890 if (!d->one_vector_p)
16891 return false;
16893 elt = d->perm[0];
16894 for (i = 1; i < 8; ++i)
16895 if (d->perm[i] != elt)
16896 return false;
16898 if (d->testing_p)
16899 return true;
16901 /* With one interleave we put two of the desired element adjacent. */
16902 t0 = gen_reg_rtx (V8QImode);
16903 if (elt < 4)
16904 emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
16905 else
16906 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
16908 /* Shuffle that one HImode element into all locations. */
16909 elt &= 3;
16910 elt *= 0x55;
16911 t1 = gen_reg_rtx (V4HImode);
16912 emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
16913 force_reg (SImode, GEN_INT (elt))));
16915 emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
16916 return true;
16919 static bool
16920 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
16922 unsigned int i, nelt = d->nelt;
16923 unsigned char perm2[MAX_VECT_LEN];
16925 if (d->one_vector_p)
16927 /* Try interleave with alternating operands. */
16928 memcpy (perm2, d->perm, sizeof(perm2));
16929 for (i = 1; i < nelt; i += 2)
16930 perm2[i] += nelt;
16931 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
16932 return true;
16934 else
16936 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
16937 d->perm, nelt))
16938 return true;
16940 /* Try again with swapped operands. */
16941 for (i = 0; i < nelt; ++i)
16942 perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
16943 if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
16944 return true;
16947 if (mips_expand_vpc_loongson_even_odd (d))
16948 return true;
16949 if (mips_expand_vpc_loongson_pshufh (d))
16950 return true;
16951 if (mips_expand_vpc_loongson_bcast (d))
16952 return true;
16953 return false;
16956 /* Expand a vec_perm_const pattern. */
16958 bool
16959 mips_expand_vec_perm_const (rtx operands[4])
16961 struct expand_vec_perm_d d;
16962 int i, nelt, which;
16963 unsigned char orig_perm[MAX_VECT_LEN];
16964 rtx sel;
16965 bool ok;
16967 d.target = operands[0];
16968 d.op0 = operands[1];
16969 d.op1 = operands[2];
16970 sel = operands[3];
16972 d.vmode = GET_MODE (d.target);
16973 gcc_assert (VECTOR_MODE_P (d.vmode));
16974 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
16975 d.testing_p = false;
16977 for (i = which = 0; i < nelt; ++i)
16979 rtx e = XVECEXP (sel, 0, i);
16980 int ei = INTVAL (e) & (2 * nelt - 1);
16981 which |= (ei < nelt ? 1 : 2);
16982 orig_perm[i] = ei;
16984 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
16986 switch (which)
16988 default:
16989 gcc_unreachable();
16991 case 3:
16992 d.one_vector_p = false;
16993 if (!rtx_equal_p (d.op0, d.op1))
16994 break;
16995 /* FALLTHRU */
16997 case 2:
16998 for (i = 0; i < nelt; ++i)
16999 d.perm[i] &= nelt - 1;
17000 d.op0 = d.op1;
17001 d.one_vector_p = true;
17002 break;
17004 case 1:
17005 d.op1 = d.op0;
17006 d.one_vector_p = true;
17007 break;
17010 ok = mips_expand_vec_perm_const_1 (&d);
17012 /* If we were given a two-vector permutation which just happened to
17013 have both input vectors equal, we folded this into a one-vector
17014 permutation. There are several loongson patterns that are matched
17015 via direct vec_select+vec_concat expansion, but we do not have
17016 support in mips_expand_vec_perm_const_1 to guess the adjustment
17017 that should be made for a single operand. Just try again with
17018 the original permutation. */
17019 if (!ok && which == 3)
17021 d.op0 = operands[1];
17022 d.op1 = operands[2];
17023 d.one_vector_p = false;
17024 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
17025 ok = mips_expand_vec_perm_const_1 (&d);
17028 return ok;
17031 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK. */
17033 static bool
17034 mips_vectorize_vec_perm_const_ok (enum machine_mode vmode,
17035 const unsigned char *sel)
17037 struct expand_vec_perm_d d;
17038 unsigned int i, nelt, which;
17039 bool ret;
17041 d.vmode = vmode;
17042 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
17043 d.testing_p = true;
17044 memcpy (d.perm, sel, nelt);
17046 /* Categorize the set of elements in the selector. */
17047 for (i = which = 0; i < nelt; ++i)
17049 unsigned char e = d.perm[i];
17050 gcc_assert (e < 2 * nelt);
17051 which |= (e < nelt ? 1 : 2);
17054 /* For all elements from second vector, fold the elements to first. */
17055 if (which == 2)
17056 for (i = 0; i < nelt; ++i)
17057 d.perm[i] -= nelt;
17059 /* Check whether the mask can be applied to the vector type. */
17060 d.one_vector_p = (which != 3);
17062 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
17063 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
17064 if (!d.one_vector_p)
17065 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
17067 start_sequence ();
17068 ret = mips_expand_vec_perm_const_1 (&d);
17069 end_sequence ();
17071 return ret;
17074 /* Expand an integral vector unpack operation. */
17076 void
17077 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
17079 enum machine_mode imode = GET_MODE (operands[1]);
17080 rtx (*unpack) (rtx, rtx, rtx);
17081 rtx (*cmpgt) (rtx, rtx, rtx);
17082 rtx tmp, dest, zero;
17084 switch (imode)
17086 case V8QImode:
17087 if (high_p)
17088 unpack = gen_loongson_punpckhbh;
17089 else
17090 unpack = gen_loongson_punpcklbh;
17091 cmpgt = gen_loongson_pcmpgtb;
17092 break;
17093 case V4HImode:
17094 if (high_p)
17095 unpack = gen_loongson_punpckhhw;
17096 else
17097 unpack = gen_loongson_punpcklhw;
17098 cmpgt = gen_loongson_pcmpgth;
17099 break;
17100 default:
17101 gcc_unreachable ();
17104 zero = force_reg (imode, CONST0_RTX (imode));
17105 if (unsigned_p)
17106 tmp = zero;
17107 else
17109 tmp = gen_reg_rtx (imode);
17110 emit_insn (cmpgt (tmp, zero, operands[1]));
17113 dest = gen_reg_rtx (imode);
17114 emit_insn (unpack (dest, operands[1], tmp));
17116 emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
17119 /* A subroutine of mips_expand_vec_init, match constant vector elements. */
17121 static inline bool
17122 mips_constant_elt_p (rtx x)
17124 return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
17127 /* A subroutine of mips_expand_vec_init, expand via broadcast. */
17129 static void
17130 mips_expand_vi_broadcast (enum machine_mode vmode, rtx target, rtx elt)
17132 struct expand_vec_perm_d d;
17133 rtx t1;
17134 bool ok;
17136 if (elt != const0_rtx)
17137 elt = force_reg (GET_MODE_INNER (vmode), elt);
17138 if (REG_P (elt))
17139 elt = gen_lowpart (DImode, elt);
17141 t1 = gen_reg_rtx (vmode);
17142 switch (vmode)
17144 case V8QImode:
17145 emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
17146 break;
17147 case V4HImode:
17148 emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
17149 break;
17150 default:
17151 gcc_unreachable ();
17154 memset (&d, 0, sizeof (d));
17155 d.target = target;
17156 d.op0 = t1;
17157 d.op1 = t1;
17158 d.vmode = vmode;
17159 d.nelt = GET_MODE_NUNITS (vmode);
17160 d.one_vector_p = true;
17162 ok = mips_expand_vec_perm_const_1 (&d);
17163 gcc_assert (ok);
17166 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
17167 elements of VALS with zeros, copy the constant vector to TARGET. */
17169 static void
17170 mips_expand_vi_constant (enum machine_mode vmode, unsigned nelt,
17171 rtx target, rtx vals)
17173 rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
17174 unsigned i;
17176 for (i = 0; i < nelt; ++i)
17178 if (!mips_constant_elt_p (RTVEC_ELT (vec, i)))
17179 RTVEC_ELT (vec, i) = const0_rtx;
17182 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
17186 /* A subroutine of mips_expand_vec_init, expand via pinsrh. */
17188 static void
17189 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
17191 mips_expand_vi_constant (V4HImode, 4, target, vals);
17193 emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
17194 GEN_INT (one_var)));
17197 /* A subroutine of mips_expand_vec_init, expand anything via memory. */
17199 static void
17200 mips_expand_vi_general (enum machine_mode vmode, enum machine_mode imode,
17201 unsigned nelt, unsigned nvar, rtx target, rtx vals)
17203 rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
17204 unsigned int i, isize = GET_MODE_SIZE (imode);
17206 if (nvar < nelt)
17207 mips_expand_vi_constant (vmode, nelt, mem, vals);
17209 for (i = 0; i < nelt; ++i)
17211 rtx x = XVECEXP (vals, 0, i);
17212 if (!mips_constant_elt_p (x))
17213 emit_move_insn (adjust_address (mem, imode, i * isize), x);
17216 emit_move_insn (target, mem);
17219 /* Expand a vector initialization. */
17221 void
17222 mips_expand_vector_init (rtx target, rtx vals)
17224 enum machine_mode vmode = GET_MODE (target);
17225 enum machine_mode imode = GET_MODE_INNER (vmode);
17226 unsigned i, nelt = GET_MODE_NUNITS (vmode);
17227 unsigned nvar = 0, one_var = -1u;
17228 bool all_same = true;
17229 rtx x;
17231 for (i = 0; i < nelt; ++i)
17233 x = XVECEXP (vals, 0, i);
17234 if (!mips_constant_elt_p (x))
17235 nvar++, one_var = i;
17236 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
17237 all_same = false;
17240 /* Load constants from the pool, or whatever's handy. */
17241 if (nvar == 0)
17243 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
17244 return;
17247 /* For two-part initialization, always use CONCAT. */
17248 if (nelt == 2)
17250 rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
17251 rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
17252 x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
17253 emit_insn (gen_rtx_SET (VOIDmode, target, x));
17254 return;
17257 /* Loongson is the only cpu with vectors with more elements. */
17258 gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
17260 /* If all values are identical, broadcast the value. */
17261 if (all_same)
17263 mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
17264 return;
17267 /* If we've only got one non-variable V4HImode, use PINSRH. */
17268 if (nvar == 1 && vmode == V4HImode)
17270 mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
17271 return;
17274 mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
17277 /* Expand a vector reduction. */
17279 void
17280 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
17282 enum machine_mode vmode = GET_MODE (in);
17283 unsigned char perm2[2];
17284 rtx last, next, fold, x;
17285 bool ok;
17287 last = in;
17288 fold = gen_reg_rtx (vmode);
17289 switch (vmode)
17291 case V2SFmode:
17292 /* Use PUL/PLU to produce { L, H } op { H, L }.
17293 By reversing the pair order, rather than a pure interleave high,
17294 we avoid erroneous exceptional conditions that we might otherwise
17295 produce from the computation of H op H. */
17296 perm2[0] = 1;
17297 perm2[1] = 2;
17298 ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
17299 gcc_assert (ok);
17300 break;
17302 case V2SImode:
17303 /* Use interleave to produce { H, L } op { H, H }. */
17304 emit_insn (gen_loongson_punpckhwd (fold, last, last));
17305 break;
17307 case V4HImode:
17308 /* Perform the first reduction with interleave,
17309 and subsequent reductions with shifts. */
17310 emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
17312 next = gen_reg_rtx (vmode);
17313 emit_insn (gen (next, last, fold));
17314 last = next;
17316 fold = gen_reg_rtx (vmode);
17317 x = force_reg (SImode, GEN_INT (16));
17318 emit_insn (gen_vec_shr_v4hi (fold, last, x));
17319 break;
17321 case V8QImode:
17322 emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
17324 next = gen_reg_rtx (vmode);
17325 emit_insn (gen (next, last, fold));
17326 last = next;
17328 fold = gen_reg_rtx (vmode);
17329 x = force_reg (SImode, GEN_INT (16));
17330 emit_insn (gen_vec_shr_v8qi (fold, last, x));
17332 next = gen_reg_rtx (vmode);
17333 emit_insn (gen (next, last, fold));
17334 last = next;
17336 fold = gen_reg_rtx (vmode);
17337 x = force_reg (SImode, GEN_INT (8));
17338 emit_insn (gen_vec_shr_v8qi (fold, last, x));
17339 break;
17341 default:
17342 gcc_unreachable ();
17345 emit_insn (gen (target, last, fold));
17348 /* Expand a vector minimum/maximum. */
17350 void
17351 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
17352 rtx (*cmp) (rtx, rtx, rtx), bool min_p)
17354 enum machine_mode vmode = GET_MODE (target);
17355 rtx tc, t0, t1, x;
17357 tc = gen_reg_rtx (vmode);
17358 t0 = gen_reg_rtx (vmode);
17359 t1 = gen_reg_rtx (vmode);
17361 /* op0 > op1 */
17362 emit_insn (cmp (tc, op0, op1));
17364 x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
17365 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
17367 x = gen_rtx_NOT (vmode, tc);
17368 x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
17369 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
17371 x = gen_rtx_IOR (vmode, t0, t1);
17372 emit_insn (gen_rtx_SET (VOIDmode, target, x));
17375 /* Initialize the GCC target structure. */
17376 #undef TARGET_ASM_ALIGNED_HI_OP
17377 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
17378 #undef TARGET_ASM_ALIGNED_SI_OP
17379 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
17380 #undef TARGET_ASM_ALIGNED_DI_OP
17381 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
17383 #undef TARGET_OPTION_OVERRIDE
17384 #define TARGET_OPTION_OVERRIDE mips_option_override
17386 #undef TARGET_LEGITIMIZE_ADDRESS
17387 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
17389 #undef TARGET_ASM_FUNCTION_PROLOGUE
17390 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
17391 #undef TARGET_ASM_FUNCTION_EPILOGUE
17392 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
17393 #undef TARGET_ASM_SELECT_RTX_SECTION
17394 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
17395 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
17396 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
17398 #undef TARGET_SCHED_INIT
17399 #define TARGET_SCHED_INIT mips_sched_init
17400 #undef TARGET_SCHED_REORDER
17401 #define TARGET_SCHED_REORDER mips_sched_reorder
17402 #undef TARGET_SCHED_REORDER2
17403 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
17404 #undef TARGET_SCHED_VARIABLE_ISSUE
17405 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
17406 #undef TARGET_SCHED_ADJUST_COST
17407 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
17408 #undef TARGET_SCHED_ISSUE_RATE
17409 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
17410 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
17411 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
17412 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
17413 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
17414 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
17415 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
17416 mips_multipass_dfa_lookahead
17417 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
17418 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
17419 mips_small_register_classes_for_mode_p
17421 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
17422 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
17424 #undef TARGET_INSERT_ATTRIBUTES
17425 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
17426 #undef TARGET_MERGE_DECL_ATTRIBUTES
17427 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
17428 #undef TARGET_SET_CURRENT_FUNCTION
17429 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
17431 #undef TARGET_VALID_POINTER_MODE
17432 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
17433 #undef TARGET_REGISTER_MOVE_COST
17434 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
17435 #undef TARGET_MEMORY_MOVE_COST
17436 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
17437 #undef TARGET_RTX_COSTS
17438 #define TARGET_RTX_COSTS mips_rtx_costs
17439 #undef TARGET_ADDRESS_COST
17440 #define TARGET_ADDRESS_COST mips_address_cost
17442 #undef TARGET_IN_SMALL_DATA_P
17443 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
17445 #undef TARGET_MACHINE_DEPENDENT_REORG
17446 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
17448 #undef TARGET_PREFERRED_RELOAD_CLASS
17449 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
17451 #undef TARGET_ASM_FILE_START
17452 #define TARGET_ASM_FILE_START mips_file_start
17453 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
17454 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
17455 #undef TARGET_ASM_CODE_END
17456 #define TARGET_ASM_CODE_END mips_code_end
17458 #undef TARGET_INIT_LIBFUNCS
17459 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
17461 #undef TARGET_BUILD_BUILTIN_VA_LIST
17462 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
17463 #undef TARGET_EXPAND_BUILTIN_VA_START
17464 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
17465 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
17466 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
17468 #undef TARGET_PROMOTE_FUNCTION_MODE
17469 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
17470 #undef TARGET_PROMOTE_PROTOTYPES
17471 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
17473 #undef TARGET_FUNCTION_VALUE
17474 #define TARGET_FUNCTION_VALUE mips_function_value
17475 #undef TARGET_LIBCALL_VALUE
17476 #define TARGET_LIBCALL_VALUE mips_libcall_value
17477 #undef TARGET_FUNCTION_VALUE_REGNO_P
17478 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
17479 #undef TARGET_RETURN_IN_MEMORY
17480 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
17481 #undef TARGET_RETURN_IN_MSB
17482 #define TARGET_RETURN_IN_MSB mips_return_in_msb
17484 #undef TARGET_ASM_OUTPUT_MI_THUNK
17485 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
17486 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
17487 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
17489 #undef TARGET_PRINT_OPERAND
17490 #define TARGET_PRINT_OPERAND mips_print_operand
17491 #undef TARGET_PRINT_OPERAND_ADDRESS
17492 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
17493 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
17494 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
17496 #undef TARGET_SETUP_INCOMING_VARARGS
17497 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
17498 #undef TARGET_STRICT_ARGUMENT_NAMING
17499 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
17500 #undef TARGET_MUST_PASS_IN_STACK
17501 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
17502 #undef TARGET_PASS_BY_REFERENCE
17503 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
17504 #undef TARGET_CALLEE_COPIES
17505 #define TARGET_CALLEE_COPIES mips_callee_copies
17506 #undef TARGET_ARG_PARTIAL_BYTES
17507 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
17508 #undef TARGET_FUNCTION_ARG
17509 #define TARGET_FUNCTION_ARG mips_function_arg
17510 #undef TARGET_FUNCTION_ARG_ADVANCE
17511 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
17512 #undef TARGET_FUNCTION_ARG_BOUNDARY
17513 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
17515 #undef TARGET_MODE_REP_EXTENDED
17516 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
17518 #undef TARGET_VECTOR_MODE_SUPPORTED_P
17519 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
17521 #undef TARGET_SCALAR_MODE_SUPPORTED_P
17522 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
17524 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
17525 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
17527 #undef TARGET_INIT_BUILTINS
17528 #define TARGET_INIT_BUILTINS mips_init_builtins
17529 #undef TARGET_BUILTIN_DECL
17530 #define TARGET_BUILTIN_DECL mips_builtin_decl
17531 #undef TARGET_EXPAND_BUILTIN
17532 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
17534 #undef TARGET_HAVE_TLS
17535 #define TARGET_HAVE_TLS HAVE_AS_TLS
17537 #undef TARGET_CANNOT_FORCE_CONST_MEM
17538 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
17540 #undef TARGET_LEGITIMATE_CONSTANT_P
17541 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
17543 #undef TARGET_ENCODE_SECTION_INFO
17544 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
17546 #undef TARGET_ATTRIBUTE_TABLE
17547 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
17548 /* All our function attributes are related to how out-of-line copies should
17549 be compiled or called. They don't in themselves prevent inlining. */
17550 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
17551 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
17553 #undef TARGET_EXTRA_LIVE_ON_ENTRY
17554 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
17556 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
17557 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
17558 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
17559 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
17561 #undef TARGET_COMP_TYPE_ATTRIBUTES
17562 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
17564 #ifdef HAVE_AS_DTPRELWORD
17565 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
17566 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
17567 #endif
17568 #undef TARGET_DWARF_REGISTER_SPAN
17569 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
17571 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
17572 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
17574 #undef TARGET_LEGITIMATE_ADDRESS_P
17575 #define TARGET_LEGITIMATE_ADDRESS_P mips_legitimate_address_p
17577 #undef TARGET_FRAME_POINTER_REQUIRED
17578 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
17580 #undef TARGET_CAN_ELIMINATE
17581 #define TARGET_CAN_ELIMINATE mips_can_eliminate
17583 #undef TARGET_CONDITIONAL_REGISTER_USAGE
17584 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
17586 #undef TARGET_TRAMPOLINE_INIT
17587 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
17589 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
17590 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
17592 #undef TARGET_SHIFT_TRUNCATION_MASK
17593 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
17595 #undef TARGET_PREPARE_PCH_SAVE
17596 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
17598 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
17599 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK mips_vectorize_vec_perm_const_ok
17601 struct gcc_target targetm = TARGET_INITIALIZER;
17603 #include "gt-mips.h"