]> gcc.gnu.org Git - gcc.git/blob - gcc/config/mips/mips.c
2010-xx-xx Mingjie Xing <mingjie.xing@gmail.com>
[gcc.git] / gcc / config / mips / mips.c
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 Free Software Foundation, Inc.
5 Contributed by A. Lichnewsky, lich@inria.inria.fr.
6 Changes by Michael Meissner, meissner@osf.org.
7 64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
8 Brendan Eich, brendan@microunity.com.
9
10 This file is part of GCC.
11
12 GCC is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 3, or (at your option)
15 any later version.
16
17 GCC is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include <signal.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 "toplev.h"
39 #include "output.h"
40 #include "tree.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "optabs.h"
44 #include "libfuncs.h"
45 #include "flags.h"
46 #include "reload.h"
47 #include "tm_p.h"
48 #include "ggc.h"
49 #include "gstab.h"
50 #include "hashtab.h"
51 #include "debug.h"
52 #include "target.h"
53 #include "target-def.h"
54 #include "integrate.h"
55 #include "langhooks.h"
56 #include "cfglayout.h"
57 #include "sched-int.h"
58 #include "gimple.h"
59 #include "bitmap.h"
60 #include "diagnostic.h"
61 #include "target-globals.h"
62
63 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
64 #define UNSPEC_ADDRESS_P(X) \
65 (GET_CODE (X) == UNSPEC \
66 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
67 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
68
69 /* Extract the symbol or label from UNSPEC wrapper X. */
70 #define UNSPEC_ADDRESS(X) \
71 XVECEXP (X, 0, 0)
72
73 /* Extract the symbol type from UNSPEC wrapper X. */
74 #define UNSPEC_ADDRESS_TYPE(X) \
75 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
76
77 /* The maximum distance between the top of the stack frame and the
78 value $sp has when we save and restore registers.
79
80 The value for normal-mode code must be a SMALL_OPERAND and must
81 preserve the maximum stack alignment. We therefore use a value
82 of 0x7ff0 in this case.
83
84 MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
85 up to 0x7f8 bytes and can usually save or restore all the registers
86 that we need to save or restore. (Note that we can only use these
87 instructions for o32, for which the stack alignment is 8 bytes.)
88
89 We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
90 RESTORE are not available. We can then use unextended instructions
91 to save and restore registers, and to allocate and deallocate the top
92 part of the frame. */
93 #define MIPS_MAX_FIRST_STACK_STEP \
94 (!TARGET_MIPS16 ? 0x7ff0 \
95 : GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8 \
96 : TARGET_64BIT ? 0x100 : 0x400)
97
98 /* True if INSN is a mips.md pattern or asm statement. */
99 #define USEFUL_INSN_P(INSN) \
100 (NONDEBUG_INSN_P (INSN) \
101 && GET_CODE (PATTERN (INSN)) != USE \
102 && GET_CODE (PATTERN (INSN)) != CLOBBER \
103 && GET_CODE (PATTERN (INSN)) != ADDR_VEC \
104 && GET_CODE (PATTERN (INSN)) != ADDR_DIFF_VEC)
105
106 /* If INSN is a delayed branch sequence, return the first instruction
107 in the sequence, otherwise return INSN itself. */
108 #define SEQ_BEGIN(INSN) \
109 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
110 ? XVECEXP (PATTERN (INSN), 0, 0) \
111 : (INSN))
112
113 /* Likewise for the last instruction in a delayed branch sequence. */
114 #define SEQ_END(INSN) \
115 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
116 ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1) \
117 : (INSN))
118
119 /* Execute the following loop body with SUBINSN set to each instruction
120 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
121 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
122 for ((SUBINSN) = SEQ_BEGIN (INSN); \
123 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
124 (SUBINSN) = NEXT_INSN (SUBINSN))
125
126 /* True if bit BIT is set in VALUE. */
127 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
128
129 /* Return the opcode for a ptr_mode load of the form:
130
131 l[wd] DEST, OFFSET(BASE). */
132 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE) \
133 (((ptr_mode == DImode ? 0x37 : 0x23) << 26) \
134 | ((BASE) << 21) \
135 | ((DEST) << 16) \
136 | (OFFSET))
137
138 /* Return the opcode to move register SRC into register DEST. */
139 #define MIPS_MOVE(DEST, SRC) \
140 ((TARGET_64BIT ? 0x2d : 0x21) \
141 | ((DEST) << 11) \
142 | ((SRC) << 21))
143
144 /* Return the opcode for:
145
146 lui DEST, VALUE. */
147 #define MIPS_LUI(DEST, VALUE) \
148 ((0xf << 26) | ((DEST) << 16) | (VALUE))
149
150 /* Return the opcode to jump to register DEST. */
151 #define MIPS_JR(DEST) \
152 (((DEST) << 21) | 0x8)
153
154 /* Return the opcode for:
155
156 bal . + (1 + OFFSET) * 4. */
157 #define MIPS_BAL(OFFSET) \
158 ((0x1 << 26) | (0x11 << 16) | (OFFSET))
159
160 /* Return the usual opcode for a nop. */
161 #define MIPS_NOP 0
162
163 /* Classifies an address.
164
165 ADDRESS_REG
166 A natural register + offset address. The register satisfies
167 mips_valid_base_register_p and the offset is a const_arith_operand.
168
169 ADDRESS_LO_SUM
170 A LO_SUM rtx. The first operand is a valid base register and
171 the second operand is a symbolic address.
172
173 ADDRESS_CONST_INT
174 A signed 16-bit constant address.
175
176 ADDRESS_SYMBOLIC:
177 A constant symbolic address. */
178 enum mips_address_type {
179 ADDRESS_REG,
180 ADDRESS_LO_SUM,
181 ADDRESS_CONST_INT,
182 ADDRESS_SYMBOLIC
183 };
184
185 /* Enumerates the setting of the -mr10k-cache-barrier option. */
186 enum mips_r10k_cache_barrier_setting {
187 R10K_CACHE_BARRIER_NONE,
188 R10K_CACHE_BARRIER_STORE,
189 R10K_CACHE_BARRIER_LOAD_STORE
190 };
191
192 /* Macros to create an enumeration identifier for a function prototype. */
193 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
194 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
195 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
196 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
197
198 /* Classifies the prototype of a built-in function. */
199 enum mips_function_type {
200 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
201 #include "config/mips/mips-ftypes.def"
202 #undef DEF_MIPS_FTYPE
203 MIPS_MAX_FTYPE_MAX
204 };
205
206 /* Specifies how a built-in function should be converted into rtl. */
207 enum mips_builtin_type {
208 /* The function corresponds directly to an .md pattern. The return
209 value is mapped to operand 0 and the arguments are mapped to
210 operands 1 and above. */
211 MIPS_BUILTIN_DIRECT,
212
213 /* The function corresponds directly to an .md pattern. There is no return
214 value and the arguments are mapped to operands 0 and above. */
215 MIPS_BUILTIN_DIRECT_NO_TARGET,
216
217 /* The function corresponds to a comparison instruction followed by
218 a mips_cond_move_tf_ps pattern. The first two arguments are the
219 values to compare and the second two arguments are the vector
220 operands for the movt.ps or movf.ps instruction (in assembly order). */
221 MIPS_BUILTIN_MOVF,
222 MIPS_BUILTIN_MOVT,
223
224 /* The function corresponds to a V2SF comparison instruction. Operand 0
225 of this instruction is the result of the comparison, which has mode
226 CCV2 or CCV4. The function arguments are mapped to operands 1 and
227 above. The function's return value is an SImode boolean that is
228 true under the following conditions:
229
230 MIPS_BUILTIN_CMP_ANY: one of the registers is true
231 MIPS_BUILTIN_CMP_ALL: all of the registers are true
232 MIPS_BUILTIN_CMP_LOWER: the first register is true
233 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
234 MIPS_BUILTIN_CMP_ANY,
235 MIPS_BUILTIN_CMP_ALL,
236 MIPS_BUILTIN_CMP_UPPER,
237 MIPS_BUILTIN_CMP_LOWER,
238
239 /* As above, but the instruction only sets a single $fcc register. */
240 MIPS_BUILTIN_CMP_SINGLE,
241
242 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
243 MIPS_BUILTIN_BPOSGE32
244 };
245
246 /* Invoke MACRO (COND) for each C.cond.fmt condition. */
247 #define MIPS_FP_CONDITIONS(MACRO) \
248 MACRO (f), \
249 MACRO (un), \
250 MACRO (eq), \
251 MACRO (ueq), \
252 MACRO (olt), \
253 MACRO (ult), \
254 MACRO (ole), \
255 MACRO (ule), \
256 MACRO (sf), \
257 MACRO (ngle), \
258 MACRO (seq), \
259 MACRO (ngl), \
260 MACRO (lt), \
261 MACRO (nge), \
262 MACRO (le), \
263 MACRO (ngt)
264
265 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
266 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
267 enum mips_fp_condition {
268 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
269 };
270
271 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
272 #define STRINGIFY(X) #X
273 static const char *const mips_fp_conditions[] = {
274 MIPS_FP_CONDITIONS (STRINGIFY)
275 };
276
277 /* Information about a function's frame layout. */
278 struct GTY(()) mips_frame_info {
279 /* The size of the frame in bytes. */
280 HOST_WIDE_INT total_size;
281
282 /* The number of bytes allocated to variables. */
283 HOST_WIDE_INT var_size;
284
285 /* The number of bytes allocated to outgoing function arguments. */
286 HOST_WIDE_INT args_size;
287
288 /* The number of bytes allocated to the .cprestore slot, or 0 if there
289 is no such slot. */
290 HOST_WIDE_INT cprestore_size;
291
292 /* Bit X is set if the function saves or restores GPR X. */
293 unsigned int mask;
294
295 /* Likewise FPR X. */
296 unsigned int fmask;
297
298 /* Likewise doubleword accumulator X ($acX). */
299 unsigned int acc_mask;
300
301 /* The number of GPRs, FPRs, doubleword accumulators and COP0
302 registers saved. */
303 unsigned int num_gp;
304 unsigned int num_fp;
305 unsigned int num_acc;
306 unsigned int num_cop0_regs;
307
308 /* The offset of the topmost GPR, FPR, accumulator and COP0-register
309 save slots from the top of the frame, or zero if no such slots are
310 needed. */
311 HOST_WIDE_INT gp_save_offset;
312 HOST_WIDE_INT fp_save_offset;
313 HOST_WIDE_INT acc_save_offset;
314 HOST_WIDE_INT cop0_save_offset;
315
316 /* Likewise, but giving offsets from the bottom of the frame. */
317 HOST_WIDE_INT gp_sp_offset;
318 HOST_WIDE_INT fp_sp_offset;
319 HOST_WIDE_INT acc_sp_offset;
320 HOST_WIDE_INT cop0_sp_offset;
321
322 /* Similar, but the value passed to _mcount. */
323 HOST_WIDE_INT ra_fp_offset;
324
325 /* The offset of arg_pointer_rtx from the bottom of the frame. */
326 HOST_WIDE_INT arg_pointer_offset;
327
328 /* The offset of hard_frame_pointer_rtx from the bottom of the frame. */
329 HOST_WIDE_INT hard_frame_pointer_offset;
330 };
331
332 struct GTY(()) machine_function {
333 /* The register returned by mips16_gp_pseudo_reg; see there for details. */
334 rtx mips16_gp_pseudo_rtx;
335
336 /* The number of extra stack bytes taken up by register varargs.
337 This area is allocated by the callee at the very top of the frame. */
338 int varargs_size;
339
340 /* The current frame information, calculated by mips_compute_frame_info. */
341 struct mips_frame_info frame;
342
343 /* The register to use as the function's global pointer, or INVALID_REGNUM
344 if the function doesn't need one. */
345 unsigned int global_pointer;
346
347 /* How many instructions it takes to load a label into $AT, or 0 if
348 this property hasn't yet been calculated. */
349 unsigned int load_label_length;
350
351 /* True if mips_adjust_insn_length should ignore an instruction's
352 hazard attribute. */
353 bool ignore_hazard_length_p;
354
355 /* True if the whole function is suitable for .set noreorder and
356 .set nomacro. */
357 bool all_noreorder_p;
358
359 /* True if the function has "inflexible" and "flexible" references
360 to the global pointer. See mips_cfun_has_inflexible_gp_ref_p
361 and mips_cfun_has_flexible_gp_ref_p for details. */
362 bool has_inflexible_gp_insn_p;
363 bool has_flexible_gp_insn_p;
364
365 /* True if the function's prologue must load the global pointer
366 value into pic_offset_table_rtx and store the same value in
367 the function's cprestore slot (if any). Even if this value
368 is currently false, we may decide to set it to true later;
369 see mips_must_initialize_gp_p () for details. */
370 bool must_initialize_gp_p;
371
372 /* True if the current function must restore $gp after any potential
373 clobber. This value is only meaningful during the first post-epilogue
374 split_insns pass; see mips_must_initialize_gp_p () for details. */
375 bool must_restore_gp_when_clobbered_p;
376
377 /* True if we have emitted an instruction to initialize
378 mips16_gp_pseudo_rtx. */
379 bool initialized_mips16_gp_pseudo_p;
380
381 /* True if this is an interrupt handler. */
382 bool interrupt_handler_p;
383
384 /* True if this is an interrupt handler that uses shadow registers. */
385 bool use_shadow_register_set_p;
386
387 /* True if this is an interrupt handler that should keep interrupts
388 masked. */
389 bool keep_interrupts_masked_p;
390
391 /* True if this is an interrupt handler that should use DERET
392 instead of ERET. */
393 bool use_debug_exception_return_p;
394 };
395
396 /* Information about a single argument. */
397 struct mips_arg_info {
398 /* True if the argument is passed in a floating-point register, or
399 would have been if we hadn't run out of registers. */
400 bool fpr_p;
401
402 /* The number of words passed in registers, rounded up. */
403 unsigned int reg_words;
404
405 /* For EABI, the offset of the first register from GP_ARG_FIRST or
406 FP_ARG_FIRST. For other ABIs, the offset of the first register from
407 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
408 comment for details).
409
410 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
411 on the stack. */
412 unsigned int reg_offset;
413
414 /* The number of words that must be passed on the stack, rounded up. */
415 unsigned int stack_words;
416
417 /* The offset from the start of the stack overflow area of the argument's
418 first stack word. Only meaningful when STACK_WORDS is nonzero. */
419 unsigned int stack_offset;
420 };
421
422 /* Information about an address described by mips_address_type.
423
424 ADDRESS_CONST_INT
425 No fields are used.
426
427 ADDRESS_REG
428 REG is the base register and OFFSET is the constant offset.
429
430 ADDRESS_LO_SUM
431 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
432 is the type of symbol it references.
433
434 ADDRESS_SYMBOLIC
435 SYMBOL_TYPE is the type of symbol that the address references. */
436 struct mips_address_info {
437 enum mips_address_type type;
438 rtx reg;
439 rtx offset;
440 enum mips_symbol_type symbol_type;
441 };
442
443 /* One stage in a constant building sequence. These sequences have
444 the form:
445
446 A = VALUE[0]
447 A = A CODE[1] VALUE[1]
448 A = A CODE[2] VALUE[2]
449 ...
450
451 where A is an accumulator, each CODE[i] is a binary rtl operation
452 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
453 struct mips_integer_op {
454 enum rtx_code code;
455 unsigned HOST_WIDE_INT value;
456 };
457
458 /* The largest number of operations needed to load an integer constant.
459 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
460 When the lowest bit is clear, we can try, but reject a sequence with
461 an extra SLL at the end. */
462 #define MIPS_MAX_INTEGER_OPS 7
463
464 /* Information about a MIPS16e SAVE or RESTORE instruction. */
465 struct mips16e_save_restore_info {
466 /* The number of argument registers saved by a SAVE instruction.
467 0 for RESTORE instructions. */
468 unsigned int nargs;
469
470 /* Bit X is set if the instruction saves or restores GPR X. */
471 unsigned int mask;
472
473 /* The total number of bytes to allocate. */
474 HOST_WIDE_INT size;
475 };
476
477 /* Global variables for machine-dependent things. */
478
479 /* The -G setting, or the configuration's default small-data limit if
480 no -G option is given. */
481 static unsigned int mips_small_data_threshold;
482
483 /* The number of file directives written by mips_output_filename. */
484 int num_source_filenames;
485
486 /* The name that appeared in the last .file directive written by
487 mips_output_filename, or "" if mips_output_filename hasn't
488 written anything yet. */
489 const char *current_function_file = "";
490
491 /* A label counter used by PUT_SDB_BLOCK_START and PUT_SDB_BLOCK_END. */
492 int sdb_label_count;
493
494 /* Arrays that map GCC register numbers to debugger register numbers. */
495 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
496 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
497
498 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs. */
499 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
500 struct mips_asm_switch mips_nomacro = { "macro", 0 };
501 struct mips_asm_switch mips_noat = { "at", 0 };
502
503 /* True if we're writing out a branch-likely instruction rather than a
504 normal branch. */
505 static bool mips_branch_likely;
506
507 /* The current instruction-set architecture. */
508 enum processor mips_arch;
509 const struct mips_cpu_info *mips_arch_info;
510
511 /* The processor that we should tune the code for. */
512 enum processor mips_tune;
513 const struct mips_cpu_info *mips_tune_info;
514
515 /* The ISA level associated with mips_arch. */
516 int mips_isa;
517
518 /* The architecture selected by -mipsN, or null if -mipsN wasn't used. */
519 static const struct mips_cpu_info *mips_isa_option_info;
520
521 /* Which ABI to use. */
522 int mips_abi = MIPS_ABI_DEFAULT;
523
524 /* Which cost information to use. */
525 const struct mips_rtx_cost_data *mips_cost;
526
527 /* The ambient target flags, excluding MASK_MIPS16. */
528 static int mips_base_target_flags;
529
530 /* True if MIPS16 is the default mode. */
531 bool mips_base_mips16;
532
533 /* The ambient values of other global variables. */
534 static int mips_base_schedule_insns; /* flag_schedule_insns */
535 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
536 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
537 static int mips_base_align_loops; /* align_loops */
538 static int mips_base_align_jumps; /* align_jumps */
539 static int mips_base_align_functions; /* align_functions */
540
541 /* The -mcode-readable setting. */
542 enum mips_code_readable_setting mips_code_readable = CODE_READABLE_YES;
543
544 /* The -mr10k-cache-barrier setting. */
545 static enum mips_r10k_cache_barrier_setting mips_r10k_cache_barrier;
546
547 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
548 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
549
550 /* Index C is true if character C is a valid PRINT_OPERAND punctation
551 character. */
552 static bool mips_print_operand_punct[256];
553
554 static GTY (()) int mips_output_filename_first_time = 1;
555
556 /* mips_split_p[X] is true if symbols of type X can be split by
557 mips_split_symbol. */
558 bool mips_split_p[NUM_SYMBOL_TYPES];
559
560 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
561 can be split by mips_split_symbol. */
562 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
563
564 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
565 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
566 if they are matched by a special .md file pattern. */
567 static const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
568
569 /* Likewise for HIGHs. */
570 static const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
571
572 /* Target state for MIPS16. */
573 struct target_globals *mips16_globals;
574
575 /* Index R is the smallest register class that contains register R. */
576 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
577 LEA_REGS, LEA_REGS, M16_REGS, V1_REG,
578 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
579 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
580 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
581 M16_REGS, M16_REGS, LEA_REGS, LEA_REGS,
582 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
583 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
584 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
585 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
586 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
587 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
588 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
589 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
590 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
591 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
592 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
593 MD0_REG, MD1_REG, NO_REGS, ST_REGS,
594 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
595 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
596 NO_REGS, FRAME_REGS, FRAME_REGS, NO_REGS,
597 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
598 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
599 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
600 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
601 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
602 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
603 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
604 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
605 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
606 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
607 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
608 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
609 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
610 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
611 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
612 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
613 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
614 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
615 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
616 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
617 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
618 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
619 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
620 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
621 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
622 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
623 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
624 };
625
626 /* The value of TARGET_ATTRIBUTE_TABLE. */
627 static const struct attribute_spec mips_attribute_table[] = {
628 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
629 { "long_call", 0, 0, false, true, true, NULL },
630 { "far", 0, 0, false, true, true, NULL },
631 { "near", 0, 0, false, true, true, NULL },
632 /* We would really like to treat "mips16" and "nomips16" as type
633 attributes, but GCC doesn't provide the hooks we need to support
634 the right conversion rules. As declaration attributes, they affect
635 code generation but don't carry other semantics. */
636 { "mips16", 0, 0, true, false, false, NULL },
637 { "nomips16", 0, 0, true, false, false, NULL },
638 /* Allow functions to be specified as interrupt handlers */
639 { "interrupt", 0, 0, false, true, true, NULL },
640 { "use_shadow_register_set", 0, 0, false, true, true, NULL },
641 { "keep_interrupts_masked", 0, 0, false, true, true, NULL },
642 { "use_debug_exception_return", 0, 0, false, true, true, NULL },
643 { NULL, 0, 0, false, false, false, NULL }
644 };
645 \f
646 /* A table describing all the processors GCC knows about. Names are
647 matched in the order listed. The first mention of an ISA level is
648 taken as the canonical name for that ISA.
649
650 To ease comparison, please keep this table in the same order
651 as GAS's mips_cpu_info_table. Please also make sure that
652 MIPS_ISA_LEVEL_SPEC and MIPS_ARCH_FLOAT_SPEC handle all -march
653 options correctly. */
654 static const struct mips_cpu_info mips_cpu_info_table[] = {
655 /* Entries for generic ISAs. */
656 { "mips1", PROCESSOR_R3000, 1, 0 },
657 { "mips2", PROCESSOR_R6000, 2, 0 },
658 { "mips3", PROCESSOR_R4000, 3, 0 },
659 { "mips4", PROCESSOR_R8000, 4, 0 },
660 /* Prefer not to use branch-likely instructions for generic MIPS32rX
661 and MIPS64rX code. The instructions were officially deprecated
662 in revisions 2 and earlier, but revision 3 is likely to downgrade
663 that to a recommendation to avoid the instructions in code that
664 isn't tuned to a specific processor. */
665 { "mips32", PROCESSOR_4KC, 32, PTF_AVOID_BRANCHLIKELY },
666 { "mips32r2", PROCESSOR_M4K, 33, PTF_AVOID_BRANCHLIKELY },
667 { "mips64", PROCESSOR_5KC, 64, PTF_AVOID_BRANCHLIKELY },
668 /* ??? For now just tune the generic MIPS64r2 for 5KC as well. */
669 { "mips64r2", PROCESSOR_5KC, 65, PTF_AVOID_BRANCHLIKELY },
670
671 /* MIPS I processors. */
672 { "r3000", PROCESSOR_R3000, 1, 0 },
673 { "r2000", PROCESSOR_R3000, 1, 0 },
674 { "r3900", PROCESSOR_R3900, 1, 0 },
675
676 /* MIPS II processors. */
677 { "r6000", PROCESSOR_R6000, 2, 0 },
678
679 /* MIPS III processors. */
680 { "r4000", PROCESSOR_R4000, 3, 0 },
681 { "vr4100", PROCESSOR_R4100, 3, 0 },
682 { "vr4111", PROCESSOR_R4111, 3, 0 },
683 { "vr4120", PROCESSOR_R4120, 3, 0 },
684 { "vr4130", PROCESSOR_R4130, 3, 0 },
685 { "vr4300", PROCESSOR_R4300, 3, 0 },
686 { "r4400", PROCESSOR_R4000, 3, 0 },
687 { "r4600", PROCESSOR_R4600, 3, 0 },
688 { "orion", PROCESSOR_R4600, 3, 0 },
689 { "r4650", PROCESSOR_R4650, 3, 0 },
690 /* ST Loongson 2E/2F processors. */
691 { "loongson2e", PROCESSOR_LOONGSON_2E, 3, PTF_AVOID_BRANCHLIKELY },
692 { "loongson2f", PROCESSOR_LOONGSON_2F, 3, PTF_AVOID_BRANCHLIKELY },
693
694 /* MIPS IV processors. */
695 { "r8000", PROCESSOR_R8000, 4, 0 },
696 { "r10000", PROCESSOR_R10000, 4, 0 },
697 { "r12000", PROCESSOR_R10000, 4, 0 },
698 { "r14000", PROCESSOR_R10000, 4, 0 },
699 { "r16000", PROCESSOR_R10000, 4, 0 },
700 { "vr5000", PROCESSOR_R5000, 4, 0 },
701 { "vr5400", PROCESSOR_R5400, 4, 0 },
702 { "vr5500", PROCESSOR_R5500, 4, PTF_AVOID_BRANCHLIKELY },
703 { "rm7000", PROCESSOR_R7000, 4, 0 },
704 { "rm9000", PROCESSOR_R9000, 4, 0 },
705
706 /* MIPS32 processors. */
707 { "4kc", PROCESSOR_4KC, 32, 0 },
708 { "4km", PROCESSOR_4KC, 32, 0 },
709 { "4kp", PROCESSOR_4KP, 32, 0 },
710 { "4ksc", PROCESSOR_4KC, 32, 0 },
711
712 /* MIPS32 Release 2 processors. */
713 { "m4k", PROCESSOR_M4K, 33, 0 },
714 { "4kec", PROCESSOR_4KC, 33, 0 },
715 { "4kem", PROCESSOR_4KC, 33, 0 },
716 { "4kep", PROCESSOR_4KP, 33, 0 },
717 { "4ksd", PROCESSOR_4KC, 33, 0 },
718
719 { "24kc", PROCESSOR_24KC, 33, 0 },
720 { "24kf2_1", PROCESSOR_24KF2_1, 33, 0 },
721 { "24kf", PROCESSOR_24KF2_1, 33, 0 },
722 { "24kf1_1", PROCESSOR_24KF1_1, 33, 0 },
723 { "24kfx", PROCESSOR_24KF1_1, 33, 0 },
724 { "24kx", PROCESSOR_24KF1_1, 33, 0 },
725
726 { "24kec", PROCESSOR_24KC, 33, 0 }, /* 24K with DSP. */
727 { "24kef2_1", PROCESSOR_24KF2_1, 33, 0 },
728 { "24kef", PROCESSOR_24KF2_1, 33, 0 },
729 { "24kef1_1", PROCESSOR_24KF1_1, 33, 0 },
730 { "24kefx", PROCESSOR_24KF1_1, 33, 0 },
731 { "24kex", PROCESSOR_24KF1_1, 33, 0 },
732
733 { "34kc", PROCESSOR_24KC, 33, 0 }, /* 34K with MT/DSP. */
734 { "34kf2_1", PROCESSOR_24KF2_1, 33, 0 },
735 { "34kf", PROCESSOR_24KF2_1, 33, 0 },
736 { "34kf1_1", PROCESSOR_24KF1_1, 33, 0 },
737 { "34kfx", PROCESSOR_24KF1_1, 33, 0 },
738 { "34kx", PROCESSOR_24KF1_1, 33, 0 },
739
740 { "74kc", PROCESSOR_74KC, 33, 0 }, /* 74K with DSPr2. */
741 { "74kf2_1", PROCESSOR_74KF2_1, 33, 0 },
742 { "74kf", PROCESSOR_74KF2_1, 33, 0 },
743 { "74kf1_1", PROCESSOR_74KF1_1, 33, 0 },
744 { "74kfx", PROCESSOR_74KF1_1, 33, 0 },
745 { "74kx", PROCESSOR_74KF1_1, 33, 0 },
746 { "74kf3_2", PROCESSOR_74KF3_2, 33, 0 },
747
748 { "1004kc", PROCESSOR_24KC, 33, 0 }, /* 1004K with MT/DSP. */
749 { "1004kf2_1", PROCESSOR_24KF2_1, 33, 0 },
750 { "1004kf", PROCESSOR_24KF2_1, 33, 0 },
751 { "1004kf1_1", PROCESSOR_24KF1_1, 33, 0 },
752
753 /* MIPS64 processors. */
754 { "5kc", PROCESSOR_5KC, 64, 0 },
755 { "5kf", PROCESSOR_5KF, 64, 0 },
756 { "20kc", PROCESSOR_20KC, 64, PTF_AVOID_BRANCHLIKELY },
757 { "sb1", PROCESSOR_SB1, 64, PTF_AVOID_BRANCHLIKELY },
758 { "sb1a", PROCESSOR_SB1A, 64, PTF_AVOID_BRANCHLIKELY },
759 { "sr71000", PROCESSOR_SR71000, 64, PTF_AVOID_BRANCHLIKELY },
760 { "xlr", PROCESSOR_XLR, 64, 0 },
761
762 /* MIPS64 Release 2 processors. */
763 { "octeon", PROCESSOR_OCTEON, 65, PTF_AVOID_BRANCHLIKELY }
764 };
765
766 /* Default costs. If these are used for a processor we should look
767 up the actual costs. */
768 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
769 COSTS_N_INSNS (7), /* fp_mult_sf */ \
770 COSTS_N_INSNS (8), /* fp_mult_df */ \
771 COSTS_N_INSNS (23), /* fp_div_sf */ \
772 COSTS_N_INSNS (36), /* fp_div_df */ \
773 COSTS_N_INSNS (10), /* int_mult_si */ \
774 COSTS_N_INSNS (10), /* int_mult_di */ \
775 COSTS_N_INSNS (69), /* int_div_si */ \
776 COSTS_N_INSNS (69), /* int_div_di */ \
777 2, /* branch_cost */ \
778 4 /* memory_latency */
779
780 /* Floating-point costs for processors without an FPU. Just assume that
781 all floating-point libcalls are very expensive. */
782 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
783 COSTS_N_INSNS (256), /* fp_mult_sf */ \
784 COSTS_N_INSNS (256), /* fp_mult_df */ \
785 COSTS_N_INSNS (256), /* fp_div_sf */ \
786 COSTS_N_INSNS (256) /* fp_div_df */
787
788 /* Costs to use when optimizing for size. */
789 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
790 COSTS_N_INSNS (1), /* fp_add */
791 COSTS_N_INSNS (1), /* fp_mult_sf */
792 COSTS_N_INSNS (1), /* fp_mult_df */
793 COSTS_N_INSNS (1), /* fp_div_sf */
794 COSTS_N_INSNS (1), /* fp_div_df */
795 COSTS_N_INSNS (1), /* int_mult_si */
796 COSTS_N_INSNS (1), /* int_mult_di */
797 COSTS_N_INSNS (1), /* int_div_si */
798 COSTS_N_INSNS (1), /* int_div_di */
799 2, /* branch_cost */
800 4 /* memory_latency */
801 };
802
803 /* Costs to use when optimizing for speed, indexed by processor. */
804 static const struct mips_rtx_cost_data
805 mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
806 { /* R3000 */
807 COSTS_N_INSNS (2), /* fp_add */
808 COSTS_N_INSNS (4), /* fp_mult_sf */
809 COSTS_N_INSNS (5), /* fp_mult_df */
810 COSTS_N_INSNS (12), /* fp_div_sf */
811 COSTS_N_INSNS (19), /* fp_div_df */
812 COSTS_N_INSNS (12), /* int_mult_si */
813 COSTS_N_INSNS (12), /* int_mult_di */
814 COSTS_N_INSNS (35), /* int_div_si */
815 COSTS_N_INSNS (35), /* int_div_di */
816 1, /* branch_cost */
817 4 /* memory_latency */
818 },
819 { /* 4KC */
820 SOFT_FP_COSTS,
821 COSTS_N_INSNS (6), /* int_mult_si */
822 COSTS_N_INSNS (6), /* int_mult_di */
823 COSTS_N_INSNS (36), /* int_div_si */
824 COSTS_N_INSNS (36), /* int_div_di */
825 1, /* branch_cost */
826 4 /* memory_latency */
827 },
828 { /* 4KP */
829 SOFT_FP_COSTS,
830 COSTS_N_INSNS (36), /* int_mult_si */
831 COSTS_N_INSNS (36), /* int_mult_di */
832 COSTS_N_INSNS (37), /* int_div_si */
833 COSTS_N_INSNS (37), /* int_div_di */
834 1, /* branch_cost */
835 4 /* memory_latency */
836 },
837 { /* 5KC */
838 SOFT_FP_COSTS,
839 COSTS_N_INSNS (4), /* int_mult_si */
840 COSTS_N_INSNS (11), /* int_mult_di */
841 COSTS_N_INSNS (36), /* int_div_si */
842 COSTS_N_INSNS (68), /* int_div_di */
843 1, /* branch_cost */
844 4 /* memory_latency */
845 },
846 { /* 5KF */
847 COSTS_N_INSNS (4), /* fp_add */
848 COSTS_N_INSNS (4), /* fp_mult_sf */
849 COSTS_N_INSNS (5), /* fp_mult_df */
850 COSTS_N_INSNS (17), /* fp_div_sf */
851 COSTS_N_INSNS (32), /* fp_div_df */
852 COSTS_N_INSNS (4), /* int_mult_si */
853 COSTS_N_INSNS (11), /* int_mult_di */
854 COSTS_N_INSNS (36), /* int_div_si */
855 COSTS_N_INSNS (68), /* int_div_di */
856 1, /* branch_cost */
857 4 /* memory_latency */
858 },
859 { /* 20KC */
860 COSTS_N_INSNS (4), /* fp_add */
861 COSTS_N_INSNS (4), /* fp_mult_sf */
862 COSTS_N_INSNS (5), /* fp_mult_df */
863 COSTS_N_INSNS (17), /* fp_div_sf */
864 COSTS_N_INSNS (32), /* fp_div_df */
865 COSTS_N_INSNS (4), /* int_mult_si */
866 COSTS_N_INSNS (7), /* int_mult_di */
867 COSTS_N_INSNS (42), /* int_div_si */
868 COSTS_N_INSNS (72), /* int_div_di */
869 1, /* branch_cost */
870 4 /* memory_latency */
871 },
872 { /* 24KC */
873 SOFT_FP_COSTS,
874 COSTS_N_INSNS (5), /* int_mult_si */
875 COSTS_N_INSNS (5), /* int_mult_di */
876 COSTS_N_INSNS (41), /* int_div_si */
877 COSTS_N_INSNS (41), /* int_div_di */
878 1, /* branch_cost */
879 4 /* memory_latency */
880 },
881 { /* 24KF2_1 */
882 COSTS_N_INSNS (8), /* fp_add */
883 COSTS_N_INSNS (8), /* fp_mult_sf */
884 COSTS_N_INSNS (10), /* fp_mult_df */
885 COSTS_N_INSNS (34), /* fp_div_sf */
886 COSTS_N_INSNS (64), /* fp_div_df */
887 COSTS_N_INSNS (5), /* int_mult_si */
888 COSTS_N_INSNS (5), /* int_mult_di */
889 COSTS_N_INSNS (41), /* int_div_si */
890 COSTS_N_INSNS (41), /* int_div_di */
891 1, /* branch_cost */
892 4 /* memory_latency */
893 },
894 { /* 24KF1_1 */
895 COSTS_N_INSNS (4), /* fp_add */
896 COSTS_N_INSNS (4), /* fp_mult_sf */
897 COSTS_N_INSNS (5), /* fp_mult_df */
898 COSTS_N_INSNS (17), /* fp_div_sf */
899 COSTS_N_INSNS (32), /* fp_div_df */
900 COSTS_N_INSNS (5), /* int_mult_si */
901 COSTS_N_INSNS (5), /* int_mult_di */
902 COSTS_N_INSNS (41), /* int_div_si */
903 COSTS_N_INSNS (41), /* int_div_di */
904 1, /* branch_cost */
905 4 /* memory_latency */
906 },
907 { /* 74KC */
908 SOFT_FP_COSTS,
909 COSTS_N_INSNS (5), /* int_mult_si */
910 COSTS_N_INSNS (5), /* int_mult_di */
911 COSTS_N_INSNS (41), /* int_div_si */
912 COSTS_N_INSNS (41), /* int_div_di */
913 1, /* branch_cost */
914 4 /* memory_latency */
915 },
916 { /* 74KF2_1 */
917 COSTS_N_INSNS (8), /* fp_add */
918 COSTS_N_INSNS (8), /* fp_mult_sf */
919 COSTS_N_INSNS (10), /* fp_mult_df */
920 COSTS_N_INSNS (34), /* fp_div_sf */
921 COSTS_N_INSNS (64), /* fp_div_df */
922 COSTS_N_INSNS (5), /* int_mult_si */
923 COSTS_N_INSNS (5), /* int_mult_di */
924 COSTS_N_INSNS (41), /* int_div_si */
925 COSTS_N_INSNS (41), /* int_div_di */
926 1, /* branch_cost */
927 4 /* memory_latency */
928 },
929 { /* 74KF1_1 */
930 COSTS_N_INSNS (4), /* fp_add */
931 COSTS_N_INSNS (4), /* fp_mult_sf */
932 COSTS_N_INSNS (5), /* fp_mult_df */
933 COSTS_N_INSNS (17), /* fp_div_sf */
934 COSTS_N_INSNS (32), /* fp_div_df */
935 COSTS_N_INSNS (5), /* int_mult_si */
936 COSTS_N_INSNS (5), /* int_mult_di */
937 COSTS_N_INSNS (41), /* int_div_si */
938 COSTS_N_INSNS (41), /* int_div_di */
939 1, /* branch_cost */
940 4 /* memory_latency */
941 },
942 { /* 74KF3_2 */
943 COSTS_N_INSNS (6), /* fp_add */
944 COSTS_N_INSNS (6), /* fp_mult_sf */
945 COSTS_N_INSNS (7), /* fp_mult_df */
946 COSTS_N_INSNS (25), /* fp_div_sf */
947 COSTS_N_INSNS (48), /* fp_div_df */
948 COSTS_N_INSNS (5), /* int_mult_si */
949 COSTS_N_INSNS (5), /* int_mult_di */
950 COSTS_N_INSNS (41), /* int_div_si */
951 COSTS_N_INSNS (41), /* int_div_di */
952 1, /* branch_cost */
953 4 /* memory_latency */
954 },
955 { /* Loongson-2E */
956 DEFAULT_COSTS
957 },
958 { /* Loongson-2F */
959 DEFAULT_COSTS
960 },
961 { /* M4k */
962 DEFAULT_COSTS
963 },
964 /* Octeon */
965 {
966 SOFT_FP_COSTS,
967 COSTS_N_INSNS (5), /* int_mult_si */
968 COSTS_N_INSNS (5), /* int_mult_di */
969 COSTS_N_INSNS (72), /* int_div_si */
970 COSTS_N_INSNS (72), /* int_div_di */
971 1, /* branch_cost */
972 4 /* memory_latency */
973 },
974 { /* R3900 */
975 COSTS_N_INSNS (2), /* fp_add */
976 COSTS_N_INSNS (4), /* fp_mult_sf */
977 COSTS_N_INSNS (5), /* fp_mult_df */
978 COSTS_N_INSNS (12), /* fp_div_sf */
979 COSTS_N_INSNS (19), /* fp_div_df */
980 COSTS_N_INSNS (2), /* int_mult_si */
981 COSTS_N_INSNS (2), /* int_mult_di */
982 COSTS_N_INSNS (35), /* int_div_si */
983 COSTS_N_INSNS (35), /* int_div_di */
984 1, /* branch_cost */
985 4 /* memory_latency */
986 },
987 { /* R6000 */
988 COSTS_N_INSNS (3), /* fp_add */
989 COSTS_N_INSNS (5), /* fp_mult_sf */
990 COSTS_N_INSNS (6), /* fp_mult_df */
991 COSTS_N_INSNS (15), /* fp_div_sf */
992 COSTS_N_INSNS (16), /* fp_div_df */
993 COSTS_N_INSNS (17), /* int_mult_si */
994 COSTS_N_INSNS (17), /* int_mult_di */
995 COSTS_N_INSNS (38), /* int_div_si */
996 COSTS_N_INSNS (38), /* int_div_di */
997 2, /* branch_cost */
998 6 /* memory_latency */
999 },
1000 { /* R4000 */
1001 COSTS_N_INSNS (6), /* fp_add */
1002 COSTS_N_INSNS (7), /* fp_mult_sf */
1003 COSTS_N_INSNS (8), /* fp_mult_df */
1004 COSTS_N_INSNS (23), /* fp_div_sf */
1005 COSTS_N_INSNS (36), /* fp_div_df */
1006 COSTS_N_INSNS (10), /* int_mult_si */
1007 COSTS_N_INSNS (10), /* int_mult_di */
1008 COSTS_N_INSNS (69), /* int_div_si */
1009 COSTS_N_INSNS (69), /* int_div_di */
1010 2, /* branch_cost */
1011 6 /* memory_latency */
1012 },
1013 { /* R4100 */
1014 DEFAULT_COSTS
1015 },
1016 { /* R4111 */
1017 DEFAULT_COSTS
1018 },
1019 { /* R4120 */
1020 DEFAULT_COSTS
1021 },
1022 { /* R4130 */
1023 /* The only costs that appear to be updated here are
1024 integer multiplication. */
1025 SOFT_FP_COSTS,
1026 COSTS_N_INSNS (4), /* int_mult_si */
1027 COSTS_N_INSNS (6), /* int_mult_di */
1028 COSTS_N_INSNS (69), /* int_div_si */
1029 COSTS_N_INSNS (69), /* int_div_di */
1030 1, /* branch_cost */
1031 4 /* memory_latency */
1032 },
1033 { /* R4300 */
1034 DEFAULT_COSTS
1035 },
1036 { /* R4600 */
1037 DEFAULT_COSTS
1038 },
1039 { /* R4650 */
1040 DEFAULT_COSTS
1041 },
1042 { /* R5000 */
1043 COSTS_N_INSNS (6), /* fp_add */
1044 COSTS_N_INSNS (4), /* fp_mult_sf */
1045 COSTS_N_INSNS (5), /* fp_mult_df */
1046 COSTS_N_INSNS (23), /* fp_div_sf */
1047 COSTS_N_INSNS (36), /* fp_div_df */
1048 COSTS_N_INSNS (5), /* int_mult_si */
1049 COSTS_N_INSNS (5), /* int_mult_di */
1050 COSTS_N_INSNS (36), /* int_div_si */
1051 COSTS_N_INSNS (36), /* int_div_di */
1052 1, /* branch_cost */
1053 4 /* memory_latency */
1054 },
1055 { /* R5400 */
1056 COSTS_N_INSNS (6), /* fp_add */
1057 COSTS_N_INSNS (5), /* fp_mult_sf */
1058 COSTS_N_INSNS (6), /* fp_mult_df */
1059 COSTS_N_INSNS (30), /* fp_div_sf */
1060 COSTS_N_INSNS (59), /* fp_div_df */
1061 COSTS_N_INSNS (3), /* int_mult_si */
1062 COSTS_N_INSNS (4), /* int_mult_di */
1063 COSTS_N_INSNS (42), /* int_div_si */
1064 COSTS_N_INSNS (74), /* int_div_di */
1065 1, /* branch_cost */
1066 4 /* memory_latency */
1067 },
1068 { /* R5500 */
1069 COSTS_N_INSNS (6), /* fp_add */
1070 COSTS_N_INSNS (5), /* fp_mult_sf */
1071 COSTS_N_INSNS (6), /* fp_mult_df */
1072 COSTS_N_INSNS (30), /* fp_div_sf */
1073 COSTS_N_INSNS (59), /* fp_div_df */
1074 COSTS_N_INSNS (5), /* int_mult_si */
1075 COSTS_N_INSNS (9), /* int_mult_di */
1076 COSTS_N_INSNS (42), /* int_div_si */
1077 COSTS_N_INSNS (74), /* int_div_di */
1078 1, /* branch_cost */
1079 4 /* memory_latency */
1080 },
1081 { /* R7000 */
1082 /* The only costs that are changed here are
1083 integer multiplication. */
1084 COSTS_N_INSNS (6), /* fp_add */
1085 COSTS_N_INSNS (7), /* fp_mult_sf */
1086 COSTS_N_INSNS (8), /* fp_mult_df */
1087 COSTS_N_INSNS (23), /* fp_div_sf */
1088 COSTS_N_INSNS (36), /* fp_div_df */
1089 COSTS_N_INSNS (5), /* int_mult_si */
1090 COSTS_N_INSNS (9), /* int_mult_di */
1091 COSTS_N_INSNS (69), /* int_div_si */
1092 COSTS_N_INSNS (69), /* int_div_di */
1093 1, /* branch_cost */
1094 4 /* memory_latency */
1095 },
1096 { /* R8000 */
1097 DEFAULT_COSTS
1098 },
1099 { /* R9000 */
1100 /* The only costs that are changed here are
1101 integer multiplication. */
1102 COSTS_N_INSNS (6), /* fp_add */
1103 COSTS_N_INSNS (7), /* fp_mult_sf */
1104 COSTS_N_INSNS (8), /* fp_mult_df */
1105 COSTS_N_INSNS (23), /* fp_div_sf */
1106 COSTS_N_INSNS (36), /* fp_div_df */
1107 COSTS_N_INSNS (3), /* int_mult_si */
1108 COSTS_N_INSNS (8), /* int_mult_di */
1109 COSTS_N_INSNS (69), /* int_div_si */
1110 COSTS_N_INSNS (69), /* int_div_di */
1111 1, /* branch_cost */
1112 4 /* memory_latency */
1113 },
1114 { /* R1x000 */
1115 COSTS_N_INSNS (2), /* fp_add */
1116 COSTS_N_INSNS (2), /* fp_mult_sf */
1117 COSTS_N_INSNS (2), /* fp_mult_df */
1118 COSTS_N_INSNS (12), /* fp_div_sf */
1119 COSTS_N_INSNS (19), /* fp_div_df */
1120 COSTS_N_INSNS (5), /* int_mult_si */
1121 COSTS_N_INSNS (9), /* int_mult_di */
1122 COSTS_N_INSNS (34), /* int_div_si */
1123 COSTS_N_INSNS (66), /* int_div_di */
1124 1, /* branch_cost */
1125 4 /* memory_latency */
1126 },
1127 { /* SB1 */
1128 /* These costs are the same as the SB-1A below. */
1129 COSTS_N_INSNS (4), /* fp_add */
1130 COSTS_N_INSNS (4), /* fp_mult_sf */
1131 COSTS_N_INSNS (4), /* fp_mult_df */
1132 COSTS_N_INSNS (24), /* fp_div_sf */
1133 COSTS_N_INSNS (32), /* fp_div_df */
1134 COSTS_N_INSNS (3), /* int_mult_si */
1135 COSTS_N_INSNS (4), /* int_mult_di */
1136 COSTS_N_INSNS (36), /* int_div_si */
1137 COSTS_N_INSNS (68), /* int_div_di */
1138 1, /* branch_cost */
1139 4 /* memory_latency */
1140 },
1141 { /* SB1-A */
1142 /* These costs are the same as the SB-1 above. */
1143 COSTS_N_INSNS (4), /* fp_add */
1144 COSTS_N_INSNS (4), /* fp_mult_sf */
1145 COSTS_N_INSNS (4), /* fp_mult_df */
1146 COSTS_N_INSNS (24), /* fp_div_sf */
1147 COSTS_N_INSNS (32), /* fp_div_df */
1148 COSTS_N_INSNS (3), /* int_mult_si */
1149 COSTS_N_INSNS (4), /* int_mult_di */
1150 COSTS_N_INSNS (36), /* int_div_si */
1151 COSTS_N_INSNS (68), /* int_div_di */
1152 1, /* branch_cost */
1153 4 /* memory_latency */
1154 },
1155 { /* SR71000 */
1156 DEFAULT_COSTS
1157 },
1158 { /* XLR */
1159 SOFT_FP_COSTS,
1160 COSTS_N_INSNS (8), /* int_mult_si */
1161 COSTS_N_INSNS (8), /* int_mult_di */
1162 COSTS_N_INSNS (72), /* int_div_si */
1163 COSTS_N_INSNS (72), /* int_div_di */
1164 1, /* branch_cost */
1165 4 /* memory_latency */
1166 }
1167 };
1168 \f
1169 static rtx mips_find_pic_call_symbol (rtx, rtx);
1170 \f
1171 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1172 for -mflip_mips16. It maps decl names onto a boolean mode setting. */
1173 struct GTY (()) mflip_mips16_entry {
1174 const char *name;
1175 bool mips16_p;
1176 };
1177 static GTY ((param_is (struct mflip_mips16_entry))) htab_t mflip_mips16_htab;
1178
1179 /* Hash table callbacks for mflip_mips16_htab. */
1180
1181 static hashval_t
1182 mflip_mips16_htab_hash (const void *entry)
1183 {
1184 return htab_hash_string (((const struct mflip_mips16_entry *) entry)->name);
1185 }
1186
1187 static int
1188 mflip_mips16_htab_eq (const void *entry, const void *name)
1189 {
1190 return strcmp (((const struct mflip_mips16_entry *) entry)->name,
1191 (const char *) name) == 0;
1192 }
1193
1194 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1195 mode, false if it should next add an attribute for the opposite mode. */
1196 static GTY(()) bool mips16_flipper;
1197
1198 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1199 for -mflip-mips16. Return true if it should use "mips16" and false if
1200 it should use "nomips16". */
1201
1202 static bool
1203 mflip_mips16_use_mips16_p (tree decl)
1204 {
1205 struct mflip_mips16_entry *entry;
1206 const char *name;
1207 hashval_t hash;
1208 void **slot;
1209
1210 /* Use the opposite of the command-line setting for anonymous decls. */
1211 if (!DECL_NAME (decl))
1212 return !mips_base_mips16;
1213
1214 if (!mflip_mips16_htab)
1215 mflip_mips16_htab = htab_create_ggc (37, mflip_mips16_htab_hash,
1216 mflip_mips16_htab_eq, NULL);
1217
1218 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1219 hash = htab_hash_string (name);
1220 slot = htab_find_slot_with_hash (mflip_mips16_htab, name, hash, INSERT);
1221 entry = (struct mflip_mips16_entry *) *slot;
1222 if (!entry)
1223 {
1224 mips16_flipper = !mips16_flipper;
1225 entry = ggc_alloc_mflip_mips16_entry ();
1226 entry->name = name;
1227 entry->mips16_p = mips16_flipper ? !mips_base_mips16 : mips_base_mips16;
1228 *slot = entry;
1229 }
1230 return entry->mips16_p;
1231 }
1232 \f
1233 /* Predicates to test for presence of "near" and "far"/"long_call"
1234 attributes on the given TYPE. */
1235
1236 static bool
1237 mips_near_type_p (const_tree type)
1238 {
1239 return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1240 }
1241
1242 static bool
1243 mips_far_type_p (const_tree type)
1244 {
1245 return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1246 || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1247 }
1248
1249 /* Similar predicates for "mips16"/"nomips16" function attributes. */
1250
1251 static bool
1252 mips_mips16_decl_p (const_tree decl)
1253 {
1254 return lookup_attribute ("mips16", DECL_ATTRIBUTES (decl)) != NULL;
1255 }
1256
1257 static bool
1258 mips_nomips16_decl_p (const_tree decl)
1259 {
1260 return lookup_attribute ("nomips16", DECL_ATTRIBUTES (decl)) != NULL;
1261 }
1262
1263 /* Check if the interrupt attribute is set for a function. */
1264
1265 static bool
1266 mips_interrupt_type_p (tree type)
1267 {
1268 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1269 }
1270
1271 /* Check if the attribute to use shadow register set is set for a function. */
1272
1273 static bool
1274 mips_use_shadow_register_set_p (tree type)
1275 {
1276 return lookup_attribute ("use_shadow_register_set",
1277 TYPE_ATTRIBUTES (type)) != NULL;
1278 }
1279
1280 /* Check if the attribute to keep interrupts masked is set for a function. */
1281
1282 static bool
1283 mips_keep_interrupts_masked_p (tree type)
1284 {
1285 return lookup_attribute ("keep_interrupts_masked",
1286 TYPE_ATTRIBUTES (type)) != NULL;
1287 }
1288
1289 /* Check if the attribute to use debug exception return is set for
1290 a function. */
1291
1292 static bool
1293 mips_use_debug_exception_return_p (tree type)
1294 {
1295 return lookup_attribute ("use_debug_exception_return",
1296 TYPE_ATTRIBUTES (type)) != NULL;
1297 }
1298
1299 /* Return true if function DECL is a MIPS16 function. Return the ambient
1300 setting if DECL is null. */
1301
1302 static bool
1303 mips_use_mips16_mode_p (tree decl)
1304 {
1305 if (decl)
1306 {
1307 /* Nested functions must use the same frame pointer as their
1308 parent and must therefore use the same ISA mode. */
1309 tree parent = decl_function_context (decl);
1310 if (parent)
1311 decl = parent;
1312 if (mips_mips16_decl_p (decl))
1313 return true;
1314 if (mips_nomips16_decl_p (decl))
1315 return false;
1316 }
1317 return mips_base_mips16;
1318 }
1319
1320 /* Implement TARGET_COMP_TYPE_ATTRIBUTES. */
1321
1322 static int
1323 mips_comp_type_attributes (const_tree type1, const_tree type2)
1324 {
1325 /* Disallow mixed near/far attributes. */
1326 if (mips_far_type_p (type1) && mips_near_type_p (type2))
1327 return 0;
1328 if (mips_near_type_p (type1) && mips_far_type_p (type2))
1329 return 0;
1330 return 1;
1331 }
1332
1333 /* Implement TARGET_INSERT_ATTRIBUTES. */
1334
1335 static void
1336 mips_insert_attributes (tree decl, tree *attributes)
1337 {
1338 const char *name;
1339 bool mips16_p, nomips16_p;
1340
1341 /* Check for "mips16" and "nomips16" attributes. */
1342 mips16_p = lookup_attribute ("mips16", *attributes) != NULL;
1343 nomips16_p = lookup_attribute ("nomips16", *attributes) != NULL;
1344 if (TREE_CODE (decl) != FUNCTION_DECL)
1345 {
1346 if (mips16_p)
1347 error ("%qs attribute only applies to functions", "mips16");
1348 if (nomips16_p)
1349 error ("%qs attribute only applies to functions", "nomips16");
1350 }
1351 else
1352 {
1353 mips16_p |= mips_mips16_decl_p (decl);
1354 nomips16_p |= mips_nomips16_decl_p (decl);
1355 if (mips16_p || nomips16_p)
1356 {
1357 /* DECL cannot be simultaneously "mips16" and "nomips16". */
1358 if (mips16_p && nomips16_p)
1359 error ("%qE cannot have both %<mips16%> and "
1360 "%<nomips16%> attributes",
1361 DECL_NAME (decl));
1362 }
1363 else if (TARGET_FLIP_MIPS16 && !DECL_ARTIFICIAL (decl))
1364 {
1365 /* Implement -mflip-mips16. If DECL has neither a "nomips16" nor a
1366 "mips16" attribute, arbitrarily pick one. We must pick the same
1367 setting for duplicate declarations of a function. */
1368 name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1369 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1370 }
1371 }
1372 }
1373
1374 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
1375
1376 static tree
1377 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1378 {
1379 /* The decls' "mips16" and "nomips16" attributes must match exactly. */
1380 if (mips_mips16_decl_p (olddecl) != mips_mips16_decl_p (newdecl))
1381 error ("%qE redeclared with conflicting %qs attributes",
1382 DECL_NAME (newdecl), "mips16");
1383 if (mips_nomips16_decl_p (olddecl) != mips_nomips16_decl_p (newdecl))
1384 error ("%qE redeclared with conflicting %qs attributes",
1385 DECL_NAME (newdecl), "nomips16");
1386
1387 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1388 DECL_ATTRIBUTES (newdecl));
1389 }
1390 \f
1391 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1392 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
1393
1394 static void
1395 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1396 {
1397 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1398 {
1399 *base_ptr = XEXP (x, 0);
1400 *offset_ptr = INTVAL (XEXP (x, 1));
1401 }
1402 else
1403 {
1404 *base_ptr = x;
1405 *offset_ptr = 0;
1406 }
1407 }
1408 \f
1409 static unsigned int mips_build_integer (struct mips_integer_op *,
1410 unsigned HOST_WIDE_INT);
1411
1412 /* A subroutine of mips_build_integer, with the same interface.
1413 Assume that the final action in the sequence should be a left shift. */
1414
1415 static unsigned int
1416 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1417 {
1418 unsigned int i, shift;
1419
1420 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1421 since signed numbers are easier to load than unsigned ones. */
1422 shift = 0;
1423 while ((value & 1) == 0)
1424 value /= 2, shift++;
1425
1426 i = mips_build_integer (codes, value);
1427 codes[i].code = ASHIFT;
1428 codes[i].value = shift;
1429 return i + 1;
1430 }
1431
1432 /* As for mips_build_shift, but assume that the final action will be
1433 an IOR or PLUS operation. */
1434
1435 static unsigned int
1436 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1437 {
1438 unsigned HOST_WIDE_INT high;
1439 unsigned int i;
1440
1441 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1442 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1443 {
1444 /* The constant is too complex to load with a simple LUI/ORI pair,
1445 so we want to give the recursive call as many trailing zeros as
1446 possible. In this case, we know bit 16 is set and that the
1447 low 16 bits form a negative number. If we subtract that number
1448 from VALUE, we will clear at least the lowest 17 bits, maybe more. */
1449 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1450 codes[i].code = PLUS;
1451 codes[i].value = CONST_LOW_PART (value);
1452 }
1453 else
1454 {
1455 /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1456 bits gives a value with at least 17 trailing zeros. */
1457 i = mips_build_integer (codes, high);
1458 codes[i].code = IOR;
1459 codes[i].value = value & 0xffff;
1460 }
1461 return i + 1;
1462 }
1463
1464 /* Fill CODES with a sequence of rtl operations to load VALUE.
1465 Return the number of operations needed. */
1466
1467 static unsigned int
1468 mips_build_integer (struct mips_integer_op *codes,
1469 unsigned HOST_WIDE_INT value)
1470 {
1471 if (SMALL_OPERAND (value)
1472 || SMALL_OPERAND_UNSIGNED (value)
1473 || LUI_OPERAND (value))
1474 {
1475 /* The value can be loaded with a single instruction. */
1476 codes[0].code = UNKNOWN;
1477 codes[0].value = value;
1478 return 1;
1479 }
1480 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1481 {
1482 /* Either the constant is a simple LUI/ORI combination or its
1483 lowest bit is set. We don't want to shift in this case. */
1484 return mips_build_lower (codes, value);
1485 }
1486 else if ((value & 0xffff) == 0)
1487 {
1488 /* The constant will need at least three actions. The lowest
1489 16 bits are clear, so the final action will be a shift. */
1490 return mips_build_shift (codes, value);
1491 }
1492 else
1493 {
1494 /* The final action could be a shift, add or inclusive OR.
1495 Rather than use a complex condition to select the best
1496 approach, try both mips_build_shift and mips_build_lower
1497 and pick the one that gives the shortest sequence.
1498 Note that this case is only used once per constant. */
1499 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1500 unsigned int cost, alt_cost;
1501
1502 cost = mips_build_shift (codes, value);
1503 alt_cost = mips_build_lower (alt_codes, value);
1504 if (alt_cost < cost)
1505 {
1506 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1507 cost = alt_cost;
1508 }
1509 return cost;
1510 }
1511 }
1512 \f
1513 /* Return true if symbols of type TYPE require a GOT access. */
1514
1515 static bool
1516 mips_got_symbol_type_p (enum mips_symbol_type type)
1517 {
1518 switch (type)
1519 {
1520 case SYMBOL_GOT_PAGE_OFST:
1521 case SYMBOL_GOT_DISP:
1522 return true;
1523
1524 default:
1525 return false;
1526 }
1527 }
1528
1529 /* Return true if X is a thread-local symbol. */
1530
1531 static bool
1532 mips_tls_symbol_p (rtx x)
1533 {
1534 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1535 }
1536
1537 /* Return true if SYMBOL_REF X is associated with a global symbol
1538 (in the STB_GLOBAL sense). */
1539
1540 static bool
1541 mips_global_symbol_p (const_rtx x)
1542 {
1543 const_tree decl = SYMBOL_REF_DECL (x);
1544
1545 if (!decl)
1546 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1547
1548 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1549 or weak symbols. Relocations in the object file will be against
1550 the target symbol, so it's that symbol's binding that matters here. */
1551 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1552 }
1553
1554 /* Return true if function X is a libgcc MIPS16 stub function. */
1555
1556 static bool
1557 mips16_stub_function_p (const_rtx x)
1558 {
1559 return (GET_CODE (x) == SYMBOL_REF
1560 && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1561 }
1562
1563 /* Return true if function X is a locally-defined and locally-binding
1564 MIPS16 function. */
1565
1566 static bool
1567 mips16_local_function_p (const_rtx x)
1568 {
1569 return (GET_CODE (x) == SYMBOL_REF
1570 && SYMBOL_REF_LOCAL_P (x)
1571 && !SYMBOL_REF_EXTERNAL_P (x)
1572 && mips_use_mips16_mode_p (SYMBOL_REF_DECL (x)));
1573 }
1574
1575 /* Return true if SYMBOL_REF X binds locally. */
1576
1577 static bool
1578 mips_symbol_binds_local_p (const_rtx x)
1579 {
1580 return (SYMBOL_REF_DECL (x)
1581 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1582 : SYMBOL_REF_LOCAL_P (x));
1583 }
1584
1585 /* Return true if rtx constants of mode MODE should be put into a small
1586 data section. */
1587
1588 static bool
1589 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1590 {
1591 return (!TARGET_EMBEDDED_DATA
1592 && TARGET_LOCAL_SDATA
1593 && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1594 }
1595
1596 /* Return true if X should not be moved directly into register $25.
1597 We need this because many versions of GAS will treat "la $25,foo" as
1598 part of a call sequence and so allow a global "foo" to be lazily bound. */
1599
1600 bool
1601 mips_dangerous_for_la25_p (rtx x)
1602 {
1603 return (!TARGET_EXPLICIT_RELOCS
1604 && TARGET_USE_GOT
1605 && GET_CODE (x) == SYMBOL_REF
1606 && mips_global_symbol_p (x));
1607 }
1608
1609 /* Return true if calls to X might need $25 to be valid on entry. */
1610
1611 bool
1612 mips_use_pic_fn_addr_reg_p (const_rtx x)
1613 {
1614 if (!TARGET_USE_PIC_FN_ADDR_REG)
1615 return false;
1616
1617 /* MIPS16 stub functions are guaranteed not to use $25. */
1618 if (mips16_stub_function_p (x))
1619 return false;
1620
1621 if (GET_CODE (x) == SYMBOL_REF)
1622 {
1623 /* If PLTs and copy relocations are available, the static linker
1624 will make sure that $25 is valid on entry to the target function. */
1625 if (TARGET_ABICALLS_PIC0)
1626 return false;
1627
1628 /* Locally-defined functions use absolute accesses to set up
1629 the global pointer. */
1630 if (TARGET_ABSOLUTE_ABICALLS
1631 && mips_symbol_binds_local_p (x)
1632 && !SYMBOL_REF_EXTERNAL_P (x))
1633 return false;
1634 }
1635
1636 return true;
1637 }
1638
1639 /* Return the method that should be used to access SYMBOL_REF or
1640 LABEL_REF X in context CONTEXT. */
1641
1642 static enum mips_symbol_type
1643 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1644 {
1645 if (TARGET_RTP_PIC)
1646 return SYMBOL_GOT_DISP;
1647
1648 if (GET_CODE (x) == LABEL_REF)
1649 {
1650 /* LABEL_REFs are used for jump tables as well as text labels.
1651 Only return SYMBOL_PC_RELATIVE if we know the label is in
1652 the text section. */
1653 if (TARGET_MIPS16_SHORT_JUMP_TABLES)
1654 return SYMBOL_PC_RELATIVE;
1655
1656 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1657 return SYMBOL_GOT_PAGE_OFST;
1658
1659 return SYMBOL_ABSOLUTE;
1660 }
1661
1662 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1663
1664 if (SYMBOL_REF_TLS_MODEL (x))
1665 return SYMBOL_TLS;
1666
1667 if (CONSTANT_POOL_ADDRESS_P (x))
1668 {
1669 if (TARGET_MIPS16_TEXT_LOADS)
1670 return SYMBOL_PC_RELATIVE;
1671
1672 if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1673 return SYMBOL_PC_RELATIVE;
1674
1675 if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1676 return SYMBOL_GP_RELATIVE;
1677 }
1678
1679 /* Do not use small-data accesses for weak symbols; they may end up
1680 being zero. */
1681 if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1682 return SYMBOL_GP_RELATIVE;
1683
1684 /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1685 is in effect. */
1686 if (TARGET_ABICALLS_PIC2
1687 && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1688 {
1689 /* There are three cases to consider:
1690
1691 - o32 PIC (either with or without explicit relocs)
1692 - n32/n64 PIC without explicit relocs
1693 - n32/n64 PIC with explicit relocs
1694
1695 In the first case, both local and global accesses will use an
1696 R_MIPS_GOT16 relocation. We must correctly predict which of
1697 the two semantics (local or global) the assembler and linker
1698 will apply. The choice depends on the symbol's binding rather
1699 than its visibility.
1700
1701 In the second case, the assembler will not use R_MIPS_GOT16
1702 relocations, but it chooses between local and global accesses
1703 in the same way as for o32 PIC.
1704
1705 In the third case we have more freedom since both forms of
1706 access will work for any kind of symbol. However, there seems
1707 little point in doing things differently. */
1708 if (mips_global_symbol_p (x))
1709 return SYMBOL_GOT_DISP;
1710
1711 return SYMBOL_GOT_PAGE_OFST;
1712 }
1713
1714 if (TARGET_MIPS16_PCREL_LOADS && context != SYMBOL_CONTEXT_CALL)
1715 return SYMBOL_FORCE_TO_MEM;
1716
1717 return SYMBOL_ABSOLUTE;
1718 }
1719
1720 /* Classify the base of symbolic expression X, given that X appears in
1721 context CONTEXT. */
1722
1723 static enum mips_symbol_type
1724 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1725 {
1726 rtx offset;
1727
1728 split_const (x, &x, &offset);
1729 if (UNSPEC_ADDRESS_P (x))
1730 return UNSPEC_ADDRESS_TYPE (x);
1731
1732 return mips_classify_symbol (x, context);
1733 }
1734
1735 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1736 is the alignment in bytes of SYMBOL_REF X. */
1737
1738 static bool
1739 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1740 {
1741 HOST_WIDE_INT align;
1742
1743 align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1744 return IN_RANGE (offset, 0, align - 1);
1745 }
1746
1747 /* Return true if X is a symbolic constant that can be used in context
1748 CONTEXT. If it is, store the type of the symbol in *SYMBOL_TYPE. */
1749
1750 bool
1751 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1752 enum mips_symbol_type *symbol_type)
1753 {
1754 rtx offset;
1755
1756 split_const (x, &x, &offset);
1757 if (UNSPEC_ADDRESS_P (x))
1758 {
1759 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1760 x = UNSPEC_ADDRESS (x);
1761 }
1762 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1763 {
1764 *symbol_type = mips_classify_symbol (x, context);
1765 if (*symbol_type == SYMBOL_TLS)
1766 return false;
1767 }
1768 else
1769 return false;
1770
1771 if (offset == const0_rtx)
1772 return true;
1773
1774 /* Check whether a nonzero offset is valid for the underlying
1775 relocations. */
1776 switch (*symbol_type)
1777 {
1778 case SYMBOL_ABSOLUTE:
1779 case SYMBOL_FORCE_TO_MEM:
1780 case SYMBOL_32_HIGH:
1781 case SYMBOL_64_HIGH:
1782 case SYMBOL_64_MID:
1783 case SYMBOL_64_LOW:
1784 /* If the target has 64-bit pointers and the object file only
1785 supports 32-bit symbols, the values of those symbols will be
1786 sign-extended. In this case we can't allow an arbitrary offset
1787 in case the 32-bit value X + OFFSET has a different sign from X. */
1788 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1789 return offset_within_block_p (x, INTVAL (offset));
1790
1791 /* In other cases the relocations can handle any offset. */
1792 return true;
1793
1794 case SYMBOL_PC_RELATIVE:
1795 /* Allow constant pool references to be converted to LABEL+CONSTANT.
1796 In this case, we no longer have access to the underlying constant,
1797 but the original symbol-based access was known to be valid. */
1798 if (GET_CODE (x) == LABEL_REF)
1799 return true;
1800
1801 /* Fall through. */
1802
1803 case SYMBOL_GP_RELATIVE:
1804 /* Make sure that the offset refers to something within the
1805 same object block. This should guarantee that the final
1806 PC- or GP-relative offset is within the 16-bit limit. */
1807 return offset_within_block_p (x, INTVAL (offset));
1808
1809 case SYMBOL_GOT_PAGE_OFST:
1810 case SYMBOL_GOTOFF_PAGE:
1811 /* If the symbol is global, the GOT entry will contain the symbol's
1812 address, and we will apply a 16-bit offset after loading it.
1813 If the symbol is local, the linker should provide enough local
1814 GOT entries for a 16-bit offset, but larger offsets may lead
1815 to GOT overflow. */
1816 return SMALL_INT (offset);
1817
1818 case SYMBOL_TPREL:
1819 case SYMBOL_DTPREL:
1820 /* There is no carry between the HI and LO REL relocations, so the
1821 offset is only valid if we know it won't lead to such a carry. */
1822 return mips_offset_within_alignment_p (x, INTVAL (offset));
1823
1824 case SYMBOL_GOT_DISP:
1825 case SYMBOL_GOTOFF_DISP:
1826 case SYMBOL_GOTOFF_CALL:
1827 case SYMBOL_GOTOFF_LOADGP:
1828 case SYMBOL_TLSGD:
1829 case SYMBOL_TLSLDM:
1830 case SYMBOL_GOTTPREL:
1831 case SYMBOL_TLS:
1832 case SYMBOL_HALF:
1833 return false;
1834 }
1835 gcc_unreachable ();
1836 }
1837 \f
1838 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
1839 single instruction. We rely on the fact that, in the worst case,
1840 all instructions involved in a MIPS16 address calculation are usually
1841 extended ones. */
1842
1843 static int
1844 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
1845 {
1846 switch (type)
1847 {
1848 case SYMBOL_ABSOLUTE:
1849 /* When using 64-bit symbols, we need 5 preparatory instructions,
1850 such as:
1851
1852 lui $at,%highest(symbol)
1853 daddiu $at,$at,%higher(symbol)
1854 dsll $at,$at,16
1855 daddiu $at,$at,%hi(symbol)
1856 dsll $at,$at,16
1857
1858 The final address is then $at + %lo(symbol). With 32-bit
1859 symbols we just need a preparatory LUI for normal mode and
1860 a preparatory LI and SLL for MIPS16. */
1861 return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
1862
1863 case SYMBOL_GP_RELATIVE:
1864 /* Treat GP-relative accesses as taking a single instruction on
1865 MIPS16 too; the copy of $gp can often be shared. */
1866 return 1;
1867
1868 case SYMBOL_PC_RELATIVE:
1869 /* PC-relative constants can be only be used with ADDIUPC,
1870 DADDIUPC, LWPC and LDPC. */
1871 if (mode == MAX_MACHINE_MODE
1872 || GET_MODE_SIZE (mode) == 4
1873 || GET_MODE_SIZE (mode) == 8)
1874 return 1;
1875
1876 /* The constant must be loaded using ADDIUPC or DADDIUPC first. */
1877 return 0;
1878
1879 case SYMBOL_FORCE_TO_MEM:
1880 /* LEAs will be converted into constant-pool references by
1881 mips_reorg. */
1882 if (mode == MAX_MACHINE_MODE)
1883 return 1;
1884
1885 /* The constant must be loaded and then dereferenced. */
1886 return 0;
1887
1888 case SYMBOL_GOT_DISP:
1889 /* The constant will have to be loaded from the GOT before it
1890 is used in an address. */
1891 if (mode != MAX_MACHINE_MODE)
1892 return 0;
1893
1894 /* Fall through. */
1895
1896 case SYMBOL_GOT_PAGE_OFST:
1897 /* Unless -funit-at-a-time is in effect, we can't be sure whether the
1898 local/global classification is accurate. The worst cases are:
1899
1900 (1) For local symbols when generating o32 or o64 code. The assembler
1901 will use:
1902
1903 lw $at,%got(symbol)
1904 nop
1905
1906 ...and the final address will be $at + %lo(symbol).
1907
1908 (2) For global symbols when -mxgot. The assembler will use:
1909
1910 lui $at,%got_hi(symbol)
1911 (d)addu $at,$at,$gp
1912
1913 ...and the final address will be $at + %got_lo(symbol). */
1914 return 3;
1915
1916 case SYMBOL_GOTOFF_PAGE:
1917 case SYMBOL_GOTOFF_DISP:
1918 case SYMBOL_GOTOFF_CALL:
1919 case SYMBOL_GOTOFF_LOADGP:
1920 case SYMBOL_32_HIGH:
1921 case SYMBOL_64_HIGH:
1922 case SYMBOL_64_MID:
1923 case SYMBOL_64_LOW:
1924 case SYMBOL_TLSGD:
1925 case SYMBOL_TLSLDM:
1926 case SYMBOL_DTPREL:
1927 case SYMBOL_GOTTPREL:
1928 case SYMBOL_TPREL:
1929 case SYMBOL_HALF:
1930 /* A 16-bit constant formed by a single relocation, or a 32-bit
1931 constant formed from a high 16-bit relocation and a low 16-bit
1932 relocation. Use mips_split_p to determine which. 32-bit
1933 constants need an "lui; addiu" sequence for normal mode and
1934 an "li; sll; addiu" sequence for MIPS16 mode. */
1935 return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
1936
1937 case SYMBOL_TLS:
1938 /* We don't treat a bare TLS symbol as a constant. */
1939 return 0;
1940 }
1941 gcc_unreachable ();
1942 }
1943
1944 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
1945 to load symbols of type TYPE into a register. Return 0 if the given
1946 type of symbol cannot be used as an immediate operand.
1947
1948 Otherwise, return the number of instructions needed to load or store
1949 values of mode MODE to or from addresses of type TYPE. Return 0 if
1950 the given type of symbol is not valid in addresses.
1951
1952 In both cases, treat extended MIPS16 instructions as two instructions. */
1953
1954 static int
1955 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
1956 {
1957 return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
1958 }
1959 \f
1960 /* A for_each_rtx callback. Stop the search if *X references a
1961 thread-local symbol. */
1962
1963 static int
1964 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1965 {
1966 return mips_tls_symbol_p (*x);
1967 }
1968
1969 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
1970
1971 static bool
1972 mips_cannot_force_const_mem (rtx x)
1973 {
1974 enum mips_symbol_type type;
1975 rtx base, offset;
1976
1977 /* There is no assembler syntax for expressing an address-sized
1978 high part. */
1979 if (GET_CODE (x) == HIGH)
1980 return true;
1981
1982 /* As an optimization, reject constants that mips_legitimize_move
1983 can expand inline.
1984
1985 Suppose we have a multi-instruction sequence that loads constant C
1986 into register R. If R does not get allocated a hard register, and
1987 R is used in an operand that allows both registers and memory
1988 references, reload will consider forcing C into memory and using
1989 one of the instruction's memory alternatives. Returning false
1990 here will force it to use an input reload instead. */
1991 if (CONST_INT_P (x) && LEGITIMATE_CONSTANT_P (x))
1992 return true;
1993
1994 split_const (x, &base, &offset);
1995 if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type)
1996 && type != SYMBOL_FORCE_TO_MEM)
1997 {
1998 /* The same optimization as for CONST_INT. */
1999 if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2000 return true;
2001
2002 /* If MIPS16 constant pools live in the text section, they should
2003 not refer to anything that might need run-time relocation. */
2004 if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2005 return true;
2006 }
2007
2008 /* TLS symbols must be computed by mips_legitimize_move. */
2009 if (for_each_rtx (&x, &mips_tls_symbol_ref_1, NULL))
2010 return true;
2011
2012 return false;
2013 }
2014
2015 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. We can't use blocks for
2016 constants when we're using a per-function constant pool. */
2017
2018 static bool
2019 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2020 const_rtx x ATTRIBUTE_UNUSED)
2021 {
2022 return !TARGET_MIPS16_PCREL_LOADS;
2023 }
2024 \f
2025 /* Return true if register REGNO is a valid base register for mode MODE.
2026 STRICT_P is true if REG_OK_STRICT is in effect. */
2027
2028 int
2029 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
2030 bool strict_p)
2031 {
2032 if (!HARD_REGISTER_NUM_P (regno))
2033 {
2034 if (!strict_p)
2035 return true;
2036 regno = reg_renumber[regno];
2037 }
2038
2039 /* These fake registers will be eliminated to either the stack or
2040 hard frame pointer, both of which are usually valid base registers.
2041 Reload deals with the cases where the eliminated form isn't valid. */
2042 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2043 return true;
2044
2045 /* In MIPS16 mode, the stack pointer can only address word and doubleword
2046 values, nothing smaller. There are two problems here:
2047
2048 (a) Instantiating virtual registers can introduce new uses of the
2049 stack pointer. If these virtual registers are valid addresses,
2050 the stack pointer should be too.
2051
2052 (b) Most uses of the stack pointer are not made explicit until
2053 FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
2054 We don't know until that stage whether we'll be eliminating to the
2055 stack pointer (which needs the restriction) or the hard frame
2056 pointer (which doesn't).
2057
2058 All in all, it seems more consistent to only enforce this restriction
2059 during and after reload. */
2060 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2061 return !strict_p || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2062
2063 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2064 }
2065
2066 /* Return true if X is a valid base register for mode MODE.
2067 STRICT_P is true if REG_OK_STRICT is in effect. */
2068
2069 static bool
2070 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
2071 {
2072 if (!strict_p && GET_CODE (x) == SUBREG)
2073 x = SUBREG_REG (x);
2074
2075 return (REG_P (x)
2076 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2077 }
2078
2079 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2080 can address a value of mode MODE. */
2081
2082 static bool
2083 mips_valid_offset_p (rtx x, enum machine_mode mode)
2084 {
2085 /* Check that X is a signed 16-bit number. */
2086 if (!const_arith_operand (x, Pmode))
2087 return false;
2088
2089 /* We may need to split multiword moves, so make sure that every word
2090 is accessible. */
2091 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2092 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2093 return false;
2094
2095 return true;
2096 }
2097
2098 /* Return true if a LO_SUM can address a value of mode MODE when the
2099 LO_SUM symbol has type SYMBOL_TYPE. */
2100
2101 static bool
2102 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, enum machine_mode mode)
2103 {
2104 /* Check that symbols of type SYMBOL_TYPE can be used to access values
2105 of mode MODE. */
2106 if (mips_symbol_insns (symbol_type, mode) == 0)
2107 return false;
2108
2109 /* Check that there is a known low-part relocation. */
2110 if (mips_lo_relocs[symbol_type] == NULL)
2111 return false;
2112
2113 /* We may need to split multiword moves, so make sure that each word
2114 can be accessed without inducing a carry. This is mainly needed
2115 for o64, which has historically only guaranteed 64-bit alignment
2116 for 128-bit types. */
2117 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2118 && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2119 return false;
2120
2121 return true;
2122 }
2123
2124 /* Return true if X is a valid address for machine mode MODE. If it is,
2125 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
2126 effect. */
2127
2128 static bool
2129 mips_classify_address (struct mips_address_info *info, rtx x,
2130 enum machine_mode mode, bool strict_p)
2131 {
2132 switch (GET_CODE (x))
2133 {
2134 case REG:
2135 case SUBREG:
2136 info->type = ADDRESS_REG;
2137 info->reg = x;
2138 info->offset = const0_rtx;
2139 return mips_valid_base_register_p (info->reg, mode, strict_p);
2140
2141 case PLUS:
2142 info->type = ADDRESS_REG;
2143 info->reg = XEXP (x, 0);
2144 info->offset = XEXP (x, 1);
2145 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2146 && mips_valid_offset_p (info->offset, mode));
2147
2148 case LO_SUM:
2149 info->type = ADDRESS_LO_SUM;
2150 info->reg = XEXP (x, 0);
2151 info->offset = XEXP (x, 1);
2152 /* We have to trust the creator of the LO_SUM to do something vaguely
2153 sane. Target-independent code that creates a LO_SUM should also
2154 create and verify the matching HIGH. Target-independent code that
2155 adds an offset to a LO_SUM must prove that the offset will not
2156 induce a carry. Failure to do either of these things would be
2157 a bug, and we are not required to check for it here. The MIPS
2158 backend itself should only create LO_SUMs for valid symbolic
2159 constants, with the high part being either a HIGH or a copy
2160 of _gp. */
2161 info->symbol_type
2162 = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2163 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2164 && mips_valid_lo_sum_p (info->symbol_type, mode));
2165
2166 case CONST_INT:
2167 /* Small-integer addresses don't occur very often, but they
2168 are legitimate if $0 is a valid base register. */
2169 info->type = ADDRESS_CONST_INT;
2170 return !TARGET_MIPS16 && SMALL_INT (x);
2171
2172 case CONST:
2173 case LABEL_REF:
2174 case SYMBOL_REF:
2175 info->type = ADDRESS_SYMBOLIC;
2176 return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2177 &info->symbol_type)
2178 && mips_symbol_insns (info->symbol_type, mode) > 0
2179 && !mips_split_p[info->symbol_type]);
2180
2181 default:
2182 return false;
2183 }
2184 }
2185
2186 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2187
2188 static bool
2189 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2190 {
2191 struct mips_address_info addr;
2192
2193 return mips_classify_address (&addr, x, mode, strict_p);
2194 }
2195
2196 /* Return true if X is a legitimate $sp-based address for mode MDOE. */
2197
2198 bool
2199 mips_stack_address_p (rtx x, enum machine_mode mode)
2200 {
2201 struct mips_address_info addr;
2202
2203 return (mips_classify_address (&addr, x, mode, false)
2204 && addr.type == ADDRESS_REG
2205 && addr.reg == stack_pointer_rtx);
2206 }
2207
2208 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2209 address instruction. Note that such addresses are not considered
2210 legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2211 is so restricted. */
2212
2213 static bool
2214 mips_lwxs_address_p (rtx addr)
2215 {
2216 if (ISA_HAS_LWXS
2217 && GET_CODE (addr) == PLUS
2218 && REG_P (XEXP (addr, 1)))
2219 {
2220 rtx offset = XEXP (addr, 0);
2221 if (GET_CODE (offset) == MULT
2222 && REG_P (XEXP (offset, 0))
2223 && CONST_INT_P (XEXP (offset, 1))
2224 && INTVAL (XEXP (offset, 1)) == 4)
2225 return true;
2226 }
2227 return false;
2228 }
2229 \f
2230 /* Return true if a value at OFFSET bytes from base register BASE can be
2231 accessed using an unextended MIPS16 instruction. MODE is the mode of
2232 the value.
2233
2234 Usually the offset in an unextended instruction is a 5-bit field.
2235 The offset is unsigned and shifted left once for LH and SH, twice
2236 for LW and SW, and so on. An exception is LWSP and SWSP, which have
2237 an 8-bit immediate field that's shifted left twice. */
2238
2239 static bool
2240 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
2241 unsigned HOST_WIDE_INT offset)
2242 {
2243 if (offset % GET_MODE_SIZE (mode) == 0)
2244 {
2245 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2246 return offset < 256U * GET_MODE_SIZE (mode);
2247 return offset < 32U * GET_MODE_SIZE (mode);
2248 }
2249 return false;
2250 }
2251
2252 /* Return the number of instructions needed to load or store a value
2253 of mode MODE at address X. Return 0 if X isn't valid for MODE.
2254 Assume that multiword moves may need to be split into word moves
2255 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2256 enough.
2257
2258 For MIPS16 code, count extended instructions as two instructions. */
2259
2260 int
2261 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
2262 {
2263 struct mips_address_info addr;
2264 int factor;
2265
2266 /* BLKmode is used for single unaligned loads and stores and should
2267 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
2268 meaningless, so we have to single it out as a special case one way
2269 or the other.) */
2270 if (mode != BLKmode && might_split_p)
2271 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2272 else
2273 factor = 1;
2274
2275 if (mips_classify_address (&addr, x, mode, false))
2276 switch (addr.type)
2277 {
2278 case ADDRESS_REG:
2279 if (TARGET_MIPS16
2280 && !mips16_unextended_reference_p (mode, addr.reg,
2281 UINTVAL (addr.offset)))
2282 return factor * 2;
2283 return factor;
2284
2285 case ADDRESS_LO_SUM:
2286 return TARGET_MIPS16 ? factor * 2 : factor;
2287
2288 case ADDRESS_CONST_INT:
2289 return factor;
2290
2291 case ADDRESS_SYMBOLIC:
2292 return factor * mips_symbol_insns (addr.symbol_type, mode);
2293 }
2294 return 0;
2295 }
2296
2297 /* Return the number of instructions needed to load constant X.
2298 Return 0 if X isn't a valid constant. */
2299
2300 int
2301 mips_const_insns (rtx x)
2302 {
2303 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2304 enum mips_symbol_type symbol_type;
2305 rtx offset;
2306
2307 switch (GET_CODE (x))
2308 {
2309 case HIGH:
2310 if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2311 &symbol_type)
2312 || !mips_split_p[symbol_type])
2313 return 0;
2314
2315 /* This is simply an LUI for normal mode. It is an extended
2316 LI followed by an extended SLL for MIPS16. */
2317 return TARGET_MIPS16 ? 4 : 1;
2318
2319 case CONST_INT:
2320 if (TARGET_MIPS16)
2321 /* Unsigned 8-bit constants can be loaded using an unextended
2322 LI instruction. Unsigned 16-bit constants can be loaded
2323 using an extended LI. Negative constants must be loaded
2324 using LI and then negated. */
2325 return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2326 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2327 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2328 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2329 : 0);
2330
2331 return mips_build_integer (codes, INTVAL (x));
2332
2333 case CONST_DOUBLE:
2334 case CONST_VECTOR:
2335 /* Allow zeros for normal mode, where we can use $0. */
2336 return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2337
2338 case CONST:
2339 if (CONST_GP_P (x))
2340 return 1;
2341
2342 /* See if we can refer to X directly. */
2343 if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2344 return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2345
2346 /* Otherwise try splitting the constant into a base and offset.
2347 If the offset is a 16-bit value, we can load the base address
2348 into a register and then use (D)ADDIU to add in the offset.
2349 If the offset is larger, we can load the base and offset
2350 into separate registers and add them together with (D)ADDU.
2351 However, the latter is only possible before reload; during
2352 and after reload, we must have the option of forcing the
2353 constant into the pool instead. */
2354 split_const (x, &x, &offset);
2355 if (offset != 0)
2356 {
2357 int n = mips_const_insns (x);
2358 if (n != 0)
2359 {
2360 if (SMALL_INT (offset))
2361 return n + 1;
2362 else if (!targetm.cannot_force_const_mem (x))
2363 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2364 }
2365 }
2366 return 0;
2367
2368 case SYMBOL_REF:
2369 case LABEL_REF:
2370 return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2371 MAX_MACHINE_MODE);
2372
2373 default:
2374 return 0;
2375 }
2376 }
2377
2378 /* X is a doubleword constant that can be handled by splitting it into
2379 two words and loading each word separately. Return the number of
2380 instructions required to do this. */
2381
2382 int
2383 mips_split_const_insns (rtx x)
2384 {
2385 unsigned int low, high;
2386
2387 low = mips_const_insns (mips_subword (x, false));
2388 high = mips_const_insns (mips_subword (x, true));
2389 gcc_assert (low > 0 && high > 0);
2390 return low + high;
2391 }
2392
2393 /* Return the number of instructions needed to implement INSN,
2394 given that it loads from or stores to MEM. Count extended
2395 MIPS16 instructions as two instructions. */
2396
2397 int
2398 mips_load_store_insns (rtx mem, rtx insn)
2399 {
2400 enum machine_mode mode;
2401 bool might_split_p;
2402 rtx set;
2403
2404 gcc_assert (MEM_P (mem));
2405 mode = GET_MODE (mem);
2406
2407 /* Try to prove that INSN does not need to be split. */
2408 might_split_p = true;
2409 if (GET_MODE_BITSIZE (mode) == 64)
2410 {
2411 set = single_set (insn);
2412 if (set && !mips_split_64bit_move_p (SET_DEST (set), SET_SRC (set)))
2413 might_split_p = false;
2414 }
2415
2416 return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2417 }
2418
2419 /* Return the number of instructions needed for an integer division. */
2420
2421 int
2422 mips_idiv_insns (void)
2423 {
2424 int count;
2425
2426 count = 1;
2427 if (TARGET_CHECK_ZERO_DIV)
2428 {
2429 if (GENERATE_DIVIDE_TRAPS)
2430 count++;
2431 else
2432 count += 2;
2433 }
2434
2435 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2436 count++;
2437 return count;
2438 }
2439 \f
2440 /* Emit a move from SRC to DEST. Assume that the move expanders can
2441 handle all moves if !can_create_pseudo_p (). The distinction is
2442 important because, unlike emit_move_insn, the move expanders know
2443 how to force Pmode objects into the constant pool even when the
2444 constant pool address is not itself legitimate. */
2445
2446 rtx
2447 mips_emit_move (rtx dest, rtx src)
2448 {
2449 return (can_create_pseudo_p ()
2450 ? emit_move_insn (dest, src)
2451 : emit_move_insn_1 (dest, src));
2452 }
2453
2454 /* Emit an instruction of the form (set TARGET (CODE OP0)). */
2455
2456 static void
2457 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2458 {
2459 emit_insn (gen_rtx_SET (VOIDmode, target,
2460 gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2461 }
2462
2463 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2464 Return that new register. */
2465
2466 static rtx
2467 mips_force_unary (enum machine_mode mode, enum rtx_code code, rtx op0)
2468 {
2469 rtx reg;
2470
2471 reg = gen_reg_rtx (mode);
2472 mips_emit_unary (code, reg, op0);
2473 return reg;
2474 }
2475
2476 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2477
2478 static void
2479 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2480 {
2481 emit_insn (gen_rtx_SET (VOIDmode, target,
2482 gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2483 }
2484
2485 /* Compute (CODE OP0 OP1) and store the result in a new register
2486 of mode MODE. Return that new register. */
2487
2488 static rtx
2489 mips_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2490 {
2491 rtx reg;
2492
2493 reg = gen_reg_rtx (mode);
2494 mips_emit_binary (code, reg, op0, op1);
2495 return reg;
2496 }
2497
2498 /* Copy VALUE to a register and return that register. If new pseudos
2499 are allowed, copy it into a new register, otherwise use DEST. */
2500
2501 static rtx
2502 mips_force_temporary (rtx dest, rtx value)
2503 {
2504 if (can_create_pseudo_p ())
2505 return force_reg (Pmode, value);
2506 else
2507 {
2508 mips_emit_move (dest, value);
2509 return dest;
2510 }
2511 }
2512
2513 /* Emit a call sequence with call pattern PATTERN and return the call
2514 instruction itself (which is not necessarily the last instruction
2515 emitted). ORIG_ADDR is the original, unlegitimized address,
2516 ADDR is the legitimized form, and LAZY_P is true if the call
2517 address is lazily-bound. */
2518
2519 static rtx
2520 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2521 {
2522 rtx insn, reg;
2523
2524 insn = emit_call_insn (pattern);
2525
2526 if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2527 {
2528 /* MIPS16 JALRs only take MIPS16 registers. If the target
2529 function requires $25 to be valid on entry, we must copy it
2530 there separately. The move instruction can be put in the
2531 call's delay slot. */
2532 reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2533 emit_insn_before (gen_move_insn (reg, addr), insn);
2534 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2535 }
2536
2537 if (lazy_p)
2538 /* Lazy-binding stubs require $gp to be valid on entry. */
2539 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2540
2541 if (TARGET_USE_GOT)
2542 {
2543 /* See the comment above load_call<mode> for details. */
2544 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2545 gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2546 emit_insn (gen_update_got_version ());
2547 }
2548 return insn;
2549 }
2550 \f
2551 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2552 then add CONST_INT OFFSET to the result. */
2553
2554 static rtx
2555 mips_unspec_address_offset (rtx base, rtx offset,
2556 enum mips_symbol_type symbol_type)
2557 {
2558 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2559 UNSPEC_ADDRESS_FIRST + symbol_type);
2560 if (offset != const0_rtx)
2561 base = gen_rtx_PLUS (Pmode, base, offset);
2562 return gen_rtx_CONST (Pmode, base);
2563 }
2564
2565 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2566 type SYMBOL_TYPE. */
2567
2568 rtx
2569 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2570 {
2571 rtx base, offset;
2572
2573 split_const (address, &base, &offset);
2574 return mips_unspec_address_offset (base, offset, symbol_type);
2575 }
2576
2577 /* If OP is an UNSPEC address, return the address to which it refers,
2578 otherwise return OP itself. */
2579
2580 static rtx
2581 mips_strip_unspec_address (rtx op)
2582 {
2583 rtx base, offset;
2584
2585 split_const (op, &base, &offset);
2586 if (UNSPEC_ADDRESS_P (base))
2587 op = plus_constant (UNSPEC_ADDRESS (base), INTVAL (offset));
2588 return op;
2589 }
2590
2591 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2592 high part to BASE and return the result. Just return BASE otherwise.
2593 TEMP is as for mips_force_temporary.
2594
2595 The returned expression can be used as the first operand to a LO_SUM. */
2596
2597 static rtx
2598 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2599 enum mips_symbol_type symbol_type)
2600 {
2601 if (mips_split_p[symbol_type])
2602 {
2603 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2604 addr = mips_force_temporary (temp, addr);
2605 base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2606 }
2607 return base;
2608 }
2609 \f
2610 /* Return an instruction that copies $gp into register REG. We want
2611 GCC to treat the register's value as constant, so that its value
2612 can be rematerialized on demand. */
2613
2614 static rtx
2615 gen_load_const_gp (rtx reg)
2616 {
2617 return (Pmode == SImode
2618 ? gen_load_const_gp_si (reg)
2619 : gen_load_const_gp_di (reg));
2620 }
2621
2622 /* Return a pseudo register that contains the value of $gp throughout
2623 the current function. Such registers are needed by MIPS16 functions,
2624 for which $gp itself is not a valid base register or addition operand. */
2625
2626 static rtx
2627 mips16_gp_pseudo_reg (void)
2628 {
2629 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2630 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2631
2632 /* Don't emit an instruction to initialize the pseudo register if
2633 we are being called from the tree optimizers' cost-calculation
2634 routines. */
2635 if (!cfun->machine->initialized_mips16_gp_pseudo_p
2636 && (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl))
2637 {
2638 rtx insn, scan;
2639
2640 push_topmost_sequence ();
2641
2642 scan = get_insns ();
2643 while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2644 scan = NEXT_INSN (scan);
2645
2646 insn = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2647 emit_insn_after (insn, scan);
2648
2649 pop_topmost_sequence ();
2650
2651 cfun->machine->initialized_mips16_gp_pseudo_p = true;
2652 }
2653
2654 return cfun->machine->mips16_gp_pseudo_rtx;
2655 }
2656
2657 /* Return a base register that holds pic_offset_table_rtx.
2658 TEMP, if nonnull, is a scratch Pmode base register. */
2659
2660 rtx
2661 mips_pic_base_register (rtx temp)
2662 {
2663 if (!TARGET_MIPS16)
2664 return pic_offset_table_rtx;
2665
2666 if (can_create_pseudo_p ())
2667 return mips16_gp_pseudo_reg ();
2668
2669 if (TARGET_USE_GOT)
2670 /* The first post-reload split exposes all references to $gp
2671 (both uses and definitions). All references must remain
2672 explicit after that point.
2673
2674 It is safe to introduce uses of $gp at any time, so for
2675 simplicity, we do that before the split too. */
2676 mips_emit_move (temp, pic_offset_table_rtx);
2677 else
2678 emit_insn (gen_load_const_gp (temp));
2679 return temp;
2680 }
2681
2682 /* Return the RHS of a load_call<mode> insn. */
2683
2684 static rtx
2685 mips_unspec_call (rtx reg, rtx symbol)
2686 {
2687 rtvec vec;
2688
2689 vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2690 return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2691 }
2692
2693 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2694 reference. Return NULL_RTX otherwise. */
2695
2696 static rtx
2697 mips_strip_unspec_call (rtx src)
2698 {
2699 if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
2700 return mips_strip_unspec_address (XVECEXP (src, 0, 1));
2701 return NULL_RTX;
2702 }
2703
2704 /* Create and return a GOT reference of type TYPE for address ADDR.
2705 TEMP, if nonnull, is a scratch Pmode base register. */
2706
2707 rtx
2708 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
2709 {
2710 rtx base, high, lo_sum_symbol;
2711
2712 base = mips_pic_base_register (temp);
2713
2714 /* If we used the temporary register to load $gp, we can't use
2715 it for the high part as well. */
2716 if (temp != NULL && reg_overlap_mentioned_p (base, temp))
2717 temp = NULL;
2718
2719 high = mips_unspec_offset_high (temp, base, addr, type);
2720 lo_sum_symbol = mips_unspec_address (addr, type);
2721
2722 if (type == SYMBOL_GOTOFF_CALL)
2723 return mips_unspec_call (high, lo_sum_symbol);
2724 else
2725 return (Pmode == SImode
2726 ? gen_unspec_gotsi (high, lo_sum_symbol)
2727 : gen_unspec_gotdi (high, lo_sum_symbol));
2728 }
2729
2730 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
2731 it appears in a MEM of that mode. Return true if ADDR is a legitimate
2732 constant in that context and can be split into high and low parts.
2733 If so, and if LOW_OUT is nonnull, emit the high part and store the
2734 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
2735
2736 TEMP is as for mips_force_temporary and is used to load the high
2737 part into a register.
2738
2739 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
2740 a legitimize SET_SRC for an .md pattern, otherwise the low part
2741 is guaranteed to be a legitimate address for mode MODE. */
2742
2743 bool
2744 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
2745 {
2746 enum mips_symbol_context context;
2747 enum mips_symbol_type symbol_type;
2748 rtx high;
2749
2750 context = (mode == MAX_MACHINE_MODE
2751 ? SYMBOL_CONTEXT_LEA
2752 : SYMBOL_CONTEXT_MEM);
2753 if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
2754 {
2755 addr = XEXP (addr, 0);
2756 if (mips_symbolic_constant_p (addr, context, &symbol_type)
2757 && mips_symbol_insns (symbol_type, mode) > 0
2758 && mips_split_hi_p[symbol_type])
2759 {
2760 if (low_out)
2761 switch (symbol_type)
2762 {
2763 case SYMBOL_GOT_PAGE_OFST:
2764 /* The high part of a page/ofst pair is loaded from the GOT. */
2765 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
2766 break;
2767
2768 default:
2769 gcc_unreachable ();
2770 }
2771 return true;
2772 }
2773 }
2774 else
2775 {
2776 if (mips_symbolic_constant_p (addr, context, &symbol_type)
2777 && mips_symbol_insns (symbol_type, mode) > 0
2778 && mips_split_p[symbol_type])
2779 {
2780 if (low_out)
2781 switch (symbol_type)
2782 {
2783 case SYMBOL_GOT_DISP:
2784 /* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
2785 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
2786 break;
2787
2788 case SYMBOL_GP_RELATIVE:
2789 high = mips_pic_base_register (temp);
2790 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2791 break;
2792
2793 default:
2794 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
2795 high = mips_force_temporary (temp, high);
2796 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2797 break;
2798 }
2799 return true;
2800 }
2801 }
2802 return false;
2803 }
2804
2805 /* Return a legitimate address for REG + OFFSET. TEMP is as for
2806 mips_force_temporary; it is only needed when OFFSET is not a
2807 SMALL_OPERAND. */
2808
2809 static rtx
2810 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2811 {
2812 if (!SMALL_OPERAND (offset))
2813 {
2814 rtx high;
2815
2816 if (TARGET_MIPS16)
2817 {
2818 /* Load the full offset into a register so that we can use
2819 an unextended instruction for the address itself. */
2820 high = GEN_INT (offset);
2821 offset = 0;
2822 }
2823 else
2824 {
2825 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
2826 The addition inside the macro CONST_HIGH_PART may cause an
2827 overflow, so we need to force a sign-extension check. */
2828 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
2829 offset = CONST_LOW_PART (offset);
2830 }
2831 high = mips_force_temporary (temp, high);
2832 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
2833 }
2834 return plus_constant (reg, offset);
2835 }
2836 \f
2837 /* The __tls_get_attr symbol. */
2838 static GTY(()) rtx mips_tls_symbol;
2839
2840 /* Return an instruction sequence that calls __tls_get_addr. SYM is
2841 the TLS symbol we are referencing and TYPE is the symbol type to use
2842 (either global dynamic or local dynamic). V0 is an RTX for the
2843 return value location. */
2844
2845 static rtx
2846 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
2847 {
2848 rtx insn, loc, a0;
2849
2850 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
2851
2852 if (!mips_tls_symbol)
2853 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
2854
2855 loc = mips_unspec_address (sym, type);
2856
2857 start_sequence ();
2858
2859 emit_insn (gen_rtx_SET (Pmode, a0,
2860 gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
2861 insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
2862 const0_rtx, NULL_RTX, false);
2863 RTL_CONST_CALL_P (insn) = 1;
2864 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
2865 insn = get_insns ();
2866
2867 end_sequence ();
2868
2869 return insn;
2870 }
2871
2872 /* Return a pseudo register that contains the current thread pointer. */
2873
2874 static rtx
2875 mips_get_tp (void)
2876 {
2877 rtx tp;
2878
2879 tp = gen_reg_rtx (Pmode);
2880 if (Pmode == DImode)
2881 emit_insn (gen_tls_get_tp_di (tp));
2882 else
2883 emit_insn (gen_tls_get_tp_si (tp));
2884 return tp;
2885 }
2886
2887 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
2888 its address. The return value will be both a valid address and a valid
2889 SET_SRC (either a REG or a LO_SUM). */
2890
2891 static rtx
2892 mips_legitimize_tls_address (rtx loc)
2893 {
2894 rtx dest, insn, v0, tp, tmp1, tmp2, eqv;
2895 enum tls_model model;
2896
2897 if (TARGET_MIPS16)
2898 {
2899 sorry ("MIPS16 TLS");
2900 return gen_reg_rtx (Pmode);
2901 }
2902
2903 model = SYMBOL_REF_TLS_MODEL (loc);
2904 /* Only TARGET_ABICALLS code can have more than one module; other
2905 code must be be static and should not use a GOT. All TLS models
2906 reduce to local exec in this situation. */
2907 if (!TARGET_ABICALLS)
2908 model = TLS_MODEL_LOCAL_EXEC;
2909
2910 switch (model)
2911 {
2912 case TLS_MODEL_GLOBAL_DYNAMIC:
2913 v0 = gen_rtx_REG (Pmode, GP_RETURN);
2914 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
2915 dest = gen_reg_rtx (Pmode);
2916 emit_libcall_block (insn, dest, v0, loc);
2917 break;
2918
2919 case TLS_MODEL_LOCAL_DYNAMIC:
2920 v0 = gen_rtx_REG (Pmode, GP_RETURN);
2921 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
2922 tmp1 = gen_reg_rtx (Pmode);
2923
2924 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2925 share the LDM result with other LD model accesses. */
2926 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2927 UNSPEC_TLS_LDM);
2928 emit_libcall_block (insn, tmp1, v0, eqv);
2929
2930 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
2931 dest = gen_rtx_LO_SUM (Pmode, tmp2,
2932 mips_unspec_address (loc, SYMBOL_DTPREL));
2933 break;
2934
2935 case TLS_MODEL_INITIAL_EXEC:
2936 tp = mips_get_tp ();
2937 tmp1 = gen_reg_rtx (Pmode);
2938 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
2939 if (Pmode == DImode)
2940 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
2941 else
2942 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
2943 dest = gen_reg_rtx (Pmode);
2944 emit_insn (gen_add3_insn (dest, tmp1, tp));
2945 break;
2946
2947 case TLS_MODEL_LOCAL_EXEC:
2948 tp = mips_get_tp ();
2949 tmp1 = mips_unspec_offset_high (NULL, tp, loc, SYMBOL_TPREL);
2950 dest = gen_rtx_LO_SUM (Pmode, tmp1,
2951 mips_unspec_address (loc, SYMBOL_TPREL));
2952 break;
2953
2954 default:
2955 gcc_unreachable ();
2956 }
2957 return dest;
2958 }
2959 \f
2960 /* If X is not a valid address for mode MODE, force it into a register. */
2961
2962 static rtx
2963 mips_force_address (rtx x, enum machine_mode mode)
2964 {
2965 if (!mips_legitimate_address_p (mode, x, false))
2966 x = force_reg (Pmode, x);
2967 return x;
2968 }
2969
2970 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
2971 be legitimized in a way that the generic machinery might not expect,
2972 return a new address, otherwise return NULL. MODE is the mode of
2973 the memory being accessed. */
2974
2975 static rtx
2976 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
2977 enum machine_mode mode)
2978 {
2979 rtx base, addr;
2980 HOST_WIDE_INT offset;
2981
2982 if (mips_tls_symbol_p (x))
2983 return mips_legitimize_tls_address (x);
2984
2985 /* See if the address can split into a high part and a LO_SUM. */
2986 if (mips_split_symbol (NULL, x, mode, &addr))
2987 return mips_force_address (addr, mode);
2988
2989 /* Handle BASE + OFFSET using mips_add_offset. */
2990 mips_split_plus (x, &base, &offset);
2991 if (offset != 0)
2992 {
2993 if (!mips_valid_base_register_p (base, mode, false))
2994 base = copy_to_mode_reg (Pmode, base);
2995 addr = mips_add_offset (NULL, base, offset);
2996 return mips_force_address (addr, mode);
2997 }
2998
2999 return x;
3000 }
3001
3002 /* Load VALUE into DEST. TEMP is as for mips_force_temporary. */
3003
3004 void
3005 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3006 {
3007 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3008 enum machine_mode mode;
3009 unsigned int i, num_ops;
3010 rtx x;
3011
3012 mode = GET_MODE (dest);
3013 num_ops = mips_build_integer (codes, value);
3014
3015 /* Apply each binary operation to X. Invariant: X is a legitimate
3016 source operand for a SET pattern. */
3017 x = GEN_INT (codes[0].value);
3018 for (i = 1; i < num_ops; i++)
3019 {
3020 if (!can_create_pseudo_p ())
3021 {
3022 emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3023 x = temp;
3024 }
3025 else
3026 x = force_reg (mode, x);
3027 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3028 }
3029
3030 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3031 }
3032
3033 /* Subroutine of mips_legitimize_move. Move constant SRC into register
3034 DEST given that SRC satisfies immediate_operand but doesn't satisfy
3035 move_operand. */
3036
3037 static void
3038 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
3039 {
3040 rtx base, offset;
3041
3042 /* Split moves of big integers into smaller pieces. */
3043 if (splittable_const_int_operand (src, mode))
3044 {
3045 mips_move_integer (dest, dest, INTVAL (src));
3046 return;
3047 }
3048
3049 /* Split moves of symbolic constants into high/low pairs. */
3050 if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3051 {
3052 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3053 return;
3054 }
3055
3056 /* Generate the appropriate access sequences for TLS symbols. */
3057 if (mips_tls_symbol_p (src))
3058 {
3059 mips_emit_move (dest, mips_legitimize_tls_address (src));
3060 return;
3061 }
3062
3063 /* If we have (const (plus symbol offset)), and that expression cannot
3064 be forced into memory, load the symbol first and add in the offset.
3065 In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3066 forced into memory, as it usually produces better code. */
3067 split_const (src, &base, &offset);
3068 if (offset != const0_rtx
3069 && (targetm.cannot_force_const_mem (src)
3070 || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3071 {
3072 base = mips_force_temporary (dest, base);
3073 mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3074 return;
3075 }
3076
3077 src = force_const_mem (mode, src);
3078
3079 /* When using explicit relocs, constant pool references are sometimes
3080 not legitimate addresses. */
3081 mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3082 mips_emit_move (dest, src);
3083 }
3084
3085 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3086 sequence that is valid. */
3087
3088 bool
3089 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
3090 {
3091 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3092 {
3093 mips_emit_move (dest, force_reg (mode, src));
3094 return true;
3095 }
3096
3097 /* We need to deal with constants that would be legitimate
3098 immediate_operands but aren't legitimate move_operands. */
3099 if (CONSTANT_P (src) && !move_operand (src, mode))
3100 {
3101 mips_legitimize_const_move (mode, dest, src);
3102 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3103 return true;
3104 }
3105 return false;
3106 }
3107 \f
3108 /* Return true if value X in context CONTEXT is a small-data address
3109 that can be rewritten as a LO_SUM. */
3110
3111 static bool
3112 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3113 {
3114 enum mips_symbol_type symbol_type;
3115
3116 return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3117 && !mips_split_p[SYMBOL_GP_RELATIVE]
3118 && mips_symbolic_constant_p (x, context, &symbol_type)
3119 && symbol_type == SYMBOL_GP_RELATIVE);
3120 }
3121
3122 /* A for_each_rtx callback for mips_small_data_pattern_p. DATA is the
3123 containing MEM, or null if none. */
3124
3125 static int
3126 mips_small_data_pattern_1 (rtx *loc, void *data)
3127 {
3128 enum mips_symbol_context context;
3129
3130 if (GET_CODE (*loc) == LO_SUM)
3131 return -1;
3132
3133 if (MEM_P (*loc))
3134 {
3135 if (for_each_rtx (&XEXP (*loc, 0), mips_small_data_pattern_1, *loc))
3136 return 1;
3137 return -1;
3138 }
3139
3140 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3141 return mips_rewrite_small_data_p (*loc, context);
3142 }
3143
3144 /* Return true if OP refers to small data symbols directly, not through
3145 a LO_SUM. */
3146
3147 bool
3148 mips_small_data_pattern_p (rtx op)
3149 {
3150 return for_each_rtx (&op, mips_small_data_pattern_1, NULL);
3151 }
3152
3153 /* A for_each_rtx callback, used by mips_rewrite_small_data.
3154 DATA is the containing MEM, or null if none. */
3155
3156 static int
3157 mips_rewrite_small_data_1 (rtx *loc, void *data)
3158 {
3159 enum mips_symbol_context context;
3160
3161 if (MEM_P (*loc))
3162 {
3163 for_each_rtx (&XEXP (*loc, 0), mips_rewrite_small_data_1, *loc);
3164 return -1;
3165 }
3166
3167 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3168 if (mips_rewrite_small_data_p (*loc, context))
3169 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3170
3171 if (GET_CODE (*loc) == LO_SUM)
3172 return -1;
3173
3174 return 0;
3175 }
3176
3177 /* Rewrite instruction pattern PATTERN so that it refers to small data
3178 using explicit relocations. */
3179
3180 rtx
3181 mips_rewrite_small_data (rtx pattern)
3182 {
3183 pattern = copy_insn (pattern);
3184 for_each_rtx (&pattern, mips_rewrite_small_data_1, NULL);
3185 return pattern;
3186 }
3187 \f
3188 /* We need a lot of little routines to check the range of MIPS16 immediate
3189 operands. */
3190
3191 static int
3192 m16_check_op (rtx op, int low, int high, int mask)
3193 {
3194 return (CONST_INT_P (op)
3195 && IN_RANGE (INTVAL (op), low, high)
3196 && (INTVAL (op) & mask) == 0);
3197 }
3198
3199 int
3200 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3201 {
3202 return m16_check_op (op, 0x1, 0x8, 0);
3203 }
3204
3205 int
3206 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3207 {
3208 return m16_check_op (op, -0x8, 0x7, 0);
3209 }
3210
3211 int
3212 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3213 {
3214 return m16_check_op (op, -0x7, 0x8, 0);
3215 }
3216
3217 int
3218 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3219 {
3220 return m16_check_op (op, -0x10, 0xf, 0);
3221 }
3222
3223 int
3224 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3225 {
3226 return m16_check_op (op, -0xf, 0x10, 0);
3227 }
3228
3229 int
3230 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3231 {
3232 return m16_check_op (op, -0x10 << 2, 0xf << 2, 3);
3233 }
3234
3235 int
3236 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3237 {
3238 return m16_check_op (op, -0xf << 2, 0x10 << 2, 3);
3239 }
3240
3241 int
3242 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3243 {
3244 return m16_check_op (op, -0x80, 0x7f, 0);
3245 }
3246
3247 int
3248 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3249 {
3250 return m16_check_op (op, -0x7f, 0x80, 0);
3251 }
3252
3253 int
3254 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3255 {
3256 return m16_check_op (op, 0x0, 0xff, 0);
3257 }
3258
3259 int
3260 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3261 {
3262 return m16_check_op (op, -0xff, 0x0, 0);
3263 }
3264
3265 int
3266 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3267 {
3268 return m16_check_op (op, -0x1, 0xfe, 0);
3269 }
3270
3271 int
3272 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3273 {
3274 return m16_check_op (op, 0x0, 0xff << 2, 3);
3275 }
3276
3277 int
3278 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3279 {
3280 return m16_check_op (op, -0xff << 2, 0x0, 3);
3281 }
3282
3283 int
3284 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3285 {
3286 return m16_check_op (op, -0x80 << 3, 0x7f << 3, 7);
3287 }
3288
3289 int
3290 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
3291 {
3292 return m16_check_op (op, -0x7f << 3, 0x80 << 3, 7);
3293 }
3294 \f
3295 /* The cost of loading values from the constant pool. It should be
3296 larger than the cost of any constant we want to synthesize inline. */
3297 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3298
3299 /* Return the cost of X when used as an operand to the MIPS16 instruction
3300 that implements CODE. Return -1 if there is no such instruction, or if
3301 X is not a valid immediate operand for it. */
3302
3303 static int
3304 mips16_constant_cost (int code, HOST_WIDE_INT x)
3305 {
3306 switch (code)
3307 {
3308 case ASHIFT:
3309 case ASHIFTRT:
3310 case LSHIFTRT:
3311 /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3312 other shifts are extended. The shift patterns truncate the shift
3313 count to the right size, so there are no out-of-range values. */
3314 if (IN_RANGE (x, 1, 8))
3315 return 0;
3316 return COSTS_N_INSNS (1);
3317
3318 case PLUS:
3319 if (IN_RANGE (x, -128, 127))
3320 return 0;
3321 if (SMALL_OPERAND (x))
3322 return COSTS_N_INSNS (1);
3323 return -1;
3324
3325 case LEU:
3326 /* Like LE, but reject the always-true case. */
3327 if (x == -1)
3328 return -1;
3329 case LE:
3330 /* We add 1 to the immediate and use SLT. */
3331 x += 1;
3332 case XOR:
3333 /* We can use CMPI for an xor with an unsigned 16-bit X. */
3334 case LT:
3335 case LTU:
3336 if (IN_RANGE (x, 0, 255))
3337 return 0;
3338 if (SMALL_OPERAND_UNSIGNED (x))
3339 return COSTS_N_INSNS (1);
3340 return -1;
3341
3342 case EQ:
3343 case NE:
3344 /* Equality comparisons with 0 are cheap. */
3345 if (x == 0)
3346 return 0;
3347 return -1;
3348
3349 default:
3350 return -1;
3351 }
3352 }
3353
3354 /* Return true if there is a non-MIPS16 instruction that implements CODE
3355 and if that instruction accepts X as an immediate operand. */
3356
3357 static int
3358 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3359 {
3360 switch (code)
3361 {
3362 case ASHIFT:
3363 case ASHIFTRT:
3364 case LSHIFTRT:
3365 /* All shift counts are truncated to a valid constant. */
3366 return true;
3367
3368 case ROTATE:
3369 case ROTATERT:
3370 /* Likewise rotates, if the target supports rotates at all. */
3371 return ISA_HAS_ROR;
3372
3373 case AND:
3374 case IOR:
3375 case XOR:
3376 /* These instructions take 16-bit unsigned immediates. */
3377 return SMALL_OPERAND_UNSIGNED (x);
3378
3379 case PLUS:
3380 case LT:
3381 case LTU:
3382 /* These instructions take 16-bit signed immediates. */
3383 return SMALL_OPERAND (x);
3384
3385 case EQ:
3386 case NE:
3387 case GT:
3388 case GTU:
3389 /* The "immediate" forms of these instructions are really
3390 implemented as comparisons with register 0. */
3391 return x == 0;
3392
3393 case GE:
3394 case GEU:
3395 /* Likewise, meaning that the only valid immediate operand is 1. */
3396 return x == 1;
3397
3398 case LE:
3399 /* We add 1 to the immediate and use SLT. */
3400 return SMALL_OPERAND (x + 1);
3401
3402 case LEU:
3403 /* Likewise SLTU, but reject the always-true case. */
3404 return SMALL_OPERAND (x + 1) && x + 1 != 0;
3405
3406 case SIGN_EXTRACT:
3407 case ZERO_EXTRACT:
3408 /* The bit position and size are immediate operands. */
3409 return ISA_HAS_EXT_INS;
3410
3411 default:
3412 /* By default assume that $0 can be used for 0. */
3413 return x == 0;
3414 }
3415 }
3416
3417 /* Return the cost of binary operation X, given that the instruction
3418 sequence for a word-sized or smaller operation has cost SINGLE_COST
3419 and that the sequence of a double-word operation has cost DOUBLE_COST.
3420 If SPEED is true, optimize for speed otherwise optimize for size. */
3421
3422 static int
3423 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3424 {
3425 int cost;
3426
3427 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3428 cost = double_cost;
3429 else
3430 cost = single_cost;
3431 return (cost
3432 + rtx_cost (XEXP (x, 0), SET, speed)
3433 + rtx_cost (XEXP (x, 1), GET_CODE (x), speed));
3434 }
3435
3436 /* Return the cost of floating-point multiplications of mode MODE. */
3437
3438 static int
3439 mips_fp_mult_cost (enum machine_mode mode)
3440 {
3441 return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3442 }
3443
3444 /* Return the cost of floating-point divisions of mode MODE. */
3445
3446 static int
3447 mips_fp_div_cost (enum machine_mode mode)
3448 {
3449 return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3450 }
3451
3452 /* Return the cost of sign-extending OP to mode MODE, not including the
3453 cost of OP itself. */
3454
3455 static int
3456 mips_sign_extend_cost (enum machine_mode mode, rtx op)
3457 {
3458 if (MEM_P (op))
3459 /* Extended loads are as cheap as unextended ones. */
3460 return 0;
3461
3462 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3463 /* A sign extension from SImode to DImode in 64-bit mode is free. */
3464 return 0;
3465
3466 if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3467 /* We can use SEB or SEH. */
3468 return COSTS_N_INSNS (1);
3469
3470 /* We need to use a shift left and a shift right. */
3471 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3472 }
3473
3474 /* Return the cost of zero-extending OP to mode MODE, not including the
3475 cost of OP itself. */
3476
3477 static int
3478 mips_zero_extend_cost (enum machine_mode mode, rtx op)
3479 {
3480 if (MEM_P (op))
3481 /* Extended loads are as cheap as unextended ones. */
3482 return 0;
3483
3484 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3485 /* We need a shift left by 32 bits and a shift right by 32 bits. */
3486 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3487
3488 if (GENERATE_MIPS16E)
3489 /* We can use ZEB or ZEH. */
3490 return COSTS_N_INSNS (1);
3491
3492 if (TARGET_MIPS16)
3493 /* We need to load 0xff or 0xffff into a register and use AND. */
3494 return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3495
3496 /* We can use ANDI. */
3497 return COSTS_N_INSNS (1);
3498 }
3499
3500 /* Implement TARGET_RTX_COSTS. */
3501
3502 static bool
3503 mips_rtx_costs (rtx x, int code, int outer_code, int *total, bool speed)
3504 {
3505 enum machine_mode mode = GET_MODE (x);
3506 bool float_mode_p = FLOAT_MODE_P (mode);
3507 int cost;
3508 rtx addr;
3509
3510 /* The cost of a COMPARE is hard to define for MIPS. COMPAREs don't
3511 appear in the instruction stream, and the cost of a comparison is
3512 really the cost of the branch or scc condition. At the time of
3513 writing, GCC only uses an explicit outer COMPARE code when optabs
3514 is testing whether a constant is expensive enough to force into a
3515 register. We want optabs to pass such constants through the MIPS
3516 expanders instead, so make all constants very cheap here. */
3517 if (outer_code == COMPARE)
3518 {
3519 gcc_assert (CONSTANT_P (x));
3520 *total = 0;
3521 return true;
3522 }
3523
3524 switch (code)
3525 {
3526 case CONST_INT:
3527 /* Treat *clear_upper32-style ANDs as having zero cost in the
3528 second operand. The cost is entirely in the first operand.
3529
3530 ??? This is needed because we would otherwise try to CSE
3531 the constant operand. Although that's the right thing for
3532 instructions that continue to be a register operation throughout
3533 compilation, it is disastrous for instructions that could
3534 later be converted into a memory operation. */
3535 if (TARGET_64BIT
3536 && outer_code == AND
3537 && UINTVAL (x) == 0xffffffff)
3538 {
3539 *total = 0;
3540 return true;
3541 }
3542
3543 if (TARGET_MIPS16)
3544 {
3545 cost = mips16_constant_cost (outer_code, INTVAL (x));
3546 if (cost >= 0)
3547 {
3548 *total = cost;
3549 return true;
3550 }
3551 }
3552 else
3553 {
3554 /* When not optimizing for size, we care more about the cost
3555 of hot code, and hot code is often in a loop. If a constant
3556 operand needs to be forced into a register, we will often be
3557 able to hoist the constant load out of the loop, so the load
3558 should not contribute to the cost. */
3559 if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3560 {
3561 *total = 0;
3562 return true;
3563 }
3564 }
3565 /* Fall through. */
3566
3567 case CONST:
3568 case SYMBOL_REF:
3569 case LABEL_REF:
3570 case CONST_DOUBLE:
3571 if (force_to_mem_operand (x, VOIDmode))
3572 {
3573 *total = COSTS_N_INSNS (1);
3574 return true;
3575 }
3576 cost = mips_const_insns (x);
3577 if (cost > 0)
3578 {
3579 /* If the constant is likely to be stored in a GPR, SETs of
3580 single-insn constants are as cheap as register sets; we
3581 never want to CSE them.
3582
3583 Don't reduce the cost of storing a floating-point zero in
3584 FPRs. If we have a zero in an FPR for other reasons, we
3585 can get better cfg-cleanup and delayed-branch results by
3586 using it consistently, rather than using $0 sometimes and
3587 an FPR at other times. Also, moves between floating-point
3588 registers are sometimes cheaper than (D)MTC1 $0. */
3589 if (cost == 1
3590 && outer_code == SET
3591 && !(float_mode_p && TARGET_HARD_FLOAT))
3592 cost = 0;
3593 /* When non-MIPS16 code loads a constant N>1 times, we rarely
3594 want to CSE the constant itself. It is usually better to
3595 have N copies of the last operation in the sequence and one
3596 shared copy of the other operations. (Note that this is
3597 not true for MIPS16 code, where the final operation in the
3598 sequence is often an extended instruction.)
3599
3600 Also, if we have a CONST_INT, we don't know whether it is
3601 for a word or doubleword operation, so we cannot rely on
3602 the result of mips_build_integer. */
3603 else if (!TARGET_MIPS16
3604 && (outer_code == SET || mode == VOIDmode))
3605 cost = 1;
3606 *total = COSTS_N_INSNS (cost);
3607 return true;
3608 }
3609 /* The value will need to be fetched from the constant pool. */
3610 *total = CONSTANT_POOL_COST;
3611 return true;
3612
3613 case MEM:
3614 /* If the address is legitimate, return the number of
3615 instructions it needs. */
3616 addr = XEXP (x, 0);
3617 cost = mips_address_insns (addr, mode, true);
3618 if (cost > 0)
3619 {
3620 *total = COSTS_N_INSNS (cost + 1);
3621 return true;
3622 }
3623 /* Check for a scaled indexed address. */
3624 if (mips_lwxs_address_p (addr))
3625 {
3626 *total = COSTS_N_INSNS (2);
3627 return true;
3628 }
3629 /* Otherwise use the default handling. */
3630 return false;
3631
3632 case FFS:
3633 *total = COSTS_N_INSNS (6);
3634 return false;
3635
3636 case NOT:
3637 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3638 return false;
3639
3640 case AND:
3641 /* Check for a *clear_upper32 pattern and treat it like a zero
3642 extension. See the pattern's comment for details. */
3643 if (TARGET_64BIT
3644 && mode == DImode
3645 && CONST_INT_P (XEXP (x, 1))
3646 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3647 {
3648 *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3649 + rtx_cost (XEXP (x, 0), SET, speed));
3650 return true;
3651 }
3652 /* Fall through. */
3653
3654 case IOR:
3655 case XOR:
3656 /* Double-word operations use two single-word operations. */
3657 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3658 speed);
3659 return true;
3660
3661 case ASHIFT:
3662 case ASHIFTRT:
3663 case LSHIFTRT:
3664 case ROTATE:
3665 case ROTATERT:
3666 if (CONSTANT_P (XEXP (x, 1)))
3667 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3668 speed);
3669 else
3670 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
3671 speed);
3672 return true;
3673
3674 case ABS:
3675 if (float_mode_p)
3676 *total = mips_cost->fp_add;
3677 else
3678 *total = COSTS_N_INSNS (4);
3679 return false;
3680
3681 case LO_SUM:
3682 /* Low-part immediates need an extended MIPS16 instruction. */
3683 *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3684 + rtx_cost (XEXP (x, 0), SET, speed));
3685 return true;
3686
3687 case LT:
3688 case LTU:
3689 case LE:
3690 case LEU:
3691 case GT:
3692 case GTU:
3693 case GE:
3694 case GEU:
3695 case EQ:
3696 case NE:
3697 case UNORDERED:
3698 case LTGT:
3699 /* Branch comparisons have VOIDmode, so use the first operand's
3700 mode instead. */
3701 mode = GET_MODE (XEXP (x, 0));
3702 if (FLOAT_MODE_P (mode))
3703 {
3704 *total = mips_cost->fp_add;
3705 return false;
3706 }
3707 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3708 speed);
3709 return true;
3710
3711 case MINUS:
3712 if (float_mode_p
3713 && (ISA_HAS_NMADD4_NMSUB4 (mode) || ISA_HAS_NMADD3_NMSUB3 (mode))
3714 && TARGET_FUSED_MADD
3715 && !HONOR_NANS (mode)
3716 && !HONOR_SIGNED_ZEROS (mode))
3717 {
3718 /* See if we can use NMADD or NMSUB. See mips.md for the
3719 associated patterns. */
3720 rtx op0 = XEXP (x, 0);
3721 rtx op1 = XEXP (x, 1);
3722 if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
3723 {
3724 *total = (mips_fp_mult_cost (mode)
3725 + rtx_cost (XEXP (XEXP (op0, 0), 0), SET, speed)
3726 + rtx_cost (XEXP (op0, 1), SET, speed)
3727 + rtx_cost (op1, SET, speed));
3728 return true;
3729 }
3730 if (GET_CODE (op1) == MULT)
3731 {
3732 *total = (mips_fp_mult_cost (mode)
3733 + rtx_cost (op0, SET, speed)
3734 + rtx_cost (XEXP (op1, 0), SET, speed)
3735 + rtx_cost (XEXP (op1, 1), SET, speed));
3736 return true;
3737 }
3738 }
3739 /* Fall through. */
3740
3741 case PLUS:
3742 if (float_mode_p)
3743 {
3744 /* If this is part of a MADD or MSUB, treat the PLUS as
3745 being free. */
3746 if (ISA_HAS_FP4
3747 && TARGET_FUSED_MADD
3748 && GET_CODE (XEXP (x, 0)) == MULT)
3749 *total = 0;
3750 else
3751 *total = mips_cost->fp_add;
3752 return false;
3753 }
3754
3755 /* Double-word operations require three single-word operations and
3756 an SLTU. The MIPS16 version then needs to move the result of
3757 the SLTU from $24 to a MIPS16 register. */
3758 *total = mips_binary_cost (x, COSTS_N_INSNS (1),
3759 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
3760 speed);
3761 return true;
3762
3763 case NEG:
3764 if (float_mode_p
3765 && (ISA_HAS_NMADD4_NMSUB4 (mode) || ISA_HAS_NMADD3_NMSUB3 (mode))
3766 && TARGET_FUSED_MADD
3767 && !HONOR_NANS (mode)
3768 && HONOR_SIGNED_ZEROS (mode))
3769 {
3770 /* See if we can use NMADD or NMSUB. See mips.md for the
3771 associated patterns. */
3772 rtx op = XEXP (x, 0);
3773 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3774 && GET_CODE (XEXP (op, 0)) == MULT)
3775 {
3776 *total = (mips_fp_mult_cost (mode)
3777 + rtx_cost (XEXP (XEXP (op, 0), 0), SET, speed)
3778 + rtx_cost (XEXP (XEXP (op, 0), 1), SET, speed)
3779 + rtx_cost (XEXP (op, 1), SET, speed));
3780 return true;
3781 }
3782 }
3783
3784 if (float_mode_p)
3785 *total = mips_cost->fp_add;
3786 else
3787 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
3788 return false;
3789
3790 case MULT:
3791 if (float_mode_p)
3792 *total = mips_fp_mult_cost (mode);
3793 else if (mode == DImode && !TARGET_64BIT)
3794 /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
3795 where the mulsidi3 always includes an MFHI and an MFLO. */
3796 *total = (speed
3797 ? mips_cost->int_mult_si * 3 + 6
3798 : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
3799 else if (!speed)
3800 *total = (ISA_HAS_MUL3 ? 1 : 2);
3801 else if (mode == DImode)
3802 *total = mips_cost->int_mult_di;
3803 else
3804 *total = mips_cost->int_mult_si;
3805 return false;
3806
3807 case DIV:
3808 /* Check for a reciprocal. */
3809 if (float_mode_p
3810 && ISA_HAS_FP4
3811 && flag_unsafe_math_optimizations
3812 && XEXP (x, 0) == CONST1_RTX (mode))
3813 {
3814 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
3815 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
3816 division as being free. */
3817 *total = rtx_cost (XEXP (x, 1), SET, speed);
3818 else
3819 *total = (mips_fp_div_cost (mode)
3820 + rtx_cost (XEXP (x, 1), SET, speed));
3821 return true;
3822 }
3823 /* Fall through. */
3824
3825 case SQRT:
3826 case MOD:
3827 if (float_mode_p)
3828 {
3829 *total = mips_fp_div_cost (mode);
3830 return false;
3831 }
3832 /* Fall through. */
3833
3834 case UDIV:
3835 case UMOD:
3836 if (!speed)
3837 {
3838 /* It is our responsibility to make division by a power of 2
3839 as cheap as 2 register additions if we want the division
3840 expanders to be used for such operations; see the setting
3841 of sdiv_pow2_cheap in optabs.c. Using (D)DIV for MIPS16
3842 should always produce shorter code than using
3843 expand_sdiv2_pow2. */
3844 if (TARGET_MIPS16
3845 && CONST_INT_P (XEXP (x, 1))
3846 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
3847 {
3848 *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), SET, speed);
3849 return true;
3850 }
3851 *total = COSTS_N_INSNS (mips_idiv_insns ());
3852 }
3853 else if (mode == DImode)
3854 *total = mips_cost->int_div_di;
3855 else
3856 *total = mips_cost->int_div_si;
3857 return false;
3858
3859 case SIGN_EXTEND:
3860 *total = mips_sign_extend_cost (mode, XEXP (x, 0));
3861 return false;
3862
3863 case ZERO_EXTEND:
3864 *total = mips_zero_extend_cost (mode, XEXP (x, 0));
3865 return false;
3866
3867 case FLOAT:
3868 case UNSIGNED_FLOAT:
3869 case FIX:
3870 case FLOAT_EXTEND:
3871 case FLOAT_TRUNCATE:
3872 *total = mips_cost->fp_add;
3873 return false;
3874
3875 default:
3876 return false;
3877 }
3878 }
3879
3880 /* Implement TARGET_ADDRESS_COST. */
3881
3882 static int
3883 mips_address_cost (rtx addr, bool speed ATTRIBUTE_UNUSED)
3884 {
3885 return mips_address_insns (addr, SImode, false);
3886 }
3887 \f
3888 /* Information about a single instruction in a multi-instruction
3889 asm sequence. */
3890 struct mips_multi_member {
3891 /* True if this is a label, false if it is code. */
3892 bool is_label_p;
3893
3894 /* The output_asm_insn format of the instruction. */
3895 const char *format;
3896
3897 /* The operands to the instruction. */
3898 rtx operands[MAX_RECOG_OPERANDS];
3899 };
3900 typedef struct mips_multi_member mips_multi_member;
3901
3902 /* Vector definitions for the above. */
3903 DEF_VEC_O(mips_multi_member);
3904 DEF_VEC_ALLOC_O(mips_multi_member, heap);
3905
3906 /* The instructions that make up the current multi-insn sequence. */
3907 static VEC (mips_multi_member, heap) *mips_multi_members;
3908
3909 /* How many instructions (as opposed to labels) are in the current
3910 multi-insn sequence. */
3911 static unsigned int mips_multi_num_insns;
3912
3913 /* Start a new multi-insn sequence. */
3914
3915 static void
3916 mips_multi_start (void)
3917 {
3918 VEC_truncate (mips_multi_member, mips_multi_members, 0);
3919 mips_multi_num_insns = 0;
3920 }
3921
3922 /* Add a new, uninitialized member to the current multi-insn sequence. */
3923
3924 static struct mips_multi_member *
3925 mips_multi_add (void)
3926 {
3927 return VEC_safe_push (mips_multi_member, heap, mips_multi_members, 0);
3928 }
3929
3930 /* Add a normal insn with the given asm format to the current multi-insn
3931 sequence. The other arguments are a null-terminated list of operands. */
3932
3933 static void
3934 mips_multi_add_insn (const char *format, ...)
3935 {
3936 struct mips_multi_member *member;
3937 va_list ap;
3938 unsigned int i;
3939 rtx op;
3940
3941 member = mips_multi_add ();
3942 member->is_label_p = false;
3943 member->format = format;
3944 va_start (ap, format);
3945 i = 0;
3946 while ((op = va_arg (ap, rtx)))
3947 member->operands[i++] = op;
3948 va_end (ap);
3949 mips_multi_num_insns++;
3950 }
3951
3952 /* Add the given label definition to the current multi-insn sequence.
3953 The definition should include the colon. */
3954
3955 static void
3956 mips_multi_add_label (const char *label)
3957 {
3958 struct mips_multi_member *member;
3959
3960 member = mips_multi_add ();
3961 member->is_label_p = true;
3962 member->format = label;
3963 }
3964
3965 /* Return the index of the last member of the current multi-insn sequence. */
3966
3967 static unsigned int
3968 mips_multi_last_index (void)
3969 {
3970 return VEC_length (mips_multi_member, mips_multi_members) - 1;
3971 }
3972
3973 /* Add a copy of an existing instruction to the current multi-insn
3974 sequence. I is the index of the instruction that should be copied. */
3975
3976 static void
3977 mips_multi_copy_insn (unsigned int i)
3978 {
3979 struct mips_multi_member *member;
3980
3981 member = mips_multi_add ();
3982 memcpy (member, VEC_index (mips_multi_member, mips_multi_members, i),
3983 sizeof (*member));
3984 gcc_assert (!member->is_label_p);
3985 }
3986
3987 /* Change the operand of an existing instruction in the current
3988 multi-insn sequence. I is the index of the instruction,
3989 OP is the index of the operand, and X is the new value. */
3990
3991 static void
3992 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
3993 {
3994 VEC_index (mips_multi_member, mips_multi_members, i)->operands[op] = x;
3995 }
3996
3997 /* Write out the asm code for the current multi-insn sequence. */
3998
3999 static void
4000 mips_multi_write (void)
4001 {
4002 struct mips_multi_member *member;
4003 unsigned int i;
4004
4005 for (i = 0;
4006 VEC_iterate (mips_multi_member, mips_multi_members, i, member);
4007 i++)
4008 if (member->is_label_p)
4009 fprintf (asm_out_file, "%s\n", member->format);
4010 else
4011 output_asm_insn (member->format, member->operands);
4012 }
4013 \f
4014 /* Return one word of double-word value OP, taking into account the fixed
4015 endianness of certain registers. HIGH_P is true to select the high part,
4016 false to select the low part. */
4017
4018 rtx
4019 mips_subword (rtx op, bool high_p)
4020 {
4021 unsigned int byte, offset;
4022 enum machine_mode mode;
4023
4024 mode = GET_MODE (op);
4025 if (mode == VOIDmode)
4026 mode = TARGET_64BIT ? TImode : DImode;
4027
4028 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4029 byte = UNITS_PER_WORD;
4030 else
4031 byte = 0;
4032
4033 if (FP_REG_RTX_P (op))
4034 {
4035 /* Paired FPRs are always ordered little-endian. */
4036 offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4037 return gen_rtx_REG (word_mode, REGNO (op) + offset);
4038 }
4039
4040 if (MEM_P (op))
4041 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4042
4043 return simplify_gen_subreg (word_mode, op, mode, byte);
4044 }
4045
4046 /* Return true if a 64-bit move from SRC to DEST should be split into two. */
4047
4048 bool
4049 mips_split_64bit_move_p (rtx dest, rtx src)
4050 {
4051 if (TARGET_64BIT)
4052 return false;
4053
4054 /* FPR-to-FPR moves can be done in a single instruction, if they're
4055 allowed at all. */
4056 if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4057 return false;
4058
4059 /* Check for floating-point loads and stores. */
4060 if (ISA_HAS_LDC1_SDC1)
4061 {
4062 if (FP_REG_RTX_P (dest) && MEM_P (src))
4063 return false;
4064 if (FP_REG_RTX_P (src) && MEM_P (dest))
4065 return false;
4066 }
4067 return true;
4068 }
4069
4070 /* Split a doubleword move from SRC to DEST. On 32-bit targets,
4071 this function handles 64-bit moves for which mips_split_64bit_move_p
4072 holds. For 64-bit targets, this function handles 128-bit moves. */
4073
4074 void
4075 mips_split_doubleword_move (rtx dest, rtx src)
4076 {
4077 rtx low_dest;
4078
4079 if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4080 {
4081 if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4082 emit_insn (gen_move_doubleword_fprdi (dest, src));
4083 else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4084 emit_insn (gen_move_doubleword_fprdf (dest, src));
4085 else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4086 emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4087 else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4088 emit_insn (gen_move_doubleword_fprv2si (dest, src));
4089 else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4090 emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4091 else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4092 emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4093 else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4094 emit_insn (gen_move_doubleword_fprtf (dest, src));
4095 else
4096 gcc_unreachable ();
4097 }
4098 else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4099 {
4100 low_dest = mips_subword (dest, false);
4101 mips_emit_move (low_dest, mips_subword (src, false));
4102 if (TARGET_64BIT)
4103 emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4104 else
4105 emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4106 }
4107 else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4108 {
4109 mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4110 if (TARGET_64BIT)
4111 emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4112 else
4113 emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4114 }
4115 else
4116 {
4117 /* The operation can be split into two normal moves. Decide in
4118 which order to do them. */
4119 low_dest = mips_subword (dest, false);
4120 if (REG_P (low_dest)
4121 && reg_overlap_mentioned_p (low_dest, src))
4122 {
4123 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4124 mips_emit_move (low_dest, mips_subword (src, false));
4125 }
4126 else
4127 {
4128 mips_emit_move (low_dest, mips_subword (src, false));
4129 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4130 }
4131 }
4132 }
4133 \f
4134 /* Return the appropriate instructions to move SRC into DEST. Assume
4135 that SRC is operand 1 and DEST is operand 0. */
4136
4137 const char *
4138 mips_output_move (rtx dest, rtx src)
4139 {
4140 enum rtx_code dest_code, src_code;
4141 enum machine_mode mode;
4142 enum mips_symbol_type symbol_type;
4143 bool dbl_p;
4144
4145 dest_code = GET_CODE (dest);
4146 src_code = GET_CODE (src);
4147 mode = GET_MODE (dest);
4148 dbl_p = (GET_MODE_SIZE (mode) == 8);
4149
4150 if (dbl_p && mips_split_64bit_move_p (dest, src))
4151 return "#";
4152
4153 if ((src_code == REG && GP_REG_P (REGNO (src)))
4154 || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4155 {
4156 if (dest_code == REG)
4157 {
4158 if (GP_REG_P (REGNO (dest)))
4159 return "move\t%0,%z1";
4160
4161 /* Moves to HI are handled by special .md insns. */
4162 if (REGNO (dest) == LO_REGNUM)
4163 return "mtlo\t%z1";
4164
4165 if (DSP_ACC_REG_P (REGNO (dest)))
4166 {
4167 static char retval[] = "mt__\t%z1,%q0";
4168
4169 retval[2] = reg_names[REGNO (dest)][4];
4170 retval[3] = reg_names[REGNO (dest)][5];
4171 return retval;
4172 }
4173
4174 if (FP_REG_P (REGNO (dest)))
4175 return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4176
4177 if (ALL_COP_REG_P (REGNO (dest)))
4178 {
4179 static char retval[] = "dmtc_\t%z1,%0";
4180
4181 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4182 return dbl_p ? retval : retval + 1;
4183 }
4184 }
4185 if (dest_code == MEM)
4186 switch (GET_MODE_SIZE (mode))
4187 {
4188 case 1: return "sb\t%z1,%0";
4189 case 2: return "sh\t%z1,%0";
4190 case 4: return "sw\t%z1,%0";
4191 case 8: return "sd\t%z1,%0";
4192 }
4193 }
4194 if (dest_code == REG && GP_REG_P (REGNO (dest)))
4195 {
4196 if (src_code == REG)
4197 {
4198 /* Moves from HI are handled by special .md insns. */
4199 if (REGNO (src) == LO_REGNUM)
4200 {
4201 /* When generating VR4120 or VR4130 code, we use MACC and
4202 DMACC instead of MFLO. This avoids both the normal
4203 MIPS III HI/LO hazards and the errata related to
4204 -mfix-vr4130. */
4205 if (ISA_HAS_MACCHI)
4206 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4207 return "mflo\t%0";
4208 }
4209
4210 if (DSP_ACC_REG_P (REGNO (src)))
4211 {
4212 static char retval[] = "mf__\t%0,%q1";
4213
4214 retval[2] = reg_names[REGNO (src)][4];
4215 retval[3] = reg_names[REGNO (src)][5];
4216 return retval;
4217 }
4218
4219 if (FP_REG_P (REGNO (src)))
4220 return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4221
4222 if (ALL_COP_REG_P (REGNO (src)))
4223 {
4224 static char retval[] = "dmfc_\t%0,%1";
4225
4226 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4227 return dbl_p ? retval : retval + 1;
4228 }
4229
4230 if (ST_REG_P (REGNO (src)) && ISA_HAS_8CC)
4231 return "lui\t%0,0x3f80\n\tmovf\t%0,%.,%1";
4232 }
4233
4234 if (src_code == MEM)
4235 switch (GET_MODE_SIZE (mode))
4236 {
4237 case 1: return "lbu\t%0,%1";
4238 case 2: return "lhu\t%0,%1";
4239 case 4: return "lw\t%0,%1";
4240 case 8: return "ld\t%0,%1";
4241 }
4242
4243 if (src_code == CONST_INT)
4244 {
4245 /* Don't use the X format for the operand itself, because that
4246 will give out-of-range numbers for 64-bit hosts and 32-bit
4247 targets. */
4248 if (!TARGET_MIPS16)
4249 return "li\t%0,%1\t\t\t# %X1";
4250
4251 if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4252 return "li\t%0,%1";
4253
4254 if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4255 return "#";
4256 }
4257
4258 if (src_code == HIGH)
4259 return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4260
4261 if (CONST_GP_P (src))
4262 return "move\t%0,%1";
4263
4264 if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4265 && mips_lo_relocs[symbol_type] != 0)
4266 {
4267 /* A signed 16-bit constant formed by applying a relocation
4268 operator to a symbolic address. */
4269 gcc_assert (!mips_split_p[symbol_type]);
4270 return "li\t%0,%R1";
4271 }
4272
4273 if (symbolic_operand (src, VOIDmode))
4274 {
4275 gcc_assert (TARGET_MIPS16
4276 ? TARGET_MIPS16_TEXT_LOADS
4277 : !TARGET_EXPLICIT_RELOCS);
4278 return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4279 }
4280 }
4281 if (src_code == REG && FP_REG_P (REGNO (src)))
4282 {
4283 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4284 {
4285 if (GET_MODE (dest) == V2SFmode)
4286 return "mov.ps\t%0,%1";
4287 else
4288 return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4289 }
4290
4291 if (dest_code == MEM)
4292 return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4293 }
4294 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4295 {
4296 if (src_code == MEM)
4297 return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4298 }
4299 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4300 {
4301 static char retval[] = "l_c_\t%0,%1";
4302
4303 retval[1] = (dbl_p ? 'd' : 'w');
4304 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4305 return retval;
4306 }
4307 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4308 {
4309 static char retval[] = "s_c_\t%1,%0";
4310
4311 retval[1] = (dbl_p ? 'd' : 'w');
4312 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4313 return retval;
4314 }
4315 gcc_unreachable ();
4316 }
4317 \f
4318 /* Return true if CMP1 is a suitable second operand for integer ordering
4319 test CODE. See also the *sCC patterns in mips.md. */
4320
4321 static bool
4322 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4323 {
4324 switch (code)
4325 {
4326 case GT:
4327 case GTU:
4328 return reg_or_0_operand (cmp1, VOIDmode);
4329
4330 case GE:
4331 case GEU:
4332 return !TARGET_MIPS16 && cmp1 == const1_rtx;
4333
4334 case LT:
4335 case LTU:
4336 return arith_operand (cmp1, VOIDmode);
4337
4338 case LE:
4339 return sle_operand (cmp1, VOIDmode);
4340
4341 case LEU:
4342 return sleu_operand (cmp1, VOIDmode);
4343
4344 default:
4345 gcc_unreachable ();
4346 }
4347 }
4348
4349 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4350 integer ordering test *CODE, or if an equivalent combination can
4351 be formed by adjusting *CODE and *CMP1. When returning true, update
4352 *CODE and *CMP1 with the chosen code and operand, otherwise leave
4353 them alone. */
4354
4355 static bool
4356 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4357 enum machine_mode mode)
4358 {
4359 HOST_WIDE_INT plus_one;
4360
4361 if (mips_int_order_operand_ok_p (*code, *cmp1))
4362 return true;
4363
4364 if (CONST_INT_P (*cmp1))
4365 switch (*code)
4366 {
4367 case LE:
4368 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4369 if (INTVAL (*cmp1) < plus_one)
4370 {
4371 *code = LT;
4372 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4373 return true;
4374 }
4375 break;
4376
4377 case LEU:
4378 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4379 if (plus_one != 0)
4380 {
4381 *code = LTU;
4382 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4383 return true;
4384 }
4385 break;
4386
4387 default:
4388 break;
4389 }
4390 return false;
4391 }
4392
4393 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4394 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
4395 is nonnull, it's OK to set TARGET to the inverse of the result and
4396 flip *INVERT_PTR instead. */
4397
4398 static void
4399 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4400 rtx target, rtx cmp0, rtx cmp1)
4401 {
4402 enum machine_mode mode;
4403
4404 /* First see if there is a MIPS instruction that can do this operation.
4405 If not, try doing the same for the inverse operation. If that also
4406 fails, force CMP1 into a register and try again. */
4407 mode = GET_MODE (cmp0);
4408 if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4409 mips_emit_binary (code, target, cmp0, cmp1);
4410 else
4411 {
4412 enum rtx_code inv_code = reverse_condition (code);
4413 if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4414 {
4415 cmp1 = force_reg (mode, cmp1);
4416 mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4417 }
4418 else if (invert_ptr == 0)
4419 {
4420 rtx inv_target;
4421
4422 inv_target = mips_force_binary (GET_MODE (target),
4423 inv_code, cmp0, cmp1);
4424 mips_emit_binary (XOR, target, inv_target, const1_rtx);
4425 }
4426 else
4427 {
4428 *invert_ptr = !*invert_ptr;
4429 mips_emit_binary (inv_code, target, cmp0, cmp1);
4430 }
4431 }
4432 }
4433
4434 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4435 The register will have the same mode as CMP0. */
4436
4437 static rtx
4438 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4439 {
4440 if (cmp1 == const0_rtx)
4441 return cmp0;
4442
4443 if (uns_arith_operand (cmp1, VOIDmode))
4444 return expand_binop (GET_MODE (cmp0), xor_optab,
4445 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4446
4447 return expand_binop (GET_MODE (cmp0), sub_optab,
4448 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4449 }
4450
4451 /* Convert *CODE into a code that can be used in a floating-point
4452 scc instruction (C.cond.fmt). Return true if the values of
4453 the condition code registers will be inverted, with 0 indicating
4454 that the condition holds. */
4455
4456 static bool
4457 mips_reversed_fp_cond (enum rtx_code *code)
4458 {
4459 switch (*code)
4460 {
4461 case NE:
4462 case LTGT:
4463 case ORDERED:
4464 *code = reverse_condition_maybe_unordered (*code);
4465 return true;
4466
4467 default:
4468 return false;
4469 }
4470 }
4471
4472 /* Convert a comparison into something that can be used in a branch or
4473 conditional move. On entry, *OP0 and *OP1 are the values being
4474 compared and *CODE is the code used to compare them.
4475
4476 Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
4477 If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
4478 otherwise any standard branch condition can be used. The standard branch
4479 conditions are:
4480
4481 - EQ or NE between two registers.
4482 - any comparison between a register and zero. */
4483
4484 static void
4485 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
4486 {
4487 rtx cmp_op0 = *op0;
4488 rtx cmp_op1 = *op1;
4489
4490 if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
4491 {
4492 if (!need_eq_ne_p && *op1 == const0_rtx)
4493 ;
4494 else if (*code == EQ || *code == NE)
4495 {
4496 if (need_eq_ne_p)
4497 {
4498 *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
4499 *op1 = const0_rtx;
4500 }
4501 else
4502 *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
4503 }
4504 else
4505 {
4506 /* The comparison needs a separate scc instruction. Store the
4507 result of the scc in *OP0 and compare it against zero. */
4508 bool invert = false;
4509 *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
4510 mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
4511 *code = (invert ? EQ : NE);
4512 *op1 = const0_rtx;
4513 }
4514 }
4515 else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
4516 {
4517 *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
4518 mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
4519 *code = NE;
4520 *op1 = const0_rtx;
4521 }
4522 else
4523 {
4524 enum rtx_code cmp_code;
4525
4526 /* Floating-point tests use a separate C.cond.fmt comparison to
4527 set a condition code register. The branch or conditional move
4528 will then compare that register against zero.
4529
4530 Set CMP_CODE to the code of the comparison instruction and
4531 *CODE to the code that the branch or move should use. */
4532 cmp_code = *code;
4533 *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
4534 *op0 = (ISA_HAS_8CC
4535 ? gen_reg_rtx (CCmode)
4536 : gen_rtx_REG (CCmode, FPSW_REGNUM));
4537 *op1 = const0_rtx;
4538 mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
4539 }
4540 }
4541 \f
4542 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
4543 and OPERAND[3]. Store the result in OPERANDS[0].
4544
4545 On 64-bit targets, the mode of the comparison and target will always be
4546 SImode, thus possibly narrower than that of the comparison's operands. */
4547
4548 void
4549 mips_expand_scc (rtx operands[])
4550 {
4551 rtx target = operands[0];
4552 enum rtx_code code = GET_CODE (operands[1]);
4553 rtx op0 = operands[2];
4554 rtx op1 = operands[3];
4555
4556 gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
4557
4558 if (code == EQ || code == NE)
4559 {
4560 if (ISA_HAS_SEQ_SNE
4561 && reg_imm10_operand (op1, GET_MODE (op1)))
4562 mips_emit_binary (code, target, op0, op1);
4563 else
4564 {
4565 rtx zie = mips_zero_if_equal (op0, op1);
4566 mips_emit_binary (code, target, zie, const0_rtx);
4567 }
4568 }
4569 else
4570 mips_emit_int_order_test (code, 0, target, op0, op1);
4571 }
4572
4573 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
4574 CODE and jump to OPERANDS[3] if the condition holds. */
4575
4576 void
4577 mips_expand_conditional_branch (rtx *operands)
4578 {
4579 enum rtx_code code = GET_CODE (operands[0]);
4580 rtx op0 = operands[1];
4581 rtx op1 = operands[2];
4582 rtx condition;
4583
4584 mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
4585 condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
4586 emit_jump_insn (gen_condjump (condition, operands[3]));
4587 }
4588
4589 /* Implement:
4590
4591 (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
4592 (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS)) */
4593
4594 void
4595 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
4596 enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
4597 {
4598 rtx cmp_result;
4599 bool reversed_p;
4600
4601 reversed_p = mips_reversed_fp_cond (&cond);
4602 cmp_result = gen_reg_rtx (CCV2mode);
4603 emit_insn (gen_scc_ps (cmp_result,
4604 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
4605 if (reversed_p)
4606 emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
4607 cmp_result));
4608 else
4609 emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
4610 cmp_result));
4611 }
4612
4613 /* Perform the comparison in OPERANDS[1]. Move OPERANDS[2] into OPERANDS[0]
4614 if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0]. */
4615
4616 void
4617 mips_expand_conditional_move (rtx *operands)
4618 {
4619 rtx cond;
4620 enum rtx_code code = GET_CODE (operands[1]);
4621 rtx op0 = XEXP (operands[1], 0);
4622 rtx op1 = XEXP (operands[1], 1);
4623
4624 mips_emit_compare (&code, &op0, &op1, true);
4625 cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
4626 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
4627 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
4628 operands[2], operands[3])));
4629 }
4630
4631 /* Perform the comparison in COMPARISON, then trap if the condition holds. */
4632
4633 void
4634 mips_expand_conditional_trap (rtx comparison)
4635 {
4636 rtx op0, op1;
4637 enum machine_mode mode;
4638 enum rtx_code code;
4639
4640 /* MIPS conditional trap instructions don't have GT or LE flavors,
4641 so we must swap the operands and convert to LT and GE respectively. */
4642 code = GET_CODE (comparison);
4643 switch (code)
4644 {
4645 case GT:
4646 case LE:
4647 case GTU:
4648 case LEU:
4649 code = swap_condition (code);
4650 op0 = XEXP (comparison, 1);
4651 op1 = XEXP (comparison, 0);
4652 break;
4653
4654 default:
4655 op0 = XEXP (comparison, 0);
4656 op1 = XEXP (comparison, 1);
4657 break;
4658 }
4659
4660 mode = GET_MODE (XEXP (comparison, 0));
4661 op0 = force_reg (mode, op0);
4662 if (!arith_operand (op1, mode))
4663 op1 = force_reg (mode, op1);
4664
4665 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
4666 gen_rtx_fmt_ee (code, mode, op0, op1),
4667 const0_rtx));
4668 }
4669 \f
4670 /* Initialize *CUM for a call to a function of type FNTYPE. */
4671
4672 void
4673 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
4674 {
4675 memset (cum, 0, sizeof (*cum));
4676 cum->prototype = (fntype && prototype_p (fntype));
4677 cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
4678 }
4679
4680 /* Fill INFO with information about a single argument. CUM is the
4681 cumulative state for earlier arguments. MODE is the mode of this
4682 argument and TYPE is its type (if known). NAMED is true if this
4683 is a named (fixed) argument rather than a variable one. */
4684
4685 static void
4686 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
4687 enum machine_mode mode, tree type, int named)
4688 {
4689 bool doubleword_aligned_p;
4690 unsigned int num_bytes, num_words, max_regs;
4691
4692 /* Work out the size of the argument. */
4693 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
4694 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4695
4696 /* Decide whether it should go in a floating-point register, assuming
4697 one is free. Later code checks for availability.
4698
4699 The checks against UNITS_PER_FPVALUE handle the soft-float and
4700 single-float cases. */
4701 switch (mips_abi)
4702 {
4703 case ABI_EABI:
4704 /* The EABI conventions have traditionally been defined in terms
4705 of TYPE_MODE, regardless of the actual type. */
4706 info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
4707 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
4708 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
4709 break;
4710
4711 case ABI_32:
4712 case ABI_O64:
4713 /* Only leading floating-point scalars are passed in
4714 floating-point registers. We also handle vector floats the same
4715 say, which is OK because they are not covered by the standard ABI. */
4716 info->fpr_p = (!cum->gp_reg_found
4717 && cum->arg_number < 2
4718 && (type == 0
4719 || SCALAR_FLOAT_TYPE_P (type)
4720 || VECTOR_FLOAT_TYPE_P (type))
4721 && (GET_MODE_CLASS (mode) == MODE_FLOAT
4722 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
4723 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
4724 break;
4725
4726 case ABI_N32:
4727 case ABI_64:
4728 /* Scalar, complex and vector floating-point types are passed in
4729 floating-point registers, as long as this is a named rather
4730 than a variable argument. */
4731 info->fpr_p = (named
4732 && (type == 0 || FLOAT_TYPE_P (type))
4733 && (GET_MODE_CLASS (mode) == MODE_FLOAT
4734 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4735 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
4736 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
4737
4738 /* ??? According to the ABI documentation, the real and imaginary
4739 parts of complex floats should be passed in individual registers.
4740 The real and imaginary parts of stack arguments are supposed
4741 to be contiguous and there should be an extra word of padding
4742 at the end.
4743
4744 This has two problems. First, it makes it impossible to use a
4745 single "void *" va_list type, since register and stack arguments
4746 are passed differently. (At the time of writing, MIPSpro cannot
4747 handle complex float varargs correctly.) Second, it's unclear
4748 what should happen when there is only one register free.
4749
4750 For now, we assume that named complex floats should go into FPRs
4751 if there are two FPRs free, otherwise they should be passed in the
4752 same way as a struct containing two floats. */
4753 if (info->fpr_p
4754 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4755 && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
4756 {
4757 if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
4758 info->fpr_p = false;
4759 else
4760 num_words = 2;
4761 }
4762 break;
4763
4764 default:
4765 gcc_unreachable ();
4766 }
4767
4768 /* See whether the argument has doubleword alignment. */
4769 doubleword_aligned_p = FUNCTION_ARG_BOUNDARY (mode, type) > BITS_PER_WORD;
4770
4771 /* Set REG_OFFSET to the register count we're interested in.
4772 The EABI allocates the floating-point registers separately,
4773 but the other ABIs allocate them like integer registers. */
4774 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
4775 ? cum->num_fprs
4776 : cum->num_gprs);
4777
4778 /* Advance to an even register if the argument is doubleword-aligned. */
4779 if (doubleword_aligned_p)
4780 info->reg_offset += info->reg_offset & 1;
4781
4782 /* Work out the offset of a stack argument. */
4783 info->stack_offset = cum->stack_words;
4784 if (doubleword_aligned_p)
4785 info->stack_offset += info->stack_offset & 1;
4786
4787 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
4788
4789 /* Partition the argument between registers and stack. */
4790 info->reg_words = MIN (num_words, max_regs);
4791 info->stack_words = num_words - info->reg_words;
4792 }
4793
4794 /* INFO describes a register argument that has the normal format for the
4795 argument's mode. Return the register it uses, assuming that FPRs are
4796 available if HARD_FLOAT_P. */
4797
4798 static unsigned int
4799 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
4800 {
4801 if (!info->fpr_p || !hard_float_p)
4802 return GP_ARG_FIRST + info->reg_offset;
4803 else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
4804 /* In o32, the second argument is always passed in $f14
4805 for TARGET_DOUBLE_FLOAT, regardless of whether the
4806 first argument was a word or doubleword. */
4807 return FP_ARG_FIRST + 2;
4808 else
4809 return FP_ARG_FIRST + info->reg_offset;
4810 }
4811
4812 /* Implement TARGET_STRICT_ARGUMENT_NAMING. */
4813
4814 static bool
4815 mips_strict_argument_naming (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
4816 {
4817 return !TARGET_OLDABI;
4818 }
4819
4820 /* Implement FUNCTION_ARG. */
4821
4822 rtx
4823 mips_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
4824 tree type, int named)
4825 {
4826 struct mips_arg_info info;
4827
4828 /* We will be called with a mode of VOIDmode after the last argument
4829 has been seen. Whatever we return will be passed to the call expander.
4830 If we need a MIPS16 fp_code, return a REG with the code stored as
4831 the mode. */
4832 if (mode == VOIDmode)
4833 {
4834 if (TARGET_MIPS16 && cum->fp_code != 0)
4835 return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
4836 else
4837 return NULL;
4838 }
4839
4840 mips_get_arg_info (&info, cum, mode, type, named);
4841
4842 /* Return straight away if the whole argument is passed on the stack. */
4843 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
4844 return NULL;
4845
4846 /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
4847 contains a double in its entirety, then that 64-bit chunk is passed
4848 in a floating-point register. */
4849 if (TARGET_NEWABI
4850 && TARGET_HARD_FLOAT
4851 && named
4852 && type != 0
4853 && TREE_CODE (type) == RECORD_TYPE
4854 && TYPE_SIZE_UNIT (type)
4855 && host_integerp (TYPE_SIZE_UNIT (type), 1))
4856 {
4857 tree field;
4858
4859 /* First check to see if there is any such field. */
4860 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4861 if (TREE_CODE (field) == FIELD_DECL
4862 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
4863 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
4864 && host_integerp (bit_position (field), 0)
4865 && int_bit_position (field) % BITS_PER_WORD == 0)
4866 break;
4867
4868 if (field != 0)
4869 {
4870 /* Now handle the special case by returning a PARALLEL
4871 indicating where each 64-bit chunk goes. INFO.REG_WORDS
4872 chunks are passed in registers. */
4873 unsigned int i;
4874 HOST_WIDE_INT bitpos;
4875 rtx ret;
4876
4877 /* assign_parms checks the mode of ENTRY_PARM, so we must
4878 use the actual mode here. */
4879 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
4880
4881 bitpos = 0;
4882 field = TYPE_FIELDS (type);
4883 for (i = 0; i < info.reg_words; i++)
4884 {
4885 rtx reg;
4886
4887 for (; field; field = DECL_CHAIN (field))
4888 if (TREE_CODE (field) == FIELD_DECL
4889 && int_bit_position (field) >= bitpos)
4890 break;
4891
4892 if (field
4893 && int_bit_position (field) == bitpos
4894 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
4895 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
4896 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
4897 else
4898 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
4899
4900 XVECEXP (ret, 0, i)
4901 = gen_rtx_EXPR_LIST (VOIDmode, reg,
4902 GEN_INT (bitpos / BITS_PER_UNIT));
4903
4904 bitpos += BITS_PER_WORD;
4905 }
4906 return ret;
4907 }
4908 }
4909
4910 /* Handle the n32/n64 conventions for passing complex floating-point
4911 arguments in FPR pairs. The real part goes in the lower register
4912 and the imaginary part goes in the upper register. */
4913 if (TARGET_NEWABI
4914 && info.fpr_p
4915 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4916 {
4917 rtx real, imag;
4918 enum machine_mode inner;
4919 unsigned int regno;
4920
4921 inner = GET_MODE_INNER (mode);
4922 regno = FP_ARG_FIRST + info.reg_offset;
4923 if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
4924 {
4925 /* Real part in registers, imaginary part on stack. */
4926 gcc_assert (info.stack_words == info.reg_words);
4927 return gen_rtx_REG (inner, regno);
4928 }
4929 else
4930 {
4931 gcc_assert (info.stack_words == 0);
4932 real = gen_rtx_EXPR_LIST (VOIDmode,
4933 gen_rtx_REG (inner, regno),
4934 const0_rtx);
4935 imag = gen_rtx_EXPR_LIST (VOIDmode,
4936 gen_rtx_REG (inner,
4937 regno + info.reg_words / 2),
4938 GEN_INT (GET_MODE_SIZE (inner)));
4939 return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
4940 }
4941 }
4942
4943 return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
4944 }
4945
4946 /* Implement FUNCTION_ARG_ADVANCE. */
4947
4948 void
4949 mips_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4950 tree type, int named)
4951 {
4952 struct mips_arg_info info;
4953
4954 mips_get_arg_info (&info, cum, mode, type, named);
4955
4956 if (!info.fpr_p)
4957 cum->gp_reg_found = true;
4958
4959 /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
4960 an explanation of what this code does. It assumes that we're using
4961 either the o32 or the o64 ABI, both of which pass at most 2 arguments
4962 in FPRs. */
4963 if (cum->arg_number < 2 && info.fpr_p)
4964 cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
4965
4966 /* Advance the register count. This has the effect of setting
4967 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
4968 argument required us to skip the final GPR and pass the whole
4969 argument on the stack. */
4970 if (mips_abi != ABI_EABI || !info.fpr_p)
4971 cum->num_gprs = info.reg_offset + info.reg_words;
4972 else if (info.reg_words > 0)
4973 cum->num_fprs += MAX_FPRS_PER_FMT;
4974
4975 /* Advance the stack word count. */
4976 if (info.stack_words > 0)
4977 cum->stack_words = info.stack_offset + info.stack_words;
4978
4979 cum->arg_number++;
4980 }
4981
4982 /* Implement TARGET_ARG_PARTIAL_BYTES. */
4983
4984 static int
4985 mips_arg_partial_bytes (CUMULATIVE_ARGS *cum,
4986 enum machine_mode mode, tree type, bool named)
4987 {
4988 struct mips_arg_info info;
4989
4990 mips_get_arg_info (&info, cum, mode, type, named);
4991 return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
4992 }
4993
4994 /* Implement FUNCTION_ARG_BOUNDARY. Every parameter gets at least
4995 PARM_BOUNDARY bits of alignment, but will be given anything up
4996 to STACK_BOUNDARY bits if the type requires it. */
4997
4998 int
4999 mips_function_arg_boundary (enum machine_mode mode, tree type)
5000 {
5001 unsigned int alignment;
5002
5003 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5004 if (alignment < PARM_BOUNDARY)
5005 alignment = PARM_BOUNDARY;
5006 if (alignment > STACK_BOUNDARY)
5007 alignment = STACK_BOUNDARY;
5008 return alignment;
5009 }
5010
5011 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5012 upward rather than downward. In other words, return true if the
5013 first byte of the stack slot has useful data, false if the last
5014 byte does. */
5015
5016 bool
5017 mips_pad_arg_upward (enum machine_mode mode, const_tree type)
5018 {
5019 /* On little-endian targets, the first byte of every stack argument
5020 is passed in the first byte of the stack slot. */
5021 if (!BYTES_BIG_ENDIAN)
5022 return true;
5023
5024 /* Otherwise, integral types are padded downward: the last byte of a
5025 stack argument is passed in the last byte of the stack slot. */
5026 if (type != 0
5027 ? (INTEGRAL_TYPE_P (type)
5028 || POINTER_TYPE_P (type)
5029 || FIXED_POINT_TYPE_P (type))
5030 : (SCALAR_INT_MODE_P (mode)
5031 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5032 return false;
5033
5034 /* Big-endian o64 pads floating-point arguments downward. */
5035 if (mips_abi == ABI_O64)
5036 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5037 return false;
5038
5039 /* Other types are padded upward for o32, o64, n32 and n64. */
5040 if (mips_abi != ABI_EABI)
5041 return true;
5042
5043 /* Arguments smaller than a stack slot are padded downward. */
5044 if (mode != BLKmode)
5045 return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5046 else
5047 return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5048 }
5049
5050 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
5051 if the least significant byte of the register has useful data. Return
5052 the opposite if the most significant byte does. */
5053
5054 bool
5055 mips_pad_reg_upward (enum machine_mode mode, tree type)
5056 {
5057 /* No shifting is required for floating-point arguments. */
5058 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5059 return !BYTES_BIG_ENDIAN;
5060
5061 /* Otherwise, apply the same padding to register arguments as we do
5062 to stack arguments. */
5063 return mips_pad_arg_upward (mode, type);
5064 }
5065
5066 /* Return nonzero when an argument must be passed by reference. */
5067
5068 static bool
5069 mips_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
5070 enum machine_mode mode, const_tree type,
5071 bool named ATTRIBUTE_UNUSED)
5072 {
5073 if (mips_abi == ABI_EABI)
5074 {
5075 int size;
5076
5077 /* ??? How should SCmode be handled? */
5078 if (mode == DImode || mode == DFmode
5079 || mode == DQmode || mode == UDQmode
5080 || mode == DAmode || mode == UDAmode)
5081 return 0;
5082
5083 size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5084 return size == -1 || size > UNITS_PER_WORD;
5085 }
5086 else
5087 {
5088 /* If we have a variable-sized parameter, we have no choice. */
5089 return targetm.calls.must_pass_in_stack (mode, type);
5090 }
5091 }
5092
5093 /* Implement TARGET_CALLEE_COPIES. */
5094
5095 static bool
5096 mips_callee_copies (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
5097 enum machine_mode mode ATTRIBUTE_UNUSED,
5098 const_tree type ATTRIBUTE_UNUSED, bool named)
5099 {
5100 return mips_abi == ABI_EABI && named;
5101 }
5102 \f
5103 /* See whether VALTYPE is a record whose fields should be returned in
5104 floating-point registers. If so, return the number of fields and
5105 list them in FIELDS (which should have two elements). Return 0
5106 otherwise.
5107
5108 For n32 & n64, a structure with one or two fields is returned in
5109 floating-point registers as long as every field has a floating-point
5110 type. */
5111
5112 static int
5113 mips_fpr_return_fields (const_tree valtype, tree *fields)
5114 {
5115 tree field;
5116 int i;
5117
5118 if (!TARGET_NEWABI)
5119 return 0;
5120
5121 if (TREE_CODE (valtype) != RECORD_TYPE)
5122 return 0;
5123
5124 i = 0;
5125 for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5126 {
5127 if (TREE_CODE (field) != FIELD_DECL)
5128 continue;
5129
5130 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5131 return 0;
5132
5133 if (i == 2)
5134 return 0;
5135
5136 fields[i++] = field;
5137 }
5138 return i;
5139 }
5140
5141 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
5142 a value in the most significant part of $2/$3 if:
5143
5144 - the target is big-endian;
5145
5146 - the value has a structure or union type (we generalize this to
5147 cover aggregates from other languages too); and
5148
5149 - the structure is not returned in floating-point registers. */
5150
5151 static bool
5152 mips_return_in_msb (const_tree valtype)
5153 {
5154 tree fields[2];
5155
5156 return (TARGET_NEWABI
5157 && TARGET_BIG_ENDIAN
5158 && AGGREGATE_TYPE_P (valtype)
5159 && mips_fpr_return_fields (valtype, fields) == 0);
5160 }
5161
5162 /* Return true if the function return value MODE will get returned in a
5163 floating-point register. */
5164
5165 static bool
5166 mips_return_mode_in_fpr_p (enum machine_mode mode)
5167 {
5168 return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5169 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
5170 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5171 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5172 }
5173
5174 /* Return the representation of an FPR return register when the
5175 value being returned in FP_RETURN has mode VALUE_MODE and the
5176 return type itself has mode TYPE_MODE. On NewABI targets,
5177 the two modes may be different for structures like:
5178
5179 struct __attribute__((packed)) foo { float f; }
5180
5181 where we return the SFmode value of "f" in FP_RETURN, but where
5182 the structure itself has mode BLKmode. */
5183
5184 static rtx
5185 mips_return_fpr_single (enum machine_mode type_mode,
5186 enum machine_mode value_mode)
5187 {
5188 rtx x;
5189
5190 x = gen_rtx_REG (value_mode, FP_RETURN);
5191 if (type_mode != value_mode)
5192 {
5193 x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5194 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5195 }
5196 return x;
5197 }
5198
5199 /* Return a composite value in a pair of floating-point registers.
5200 MODE1 and OFFSET1 are the mode and byte offset for the first value,
5201 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
5202 complete value.
5203
5204 For n32 & n64, $f0 always holds the first value and $f2 the second.
5205 Otherwise the values are packed together as closely as possible. */
5206
5207 static rtx
5208 mips_return_fpr_pair (enum machine_mode mode,
5209 enum machine_mode mode1, HOST_WIDE_INT offset1,
5210 enum machine_mode mode2, HOST_WIDE_INT offset2)
5211 {
5212 int inc;
5213
5214 inc = (TARGET_NEWABI ? 2 : MAX_FPRS_PER_FMT);
5215 return gen_rtx_PARALLEL
5216 (mode,
5217 gen_rtvec (2,
5218 gen_rtx_EXPR_LIST (VOIDmode,
5219 gen_rtx_REG (mode1, FP_RETURN),
5220 GEN_INT (offset1)),
5221 gen_rtx_EXPR_LIST (VOIDmode,
5222 gen_rtx_REG (mode2, FP_RETURN + inc),
5223 GEN_INT (offset2))));
5224
5225 }
5226
5227 /* Implement FUNCTION_VALUE and LIBCALL_VALUE. For normal calls,
5228 VALTYPE is the return type and MODE is VOIDmode. For libcalls,
5229 VALTYPE is null and MODE is the mode of the return value. */
5230
5231 rtx
5232 mips_function_value (const_tree valtype, const_tree func, enum machine_mode mode)
5233 {
5234 if (valtype)
5235 {
5236 tree fields[2];
5237 int unsigned_p;
5238
5239 mode = TYPE_MODE (valtype);
5240 unsigned_p = TYPE_UNSIGNED (valtype);
5241
5242 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5243 return values, promote the mode here too. */
5244 mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5245
5246 /* Handle structures whose fields are returned in $f0/$f2. */
5247 switch (mips_fpr_return_fields (valtype, fields))
5248 {
5249 case 1:
5250 return mips_return_fpr_single (mode,
5251 TYPE_MODE (TREE_TYPE (fields[0])));
5252
5253 case 2:
5254 return mips_return_fpr_pair (mode,
5255 TYPE_MODE (TREE_TYPE (fields[0])),
5256 int_byte_position (fields[0]),
5257 TYPE_MODE (TREE_TYPE (fields[1])),
5258 int_byte_position (fields[1]));
5259 }
5260
5261 /* If a value is passed in the most significant part of a register, see
5262 whether we have to round the mode up to a whole number of words. */
5263 if (mips_return_in_msb (valtype))
5264 {
5265 HOST_WIDE_INT size = int_size_in_bytes (valtype);
5266 if (size % UNITS_PER_WORD != 0)
5267 {
5268 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5269 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5270 }
5271 }
5272
5273 /* For EABI, the class of return register depends entirely on MODE.
5274 For example, "struct { some_type x; }" and "union { some_type x; }"
5275 are returned in the same way as a bare "some_type" would be.
5276 Other ABIs only use FPRs for scalar, complex or vector types. */
5277 if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5278 return gen_rtx_REG (mode, GP_RETURN);
5279 }
5280
5281 if (!TARGET_MIPS16)
5282 {
5283 /* Handle long doubles for n32 & n64. */
5284 if (mode == TFmode)
5285 return mips_return_fpr_pair (mode,
5286 DImode, 0,
5287 DImode, GET_MODE_SIZE (mode) / 2);
5288
5289 if (mips_return_mode_in_fpr_p (mode))
5290 {
5291 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5292 return mips_return_fpr_pair (mode,
5293 GET_MODE_INNER (mode), 0,
5294 GET_MODE_INNER (mode),
5295 GET_MODE_SIZE (mode) / 2);
5296 else
5297 return gen_rtx_REG (mode, FP_RETURN);
5298 }
5299 }
5300
5301 return gen_rtx_REG (mode, GP_RETURN);
5302 }
5303
5304 /* Implement TARGET_RETURN_IN_MEMORY. Under the o32 and o64 ABIs,
5305 all BLKmode objects are returned in memory. Under the n32, n64
5306 and embedded ABIs, small structures are returned in a register.
5307 Objects with varying size must still be returned in memory, of
5308 course. */
5309
5310 static bool
5311 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5312 {
5313 return (TARGET_OLDABI
5314 ? TYPE_MODE (type) == BLKmode
5315 : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5316 }
5317 \f
5318 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
5319
5320 static void
5321 mips_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
5322 tree type, int *pretend_size ATTRIBUTE_UNUSED,
5323 int no_rtl)
5324 {
5325 CUMULATIVE_ARGS local_cum;
5326 int gp_saved, fp_saved;
5327
5328 /* The caller has advanced CUM up to, but not beyond, the last named
5329 argument. Advance a local copy of CUM past the last "real" named
5330 argument, to find out how many registers are left over. */
5331 local_cum = *cum;
5332 FUNCTION_ARG_ADVANCE (local_cum, mode, type, true);
5333
5334 /* Found out how many registers we need to save. */
5335 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
5336 fp_saved = (EABI_FLOAT_VARARGS_P
5337 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
5338 : 0);
5339
5340 if (!no_rtl)
5341 {
5342 if (gp_saved > 0)
5343 {
5344 rtx ptr, mem;
5345
5346 ptr = plus_constant (virtual_incoming_args_rtx,
5347 REG_PARM_STACK_SPACE (cfun->decl)
5348 - gp_saved * UNITS_PER_WORD);
5349 mem = gen_frame_mem (BLKmode, ptr);
5350 set_mem_alias_set (mem, get_varargs_alias_set ());
5351
5352 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
5353 mem, gp_saved);
5354 }
5355 if (fp_saved > 0)
5356 {
5357 /* We can't use move_block_from_reg, because it will use
5358 the wrong mode. */
5359 enum machine_mode mode;
5360 int off, i;
5361
5362 /* Set OFF to the offset from virtual_incoming_args_rtx of
5363 the first float register. The FP save area lies below
5364 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
5365 off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
5366 off -= fp_saved * UNITS_PER_FPREG;
5367
5368 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
5369
5370 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
5371 i += MAX_FPRS_PER_FMT)
5372 {
5373 rtx ptr, mem;
5374
5375 ptr = plus_constant (virtual_incoming_args_rtx, off);
5376 mem = gen_frame_mem (mode, ptr);
5377 set_mem_alias_set (mem, get_varargs_alias_set ());
5378 mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
5379 off += UNITS_PER_HWFPVALUE;
5380 }
5381 }
5382 }
5383 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
5384 cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
5385 + fp_saved * UNITS_PER_FPREG);
5386 }
5387
5388 /* Implement TARGET_BUILTIN_VA_LIST. */
5389
5390 static tree
5391 mips_build_builtin_va_list (void)
5392 {
5393 if (EABI_FLOAT_VARARGS_P)
5394 {
5395 /* We keep 3 pointers, and two offsets.
5396
5397 Two pointers are to the overflow area, which starts at the CFA.
5398 One of these is constant, for addressing into the GPR save area
5399 below it. The other is advanced up the stack through the
5400 overflow region.
5401
5402 The third pointer is to the bottom of the GPR save area.
5403 Since the FPR save area is just below it, we can address
5404 FPR slots off this pointer.
5405
5406 We also keep two one-byte offsets, which are to be subtracted
5407 from the constant pointers to yield addresses in the GPR and
5408 FPR save areas. These are downcounted as float or non-float
5409 arguments are used, and when they get to zero, the argument
5410 must be obtained from the overflow region. */
5411 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
5412 tree array, index;
5413
5414 record = lang_hooks.types.make_type (RECORD_TYPE);
5415
5416 f_ovfl = build_decl (BUILTINS_LOCATION,
5417 FIELD_DECL, get_identifier ("__overflow_argptr"),
5418 ptr_type_node);
5419 f_gtop = build_decl (BUILTINS_LOCATION,
5420 FIELD_DECL, get_identifier ("__gpr_top"),
5421 ptr_type_node);
5422 f_ftop = build_decl (BUILTINS_LOCATION,
5423 FIELD_DECL, get_identifier ("__fpr_top"),
5424 ptr_type_node);
5425 f_goff = build_decl (BUILTINS_LOCATION,
5426 FIELD_DECL, get_identifier ("__gpr_offset"),
5427 unsigned_char_type_node);
5428 f_foff = build_decl (BUILTINS_LOCATION,
5429 FIELD_DECL, get_identifier ("__fpr_offset"),
5430 unsigned_char_type_node);
5431 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
5432 warn on every user file. */
5433 index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
5434 array = build_array_type (unsigned_char_type_node,
5435 build_index_type (index));
5436 f_res = build_decl (BUILTINS_LOCATION,
5437 FIELD_DECL, get_identifier ("__reserved"), array);
5438
5439 DECL_FIELD_CONTEXT (f_ovfl) = record;
5440 DECL_FIELD_CONTEXT (f_gtop) = record;
5441 DECL_FIELD_CONTEXT (f_ftop) = record;
5442 DECL_FIELD_CONTEXT (f_goff) = record;
5443 DECL_FIELD_CONTEXT (f_foff) = record;
5444 DECL_FIELD_CONTEXT (f_res) = record;
5445
5446 TYPE_FIELDS (record) = f_ovfl;
5447 DECL_CHAIN (f_ovfl) = f_gtop;
5448 DECL_CHAIN (f_gtop) = f_ftop;
5449 DECL_CHAIN (f_ftop) = f_goff;
5450 DECL_CHAIN (f_goff) = f_foff;
5451 DECL_CHAIN (f_foff) = f_res;
5452
5453 layout_type (record);
5454 return record;
5455 }
5456 else if (TARGET_IRIX6)
5457 /* On IRIX 6, this type is 'char *'. */
5458 return build_pointer_type (char_type_node);
5459 else
5460 /* Otherwise, we use 'void *'. */
5461 return ptr_type_node;
5462 }
5463
5464 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
5465
5466 static void
5467 mips_va_start (tree valist, rtx nextarg)
5468 {
5469 if (EABI_FLOAT_VARARGS_P)
5470 {
5471 const CUMULATIVE_ARGS *cum;
5472 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5473 tree ovfl, gtop, ftop, goff, foff;
5474 tree t;
5475 int gpr_save_area_size;
5476 int fpr_save_area_size;
5477 int fpr_offset;
5478
5479 cum = &crtl->args.info;
5480 gpr_save_area_size
5481 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5482 fpr_save_area_size
5483 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5484
5485 f_ovfl = TYPE_FIELDS (va_list_type_node);
5486 f_gtop = DECL_CHAIN (f_ovfl);
5487 f_ftop = DECL_CHAIN (f_gtop);
5488 f_goff = DECL_CHAIN (f_ftop);
5489 f_foff = DECL_CHAIN (f_goff);
5490
5491 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5492 NULL_TREE);
5493 gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
5494 NULL_TREE);
5495 ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
5496 NULL_TREE);
5497 goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
5498 NULL_TREE);
5499 foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
5500 NULL_TREE);
5501
5502 /* Emit code to initialize OVFL, which points to the next varargs
5503 stack argument. CUM->STACK_WORDS gives the number of stack
5504 words used by named arguments. */
5505 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5506 if (cum->stack_words > 0)
5507 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovfl), t,
5508 size_int (cum->stack_words * UNITS_PER_WORD));
5509 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5510 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5511
5512 /* Emit code to initialize GTOP, the top of the GPR save area. */
5513 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5514 t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5515 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5516
5517 /* Emit code to initialize FTOP, the top of the FPR save area.
5518 This address is gpr_save_area_bytes below GTOP, rounded
5519 down to the next fp-aligned boundary. */
5520 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5521 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5522 fpr_offset &= -UNITS_PER_FPVALUE;
5523 if (fpr_offset)
5524 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ftop), t,
5525 size_int (-fpr_offset));
5526 t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5527 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5528
5529 /* Emit code to initialize GOFF, the offset from GTOP of the
5530 next GPR argument. */
5531 t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
5532 build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
5533 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5534
5535 /* Likewise emit code to initialize FOFF, the offset from FTOP
5536 of the next FPR argument. */
5537 t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
5538 build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
5539 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5540 }
5541 else
5542 {
5543 nextarg = plus_constant (nextarg, -cfun->machine->varargs_size);
5544 std_expand_builtin_va_start (valist, nextarg);
5545 }
5546 }
5547
5548 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
5549
5550 static tree
5551 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5552 gimple_seq *post_p)
5553 {
5554 tree addr;
5555 bool indirect_p;
5556
5557 indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
5558 if (indirect_p)
5559 type = build_pointer_type (type);
5560
5561 if (!EABI_FLOAT_VARARGS_P)
5562 addr = std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5563 else
5564 {
5565 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5566 tree ovfl, top, off, align;
5567 HOST_WIDE_INT size, rsize, osize;
5568 tree t, u;
5569
5570 f_ovfl = TYPE_FIELDS (va_list_type_node);
5571 f_gtop = DECL_CHAIN (f_ovfl);
5572 f_ftop = DECL_CHAIN (f_gtop);
5573 f_goff = DECL_CHAIN (f_ftop);
5574 f_foff = DECL_CHAIN (f_goff);
5575
5576 /* Let:
5577
5578 TOP be the top of the GPR or FPR save area;
5579 OFF be the offset from TOP of the next register;
5580 ADDR_RTX be the address of the argument;
5581 SIZE be the number of bytes in the argument type;
5582 RSIZE be the number of bytes used to store the argument
5583 when it's in the register save area; and
5584 OSIZE be the number of bytes used to store it when it's
5585 in the stack overflow area.
5586
5587 The code we want is:
5588
5589 1: off &= -rsize; // round down
5590 2: if (off != 0)
5591 3: {
5592 4: addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
5593 5: off -= rsize;
5594 6: }
5595 7: else
5596 8: {
5597 9: ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
5598 10: addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
5599 11: ovfl += osize;
5600 14: }
5601
5602 [1] and [9] can sometimes be optimized away. */
5603
5604 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5605 NULL_TREE);
5606 size = int_size_in_bytes (type);
5607
5608 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
5609 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
5610 {
5611 top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
5612 unshare_expr (valist), f_ftop, NULL_TREE);
5613 off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
5614 unshare_expr (valist), f_foff, NULL_TREE);
5615
5616 /* When va_start saves FPR arguments to the stack, each slot
5617 takes up UNITS_PER_HWFPVALUE bytes, regardless of the
5618 argument's precision. */
5619 rsize = UNITS_PER_HWFPVALUE;
5620
5621 /* Overflow arguments are padded to UNITS_PER_WORD bytes
5622 (= PARM_BOUNDARY bits). This can be different from RSIZE
5623 in two cases:
5624
5625 (1) On 32-bit targets when TYPE is a structure such as:
5626
5627 struct s { float f; };
5628
5629 Such structures are passed in paired FPRs, so RSIZE
5630 will be 8 bytes. However, the structure only takes
5631 up 4 bytes of memory, so OSIZE will only be 4.
5632
5633 (2) In combinations such as -mgp64 -msingle-float
5634 -fshort-double. Doubles passed in registers will then take
5635 up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
5636 stack take up UNITS_PER_WORD bytes. */
5637 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
5638 }
5639 else
5640 {
5641 top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
5642 unshare_expr (valist), f_gtop, NULL_TREE);
5643 off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
5644 unshare_expr (valist), f_goff, NULL_TREE);
5645 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
5646 if (rsize > UNITS_PER_WORD)
5647 {
5648 /* [1] Emit code for: off &= -rsize. */
5649 t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
5650 build_int_cst (TREE_TYPE (off), -rsize));
5651 gimplify_assign (unshare_expr (off), t, pre_p);
5652 }
5653 osize = rsize;
5654 }
5655
5656 /* [2] Emit code to branch if off == 0. */
5657 t = build2 (NE_EXPR, boolean_type_node, off,
5658 build_int_cst (TREE_TYPE (off), 0));
5659 addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
5660
5661 /* [5] Emit code for: off -= rsize. We do this as a form of
5662 post-decrement not available to C. */
5663 t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
5664 t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
5665
5666 /* [4] Emit code for:
5667 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0). */
5668 t = fold_convert (sizetype, t);
5669 t = fold_build1 (NEGATE_EXPR, sizetype, t);
5670 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (top), top, t);
5671 if (BYTES_BIG_ENDIAN && rsize > size)
5672 {
5673 u = size_int (rsize - size);
5674 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t, u);
5675 }
5676 COND_EXPR_THEN (addr) = t;
5677
5678 if (osize > UNITS_PER_WORD)
5679 {
5680 /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize. */
5681 u = size_int (osize - 1);
5682 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovfl),
5683 unshare_expr (ovfl), u);
5684 t = fold_convert (sizetype, t);
5685 u = size_int (-osize);
5686 t = build2 (BIT_AND_EXPR, sizetype, t, u);
5687 t = fold_convert (TREE_TYPE (ovfl), t);
5688 align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
5689 unshare_expr (ovfl), t);
5690 }
5691 else
5692 align = NULL;
5693
5694 /* [10, 11] Emit code for:
5695 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
5696 ovfl += osize. */
5697 u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
5698 t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
5699 if (BYTES_BIG_ENDIAN && osize > size)
5700 {
5701 u = size_int (osize - size);
5702 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t, u);
5703 }
5704
5705 /* String [9] and [10, 11] together. */
5706 if (align)
5707 t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
5708 COND_EXPR_ELSE (addr) = t;
5709
5710 addr = fold_convert (build_pointer_type (type), addr);
5711 addr = build_va_arg_indirect_ref (addr);
5712 }
5713
5714 if (indirect_p)
5715 addr = build_va_arg_indirect_ref (addr);
5716
5717 return addr;
5718 }
5719 \f
5720 /* Start a definition of function NAME. MIPS16_P indicates whether the
5721 function contains MIPS16 code. */
5722
5723 static void
5724 mips_start_function_definition (const char *name, bool mips16_p)
5725 {
5726 if (mips16_p)
5727 fprintf (asm_out_file, "\t.set\tmips16\n");
5728 else
5729 fprintf (asm_out_file, "\t.set\tnomips16\n");
5730
5731 if (!flag_inhibit_size_directive)
5732 {
5733 fputs ("\t.ent\t", asm_out_file);
5734 assemble_name (asm_out_file, name);
5735 fputs ("\n", asm_out_file);
5736 }
5737
5738 ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
5739
5740 /* Start the definition proper. */
5741 assemble_name (asm_out_file, name);
5742 fputs (":\n", asm_out_file);
5743 }
5744
5745 /* End a function definition started by mips_start_function_definition. */
5746
5747 static void
5748 mips_end_function_definition (const char *name)
5749 {
5750 if (!flag_inhibit_size_directive)
5751 {
5752 fputs ("\t.end\t", asm_out_file);
5753 assemble_name (asm_out_file, name);
5754 fputs ("\n", asm_out_file);
5755 }
5756 }
5757 \f
5758 /* Return true if calls to X can use R_MIPS_CALL* relocations. */
5759
5760 static bool
5761 mips_ok_for_lazy_binding_p (rtx x)
5762 {
5763 return (TARGET_USE_GOT
5764 && GET_CODE (x) == SYMBOL_REF
5765 && !SYMBOL_REF_BIND_NOW_P (x)
5766 && !mips_symbol_binds_local_p (x));
5767 }
5768
5769 /* Load function address ADDR into register DEST. TYPE is as for
5770 mips_expand_call. Return true if we used an explicit lazy-binding
5771 sequence. */
5772
5773 static bool
5774 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
5775 {
5776 /* If we're generating PIC, and this call is to a global function,
5777 try to allow its address to be resolved lazily. This isn't
5778 possible for sibcalls when $gp is call-saved because the value
5779 of $gp on entry to the stub would be our caller's gp, not ours. */
5780 if (TARGET_EXPLICIT_RELOCS
5781 && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
5782 && mips_ok_for_lazy_binding_p (addr))
5783 {
5784 addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
5785 emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
5786 return true;
5787 }
5788 else
5789 {
5790 mips_emit_move (dest, addr);
5791 return false;
5792 }
5793 }
5794 \f
5795 /* Each locally-defined hard-float MIPS16 function has a local symbol
5796 associated with it. This hash table maps the function symbol (FUNC)
5797 to the local symbol (LOCAL). */
5798 struct GTY(()) mips16_local_alias {
5799 rtx func;
5800 rtx local;
5801 };
5802 static GTY ((param_is (struct mips16_local_alias))) htab_t mips16_local_aliases;
5803
5804 /* Hash table callbacks for mips16_local_aliases. */
5805
5806 static hashval_t
5807 mips16_local_aliases_hash (const void *entry)
5808 {
5809 const struct mips16_local_alias *alias;
5810
5811 alias = (const struct mips16_local_alias *) entry;
5812 return htab_hash_string (XSTR (alias->func, 0));
5813 }
5814
5815 static int
5816 mips16_local_aliases_eq (const void *entry1, const void *entry2)
5817 {
5818 const struct mips16_local_alias *alias1, *alias2;
5819
5820 alias1 = (const struct mips16_local_alias *) entry1;
5821 alias2 = (const struct mips16_local_alias *) entry2;
5822 return rtx_equal_p (alias1->func, alias2->func);
5823 }
5824
5825 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
5826 Return a local alias for it, creating a new one if necessary. */
5827
5828 static rtx
5829 mips16_local_alias (rtx func)
5830 {
5831 struct mips16_local_alias *alias, tmp_alias;
5832 void **slot;
5833
5834 /* Create the hash table if this is the first call. */
5835 if (mips16_local_aliases == NULL)
5836 mips16_local_aliases = htab_create_ggc (37, mips16_local_aliases_hash,
5837 mips16_local_aliases_eq, NULL);
5838
5839 /* Look up the function symbol, creating a new entry if need be. */
5840 tmp_alias.func = func;
5841 slot = htab_find_slot (mips16_local_aliases, &tmp_alias, INSERT);
5842 gcc_assert (slot != NULL);
5843
5844 alias = (struct mips16_local_alias *) *slot;
5845 if (alias == NULL)
5846 {
5847 const char *func_name, *local_name;
5848 rtx local;
5849
5850 /* Create a new SYMBOL_REF for the local symbol. The choice of
5851 __fn_local_* is based on the __fn_stub_* names that we've
5852 traditionally used for the non-MIPS16 stub. */
5853 func_name = targetm.strip_name_encoding (XSTR (func, 0));
5854 local_name = ACONCAT (("__fn_local_", func_name, NULL));
5855 local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
5856 SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
5857
5858 /* Create a new structure to represent the mapping. */
5859 alias = ggc_alloc_mips16_local_alias ();
5860 alias->func = func;
5861 alias->local = local;
5862 *slot = alias;
5863 }
5864 return alias->local;
5865 }
5866 \f
5867 /* A chained list of functions for which mips16_build_call_stub has already
5868 generated a stub. NAME is the name of the function and FP_RET_P is true
5869 if the function returns a value in floating-point registers. */
5870 struct mips16_stub {
5871 struct mips16_stub *next;
5872 char *name;
5873 bool fp_ret_p;
5874 };
5875 static struct mips16_stub *mips16_stubs;
5876
5877 /* Return a SYMBOL_REF for a MIPS16 function called NAME. */
5878
5879 static rtx
5880 mips16_stub_function (const char *name)
5881 {
5882 rtx x;
5883
5884 x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
5885 SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
5886 return x;
5887 }
5888
5889 /* Return the two-character string that identifies floating-point
5890 return mode MODE in the name of a MIPS16 function stub. */
5891
5892 static const char *
5893 mips16_call_stub_mode_suffix (enum machine_mode mode)
5894 {
5895 if (mode == SFmode)
5896 return "sf";
5897 else if (mode == DFmode)
5898 return "df";
5899 else if (mode == SCmode)
5900 return "sc";
5901 else if (mode == DCmode)
5902 return "dc";
5903 else if (mode == V2SFmode)
5904 return "df";
5905 else
5906 gcc_unreachable ();
5907 }
5908
5909 /* Write instructions to move a 32-bit value between general register
5910 GPREG and floating-point register FPREG. DIRECTION is 't' to move
5911 from GPREG to FPREG and 'f' to move in the opposite direction. */
5912
5913 static void
5914 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
5915 {
5916 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
5917 reg_names[gpreg], reg_names[fpreg]);
5918 }
5919
5920 /* Likewise for 64-bit values. */
5921
5922 static void
5923 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
5924 {
5925 if (TARGET_64BIT)
5926 fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
5927 reg_names[gpreg], reg_names[fpreg]);
5928 else if (TARGET_FLOAT64)
5929 {
5930 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
5931 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
5932 fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
5933 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
5934 }
5935 else
5936 {
5937 /* Move the least-significant word. */
5938 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
5939 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
5940 /* ...then the most significant word. */
5941 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
5942 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
5943 }
5944 }
5945
5946 /* Write out code to move floating-point arguments into or out of
5947 general registers. FP_CODE is the code describing which arguments
5948 are present (see the comment above the definition of CUMULATIVE_ARGS
5949 in mips.h). DIRECTION is as for mips_output_32bit_xfer. */
5950
5951 static void
5952 mips_output_args_xfer (int fp_code, char direction)
5953 {
5954 unsigned int gparg, fparg, f;
5955 CUMULATIVE_ARGS cum;
5956
5957 /* This code only works for o32 and o64. */
5958 gcc_assert (TARGET_OLDABI);
5959
5960 mips_init_cumulative_args (&cum, NULL);
5961
5962 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
5963 {
5964 enum machine_mode mode;
5965 struct mips_arg_info info;
5966
5967 if ((f & 3) == 1)
5968 mode = SFmode;
5969 else if ((f & 3) == 2)
5970 mode = DFmode;
5971 else
5972 gcc_unreachable ();
5973
5974 mips_get_arg_info (&info, &cum, mode, NULL, true);
5975 gparg = mips_arg_regno (&info, false);
5976 fparg = mips_arg_regno (&info, true);
5977
5978 if (mode == SFmode)
5979 mips_output_32bit_xfer (direction, gparg, fparg);
5980 else
5981 mips_output_64bit_xfer (direction, gparg, fparg);
5982
5983 mips_function_arg_advance (&cum, mode, NULL, true);
5984 }
5985 }
5986
5987 /* Write a MIPS16 stub for the current function. This stub is used
5988 for functions which take arguments in the floating-point registers.
5989 It is normal-mode code that moves the floating-point arguments
5990 into the general registers and then jumps to the MIPS16 code. */
5991
5992 static void
5993 mips16_build_function_stub (void)
5994 {
5995 const char *fnname, *alias_name, *separator;
5996 char *secname, *stubname;
5997 tree stubdecl;
5998 unsigned int f;
5999 rtx symbol, alias;
6000
6001 /* Create the name of the stub, and its unique section. */
6002 symbol = XEXP (DECL_RTL (current_function_decl), 0);
6003 alias = mips16_local_alias (symbol);
6004
6005 fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6006 alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6007 secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6008 stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6009
6010 /* Build a decl for the stub. */
6011 stubdecl = build_decl (BUILTINS_LOCATION,
6012 FUNCTION_DECL, get_identifier (stubname),
6013 build_function_type (void_type_node, NULL_TREE));
6014 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6015 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6016 RESULT_DECL, NULL_TREE, void_type_node);
6017
6018 /* Output a comment. */
6019 fprintf (asm_out_file, "\t# Stub function for %s (",
6020 current_function_name ());
6021 separator = "";
6022 for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6023 {
6024 fprintf (asm_out_file, "%s%s", separator,
6025 (f & 3) == 1 ? "float" : "double");
6026 separator = ", ";
6027 }
6028 fprintf (asm_out_file, ")\n");
6029
6030 /* Start the function definition. */
6031 assemble_start_function (stubdecl, stubname);
6032 mips_start_function_definition (stubname, false);
6033
6034 /* If generating pic2 code, either set up the global pointer or
6035 switch to pic0. */
6036 if (TARGET_ABICALLS_PIC2)
6037 {
6038 if (TARGET_ABSOLUTE_ABICALLS)
6039 fprintf (asm_out_file, "\t.option\tpic0\n");
6040 else
6041 {
6042 output_asm_insn ("%(.cpload\t%^%)", NULL);
6043 /* Emit an R_MIPS_NONE relocation to tell the linker what the
6044 target function is. Use a local GOT access when loading the
6045 symbol, to cut down on the number of unnecessary GOT entries
6046 for stubs that aren't needed. */
6047 output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6048 symbol = alias;
6049 }
6050 }
6051
6052 /* Load the address of the MIPS16 function into $25. Do this first so
6053 that targets with coprocessor interlocks can use an MFC1 to fill the
6054 delay slot. */
6055 output_asm_insn ("la\t%^,%0", &symbol);
6056
6057 /* Move the arguments from floating-point registers to general registers. */
6058 mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6059
6060 /* Jump to the MIPS16 function. */
6061 output_asm_insn ("jr\t%^", NULL);
6062
6063 if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6064 fprintf (asm_out_file, "\t.option\tpic2\n");
6065
6066 mips_end_function_definition (stubname);
6067
6068 /* If the linker needs to create a dynamic symbol for the target
6069 function, it will associate the symbol with the stub (which,
6070 unlike the target function, follows the proper calling conventions).
6071 It is therefore useful to have a local alias for the target function,
6072 so that it can still be identified as MIPS16 code. As an optimization,
6073 this symbol can also be used for indirect MIPS16 references from
6074 within this file. */
6075 ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6076
6077 switch_to_section (function_section (current_function_decl));
6078 }
6079
6080 /* The current function is a MIPS16 function that returns a value in an FPR.
6081 Copy the return value from its soft-float to its hard-float location.
6082 libgcc2 has special non-MIPS16 helper functions for each case. */
6083
6084 static void
6085 mips16_copy_fpr_return_value (void)
6086 {
6087 rtx fn, insn, retval;
6088 tree return_type;
6089 enum machine_mode return_mode;
6090 const char *name;
6091
6092 return_type = DECL_RESULT (current_function_decl);
6093 return_mode = DECL_MODE (return_type);
6094
6095 name = ACONCAT (("__mips16_ret_",
6096 mips16_call_stub_mode_suffix (return_mode),
6097 NULL));
6098 fn = mips16_stub_function (name);
6099
6100 /* The function takes arguments in $2 (and possibly $3), so calls
6101 to it cannot be lazily bound. */
6102 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6103
6104 /* Model the call as something that takes the GPR return value as
6105 argument and returns an "updated" value. */
6106 retval = gen_rtx_REG (return_mode, GP_RETURN);
6107 insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6108 const0_rtx, NULL_RTX, false);
6109 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6110 }
6111
6112 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6113 RETVAL is the location of the return value, or null if this is
6114 a "call" rather than a "call_value". ARGS_SIZE is the size of the
6115 arguments and FP_CODE is the code built by mips_function_arg;
6116 see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6117
6118 There are three alternatives:
6119
6120 - If a stub was needed, emit the call and return the call insn itself.
6121
6122 - If we can avoid using a stub by redirecting the call, set *FN_PTR
6123 to the new target and return null.
6124
6125 - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6126 unmodified.
6127
6128 A stub is needed for calls to functions that, in normal mode,
6129 receive arguments in FPRs or return values in FPRs. The stub
6130 copies the arguments from their soft-float positions to their
6131 hard-float positions, calls the real function, then copies the
6132 return value from its hard-float position to its soft-float
6133 position.
6134
6135 We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6136 If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6137 automatically redirects the JAL to the stub, otherwise the JAL
6138 continues to call FN directly. */
6139
6140 static rtx
6141 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6142 {
6143 const char *fnname;
6144 bool fp_ret_p;
6145 struct mips16_stub *l;
6146 rtx insn, fn;
6147
6148 /* We don't need to do anything if we aren't in MIPS16 mode, or if
6149 we were invoked with the -msoft-float option. */
6150 if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
6151 return NULL_RTX;
6152
6153 /* Figure out whether the value might come back in a floating-point
6154 register. */
6155 fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
6156
6157 /* We don't need to do anything if there were no floating-point
6158 arguments and the value will not be returned in a floating-point
6159 register. */
6160 if (fp_code == 0 && !fp_ret_p)
6161 return NULL_RTX;
6162
6163 /* We don't need to do anything if this is a call to a special
6164 MIPS16 support function. */
6165 fn = *fn_ptr;
6166 if (mips16_stub_function_p (fn))
6167 return NULL_RTX;
6168
6169 /* This code will only work for o32 and o64 abis. The other ABI's
6170 require more sophisticated support. */
6171 gcc_assert (TARGET_OLDABI);
6172
6173 /* If we're calling via a function pointer, use one of the magic
6174 libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
6175 Each stub expects the function address to arrive in register $2. */
6176 if (GET_CODE (fn) != SYMBOL_REF
6177 || !call_insn_operand (fn, VOIDmode))
6178 {
6179 char buf[30];
6180 rtx stub_fn, insn, addr;
6181 bool lazy_p;
6182
6183 /* If this is a locally-defined and locally-binding function,
6184 avoid the stub by calling the local alias directly. */
6185 if (mips16_local_function_p (fn))
6186 {
6187 *fn_ptr = mips16_local_alias (fn);
6188 return NULL_RTX;
6189 }
6190
6191 /* Create a SYMBOL_REF for the libgcc.a function. */
6192 if (fp_ret_p)
6193 sprintf (buf, "__mips16_call_stub_%s_%d",
6194 mips16_call_stub_mode_suffix (GET_MODE (retval)),
6195 fp_code);
6196 else
6197 sprintf (buf, "__mips16_call_stub_%d", fp_code);
6198 stub_fn = mips16_stub_function (buf);
6199
6200 /* The function uses $2 as an argument, so calls to it
6201 cannot be lazily bound. */
6202 SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6203
6204 /* Load the target function into $2. */
6205 addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6206 lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
6207
6208 /* Emit the call. */
6209 insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
6210 args_size, NULL_RTX, lazy_p);
6211
6212 /* Tell GCC that this call does indeed use the value of $2. */
6213 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
6214
6215 /* If we are handling a floating-point return value, we need to
6216 save $18 in the function prologue. Putting a note on the
6217 call will mean that df_regs_ever_live_p ($18) will be true if the
6218 call is not eliminated, and we can check that in the prologue
6219 code. */
6220 if (fp_ret_p)
6221 CALL_INSN_FUNCTION_USAGE (insn) =
6222 gen_rtx_EXPR_LIST (VOIDmode,
6223 gen_rtx_CLOBBER (VOIDmode,
6224 gen_rtx_REG (word_mode, 18)),
6225 CALL_INSN_FUNCTION_USAGE (insn));
6226
6227 return insn;
6228 }
6229
6230 /* We know the function we are going to call. If we have already
6231 built a stub, we don't need to do anything further. */
6232 fnname = targetm.strip_name_encoding (XSTR (fn, 0));
6233 for (l = mips16_stubs; l != NULL; l = l->next)
6234 if (strcmp (l->name, fnname) == 0)
6235 break;
6236
6237 if (l == NULL)
6238 {
6239 const char *separator;
6240 char *secname, *stubname;
6241 tree stubid, stubdecl;
6242 unsigned int f;
6243
6244 /* If the function does not return in FPRs, the special stub
6245 section is named
6246 .mips16.call.FNNAME
6247
6248 If the function does return in FPRs, the stub section is named
6249 .mips16.call.fp.FNNAME
6250
6251 Build a decl for the stub. */
6252 secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
6253 fnname, NULL));
6254 stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
6255 fnname, NULL));
6256 stubid = get_identifier (stubname);
6257 stubdecl = build_decl (BUILTINS_LOCATION,
6258 FUNCTION_DECL, stubid,
6259 build_function_type (void_type_node, NULL_TREE));
6260 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6261 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6262 RESULT_DECL, NULL_TREE,
6263 void_type_node);
6264
6265 /* Output a comment. */
6266 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
6267 (fp_ret_p
6268 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
6269 : ""),
6270 fnname);
6271 separator = "";
6272 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6273 {
6274 fprintf (asm_out_file, "%s%s", separator,
6275 (f & 3) == 1 ? "float" : "double");
6276 separator = ", ";
6277 }
6278 fprintf (asm_out_file, ")\n");
6279
6280 /* Start the function definition. */
6281 assemble_start_function (stubdecl, stubname);
6282 mips_start_function_definition (stubname, false);
6283
6284 if (!fp_ret_p)
6285 {
6286 /* Load the address of the MIPS16 function into $25. Do this
6287 first so that targets with coprocessor interlocks can use
6288 an MFC1 to fill the delay slot. */
6289 if (TARGET_EXPLICIT_RELOCS)
6290 {
6291 output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
6292 output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
6293 }
6294 else
6295 output_asm_insn ("la\t%^,%0", &fn);
6296 }
6297
6298 /* Move the arguments from general registers to floating-point
6299 registers. */
6300 mips_output_args_xfer (fp_code, 't');
6301
6302 if (!fp_ret_p)
6303 {
6304 /* Jump to the previously-loaded address. */
6305 output_asm_insn ("jr\t%^", NULL);
6306 }
6307 else
6308 {
6309 /* Save the return address in $18 and call the non-MIPS16 function.
6310 The stub's caller knows that $18 might be clobbered, even though
6311 $18 is usually a call-saved register. */
6312 fprintf (asm_out_file, "\tmove\t%s,%s\n",
6313 reg_names[GP_REG_FIRST + 18], reg_names[RETURN_ADDR_REGNUM]);
6314 output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
6315
6316 /* Move the result from floating-point registers to
6317 general registers. */
6318 switch (GET_MODE (retval))
6319 {
6320 case SCmode:
6321 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
6322 TARGET_BIG_ENDIAN
6323 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6324 : FP_REG_FIRST);
6325 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
6326 TARGET_LITTLE_ENDIAN
6327 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6328 : FP_REG_FIRST);
6329 if (GET_MODE (retval) == SCmode && TARGET_64BIT)
6330 {
6331 /* On 64-bit targets, complex floats are returned in
6332 a single GPR, such that "sd" on a suitably-aligned
6333 target would store the value correctly. */
6334 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6335 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6336 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6337 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6338 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
6339 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
6340 fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
6341 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6342 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6343 fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
6344 reg_names[GP_RETURN],
6345 reg_names[GP_RETURN],
6346 reg_names[GP_RETURN + 1]);
6347 }
6348 break;
6349
6350 case SFmode:
6351 mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6352 break;
6353
6354 case DCmode:
6355 mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
6356 FP_REG_FIRST + MAX_FPRS_PER_FMT);
6357 /* Fall though. */
6358 case DFmode:
6359 case V2SFmode:
6360 mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6361 break;
6362
6363 default:
6364 gcc_unreachable ();
6365 }
6366 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
6367 }
6368
6369 #ifdef ASM_DECLARE_FUNCTION_SIZE
6370 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
6371 #endif
6372
6373 mips_end_function_definition (stubname);
6374
6375 /* Record this stub. */
6376 l = XNEW (struct mips16_stub);
6377 l->name = xstrdup (fnname);
6378 l->fp_ret_p = fp_ret_p;
6379 l->next = mips16_stubs;
6380 mips16_stubs = l;
6381 }
6382
6383 /* If we expect a floating-point return value, but we've built a
6384 stub which does not expect one, then we're in trouble. We can't
6385 use the existing stub, because it won't handle the floating-point
6386 value. We can't build a new stub, because the linker won't know
6387 which stub to use for the various calls in this object file.
6388 Fortunately, this case is illegal, since it means that a function
6389 was declared in two different ways in a single compilation. */
6390 if (fp_ret_p && !l->fp_ret_p)
6391 error ("cannot handle inconsistent calls to %qs", fnname);
6392
6393 if (retval == NULL_RTX)
6394 insn = gen_call_internal_direct (fn, args_size);
6395 else
6396 insn = gen_call_value_internal_direct (retval, fn, args_size);
6397 insn = mips_emit_call_insn (insn, fn, fn, false);
6398
6399 /* If we are calling a stub which handles a floating-point return
6400 value, we need to arrange to save $18 in the prologue. We do this
6401 by marking the function call as using the register. The prologue
6402 will later see that it is used, and emit code to save it. */
6403 if (fp_ret_p)
6404 CALL_INSN_FUNCTION_USAGE (insn) =
6405 gen_rtx_EXPR_LIST (VOIDmode,
6406 gen_rtx_CLOBBER (VOIDmode,
6407 gen_rtx_REG (word_mode, 18)),
6408 CALL_INSN_FUNCTION_USAGE (insn));
6409
6410 return insn;
6411 }
6412 \f
6413 /* Expand a call of type TYPE. RESULT is where the result will go (null
6414 for "call"s and "sibcall"s), ADDR is the address of the function,
6415 ARGS_SIZE is the size of the arguments and AUX is the value passed
6416 to us by mips_function_arg. LAZY_P is true if this call already
6417 involves a lazily-bound function address (such as when calling
6418 functions through a MIPS16 hard-float stub).
6419
6420 Return the call itself. */
6421
6422 rtx
6423 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
6424 rtx args_size, rtx aux, bool lazy_p)
6425 {
6426 rtx orig_addr, pattern, insn;
6427 int fp_code;
6428
6429 fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
6430 insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
6431 if (insn)
6432 {
6433 gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
6434 return insn;
6435 }
6436 ;
6437 orig_addr = addr;
6438 if (!call_insn_operand (addr, VOIDmode))
6439 {
6440 if (type == MIPS_CALL_EPILOGUE)
6441 addr = MIPS_EPILOGUE_TEMP (Pmode);
6442 else
6443 addr = gen_reg_rtx (Pmode);
6444 lazy_p |= mips_load_call_address (type, addr, orig_addr);
6445 }
6446
6447 if (result == 0)
6448 {
6449 rtx (*fn) (rtx, rtx);
6450
6451 if (type == MIPS_CALL_SIBCALL)
6452 fn = gen_sibcall_internal;
6453 else
6454 fn = gen_call_internal;
6455
6456 pattern = fn (addr, args_size);
6457 }
6458 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
6459 {
6460 /* Handle return values created by mips_return_fpr_pair. */
6461 rtx (*fn) (rtx, rtx, rtx, rtx);
6462 rtx reg1, reg2;
6463
6464 if (type == MIPS_CALL_SIBCALL)
6465 fn = gen_sibcall_value_multiple_internal;
6466 else
6467 fn = gen_call_value_multiple_internal;
6468
6469 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
6470 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
6471 pattern = fn (reg1, addr, args_size, reg2);
6472 }
6473 else
6474 {
6475 rtx (*fn) (rtx, rtx, rtx);
6476
6477 if (type == MIPS_CALL_SIBCALL)
6478 fn = gen_sibcall_value_internal;
6479 else
6480 fn = gen_call_value_internal;
6481
6482 /* Handle return values created by mips_return_fpr_single. */
6483 if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
6484 result = XEXP (XVECEXP (result, 0, 0), 0);
6485 pattern = fn (result, addr, args_size);
6486 }
6487
6488 return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
6489 }
6490
6491 /* Split call instruction INSN into a $gp-clobbering call and
6492 (where necessary) an instruction to restore $gp from its save slot.
6493 CALL_PATTERN is the pattern of the new call. */
6494
6495 void
6496 mips_split_call (rtx insn, rtx call_pattern)
6497 {
6498 rtx new_insn;
6499
6500 new_insn = emit_call_insn (call_pattern);
6501 CALL_INSN_FUNCTION_USAGE (new_insn)
6502 = copy_rtx (CALL_INSN_FUNCTION_USAGE (insn));
6503 if (!find_reg_note (insn, REG_NORETURN, 0))
6504 /* Pick a temporary register that is suitable for both MIPS16 and
6505 non-MIPS16 code. $4 and $5 are used for returning complex double
6506 values in soft-float code, so $6 is the first suitable candidate. */
6507 mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode, GP_ARG_FIRST + 2));
6508 }
6509
6510 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
6511
6512 static bool
6513 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
6514 {
6515 if (!TARGET_SIBCALLS)
6516 return false;
6517
6518 /* Interrupt handlers need special epilogue code and therefore can't
6519 use sibcalls. */
6520 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
6521 return false;
6522
6523 /* We can't do a sibcall if the called function is a MIPS16 function
6524 because there is no direct "jx" instruction equivalent to "jalx" to
6525 switch the ISA mode. We only care about cases where the sibling
6526 and normal calls would both be direct. */
6527 if (decl
6528 && mips_use_mips16_mode_p (decl)
6529 && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode))
6530 return false;
6531
6532 /* When -minterlink-mips16 is in effect, assume that non-locally-binding
6533 functions could be MIPS16 ones unless an attribute explicitly tells
6534 us otherwise. */
6535 if (TARGET_INTERLINK_MIPS16
6536 && decl
6537 && (DECL_EXTERNAL (decl) || !targetm.binds_local_p (decl))
6538 && !mips_nomips16_decl_p (decl)
6539 && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode))
6540 return false;
6541
6542 /* Otherwise OK. */
6543 return true;
6544 }
6545 \f
6546 /* Emit code to move general operand SRC into condition-code
6547 register DEST given that SCRATCH is a scratch TFmode FPR.
6548 The sequence is:
6549
6550 FP1 = SRC
6551 FP2 = 0.0f
6552 DEST = FP2 < FP1
6553
6554 where FP1 and FP2 are single-precision FPRs taken from SCRATCH. */
6555
6556 void
6557 mips_expand_fcc_reload (rtx dest, rtx src, rtx scratch)
6558 {
6559 rtx fp1, fp2;
6560
6561 /* Change the source to SFmode. */
6562 if (MEM_P (src))
6563 src = adjust_address (src, SFmode, 0);
6564 else if (REG_P (src) || GET_CODE (src) == SUBREG)
6565 src = gen_rtx_REG (SFmode, true_regnum (src));
6566
6567 fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
6568 fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + MAX_FPRS_PER_FMT);
6569
6570 mips_emit_move (copy_rtx (fp1), src);
6571 mips_emit_move (copy_rtx (fp2), CONST0_RTX (SFmode));
6572 emit_insn (gen_slt_sf (dest, fp2, fp1));
6573 }
6574 \f
6575 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
6576 Assume that the areas do not overlap. */
6577
6578 static void
6579 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
6580 {
6581 HOST_WIDE_INT offset, delta;
6582 unsigned HOST_WIDE_INT bits;
6583 int i;
6584 enum machine_mode mode;
6585 rtx *regs;
6586
6587 /* Work out how many bits to move at a time. If both operands have
6588 half-word alignment, it is usually better to move in half words.
6589 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
6590 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
6591 Otherwise move word-sized chunks. */
6592 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
6593 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
6594 bits = BITS_PER_WORD / 2;
6595 else
6596 bits = BITS_PER_WORD;
6597
6598 mode = mode_for_size (bits, MODE_INT, 0);
6599 delta = bits / BITS_PER_UNIT;
6600
6601 /* Allocate a buffer for the temporary registers. */
6602 regs = XALLOCAVEC (rtx, length / delta);
6603
6604 /* Load as many BITS-sized chunks as possible. Use a normal load if
6605 the source has enough alignment, otherwise use left/right pairs. */
6606 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
6607 {
6608 regs[i] = gen_reg_rtx (mode);
6609 if (MEM_ALIGN (src) >= bits)
6610 mips_emit_move (regs[i], adjust_address (src, mode, offset));
6611 else
6612 {
6613 rtx part = adjust_address (src, BLKmode, offset);
6614 if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0))
6615 gcc_unreachable ();
6616 }
6617 }
6618
6619 /* Copy the chunks to the destination. */
6620 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
6621 if (MEM_ALIGN (dest) >= bits)
6622 mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
6623 else
6624 {
6625 rtx part = adjust_address (dest, BLKmode, offset);
6626 if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
6627 gcc_unreachable ();
6628 }
6629
6630 /* Mop up any left-over bytes. */
6631 if (offset < length)
6632 {
6633 src = adjust_address (src, BLKmode, offset);
6634 dest = adjust_address (dest, BLKmode, offset);
6635 move_by_pieces (dest, src, length - offset,
6636 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
6637 }
6638 }
6639
6640 /* Helper function for doing a loop-based block operation on memory
6641 reference MEM. Each iteration of the loop will operate on LENGTH
6642 bytes of MEM.
6643
6644 Create a new base register for use within the loop and point it to
6645 the start of MEM. Create a new memory reference that uses this
6646 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
6647
6648 static void
6649 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
6650 rtx *loop_reg, rtx *loop_mem)
6651 {
6652 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
6653
6654 /* Although the new mem does not refer to a known location,
6655 it does keep up to LENGTH bytes of alignment. */
6656 *loop_mem = change_address (mem, BLKmode, *loop_reg);
6657 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
6658 }
6659
6660 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
6661 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
6662 the memory regions do not overlap. */
6663
6664 static void
6665 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
6666 HOST_WIDE_INT bytes_per_iter)
6667 {
6668 rtx label, src_reg, dest_reg, final_src, test;
6669 HOST_WIDE_INT leftover;
6670
6671 leftover = length % bytes_per_iter;
6672 length -= leftover;
6673
6674 /* Create registers and memory references for use within the loop. */
6675 mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
6676 mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
6677
6678 /* Calculate the value that SRC_REG should have after the last iteration
6679 of the loop. */
6680 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
6681 0, 0, OPTAB_WIDEN);
6682
6683 /* Emit the start of the loop. */
6684 label = gen_label_rtx ();
6685 emit_label (label);
6686
6687 /* Emit the loop body. */
6688 mips_block_move_straight (dest, src, bytes_per_iter);
6689
6690 /* Move on to the next block. */
6691 mips_emit_move (src_reg, plus_constant (src_reg, bytes_per_iter));
6692 mips_emit_move (dest_reg, plus_constant (dest_reg, bytes_per_iter));
6693
6694 /* Emit the loop condition. */
6695 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
6696 if (Pmode == DImode)
6697 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
6698 else
6699 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
6700
6701 /* Mop up any left-over bytes. */
6702 if (leftover)
6703 mips_block_move_straight (dest, src, leftover);
6704 }
6705
6706 /* Expand a movmemsi instruction, which copies LENGTH bytes from
6707 memory reference SRC to memory reference DEST. */
6708
6709 bool
6710 mips_expand_block_move (rtx dest, rtx src, rtx length)
6711 {
6712 if (CONST_INT_P (length))
6713 {
6714 if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
6715 {
6716 mips_block_move_straight (dest, src, INTVAL (length));
6717 return true;
6718 }
6719 else if (optimize)
6720 {
6721 mips_block_move_loop (dest, src, INTVAL (length),
6722 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
6723 return true;
6724 }
6725 }
6726 return false;
6727 }
6728 \f
6729 /* Expand a loop of synci insns for the address range [BEGIN, END). */
6730
6731 void
6732 mips_expand_synci_loop (rtx begin, rtx end)
6733 {
6734 rtx inc, label, end_label, cmp_result, mask, length;
6735
6736 /* Create end_label. */
6737 end_label = gen_label_rtx ();
6738
6739 /* Check if begin equals end. */
6740 cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
6741 emit_jump_insn (gen_condjump (cmp_result, end_label));
6742
6743 /* Load INC with the cache line size (rdhwr INC,$1). */
6744 inc = gen_reg_rtx (Pmode);
6745 emit_insn (Pmode == SImode
6746 ? gen_rdhwr_synci_step_si (inc)
6747 : gen_rdhwr_synci_step_di (inc));
6748
6749 /* Check if inc is 0. */
6750 cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
6751 emit_jump_insn (gen_condjump (cmp_result, end_label));
6752
6753 /* Calculate mask. */
6754 mask = mips_force_unary (Pmode, NEG, inc);
6755
6756 /* Mask out begin by mask. */
6757 begin = mips_force_binary (Pmode, AND, begin, mask);
6758
6759 /* Calculate length. */
6760 length = mips_force_binary (Pmode, MINUS, end, begin);
6761
6762 /* Loop back to here. */
6763 label = gen_label_rtx ();
6764 emit_label (label);
6765
6766 emit_insn (gen_synci (begin));
6767
6768 /* Update length. */
6769 mips_emit_binary (MINUS, length, length, inc);
6770
6771 /* Update begin. */
6772 mips_emit_binary (PLUS, begin, begin, inc);
6773
6774 /* Check if length is greater than 0. */
6775 cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
6776 emit_jump_insn (gen_condjump (cmp_result, label));
6777
6778 emit_label (end_label);
6779 }
6780 \f
6781 /* Expand a QI or HI mode atomic memory operation.
6782
6783 GENERATOR contains a pointer to the gen_* function that generates
6784 the SI mode underlying atomic operation using masks that we
6785 calculate.
6786
6787 RESULT is the return register for the operation. Its value is NULL
6788 if unused.
6789
6790 MEM is the location of the atomic access.
6791
6792 OLDVAL is the first operand for the operation.
6793
6794 NEWVAL is the optional second operand for the operation. Its value
6795 is NULL if unused. */
6796
6797 void
6798 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
6799 rtx result, rtx mem, rtx oldval, rtx newval)
6800 {
6801 rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
6802 rtx unshifted_mask_reg, mask, inverted_mask, si_op;
6803 rtx res = NULL;
6804 enum machine_mode mode;
6805
6806 mode = GET_MODE (mem);
6807
6808 /* Compute the address of the containing SImode value. */
6809 orig_addr = force_reg (Pmode, XEXP (mem, 0));
6810 memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
6811 force_reg (Pmode, GEN_INT (-4)));
6812
6813 /* Create a memory reference for it. */
6814 memsi = gen_rtx_MEM (SImode, memsi_addr);
6815 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
6816 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
6817
6818 /* Work out the byte offset of the QImode or HImode value,
6819 counting from the least significant byte. */
6820 shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
6821 if (TARGET_BIG_ENDIAN)
6822 mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
6823
6824 /* Multiply by eight to convert the shift value from bytes to bits. */
6825 mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
6826
6827 /* Make the final shift an SImode value, so that it can be used in
6828 SImode operations. */
6829 shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
6830
6831 /* Set MASK to an inclusive mask of the QImode or HImode value. */
6832 unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
6833 unshifted_mask_reg = force_reg (SImode, unshifted_mask);
6834 mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
6835
6836 /* Compute the equivalent exclusive mask. */
6837 inverted_mask = gen_reg_rtx (SImode);
6838 emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
6839 gen_rtx_NOT (SImode, mask)));
6840
6841 /* Shift the old value into place. */
6842 if (oldval != const0_rtx)
6843 {
6844 oldval = convert_modes (SImode, mode, oldval, true);
6845 oldval = force_reg (SImode, oldval);
6846 oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
6847 }
6848
6849 /* Do the same for the new value. */
6850 if (newval && newval != const0_rtx)
6851 {
6852 newval = convert_modes (SImode, mode, newval, true);
6853 newval = force_reg (SImode, newval);
6854 newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
6855 }
6856
6857 /* Do the SImode atomic access. */
6858 if (result)
6859 res = gen_reg_rtx (SImode);
6860 if (newval)
6861 si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
6862 else if (result)
6863 si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
6864 else
6865 si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
6866
6867 emit_insn (si_op);
6868
6869 if (result)
6870 {
6871 /* Shift and convert the result. */
6872 mips_emit_binary (AND, res, res, mask);
6873 mips_emit_binary (LSHIFTRT, res, res, shiftsi);
6874 mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
6875 }
6876 }
6877
6878 /* Return true if it is possible to use left/right accesses for a
6879 bitfield of WIDTH bits starting BITPOS bits into *OP. When
6880 returning true, update *OP, *LEFT and *RIGHT as follows:
6881
6882 *OP is a BLKmode reference to the whole field.
6883
6884 *LEFT is a QImode reference to the first byte if big endian or
6885 the last byte if little endian. This address can be used in the
6886 left-side instructions (LWL, SWL, LDL, SDL).
6887
6888 *RIGHT is a QImode reference to the opposite end of the field and
6889 can be used in the patterning right-side instruction. */
6890
6891 static bool
6892 mips_get_unaligned_mem (rtx *op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
6893 rtx *left, rtx *right)
6894 {
6895 rtx first, last;
6896
6897 /* Check that the operand really is a MEM. Not all the extv and
6898 extzv predicates are checked. */
6899 if (!MEM_P (*op))
6900 return false;
6901
6902 /* Check that the size is valid. */
6903 if (width != 32 && (!TARGET_64BIT || width != 64))
6904 return false;
6905
6906 /* We can only access byte-aligned values. Since we are always passed
6907 a reference to the first byte of the field, it is not necessary to
6908 do anything with BITPOS after this check. */
6909 if (bitpos % BITS_PER_UNIT != 0)
6910 return false;
6911
6912 /* Reject aligned bitfields: we want to use a normal load or store
6913 instead of a left/right pair. */
6914 if (MEM_ALIGN (*op) >= width)
6915 return false;
6916
6917 /* Adjust *OP to refer to the whole field. This also has the effect
6918 of legitimizing *OP's address for BLKmode, possibly simplifying it. */
6919 *op = adjust_address (*op, BLKmode, 0);
6920 set_mem_size (*op, GEN_INT (width / BITS_PER_UNIT));
6921
6922 /* Get references to both ends of the field. We deliberately don't
6923 use the original QImode *OP for FIRST since the new BLKmode one
6924 might have a simpler address. */
6925 first = adjust_address (*op, QImode, 0);
6926 last = adjust_address (*op, QImode, width / BITS_PER_UNIT - 1);
6927
6928 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
6929 correspond to the MSB and RIGHT to the LSB. */
6930 if (TARGET_BIG_ENDIAN)
6931 *left = first, *right = last;
6932 else
6933 *left = last, *right = first;
6934
6935 return true;
6936 }
6937
6938 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
6939 DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
6940 the operation is the equivalent of:
6941
6942 (set DEST (*_extract SRC WIDTH BITPOS))
6943
6944 Return true on success. */
6945
6946 bool
6947 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
6948 HOST_WIDE_INT bitpos)
6949 {
6950 rtx left, right, temp;
6951
6952 /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
6953 be a paradoxical word_mode subreg. This is the only case in which
6954 we allow the destination to be larger than the source. */
6955 if (GET_CODE (dest) == SUBREG
6956 && GET_MODE (dest) == DImode
6957 && GET_MODE (SUBREG_REG (dest)) == SImode)
6958 dest = SUBREG_REG (dest);
6959
6960 /* After the above adjustment, the destination must be the same
6961 width as the source. */
6962 if (GET_MODE_BITSIZE (GET_MODE (dest)) != width)
6963 return false;
6964
6965 if (!mips_get_unaligned_mem (&src, width, bitpos, &left, &right))
6966 return false;
6967
6968 temp = gen_reg_rtx (GET_MODE (dest));
6969 if (GET_MODE (dest) == DImode)
6970 {
6971 emit_insn (gen_mov_ldl (temp, src, left));
6972 emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
6973 }
6974 else
6975 {
6976 emit_insn (gen_mov_lwl (temp, src, left));
6977 emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
6978 }
6979 return true;
6980 }
6981
6982 /* Try to use left/right stores to expand an "ins" pattern. DEST, WIDTH,
6983 BITPOS and SRC are the operands passed to the expander; the operation
6984 is the equivalent of:
6985
6986 (set (zero_extract DEST WIDTH BITPOS) SRC)
6987
6988 Return true on success. */
6989
6990 bool
6991 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
6992 HOST_WIDE_INT bitpos)
6993 {
6994 rtx left, right;
6995 enum machine_mode mode;
6996
6997 if (!mips_get_unaligned_mem (&dest, width, bitpos, &left, &right))
6998 return false;
6999
7000 mode = mode_for_size (width, MODE_INT, 0);
7001 src = gen_lowpart (mode, src);
7002 if (mode == DImode)
7003 {
7004 emit_insn (gen_mov_sdl (dest, src, left));
7005 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7006 }
7007 else
7008 {
7009 emit_insn (gen_mov_swl (dest, src, left));
7010 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7011 }
7012 return true;
7013 }
7014
7015 /* Return true if X is a MEM with the same size as MODE. */
7016
7017 bool
7018 mips_mem_fits_mode_p (enum machine_mode mode, rtx x)
7019 {
7020 rtx size;
7021
7022 if (!MEM_P (x))
7023 return false;
7024
7025 size = MEM_SIZE (x);
7026 return size && INTVAL (size) == GET_MODE_SIZE (mode);
7027 }
7028
7029 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7030 source of an "ext" instruction or the destination of an "ins"
7031 instruction. OP must be a register operand and the following
7032 conditions must hold:
7033
7034 0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7035 0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7036 0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7037
7038 Also reject lengths equal to a word as they are better handled
7039 by the move patterns. */
7040
7041 bool
7042 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7043 {
7044 if (!ISA_HAS_EXT_INS
7045 || !register_operand (op, VOIDmode)
7046 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7047 return false;
7048
7049 if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7050 return false;
7051
7052 if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7053 return false;
7054
7055 return true;
7056 }
7057
7058 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7059 operation if MAXLEN is the maxium length of consecutive bits that
7060 can make up MASK. MODE is the mode of the operation. See
7061 mask_low_and_shift_len for the actual definition. */
7062
7063 bool
7064 mask_low_and_shift_p (enum machine_mode mode, rtx mask, rtx shift, int maxlen)
7065 {
7066 return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7067 }
7068
7069 /* Return true iff OP1 and OP2 are valid operands together for the
7070 *and<MODE>3 and *and<MODE>3_mips16 patterns. For the cases to consider,
7071 see the table in the comment before the pattern. */
7072
7073 bool
7074 and_operands_ok (enum machine_mode mode, rtx op1, rtx op2)
7075 {
7076 return (memory_operand (op1, mode)
7077 ? and_load_operand (op2, mode)
7078 : and_reg_operand (op2, mode));
7079 }
7080
7081 /* The canonical form of a mask-low-and-shift-left operation is
7082 (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7083 cleared. Thus we need to shift MASK to the right before checking if it
7084 is a valid mask value. MODE is the mode of the operation. If true
7085 return the length of the mask, otherwise return -1. */
7086
7087 int
7088 mask_low_and_shift_len (enum machine_mode mode, rtx mask, rtx shift)
7089 {
7090 HOST_WIDE_INT shval;
7091
7092 shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7093 return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7094 }
7095 \f
7096 /* Return true if -msplit-addresses is selected and should be honored.
7097
7098 -msplit-addresses is a half-way house between explicit relocations
7099 and the traditional assembler macros. It can split absolute 32-bit
7100 symbolic constants into a high/lo_sum pair but uses macros for other
7101 sorts of access.
7102
7103 Like explicit relocation support for REL targets, it relies
7104 on GNU extensions in the assembler and the linker.
7105
7106 Although this code should work for -O0, it has traditionally
7107 been treated as an optimization. */
7108
7109 static bool
7110 mips_split_addresses_p (void)
7111 {
7112 return (TARGET_SPLIT_ADDRESSES
7113 && optimize
7114 && !TARGET_MIPS16
7115 && !flag_pic
7116 && !ABI_HAS_64BIT_SYMBOLS);
7117 }
7118
7119 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs. */
7120
7121 static void
7122 mips_init_relocs (void)
7123 {
7124 memset (mips_split_p, '\0', sizeof (mips_split_p));
7125 memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
7126 memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
7127 memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
7128
7129 if (ABI_HAS_64BIT_SYMBOLS)
7130 {
7131 if (TARGET_EXPLICIT_RELOCS)
7132 {
7133 mips_split_p[SYMBOL_64_HIGH] = true;
7134 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
7135 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
7136
7137 mips_split_p[SYMBOL_64_MID] = true;
7138 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
7139 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
7140
7141 mips_split_p[SYMBOL_64_LOW] = true;
7142 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
7143 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
7144
7145 mips_split_p[SYMBOL_ABSOLUTE] = true;
7146 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7147 }
7148 }
7149 else
7150 {
7151 if (TARGET_EXPLICIT_RELOCS || mips_split_addresses_p () || TARGET_MIPS16)
7152 {
7153 mips_split_p[SYMBOL_ABSOLUTE] = true;
7154 mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
7155 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7156
7157 mips_lo_relocs[SYMBOL_32_HIGH] = "%hi(";
7158 }
7159 }
7160
7161 if (TARGET_MIPS16)
7162 {
7163 /* The high part is provided by a pseudo copy of $gp. */
7164 mips_split_p[SYMBOL_GP_RELATIVE] = true;
7165 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
7166 }
7167 else if (TARGET_EXPLICIT_RELOCS)
7168 /* Small data constants are kept whole until after reload,
7169 then lowered by mips_rewrite_small_data. */
7170 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
7171
7172 if (TARGET_EXPLICIT_RELOCS)
7173 {
7174 mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
7175 if (TARGET_NEWABI)
7176 {
7177 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
7178 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
7179 }
7180 else
7181 {
7182 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
7183 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
7184 }
7185 if (TARGET_MIPS16)
7186 /* Expose the use of $28 as soon as possible. */
7187 mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
7188
7189 if (TARGET_XGOT)
7190 {
7191 /* The HIGH and LO_SUM are matched by special .md patterns. */
7192 mips_split_p[SYMBOL_GOT_DISP] = true;
7193
7194 mips_split_p[SYMBOL_GOTOFF_DISP] = true;
7195 mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
7196 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
7197
7198 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
7199 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
7200 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
7201 }
7202 else
7203 {
7204 if (TARGET_NEWABI)
7205 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
7206 else
7207 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
7208 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
7209 if (TARGET_MIPS16)
7210 /* Expose the use of $28 as soon as possible. */
7211 mips_split_p[SYMBOL_GOT_DISP] = true;
7212 }
7213 }
7214
7215 if (TARGET_NEWABI)
7216 {
7217 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
7218 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
7219 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
7220 }
7221
7222 mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
7223 mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
7224
7225 mips_split_p[SYMBOL_DTPREL] = true;
7226 mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
7227 mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
7228
7229 mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
7230
7231 mips_split_p[SYMBOL_TPREL] = true;
7232 mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
7233 mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
7234
7235 mips_lo_relocs[SYMBOL_HALF] = "%half(";
7236 }
7237
7238 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
7239 in context CONTEXT. RELOCS is the array of relocations to use. */
7240
7241 static void
7242 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
7243 const char **relocs)
7244 {
7245 enum mips_symbol_type symbol_type;
7246 const char *p;
7247
7248 symbol_type = mips_classify_symbolic_expression (op, context);
7249 gcc_assert (relocs[symbol_type]);
7250
7251 fputs (relocs[symbol_type], file);
7252 output_addr_const (file, mips_strip_unspec_address (op));
7253 for (p = relocs[symbol_type]; *p != 0; p++)
7254 if (*p == '(')
7255 fputc (')', file);
7256 }
7257
7258 /* Start a new block with the given asm switch enabled. If we need
7259 to print a directive, emit PREFIX before it and SUFFIX after it. */
7260
7261 static void
7262 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
7263 const char *prefix, const char *suffix)
7264 {
7265 if (asm_switch->nesting_level == 0)
7266 fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
7267 asm_switch->nesting_level++;
7268 }
7269
7270 /* Likewise, but end a block. */
7271
7272 static void
7273 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
7274 const char *prefix, const char *suffix)
7275 {
7276 gcc_assert (asm_switch->nesting_level);
7277 asm_switch->nesting_level--;
7278 if (asm_switch->nesting_level == 0)
7279 fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
7280 }
7281
7282 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
7283 that either print a complete line or print nothing. */
7284
7285 void
7286 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
7287 {
7288 mips_push_asm_switch_1 (asm_switch, "\t", "\n");
7289 }
7290
7291 void
7292 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
7293 {
7294 mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
7295 }
7296
7297 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
7298 The punctuation characters are:
7299
7300 '(' Start a nested ".set noreorder" block.
7301 ')' End a nested ".set noreorder" block.
7302 '[' Start a nested ".set noat" block.
7303 ']' End a nested ".set noat" block.
7304 '<' Start a nested ".set nomacro" block.
7305 '>' End a nested ".set nomacro" block.
7306 '*' Behave like %(%< if generating a delayed-branch sequence.
7307 '#' Print a nop if in a ".set noreorder" block.
7308 '/' Like '#', but do nothing within a delayed-branch sequence.
7309 '?' Print "l" if mips_branch_likely is true
7310 '~' Print a nop if mips_branch_likely is true
7311 '.' Print the name of the register with a hard-wired zero (zero or $0).
7312 '@' Print the name of the assembler temporary register (at or $1).
7313 '^' Print the name of the pic call-through register (t9 or $25).
7314 '+' Print the name of the gp register (usually gp or $28).
7315 '$' Print the name of the stack pointer register (sp or $29).
7316
7317 See also mips_init_print_operand_pucnt. */
7318
7319 static void
7320 mips_print_operand_punctuation (FILE *file, int ch)
7321 {
7322 switch (ch)
7323 {
7324 case '(':
7325 mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
7326 break;
7327
7328 case ')':
7329 mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
7330 break;
7331
7332 case '[':
7333 mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
7334 break;
7335
7336 case ']':
7337 mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
7338 break;
7339
7340 case '<':
7341 mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
7342 break;
7343
7344 case '>':
7345 mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
7346 break;
7347
7348 case '*':
7349 if (final_sequence != 0)
7350 {
7351 mips_print_operand_punctuation (file, '(');
7352 mips_print_operand_punctuation (file, '<');
7353 }
7354 break;
7355
7356 case '#':
7357 if (mips_noreorder.nesting_level > 0)
7358 fputs ("\n\tnop", file);
7359 break;
7360
7361 case '/':
7362 /* Print an extra newline so that the delayed insn is separated
7363 from the following ones. This looks neater and is consistent
7364 with non-nop delayed sequences. */
7365 if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
7366 fputs ("\n\tnop\n", file);
7367 break;
7368
7369 case '?':
7370 if (mips_branch_likely)
7371 putc ('l', file);
7372 break;
7373
7374 case '~':
7375 if (mips_branch_likely)
7376 fputs ("\n\tnop", file);
7377 break;
7378
7379 case '.':
7380 fputs (reg_names[GP_REG_FIRST + 0], file);
7381 break;
7382
7383 case '@':
7384 fputs (reg_names[AT_REGNUM], file);
7385 break;
7386
7387 case '^':
7388 fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
7389 break;
7390
7391 case '+':
7392 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
7393 break;
7394
7395 case '$':
7396 fputs (reg_names[STACK_POINTER_REGNUM], file);
7397 break;
7398
7399 default:
7400 gcc_unreachable ();
7401 break;
7402 }
7403 }
7404
7405 /* Initialize mips_print_operand_punct. */
7406
7407 static void
7408 mips_init_print_operand_punct (void)
7409 {
7410 const char *p;
7411
7412 for (p = "()[]<>*#/?~.@^+$"; *p; p++)
7413 mips_print_operand_punct[(unsigned char) *p] = true;
7414 }
7415
7416 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
7417 associated with condition CODE. Print the condition part of the
7418 opcode to FILE. */
7419
7420 static void
7421 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
7422 {
7423 switch (code)
7424 {
7425 case EQ:
7426 case NE:
7427 case GT:
7428 case GE:
7429 case LT:
7430 case LE:
7431 case GTU:
7432 case GEU:
7433 case LTU:
7434 case LEU:
7435 /* Conveniently, the MIPS names for these conditions are the same
7436 as their RTL equivalents. */
7437 fputs (GET_RTX_NAME (code), file);
7438 break;
7439
7440 default:
7441 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
7442 break;
7443 }
7444 }
7445
7446 /* Likewise floating-point branches. */
7447
7448 static void
7449 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
7450 {
7451 switch (code)
7452 {
7453 case EQ:
7454 fputs ("c1f", file);
7455 break;
7456
7457 case NE:
7458 fputs ("c1t", file);
7459 break;
7460
7461 default:
7462 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
7463 break;
7464 }
7465 }
7466
7467 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
7468
7469 static bool
7470 mips_print_operand_punct_valid_p (unsigned char code)
7471 {
7472 return mips_print_operand_punct[code];
7473 }
7474
7475 /* Implement TARGET_PRINT_OPERAND. The MIPS-specific operand codes are:
7476
7477 'X' Print CONST_INT OP in hexadecimal format.
7478 'x' Print the low 16 bits of CONST_INT OP in hexadecimal format.
7479 'd' Print CONST_INT OP in decimal.
7480 'm' Print one less than CONST_INT OP in decimal.
7481 'h' Print the high-part relocation associated with OP, after stripping
7482 any outermost HIGH.
7483 'R' Print the low-part relocation associated with OP.
7484 'C' Print the integer branch condition for comparison OP.
7485 'N' Print the inverse of the integer branch condition for comparison OP.
7486 'F' Print the FPU branch condition for comparison OP.
7487 'W' Print the inverse of the FPU branch condition for comparison OP.
7488 'T' Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
7489 'z' for (eq:?I ...), 'n' for (ne:?I ...).
7490 't' Like 'T', but with the EQ/NE cases reversed
7491 'Y' Print mips_fp_conditions[INTVAL (OP)]
7492 'Z' Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
7493 'q' Print a DSP accumulator register.
7494 'D' Print the second part of a double-word register or memory operand.
7495 'L' Print the low-order register in a double-word register operand.
7496 'M' Print high-order register in a double-word register operand.
7497 'z' Print $0 if OP is zero, otherwise print OP normally. */
7498
7499 static void
7500 mips_print_operand (FILE *file, rtx op, int letter)
7501 {
7502 enum rtx_code code;
7503
7504 if (mips_print_operand_punct_valid_p (letter))
7505 {
7506 mips_print_operand_punctuation (file, letter);
7507 return;
7508 }
7509
7510 gcc_assert (op);
7511 code = GET_CODE (op);
7512
7513 switch (letter)
7514 {
7515 case 'X':
7516 if (CONST_INT_P (op))
7517 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
7518 else
7519 output_operand_lossage ("invalid use of '%%%c'", letter);
7520 break;
7521
7522 case 'x':
7523 if (CONST_INT_P (op))
7524 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
7525 else
7526 output_operand_lossage ("invalid use of '%%%c'", letter);
7527 break;
7528
7529 case 'd':
7530 if (CONST_INT_P (op))
7531 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
7532 else
7533 output_operand_lossage ("invalid use of '%%%c'", letter);
7534 break;
7535
7536 case 'm':
7537 if (CONST_INT_P (op))
7538 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
7539 else
7540 output_operand_lossage ("invalid use of '%%%c'", letter);
7541 break;
7542
7543 case 'h':
7544 if (code == HIGH)
7545 op = XEXP (op, 0);
7546 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
7547 break;
7548
7549 case 'R':
7550 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
7551 break;
7552
7553 case 'C':
7554 mips_print_int_branch_condition (file, code, letter);
7555 break;
7556
7557 case 'N':
7558 mips_print_int_branch_condition (file, reverse_condition (code), letter);
7559 break;
7560
7561 case 'F':
7562 mips_print_float_branch_condition (file, code, letter);
7563 break;
7564
7565 case 'W':
7566 mips_print_float_branch_condition (file, reverse_condition (code),
7567 letter);
7568 break;
7569
7570 case 'T':
7571 case 't':
7572 {
7573 int truth = (code == NE) == (letter == 'T');
7574 fputc ("zfnt"[truth * 2 + (GET_MODE (op) == CCmode)], file);
7575 }
7576 break;
7577
7578 case 'Y':
7579 if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
7580 fputs (mips_fp_conditions[UINTVAL (op)], file);
7581 else
7582 output_operand_lossage ("'%%%c' is not a valid operand prefix",
7583 letter);
7584 break;
7585
7586 case 'Z':
7587 if (ISA_HAS_8CC)
7588 {
7589 mips_print_operand (file, op, 0);
7590 fputc (',', file);
7591 }
7592 break;
7593
7594 case 'q':
7595 if (code == REG && MD_REG_P (REGNO (op)))
7596 fprintf (file, "$ac0");
7597 else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
7598 fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
7599 else
7600 output_operand_lossage ("invalid use of '%%%c'", letter);
7601 break;
7602
7603 default:
7604 switch (code)
7605 {
7606 case REG:
7607 {
7608 unsigned int regno = REGNO (op);
7609 if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
7610 || (letter == 'L' && TARGET_BIG_ENDIAN)
7611 || letter == 'D')
7612 regno++;
7613 else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
7614 output_operand_lossage ("invalid use of '%%%c'", letter);
7615 /* We need to print $0 .. $31 for COP0 registers. */
7616 if (COP0_REG_P (regno))
7617 fprintf (file, "$%s", &reg_names[regno][4]);
7618 else
7619 fprintf (file, "%s", reg_names[regno]);
7620 }
7621 break;
7622
7623 case MEM:
7624 if (letter == 'D')
7625 output_address (plus_constant (XEXP (op, 0), 4));
7626 else if (letter && letter != 'z')
7627 output_operand_lossage ("invalid use of '%%%c'", letter);
7628 else
7629 output_address (XEXP (op, 0));
7630 break;
7631
7632 default:
7633 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
7634 fputs (reg_names[GP_REG_FIRST], file);
7635 else if (letter && letter != 'z')
7636 output_operand_lossage ("invalid use of '%%%c'", letter);
7637 else if (CONST_GP_P (op))
7638 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
7639 else
7640 output_addr_const (file, mips_strip_unspec_address (op));
7641 break;
7642 }
7643 }
7644 }
7645
7646 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
7647
7648 static void
7649 mips_print_operand_address (FILE *file, rtx x)
7650 {
7651 struct mips_address_info addr;
7652
7653 if (mips_classify_address (&addr, x, word_mode, true))
7654 switch (addr.type)
7655 {
7656 case ADDRESS_REG:
7657 mips_print_operand (file, addr.offset, 0);
7658 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
7659 return;
7660
7661 case ADDRESS_LO_SUM:
7662 mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
7663 mips_lo_relocs);
7664 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
7665 return;
7666
7667 case ADDRESS_CONST_INT:
7668 output_addr_const (file, x);
7669 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
7670 return;
7671
7672 case ADDRESS_SYMBOLIC:
7673 output_addr_const (file, mips_strip_unspec_address (x));
7674 return;
7675 }
7676 gcc_unreachable ();
7677 }
7678 \f
7679 /* Implement TARGET_ENCODE_SECTION_INFO. */
7680
7681 static void
7682 mips_encode_section_info (tree decl, rtx rtl, int first)
7683 {
7684 default_encode_section_info (decl, rtl, first);
7685
7686 if (TREE_CODE (decl) == FUNCTION_DECL)
7687 {
7688 rtx symbol = XEXP (rtl, 0);
7689 tree type = TREE_TYPE (decl);
7690
7691 /* Encode whether the symbol is short or long. */
7692 if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
7693 || mips_far_type_p (type))
7694 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
7695 }
7696 }
7697
7698 /* Implement TARGET_SELECT_RTX_SECTION. */
7699
7700 static section *
7701 mips_select_rtx_section (enum machine_mode mode, rtx x,
7702 unsigned HOST_WIDE_INT align)
7703 {
7704 /* ??? Consider using mergeable small data sections. */
7705 if (mips_rtx_constant_in_small_data_p (mode))
7706 return get_named_section (NULL, ".sdata", 0);
7707
7708 return default_elf_select_rtx_section (mode, x, align);
7709 }
7710
7711 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
7712
7713 The complication here is that, with the combination TARGET_ABICALLS
7714 && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
7715 absolute addresses, and should therefore not be included in the
7716 read-only part of a DSO. Handle such cases by selecting a normal
7717 data section instead of a read-only one. The logic apes that in
7718 default_function_rodata_section. */
7719
7720 static section *
7721 mips_function_rodata_section (tree decl)
7722 {
7723 if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
7724 return default_function_rodata_section (decl);
7725
7726 if (decl && DECL_SECTION_NAME (decl))
7727 {
7728 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
7729 if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
7730 {
7731 char *rname = ASTRDUP (name);
7732 rname[14] = 'd';
7733 return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
7734 }
7735 else if (flag_function_sections
7736 && flag_data_sections
7737 && strncmp (name, ".text.", 6) == 0)
7738 {
7739 char *rname = ASTRDUP (name);
7740 memcpy (rname + 1, "data", 4);
7741 return get_section (rname, SECTION_WRITE, decl);
7742 }
7743 }
7744 return data_section;
7745 }
7746
7747 /* Implement TARGET_IN_SMALL_DATA_P. */
7748
7749 static bool
7750 mips_in_small_data_p (const_tree decl)
7751 {
7752 unsigned HOST_WIDE_INT size;
7753
7754 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
7755 return false;
7756
7757 /* We don't yet generate small-data references for -mabicalls
7758 or VxWorks RTP code. See the related -G handling in
7759 mips_override_options. */
7760 if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
7761 return false;
7762
7763 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
7764 {
7765 const char *name;
7766
7767 /* Reject anything that isn't in a known small-data section. */
7768 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
7769 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
7770 return false;
7771
7772 /* If a symbol is defined externally, the assembler will use the
7773 usual -G rules when deciding how to implement macros. */
7774 if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
7775 return true;
7776 }
7777 else if (TARGET_EMBEDDED_DATA)
7778 {
7779 /* Don't put constants into the small data section: we want them
7780 to be in ROM rather than RAM. */
7781 if (TREE_CODE (decl) != VAR_DECL)
7782 return false;
7783
7784 if (TREE_READONLY (decl)
7785 && !TREE_SIDE_EFFECTS (decl)
7786 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
7787 return false;
7788 }
7789
7790 /* Enforce -mlocal-sdata. */
7791 if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
7792 return false;
7793
7794 /* Enforce -mextern-sdata. */
7795 if (!TARGET_EXTERN_SDATA && DECL_P (decl))
7796 {
7797 if (DECL_EXTERNAL (decl))
7798 return false;
7799 if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
7800 return false;
7801 }
7802
7803 /* We have traditionally not treated zero-sized objects as small data,
7804 so this is now effectively part of the ABI. */
7805 size = int_size_in_bytes (TREE_TYPE (decl));
7806 return size > 0 && size <= mips_small_data_threshold;
7807 }
7808
7809 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P. We don't want to use
7810 anchors for small data: the GP register acts as an anchor in that
7811 case. We also don't want to use them for PC-relative accesses,
7812 where the PC acts as an anchor. */
7813
7814 static bool
7815 mips_use_anchors_for_symbol_p (const_rtx symbol)
7816 {
7817 switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
7818 {
7819 case SYMBOL_PC_RELATIVE:
7820 case SYMBOL_GP_RELATIVE:
7821 return false;
7822
7823 default:
7824 return default_use_anchors_for_symbol_p (symbol);
7825 }
7826 }
7827 \f
7828 /* The MIPS debug format wants all automatic variables and arguments
7829 to be in terms of the virtual frame pointer (stack pointer before
7830 any adjustment in the function), while the MIPS 3.0 linker wants
7831 the frame pointer to be the stack pointer after the initial
7832 adjustment. So, we do the adjustment here. The arg pointer (which
7833 is eliminated) points to the virtual frame pointer, while the frame
7834 pointer (which may be eliminated) points to the stack pointer after
7835 the initial adjustments. */
7836
7837 HOST_WIDE_INT
7838 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
7839 {
7840 rtx offset2 = const0_rtx;
7841 rtx reg = eliminate_constant_term (addr, &offset2);
7842
7843 if (offset == 0)
7844 offset = INTVAL (offset2);
7845
7846 if (reg == stack_pointer_rtx
7847 || reg == frame_pointer_rtx
7848 || reg == hard_frame_pointer_rtx)
7849 {
7850 offset -= cfun->machine->frame.total_size;
7851 if (reg == hard_frame_pointer_rtx)
7852 offset += cfun->machine->frame.hard_frame_pointer_offset;
7853 }
7854
7855 /* sdbout_parms does not want this to crash for unrecognized cases. */
7856 #if 0
7857 else if (reg != arg_pointer_rtx)
7858 fatal_insn ("mips_debugger_offset called with non stack/frame/arg pointer",
7859 addr);
7860 #endif
7861
7862 return offset;
7863 }
7864 \f
7865 /* Implement ASM_OUTPUT_EXTERNAL. */
7866
7867 void
7868 mips_output_external (FILE *file, tree decl, const char *name)
7869 {
7870 default_elf_asm_output_external (file, decl, name);
7871
7872 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7873 set in order to avoid putting out names that are never really
7874 used. */
7875 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
7876 {
7877 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
7878 {
7879 /* When using assembler macros, emit .extern directives for
7880 all small-data externs so that the assembler knows how
7881 big they are.
7882
7883 In most cases it would be safe (though pointless) to emit
7884 .externs for other symbols too. One exception is when an
7885 object is within the -G limit but declared by the user to
7886 be in a section other than .sbss or .sdata. */
7887 fputs ("\t.extern\t", file);
7888 assemble_name (file, name);
7889 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
7890 int_size_in_bytes (TREE_TYPE (decl)));
7891 }
7892 }
7893 }
7894
7895 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7896
7897 static void
7898 mips_output_filename (FILE *stream, const char *name)
7899 {
7900 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
7901 directives. */
7902 if (write_symbols == DWARF2_DEBUG)
7903 return;
7904 else if (mips_output_filename_first_time)
7905 {
7906 mips_output_filename_first_time = 0;
7907 num_source_filenames += 1;
7908 current_function_file = name;
7909 fprintf (stream, "\t.file\t%d ", num_source_filenames);
7910 output_quoted_string (stream, name);
7911 putc ('\n', stream);
7912 }
7913 /* If we are emitting stabs, let dbxout.c handle this (except for
7914 the mips_output_filename_first_time case). */
7915 else if (write_symbols == DBX_DEBUG)
7916 return;
7917 else if (name != current_function_file
7918 && strcmp (name, current_function_file) != 0)
7919 {
7920 num_source_filenames += 1;
7921 current_function_file = name;
7922 fprintf (stream, "\t.file\t%d ", num_source_filenames);
7923 output_quoted_string (stream, name);
7924 putc ('\n', stream);
7925 }
7926 }
7927
7928 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL. */
7929
7930 static void ATTRIBUTE_UNUSED
7931 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
7932 {
7933 switch (size)
7934 {
7935 case 4:
7936 fputs ("\t.dtprelword\t", file);
7937 break;
7938
7939 case 8:
7940 fputs ("\t.dtpreldword\t", file);
7941 break;
7942
7943 default:
7944 gcc_unreachable ();
7945 }
7946 output_addr_const (file, x);
7947 fputs ("+0x8000", file);
7948 }
7949
7950 /* Implement TARGET_DWARF_REGISTER_SPAN. */
7951
7952 static rtx
7953 mips_dwarf_register_span (rtx reg)
7954 {
7955 rtx high, low;
7956 enum machine_mode mode;
7957
7958 /* By default, GCC maps increasing register numbers to increasing
7959 memory locations, but paired FPRs are always little-endian,
7960 regardless of the prevailing endianness. */
7961 mode = GET_MODE (reg);
7962 if (FP_REG_P (REGNO (reg))
7963 && TARGET_BIG_ENDIAN
7964 && MAX_FPRS_PER_FMT > 1
7965 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
7966 {
7967 gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
7968 high = mips_subword (reg, true);
7969 low = mips_subword (reg, false);
7970 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
7971 }
7972
7973 return NULL_RTX;
7974 }
7975
7976 /* Implement ASM_OUTPUT_ASCII. */
7977
7978 void
7979 mips_output_ascii (FILE *stream, const char *string, size_t len)
7980 {
7981 size_t i;
7982 int cur_pos;
7983
7984 cur_pos = 17;
7985 fprintf (stream, "\t.ascii\t\"");
7986 for (i = 0; i < len; i++)
7987 {
7988 int c;
7989
7990 c = (unsigned char) string[i];
7991 if (ISPRINT (c))
7992 {
7993 if (c == '\\' || c == '\"')
7994 {
7995 putc ('\\', stream);
7996 cur_pos++;
7997 }
7998 putc (c, stream);
7999 cur_pos++;
8000 }
8001 else
8002 {
8003 fprintf (stream, "\\%03o", c);
8004 cur_pos += 4;
8005 }
8006
8007 if (cur_pos > 72 && i+1 < len)
8008 {
8009 cur_pos = 17;
8010 fprintf (stream, "\"\n\t.ascii\t\"");
8011 }
8012 }
8013 fprintf (stream, "\"\n");
8014 }
8015
8016 /* Emit either a label, .comm, or .lcomm directive. When using assembler
8017 macros, mark the symbol as written so that mips_asm_output_external
8018 won't emit an .extern for it. STREAM is the output file, NAME is the
8019 name of the symbol, INIT_STRING is the string that should be written
8020 before the symbol and FINAL_STRING is the string that should be
8021 written after it. FINAL_STRING is a printf format that consumes the
8022 remaining arguments. */
8023
8024 void
8025 mips_declare_object (FILE *stream, const char *name, const char *init_string,
8026 const char *final_string, ...)
8027 {
8028 va_list ap;
8029
8030 fputs (init_string, stream);
8031 assemble_name (stream, name);
8032 va_start (ap, final_string);
8033 vfprintf (stream, final_string, ap);
8034 va_end (ap);
8035
8036 if (!TARGET_EXPLICIT_RELOCS)
8037 {
8038 tree name_tree = get_identifier (name);
8039 TREE_ASM_WRITTEN (name_tree) = 1;
8040 }
8041 }
8042
8043 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
8044 NAME is the name of the object and ALIGN is the required alignment
8045 in bytes. TAKES_ALIGNMENT_P is true if the directive takes a third
8046 alignment argument. */
8047
8048 void
8049 mips_declare_common_object (FILE *stream, const char *name,
8050 const char *init_string,
8051 unsigned HOST_WIDE_INT size,
8052 unsigned int align, bool takes_alignment_p)
8053 {
8054 if (!takes_alignment_p)
8055 {
8056 size += (align / BITS_PER_UNIT) - 1;
8057 size -= size % (align / BITS_PER_UNIT);
8058 mips_declare_object (stream, name, init_string,
8059 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
8060 }
8061 else
8062 mips_declare_object (stream, name, init_string,
8063 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
8064 size, align / BITS_PER_UNIT);
8065 }
8066
8067 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as the
8068 elfos.h version, but we also need to handle -muninit-const-in-rodata. */
8069
8070 void
8071 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
8072 unsigned HOST_WIDE_INT size,
8073 unsigned int align)
8074 {
8075 /* If the target wants uninitialized const declarations in
8076 .rdata then don't put them in .comm. */
8077 if (TARGET_EMBEDDED_DATA
8078 && TARGET_UNINIT_CONST_IN_RODATA
8079 && TREE_CODE (decl) == VAR_DECL
8080 && TREE_READONLY (decl)
8081 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
8082 {
8083 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
8084 targetm.asm_out.globalize_label (stream, name);
8085
8086 switch_to_section (readonly_data_section);
8087 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
8088 mips_declare_object (stream, name, "",
8089 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
8090 size);
8091 }
8092 else
8093 mips_declare_common_object (stream, name, "\n\t.comm\t",
8094 size, align, true);
8095 }
8096
8097 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
8098 extern int size_directive_output;
8099
8100 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
8101 definitions except that it uses mips_declare_object to emit the label. */
8102
8103 void
8104 mips_declare_object_name (FILE *stream, const char *name,
8105 tree decl ATTRIBUTE_UNUSED)
8106 {
8107 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
8108 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
8109 #endif
8110
8111 size_directive_output = 0;
8112 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
8113 {
8114 HOST_WIDE_INT size;
8115
8116 size_directive_output = 1;
8117 size = int_size_in_bytes (TREE_TYPE (decl));
8118 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8119 }
8120
8121 mips_declare_object (stream, name, "", ":\n");
8122 }
8123
8124 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
8125
8126 void
8127 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
8128 {
8129 const char *name;
8130
8131 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8132 if (!flag_inhibit_size_directive
8133 && DECL_SIZE (decl) != 0
8134 && !at_end
8135 && top_level
8136 && DECL_INITIAL (decl) == error_mark_node
8137 && !size_directive_output)
8138 {
8139 HOST_WIDE_INT size;
8140
8141 size_directive_output = 1;
8142 size = int_size_in_bytes (TREE_TYPE (decl));
8143 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8144 }
8145 }
8146 #endif
8147 \f
8148 /* Return the FOO in the name of the ".mdebug.FOO" section associated
8149 with the current ABI. */
8150
8151 static const char *
8152 mips_mdebug_abi_name (void)
8153 {
8154 switch (mips_abi)
8155 {
8156 case ABI_32:
8157 return "abi32";
8158 case ABI_O64:
8159 return "abiO64";
8160 case ABI_N32:
8161 return "abiN32";
8162 case ABI_64:
8163 return "abi64";
8164 case ABI_EABI:
8165 return TARGET_64BIT ? "eabi64" : "eabi32";
8166 default:
8167 gcc_unreachable ();
8168 }
8169 }
8170
8171 /* Implement TARGET_ASM_FILE_START. */
8172
8173 static void
8174 mips_file_start (void)
8175 {
8176 default_file_start ();
8177
8178 /* Generate a special section to describe the ABI switches used to
8179 produce the resultant binary. This is unnecessary on IRIX and
8180 causes unwanted warnings from the native linker. */
8181 if (!TARGET_IRIX6)
8182 {
8183 /* Record the ABI itself. Modern versions of binutils encode
8184 this information in the ELF header flags, but GDB needs the
8185 information in order to correctly debug binaries produced by
8186 older binutils. See the function mips_gdbarch_init in
8187 gdb/mips-tdep.c. */
8188 fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
8189 mips_mdebug_abi_name ());
8190
8191 /* There is no ELF header flag to distinguish long32 forms of the
8192 EABI from long64 forms. Emit a special section to help tools
8193 such as GDB. Do the same for o64, which is sometimes used with
8194 -mlong64. */
8195 if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
8196 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
8197 "\t.previous\n", TARGET_LONG64 ? 64 : 32);
8198
8199 #ifdef HAVE_AS_GNU_ATTRIBUTE
8200 {
8201 int attr;
8202
8203 /* No floating-point operations, -mno-float. */
8204 if (TARGET_NO_FLOAT)
8205 attr = 0;
8206 /* Soft-float code, -msoft-float. */
8207 else if (!TARGET_HARD_FLOAT_ABI)
8208 attr = 3;
8209 /* Single-float code, -msingle-float. */
8210 else if (!TARGET_DOUBLE_FLOAT)
8211 attr = 2;
8212 /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64. */
8213 else if (!TARGET_64BIT && TARGET_FLOAT64)
8214 attr = 4;
8215 /* Regular FP code, FP regs same size as GP regs, -mdouble-float. */
8216 else
8217 attr = 1;
8218
8219 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
8220 }
8221 #endif
8222 }
8223
8224 /* If TARGET_ABICALLS, tell GAS to generate -KPIC code. */
8225 if (TARGET_ABICALLS)
8226 {
8227 fprintf (asm_out_file, "\t.abicalls\n");
8228 if (TARGET_ABICALLS_PIC0)
8229 fprintf (asm_out_file, "\t.option\tpic0\n");
8230 }
8231
8232 if (flag_verbose_asm)
8233 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
8234 ASM_COMMENT_START,
8235 mips_small_data_threshold, mips_arch_info->name, mips_isa);
8236 }
8237 \f
8238 /* Make the last instruction frame-related and note that it performs
8239 the operation described by FRAME_PATTERN. */
8240
8241 static void
8242 mips_set_frame_expr (rtx frame_pattern)
8243 {
8244 rtx insn;
8245
8246 insn = get_last_insn ();
8247 RTX_FRAME_RELATED_P (insn) = 1;
8248 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8249 frame_pattern,
8250 REG_NOTES (insn));
8251 }
8252
8253 /* Return a frame-related rtx that stores REG at MEM.
8254 REG must be a single register. */
8255
8256 static rtx
8257 mips_frame_set (rtx mem, rtx reg)
8258 {
8259 rtx set;
8260
8261 /* If we're saving the return address register and the DWARF return
8262 address column differs from the hard register number, adjust the
8263 note reg to refer to the former. */
8264 if (REGNO (reg) == RETURN_ADDR_REGNUM
8265 && DWARF_FRAME_RETURN_COLUMN != RETURN_ADDR_REGNUM)
8266 reg = gen_rtx_REG (GET_MODE (reg), DWARF_FRAME_RETURN_COLUMN);
8267
8268 set = gen_rtx_SET (VOIDmode, mem, reg);
8269 RTX_FRAME_RELATED_P (set) = 1;
8270
8271 return set;
8272 }
8273 \f
8274 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
8275 mips16e_s2_s8_regs[X], it must also save the registers in indexes
8276 X + 1 onwards. Likewise mips16e_a0_a3_regs. */
8277 static const unsigned char mips16e_s2_s8_regs[] = {
8278 30, 23, 22, 21, 20, 19, 18
8279 };
8280 static const unsigned char mips16e_a0_a3_regs[] = {
8281 4, 5, 6, 7
8282 };
8283
8284 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
8285 ordered from the uppermost in memory to the lowest in memory. */
8286 static const unsigned char mips16e_save_restore_regs[] = {
8287 31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
8288 };
8289
8290 /* Return the index of the lowest X in the range [0, SIZE) for which
8291 bit REGS[X] is set in MASK. Return SIZE if there is no such X. */
8292
8293 static unsigned int
8294 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
8295 unsigned int size)
8296 {
8297 unsigned int i;
8298
8299 for (i = 0; i < size; i++)
8300 if (BITSET_P (mask, regs[i]))
8301 break;
8302
8303 return i;
8304 }
8305
8306 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
8307 is the number of set bits. If *MASK_PTR contains REGS[X] for some X
8308 in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
8309 is true for all indexes (X, SIZE). */
8310
8311 static void
8312 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
8313 unsigned int size, unsigned int *num_regs_ptr)
8314 {
8315 unsigned int i;
8316
8317 i = mips16e_find_first_register (*mask_ptr, regs, size);
8318 for (i++; i < size; i++)
8319 if (!BITSET_P (*mask_ptr, regs[i]))
8320 {
8321 *num_regs_ptr += 1;
8322 *mask_ptr |= 1 << regs[i];
8323 }
8324 }
8325
8326 /* Return a simplified form of X using the register values in REG_VALUES.
8327 REG_VALUES[R] is the last value assigned to hard register R, or null
8328 if R has not been modified.
8329
8330 This function is rather limited, but is good enough for our purposes. */
8331
8332 static rtx
8333 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
8334 {
8335 x = avoid_constant_pool_reference (x);
8336
8337 if (UNARY_P (x))
8338 {
8339 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
8340 return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
8341 x0, GET_MODE (XEXP (x, 0)));
8342 }
8343
8344 if (ARITHMETIC_P (x))
8345 {
8346 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
8347 rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
8348 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
8349 }
8350
8351 if (REG_P (x)
8352 && reg_values[REGNO (x)]
8353 && !rtx_unstable_p (reg_values[REGNO (x)]))
8354 return reg_values[REGNO (x)];
8355
8356 return x;
8357 }
8358
8359 /* Return true if (set DEST SRC) stores an argument register into its
8360 caller-allocated save slot, storing the number of that argument
8361 register in *REGNO_PTR if so. REG_VALUES is as for
8362 mips16e_collect_propagate_value. */
8363
8364 static bool
8365 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
8366 unsigned int *regno_ptr)
8367 {
8368 unsigned int argno, regno;
8369 HOST_WIDE_INT offset, required_offset;
8370 rtx addr, base;
8371
8372 /* Check that this is a word-mode store. */
8373 if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
8374 return false;
8375
8376 /* Check that the register being saved is an unmodified argument
8377 register. */
8378 regno = REGNO (src);
8379 if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
8380 return false;
8381 argno = regno - GP_ARG_FIRST;
8382
8383 /* Check whether the address is an appropriate stack-pointer or
8384 frame-pointer access. */
8385 addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
8386 mips_split_plus (addr, &base, &offset);
8387 required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
8388 if (base == hard_frame_pointer_rtx)
8389 required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
8390 else if (base != stack_pointer_rtx)
8391 return false;
8392 if (offset != required_offset)
8393 return false;
8394
8395 *regno_ptr = regno;
8396 return true;
8397 }
8398
8399 /* A subroutine of mips_expand_prologue, called only when generating
8400 MIPS16e SAVE instructions. Search the start of the function for any
8401 instructions that save argument registers into their caller-allocated
8402 save slots. Delete such instructions and return a value N such that
8403 saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
8404 instructions redundant. */
8405
8406 static unsigned int
8407 mips16e_collect_argument_saves (void)
8408 {
8409 rtx reg_values[FIRST_PSEUDO_REGISTER];
8410 rtx insn, next, set, dest, src;
8411 unsigned int nargs, regno;
8412
8413 push_topmost_sequence ();
8414 nargs = 0;
8415 memset (reg_values, 0, sizeof (reg_values));
8416 for (insn = get_insns (); insn; insn = next)
8417 {
8418 next = NEXT_INSN (insn);
8419 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
8420 continue;
8421
8422 if (!INSN_P (insn))
8423 break;
8424
8425 set = PATTERN (insn);
8426 if (GET_CODE (set) != SET)
8427 break;
8428
8429 dest = SET_DEST (set);
8430 src = SET_SRC (set);
8431 if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
8432 {
8433 if (!BITSET_P (cfun->machine->frame.mask, regno))
8434 {
8435 delete_insn (insn);
8436 nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
8437 }
8438 }
8439 else if (REG_P (dest) && GET_MODE (dest) == word_mode)
8440 reg_values[REGNO (dest)]
8441 = mips16e_collect_propagate_value (src, reg_values);
8442 else
8443 break;
8444 }
8445 pop_topmost_sequence ();
8446
8447 return nargs;
8448 }
8449
8450 /* Return a move between register REGNO and memory location SP + OFFSET.
8451 Make the move a load if RESTORE_P, otherwise make it a frame-related
8452 store. */
8453
8454 static rtx
8455 mips16e_save_restore_reg (bool restore_p, HOST_WIDE_INT offset,
8456 unsigned int regno)
8457 {
8458 rtx reg, mem;
8459
8460 mem = gen_frame_mem (SImode, plus_constant (stack_pointer_rtx, offset));
8461 reg = gen_rtx_REG (SImode, regno);
8462 return (restore_p
8463 ? gen_rtx_SET (VOIDmode, reg, mem)
8464 : mips_frame_set (mem, reg));
8465 }
8466
8467 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
8468 The instruction must:
8469
8470 - Allocate or deallocate SIZE bytes in total; SIZE is known
8471 to be nonzero.
8472
8473 - Save or restore as many registers in *MASK_PTR as possible.
8474 The instruction saves the first registers at the top of the
8475 allocated area, with the other registers below it.
8476
8477 - Save NARGS argument registers above the allocated area.
8478
8479 (NARGS is always zero if RESTORE_P.)
8480
8481 The SAVE and RESTORE instructions cannot save and restore all general
8482 registers, so there may be some registers left over for the caller to
8483 handle. Destructively modify *MASK_PTR so that it contains the registers
8484 that still need to be saved or restored. The caller can save these
8485 registers in the memory immediately below *OFFSET_PTR, which is a
8486 byte offset from the bottom of the allocated stack area. */
8487
8488 static rtx
8489 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
8490 HOST_WIDE_INT *offset_ptr, unsigned int nargs,
8491 HOST_WIDE_INT size)
8492 {
8493 rtx pattern, set;
8494 HOST_WIDE_INT offset, top_offset;
8495 unsigned int i, regno;
8496 int n;
8497
8498 gcc_assert (cfun->machine->frame.num_fp == 0);
8499
8500 /* Calculate the number of elements in the PARALLEL. We need one element
8501 for the stack adjustment, one for each argument register save, and one
8502 for each additional register move. */
8503 n = 1 + nargs;
8504 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
8505 if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
8506 n++;
8507
8508 /* Create the final PARALLEL. */
8509 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
8510 n = 0;
8511
8512 /* Add the stack pointer adjustment. */
8513 set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8514 plus_constant (stack_pointer_rtx,
8515 restore_p ? size : -size));
8516 RTX_FRAME_RELATED_P (set) = 1;
8517 XVECEXP (pattern, 0, n++) = set;
8518
8519 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
8520 top_offset = restore_p ? size : 0;
8521
8522 /* Save the arguments. */
8523 for (i = 0; i < nargs; i++)
8524 {
8525 offset = top_offset + i * UNITS_PER_WORD;
8526 set = mips16e_save_restore_reg (restore_p, offset, GP_ARG_FIRST + i);
8527 XVECEXP (pattern, 0, n++) = set;
8528 }
8529
8530 /* Then fill in the other register moves. */
8531 offset = top_offset;
8532 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
8533 {
8534 regno = mips16e_save_restore_regs[i];
8535 if (BITSET_P (*mask_ptr, regno))
8536 {
8537 offset -= UNITS_PER_WORD;
8538 set = mips16e_save_restore_reg (restore_p, offset, regno);
8539 XVECEXP (pattern, 0, n++) = set;
8540 *mask_ptr &= ~(1 << regno);
8541 }
8542 }
8543
8544 /* Tell the caller what offset it should use for the remaining registers. */
8545 *offset_ptr = size + (offset - top_offset);
8546
8547 gcc_assert (n == XVECLEN (pattern, 0));
8548
8549 return pattern;
8550 }
8551
8552 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
8553 pointer. Return true if PATTERN matches the kind of instruction
8554 generated by mips16e_build_save_restore. If INFO is nonnull,
8555 initialize it when returning true. */
8556
8557 bool
8558 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
8559 struct mips16e_save_restore_info *info)
8560 {
8561 unsigned int i, nargs, mask, extra;
8562 HOST_WIDE_INT top_offset, save_offset, offset;
8563 rtx set, reg, mem, base;
8564 int n;
8565
8566 if (!GENERATE_MIPS16E_SAVE_RESTORE)
8567 return false;
8568
8569 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
8570 top_offset = adjust > 0 ? adjust : 0;
8571
8572 /* Interpret all other members of the PARALLEL. */
8573 save_offset = top_offset - UNITS_PER_WORD;
8574 mask = 0;
8575 nargs = 0;
8576 i = 0;
8577 for (n = 1; n < XVECLEN (pattern, 0); n++)
8578 {
8579 /* Check that we have a SET. */
8580 set = XVECEXP (pattern, 0, n);
8581 if (GET_CODE (set) != SET)
8582 return false;
8583
8584 /* Check that the SET is a load (if restoring) or a store
8585 (if saving). */
8586 mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
8587 if (!MEM_P (mem))
8588 return false;
8589
8590 /* Check that the address is the sum of the stack pointer and a
8591 possibly-zero constant offset. */
8592 mips_split_plus (XEXP (mem, 0), &base, &offset);
8593 if (base != stack_pointer_rtx)
8594 return false;
8595
8596 /* Check that SET's other operand is a register. */
8597 reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
8598 if (!REG_P (reg))
8599 return false;
8600
8601 /* Check for argument saves. */
8602 if (offset == top_offset + nargs * UNITS_PER_WORD
8603 && REGNO (reg) == GP_ARG_FIRST + nargs)
8604 nargs++;
8605 else if (offset == save_offset)
8606 {
8607 while (mips16e_save_restore_regs[i++] != REGNO (reg))
8608 if (i == ARRAY_SIZE (mips16e_save_restore_regs))
8609 return false;
8610
8611 mask |= 1 << REGNO (reg);
8612 save_offset -= UNITS_PER_WORD;
8613 }
8614 else
8615 return false;
8616 }
8617
8618 /* Check that the restrictions on register ranges are met. */
8619 extra = 0;
8620 mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
8621 ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
8622 mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
8623 ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
8624 if (extra != 0)
8625 return false;
8626
8627 /* Make sure that the topmost argument register is not saved twice.
8628 The checks above ensure that the same is then true for the other
8629 argument registers. */
8630 if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
8631 return false;
8632
8633 /* Pass back information, if requested. */
8634 if (info)
8635 {
8636 info->nargs = nargs;
8637 info->mask = mask;
8638 info->size = (adjust > 0 ? adjust : -adjust);
8639 }
8640
8641 return true;
8642 }
8643
8644 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
8645 for the register range [MIN_REG, MAX_REG]. Return a pointer to
8646 the null terminator. */
8647
8648 static char *
8649 mips16e_add_register_range (char *s, unsigned int min_reg,
8650 unsigned int max_reg)
8651 {
8652 if (min_reg != max_reg)
8653 s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
8654 else
8655 s += sprintf (s, ",%s", reg_names[min_reg]);
8656 return s;
8657 }
8658
8659 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
8660 PATTERN and ADJUST are as for mips16e_save_restore_pattern_p. */
8661
8662 const char *
8663 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
8664 {
8665 static char buffer[300];
8666
8667 struct mips16e_save_restore_info info;
8668 unsigned int i, end;
8669 char *s;
8670
8671 /* Parse the pattern. */
8672 if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
8673 gcc_unreachable ();
8674
8675 /* Add the mnemonic. */
8676 s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
8677 s += strlen (s);
8678
8679 /* Save the arguments. */
8680 if (info.nargs > 1)
8681 s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
8682 reg_names[GP_ARG_FIRST + info.nargs - 1]);
8683 else if (info.nargs == 1)
8684 s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
8685
8686 /* Emit the amount of stack space to allocate or deallocate. */
8687 s += sprintf (s, "%d", (int) info.size);
8688
8689 /* Save or restore $16. */
8690 if (BITSET_P (info.mask, 16))
8691 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
8692
8693 /* Save or restore $17. */
8694 if (BITSET_P (info.mask, 17))
8695 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
8696
8697 /* Save or restore registers in the range $s2...$s8, which
8698 mips16e_s2_s8_regs lists in decreasing order. Note that this
8699 is a software register range; the hardware registers are not
8700 numbered consecutively. */
8701 end = ARRAY_SIZE (mips16e_s2_s8_regs);
8702 i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
8703 if (i < end)
8704 s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
8705 mips16e_s2_s8_regs[i]);
8706
8707 /* Save or restore registers in the range $a0...$a3. */
8708 end = ARRAY_SIZE (mips16e_a0_a3_regs);
8709 i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
8710 if (i < end)
8711 s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
8712 mips16e_a0_a3_regs[end - 1]);
8713
8714 /* Save or restore $31. */
8715 if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
8716 s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
8717
8718 return buffer;
8719 }
8720 \f
8721 /* Return true if the current function returns its value in a floating-point
8722 register in MIPS16 mode. */
8723
8724 static bool
8725 mips16_cfun_returns_in_fpr_p (void)
8726 {
8727 tree return_type = DECL_RESULT (current_function_decl);
8728 return (TARGET_MIPS16
8729 && TARGET_HARD_FLOAT_ABI
8730 && !aggregate_value_p (return_type, current_function_decl)
8731 && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
8732 }
8733
8734 /* Return true if predicate PRED is true for at least one instruction.
8735 Cache the result in *CACHE, and assume that the result is true
8736 if *CACHE is already true. */
8737
8738 static bool
8739 mips_find_gp_ref (bool *cache, bool (*pred) (rtx))
8740 {
8741 rtx insn;
8742
8743 if (!*cache)
8744 {
8745 push_topmost_sequence ();
8746 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8747 if (USEFUL_INSN_P (insn) && pred (insn))
8748 {
8749 *cache = true;
8750 break;
8751 }
8752 pop_topmost_sequence ();
8753 }
8754 return *cache;
8755 }
8756
8757 /* Return true if INSN refers to the global pointer in an "inflexible" way.
8758 See mips_cfun_has_inflexible_gp_ref_p for details. */
8759
8760 static bool
8761 mips_insn_has_inflexible_gp_ref_p (rtx insn)
8762 {
8763 /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
8764 indicate that the target could be a traditional MIPS
8765 lazily-binding stub. */
8766 return find_reg_fusage (insn, USE, pic_offset_table_rtx);
8767 }
8768
8769 /* Return true if the current function refers to the global pointer
8770 in a way that forces $28 to be valid. This means that we can't
8771 change the choice of global pointer, even for NewABI code.
8772
8773 One example of this (and one which needs several checks) is that
8774 $28 must be valid when calling traditional MIPS lazy-binding stubs.
8775 (This restriction does not apply to PLTs.) */
8776
8777 static bool
8778 mips_cfun_has_inflexible_gp_ref_p (void)
8779 {
8780 /* If the function has a nonlocal goto, $28 must hold the correct
8781 global pointer for the target function. That is, the target
8782 of the goto implicitly uses $28. */
8783 if (crtl->has_nonlocal_goto)
8784 return true;
8785
8786 if (TARGET_ABICALLS_PIC2)
8787 {
8788 /* Symbolic accesses implicitly use the global pointer unless
8789 -mexplicit-relocs is in effect. JAL macros to symbolic addresses
8790 might go to traditional MIPS lazy-binding stubs. */
8791 if (!TARGET_EXPLICIT_RELOCS)
8792 return true;
8793
8794 /* FUNCTION_PROFILER includes a JAL to _mcount, which again
8795 can be lazily-bound. */
8796 if (crtl->profile)
8797 return true;
8798
8799 /* MIPS16 functions that return in FPRs need to call an
8800 external libgcc routine. This call is only made explict
8801 during mips_expand_epilogue, and it too might be lazily bound. */
8802 if (mips16_cfun_returns_in_fpr_p ())
8803 return true;
8804 }
8805
8806 return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
8807 mips_insn_has_inflexible_gp_ref_p);
8808 }
8809
8810 /* Return true if INSN refers to the global pointer in a "flexible" way.
8811 See mips_cfun_has_flexible_gp_ref_p for details. */
8812
8813 static bool
8814 mips_insn_has_flexible_gp_ref_p (rtx insn)
8815 {
8816 return (get_attr_got (insn) != GOT_UNSET
8817 || mips_small_data_pattern_p (PATTERN (insn))
8818 || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
8819 }
8820
8821 /* Return true if the current function references the global pointer,
8822 but if those references do not inherently require the global pointer
8823 to be $28. Assume !mips_cfun_has_inflexible_gp_ref_p (). */
8824
8825 static bool
8826 mips_cfun_has_flexible_gp_ref_p (void)
8827 {
8828 /* Reload can sometimes introduce constant pool references
8829 into a function that otherwise didn't need them. For example,
8830 suppose we have an instruction like:
8831
8832 (set (reg:DF R1) (float:DF (reg:SI R2)))
8833
8834 If R2 turns out to be a constant such as 1, the instruction may
8835 have a REG_EQUAL note saying that R1 == 1.0. Reload then has
8836 the option of using this constant if R2 doesn't get allocated
8837 to a register.
8838
8839 In cases like these, reload will have added the constant to the
8840 pool but no instruction will yet refer to it. */
8841 if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
8842 return true;
8843
8844 return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
8845 mips_insn_has_flexible_gp_ref_p);
8846 }
8847
8848 /* Return the register that should be used as the global pointer
8849 within this function. Return INVALID_REGNUM if the function
8850 doesn't need a global pointer. */
8851
8852 static unsigned int
8853 mips_global_pointer (void)
8854 {
8855 unsigned int regno;
8856
8857 /* $gp is always available unless we're using a GOT. */
8858 if (!TARGET_USE_GOT)
8859 return GLOBAL_POINTER_REGNUM;
8860
8861 /* If there are inflexible references to $gp, we must use the
8862 standard register. */
8863 if (mips_cfun_has_inflexible_gp_ref_p ())
8864 return GLOBAL_POINTER_REGNUM;
8865
8866 /* If there are no current references to $gp, then the only uses
8867 we can introduce later are those involved in long branches. */
8868 if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
8869 return INVALID_REGNUM;
8870
8871 /* If the global pointer is call-saved, try to use a call-clobbered
8872 alternative. */
8873 if (TARGET_CALL_SAVED_GP && current_function_is_leaf)
8874 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
8875 if (!df_regs_ever_live_p (regno)
8876 && call_really_used_regs[regno]
8877 && !fixed_regs[regno]
8878 && regno != PIC_FUNCTION_ADDR_REGNUM)
8879 return regno;
8880
8881 return GLOBAL_POINTER_REGNUM;
8882 }
8883
8884 /* Return true if the current function's prologue must load the global
8885 pointer value into pic_offset_table_rtx and store the same value in
8886 the function's cprestore slot (if any).
8887
8888 One problem we have to deal with is that, when emitting GOT-based
8889 position independent code, long-branch sequences will need to load
8890 the address of the branch target from the GOT. We don't know until
8891 the very end of compilation whether (and where) the function needs
8892 long branches, so we must ensure that _any_ branch can access the
8893 global pointer in some form. However, we do not want to pessimize
8894 the usual case in which all branches are short.
8895
8896 We handle this as follows:
8897
8898 (1) During reload, we set cfun->machine->global_pointer to
8899 INVALID_REGNUM if we _know_ that the current function
8900 doesn't need a global pointer. This is only valid if
8901 long branches don't need the GOT.
8902
8903 Otherwise, we assume that we might need a global pointer
8904 and pick an appropriate register.
8905
8906 (2) If cfun->machine->global_pointer != INVALID_REGNUM,
8907 we ensure that the global pointer is available at every
8908 block boundary bar entry and exit. We do this in one of two ways:
8909
8910 - If the function has a cprestore slot, we ensure that this
8911 slot is valid at every branch. However, as explained in
8912 point (6) below, there is no guarantee that pic_offset_table_rtx
8913 itself is valid if new uses of the global pointer are introduced
8914 after the first post-epilogue split.
8915
8916 We guarantee that the cprestore slot is valid by loading it
8917 into a fake register, CPRESTORE_SLOT_REGNUM. We then make
8918 this register live at every block boundary bar function entry
8919 and exit. It is then invalid to move the load (and thus the
8920 preceding store) across a block boundary.
8921
8922 - If the function has no cprestore slot, we guarantee that
8923 pic_offset_table_rtx itself is valid at every branch.
8924
8925 See mips_eh_uses for the handling of the register liveness.
8926
8927 (3) During prologue and epilogue generation, we emit "ghost"
8928 placeholder instructions to manipulate the global pointer.
8929
8930 (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
8931 and cfun->machine->must_restore_gp_when_clobbered_p if we already know
8932 that the function needs a global pointer. (There is no need to set
8933 them earlier than this, and doing it as late as possible leads to
8934 fewer false positives.)
8935
8936 (5) If cfun->machine->must_initialize_gp_p is true during a
8937 split_insns pass, we split the ghost instructions into real
8938 instructions. These split instructions can then be optimized in
8939 the usual way. Otherwise, we keep the ghost instructions intact,
8940 and optimize for the case where they aren't needed. We still
8941 have the option of splitting them later, if we need to introduce
8942 new uses of the global pointer.
8943
8944 For example, the scheduler ignores a ghost instruction that
8945 stores $28 to the stack, but it handles the split form of
8946 the ghost instruction as an ordinary store.
8947
8948 (6) [OldABI only.] If cfun->machine->must_restore_gp_when_clobbered_p
8949 is true during the first post-epilogue split_insns pass, we split
8950 calls and restore_gp patterns into instructions that explicitly
8951 load pic_offset_table_rtx from the cprestore slot. Otherwise,
8952 we split these patterns into instructions that _don't_ load from
8953 the cprestore slot.
8954
8955 If cfun->machine->must_restore_gp_when_clobbered_p is true at the
8956 time of the split, then any instructions that exist at that time
8957 can make free use of pic_offset_table_rtx. However, if we want
8958 to introduce new uses of the global pointer after the split,
8959 we must explicitly load the value from the cprestore slot, since
8960 pic_offset_table_rtx itself might not be valid at a given point
8961 in the function.
8962
8963 The idea is that we want to be able to delete redundant
8964 loads from the cprestore slot in the usual case where no
8965 long branches are needed.
8966
8967 (7) If cfun->machine->must_initialize_gp_p is still false at the end
8968 of md_reorg, we decide whether the global pointer is needed for
8969 long branches. If so, we set cfun->machine->must_initialize_gp_p
8970 to true and split the ghost instructions into real instructions
8971 at that stage.
8972
8973 Note that the ghost instructions must have a zero length for three reasons:
8974
8975 - Giving the length of the underlying $gp sequence might cause
8976 us to use long branches in cases where they aren't really needed.
8977
8978 - They would perturb things like alignment calculations.
8979
8980 - More importantly, the hazard detection in md_reorg relies on
8981 empty instructions having a zero length.
8982
8983 If we find a long branch and split the ghost instructions at the
8984 end of md_reorg, the split could introduce more long branches.
8985 That isn't a problem though, because we still do the split before
8986 the final shorten_branches pass.
8987
8988 This is extremely ugly, but it seems like the best compromise between
8989 correctness and efficiency. */
8990
8991 bool
8992 mips_must_initialize_gp_p (void)
8993 {
8994 return cfun->machine->must_initialize_gp_p;
8995 }
8996
8997 /* Return true if REGNO is a register that is ordinarily call-clobbered
8998 but must nevertheless be preserved by an interrupt handler. */
8999
9000 static bool
9001 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
9002 {
9003 if (MD_REG_P (regno))
9004 return true;
9005
9006 if (TARGET_DSP && DSP_ACC_REG_P (regno))
9007 return true;
9008
9009 if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
9010 {
9011 /* $0 is hard-wired. */
9012 if (regno == GP_REG_FIRST)
9013 return false;
9014
9015 /* The interrupt handler can treat kernel registers as
9016 scratch registers. */
9017 if (KERNEL_REG_P (regno))
9018 return false;
9019
9020 /* The function will return the stack pointer to its original value
9021 anyway. */
9022 if (regno == STACK_POINTER_REGNUM)
9023 return false;
9024
9025 /* Otherwise, return true for registers that aren't ordinarily
9026 call-clobbered. */
9027 return call_really_used_regs[regno];
9028 }
9029
9030 return false;
9031 }
9032
9033 /* Return true if the current function should treat register REGNO
9034 as call-saved. */
9035
9036 static bool
9037 mips_cfun_call_saved_reg_p (unsigned int regno)
9038 {
9039 /* Interrupt handlers need to save extra registers. */
9040 if (cfun->machine->interrupt_handler_p
9041 && mips_interrupt_extra_call_saved_reg_p (regno))
9042 return true;
9043
9044 /* call_insns preserve $28 unless they explicitly say otherwise,
9045 so call_really_used_regs[] treats $28 as call-saved. However,
9046 we want the ABI property rather than the default call_insn
9047 property here. */
9048 return (regno == GLOBAL_POINTER_REGNUM
9049 ? TARGET_CALL_SAVED_GP
9050 : !call_really_used_regs[regno]);
9051 }
9052
9053 /* Return true if the function body might clobber register REGNO.
9054 We know that REGNO is call-saved. */
9055
9056 static bool
9057 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
9058 {
9059 /* Some functions should be treated as clobbering all call-saved
9060 registers. */
9061 if (crtl->saves_all_registers)
9062 return true;
9063
9064 /* DF handles cases where a register is explicitly referenced in
9065 the rtl. Incoming values are passed in call-clobbered registers,
9066 so we can assume that any live call-saved register is set within
9067 the function. */
9068 if (df_regs_ever_live_p (regno))
9069 return true;
9070
9071 /* Check for registers that are clobbered by FUNCTION_PROFILER.
9072 These clobbers are not explicit in the rtl. */
9073 if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
9074 return true;
9075
9076 /* If we're using a call-saved global pointer, the function's
9077 prologue will need to set it up. */
9078 if (cfun->machine->global_pointer == regno)
9079 return true;
9080
9081 /* The function's prologue will need to set the frame pointer if
9082 frame_pointer_needed. */
9083 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
9084 return true;
9085
9086 /* If a MIPS16 function returns a value in FPRs, its epilogue
9087 will need to call an external libgcc routine. This yet-to-be
9088 generated call_insn will clobber $31. */
9089 if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
9090 return true;
9091
9092 /* If REGNO is ordinarily call-clobbered, we must assume that any
9093 called function could modify it. */
9094 if (cfun->machine->interrupt_handler_p
9095 && !current_function_is_leaf
9096 && mips_interrupt_extra_call_saved_reg_p (regno))
9097 return true;
9098
9099 return false;
9100 }
9101
9102 /* Return true if the current function must save register REGNO. */
9103
9104 static bool
9105 mips_save_reg_p (unsigned int regno)
9106 {
9107 if (mips_cfun_call_saved_reg_p (regno))
9108 {
9109 if (mips_cfun_might_clobber_call_saved_reg_p (regno))
9110 return true;
9111
9112 /* Save both registers in an FPR pair if either one is used. This is
9113 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
9114 register to be used without the even register. */
9115 if (FP_REG_P (regno)
9116 && MAX_FPRS_PER_FMT == 2
9117 && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
9118 return true;
9119 }
9120
9121 /* We need to save the incoming return address if __builtin_eh_return
9122 is being used to set a different return address. */
9123 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
9124 return true;
9125
9126 return false;
9127 }
9128
9129 /* Populate the current function's mips_frame_info structure.
9130
9131 MIPS stack frames look like:
9132
9133 +-------------------------------+
9134 | |
9135 | incoming stack arguments |
9136 | |
9137 +-------------------------------+
9138 | |
9139 | caller-allocated save area |
9140 A | for register arguments |
9141 | |
9142 +-------------------------------+ <-- incoming stack pointer
9143 | |
9144 | callee-allocated save area |
9145 B | for arguments that are |
9146 | split between registers and |
9147 | the stack |
9148 | |
9149 +-------------------------------+ <-- arg_pointer_rtx
9150 | |
9151 C | callee-allocated save area |
9152 | for register varargs |
9153 | |
9154 +-------------------------------+ <-- frame_pointer_rtx
9155 | | + cop0_sp_offset
9156 | COP0 reg save area | + UNITS_PER_WORD
9157 | |
9158 +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
9159 | | + UNITS_PER_WORD
9160 | accumulator save area |
9161 | |
9162 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
9163 | | + UNITS_PER_HWFPVALUE
9164 | FPR save area |
9165 | |
9166 +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
9167 | | + UNITS_PER_WORD
9168 | GPR save area |
9169 | |
9170 +-------------------------------+ <-- frame_pointer_rtx with
9171 | | \ -fstack-protector
9172 | local variables | | var_size
9173 | | /
9174 +-------------------------------+
9175 | | \
9176 | $gp save area | | cprestore_size
9177 | | /
9178 P +-------------------------------+ <-- hard_frame_pointer_rtx for
9179 | | \ MIPS16 code
9180 | outgoing stack arguments | |
9181 | | |
9182 +-------------------------------+ | args_size
9183 | | |
9184 | caller-allocated save area | |
9185 | for register arguments | |
9186 | | /
9187 +-------------------------------+ <-- stack_pointer_rtx
9188 frame_pointer_rtx without
9189 -fstack-protector
9190 hard_frame_pointer_rtx for
9191 non-MIPS16 code.
9192
9193 At least two of A, B and C will be empty.
9194
9195 Dynamic stack allocations such as alloca insert data at point P.
9196 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
9197 hard_frame_pointer_rtx unchanged. */
9198
9199 static void
9200 mips_compute_frame_info (void)
9201 {
9202 struct mips_frame_info *frame;
9203 HOST_WIDE_INT offset, size;
9204 unsigned int regno, i;
9205
9206 /* Set this function's interrupt properties. */
9207 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
9208 {
9209 if (!ISA_MIPS32R2)
9210 error ("the %<interrupt%> attribute requires a MIPS32r2 processor");
9211 else if (TARGET_HARD_FLOAT)
9212 error ("the %<interrupt%> attribute requires %<-msoft-float%>");
9213 else if (TARGET_MIPS16)
9214 error ("interrupt handlers cannot be MIPS16 functions");
9215 else
9216 {
9217 cfun->machine->interrupt_handler_p = true;
9218 cfun->machine->use_shadow_register_set_p =
9219 mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
9220 cfun->machine->keep_interrupts_masked_p =
9221 mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
9222 cfun->machine->use_debug_exception_return_p =
9223 mips_use_debug_exception_return_p (TREE_TYPE
9224 (current_function_decl));
9225 }
9226 }
9227
9228 frame = &cfun->machine->frame;
9229 memset (frame, 0, sizeof (*frame));
9230 size = get_frame_size ();
9231
9232 cfun->machine->global_pointer = mips_global_pointer ();
9233
9234 /* The first two blocks contain the outgoing argument area and the $gp save
9235 slot. This area isn't needed in leaf functions, but if the
9236 target-independent frame size is nonzero, we have already committed to
9237 allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD. */
9238 if ((size == 0 || FRAME_GROWS_DOWNWARD) && current_function_is_leaf)
9239 {
9240 /* The MIPS 3.0 linker does not like functions that dynamically
9241 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
9242 looks like we are trying to create a second frame pointer to the
9243 function, so allocate some stack space to make it happy. */
9244 if (cfun->calls_alloca)
9245 frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
9246 else
9247 frame->args_size = 0;
9248 frame->cprestore_size = 0;
9249 }
9250 else
9251 {
9252 frame->args_size = crtl->outgoing_args_size;
9253 frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
9254 }
9255 offset = frame->args_size + frame->cprestore_size;
9256
9257 /* Move above the local variables. */
9258 frame->var_size = MIPS_STACK_ALIGN (size);
9259 offset += frame->var_size;
9260
9261 /* Find out which GPRs we need to save. */
9262 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9263 if (mips_save_reg_p (regno))
9264 {
9265 frame->num_gp++;
9266 frame->mask |= 1 << (regno - GP_REG_FIRST);
9267 }
9268
9269 /* If this function calls eh_return, we must also save and restore the
9270 EH data registers. */
9271 if (crtl->calls_eh_return)
9272 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
9273 {
9274 frame->num_gp++;
9275 frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
9276 }
9277
9278 /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
9279 $a3-$a0 and $s2-$s8. If we save one register in the range, we must
9280 save all later registers too. */
9281 if (GENERATE_MIPS16E_SAVE_RESTORE)
9282 {
9283 mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
9284 ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
9285 mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
9286 ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
9287 }
9288
9289 /* Move above the GPR save area. */
9290 if (frame->num_gp > 0)
9291 {
9292 offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
9293 frame->gp_sp_offset = offset - UNITS_PER_WORD;
9294 }
9295
9296 /* Find out which FPRs we need to save. This loop must iterate over
9297 the same space as its companion in mips_for_each_saved_gpr_and_fpr. */
9298 if (TARGET_HARD_FLOAT)
9299 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
9300 if (mips_save_reg_p (regno))
9301 {
9302 frame->num_fp += MAX_FPRS_PER_FMT;
9303 frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
9304 }
9305
9306 /* Move above the FPR save area. */
9307 if (frame->num_fp > 0)
9308 {
9309 offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
9310 frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
9311 }
9312
9313 /* Add in space for the interrupt context information. */
9314 if (cfun->machine->interrupt_handler_p)
9315 {
9316 /* Check HI/LO. */
9317 if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
9318 {
9319 frame->num_acc++;
9320 frame->acc_mask |= (1 << 0);
9321 }
9322
9323 /* Check accumulators 1, 2, 3. */
9324 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
9325 if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
9326 {
9327 frame->num_acc++;
9328 frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
9329 }
9330
9331 /* All interrupt context functions need space to preserve STATUS. */
9332 frame->num_cop0_regs++;
9333
9334 /* If we don't keep interrupts masked, we need to save EPC. */
9335 if (!cfun->machine->keep_interrupts_masked_p)
9336 frame->num_cop0_regs++;
9337 }
9338
9339 /* Move above the accumulator save area. */
9340 if (frame->num_acc > 0)
9341 {
9342 /* Each accumulator needs 2 words. */
9343 offset += frame->num_acc * 2 * UNITS_PER_WORD;
9344 frame->acc_sp_offset = offset - UNITS_PER_WORD;
9345 }
9346
9347 /* Move above the COP0 register save area. */
9348 if (frame->num_cop0_regs > 0)
9349 {
9350 offset += frame->num_cop0_regs * UNITS_PER_WORD;
9351 frame->cop0_sp_offset = offset - UNITS_PER_WORD;
9352 }
9353
9354 /* Move above the callee-allocated varargs save area. */
9355 offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
9356 frame->arg_pointer_offset = offset;
9357
9358 /* Move above the callee-allocated area for pretend stack arguments. */
9359 offset += crtl->args.pretend_args_size;
9360 frame->total_size = offset;
9361
9362 /* Work out the offsets of the save areas from the top of the frame. */
9363 if (frame->gp_sp_offset > 0)
9364 frame->gp_save_offset = frame->gp_sp_offset - offset;
9365 if (frame->fp_sp_offset > 0)
9366 frame->fp_save_offset = frame->fp_sp_offset - offset;
9367 if (frame->acc_sp_offset > 0)
9368 frame->acc_save_offset = frame->acc_sp_offset - offset;
9369 if (frame->num_cop0_regs > 0)
9370 frame->cop0_save_offset = frame->cop0_sp_offset - offset;
9371
9372 /* MIPS16 code offsets the frame pointer by the size of the outgoing
9373 arguments. This tends to increase the chances of using unextended
9374 instructions for local variables and incoming arguments. */
9375 if (TARGET_MIPS16)
9376 frame->hard_frame_pointer_offset = frame->args_size;
9377 }
9378
9379 /* Return the style of GP load sequence that is being used for the
9380 current function. */
9381
9382 enum mips_loadgp_style
9383 mips_current_loadgp_style (void)
9384 {
9385 if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
9386 return LOADGP_NONE;
9387
9388 if (TARGET_RTP_PIC)
9389 return LOADGP_RTP;
9390
9391 if (TARGET_ABSOLUTE_ABICALLS)
9392 return LOADGP_ABSOLUTE;
9393
9394 return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
9395 }
9396
9397 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
9398
9399 static bool
9400 mips_frame_pointer_required (void)
9401 {
9402 /* If the function contains dynamic stack allocations, we need to
9403 use the frame pointer to access the static parts of the frame. */
9404 if (cfun->calls_alloca)
9405 return true;
9406
9407 /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
9408 reload may be unable to compute the address of a local variable,
9409 since there is no way to add a large constant to the stack pointer
9410 without using a second temporary register. */
9411 if (TARGET_MIPS16)
9412 {
9413 mips_compute_frame_info ();
9414 if (!SMALL_OPERAND (cfun->machine->frame.total_size))
9415 return true;
9416 }
9417
9418 return false;
9419 }
9420
9421 /* Make sure that we're not trying to eliminate to the wrong hard frame
9422 pointer. */
9423
9424 static bool
9425 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
9426 {
9427 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
9428 }
9429
9430 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
9431 or argument pointer. TO is either the stack pointer or hard frame
9432 pointer. */
9433
9434 HOST_WIDE_INT
9435 mips_initial_elimination_offset (int from, int to)
9436 {
9437 HOST_WIDE_INT offset;
9438
9439 mips_compute_frame_info ();
9440
9441 /* Set OFFSET to the offset from the end-of-prologue stack pointer. */
9442 switch (from)
9443 {
9444 case FRAME_POINTER_REGNUM:
9445 if (FRAME_GROWS_DOWNWARD)
9446 offset = (cfun->machine->frame.args_size
9447 + cfun->machine->frame.cprestore_size
9448 + cfun->machine->frame.var_size);
9449 else
9450 offset = 0;
9451 break;
9452
9453 case ARG_POINTER_REGNUM:
9454 offset = cfun->machine->frame.arg_pointer_offset;
9455 break;
9456
9457 default:
9458 gcc_unreachable ();
9459 }
9460
9461 if (to == HARD_FRAME_POINTER_REGNUM)
9462 offset -= cfun->machine->frame.hard_frame_pointer_offset;
9463
9464 return offset;
9465 }
9466 \f
9467 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY. */
9468
9469 static void
9470 mips_extra_live_on_entry (bitmap regs)
9471 {
9472 if (TARGET_USE_GOT)
9473 {
9474 /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
9475 the global pointer. */
9476 if (!TARGET_ABSOLUTE_ABICALLS)
9477 bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
9478
9479 /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
9480 the global pointer. */
9481 if (TARGET_MIPS16)
9482 bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
9483
9484 /* See the comment above load_call<mode> for details. */
9485 bitmap_set_bit (regs, GOT_VERSION_REGNUM);
9486 }
9487 }
9488
9489 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
9490 previous frame. */
9491
9492 rtx
9493 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
9494 {
9495 if (count != 0)
9496 return const0_rtx;
9497
9498 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
9499 }
9500
9501 /* Emit code to change the current function's return address to
9502 ADDRESS. SCRATCH is available as a scratch register, if needed.
9503 ADDRESS and SCRATCH are both word-mode GPRs. */
9504
9505 void
9506 mips_set_return_address (rtx address, rtx scratch)
9507 {
9508 rtx slot_address;
9509
9510 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
9511 slot_address = mips_add_offset (scratch, stack_pointer_rtx,
9512 cfun->machine->frame.gp_sp_offset);
9513 mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
9514 }
9515
9516 /* Return true if the current function has a cprestore slot. */
9517
9518 bool
9519 mips_cfun_has_cprestore_slot_p (void)
9520 {
9521 return (cfun->machine->global_pointer != INVALID_REGNUM
9522 && cfun->machine->frame.cprestore_size > 0);
9523 }
9524
9525 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
9526 cprestore slot. LOAD_P is true if the caller wants to load from
9527 the cprestore slot; it is false if the caller wants to store to
9528 the slot. */
9529
9530 static void
9531 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
9532 bool load_p)
9533 {
9534 const struct mips_frame_info *frame;
9535
9536 frame = &cfun->machine->frame;
9537 /* .cprestore always uses the stack pointer instead of the frame pointer.
9538 We have a free choice for direct stores for non-MIPS16 functions,
9539 and for MIPS16 functions whose cprestore slot is in range of the
9540 stack pointer. Using the stack pointer would sometimes give more
9541 (early) scheduling freedom, but using the frame pointer would
9542 sometimes give more (late) scheduling freedom. It's hard to
9543 predict which applies to a given function, so let's keep things
9544 simple.
9545
9546 Loads must always use the frame pointer in functions that call
9547 alloca, and there's little benefit to using the stack pointer
9548 otherwise. */
9549 if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
9550 {
9551 *base = hard_frame_pointer_rtx;
9552 *offset = frame->args_size - frame->hard_frame_pointer_offset;
9553 }
9554 else
9555 {
9556 *base = stack_pointer_rtx;
9557 *offset = frame->args_size;
9558 }
9559 }
9560
9561 /* Return true if X is the load or store address of the cprestore slot;
9562 LOAD_P says which. */
9563
9564 bool
9565 mips_cprestore_address_p (rtx x, bool load_p)
9566 {
9567 rtx given_base, required_base;
9568 HOST_WIDE_INT given_offset, required_offset;
9569
9570 mips_split_plus (x, &given_base, &given_offset);
9571 mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
9572 return given_base == required_base && given_offset == required_offset;
9573 }
9574
9575 /* Return a MEM rtx for the cprestore slot. LOAD_P is true if we are
9576 going to load from it, false if we are going to store to it.
9577 Use TEMP as a temporary register if need be. */
9578
9579 static rtx
9580 mips_cprestore_slot (rtx temp, bool load_p)
9581 {
9582 rtx base;
9583 HOST_WIDE_INT offset;
9584
9585 mips_get_cprestore_base_and_offset (&base, &offset, load_p);
9586 return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
9587 }
9588
9589 /* Emit instructions to save global pointer value GP into cprestore
9590 slot MEM. OFFSET is the offset that MEM applies to the base register.
9591
9592 MEM may not be a legitimate address. If it isn't, TEMP is a
9593 temporary register that can be used, otherwise it is a SCRATCH. */
9594
9595 void
9596 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
9597 {
9598 if (TARGET_CPRESTORE_DIRECTIVE)
9599 {
9600 gcc_assert (gp == pic_offset_table_rtx);
9601 emit_insn (gen_cprestore (mem, offset));
9602 }
9603 else
9604 mips_emit_move (mips_cprestore_slot (temp, false), gp);
9605 }
9606
9607 /* Restore $gp from its save slot, using TEMP as a temporary base register
9608 if need be. This function is for o32 and o64 abicalls only.
9609
9610 See mips_must_initialize_gp_p for details about how we manage the
9611 global pointer. */
9612
9613 void
9614 mips_restore_gp_from_cprestore_slot (rtx temp)
9615 {
9616 gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
9617
9618 if (!cfun->machine->must_restore_gp_when_clobbered_p)
9619 {
9620 emit_note (NOTE_INSN_DELETED);
9621 return;
9622 }
9623
9624 if (TARGET_MIPS16)
9625 {
9626 mips_emit_move (temp, mips_cprestore_slot (temp, true));
9627 mips_emit_move (pic_offset_table_rtx, temp);
9628 }
9629 else
9630 mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
9631 if (!TARGET_EXPLICIT_RELOCS)
9632 emit_insn (gen_blockage ());
9633 }
9634 \f
9635 /* A function to save or store a register. The first argument is the
9636 register and the second is the stack slot. */
9637 typedef void (*mips_save_restore_fn) (rtx, rtx);
9638
9639 /* Use FN to save or restore register REGNO. MODE is the register's
9640 mode and OFFSET is the offset of its save slot from the current
9641 stack pointer. */
9642
9643 static void
9644 mips_save_restore_reg (enum machine_mode mode, int regno,
9645 HOST_WIDE_INT offset, mips_save_restore_fn fn)
9646 {
9647 rtx mem;
9648
9649 mem = gen_frame_mem (mode, plus_constant (stack_pointer_rtx, offset));
9650 fn (gen_rtx_REG (mode, regno), mem);
9651 }
9652
9653 /* Call FN for each accumlator that is saved by the current function.
9654 SP_OFFSET is the offset of the current stack pointer from the start
9655 of the frame. */
9656
9657 static void
9658 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
9659 {
9660 HOST_WIDE_INT offset;
9661 int regno;
9662
9663 offset = cfun->machine->frame.acc_sp_offset - sp_offset;
9664 if (BITSET_P (cfun->machine->frame.acc_mask, 0))
9665 {
9666 mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
9667 offset -= UNITS_PER_WORD;
9668 mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
9669 offset -= UNITS_PER_WORD;
9670 }
9671
9672 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
9673 if (BITSET_P (cfun->machine->frame.acc_mask,
9674 ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
9675 {
9676 mips_save_restore_reg (word_mode, regno, offset, fn);
9677 offset -= UNITS_PER_WORD;
9678 }
9679 }
9680
9681 /* Call FN for each register that is saved by the current function.
9682 SP_OFFSET is the offset of the current stack pointer from the start
9683 of the frame. */
9684
9685 static void
9686 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
9687 mips_save_restore_fn fn)
9688 {
9689 enum machine_mode fpr_mode;
9690 HOST_WIDE_INT offset;
9691 int regno;
9692
9693 /* Save registers starting from high to low. The debuggers prefer at least
9694 the return register be stored at func+4, and also it allows us not to
9695 need a nop in the epilogue if at least one register is reloaded in
9696 addition to return address. */
9697 offset = cfun->machine->frame.gp_sp_offset - sp_offset;
9698 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
9699 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
9700 {
9701 /* Record the ra offset for use by mips_function_profiler. */
9702 if (regno == RETURN_ADDR_REGNUM)
9703 cfun->machine->frame.ra_fp_offset = offset + sp_offset;
9704 mips_save_restore_reg (word_mode, regno, offset, fn);
9705 offset -= UNITS_PER_WORD;
9706 }
9707
9708 /* This loop must iterate over the same space as its companion in
9709 mips_compute_frame_info. */
9710 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
9711 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
9712 for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
9713 regno >= FP_REG_FIRST;
9714 regno -= MAX_FPRS_PER_FMT)
9715 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
9716 {
9717 mips_save_restore_reg (fpr_mode, regno, offset, fn);
9718 offset -= GET_MODE_SIZE (fpr_mode);
9719 }
9720 }
9721
9722 /* Return true if a move between register REGNO and its save slot (MEM)
9723 can be done in a single move. LOAD_P is true if we are loading
9724 from the slot, false if we are storing to it. */
9725
9726 static bool
9727 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
9728 {
9729 /* There is a specific MIPS16 instruction for saving $31 to the stack. */
9730 if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
9731 return false;
9732
9733 return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
9734 GET_MODE (mem), mem, load_p) == NO_REGS;
9735 }
9736
9737 /* Emit a move from SRC to DEST, given that one of them is a register
9738 save slot and that the other is a register. TEMP is a temporary
9739 GPR of the same mode that is available if need be. */
9740
9741 void
9742 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
9743 {
9744 unsigned int regno;
9745 rtx mem;
9746
9747 if (REG_P (src))
9748 {
9749 regno = REGNO (src);
9750 mem = dest;
9751 }
9752 else
9753 {
9754 regno = REGNO (dest);
9755 mem = src;
9756 }
9757
9758 if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
9759 {
9760 /* We don't yet know whether we'll need this instruction or not.
9761 Postpone the decision by emitting a ghost move. This move
9762 is specifically not frame-related; only the split version is. */
9763 if (TARGET_64BIT)
9764 emit_insn (gen_move_gpdi (dest, src));
9765 else
9766 emit_insn (gen_move_gpsi (dest, src));
9767 return;
9768 }
9769
9770 if (regno == HI_REGNUM)
9771 {
9772 if (REG_P (dest))
9773 {
9774 mips_emit_move (temp, src);
9775 if (TARGET_64BIT)
9776 emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
9777 temp, gen_rtx_REG (DImode, LO_REGNUM)));
9778 else
9779 emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
9780 temp, gen_rtx_REG (SImode, LO_REGNUM)));
9781 }
9782 else
9783 {
9784 if (TARGET_64BIT)
9785 emit_insn (gen_mfhidi_ti (temp,
9786 gen_rtx_REG (TImode, MD_REG_FIRST)));
9787 else
9788 emit_insn (gen_mfhisi_di (temp,
9789 gen_rtx_REG (DImode, MD_REG_FIRST)));
9790 mips_emit_move (dest, temp);
9791 }
9792 }
9793 else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
9794 mips_emit_move (dest, src);
9795 else
9796 {
9797 gcc_assert (!reg_overlap_mentioned_p (dest, temp));
9798 mips_emit_move (temp, src);
9799 mips_emit_move (dest, temp);
9800 }
9801 if (MEM_P (dest))
9802 mips_set_frame_expr (mips_frame_set (dest, src));
9803 }
9804 \f
9805 /* If we're generating n32 or n64 abicalls, and the current function
9806 does not use $28 as its global pointer, emit a cplocal directive.
9807 Use pic_offset_table_rtx as the argument to the directive. */
9808
9809 static void
9810 mips_output_cplocal (void)
9811 {
9812 if (!TARGET_EXPLICIT_RELOCS
9813 && mips_must_initialize_gp_p ()
9814 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
9815 output_asm_insn (".cplocal %+", 0);
9816 }
9817
9818 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE. */
9819
9820 static void
9821 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
9822 {
9823 const char *fnname;
9824
9825 #ifdef SDB_DEBUGGING_INFO
9826 if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
9827 SDB_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl));
9828 #endif
9829
9830 /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
9831 floating-point arguments. */
9832 if (TARGET_MIPS16
9833 && TARGET_HARD_FLOAT_ABI
9834 && crtl->args.info.fp_code != 0)
9835 mips16_build_function_stub ();
9836
9837 /* Get the function name the same way that toplev.c does before calling
9838 assemble_start_function. This is needed so that the name used here
9839 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
9840 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
9841 mips_start_function_definition (fnname, TARGET_MIPS16);
9842
9843 /* Output MIPS-specific frame information. */
9844 if (!flag_inhibit_size_directive)
9845 {
9846 const struct mips_frame_info *frame;
9847
9848 frame = &cfun->machine->frame;
9849
9850 /* .frame FRAMEREG, FRAMESIZE, RETREG. */
9851 fprintf (file,
9852 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
9853 "# vars= " HOST_WIDE_INT_PRINT_DEC
9854 ", regs= %d/%d"
9855 ", args= " HOST_WIDE_INT_PRINT_DEC
9856 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
9857 reg_names[frame_pointer_needed
9858 ? HARD_FRAME_POINTER_REGNUM
9859 : STACK_POINTER_REGNUM],
9860 (frame_pointer_needed
9861 ? frame->total_size - frame->hard_frame_pointer_offset
9862 : frame->total_size),
9863 reg_names[RETURN_ADDR_REGNUM],
9864 frame->var_size,
9865 frame->num_gp, frame->num_fp,
9866 frame->args_size,
9867 frame->cprestore_size);
9868
9869 /* .mask MASK, OFFSET. */
9870 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
9871 frame->mask, frame->gp_save_offset);
9872
9873 /* .fmask MASK, OFFSET. */
9874 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
9875 frame->fmask, frame->fp_save_offset);
9876 }
9877
9878 /* Handle the initialization of $gp for SVR4 PIC, if applicable.
9879 Also emit the ".set noreorder; .set nomacro" sequence for functions
9880 that need it. */
9881 if (mips_must_initialize_gp_p ()
9882 && mips_current_loadgp_style () == LOADGP_OLDABI)
9883 {
9884 if (TARGET_MIPS16)
9885 {
9886 /* This is a fixed-form sequence. The position of the
9887 first two instructions is important because of the
9888 way _gp_disp is defined. */
9889 output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
9890 output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
9891 output_asm_insn ("sll\t$2,16", 0);
9892 output_asm_insn ("addu\t$2,$3", 0);
9893 }
9894 else
9895 {
9896 /* .cpload must be in a .set noreorder but not a
9897 .set nomacro block. */
9898 mips_push_asm_switch (&mips_noreorder);
9899 output_asm_insn (".cpload\t%^", 0);
9900 if (!cfun->machine->all_noreorder_p)
9901 mips_pop_asm_switch (&mips_noreorder);
9902 else
9903 mips_push_asm_switch (&mips_nomacro);
9904 }
9905 }
9906 else if (cfun->machine->all_noreorder_p)
9907 {
9908 mips_push_asm_switch (&mips_noreorder);
9909 mips_push_asm_switch (&mips_nomacro);
9910 }
9911
9912 /* Tell the assembler which register we're using as the global
9913 pointer. This is needed for thunks, since they can use either
9914 explicit relocs or assembler macros. */
9915 mips_output_cplocal ();
9916 }
9917
9918 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE. */
9919
9920 static void
9921 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9922 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
9923 {
9924 const char *fnname;
9925
9926 /* Reinstate the normal $gp. */
9927 SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
9928 mips_output_cplocal ();
9929
9930 if (cfun->machine->all_noreorder_p)
9931 {
9932 mips_pop_asm_switch (&mips_nomacro);
9933 mips_pop_asm_switch (&mips_noreorder);
9934 }
9935
9936 /* Get the function name the same way that toplev.c does before calling
9937 assemble_start_function. This is needed so that the name used here
9938 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
9939 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
9940 mips_end_function_definition (fnname);
9941 }
9942 \f
9943 /* Save register REG to MEM. Make the instruction frame-related. */
9944
9945 static void
9946 mips_save_reg (rtx reg, rtx mem)
9947 {
9948 if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
9949 {
9950 rtx x1, x2;
9951
9952 if (mips_split_64bit_move_p (mem, reg))
9953 mips_split_doubleword_move (mem, reg);
9954 else
9955 mips_emit_move (mem, reg);
9956
9957 x1 = mips_frame_set (mips_subword (mem, false),
9958 mips_subword (reg, false));
9959 x2 = mips_frame_set (mips_subword (mem, true),
9960 mips_subword (reg, true));
9961 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
9962 }
9963 else
9964 mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
9965 }
9966
9967 /* The __gnu_local_gp symbol. */
9968
9969 static GTY(()) rtx mips_gnu_local_gp;
9970
9971 /* If we're generating n32 or n64 abicalls, emit instructions
9972 to set up the global pointer. */
9973
9974 static void
9975 mips_emit_loadgp (void)
9976 {
9977 rtx addr, offset, incoming_address, base, index, pic_reg;
9978
9979 pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
9980 switch (mips_current_loadgp_style ())
9981 {
9982 case LOADGP_ABSOLUTE:
9983 if (mips_gnu_local_gp == NULL)
9984 {
9985 mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
9986 SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
9987 }
9988 emit_insn (Pmode == SImode
9989 ? gen_loadgp_absolute_si (pic_reg, mips_gnu_local_gp)
9990 : gen_loadgp_absolute_di (pic_reg, mips_gnu_local_gp));
9991 break;
9992
9993 case LOADGP_OLDABI:
9994 /* Added by mips_output_function_prologue. */
9995 break;
9996
9997 case LOADGP_NEWABI:
9998 addr = XEXP (DECL_RTL (current_function_decl), 0);
9999 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
10000 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
10001 emit_insn (Pmode == SImode
10002 ? gen_loadgp_newabi_si (pic_reg, offset, incoming_address)
10003 : gen_loadgp_newabi_di (pic_reg, offset, incoming_address));
10004 break;
10005
10006 case LOADGP_RTP:
10007 base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
10008 index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
10009 emit_insn (Pmode == SImode
10010 ? gen_loadgp_rtp_si (pic_reg, base, index)
10011 : gen_loadgp_rtp_di (pic_reg, base, index));
10012 break;
10013
10014 default:
10015 return;
10016 }
10017
10018 if (TARGET_MIPS16)
10019 emit_insn (gen_copygp_mips16 (pic_offset_table_rtx, pic_reg));
10020
10021 /* Emit a blockage if there are implicit uses of the GP register.
10022 This includes profiled functions, because FUNCTION_PROFILE uses
10023 a jal macro. */
10024 if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
10025 emit_insn (gen_loadgp_blockage ());
10026 }
10027
10028 /* A for_each_rtx callback. Stop the search if *X is a kernel register. */
10029
10030 static int
10031 mips_kernel_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
10032 {
10033 return REG_P (*x) && KERNEL_REG_P (REGNO (*x));
10034 }
10035
10036 /* Expand the "prologue" pattern. */
10037
10038 void
10039 mips_expand_prologue (void)
10040 {
10041 const struct mips_frame_info *frame;
10042 HOST_WIDE_INT size;
10043 unsigned int nargs;
10044 rtx insn;
10045
10046 if (cfun->machine->global_pointer != INVALID_REGNUM)
10047 {
10048 /* Check whether an insn uses pic_offset_table_rtx, either explicitly
10049 or implicitly. If so, we can commit to using a global pointer
10050 straight away, otherwise we need to defer the decision. */
10051 if (mips_cfun_has_inflexible_gp_ref_p ()
10052 || mips_cfun_has_flexible_gp_ref_p ())
10053 {
10054 cfun->machine->must_initialize_gp_p = true;
10055 cfun->machine->must_restore_gp_when_clobbered_p = true;
10056 }
10057
10058 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
10059 }
10060
10061 frame = &cfun->machine->frame;
10062 size = frame->total_size;
10063
10064 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
10065 bytes beforehand; this is enough to cover the register save area
10066 without going out of range. */
10067 if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
10068 || frame->num_cop0_regs > 0)
10069 {
10070 HOST_WIDE_INT step1;
10071
10072 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
10073 if (GENERATE_MIPS16E_SAVE_RESTORE)
10074 {
10075 HOST_WIDE_INT offset;
10076 unsigned int mask, regno;
10077
10078 /* Try to merge argument stores into the save instruction. */
10079 nargs = mips16e_collect_argument_saves ();
10080
10081 /* Build the save instruction. */
10082 mask = frame->mask;
10083 insn = mips16e_build_save_restore (false, &mask, &offset,
10084 nargs, step1);
10085 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10086 size -= step1;
10087
10088 /* Check if we need to save other registers. */
10089 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
10090 if (BITSET_P (mask, regno - GP_REG_FIRST))
10091 {
10092 offset -= UNITS_PER_WORD;
10093 mips_save_restore_reg (word_mode, regno,
10094 offset, mips_save_reg);
10095 }
10096 }
10097 else
10098 {
10099 if (cfun->machine->interrupt_handler_p)
10100 {
10101 HOST_WIDE_INT offset;
10102 rtx mem;
10103
10104 /* If this interrupt is using a shadow register set, we need to
10105 get the stack pointer from the previous register set. */
10106 if (cfun->machine->use_shadow_register_set_p)
10107 emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
10108 stack_pointer_rtx));
10109
10110 if (!cfun->machine->keep_interrupts_masked_p)
10111 {
10112 /* Move from COP0 Cause to K0. */
10113 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
10114 gen_rtx_REG (SImode,
10115 COP0_CAUSE_REG_NUM)));
10116 /* Move from COP0 EPC to K1. */
10117 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
10118 gen_rtx_REG (SImode,
10119 COP0_EPC_REG_NUM)));
10120 }
10121
10122 /* Allocate the first part of the frame. */
10123 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
10124 GEN_INT (-step1));
10125 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10126 size -= step1;
10127
10128 /* Start at the uppermost location for saving. */
10129 offset = frame->cop0_sp_offset - size;
10130 if (!cfun->machine->keep_interrupts_masked_p)
10131 {
10132 /* Push EPC into its stack slot. */
10133 mem = gen_frame_mem (word_mode,
10134 plus_constant (stack_pointer_rtx,
10135 offset));
10136 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
10137 offset -= UNITS_PER_WORD;
10138 }
10139
10140 /* Move from COP0 Status to K1. */
10141 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
10142 gen_rtx_REG (SImode,
10143 COP0_STATUS_REG_NUM)));
10144
10145 /* Right justify the RIPL in k0. */
10146 if (!cfun->machine->keep_interrupts_masked_p)
10147 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
10148 gen_rtx_REG (SImode, K0_REG_NUM),
10149 GEN_INT (CAUSE_IPL)));
10150
10151 /* Push Status into its stack slot. */
10152 mem = gen_frame_mem (word_mode,
10153 plus_constant (stack_pointer_rtx, offset));
10154 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
10155 offset -= UNITS_PER_WORD;
10156
10157 /* Insert the RIPL into our copy of SR (k1) as the new IPL. */
10158 if (!cfun->machine->keep_interrupts_masked_p)
10159 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10160 GEN_INT (6),
10161 GEN_INT (SR_IPL),
10162 gen_rtx_REG (SImode, K0_REG_NUM)));
10163
10164 if (!cfun->machine->keep_interrupts_masked_p)
10165 /* Enable interrupts by clearing the KSU ERL and EXL bits.
10166 IE is already the correct value, so we don't have to do
10167 anything explicit. */
10168 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10169 GEN_INT (4),
10170 GEN_INT (SR_EXL),
10171 gen_rtx_REG (SImode, GP_REG_FIRST)));
10172 else
10173 /* Disable interrupts by clearing the KSU, ERL, EXL,
10174 and IE bits. */
10175 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
10176 GEN_INT (5),
10177 GEN_INT (SR_IE),
10178 gen_rtx_REG (SImode, GP_REG_FIRST)));
10179 }
10180 else
10181 {
10182 insn = gen_add3_insn (stack_pointer_rtx,
10183 stack_pointer_rtx,
10184 GEN_INT (-step1));
10185 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10186 size -= step1;
10187 }
10188 mips_for_each_saved_acc (size, mips_save_reg);
10189 mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
10190 }
10191 }
10192
10193 /* Allocate the rest of the frame. */
10194 if (size > 0)
10195 {
10196 if (SMALL_OPERAND (-size))
10197 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
10198 stack_pointer_rtx,
10199 GEN_INT (-size)))) = 1;
10200 else
10201 {
10202 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
10203 if (TARGET_MIPS16)
10204 {
10205 /* There are no instructions to add or subtract registers
10206 from the stack pointer, so use the frame pointer as a
10207 temporary. We should always be using a frame pointer
10208 in this case anyway. */
10209 gcc_assert (frame_pointer_needed);
10210 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10211 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
10212 hard_frame_pointer_rtx,
10213 MIPS_PROLOGUE_TEMP (Pmode)));
10214 mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
10215 }
10216 else
10217 emit_insn (gen_sub3_insn (stack_pointer_rtx,
10218 stack_pointer_rtx,
10219 MIPS_PROLOGUE_TEMP (Pmode)));
10220
10221 /* Describe the combined effect of the previous instructions. */
10222 mips_set_frame_expr
10223 (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10224 plus_constant (stack_pointer_rtx, -size)));
10225 }
10226 }
10227
10228 /* Set up the frame pointer, if we're using one. */
10229 if (frame_pointer_needed)
10230 {
10231 HOST_WIDE_INT offset;
10232
10233 offset = frame->hard_frame_pointer_offset;
10234 if (offset == 0)
10235 {
10236 insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10237 RTX_FRAME_RELATED_P (insn) = 1;
10238 }
10239 else if (SMALL_OPERAND (offset))
10240 {
10241 insn = gen_add3_insn (hard_frame_pointer_rtx,
10242 stack_pointer_rtx, GEN_INT (offset));
10243 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
10244 }
10245 else
10246 {
10247 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
10248 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
10249 emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
10250 hard_frame_pointer_rtx,
10251 MIPS_PROLOGUE_TEMP (Pmode)));
10252 mips_set_frame_expr
10253 (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
10254 plus_constant (stack_pointer_rtx, offset)));
10255 }
10256 }
10257
10258 mips_emit_loadgp ();
10259
10260 /* Initialize the $gp save slot. */
10261 if (mips_cfun_has_cprestore_slot_p ())
10262 {
10263 rtx base, mem, gp, temp;
10264 HOST_WIDE_INT offset;
10265
10266 mips_get_cprestore_base_and_offset (&base, &offset, false);
10267 mem = gen_frame_mem (Pmode, plus_constant (base, offset));
10268 gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10269 temp = (SMALL_OPERAND (offset)
10270 ? gen_rtx_SCRATCH (Pmode)
10271 : MIPS_PROLOGUE_TEMP (Pmode));
10272 emit_insn (gen_potential_cprestore (mem, GEN_INT (offset), gp, temp));
10273
10274 mips_get_cprestore_base_and_offset (&base, &offset, true);
10275 mem = gen_frame_mem (Pmode, plus_constant (base, offset));
10276 emit_insn (gen_use_cprestore (mem));
10277 }
10278
10279 /* We need to search back to the last use of K0 or K1. */
10280 if (cfun->machine->interrupt_handler_p)
10281 {
10282 for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
10283 if (INSN_P (insn)
10284 && for_each_rtx (&PATTERN (insn), mips_kernel_reg_p, NULL))
10285 break;
10286 /* Emit a move from K1 to COP0 Status after insn. */
10287 gcc_assert (insn != NULL_RTX);
10288 emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
10289 gen_rtx_REG (SImode, K1_REG_NUM)),
10290 insn);
10291 }
10292
10293 /* If we are profiling, make sure no instructions are scheduled before
10294 the call to mcount. */
10295 if (crtl->profile)
10296 emit_insn (gen_blockage ());
10297 }
10298 \f
10299 /* Emit instructions to restore register REG from slot MEM. */
10300
10301 static void
10302 mips_restore_reg (rtx reg, rtx mem)
10303 {
10304 /* There's no MIPS16 instruction to load $31 directly. Load into
10305 $7 instead and adjust the return insn appropriately. */
10306 if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
10307 reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
10308
10309 mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
10310 }
10311
10312 /* Emit any instructions needed before a return. */
10313
10314 void
10315 mips_expand_before_return (void)
10316 {
10317 /* When using a call-clobbered gp, we start out with unified call
10318 insns that include instructions to restore the gp. We then split
10319 these unified calls after reload. These split calls explicitly
10320 clobber gp, so there is no need to define
10321 PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
10322
10323 For consistency, we should also insert an explicit clobber of $28
10324 before return insns, so that the post-reload optimizers know that
10325 the register is not live on exit. */
10326 if (TARGET_CALL_CLOBBERED_GP)
10327 emit_clobber (pic_offset_table_rtx);
10328 }
10329
10330 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
10331 says which. */
10332
10333 void
10334 mips_expand_epilogue (bool sibcall_p)
10335 {
10336 const struct mips_frame_info *frame;
10337 HOST_WIDE_INT step1, step2;
10338 rtx base, target, insn;
10339
10340 if (!sibcall_p && mips_can_use_return_insn ())
10341 {
10342 emit_jump_insn (gen_return ());
10343 return;
10344 }
10345
10346 /* In MIPS16 mode, if the return value should go into a floating-point
10347 register, we need to call a helper routine to copy it over. */
10348 if (mips16_cfun_returns_in_fpr_p ())
10349 mips16_copy_fpr_return_value ();
10350
10351 /* Split the frame into two. STEP1 is the amount of stack we should
10352 deallocate before restoring the registers. STEP2 is the amount we
10353 should deallocate afterwards.
10354
10355 Start off by assuming that no registers need to be restored. */
10356 frame = &cfun->machine->frame;
10357 step1 = frame->total_size;
10358 step2 = 0;
10359
10360 /* Work out which register holds the frame address. */
10361 if (!frame_pointer_needed)
10362 base = stack_pointer_rtx;
10363 else
10364 {
10365 base = hard_frame_pointer_rtx;
10366 step1 -= frame->hard_frame_pointer_offset;
10367 }
10368
10369 /* If we need to restore registers, deallocate as much stack as
10370 possible in the second step without going out of range. */
10371 if ((frame->mask | frame->fmask | frame->acc_mask) != 0
10372 || frame->num_cop0_regs > 0)
10373 {
10374 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
10375 step1 -= step2;
10376 }
10377
10378 /* Set TARGET to BASE + STEP1. */
10379 target = base;
10380 if (step1 > 0)
10381 {
10382 rtx adjust;
10383
10384 /* Get an rtx for STEP1 that we can add to BASE. */
10385 adjust = GEN_INT (step1);
10386 if (!SMALL_OPERAND (step1))
10387 {
10388 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
10389 adjust = MIPS_EPILOGUE_TEMP (Pmode);
10390 }
10391
10392 /* Normal mode code can copy the result straight into $sp. */
10393 if (!TARGET_MIPS16)
10394 target = stack_pointer_rtx;
10395
10396 emit_insn (gen_add3_insn (target, base, adjust));
10397 }
10398
10399 /* Copy TARGET into the stack pointer. */
10400 if (target != stack_pointer_rtx)
10401 mips_emit_move (stack_pointer_rtx, target);
10402
10403 /* If we're using addressing macros, $gp is implicitly used by all
10404 SYMBOL_REFs. We must emit a blockage insn before restoring $gp
10405 from the stack. */
10406 if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
10407 emit_insn (gen_blockage ());
10408
10409 if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
10410 {
10411 unsigned int regno, mask;
10412 HOST_WIDE_INT offset;
10413 rtx restore;
10414
10415 /* Generate the restore instruction. */
10416 mask = frame->mask;
10417 restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
10418
10419 /* Restore any other registers manually. */
10420 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
10421 if (BITSET_P (mask, regno - GP_REG_FIRST))
10422 {
10423 offset -= UNITS_PER_WORD;
10424 mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
10425 }
10426
10427 /* Restore the remaining registers and deallocate the final bit
10428 of the frame. */
10429 emit_insn (restore);
10430 }
10431 else
10432 {
10433 /* Restore the registers. */
10434 mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
10435 mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
10436 mips_restore_reg);
10437
10438 if (cfun->machine->interrupt_handler_p)
10439 {
10440 HOST_WIDE_INT offset;
10441 rtx mem;
10442
10443 offset = frame->cop0_sp_offset - (frame->total_size - step2);
10444 if (!cfun->machine->keep_interrupts_masked_p)
10445 {
10446 /* Restore the original EPC. */
10447 mem = gen_frame_mem (word_mode,
10448 plus_constant (stack_pointer_rtx, offset));
10449 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
10450 offset -= UNITS_PER_WORD;
10451
10452 /* Move to COP0 EPC. */
10453 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
10454 gen_rtx_REG (SImode, K0_REG_NUM)));
10455 }
10456
10457 /* Restore the original Status. */
10458 mem = gen_frame_mem (word_mode,
10459 plus_constant (stack_pointer_rtx, offset));
10460 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
10461 offset -= UNITS_PER_WORD;
10462
10463 /* If we don't use shoadow register set, we need to update SP. */
10464 if (!cfun->machine->use_shadow_register_set_p && step2 > 0)
10465 emit_insn (gen_add3_insn (stack_pointer_rtx,
10466 stack_pointer_rtx,
10467 GEN_INT (step2)));
10468
10469 /* Move to COP0 Status. */
10470 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
10471 gen_rtx_REG (SImode, K0_REG_NUM)));
10472 }
10473 else
10474 {
10475 /* Deallocate the final bit of the frame. */
10476 if (step2 > 0)
10477 emit_insn (gen_add3_insn (stack_pointer_rtx,
10478 stack_pointer_rtx,
10479 GEN_INT (step2)));
10480 }
10481 }
10482
10483 /* Add in the __builtin_eh_return stack adjustment. We need to
10484 use a temporary in MIPS16 code. */
10485 if (crtl->calls_eh_return)
10486 {
10487 if (TARGET_MIPS16)
10488 {
10489 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
10490 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
10491 MIPS_EPILOGUE_TEMP (Pmode),
10492 EH_RETURN_STACKADJ_RTX));
10493 mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
10494 }
10495 else
10496 emit_insn (gen_add3_insn (stack_pointer_rtx,
10497 stack_pointer_rtx,
10498 EH_RETURN_STACKADJ_RTX));
10499 }
10500
10501 if (!sibcall_p)
10502 {
10503 mips_expand_before_return ();
10504 if (cfun->machine->interrupt_handler_p)
10505 {
10506 /* Interrupt handlers generate eret or deret. */
10507 if (cfun->machine->use_debug_exception_return_p)
10508 emit_jump_insn (gen_mips_deret ());
10509 else
10510 emit_jump_insn (gen_mips_eret ());
10511 }
10512 else
10513 {
10514 unsigned int regno;
10515
10516 /* When generating MIPS16 code, the normal
10517 mips_for_each_saved_gpr_and_fpr path will restore the return
10518 address into $7 rather than $31. */
10519 if (TARGET_MIPS16
10520 && !GENERATE_MIPS16E_SAVE_RESTORE
10521 && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
10522 regno = GP_REG_FIRST + 7;
10523 else
10524 regno = RETURN_ADDR_REGNUM;
10525 emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode, regno)));
10526 }
10527 }
10528
10529 /* Search from the beginning to the first use of K0 or K1. */
10530 if (cfun->machine->interrupt_handler_p
10531 && !cfun->machine->keep_interrupts_masked_p)
10532 {
10533 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
10534 if (INSN_P (insn)
10535 && for_each_rtx (&PATTERN(insn), mips_kernel_reg_p, NULL))
10536 break;
10537 gcc_assert (insn != NULL_RTX);
10538 /* Insert disable interrupts before the first use of K0 or K1. */
10539 emit_insn_before (gen_mips_di (), insn);
10540 emit_insn_before (gen_mips_ehb (), insn);
10541 }
10542 }
10543 \f
10544 /* Return nonzero if this function is known to have a null epilogue.
10545 This allows the optimizer to omit jumps to jumps if no stack
10546 was created. */
10547
10548 bool
10549 mips_can_use_return_insn (void)
10550 {
10551 /* Interrupt handlers need to go through the epilogue. */
10552 if (cfun->machine->interrupt_handler_p)
10553 return false;
10554
10555 if (!reload_completed)
10556 return false;
10557
10558 if (crtl->profile)
10559 return false;
10560
10561 /* In MIPS16 mode, a function that returns a floating-point value
10562 needs to arrange to copy the return value into the floating-point
10563 registers. */
10564 if (mips16_cfun_returns_in_fpr_p ())
10565 return false;
10566
10567 return cfun->machine->frame.total_size == 0;
10568 }
10569 \f
10570 /* Return true if register REGNO can store a value of mode MODE.
10571 The result of this function is cached in mips_hard_regno_mode_ok. */
10572
10573 static bool
10574 mips_hard_regno_mode_ok_p (unsigned int regno, enum machine_mode mode)
10575 {
10576 unsigned int size;
10577 enum mode_class mclass;
10578
10579 if (mode == CCV2mode)
10580 return (ISA_HAS_8CC
10581 && ST_REG_P (regno)
10582 && (regno - ST_REG_FIRST) % 2 == 0);
10583
10584 if (mode == CCV4mode)
10585 return (ISA_HAS_8CC
10586 && ST_REG_P (regno)
10587 && (regno - ST_REG_FIRST) % 4 == 0);
10588
10589 if (mode == CCmode)
10590 {
10591 if (!ISA_HAS_8CC)
10592 return regno == FPSW_REGNUM;
10593
10594 return (ST_REG_P (regno)
10595 || GP_REG_P (regno)
10596 || FP_REG_P (regno));
10597 }
10598
10599 size = GET_MODE_SIZE (mode);
10600 mclass = GET_MODE_CLASS (mode);
10601
10602 if (GP_REG_P (regno))
10603 return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
10604
10605 if (FP_REG_P (regno)
10606 && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
10607 || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
10608 {
10609 /* Allow TFmode for CCmode reloads. */
10610 if (mode == TFmode && ISA_HAS_8CC)
10611 return true;
10612
10613 /* Allow 64-bit vector modes for Loongson-2E/2F. */
10614 if (TARGET_LOONGSON_VECTORS
10615 && (mode == V2SImode
10616 || mode == V4HImode
10617 || mode == V8QImode
10618 || mode == DImode))
10619 return true;
10620
10621 if (mclass == MODE_FLOAT
10622 || mclass == MODE_COMPLEX_FLOAT
10623 || mclass == MODE_VECTOR_FLOAT)
10624 return size <= UNITS_PER_FPVALUE;
10625
10626 /* Allow integer modes that fit into a single register. We need
10627 to put integers into FPRs when using instructions like CVT
10628 and TRUNC. There's no point allowing sizes smaller than a word,
10629 because the FPU has no appropriate load/store instructions. */
10630 if (mclass == MODE_INT)
10631 return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
10632 }
10633
10634 if (ACC_REG_P (regno)
10635 && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
10636 {
10637 if (MD_REG_P (regno))
10638 {
10639 /* After a multiplication or division, clobbering HI makes
10640 the value of LO unpredictable, and vice versa. This means
10641 that, for all interesting cases, HI and LO are effectively
10642 a single register.
10643
10644 We model this by requiring that any value that uses HI
10645 also uses LO. */
10646 if (size <= UNITS_PER_WORD * 2)
10647 return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
10648 }
10649 else
10650 {
10651 /* DSP accumulators do not have the same restrictions as
10652 HI and LO, so we can treat them as normal doubleword
10653 registers. */
10654 if (size <= UNITS_PER_WORD)
10655 return true;
10656
10657 if (size <= UNITS_PER_WORD * 2
10658 && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
10659 return true;
10660 }
10661 }
10662
10663 if (ALL_COP_REG_P (regno))
10664 return mclass == MODE_INT && size <= UNITS_PER_WORD;
10665
10666 if (regno == GOT_VERSION_REGNUM)
10667 return mode == SImode;
10668
10669 return false;
10670 }
10671
10672 /* Implement HARD_REGNO_NREGS. */
10673
10674 unsigned int
10675 mips_hard_regno_nregs (int regno, enum machine_mode mode)
10676 {
10677 if (ST_REG_P (regno))
10678 /* The size of FP status registers is always 4, because they only hold
10679 CCmode values, and CCmode is always considered to be 4 bytes wide. */
10680 return (GET_MODE_SIZE (mode) + 3) / 4;
10681
10682 if (FP_REG_P (regno))
10683 return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
10684
10685 /* All other registers are word-sized. */
10686 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
10687 }
10688
10689 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
10690 in mips_hard_regno_nregs. */
10691
10692 int
10693 mips_class_max_nregs (enum reg_class rclass, enum machine_mode mode)
10694 {
10695 int size;
10696 HARD_REG_SET left;
10697
10698 size = 0x8000;
10699 COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
10700 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
10701 {
10702 size = MIN (size, 4);
10703 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
10704 }
10705 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
10706 {
10707 size = MIN (size, UNITS_PER_FPREG);
10708 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
10709 }
10710 if (!hard_reg_set_empty_p (left))
10711 size = MIN (size, UNITS_PER_WORD);
10712 return (GET_MODE_SIZE (mode) + size - 1) / size;
10713 }
10714
10715 /* Implement CANNOT_CHANGE_MODE_CLASS. */
10716
10717 bool
10718 mips_cannot_change_mode_class (enum machine_mode from ATTRIBUTE_UNUSED,
10719 enum machine_mode to ATTRIBUTE_UNUSED,
10720 enum reg_class rclass)
10721 {
10722 /* There are several problems with changing the modes of values
10723 in floating-point registers:
10724
10725 - When a multi-word value is stored in paired floating-point
10726 registers, the first register always holds the low word.
10727 We therefore can't allow FPRs to change between single-word
10728 and multi-word modes on big-endian targets.
10729
10730 - GCC assumes that each word of a multiword register can be accessed
10731 individually using SUBREGs. This is not true for floating-point
10732 registers if they are bigger than a word.
10733
10734 - Loading a 32-bit value into a 64-bit floating-point register
10735 will not sign-extend the value, despite what LOAD_EXTEND_OP says.
10736 We can't allow FPRs to change from SImode to to a wider mode on
10737 64-bit targets.
10738
10739 - If the FPU has already interpreted a value in one format, we must
10740 not ask it to treat the value as having a different format.
10741
10742 We therefore disallow all mode changes involving FPRs. */
10743 return reg_classes_intersect_p (FP_REGS, rclass);
10744 }
10745
10746 /* Implement target hook small_register_classes_for_mode_p. */
10747
10748 static bool
10749 mips_small_register_classes_for_mode_p (enum machine_mode mode
10750 ATTRIBUTE_UNUSED)
10751 {
10752 return TARGET_MIPS16;
10753 }
10754
10755 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction. */
10756
10757 static bool
10758 mips_mode_ok_for_mov_fmt_p (enum machine_mode mode)
10759 {
10760 switch (mode)
10761 {
10762 case SFmode:
10763 return TARGET_HARD_FLOAT;
10764
10765 case DFmode:
10766 return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
10767
10768 case V2SFmode:
10769 return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
10770
10771 default:
10772 return false;
10773 }
10774 }
10775
10776 /* Implement MODES_TIEABLE_P. */
10777
10778 bool
10779 mips_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
10780 {
10781 /* FPRs allow no mode punning, so it's not worth tying modes if we'd
10782 prefer to put one of them in FPRs. */
10783 return (mode1 == mode2
10784 || (!mips_mode_ok_for_mov_fmt_p (mode1)
10785 && !mips_mode_ok_for_mov_fmt_p (mode2)));
10786 }
10787
10788 /* Implement PREFERRED_RELOAD_CLASS. */
10789
10790 enum reg_class
10791 mips_preferred_reload_class (rtx x, enum reg_class rclass)
10792 {
10793 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
10794 return LEA_REGS;
10795
10796 if (reg_class_subset_p (FP_REGS, rclass)
10797 && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
10798 return FP_REGS;
10799
10800 if (reg_class_subset_p (GR_REGS, rclass))
10801 rclass = GR_REGS;
10802
10803 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
10804 rclass = M16_REGS;
10805
10806 return rclass;
10807 }
10808
10809 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
10810 Return a "canonical" class to represent it in later calculations. */
10811
10812 static enum reg_class
10813 mips_canonicalize_move_class (enum reg_class rclass)
10814 {
10815 /* All moves involving accumulator registers have the same cost. */
10816 if (reg_class_subset_p (rclass, ACC_REGS))
10817 rclass = ACC_REGS;
10818
10819 /* Likewise promote subclasses of general registers to the most
10820 interesting containing class. */
10821 if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
10822 rclass = M16_REGS;
10823 else if (reg_class_subset_p (rclass, GENERAL_REGS))
10824 rclass = GENERAL_REGS;
10825
10826 return rclass;
10827 }
10828
10829 /* Return the cost of moving a value of mode MODE from a register of
10830 class FROM to a GPR. Return 0 for classes that are unions of other
10831 classes handled by this function. */
10832
10833 static int
10834 mips_move_to_gpr_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
10835 enum reg_class from)
10836 {
10837 switch (from)
10838 {
10839 case GENERAL_REGS:
10840 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
10841 return 2;
10842
10843 case ACC_REGS:
10844 /* MFLO and MFHI. */
10845 return 6;
10846
10847 case FP_REGS:
10848 /* MFC1, etc. */
10849 return 4;
10850
10851 case ST_REGS:
10852 /* LUI followed by MOVF. */
10853 return 4;
10854
10855 case COP0_REGS:
10856 case COP2_REGS:
10857 case COP3_REGS:
10858 /* This choice of value is historical. */
10859 return 5;
10860
10861 default:
10862 return 0;
10863 }
10864 }
10865
10866 /* Return the cost of moving a value of mode MODE from a GPR to a
10867 register of class TO. Return 0 for classes that are unions of
10868 other classes handled by this function. */
10869
10870 static int
10871 mips_move_from_gpr_cost (enum machine_mode mode, enum reg_class to)
10872 {
10873 switch (to)
10874 {
10875 case GENERAL_REGS:
10876 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
10877 return 2;
10878
10879 case ACC_REGS:
10880 /* MTLO and MTHI. */
10881 return 6;
10882
10883 case FP_REGS:
10884 /* MTC1, etc. */
10885 return 4;
10886
10887 case ST_REGS:
10888 /* A secondary reload through an FPR scratch. */
10889 return (mips_register_move_cost (mode, GENERAL_REGS, FP_REGS)
10890 + mips_register_move_cost (mode, FP_REGS, ST_REGS));
10891
10892 case COP0_REGS:
10893 case COP2_REGS:
10894 case COP3_REGS:
10895 /* This choice of value is historical. */
10896 return 5;
10897
10898 default:
10899 return 0;
10900 }
10901 }
10902
10903 /* Implement REGISTER_MOVE_COST. Return 0 for classes that are the
10904 maximum of the move costs for subclasses; regclass will work out
10905 the maximum for us. */
10906
10907 int
10908 mips_register_move_cost (enum machine_mode mode,
10909 enum reg_class from, enum reg_class to)
10910 {
10911 enum reg_class dregs;
10912 int cost1, cost2;
10913
10914 from = mips_canonicalize_move_class (from);
10915 to = mips_canonicalize_move_class (to);
10916
10917 /* Handle moves that can be done without using general-purpose registers. */
10918 if (from == FP_REGS)
10919 {
10920 if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
10921 /* MOV.FMT. */
10922 return 4;
10923 if (to == ST_REGS)
10924 /* The sequence generated by mips_expand_fcc_reload. */
10925 return 8;
10926 }
10927
10928 /* Handle cases in which only one class deviates from the ideal. */
10929 dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
10930 if (from == dregs)
10931 return mips_move_from_gpr_cost (mode, to);
10932 if (to == dregs)
10933 return mips_move_to_gpr_cost (mode, from);
10934
10935 /* Handles cases that require a GPR temporary. */
10936 cost1 = mips_move_to_gpr_cost (mode, from);
10937 if (cost1 != 0)
10938 {
10939 cost2 = mips_move_from_gpr_cost (mode, to);
10940 if (cost2 != 0)
10941 return cost1 + cost2;
10942 }
10943
10944 return 0;
10945 }
10946
10947 /* Implement TARGET_IRA_COVER_CLASSES. */
10948
10949 static const reg_class_t *
10950 mips_ira_cover_classes (void)
10951 {
10952 static const reg_class_t acc_classes[] = {
10953 GR_AND_ACC_REGS, FP_REGS, COP0_REGS, COP2_REGS, COP3_REGS,
10954 ST_REGS, LIM_REG_CLASSES
10955 };
10956 static const reg_class_t no_acc_classes[] = {
10957 GR_REGS, FP_REGS, COP0_REGS, COP2_REGS, COP3_REGS,
10958 ST_REGS, LIM_REG_CLASSES
10959 };
10960
10961 /* Don't allow the register allocators to use LO and HI in MIPS16 mode,
10962 which has no MTLO or MTHI instructions. Also, using GR_AND_ACC_REGS
10963 as a cover class only works well when we keep per-register costs.
10964 Using it when not optimizing can cause us to think accumulators
10965 have the same cost as GPRs in cases where GPRs are actually much
10966 cheaper. */
10967 return TARGET_MIPS16 || !optimize ? no_acc_classes : acc_classes;
10968 }
10969
10970 /* Return the register class required for a secondary register when
10971 copying between one of the registers in RCLASS and value X, which
10972 has mode MODE. X is the source of the move if IN_P, otherwise it
10973 is the destination. Return NO_REGS if no secondary register is
10974 needed. */
10975
10976 enum reg_class
10977 mips_secondary_reload_class (enum reg_class rclass,
10978 enum machine_mode mode, rtx x, bool in_p)
10979 {
10980 int regno;
10981
10982 /* If X is a constant that cannot be loaded into $25, it must be loaded
10983 into some other GPR. No other register class allows a direct move. */
10984 if (mips_dangerous_for_la25_p (x))
10985 return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
10986
10987 regno = true_regnum (x);
10988 if (TARGET_MIPS16)
10989 {
10990 /* In MIPS16 mode, every move must involve a member of M16_REGS. */
10991 if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
10992 return M16_REGS;
10993
10994 return NO_REGS;
10995 }
10996
10997 /* Copying from accumulator registers to anywhere other than a general
10998 register requires a temporary general register. */
10999 if (reg_class_subset_p (rclass, ACC_REGS))
11000 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
11001 if (ACC_REG_P (regno))
11002 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11003
11004 /* We can only copy a value to a condition code register from a
11005 floating-point register, and even then we require a scratch
11006 floating-point register. We can only copy a value out of a
11007 condition-code register into a general register. */
11008 if (reg_class_subset_p (rclass, ST_REGS))
11009 {
11010 if (in_p)
11011 return FP_REGS;
11012 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
11013 }
11014 if (ST_REG_P (regno))
11015 {
11016 if (!in_p)
11017 return FP_REGS;
11018 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11019 }
11020
11021 if (reg_class_subset_p (rclass, FP_REGS))
11022 {
11023 if (MEM_P (x)
11024 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8))
11025 /* In this case we can use lwc1, swc1, ldc1 or sdc1. We'll use
11026 pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported. */
11027 return NO_REGS;
11028
11029 if (GP_REG_P (regno) || x == CONST0_RTX (mode))
11030 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
11031 return NO_REGS;
11032
11033 if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (x))
11034 /* We can force the constant to memory and use lwc1
11035 and ldc1. As above, we will use pairs of lwc1s if
11036 ldc1 is not supported. */
11037 return NO_REGS;
11038
11039 if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
11040 /* In this case we can use mov.fmt. */
11041 return NO_REGS;
11042
11043 /* Otherwise, we need to reload through an integer register. */
11044 return GR_REGS;
11045 }
11046 if (FP_REG_P (regno))
11047 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
11048
11049 return NO_REGS;
11050 }
11051
11052 /* Implement TARGET_MODE_REP_EXTENDED. */
11053
11054 static int
11055 mips_mode_rep_extended (enum machine_mode mode, enum machine_mode mode_rep)
11056 {
11057 /* On 64-bit targets, SImode register values are sign-extended to DImode. */
11058 if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
11059 return SIGN_EXTEND;
11060
11061 return UNKNOWN;
11062 }
11063 \f
11064 /* Implement TARGET_VALID_POINTER_MODE. */
11065
11066 static bool
11067 mips_valid_pointer_mode (enum machine_mode mode)
11068 {
11069 return mode == SImode || (TARGET_64BIT && mode == DImode);
11070 }
11071
11072 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P. */
11073
11074 static bool
11075 mips_vector_mode_supported_p (enum machine_mode mode)
11076 {
11077 switch (mode)
11078 {
11079 case V2SFmode:
11080 return TARGET_PAIRED_SINGLE_FLOAT;
11081
11082 case V2HImode:
11083 case V4QImode:
11084 case V2HQmode:
11085 case V2UHQmode:
11086 case V2HAmode:
11087 case V2UHAmode:
11088 case V4QQmode:
11089 case V4UQQmode:
11090 return TARGET_DSP;
11091
11092 case V2SImode:
11093 case V4HImode:
11094 case V8QImode:
11095 return TARGET_LOONGSON_VECTORS;
11096
11097 default:
11098 return false;
11099 }
11100 }
11101
11102 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
11103
11104 static bool
11105 mips_scalar_mode_supported_p (enum machine_mode mode)
11106 {
11107 if (ALL_FIXED_POINT_MODE_P (mode)
11108 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
11109 return true;
11110
11111 return default_scalar_mode_supported_p (mode);
11112 }
11113 \f
11114 /* Implement TARGET_INIT_LIBFUNCS. */
11115
11116 #include "config/gofast.h"
11117
11118 static void
11119 mips_init_libfuncs (void)
11120 {
11121 if (TARGET_FIX_VR4120)
11122 {
11123 /* Register the special divsi3 and modsi3 functions needed to work
11124 around VR4120 division errata. */
11125 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
11126 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
11127 }
11128
11129 if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
11130 {
11131 /* Register the MIPS16 -mhard-float stubs. */
11132 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
11133 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
11134 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
11135 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
11136
11137 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
11138 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
11139 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
11140 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
11141 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
11142 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
11143 set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
11144
11145 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
11146 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
11147 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
11148
11149 if (TARGET_DOUBLE_FLOAT)
11150 {
11151 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
11152 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
11153 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
11154 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
11155
11156 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
11157 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
11158 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
11159 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
11160 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
11161 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
11162 set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
11163
11164 set_conv_libfunc (sext_optab, DFmode, SFmode,
11165 "__mips16_extendsfdf2");
11166 set_conv_libfunc (trunc_optab, SFmode, DFmode,
11167 "__mips16_truncdfsf2");
11168 set_conv_libfunc (sfix_optab, SImode, DFmode,
11169 "__mips16_fix_truncdfsi");
11170 set_conv_libfunc (sfloat_optab, DFmode, SImode,
11171 "__mips16_floatsidf");
11172 set_conv_libfunc (ufloat_optab, DFmode, SImode,
11173 "__mips16_floatunsidf");
11174 }
11175 }
11176 else
11177 /* Register the gofast functions if selected using --enable-gofast. */
11178 gofast_maybe_init_libfuncs ();
11179
11180 /* The MIPS16 ISA does not have an encoding for "sync", so we rely
11181 on an external non-MIPS16 routine to implement __sync_synchronize. */
11182 if (TARGET_MIPS16)
11183 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
11184 }
11185
11186 /* Build up a multi-insn sequence that loads label TARGET into $AT. */
11187
11188 static void
11189 mips_process_load_label (rtx target)
11190 {
11191 rtx base, gp, intop;
11192 HOST_WIDE_INT offset;
11193
11194 mips_multi_start ();
11195 switch (mips_abi)
11196 {
11197 case ABI_N32:
11198 mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
11199 mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
11200 break;
11201
11202 case ABI_64:
11203 mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
11204 mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
11205 break;
11206
11207 default:
11208 gp = pic_offset_table_rtx;
11209 if (mips_cfun_has_cprestore_slot_p ())
11210 {
11211 gp = gen_rtx_REG (Pmode, AT_REGNUM);
11212 mips_get_cprestore_base_and_offset (&base, &offset, true);
11213 if (!SMALL_OPERAND (offset))
11214 {
11215 intop = GEN_INT (CONST_HIGH_PART (offset));
11216 mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
11217 mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
11218
11219 base = gp;
11220 offset = CONST_LOW_PART (offset);
11221 }
11222 intop = GEN_INT (offset);
11223 if (ISA_HAS_LOAD_DELAY)
11224 mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
11225 else
11226 mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
11227 }
11228 if (ISA_HAS_LOAD_DELAY)
11229 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
11230 else
11231 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
11232 mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
11233 break;
11234 }
11235 }
11236
11237 /* Return the number of instructions needed to load a label into $AT. */
11238
11239 static unsigned int
11240 mips_load_label_length (void)
11241 {
11242 if (cfun->machine->load_label_length == 0)
11243 {
11244 mips_process_load_label (pc_rtx);
11245 cfun->machine->load_label_length = mips_multi_num_insns;
11246 }
11247 return cfun->machine->load_label_length;
11248 }
11249
11250 /* Emit an asm sequence to start a noat block and load the address
11251 of a label into $1. */
11252
11253 void
11254 mips_output_load_label (rtx target)
11255 {
11256 mips_push_asm_switch (&mips_noat);
11257 if (TARGET_EXPLICIT_RELOCS)
11258 {
11259 mips_process_load_label (target);
11260 mips_multi_write ();
11261 }
11262 else
11263 {
11264 if (Pmode == DImode)
11265 output_asm_insn ("dla\t%@,%0", &target);
11266 else
11267 output_asm_insn ("la\t%@,%0", &target);
11268 }
11269 }
11270
11271 /* Return the length of INSN. LENGTH is the initial length computed by
11272 attributes in the machine-description file. */
11273
11274 int
11275 mips_adjust_insn_length (rtx insn, int length)
11276 {
11277 /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
11278 of a PIC long-branch sequence. Substitute the correct value. */
11279 if (length == MAX_PIC_BRANCH_LENGTH
11280 && INSN_CODE (insn) >= 0
11281 && get_attr_type (insn) == TYPE_BRANCH)
11282 {
11283 /* Add the branch-over instruction and its delay slot, if this
11284 is a conditional branch. */
11285 length = simplejump_p (insn) ? 0 : 8;
11286
11287 /* Load the label into $AT and jump to it. Ignore the delay
11288 slot of the jump. */
11289 length += mips_load_label_length () + 4;
11290 }
11291
11292 /* A unconditional jump has an unfilled delay slot if it is not part
11293 of a sequence. A conditional jump normally has a delay slot, but
11294 does not on MIPS16. */
11295 if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
11296 length += 4;
11297
11298 /* See how many nops might be needed to avoid hardware hazards. */
11299 if (!cfun->machine->ignore_hazard_length_p && INSN_CODE (insn) >= 0)
11300 switch (get_attr_hazard (insn))
11301 {
11302 case HAZARD_NONE:
11303 break;
11304
11305 case HAZARD_DELAY:
11306 length += 4;
11307 break;
11308
11309 case HAZARD_HILO:
11310 length += 8;
11311 break;
11312 }
11313
11314 /* In order to make it easier to share MIPS16 and non-MIPS16 patterns,
11315 the .md file length attributes are 4-based for both modes.
11316 Adjust the MIPS16 ones here. */
11317 if (TARGET_MIPS16)
11318 length /= 2;
11319
11320 return length;
11321 }
11322
11323 /* Return the assembly code for INSN, which has the operands given by
11324 OPERANDS, and which branches to OPERANDS[0] if some condition is true.
11325 BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
11326 is in range of a direct branch. BRANCH_IF_FALSE is an inverted
11327 version of BRANCH_IF_TRUE. */
11328
11329 const char *
11330 mips_output_conditional_branch (rtx insn, rtx *operands,
11331 const char *branch_if_true,
11332 const char *branch_if_false)
11333 {
11334 unsigned int length;
11335 rtx taken, not_taken;
11336
11337 gcc_assert (LABEL_P (operands[0]));
11338
11339 length = get_attr_length (insn);
11340 if (length <= 8)
11341 {
11342 /* Just a simple conditional branch. */
11343 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
11344 return branch_if_true;
11345 }
11346
11347 /* Generate a reversed branch around a direct jump. This fallback does
11348 not use branch-likely instructions. */
11349 mips_branch_likely = false;
11350 not_taken = gen_label_rtx ();
11351 taken = operands[0];
11352
11353 /* Generate the reversed branch to NOT_TAKEN. */
11354 operands[0] = not_taken;
11355 output_asm_insn (branch_if_false, operands);
11356
11357 /* If INSN has a delay slot, we must provide delay slots for both the
11358 branch to NOT_TAKEN and the conditional jump. We must also ensure
11359 that INSN's delay slot is executed in the appropriate cases. */
11360 if (final_sequence)
11361 {
11362 /* This first delay slot will always be executed, so use INSN's
11363 delay slot if is not annulled. */
11364 if (!INSN_ANNULLED_BRANCH_P (insn))
11365 {
11366 final_scan_insn (XVECEXP (final_sequence, 0, 1),
11367 asm_out_file, optimize, 1, NULL);
11368 INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
11369 }
11370 else
11371 output_asm_insn ("nop", 0);
11372 fprintf (asm_out_file, "\n");
11373 }
11374
11375 /* Output the unconditional branch to TAKEN. */
11376 if (TARGET_ABSOLUTE_JUMPS)
11377 output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
11378 else
11379 {
11380 mips_output_load_label (taken);
11381 output_asm_insn ("jr\t%@%]%/", 0);
11382 }
11383
11384 /* Now deal with its delay slot; see above. */
11385 if (final_sequence)
11386 {
11387 /* This delay slot will only be executed if the branch is taken.
11388 Use INSN's delay slot if is annulled. */
11389 if (INSN_ANNULLED_BRANCH_P (insn))
11390 {
11391 final_scan_insn (XVECEXP (final_sequence, 0, 1),
11392 asm_out_file, optimize, 1, NULL);
11393 INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
11394 }
11395 else
11396 output_asm_insn ("nop", 0);
11397 fprintf (asm_out_file, "\n");
11398 }
11399
11400 /* Output NOT_TAKEN. */
11401 targetm.asm_out.internal_label (asm_out_file, "L",
11402 CODE_LABEL_NUMBER (not_taken));
11403 return "";
11404 }
11405
11406 /* Return the assembly code for INSN, which branches to OPERANDS[0]
11407 if some ordering condition is true. The condition is given by
11408 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
11409 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
11410 its second is always zero. */
11411
11412 const char *
11413 mips_output_order_conditional_branch (rtx insn, rtx *operands, bool inverted_p)
11414 {
11415 const char *branch[2];
11416
11417 /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
11418 Make BRANCH[0] branch on the inverse condition. */
11419 switch (GET_CODE (operands[1]))
11420 {
11421 /* These cases are equivalent to comparisons against zero. */
11422 case LEU:
11423 inverted_p = !inverted_p;
11424 /* Fall through. */
11425 case GTU:
11426 branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
11427 branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
11428 break;
11429
11430 /* These cases are always true or always false. */
11431 case LTU:
11432 inverted_p = !inverted_p;
11433 /* Fall through. */
11434 case GEU:
11435 branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
11436 branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
11437 break;
11438
11439 default:
11440 branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
11441 branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
11442 break;
11443 }
11444 return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
11445 }
11446 \f
11447 /* Start a block of code that needs access to the LL, SC and SYNC
11448 instructions. */
11449
11450 static void
11451 mips_start_ll_sc_sync_block (void)
11452 {
11453 if (!ISA_HAS_LL_SC)
11454 {
11455 output_asm_insn (".set\tpush", 0);
11456 output_asm_insn (".set\tmips2", 0);
11457 }
11458 }
11459
11460 /* End a block started by mips_start_ll_sc_sync_block. */
11461
11462 static void
11463 mips_end_ll_sc_sync_block (void)
11464 {
11465 if (!ISA_HAS_LL_SC)
11466 output_asm_insn (".set\tpop", 0);
11467 }
11468
11469 /* Output and/or return the asm template for a sync instruction. */
11470
11471 const char *
11472 mips_output_sync (void)
11473 {
11474 mips_start_ll_sc_sync_block ();
11475 output_asm_insn ("sync", 0);
11476 mips_end_ll_sc_sync_block ();
11477 return "";
11478 }
11479
11480 /* Return the asm template associated with sync_insn1 value TYPE.
11481 IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation. */
11482
11483 static const char *
11484 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
11485 {
11486 switch (type)
11487 {
11488 case SYNC_INSN1_MOVE:
11489 return "move\t%0,%z2";
11490 case SYNC_INSN1_LI:
11491 return "li\t%0,%2";
11492 case SYNC_INSN1_ADDU:
11493 return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
11494 case SYNC_INSN1_ADDIU:
11495 return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
11496 case SYNC_INSN1_SUBU:
11497 return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
11498 case SYNC_INSN1_AND:
11499 return "and\t%0,%1,%z2";
11500 case SYNC_INSN1_ANDI:
11501 return "andi\t%0,%1,%2";
11502 case SYNC_INSN1_OR:
11503 return "or\t%0,%1,%z2";
11504 case SYNC_INSN1_ORI:
11505 return "ori\t%0,%1,%2";
11506 case SYNC_INSN1_XOR:
11507 return "xor\t%0,%1,%z2";
11508 case SYNC_INSN1_XORI:
11509 return "xori\t%0,%1,%2";
11510 }
11511 gcc_unreachable ();
11512 }
11513
11514 /* Return the asm template associated with sync_insn2 value TYPE. */
11515
11516 static const char *
11517 mips_sync_insn2_template (enum attr_sync_insn2 type)
11518 {
11519 switch (type)
11520 {
11521 case SYNC_INSN2_NOP:
11522 gcc_unreachable ();
11523 case SYNC_INSN2_AND:
11524 return "and\t%0,%1,%z2";
11525 case SYNC_INSN2_XOR:
11526 return "xor\t%0,%1,%z2";
11527 case SYNC_INSN2_NOT:
11528 return "nor\t%0,%1,%.";
11529 }
11530 gcc_unreachable ();
11531 }
11532
11533 /* OPERANDS are the operands to a sync loop instruction and INDEX is
11534 the value of the one of the sync_* attributes. Return the operand
11535 referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
11536 have the associated attribute. */
11537
11538 static rtx
11539 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
11540 {
11541 if (index > 0)
11542 default_value = operands[index - 1];
11543 return default_value;
11544 }
11545
11546 /* INSN is a sync loop with operands OPERANDS. Build up a multi-insn
11547 sequence for it. */
11548
11549 static void
11550 mips_process_sync_loop (rtx insn, rtx *operands)
11551 {
11552 rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
11553 rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3;
11554 unsigned int tmp3_insn;
11555 enum attr_sync_insn1 insn1;
11556 enum attr_sync_insn2 insn2;
11557 bool is_64bit_p;
11558
11559 /* Read an operand from the sync_WHAT attribute and store it in
11560 variable WHAT. DEFAULT is the default value if no attribute
11561 is specified. */
11562 #define READ_OPERAND(WHAT, DEFAULT) \
11563 WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
11564 DEFAULT)
11565
11566 /* Read the memory. */
11567 READ_OPERAND (mem, 0);
11568 gcc_assert (mem);
11569 is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
11570
11571 /* Read the other attributes. */
11572 at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
11573 READ_OPERAND (oldval, at);
11574 READ_OPERAND (newval, at);
11575 READ_OPERAND (inclusive_mask, 0);
11576 READ_OPERAND (exclusive_mask, 0);
11577 READ_OPERAND (required_oldval, 0);
11578 READ_OPERAND (insn1_op2, 0);
11579 insn1 = get_attr_sync_insn1 (insn);
11580 insn2 = get_attr_sync_insn2 (insn);
11581
11582 mips_multi_start ();
11583
11584 /* Output the release side of the memory barrier. */
11585 if (get_attr_sync_release_barrier (insn) == SYNC_RELEASE_BARRIER_YES)
11586 {
11587 if (required_oldval == 0 && TARGET_OCTEON)
11588 {
11589 /* Octeon doesn't reorder reads, so a full barrier can be
11590 created by using SYNCW to order writes combined with the
11591 write from the following SC. When the SC successfully
11592 completes, we know that all preceding writes are also
11593 committed to the coherent memory system. It is possible
11594 for a single SYNCW to fail, but a pair of them will never
11595 fail, so we use two. */
11596 mips_multi_add_insn ("syncw", NULL);
11597 mips_multi_add_insn ("syncw", NULL);
11598 }
11599 else
11600 mips_multi_add_insn ("sync", NULL);
11601 }
11602
11603 /* Output the branch-back label. */
11604 mips_multi_add_label ("1:");
11605
11606 /* OLDVAL = *MEM. */
11607 mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
11608 oldval, mem, NULL);
11609
11610 /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2. */
11611 if (required_oldval)
11612 {
11613 if (inclusive_mask == 0)
11614 tmp1 = oldval;
11615 else
11616 {
11617 gcc_assert (oldval != at);
11618 mips_multi_add_insn ("and\t%0,%1,%2",
11619 at, oldval, inclusive_mask, NULL);
11620 tmp1 = at;
11621 }
11622 mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
11623 }
11624
11625 /* $TMP1 = OLDVAL & EXCLUSIVE_MASK. */
11626 if (exclusive_mask == 0)
11627 tmp1 = const0_rtx;
11628 else
11629 {
11630 gcc_assert (oldval != at);
11631 mips_multi_add_insn ("and\t%0,%1,%z2",
11632 at, oldval, exclusive_mask, NULL);
11633 tmp1 = at;
11634 }
11635
11636 /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
11637
11638 We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
11639 at least one instruction in that case. */
11640 if (insn1 == SYNC_INSN1_MOVE
11641 && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
11642 tmp2 = insn1_op2;
11643 else
11644 {
11645 mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
11646 newval, oldval, insn1_op2, NULL);
11647 tmp2 = newval;
11648 }
11649
11650 /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK). */
11651 if (insn2 == SYNC_INSN2_NOP)
11652 tmp3 = tmp2;
11653 else
11654 {
11655 mips_multi_add_insn (mips_sync_insn2_template (insn2),
11656 newval, tmp2, inclusive_mask, NULL);
11657 tmp3 = newval;
11658 }
11659 tmp3_insn = mips_multi_last_index ();
11660
11661 /* $AT = $TMP1 | $TMP3. */
11662 if (tmp1 == const0_rtx || tmp3 == const0_rtx)
11663 {
11664 mips_multi_set_operand (tmp3_insn, 0, at);
11665 tmp3 = at;
11666 }
11667 else
11668 {
11669 gcc_assert (tmp1 != tmp3);
11670 mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
11671 }
11672
11673 /* if (!commit (*MEM = $AT)) goto 1.
11674
11675 This will sometimes be a delayed branch; see the write code below
11676 for details. */
11677 mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
11678 mips_multi_add_insn ("beq%?\t%0,%.,1b", at, NULL);
11679
11680 /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot]. */
11681 if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
11682 {
11683 mips_multi_copy_insn (tmp3_insn);
11684 mips_multi_set_operand (mips_multi_last_index (), 0, newval);
11685 }
11686 else
11687 mips_multi_add_insn ("nop", NULL);
11688
11689 /* Output the acquire side of the memory barrier. */
11690 if (TARGET_SYNC_AFTER_SC)
11691 mips_multi_add_insn ("sync", NULL);
11692
11693 /* Output the exit label, if needed. */
11694 if (required_oldval)
11695 mips_multi_add_label ("2:");
11696
11697 #undef READ_OPERAND
11698 }
11699
11700 /* Output and/or return the asm template for sync loop INSN, which has
11701 the operands given by OPERANDS. */
11702
11703 const char *
11704 mips_output_sync_loop (rtx insn, rtx *operands)
11705 {
11706 mips_process_sync_loop (insn, operands);
11707
11708 /* Use branch-likely instructions to work around the LL/SC R10000
11709 errata. */
11710 mips_branch_likely = TARGET_FIX_R10000;
11711
11712 mips_push_asm_switch (&mips_noreorder);
11713 mips_push_asm_switch (&mips_nomacro);
11714 mips_push_asm_switch (&mips_noat);
11715 mips_start_ll_sc_sync_block ();
11716
11717 mips_multi_write ();
11718
11719 mips_end_ll_sc_sync_block ();
11720 mips_pop_asm_switch (&mips_noat);
11721 mips_pop_asm_switch (&mips_nomacro);
11722 mips_pop_asm_switch (&mips_noreorder);
11723
11724 return "";
11725 }
11726
11727 /* Return the number of individual instructions in sync loop INSN,
11728 which has the operands given by OPERANDS. */
11729
11730 unsigned int
11731 mips_sync_loop_insns (rtx insn, rtx *operands)
11732 {
11733 mips_process_sync_loop (insn, operands);
11734 return mips_multi_num_insns;
11735 }
11736 \f
11737 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
11738 the operands given by OPERANDS. Add in a divide-by-zero check if needed.
11739
11740 When working around R4000 and R4400 errata, we need to make sure that
11741 the division is not immediately followed by a shift[1][2]. We also
11742 need to stop the division from being put into a branch delay slot[3].
11743 The easiest way to avoid both problems is to add a nop after the
11744 division. When a divide-by-zero check is needed, this nop can be
11745 used to fill the branch delay slot.
11746
11747 [1] If a double-word or a variable shift executes immediately
11748 after starting an integer division, the shift may give an
11749 incorrect result. See quotations of errata #16 and #28 from
11750 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
11751 in mips.md for details.
11752
11753 [2] A similar bug to [1] exists for all revisions of the
11754 R4000 and the R4400 when run in an MC configuration.
11755 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
11756
11757 "19. In this following sequence:
11758
11759 ddiv (or ddivu or div or divu)
11760 dsll32 (or dsrl32, dsra32)
11761
11762 if an MPT stall occurs, while the divide is slipping the cpu
11763 pipeline, then the following double shift would end up with an
11764 incorrect result.
11765
11766 Workaround: The compiler needs to avoid generating any
11767 sequence with divide followed by extended double shift."
11768
11769 This erratum is also present in "MIPS R4400MC Errata, Processor
11770 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
11771 & 3.0" as errata #10 and #4, respectively.
11772
11773 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
11774 (also valid for MIPS R4000MC processors):
11775
11776 "52. R4000SC: This bug does not apply for the R4000PC.
11777
11778 There are two flavors of this bug:
11779
11780 1) If the instruction just after divide takes an RF exception
11781 (tlb-refill, tlb-invalid) and gets an instruction cache
11782 miss (both primary and secondary) and the line which is
11783 currently in secondary cache at this index had the first
11784 data word, where the bits 5..2 are set, then R4000 would
11785 get a wrong result for the div.
11786
11787 ##1
11788 nop
11789 div r8, r9
11790 ------------------- # end-of page. -tlb-refill
11791 nop
11792 ##2
11793 nop
11794 div r8, r9
11795 ------------------- # end-of page. -tlb-invalid
11796 nop
11797
11798 2) If the divide is in the taken branch delay slot, where the
11799 target takes RF exception and gets an I-cache miss for the
11800 exception vector or where I-cache miss occurs for the
11801 target address, under the above mentioned scenarios, the
11802 div would get wrong results.
11803
11804 ##1
11805 j r2 # to next page mapped or unmapped
11806 div r8,r9 # this bug would be there as long
11807 # as there is an ICache miss and
11808 nop # the "data pattern" is present
11809
11810 ##2
11811 beq r0, r0, NextPage # to Next page
11812 div r8,r9
11813 nop
11814
11815 This bug is present for div, divu, ddiv, and ddivu
11816 instructions.
11817
11818 Workaround: For item 1), OS could make sure that the next page
11819 after the divide instruction is also mapped. For item 2), the
11820 compiler could make sure that the divide instruction is not in
11821 the branch delay slot."
11822
11823 These processors have PRId values of 0x00004220 and 0x00004300 for
11824 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
11825
11826 const char *
11827 mips_output_division (const char *division, rtx *operands)
11828 {
11829 const char *s;
11830
11831 s = division;
11832 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
11833 {
11834 output_asm_insn (s, operands);
11835 s = "nop";
11836 }
11837 if (TARGET_CHECK_ZERO_DIV)
11838 {
11839 if (TARGET_MIPS16)
11840 {
11841 output_asm_insn (s, operands);
11842 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
11843 }
11844 else if (GENERATE_DIVIDE_TRAPS)
11845 {
11846 /* Avoid long replay penalty on load miss by putting the trap before
11847 the divide. */
11848 if (TUNE_74K)
11849 output_asm_insn ("teq\t%2,%.,7", operands);
11850 else
11851 {
11852 output_asm_insn (s, operands);
11853 s = "teq\t%2,%.,7";
11854 }
11855 }
11856 else
11857 {
11858 output_asm_insn ("%(bne\t%2,%.,1f", operands);
11859 output_asm_insn (s, operands);
11860 s = "break\t7%)\n1:";
11861 }
11862 }
11863 return s;
11864 }
11865 \f
11866 /* Return true if IN_INSN is a multiply-add or multiply-subtract
11867 instruction and if OUT_INSN assigns to the accumulator operand. */
11868
11869 bool
11870 mips_linked_madd_p (rtx out_insn, rtx in_insn)
11871 {
11872 rtx x;
11873
11874 x = single_set (in_insn);
11875 if (x == 0)
11876 return false;
11877
11878 x = SET_SRC (x);
11879
11880 if (GET_CODE (x) == PLUS
11881 && GET_CODE (XEXP (x, 0)) == MULT
11882 && reg_set_p (XEXP (x, 1), out_insn))
11883 return true;
11884
11885 if (GET_CODE (x) == MINUS
11886 && GET_CODE (XEXP (x, 1)) == MULT
11887 && reg_set_p (XEXP (x, 0), out_insn))
11888 return true;
11889
11890 return false;
11891 }
11892
11893 /* True if the dependency between OUT_INSN and IN_INSN is on the store
11894 data rather than the address. We need this because the cprestore
11895 pattern is type "store", but is defined using an UNSPEC_VOLATILE,
11896 which causes the default routine to abort. We just return false
11897 for that case. */
11898
11899 bool
11900 mips_store_data_bypass_p (rtx out_insn, rtx in_insn)
11901 {
11902 if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
11903 return false;
11904
11905 return !store_data_bypass_p (out_insn, in_insn);
11906 }
11907 \f
11908
11909 /* Variables and flags used in scheduler hooks when tuning for
11910 Loongson 2E/2F. */
11911 static struct
11912 {
11913 /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
11914 strategy. */
11915
11916 /* If true, then next ALU1/2 instruction will go to ALU1. */
11917 bool alu1_turn_p;
11918
11919 /* If true, then next FALU1/2 unstruction will go to FALU1. */
11920 bool falu1_turn_p;
11921
11922 /* Codes to query if [f]alu{1,2}_core units are subscribed or not. */
11923 int alu1_core_unit_code;
11924 int alu2_core_unit_code;
11925 int falu1_core_unit_code;
11926 int falu2_core_unit_code;
11927
11928 /* True if current cycle has a multi instruction.
11929 This flag is used in mips_ls2_dfa_post_advance_cycle. */
11930 bool cycle_has_multi_p;
11931
11932 /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
11933 These are used in mips_ls2_dfa_post_advance_cycle to initialize
11934 DFA state.
11935 E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
11936 instruction to go ALU1. */
11937 rtx alu1_turn_enabled_insn;
11938 rtx alu2_turn_enabled_insn;
11939 rtx falu1_turn_enabled_insn;
11940 rtx falu2_turn_enabled_insn;
11941 } mips_ls2;
11942
11943 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
11944 dependencies have no cost, except on the 20Kc where output-dependence
11945 is treated like input-dependence. */
11946
11947 static int
11948 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
11949 rtx dep ATTRIBUTE_UNUSED, int cost)
11950 {
11951 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
11952 && TUNE_20KC)
11953 return cost;
11954 if (REG_NOTE_KIND (link) != 0)
11955 return 0;
11956 return cost;
11957 }
11958
11959 /* Return the number of instructions that can be issued per cycle. */
11960
11961 static int
11962 mips_issue_rate (void)
11963 {
11964 switch (mips_tune)
11965 {
11966 case PROCESSOR_74KC:
11967 case PROCESSOR_74KF2_1:
11968 case PROCESSOR_74KF1_1:
11969 case PROCESSOR_74KF3_2:
11970 /* The 74k is not strictly quad-issue cpu, but can be seen as one
11971 by the scheduler. It can issue 1 ALU, 1 AGEN and 2 FPU insns,
11972 but in reality only a maximum of 3 insns can be issued as
11973 floating-point loads and stores also require a slot in the
11974 AGEN pipe. */
11975 case PROCESSOR_R10000:
11976 /* All R10K Processors are quad-issue (being the first MIPS
11977 processors to support this feature). */
11978 return 4;
11979
11980 case PROCESSOR_20KC:
11981 case PROCESSOR_R4130:
11982 case PROCESSOR_R5400:
11983 case PROCESSOR_R5500:
11984 case PROCESSOR_R7000:
11985 case PROCESSOR_R9000:
11986 case PROCESSOR_OCTEON:
11987 return 2;
11988
11989 case PROCESSOR_SB1:
11990 case PROCESSOR_SB1A:
11991 /* This is actually 4, but we get better performance if we claim 3.
11992 This is partly because of unwanted speculative code motion with the
11993 larger number, and partly because in most common cases we can't
11994 reach the theoretical max of 4. */
11995 return 3;
11996
11997 case PROCESSOR_LOONGSON_2E:
11998 case PROCESSOR_LOONGSON_2F:
11999 return 4;
12000
12001 default:
12002 return 1;
12003 }
12004 }
12005
12006 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2. */
12007
12008 static void
12009 mips_ls2_init_dfa_post_cycle_insn (void)
12010 {
12011 start_sequence ();
12012 emit_insn (gen_ls2_alu1_turn_enabled_insn ());
12013 mips_ls2.alu1_turn_enabled_insn = get_insns ();
12014 end_sequence ();
12015
12016 start_sequence ();
12017 emit_insn (gen_ls2_alu2_turn_enabled_insn ());
12018 mips_ls2.alu2_turn_enabled_insn = get_insns ();
12019 end_sequence ();
12020
12021 start_sequence ();
12022 emit_insn (gen_ls2_falu1_turn_enabled_insn ());
12023 mips_ls2.falu1_turn_enabled_insn = get_insns ();
12024 end_sequence ();
12025
12026 start_sequence ();
12027 emit_insn (gen_ls2_falu2_turn_enabled_insn ());
12028 mips_ls2.falu2_turn_enabled_insn = get_insns ();
12029 end_sequence ();
12030
12031 mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
12032 mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
12033 mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
12034 mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
12035 }
12036
12037 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
12038 Init data used in mips_dfa_post_advance_cycle. */
12039
12040 static void
12041 mips_init_dfa_post_cycle_insn (void)
12042 {
12043 if (TUNE_LOONGSON_2EF)
12044 mips_ls2_init_dfa_post_cycle_insn ();
12045 }
12046
12047 /* Initialize STATE when scheduling for Loongson 2E/2F.
12048 Support round-robin dispatch scheme by enabling only one of
12049 ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
12050 respectively. */
12051
12052 static void
12053 mips_ls2_dfa_post_advance_cycle (state_t state)
12054 {
12055 if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
12056 {
12057 /* Though there are no non-pipelined ALU1 insns,
12058 we can get an instruction of type 'multi' before reload. */
12059 gcc_assert (mips_ls2.cycle_has_multi_p);
12060 mips_ls2.alu1_turn_p = false;
12061 }
12062
12063 mips_ls2.cycle_has_multi_p = false;
12064
12065 if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
12066 /* We have a non-pipelined alu instruction in the core,
12067 adjust round-robin counter. */
12068 mips_ls2.alu1_turn_p = true;
12069
12070 if (mips_ls2.alu1_turn_p)
12071 {
12072 if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
12073 gcc_unreachable ();
12074 }
12075 else
12076 {
12077 if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
12078 gcc_unreachable ();
12079 }
12080
12081 if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
12082 {
12083 /* There are no non-pipelined FALU1 insns. */
12084 gcc_unreachable ();
12085 mips_ls2.falu1_turn_p = false;
12086 }
12087
12088 if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
12089 /* We have a non-pipelined falu instruction in the core,
12090 adjust round-robin counter. */
12091 mips_ls2.falu1_turn_p = true;
12092
12093 if (mips_ls2.falu1_turn_p)
12094 {
12095 if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
12096 gcc_unreachable ();
12097 }
12098 else
12099 {
12100 if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
12101 gcc_unreachable ();
12102 }
12103 }
12104
12105 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
12106 This hook is being called at the start of each cycle. */
12107
12108 static void
12109 mips_dfa_post_advance_cycle (void)
12110 {
12111 if (TUNE_LOONGSON_2EF)
12112 mips_ls2_dfa_post_advance_cycle (curr_state);
12113 }
12114
12115 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
12116 be as wide as the scheduling freedom in the DFA. */
12117
12118 static int
12119 mips_multipass_dfa_lookahead (void)
12120 {
12121 /* Can schedule up to 4 of the 6 function units in any one cycle. */
12122 if (TUNE_SB1)
12123 return 4;
12124
12125 if (TUNE_LOONGSON_2EF)
12126 return 4;
12127
12128 if (TUNE_OCTEON)
12129 return 2;
12130
12131 return 0;
12132 }
12133 \f
12134 /* Remove the instruction at index LOWER from ready queue READY and
12135 reinsert it in front of the instruction at index HIGHER. LOWER must
12136 be <= HIGHER. */
12137
12138 static void
12139 mips_promote_ready (rtx *ready, int lower, int higher)
12140 {
12141 rtx new_head;
12142 int i;
12143
12144 new_head = ready[lower];
12145 for (i = lower; i < higher; i++)
12146 ready[i] = ready[i + 1];
12147 ready[i] = new_head;
12148 }
12149
12150 /* If the priority of the instruction at POS2 in the ready queue READY
12151 is within LIMIT units of that of the instruction at POS1, swap the
12152 instructions if POS2 is not already less than POS1. */
12153
12154 static void
12155 mips_maybe_swap_ready (rtx *ready, int pos1, int pos2, int limit)
12156 {
12157 if (pos1 < pos2
12158 && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
12159 {
12160 rtx temp;
12161
12162 temp = ready[pos1];
12163 ready[pos1] = ready[pos2];
12164 ready[pos2] = temp;
12165 }
12166 }
12167 \f
12168 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
12169 that may clobber hi or lo. */
12170 static rtx mips_macc_chains_last_hilo;
12171
12172 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
12173 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
12174
12175 static void
12176 mips_macc_chains_record (rtx insn)
12177 {
12178 if (get_attr_may_clobber_hilo (insn))
12179 mips_macc_chains_last_hilo = insn;
12180 }
12181
12182 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
12183 has NREADY elements, looking for a multiply-add or multiply-subtract
12184 instruction that is cumulative with mips_macc_chains_last_hilo.
12185 If there is one, promote it ahead of anything else that might
12186 clobber hi or lo. */
12187
12188 static void
12189 mips_macc_chains_reorder (rtx *ready, int nready)
12190 {
12191 int i, j;
12192
12193 if (mips_macc_chains_last_hilo != 0)
12194 for (i = nready - 1; i >= 0; i--)
12195 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
12196 {
12197 for (j = nready - 1; j > i; j--)
12198 if (recog_memoized (ready[j]) >= 0
12199 && get_attr_may_clobber_hilo (ready[j]))
12200 {
12201 mips_promote_ready (ready, i, j);
12202 break;
12203 }
12204 break;
12205 }
12206 }
12207 \f
12208 /* The last instruction to be scheduled. */
12209 static rtx vr4130_last_insn;
12210
12211 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
12212 points to an rtx that is initially an instruction. Nullify the rtx
12213 if the instruction uses the value of register X. */
12214
12215 static void
12216 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
12217 void *data)
12218 {
12219 rtx *insn_ptr;
12220
12221 insn_ptr = (rtx *) data;
12222 if (REG_P (x)
12223 && *insn_ptr != 0
12224 && reg_referenced_p (x, PATTERN (*insn_ptr)))
12225 *insn_ptr = 0;
12226 }
12227
12228 /* Return true if there is true register dependence between vr4130_last_insn
12229 and INSN. */
12230
12231 static bool
12232 vr4130_true_reg_dependence_p (rtx insn)
12233 {
12234 note_stores (PATTERN (vr4130_last_insn),
12235 vr4130_true_reg_dependence_p_1, &insn);
12236 return insn == 0;
12237 }
12238
12239 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
12240 the ready queue and that INSN2 is the instruction after it, return
12241 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
12242 in which INSN1 and INSN2 can probably issue in parallel, but for
12243 which (INSN2, INSN1) should be less sensitive to instruction
12244 alignment than (INSN1, INSN2). See 4130.md for more details. */
12245
12246 static bool
12247 vr4130_swap_insns_p (rtx insn1, rtx insn2)
12248 {
12249 sd_iterator_def sd_it;
12250 dep_t dep;
12251
12252 /* Check for the following case:
12253
12254 1) there is some other instruction X with an anti dependence on INSN1;
12255 2) X has a higher priority than INSN2; and
12256 3) X is an arithmetic instruction (and thus has no unit restrictions).
12257
12258 If INSN1 is the last instruction blocking X, it would better to
12259 choose (INSN1, X) over (INSN2, INSN1). */
12260 FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
12261 if (DEP_TYPE (dep) == REG_DEP_ANTI
12262 && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
12263 && recog_memoized (DEP_CON (dep)) >= 0
12264 && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
12265 return false;
12266
12267 if (vr4130_last_insn != 0
12268 && recog_memoized (insn1) >= 0
12269 && recog_memoized (insn2) >= 0)
12270 {
12271 /* See whether INSN1 and INSN2 use different execution units,
12272 or if they are both ALU-type instructions. If so, they can
12273 probably execute in parallel. */
12274 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
12275 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
12276 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
12277 {
12278 /* If only one of the instructions has a dependence on
12279 vr4130_last_insn, prefer to schedule the other one first. */
12280 bool dep1_p = vr4130_true_reg_dependence_p (insn1);
12281 bool dep2_p = vr4130_true_reg_dependence_p (insn2);
12282 if (dep1_p != dep2_p)
12283 return dep1_p;
12284
12285 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
12286 is not an ALU-type instruction and if INSN1 uses the same
12287 execution unit. (Note that if this condition holds, we already
12288 know that INSN2 uses a different execution unit.) */
12289 if (class1 != VR4130_CLASS_ALU
12290 && recog_memoized (vr4130_last_insn) >= 0
12291 && class1 == get_attr_vr4130_class (vr4130_last_insn))
12292 return true;
12293 }
12294 }
12295 return false;
12296 }
12297
12298 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
12299 queue with at least two instructions. Swap the first two if
12300 vr4130_swap_insns_p says that it could be worthwhile. */
12301
12302 static void
12303 vr4130_reorder (rtx *ready, int nready)
12304 {
12305 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
12306 mips_promote_ready (ready, nready - 2, nready - 1);
12307 }
12308 \f
12309 /* Record whether last 74k AGEN instruction was a load or store. */
12310 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
12311
12312 /* Initialize mips_last_74k_agen_insn from INSN. A null argument
12313 resets to TYPE_UNKNOWN state. */
12314
12315 static void
12316 mips_74k_agen_init (rtx insn)
12317 {
12318 if (!insn || CALL_P (insn) || JUMP_P (insn))
12319 mips_last_74k_agen_insn = TYPE_UNKNOWN;
12320 else
12321 {
12322 enum attr_type type = get_attr_type (insn);
12323 if (type == TYPE_LOAD || type == TYPE_STORE)
12324 mips_last_74k_agen_insn = type;
12325 }
12326 }
12327
12328 /* A TUNE_74K helper function. The 74K AGEN pipeline likes multiple
12329 loads to be grouped together, and multiple stores to be grouped
12330 together. Swap things around in the ready queue to make this happen. */
12331
12332 static void
12333 mips_74k_agen_reorder (rtx *ready, int nready)
12334 {
12335 int i;
12336 int store_pos, load_pos;
12337
12338 store_pos = -1;
12339 load_pos = -1;
12340
12341 for (i = nready - 1; i >= 0; i--)
12342 {
12343 rtx insn = ready[i];
12344 if (USEFUL_INSN_P (insn))
12345 switch (get_attr_type (insn))
12346 {
12347 case TYPE_STORE:
12348 if (store_pos == -1)
12349 store_pos = i;
12350 break;
12351
12352 case TYPE_LOAD:
12353 if (load_pos == -1)
12354 load_pos = i;
12355 break;
12356
12357 default:
12358 break;
12359 }
12360 }
12361
12362 if (load_pos == -1 || store_pos == -1)
12363 return;
12364
12365 switch (mips_last_74k_agen_insn)
12366 {
12367 case TYPE_UNKNOWN:
12368 /* Prefer to schedule loads since they have a higher latency. */
12369 case TYPE_LOAD:
12370 /* Swap loads to the front of the queue. */
12371 mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
12372 break;
12373 case TYPE_STORE:
12374 /* Swap stores to the front of the queue. */
12375 mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
12376 break;
12377 default:
12378 break;
12379 }
12380 }
12381 \f
12382 /* Implement TARGET_SCHED_INIT. */
12383
12384 static void
12385 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
12386 int max_ready ATTRIBUTE_UNUSED)
12387 {
12388 mips_macc_chains_last_hilo = 0;
12389 vr4130_last_insn = 0;
12390 mips_74k_agen_init (NULL_RTX);
12391
12392 /* When scheduling for Loongson2, branch instructions go to ALU1,
12393 therefore basic block is most likely to start with round-robin counter
12394 pointed to ALU2. */
12395 mips_ls2.alu1_turn_p = false;
12396 mips_ls2.falu1_turn_p = true;
12397 }
12398
12399 /* Implement TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2. */
12400
12401 static int
12402 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
12403 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
12404 {
12405 if (!reload_completed
12406 && TUNE_MACC_CHAINS
12407 && *nreadyp > 0)
12408 mips_macc_chains_reorder (ready, *nreadyp);
12409
12410 if (reload_completed
12411 && TUNE_MIPS4130
12412 && !TARGET_VR4130_ALIGN
12413 && *nreadyp > 1)
12414 vr4130_reorder (ready, *nreadyp);
12415
12416 if (TUNE_74K)
12417 mips_74k_agen_reorder (ready, *nreadyp);
12418
12419 return mips_issue_rate ();
12420 }
12421
12422 /* Update round-robin counters for ALU1/2 and FALU1/2. */
12423
12424 static void
12425 mips_ls2_variable_issue (rtx insn)
12426 {
12427 if (mips_ls2.alu1_turn_p)
12428 {
12429 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
12430 mips_ls2.alu1_turn_p = false;
12431 }
12432 else
12433 {
12434 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
12435 mips_ls2.alu1_turn_p = true;
12436 }
12437
12438 if (mips_ls2.falu1_turn_p)
12439 {
12440 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
12441 mips_ls2.falu1_turn_p = false;
12442 }
12443 else
12444 {
12445 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
12446 mips_ls2.falu1_turn_p = true;
12447 }
12448
12449 if (recog_memoized (insn) >= 0)
12450 mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
12451 }
12452
12453 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
12454
12455 static int
12456 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
12457 rtx insn, int more)
12458 {
12459 /* Ignore USEs and CLOBBERs; don't count them against the issue rate. */
12460 if (USEFUL_INSN_P (insn))
12461 {
12462 if (get_attr_type (insn) != TYPE_GHOST)
12463 more--;
12464 if (!reload_completed && TUNE_MACC_CHAINS)
12465 mips_macc_chains_record (insn);
12466 vr4130_last_insn = insn;
12467 if (TUNE_74K)
12468 mips_74k_agen_init (insn);
12469 else if (TUNE_LOONGSON_2EF)
12470 mips_ls2_variable_issue (insn);
12471 }
12472
12473 /* Instructions of type 'multi' should all be split before
12474 the second scheduling pass. */
12475 gcc_assert (!reload_completed
12476 || recog_memoized (insn) < 0
12477 || get_attr_type (insn) != TYPE_MULTI);
12478
12479 return more;
12480 }
12481 \f
12482 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
12483 return the first operand of the associated PREF or PREFX insn. */
12484
12485 rtx
12486 mips_prefetch_cookie (rtx write, rtx locality)
12487 {
12488 /* store_streamed / load_streamed. */
12489 if (INTVAL (locality) <= 0)
12490 return GEN_INT (INTVAL (write) + 4);
12491
12492 /* store / load. */
12493 if (INTVAL (locality) <= 2)
12494 return write;
12495
12496 /* store_retained / load_retained. */
12497 return GEN_INT (INTVAL (write) + 6);
12498 }
12499 \f
12500 /* Flags that indicate when a built-in function is available.
12501
12502 BUILTIN_AVAIL_NON_MIPS16
12503 The function is available on the current target, but only
12504 in non-MIPS16 mode. */
12505 #define BUILTIN_AVAIL_NON_MIPS16 1
12506
12507 /* Declare an availability predicate for built-in functions that
12508 require non-MIPS16 mode and also require COND to be true.
12509 NAME is the main part of the predicate's name. */
12510 #define AVAIL_NON_MIPS16(NAME, COND) \
12511 static unsigned int \
12512 mips_builtin_avail_##NAME (void) \
12513 { \
12514 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0; \
12515 }
12516
12517 /* This structure describes a single built-in function. */
12518 struct mips_builtin_description {
12519 /* The code of the main .md file instruction. See mips_builtin_type
12520 for more information. */
12521 enum insn_code icode;
12522
12523 /* The floating-point comparison code to use with ICODE, if any. */
12524 enum mips_fp_condition cond;
12525
12526 /* The name of the built-in function. */
12527 const char *name;
12528
12529 /* Specifies how the function should be expanded. */
12530 enum mips_builtin_type builtin_type;
12531
12532 /* The function's prototype. */
12533 enum mips_function_type function_type;
12534
12535 /* Whether the function is available. */
12536 unsigned int (*avail) (void);
12537 };
12538
12539 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
12540 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
12541 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
12542 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
12543 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
12544 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
12545 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
12546 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
12547 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
12548
12549 /* Construct a mips_builtin_description from the given arguments.
12550
12551 INSN is the name of the associated instruction pattern, without the
12552 leading CODE_FOR_mips_.
12553
12554 CODE is the floating-point condition code associated with the
12555 function. It can be 'f' if the field is not applicable.
12556
12557 NAME is the name of the function itself, without the leading
12558 "__builtin_mips_".
12559
12560 BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
12561
12562 AVAIL is the name of the availability predicate, without the leading
12563 mips_builtin_avail_. */
12564 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE, \
12565 FUNCTION_TYPE, AVAIL) \
12566 { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND, \
12567 "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE, \
12568 mips_builtin_avail_ ## AVAIL }
12569
12570 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
12571 mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE and AVAIL
12572 are as for MIPS_BUILTIN. */
12573 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
12574 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
12575
12576 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
12577 are subject to mips_builtin_avail_<AVAIL>. */
12578 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL) \
12579 MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s", \
12580 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL), \
12581 MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d", \
12582 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
12583
12584 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
12585 The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
12586 while the any and all forms are subject to mips_builtin_avail_mips3d. */
12587 #define CMP_PS_BUILTINS(INSN, COND, AVAIL) \
12588 MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps", \
12589 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF, \
12590 mips3d), \
12591 MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps", \
12592 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF, \
12593 mips3d), \
12594 MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
12595 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF, \
12596 AVAIL), \
12597 MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
12598 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF, \
12599 AVAIL)
12600
12601 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s. The functions
12602 are subject to mips_builtin_avail_mips3d. */
12603 #define CMP_4S_BUILTINS(INSN, COND) \
12604 MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s", \
12605 MIPS_BUILTIN_CMP_ANY, \
12606 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d), \
12607 MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s", \
12608 MIPS_BUILTIN_CMP_ALL, \
12609 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
12610
12611 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps. The comparison
12612 instruction requires mips_builtin_avail_<AVAIL>. */
12613 #define MOVTF_BUILTINS(INSN, COND, AVAIL) \
12614 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps", \
12615 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
12616 AVAIL), \
12617 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps", \
12618 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
12619 AVAIL)
12620
12621 /* Define all the built-in functions related to C.cond.fmt condition COND. */
12622 #define CMP_BUILTINS(COND) \
12623 MOVTF_BUILTINS (c, COND, paired_single), \
12624 MOVTF_BUILTINS (cabs, COND, mips3d), \
12625 CMP_SCALAR_BUILTINS (cabs, COND, mips3d), \
12626 CMP_PS_BUILTINS (c, COND, paired_single), \
12627 CMP_PS_BUILTINS (cabs, COND, mips3d), \
12628 CMP_4S_BUILTINS (c, COND), \
12629 CMP_4S_BUILTINS (cabs, COND)
12630
12631 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
12632 function mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE
12633 and AVAIL are as for MIPS_BUILTIN. */
12634 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
12635 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET, \
12636 FUNCTION_TYPE, AVAIL)
12637
12638 /* Define __builtin_mips_bposge<VALUE>. <VALUE> is 32 for the MIPS32 DSP
12639 branch instruction. AVAIL is as for MIPS_BUILTIN. */
12640 #define BPOSGE_BUILTIN(VALUE, AVAIL) \
12641 MIPS_BUILTIN (bposge, f, "bposge" #VALUE, \
12642 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
12643
12644 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
12645 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
12646 builtin_description field. */
12647 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE) \
12648 { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f, \
12649 "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT, \
12650 FUNCTION_TYPE, mips_builtin_avail_loongson }
12651
12652 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
12653 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
12654 builtin_description field. */
12655 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE) \
12656 LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
12657
12658 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
12659 We use functions of this form when the same insn can be usefully applied
12660 to more than one datatype. */
12661 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE) \
12662 LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
12663
12664 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
12665 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
12666 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
12667 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
12668 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
12669 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
12670
12671 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
12672 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
12673 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
12674 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
12675 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
12676 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
12677 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
12678 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
12679 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
12680 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
12681 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
12682 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
12683 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
12684 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
12685 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
12686 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
12687 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
12688 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
12689 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
12690 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
12691 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
12692 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
12693 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
12694 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
12695 #define CODE_FOR_loongson_punpckhbh CODE_FOR_vec_interleave_highv8qi
12696 #define CODE_FOR_loongson_punpckhhw CODE_FOR_vec_interleave_highv4hi
12697 #define CODE_FOR_loongson_punpckhwd CODE_FOR_vec_interleave_highv2si
12698 #define CODE_FOR_loongson_punpcklbh CODE_FOR_vec_interleave_lowv8qi
12699 #define CODE_FOR_loongson_punpcklhw CODE_FOR_vec_interleave_lowv4hi
12700 #define CODE_FOR_loongson_punpcklwd CODE_FOR_vec_interleave_lowv2si
12701
12702 static const struct mips_builtin_description mips_builtins[] = {
12703 DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
12704 DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
12705 DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
12706 DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
12707 DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
12708 DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
12709 DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
12710 DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
12711
12712 DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
12713 DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
12714 DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
12715 DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
12716 DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
12717
12718 DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
12719 DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
12720 DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
12721 DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
12722 DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
12723 DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
12724
12725 DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
12726 DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
12727 DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
12728 DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
12729 DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
12730 DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
12731
12732 MIPS_FP_CONDITIONS (CMP_BUILTINS),
12733
12734 /* Built-in functions for the SB-1 processor. */
12735 DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
12736
12737 /* Built-in functions for the DSP ASE (32-bit and 64-bit). */
12738 DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
12739 DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
12740 DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
12741 DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
12742 DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
12743 DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
12744 DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
12745 DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
12746 DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
12747 DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
12748 DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
12749 DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
12750 DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
12751 DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
12752 DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
12753 DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
12754 DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
12755 DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
12756 DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
12757 DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
12758 DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
12759 DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
12760 DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
12761 DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
12762 DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
12763 DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
12764 DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
12765 DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
12766 DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
12767 DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
12768 DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
12769 DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
12770 DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
12771 DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
12772 DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
12773 DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
12774 DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
12775 DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
12776 DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
12777 DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
12778 DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
12779 DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
12780 DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
12781 DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
12782 DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
12783 DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
12784 DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
12785 DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
12786 DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
12787 DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
12788 DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
12789 DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
12790 DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
12791 DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
12792 DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
12793 DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
12794 DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
12795 DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
12796 DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
12797 DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
12798 DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
12799 DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
12800 DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
12801 DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
12802 BPOSGE_BUILTIN (32, dsp),
12803
12804 /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit). */
12805 DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
12806 DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12807 DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12808 DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
12809 DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
12810 DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
12811 DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
12812 DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
12813 DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
12814 DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
12815 DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12816 DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12817 DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
12818 DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12819 DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
12820 DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
12821 DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
12822 DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
12823 DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
12824 DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
12825 DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
12826 DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
12827 DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12828 DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12829 DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
12830 DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
12831 DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12832 DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12833 DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
12834 DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
12835 DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12836 DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
12837 DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
12838 DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
12839
12840 /* Built-in functions for the DSP ASE (32-bit only). */
12841 DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
12842 DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
12843 DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
12844 DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
12845 DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
12846 DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
12847 DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
12848 DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
12849 DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
12850 DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
12851 DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
12852 DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
12853 DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
12854 DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
12855 DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
12856 DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
12857 DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
12858 DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
12859 DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
12860 DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
12861 DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
12862
12863 /* The following are for the MIPS DSP ASE REV 2 (32-bit only). */
12864 DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12865 DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12866 DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dspr2_32),
12867 DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dspr2_32),
12868 DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dspr2_32),
12869 DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dspr2_32),
12870 DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12871 DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dspr2_32),
12872 DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dspr2_32),
12873 DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12874 DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12875 DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12876 DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12877 DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12878 DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
12879
12880 /* Builtin functions for ST Microelectronics Loongson-2E/2F cores. */
12881 LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
12882 LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
12883 LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
12884 LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
12885 LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12886 LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12887 LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
12888 LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12889 LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
12890 LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
12891 LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
12892 LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
12893 LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
12894 LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12895 LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12896 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
12897 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
12898 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12899 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12900 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
12901 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
12902 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
12903 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
12904 LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12905 LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12906 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
12907 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12908 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12909 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
12910 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12911 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
12912 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
12913 LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12914 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12915 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
12916 LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12917 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
12918 LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
12919 LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
12920 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12921 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12922 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12923 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12924 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12925 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12926 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12927 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12928 LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
12929 LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
12930 LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12931 LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
12932 LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12933 LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
12934 LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
12935 LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12936 LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
12937 LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
12938 LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
12939 LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12940 LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
12941 LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
12942 LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI_UQI),
12943 LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_V4HI_UQI),
12944 LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
12945 LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
12946 LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
12947 LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
12948 LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
12949 LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
12950 LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
12951 LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
12952 LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
12953 LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
12954 LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
12955 LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
12956 LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
12957 LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12958 LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12959 LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
12960 LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12961 LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
12962 LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
12963 LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
12964 LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
12965 LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
12966 LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12967 LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12968 LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12969 LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12970 LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
12971 LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
12972 LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12973 LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
12974 LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
12975 LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
12976 LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
12977 LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
12978 LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
12979 LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
12980
12981 /* Sundry other built-in functions. */
12982 DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
12983 };
12984
12985 /* MODE is a vector mode whose elements have type TYPE. Return the type
12986 of the vector itself. */
12987
12988 static tree
12989 mips_builtin_vector_type (tree type, enum machine_mode mode)
12990 {
12991 static tree types[2 * (int) MAX_MACHINE_MODE];
12992 int mode_index;
12993
12994 mode_index = (int) mode;
12995
12996 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
12997 mode_index += MAX_MACHINE_MODE;
12998
12999 if (types[mode_index] == NULL_TREE)
13000 types[mode_index] = build_vector_type_for_mode (type, mode);
13001 return types[mode_index];
13002 }
13003
13004 /* Return a type for 'const volatile void *'. */
13005
13006 static tree
13007 mips_build_cvpointer_type (void)
13008 {
13009 static tree cache;
13010
13011 if (cache == NULL_TREE)
13012 cache = build_pointer_type (build_qualified_type
13013 (void_type_node,
13014 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
13015 return cache;
13016 }
13017
13018 /* Source-level argument types. */
13019 #define MIPS_ATYPE_VOID void_type_node
13020 #define MIPS_ATYPE_INT integer_type_node
13021 #define MIPS_ATYPE_POINTER ptr_type_node
13022 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
13023
13024 /* Standard mode-based argument types. */
13025 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
13026 #define MIPS_ATYPE_SI intSI_type_node
13027 #define MIPS_ATYPE_USI unsigned_intSI_type_node
13028 #define MIPS_ATYPE_DI intDI_type_node
13029 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
13030 #define MIPS_ATYPE_SF float_type_node
13031 #define MIPS_ATYPE_DF double_type_node
13032
13033 /* Vector argument types. */
13034 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
13035 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
13036 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
13037 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
13038 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
13039 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
13040 #define MIPS_ATYPE_UV2SI \
13041 mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
13042 #define MIPS_ATYPE_UV4HI \
13043 mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
13044 #define MIPS_ATYPE_UV8QI \
13045 mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
13046
13047 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
13048 their associated MIPS_ATYPEs. */
13049 #define MIPS_FTYPE_ATYPES1(A, B) \
13050 MIPS_ATYPE_##A, MIPS_ATYPE_##B
13051
13052 #define MIPS_FTYPE_ATYPES2(A, B, C) \
13053 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
13054
13055 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
13056 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
13057
13058 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
13059 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
13060 MIPS_ATYPE_##E
13061
13062 /* Return the function type associated with function prototype TYPE. */
13063
13064 static tree
13065 mips_build_function_type (enum mips_function_type type)
13066 {
13067 static tree types[(int) MIPS_MAX_FTYPE_MAX];
13068
13069 if (types[(int) type] == NULL_TREE)
13070 switch (type)
13071 {
13072 #define DEF_MIPS_FTYPE(NUM, ARGS) \
13073 case MIPS_FTYPE_NAME##NUM ARGS: \
13074 types[(int) type] \
13075 = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS, \
13076 NULL_TREE); \
13077 break;
13078 #include "config/mips/mips-ftypes.def"
13079 #undef DEF_MIPS_FTYPE
13080 default:
13081 gcc_unreachable ();
13082 }
13083
13084 return types[(int) type];
13085 }
13086
13087 /* Implement TARGET_INIT_BUILTINS. */
13088
13089 static void
13090 mips_init_builtins (void)
13091 {
13092 const struct mips_builtin_description *d;
13093 unsigned int i;
13094
13095 /* Iterate through all of the bdesc arrays, initializing all of the
13096 builtin functions. */
13097 for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
13098 {
13099 d = &mips_builtins[i];
13100 if (d->avail ())
13101 add_builtin_function (d->name,
13102 mips_build_function_type (d->function_type),
13103 i, BUILT_IN_MD, NULL, NULL);
13104 }
13105 }
13106
13107 /* Take argument ARGNO from EXP's argument list and convert it into a
13108 form suitable for input operand OPNO of instruction ICODE. Return the
13109 value. */
13110
13111 static rtx
13112 mips_prepare_builtin_arg (enum insn_code icode,
13113 unsigned int opno, tree exp, unsigned int argno)
13114 {
13115 tree arg;
13116 rtx value;
13117 enum machine_mode mode;
13118
13119 arg = CALL_EXPR_ARG (exp, argno);
13120 value = expand_normal (arg);
13121 mode = insn_data[icode].operand[opno].mode;
13122 if (!insn_data[icode].operand[opno].predicate (value, mode))
13123 {
13124 /* We need to get the mode from ARG for two reasons:
13125
13126 - to cope with address operands, where MODE is the mode of the
13127 memory, rather than of VALUE itself.
13128
13129 - to cope with special predicates like pmode_register_operand,
13130 where MODE is VOIDmode. */
13131 value = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (arg)), value);
13132
13133 /* Check the predicate again. */
13134 if (!insn_data[icode].operand[opno].predicate (value, mode))
13135 {
13136 error ("invalid argument to built-in function");
13137 return const0_rtx;
13138 }
13139 }
13140
13141 return value;
13142 }
13143
13144 /* Return an rtx suitable for output operand OP of instruction ICODE.
13145 If TARGET is non-null, try to use it where possible. */
13146
13147 static rtx
13148 mips_prepare_builtin_target (enum insn_code icode, unsigned int op, rtx target)
13149 {
13150 enum machine_mode mode;
13151
13152 mode = insn_data[icode].operand[op].mode;
13153 if (target == 0 || !insn_data[icode].operand[op].predicate (target, mode))
13154 target = gen_reg_rtx (mode);
13155
13156 return target;
13157 }
13158
13159 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
13160 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
13161 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
13162 suggests a good place to put the result. */
13163
13164 static rtx
13165 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
13166 bool has_target_p)
13167 {
13168 rtx ops[MAX_RECOG_OPERANDS];
13169 int opno, argno;
13170
13171 /* Map any target to operand 0. */
13172 opno = 0;
13173 if (has_target_p)
13174 {
13175 target = mips_prepare_builtin_target (icode, opno, target);
13176 ops[opno] = target;
13177 opno++;
13178 }
13179
13180 /* Map the arguments to the other operands. The n_operands value
13181 for an expander includes match_dups and match_scratches as well as
13182 match_operands, so n_operands is only an upper bound on the number
13183 of arguments to the expander function. */
13184 gcc_assert (opno + call_expr_nargs (exp) <= insn_data[icode].n_operands);
13185 for (argno = 0; argno < call_expr_nargs (exp); argno++, opno++)
13186 ops[opno] = mips_prepare_builtin_arg (icode, opno, exp, argno);
13187
13188 switch (opno)
13189 {
13190 case 2:
13191 emit_insn (GEN_FCN (icode) (ops[0], ops[1]));
13192 break;
13193
13194 case 3:
13195 emit_insn (GEN_FCN (icode) (ops[0], ops[1], ops[2]));
13196 break;
13197
13198 case 4:
13199 emit_insn (GEN_FCN (icode) (ops[0], ops[1], ops[2], ops[3]));
13200 break;
13201
13202 default:
13203 gcc_unreachable ();
13204 }
13205 return target;
13206 }
13207
13208 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
13209 function; TYPE says which. EXP is the CALL_EXPR that calls the
13210 function, ICODE is the instruction that should be used to compare
13211 the first two arguments, and COND is the condition it should test.
13212 TARGET, if nonnull, suggests a good place to put the result. */
13213
13214 static rtx
13215 mips_expand_builtin_movtf (enum mips_builtin_type type,
13216 enum insn_code icode, enum mips_fp_condition cond,
13217 rtx target, tree exp)
13218 {
13219 rtx cmp_result, op0, op1;
13220
13221 cmp_result = mips_prepare_builtin_target (icode, 0, 0);
13222 op0 = mips_prepare_builtin_arg (icode, 1, exp, 0);
13223 op1 = mips_prepare_builtin_arg (icode, 2, exp, 1);
13224 emit_insn (GEN_FCN (icode) (cmp_result, op0, op1, GEN_INT (cond)));
13225
13226 icode = CODE_FOR_mips_cond_move_tf_ps;
13227 target = mips_prepare_builtin_target (icode, 0, target);
13228 if (type == MIPS_BUILTIN_MOVT)
13229 {
13230 op1 = mips_prepare_builtin_arg (icode, 2, exp, 2);
13231 op0 = mips_prepare_builtin_arg (icode, 1, exp, 3);
13232 }
13233 else
13234 {
13235 op0 = mips_prepare_builtin_arg (icode, 1, exp, 2);
13236 op1 = mips_prepare_builtin_arg (icode, 2, exp, 3);
13237 }
13238 emit_insn (gen_mips_cond_move_tf_ps (target, op0, op1, cmp_result));
13239 return target;
13240 }
13241
13242 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
13243 into TARGET otherwise. Return TARGET. */
13244
13245 static rtx
13246 mips_builtin_branch_and_move (rtx condition, rtx target,
13247 rtx value_if_true, rtx value_if_false)
13248 {
13249 rtx true_label, done_label;
13250
13251 true_label = gen_label_rtx ();
13252 done_label = gen_label_rtx ();
13253
13254 /* First assume that CONDITION is false. */
13255 mips_emit_move (target, value_if_false);
13256
13257 /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise. */
13258 emit_jump_insn (gen_condjump (condition, true_label));
13259 emit_jump_insn (gen_jump (done_label));
13260 emit_barrier ();
13261
13262 /* Fix TARGET if CONDITION is true. */
13263 emit_label (true_label);
13264 mips_emit_move (target, value_if_true);
13265
13266 emit_label (done_label);
13267 return target;
13268 }
13269
13270 /* Expand a comparison built-in function of type BUILTIN_TYPE. EXP is
13271 the CALL_EXPR that calls the function, ICODE is the code of the
13272 comparison instruction, and COND is the condition it should test.
13273 TARGET, if nonnull, suggests a good place to put the boolean result. */
13274
13275 static rtx
13276 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
13277 enum insn_code icode, enum mips_fp_condition cond,
13278 rtx target, tree exp)
13279 {
13280 rtx offset, condition, cmp_result, args[MAX_RECOG_OPERANDS];
13281 int argno;
13282
13283 if (target == 0 || GET_MODE (target) != SImode)
13284 target = gen_reg_rtx (SImode);
13285
13286 /* The instruction should have a target operand, an operand for each
13287 argument, and an operand for COND. */
13288 gcc_assert (call_expr_nargs (exp) + 2 == insn_data[icode].n_operands);
13289
13290 /* Prepare the operands to the comparison. */
13291 cmp_result = mips_prepare_builtin_target (icode, 0, 0);
13292 for (argno = 0; argno < call_expr_nargs (exp); argno++)
13293 args[argno] = mips_prepare_builtin_arg (icode, argno + 1, exp, argno);
13294
13295 switch (insn_data[icode].n_operands)
13296 {
13297 case 4:
13298 emit_insn (GEN_FCN (icode) (cmp_result, args[0], args[1],
13299 GEN_INT (cond)));
13300 break;
13301
13302 case 6:
13303 emit_insn (GEN_FCN (icode) (cmp_result, args[0], args[1],
13304 args[2], args[3], GEN_INT (cond)));
13305 break;
13306
13307 default:
13308 gcc_unreachable ();
13309 }
13310
13311 /* If the comparison sets more than one register, we define the result
13312 to be 0 if all registers are false and -1 if all registers are true.
13313 The value of the complete result is indeterminate otherwise. */
13314 switch (builtin_type)
13315 {
13316 case MIPS_BUILTIN_CMP_ALL:
13317 condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
13318 return mips_builtin_branch_and_move (condition, target,
13319 const0_rtx, const1_rtx);
13320
13321 case MIPS_BUILTIN_CMP_UPPER:
13322 case MIPS_BUILTIN_CMP_LOWER:
13323 offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
13324 condition = gen_single_cc (cmp_result, offset);
13325 return mips_builtin_branch_and_move (condition, target,
13326 const1_rtx, const0_rtx);
13327
13328 default:
13329 condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
13330 return mips_builtin_branch_and_move (condition, target,
13331 const1_rtx, const0_rtx);
13332 }
13333 }
13334
13335 /* Expand a bposge built-in function of type BUILTIN_TYPE. TARGET,
13336 if nonnull, suggests a good place to put the boolean result. */
13337
13338 static rtx
13339 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
13340 {
13341 rtx condition, cmp_result;
13342 int cmp_value;
13343
13344 if (target == 0 || GET_MODE (target) != SImode)
13345 target = gen_reg_rtx (SImode);
13346
13347 cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
13348
13349 if (builtin_type == MIPS_BUILTIN_BPOSGE32)
13350 cmp_value = 32;
13351 else
13352 gcc_assert (0);
13353
13354 condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
13355 return mips_builtin_branch_and_move (condition, target,
13356 const1_rtx, const0_rtx);
13357 }
13358
13359 /* Implement TARGET_EXPAND_BUILTIN. */
13360
13361 static rtx
13362 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
13363 enum machine_mode mode, int ignore)
13364 {
13365 tree fndecl;
13366 unsigned int fcode, avail;
13367 const struct mips_builtin_description *d;
13368
13369 fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13370 fcode = DECL_FUNCTION_CODE (fndecl);
13371 gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
13372 d = &mips_builtins[fcode];
13373 avail = d->avail ();
13374 gcc_assert (avail != 0);
13375 if (TARGET_MIPS16)
13376 {
13377 error ("built-in function %qE not supported for MIPS16",
13378 DECL_NAME (fndecl));
13379 return ignore ? const0_rtx : CONST0_RTX (mode);
13380 }
13381 switch (d->builtin_type)
13382 {
13383 case MIPS_BUILTIN_DIRECT:
13384 return mips_expand_builtin_direct (d->icode, target, exp, true);
13385
13386 case MIPS_BUILTIN_DIRECT_NO_TARGET:
13387 return mips_expand_builtin_direct (d->icode, target, exp, false);
13388
13389 case MIPS_BUILTIN_MOVT:
13390 case MIPS_BUILTIN_MOVF:
13391 return mips_expand_builtin_movtf (d->builtin_type, d->icode,
13392 d->cond, target, exp);
13393
13394 case MIPS_BUILTIN_CMP_ANY:
13395 case MIPS_BUILTIN_CMP_ALL:
13396 case MIPS_BUILTIN_CMP_UPPER:
13397 case MIPS_BUILTIN_CMP_LOWER:
13398 case MIPS_BUILTIN_CMP_SINGLE:
13399 return mips_expand_builtin_compare (d->builtin_type, d->icode,
13400 d->cond, target, exp);
13401
13402 case MIPS_BUILTIN_BPOSGE32:
13403 return mips_expand_builtin_bposge (d->builtin_type, target);
13404 }
13405 gcc_unreachable ();
13406 }
13407 \f
13408 /* An entry in the MIPS16 constant pool. VALUE is the pool constant,
13409 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
13410 struct mips16_constant {
13411 struct mips16_constant *next;
13412 rtx value;
13413 rtx label;
13414 enum machine_mode mode;
13415 };
13416
13417 /* Information about an incomplete MIPS16 constant pool. FIRST is the
13418 first constant, HIGHEST_ADDRESS is the highest address that the first
13419 byte of the pool can have, and INSN_ADDRESS is the current instruction
13420 address. */
13421 struct mips16_constant_pool {
13422 struct mips16_constant *first;
13423 int highest_address;
13424 int insn_address;
13425 };
13426
13427 /* Add constant VALUE to POOL and return its label. MODE is the
13428 value's mode (used for CONST_INTs, etc.). */
13429
13430 static rtx
13431 mips16_add_constant (struct mips16_constant_pool *pool,
13432 rtx value, enum machine_mode mode)
13433 {
13434 struct mips16_constant **p, *c;
13435 bool first_of_size_p;
13436
13437 /* See whether the constant is already in the pool. If so, return the
13438 existing label, otherwise leave P pointing to the place where the
13439 constant should be added.
13440
13441 Keep the pool sorted in increasing order of mode size so that we can
13442 reduce the number of alignments needed. */
13443 first_of_size_p = true;
13444 for (p = &pool->first; *p != 0; p = &(*p)->next)
13445 {
13446 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
13447 return (*p)->label;
13448 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
13449 break;
13450 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
13451 first_of_size_p = false;
13452 }
13453
13454 /* In the worst case, the constant needed by the earliest instruction
13455 will end up at the end of the pool. The entire pool must then be
13456 accessible from that instruction.
13457
13458 When adding the first constant, set the pool's highest address to
13459 the address of the first out-of-range byte. Adjust this address
13460 downwards each time a new constant is added. */
13461 if (pool->first == 0)
13462 /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
13463 of the instruction with the lowest two bits clear. The base PC
13464 value for LDPC has the lowest three bits clear. Assume the worst
13465 case here; namely that the PC-relative instruction occupies the
13466 last 2 bytes in an aligned word. */
13467 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
13468 pool->highest_address -= GET_MODE_SIZE (mode);
13469 if (first_of_size_p)
13470 /* Take into account the worst possible padding due to alignment. */
13471 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
13472
13473 /* Create a new entry. */
13474 c = XNEW (struct mips16_constant);
13475 c->value = value;
13476 c->mode = mode;
13477 c->label = gen_label_rtx ();
13478 c->next = *p;
13479 *p = c;
13480
13481 return c->label;
13482 }
13483
13484 /* Output constant VALUE after instruction INSN and return the last
13485 instruction emitted. MODE is the mode of the constant. */
13486
13487 static rtx
13488 mips16_emit_constants_1 (enum machine_mode mode, rtx value, rtx insn)
13489 {
13490 if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
13491 {
13492 rtx size = GEN_INT (GET_MODE_SIZE (mode));
13493 return emit_insn_after (gen_consttable_int (value, size), insn);
13494 }
13495
13496 if (SCALAR_FLOAT_MODE_P (mode))
13497 return emit_insn_after (gen_consttable_float (value), insn);
13498
13499 if (VECTOR_MODE_P (mode))
13500 {
13501 int i;
13502
13503 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
13504 insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
13505 CONST_VECTOR_ELT (value, i), insn);
13506 return insn;
13507 }
13508
13509 gcc_unreachable ();
13510 }
13511
13512 /* Dump out the constants in CONSTANTS after INSN. */
13513
13514 static void
13515 mips16_emit_constants (struct mips16_constant *constants, rtx insn)
13516 {
13517 struct mips16_constant *c, *next;
13518 int align;
13519
13520 align = 0;
13521 for (c = constants; c != NULL; c = next)
13522 {
13523 /* If necessary, increase the alignment of PC. */
13524 if (align < GET_MODE_SIZE (c->mode))
13525 {
13526 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
13527 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
13528 }
13529 align = GET_MODE_SIZE (c->mode);
13530
13531 insn = emit_label_after (c->label, insn);
13532 insn = mips16_emit_constants_1 (c->mode, c->value, insn);
13533
13534 next = c->next;
13535 free (c);
13536 }
13537
13538 emit_barrier_after (insn);
13539 }
13540
13541 /* Return the length of instruction INSN. */
13542
13543 static int
13544 mips16_insn_length (rtx insn)
13545 {
13546 if (JUMP_P (insn))
13547 {
13548 rtx body = PATTERN (insn);
13549 if (GET_CODE (body) == ADDR_VEC)
13550 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
13551 if (GET_CODE (body) == ADDR_DIFF_VEC)
13552 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
13553 }
13554 return get_attr_length (insn);
13555 }
13556
13557 /* If *X is a symbolic constant that refers to the constant pool, add
13558 the constant to POOL and rewrite *X to use the constant's label. */
13559
13560 static void
13561 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
13562 {
13563 rtx base, offset, label;
13564
13565 split_const (*x, &base, &offset);
13566 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
13567 {
13568 label = mips16_add_constant (pool, get_pool_constant (base),
13569 get_pool_mode (base));
13570 base = gen_rtx_LABEL_REF (Pmode, label);
13571 *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
13572 }
13573 }
13574
13575 /* This structure is used to communicate with mips16_rewrite_pool_refs.
13576 INSN is the instruction we're rewriting and POOL points to the current
13577 constant pool. */
13578 struct mips16_rewrite_pool_refs_info {
13579 rtx insn;
13580 struct mips16_constant_pool *pool;
13581 };
13582
13583 /* Rewrite *X so that constant pool references refer to the constant's
13584 label instead. DATA points to a mips16_rewrite_pool_refs_info
13585 structure. */
13586
13587 static int
13588 mips16_rewrite_pool_refs (rtx *x, void *data)
13589 {
13590 struct mips16_rewrite_pool_refs_info *info =
13591 (struct mips16_rewrite_pool_refs_info *) data;
13592
13593 if (force_to_mem_operand (*x, Pmode))
13594 {
13595 rtx mem = force_const_mem (GET_MODE (*x), *x);
13596 validate_change (info->insn, x, mem, false);
13597 }
13598
13599 if (MEM_P (*x))
13600 {
13601 mips16_rewrite_pool_constant (info->pool, &XEXP (*x, 0));
13602 return -1;
13603 }
13604
13605 if (TARGET_MIPS16_TEXT_LOADS)
13606 mips16_rewrite_pool_constant (info->pool, x);
13607
13608 return GET_CODE (*x) == CONST ? -1 : 0;
13609 }
13610
13611 /* Return whether CFG is used in mips_reorg. */
13612
13613 static bool
13614 mips_cfg_in_reorg (void)
13615 {
13616 return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
13617 || TARGET_RELAX_PIC_CALLS);
13618 }
13619
13620 /* Build MIPS16 constant pools. */
13621
13622 static void
13623 mips16_lay_out_constants (void)
13624 {
13625 struct mips16_constant_pool pool;
13626 struct mips16_rewrite_pool_refs_info info;
13627 rtx insn, barrier;
13628
13629 if (!TARGET_MIPS16_PCREL_LOADS)
13630 return;
13631
13632 if (mips_cfg_in_reorg ())
13633 split_all_insns ();
13634 else
13635 split_all_insns_noflow ();
13636 barrier = 0;
13637 memset (&pool, 0, sizeof (pool));
13638 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13639 {
13640 /* Rewrite constant pool references in INSN. */
13641 if (USEFUL_INSN_P (insn))
13642 {
13643 info.insn = insn;
13644 info.pool = &pool;
13645 for_each_rtx (&PATTERN (insn), mips16_rewrite_pool_refs, &info);
13646 }
13647
13648 pool.insn_address += mips16_insn_length (insn);
13649
13650 if (pool.first != NULL)
13651 {
13652 /* If there are no natural barriers between the first user of
13653 the pool and the highest acceptable address, we'll need to
13654 create a new instruction to jump around the constant pool.
13655 In the worst case, this instruction will be 4 bytes long.
13656
13657 If it's too late to do this transformation after INSN,
13658 do it immediately before INSN. */
13659 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
13660 {
13661 rtx label, jump;
13662
13663 label = gen_label_rtx ();
13664
13665 jump = emit_jump_insn_before (gen_jump (label), insn);
13666 JUMP_LABEL (jump) = label;
13667 LABEL_NUSES (label) = 1;
13668 barrier = emit_barrier_after (jump);
13669
13670 emit_label_after (label, barrier);
13671 pool.insn_address += 4;
13672 }
13673
13674 /* See whether the constant pool is now out of range of the first
13675 user. If so, output the constants after the previous barrier.
13676 Note that any instructions between BARRIER and INSN (inclusive)
13677 will use negative offsets to refer to the pool. */
13678 if (pool.insn_address > pool.highest_address)
13679 {
13680 mips16_emit_constants (pool.first, barrier);
13681 pool.first = NULL;
13682 barrier = 0;
13683 }
13684 else if (BARRIER_P (insn))
13685 barrier = insn;
13686 }
13687 }
13688 mips16_emit_constants (pool.first, get_last_insn ());
13689 }
13690 \f
13691 /* Return true if it is worth r10k_simplify_address's while replacing
13692 an address with X. We are looking for constants, and for addresses
13693 at a known offset from the incoming stack pointer. */
13694
13695 static bool
13696 r10k_simplified_address_p (rtx x)
13697 {
13698 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
13699 x = XEXP (x, 0);
13700 return x == virtual_incoming_args_rtx || CONSTANT_P (x);
13701 }
13702
13703 /* X is an expression that appears in INSN. Try to use the UD chains
13704 to simplify it, returning the simplified form on success and the
13705 original form otherwise. Replace the incoming value of $sp with
13706 virtual_incoming_args_rtx (which should never occur in X otherwise). */
13707
13708 static rtx
13709 r10k_simplify_address (rtx x, rtx insn)
13710 {
13711 rtx newx, op0, op1, set, def_insn, note;
13712 df_ref use, def;
13713 struct df_link *defs;
13714
13715 newx = NULL_RTX;
13716 if (UNARY_P (x))
13717 {
13718 op0 = r10k_simplify_address (XEXP (x, 0), insn);
13719 if (op0 != XEXP (x, 0))
13720 newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
13721 op0, GET_MODE (XEXP (x, 0)));
13722 }
13723 else if (BINARY_P (x))
13724 {
13725 op0 = r10k_simplify_address (XEXP (x, 0), insn);
13726 op1 = r10k_simplify_address (XEXP (x, 1), insn);
13727 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
13728 newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
13729 }
13730 else if (GET_CODE (x) == LO_SUM)
13731 {
13732 /* LO_SUMs can be offset from HIGHs, if we know they won't
13733 overflow. See mips_classify_address for the rationale behind
13734 the lax check. */
13735 op0 = r10k_simplify_address (XEXP (x, 0), insn);
13736 if (GET_CODE (op0) == HIGH)
13737 newx = XEXP (x, 1);
13738 }
13739 else if (REG_P (x))
13740 {
13741 /* Uses are recorded by regno_reg_rtx, not X itself. */
13742 use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
13743 gcc_assert (use);
13744 defs = DF_REF_CHAIN (use);
13745
13746 /* Require a single definition. */
13747 if (defs && defs->next == NULL)
13748 {
13749 def = defs->ref;
13750 if (DF_REF_IS_ARTIFICIAL (def))
13751 {
13752 /* Replace the incoming value of $sp with
13753 virtual_incoming_args_rtx. */
13754 if (x == stack_pointer_rtx
13755 && DF_REF_BB (def) == ENTRY_BLOCK_PTR)
13756 newx = virtual_incoming_args_rtx;
13757 }
13758 else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
13759 DF_REF_BB (def)))
13760 {
13761 /* Make sure that DEF_INSN is a single set of REG. */
13762 def_insn = DF_REF_INSN (def);
13763 if (NONJUMP_INSN_P (def_insn))
13764 {
13765 set = single_set (def_insn);
13766 if (set && rtx_equal_p (SET_DEST (set), x))
13767 {
13768 /* Prefer to use notes, since the def-use chains
13769 are often shorter. */
13770 note = find_reg_equal_equiv_note (def_insn);
13771 if (note)
13772 newx = XEXP (note, 0);
13773 else
13774 newx = SET_SRC (set);
13775 newx = r10k_simplify_address (newx, def_insn);
13776 }
13777 }
13778 }
13779 }
13780 }
13781 if (newx && r10k_simplified_address_p (newx))
13782 return newx;
13783 return x;
13784 }
13785
13786 /* Return true if ADDRESS is known to be an uncached address
13787 on R10K systems. */
13788
13789 static bool
13790 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
13791 {
13792 unsigned HOST_WIDE_INT upper;
13793
13794 /* Check for KSEG1. */
13795 if (address + 0x60000000 < 0x20000000)
13796 return true;
13797
13798 /* Check for uncached XKPHYS addresses. */
13799 if (Pmode == DImode)
13800 {
13801 upper = (address >> 40) & 0xf9ffff;
13802 if (upper == 0x900000 || upper == 0xb80000)
13803 return true;
13804 }
13805 return false;
13806 }
13807
13808 /* Return true if we can prove that an access to address X in instruction
13809 INSN would be safe from R10K speculation. This X is a general
13810 expression; it might not be a legitimate address. */
13811
13812 static bool
13813 r10k_safe_address_p (rtx x, rtx insn)
13814 {
13815 rtx base, offset;
13816 HOST_WIDE_INT offset_val;
13817
13818 x = r10k_simplify_address (x, insn);
13819
13820 /* Check for references to the stack frame. It doesn't really matter
13821 how much of the frame has been allocated at INSN; -mr10k-cache-barrier
13822 allows us to assume that accesses to any part of the eventual frame
13823 is safe from speculation at any point in the function. */
13824 mips_split_plus (x, &base, &offset_val);
13825 if (base == virtual_incoming_args_rtx
13826 && offset_val >= -cfun->machine->frame.total_size
13827 && offset_val < cfun->machine->frame.args_size)
13828 return true;
13829
13830 /* Check for uncached addresses. */
13831 if (CONST_INT_P (x))
13832 return r10k_uncached_address_p (INTVAL (x));
13833
13834 /* Check for accesses to a static object. */
13835 split_const (x, &base, &offset);
13836 return offset_within_block_p (base, INTVAL (offset));
13837 }
13838
13839 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
13840 an in-range access to an automatic variable, or to an object with
13841 a link-time-constant address. */
13842
13843 static bool
13844 r10k_safe_mem_expr_p (tree expr, rtx offset)
13845 {
13846 if (expr == NULL_TREE
13847 || offset == NULL_RTX
13848 || !CONST_INT_P (offset)
13849 || INTVAL (offset) < 0
13850 || INTVAL (offset) >= int_size_in_bytes (TREE_TYPE (expr)))
13851 return false;
13852
13853 while (TREE_CODE (expr) == COMPONENT_REF)
13854 {
13855 expr = TREE_OPERAND (expr, 0);
13856 if (expr == NULL_TREE)
13857 return false;
13858 }
13859
13860 return DECL_P (expr);
13861 }
13862
13863 /* A for_each_rtx callback for which DATA points to the instruction
13864 containing *X. Stop the search if we find a MEM that is not safe
13865 from R10K speculation. */
13866
13867 static int
13868 r10k_needs_protection_p_1 (rtx *loc, void *data)
13869 {
13870 rtx mem;
13871
13872 mem = *loc;
13873 if (!MEM_P (mem))
13874 return 0;
13875
13876 if (r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
13877 return -1;
13878
13879 if (r10k_safe_address_p (XEXP (mem, 0), (rtx) data))
13880 return -1;
13881
13882 return 1;
13883 }
13884
13885 /* A note_stores callback for which DATA points to an instruction pointer.
13886 If *DATA is nonnull, make it null if it X contains a MEM that is not
13887 safe from R10K speculation. */
13888
13889 static void
13890 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
13891 void *data)
13892 {
13893 rtx *insn_ptr;
13894
13895 insn_ptr = (rtx *) data;
13896 if (*insn_ptr && for_each_rtx (&x, r10k_needs_protection_p_1, *insn_ptr))
13897 *insn_ptr = NULL_RTX;
13898 }
13899
13900 /* A for_each_rtx callback that iterates over the pattern of a CALL_INSN.
13901 Return nonzero if the call is not to a declared function. */
13902
13903 static int
13904 r10k_needs_protection_p_call (rtx *loc, void *data ATTRIBUTE_UNUSED)
13905 {
13906 rtx x;
13907
13908 x = *loc;
13909 if (!MEM_P (x))
13910 return 0;
13911
13912 x = XEXP (x, 0);
13913 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DECL (x))
13914 return -1;
13915
13916 return 1;
13917 }
13918
13919 /* Return true if instruction INSN needs to be protected by an R10K
13920 cache barrier. */
13921
13922 static bool
13923 r10k_needs_protection_p (rtx insn)
13924 {
13925 if (CALL_P (insn))
13926 return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_call, NULL);
13927
13928 if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
13929 {
13930 note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
13931 return insn == NULL_RTX;
13932 }
13933
13934 return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_1, insn);
13935 }
13936
13937 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
13938 edge is unconditional. */
13939
13940 static bool
13941 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
13942 {
13943 edge_iterator ei;
13944 edge e;
13945
13946 FOR_EACH_EDGE (e, ei, bb->preds)
13947 if (!single_succ_p (e->src)
13948 || !TEST_BIT (protected_bbs, e->src->index)
13949 || (e->flags & EDGE_COMPLEX) != 0)
13950 return false;
13951 return true;
13952 }
13953
13954 /* Implement -mr10k-cache-barrier= for the current function. */
13955
13956 static void
13957 r10k_insert_cache_barriers (void)
13958 {
13959 int *rev_post_order;
13960 unsigned int i, n;
13961 basic_block bb;
13962 sbitmap protected_bbs;
13963 rtx insn, end, unprotected_region;
13964
13965 if (TARGET_MIPS16)
13966 {
13967 sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
13968 return;
13969 }
13970
13971 /* Calculate dominators. */
13972 calculate_dominance_info (CDI_DOMINATORS);
13973
13974 /* Bit X of PROTECTED_BBS is set if the last operation in basic block
13975 X is protected by a cache barrier. */
13976 protected_bbs = sbitmap_alloc (last_basic_block);
13977 sbitmap_zero (protected_bbs);
13978
13979 /* Iterate over the basic blocks in reverse post-order. */
13980 rev_post_order = XNEWVEC (int, last_basic_block);
13981 n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
13982 for (i = 0; i < n; i++)
13983 {
13984 bb = BASIC_BLOCK (rev_post_order[i]);
13985
13986 /* If this block is only reached by unconditional edges, and if the
13987 source of every edge is protected, the beginning of the block is
13988 also protected. */
13989 if (r10k_protected_bb_p (bb, protected_bbs))
13990 unprotected_region = NULL_RTX;
13991 else
13992 unprotected_region = pc_rtx;
13993 end = NEXT_INSN (BB_END (bb));
13994
13995 /* UNPROTECTED_REGION is:
13996
13997 - null if we are processing a protected region,
13998 - pc_rtx if we are processing an unprotected region but have
13999 not yet found the first instruction in it
14000 - the first instruction in an unprotected region otherwise. */
14001 for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
14002 {
14003 if (unprotected_region && USEFUL_INSN_P (insn))
14004 {
14005 if (recog_memoized (insn) == CODE_FOR_mips_cache)
14006 /* This CACHE instruction protects the following code. */
14007 unprotected_region = NULL_RTX;
14008 else
14009 {
14010 /* See if INSN is the first instruction in this
14011 unprotected region. */
14012 if (unprotected_region == pc_rtx)
14013 unprotected_region = insn;
14014
14015 /* See if INSN needs to be protected. If so,
14016 we must insert a cache barrier somewhere between
14017 PREV_INSN (UNPROTECTED_REGION) and INSN. It isn't
14018 clear which position is better performance-wise,
14019 but as a tie-breaker, we assume that it is better
14020 to allow delay slots to be back-filled where
14021 possible, and that it is better not to insert
14022 barriers in the middle of already-scheduled code.
14023 We therefore insert the barrier at the beginning
14024 of the region. */
14025 if (r10k_needs_protection_p (insn))
14026 {
14027 emit_insn_before (gen_r10k_cache_barrier (),
14028 unprotected_region);
14029 unprotected_region = NULL_RTX;
14030 }
14031 }
14032 }
14033
14034 if (CALL_P (insn))
14035 /* The called function is not required to protect the exit path.
14036 The code that follows a call is therefore unprotected. */
14037 unprotected_region = pc_rtx;
14038 }
14039
14040 /* Record whether the end of this block is protected. */
14041 if (unprotected_region == NULL_RTX)
14042 SET_BIT (protected_bbs, bb->index);
14043 }
14044 XDELETEVEC (rev_post_order);
14045
14046 sbitmap_free (protected_bbs);
14047
14048 free_dominance_info (CDI_DOMINATORS);
14049 }
14050 \f
14051 /* If INSN is a call, return the underlying CALL expr. Return NULL_RTX
14052 otherwise. If INSN has two call rtx, then store the second one in
14053 SECOND_CALL. */
14054
14055 static rtx
14056 mips_call_expr_from_insn (rtx insn, rtx *second_call)
14057 {
14058 rtx x;
14059 rtx x2;
14060
14061 if (!CALL_P (insn))
14062 return NULL_RTX;
14063
14064 x = PATTERN (insn);
14065 if (GET_CODE (x) == PARALLEL)
14066 {
14067 /* Calls returning complex values have two CALL rtx. Look for the second
14068 one here, and return it via the SECOND_CALL arg. */
14069 x2 = XVECEXP (x, 0, 1);
14070 if (GET_CODE (x2) == SET)
14071 x2 = XEXP (x2, 1);
14072 if (GET_CODE (x2) == CALL)
14073 *second_call = x2;
14074
14075 x = XVECEXP (x, 0, 0);
14076 }
14077 if (GET_CODE (x) == SET)
14078 x = XEXP (x, 1);
14079 gcc_assert (GET_CODE (x) == CALL);
14080
14081 return x;
14082 }
14083
14084 /* REG is set in DEF. See if the definition is one of the ways we load a
14085 register with a symbol address for a mips_use_pic_fn_addr_reg_p call. If
14086 it is return the symbol reference of the function, otherwise return
14087 NULL_RTX. */
14088
14089 static rtx
14090 mips_pic_call_symbol_from_set (df_ref def, rtx reg)
14091 {
14092 rtx def_insn, set;
14093
14094 if (DF_REF_IS_ARTIFICIAL (def))
14095 return NULL_RTX;
14096
14097 def_insn = DF_REF_INSN (def);
14098 set = single_set (def_insn);
14099 if (set && rtx_equal_p (SET_DEST (set), reg))
14100 {
14101 rtx note, src, symbol;
14102
14103 /* First, look at REG_EQUAL/EQUIV notes. */
14104 note = find_reg_equal_equiv_note (def_insn);
14105 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
14106 return XEXP (note, 0);
14107
14108 /* For %call16 references we don't have REG_EQUAL. */
14109 src = SET_SRC (set);
14110 symbol = mips_strip_unspec_call (src);
14111 if (symbol)
14112 {
14113 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
14114 return symbol;
14115 }
14116
14117 /* Follow simple register copies. */
14118 if (REG_P (src))
14119 return mips_find_pic_call_symbol (def_insn, src);
14120 }
14121
14122 return NULL_RTX;
14123 }
14124
14125 /* Find the definition of the use of REG in INSN. See if the definition is
14126 one of the ways we load a register with a symbol address for a
14127 mips_use_pic_fn_addr_reg_p call. If it is return the symbol reference of
14128 the function, otherwise return NULL_RTX. */
14129
14130 static rtx
14131 mips_find_pic_call_symbol (rtx insn, rtx reg)
14132 {
14133 df_ref use;
14134 struct df_link *defs;
14135 rtx symbol;
14136
14137 use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
14138 if (!use)
14139 return NULL_RTX;
14140 defs = DF_REF_CHAIN (use);
14141 if (!defs)
14142 return NULL_RTX;
14143 symbol = mips_pic_call_symbol_from_set (defs->ref, reg);
14144 if (!symbol)
14145 return NULL_RTX;
14146
14147 /* If we have more than one definition, they need to be identical. */
14148 for (defs = defs->next; defs; defs = defs->next)
14149 {
14150 rtx other;
14151
14152 other = mips_pic_call_symbol_from_set (defs->ref, reg);
14153 if (!rtx_equal_p (symbol, other))
14154 return NULL_RTX;
14155 }
14156
14157 return symbol;
14158 }
14159
14160 /* Replace the args_size operand of the call expression CALL with the
14161 call-attribute UNSPEC and fill in SYMBOL as the function symbol. */
14162
14163 static void
14164 mips_annotate_pic_call_expr (rtx call, rtx symbol)
14165 {
14166 rtx args_size;
14167
14168 args_size = XEXP (call, 1);
14169 XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
14170 gen_rtvec (2, args_size, symbol),
14171 UNSPEC_CALL_ATTR);
14172 }
14173
14174 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression. See
14175 if instead of the arg_size argument it contains the call attributes. If
14176 yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
14177 symbol from the call attributes. Also return false if ARGS_SIZE_OPNO is
14178 -1. */
14179
14180 bool
14181 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
14182 {
14183 rtx args_size, symbol;
14184
14185 if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
14186 return false;
14187
14188 args_size = operands[args_size_opno];
14189 if (GET_CODE (args_size) != UNSPEC)
14190 return false;
14191 gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
14192
14193 symbol = XVECEXP (args_size, 0, 1);
14194 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
14195
14196 operands[args_size_opno] = symbol;
14197 return true;
14198 }
14199
14200 /* Use DF to annotate PIC indirect calls with the function symbol they
14201 dispatch to. */
14202
14203 static void
14204 mips_annotate_pic_calls (void)
14205 {
14206 basic_block bb;
14207 rtx insn;
14208
14209 FOR_EACH_BB (bb)
14210 FOR_BB_INSNS (bb, insn)
14211 {
14212 rtx call, reg, symbol, second_call;
14213
14214 second_call = 0;
14215 call = mips_call_expr_from_insn (insn, &second_call);
14216 if (!call)
14217 continue;
14218 gcc_assert (MEM_P (XEXP (call, 0)));
14219 reg = XEXP (XEXP (call, 0), 0);
14220 if (!REG_P (reg))
14221 continue;
14222
14223 symbol = mips_find_pic_call_symbol (insn, reg);
14224 if (symbol)
14225 {
14226 mips_annotate_pic_call_expr (call, symbol);
14227 if (second_call)
14228 mips_annotate_pic_call_expr (second_call, symbol);
14229 }
14230 }
14231 }
14232 \f
14233 /* A temporary variable used by for_each_rtx callbacks, etc. */
14234 static rtx mips_sim_insn;
14235
14236 /* A structure representing the state of the processor pipeline.
14237 Used by the mips_sim_* family of functions. */
14238 struct mips_sim {
14239 /* The maximum number of instructions that can be issued in a cycle.
14240 (Caches mips_issue_rate.) */
14241 unsigned int issue_rate;
14242
14243 /* The current simulation time. */
14244 unsigned int time;
14245
14246 /* How many more instructions can be issued in the current cycle. */
14247 unsigned int insns_left;
14248
14249 /* LAST_SET[X].INSN is the last instruction to set register X.
14250 LAST_SET[X].TIME is the time at which that instruction was issued.
14251 INSN is null if no instruction has yet set register X. */
14252 struct {
14253 rtx insn;
14254 unsigned int time;
14255 } last_set[FIRST_PSEUDO_REGISTER];
14256
14257 /* The pipeline's current DFA state. */
14258 state_t dfa_state;
14259 };
14260
14261 /* Reset STATE to the initial simulation state. */
14262
14263 static void
14264 mips_sim_reset (struct mips_sim *state)
14265 {
14266 state->time = 0;
14267 state->insns_left = state->issue_rate;
14268 memset (&state->last_set, 0, sizeof (state->last_set));
14269 state_reset (state->dfa_state);
14270 }
14271
14272 /* Initialize STATE before its first use. DFA_STATE points to an
14273 allocated but uninitialized DFA state. */
14274
14275 static void
14276 mips_sim_init (struct mips_sim *state, state_t dfa_state)
14277 {
14278 state->issue_rate = mips_issue_rate ();
14279 state->dfa_state = dfa_state;
14280 mips_sim_reset (state);
14281 }
14282
14283 /* Advance STATE by one clock cycle. */
14284
14285 static void
14286 mips_sim_next_cycle (struct mips_sim *state)
14287 {
14288 state->time++;
14289 state->insns_left = state->issue_rate;
14290 state_transition (state->dfa_state, 0);
14291 }
14292
14293 /* Advance simulation state STATE until instruction INSN can read
14294 register REG. */
14295
14296 static void
14297 mips_sim_wait_reg (struct mips_sim *state, rtx insn, rtx reg)
14298 {
14299 unsigned int regno, end_regno;
14300
14301 end_regno = END_REGNO (reg);
14302 for (regno = REGNO (reg); regno < end_regno; regno++)
14303 if (state->last_set[regno].insn != 0)
14304 {
14305 unsigned int t;
14306
14307 t = (state->last_set[regno].time
14308 + insn_latency (state->last_set[regno].insn, insn));
14309 while (state->time < t)
14310 mips_sim_next_cycle (state);
14311 }
14312 }
14313
14314 /* A for_each_rtx callback. If *X is a register, advance simulation state
14315 DATA until mips_sim_insn can read the register's value. */
14316
14317 static int
14318 mips_sim_wait_regs_2 (rtx *x, void *data)
14319 {
14320 if (REG_P (*x))
14321 mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *x);
14322 return 0;
14323 }
14324
14325 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X. */
14326
14327 static void
14328 mips_sim_wait_regs_1 (rtx *x, void *data)
14329 {
14330 for_each_rtx (x, mips_sim_wait_regs_2, data);
14331 }
14332
14333 /* Advance simulation state STATE until all of INSN's register
14334 dependencies are satisfied. */
14335
14336 static void
14337 mips_sim_wait_regs (struct mips_sim *state, rtx insn)
14338 {
14339 mips_sim_insn = insn;
14340 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
14341 }
14342
14343 /* Advance simulation state STATE until the units required by
14344 instruction INSN are available. */
14345
14346 static void
14347 mips_sim_wait_units (struct mips_sim *state, rtx insn)
14348 {
14349 state_t tmp_state;
14350
14351 tmp_state = alloca (state_size ());
14352 while (state->insns_left == 0
14353 || (memcpy (tmp_state, state->dfa_state, state_size ()),
14354 state_transition (tmp_state, insn) >= 0))
14355 mips_sim_next_cycle (state);
14356 }
14357
14358 /* Advance simulation state STATE until INSN is ready to issue. */
14359
14360 static void
14361 mips_sim_wait_insn (struct mips_sim *state, rtx insn)
14362 {
14363 mips_sim_wait_regs (state, insn);
14364 mips_sim_wait_units (state, insn);
14365 }
14366
14367 /* mips_sim_insn has just set X. Update the LAST_SET array
14368 in simulation state DATA. */
14369
14370 static void
14371 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
14372 {
14373 struct mips_sim *state;
14374
14375 state = (struct mips_sim *) data;
14376 if (REG_P (x))
14377 {
14378 unsigned int regno, end_regno;
14379
14380 end_regno = END_REGNO (x);
14381 for (regno = REGNO (x); regno < end_regno; regno++)
14382 {
14383 state->last_set[regno].insn = mips_sim_insn;
14384 state->last_set[regno].time = state->time;
14385 }
14386 }
14387 }
14388
14389 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
14390 can issue immediately (i.e., that mips_sim_wait_insn has already
14391 been called). */
14392
14393 static void
14394 mips_sim_issue_insn (struct mips_sim *state, rtx insn)
14395 {
14396 state_transition (state->dfa_state, insn);
14397 state->insns_left--;
14398
14399 mips_sim_insn = insn;
14400 note_stores (PATTERN (insn), mips_sim_record_set, state);
14401 }
14402
14403 /* Simulate issuing a NOP in state STATE. */
14404
14405 static void
14406 mips_sim_issue_nop (struct mips_sim *state)
14407 {
14408 if (state->insns_left == 0)
14409 mips_sim_next_cycle (state);
14410 state->insns_left--;
14411 }
14412
14413 /* Update simulation state STATE so that it's ready to accept the instruction
14414 after INSN. INSN should be part of the main rtl chain, not a member of a
14415 SEQUENCE. */
14416
14417 static void
14418 mips_sim_finish_insn (struct mips_sim *state, rtx insn)
14419 {
14420 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
14421 if (JUMP_P (insn))
14422 mips_sim_issue_nop (state);
14423
14424 switch (GET_CODE (SEQ_BEGIN (insn)))
14425 {
14426 case CODE_LABEL:
14427 case CALL_INSN:
14428 /* We can't predict the processor state after a call or label. */
14429 mips_sim_reset (state);
14430 break;
14431
14432 case JUMP_INSN:
14433 /* The delay slots of branch likely instructions are only executed
14434 when the branch is taken. Therefore, if the caller has simulated
14435 the delay slot instruction, STATE does not really reflect the state
14436 of the pipeline for the instruction after the delay slot. Also,
14437 branch likely instructions tend to incur a penalty when not taken,
14438 so there will probably be an extra delay between the branch and
14439 the instruction after the delay slot. */
14440 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
14441 mips_sim_reset (state);
14442 break;
14443
14444 default:
14445 break;
14446 }
14447 }
14448 \f
14449 /* The VR4130 pipeline issues aligned pairs of instructions together,
14450 but it stalls the second instruction if it depends on the first.
14451 In order to cut down the amount of logic required, this dependence
14452 check is not based on a full instruction decode. Instead, any non-SPECIAL
14453 instruction is assumed to modify the register specified by bits 20-16
14454 (which is usually the "rt" field).
14455
14456 In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
14457 input, so we can end up with a false dependence between the branch
14458 and its delay slot. If this situation occurs in instruction INSN,
14459 try to avoid it by swapping rs and rt. */
14460
14461 static void
14462 vr4130_avoid_branch_rt_conflict (rtx insn)
14463 {
14464 rtx first, second;
14465
14466 first = SEQ_BEGIN (insn);
14467 second = SEQ_END (insn);
14468 if (JUMP_P (first)
14469 && NONJUMP_INSN_P (second)
14470 && GET_CODE (PATTERN (first)) == SET
14471 && GET_CODE (SET_DEST (PATTERN (first))) == PC
14472 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
14473 {
14474 /* Check for the right kind of condition. */
14475 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
14476 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
14477 && REG_P (XEXP (cond, 0))
14478 && REG_P (XEXP (cond, 1))
14479 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
14480 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
14481 {
14482 /* SECOND mentions the rt register but not the rs register. */
14483 rtx tmp = XEXP (cond, 0);
14484 XEXP (cond, 0) = XEXP (cond, 1);
14485 XEXP (cond, 1) = tmp;
14486 }
14487 }
14488 }
14489
14490 /* Implement -mvr4130-align. Go through each basic block and simulate the
14491 processor pipeline. If we find that a pair of instructions could execute
14492 in parallel, and the first of those instructions is not 8-byte aligned,
14493 insert a nop to make it aligned. */
14494
14495 static void
14496 vr4130_align_insns (void)
14497 {
14498 struct mips_sim state;
14499 rtx insn, subinsn, last, last2, next;
14500 bool aligned_p;
14501
14502 dfa_start ();
14503
14504 /* LAST is the last instruction before INSN to have a nonzero length.
14505 LAST2 is the last such instruction before LAST. */
14506 last = 0;
14507 last2 = 0;
14508
14509 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
14510 aligned_p = true;
14511
14512 mips_sim_init (&state, alloca (state_size ()));
14513 for (insn = get_insns (); insn != 0; insn = next)
14514 {
14515 unsigned int length;
14516
14517 next = NEXT_INSN (insn);
14518
14519 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
14520 This isn't really related to the alignment pass, but we do it on
14521 the fly to avoid a separate instruction walk. */
14522 vr4130_avoid_branch_rt_conflict (insn);
14523
14524 if (USEFUL_INSN_P (insn))
14525 FOR_EACH_SUBINSN (subinsn, insn)
14526 {
14527 mips_sim_wait_insn (&state, subinsn);
14528
14529 /* If we want this instruction to issue in parallel with the
14530 previous one, make sure that the previous instruction is
14531 aligned. There are several reasons why this isn't worthwhile
14532 when the second instruction is a call:
14533
14534 - Calls are less likely to be performance critical,
14535 - There's a good chance that the delay slot can execute
14536 in parallel with the call.
14537 - The return address would then be unaligned.
14538
14539 In general, if we're going to insert a nop between instructions
14540 X and Y, it's better to insert it immediately after X. That
14541 way, if the nop makes Y aligned, it will also align any labels
14542 between X and Y. */
14543 if (state.insns_left != state.issue_rate
14544 && !CALL_P (subinsn))
14545 {
14546 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
14547 {
14548 /* SUBINSN is the first instruction in INSN and INSN is
14549 aligned. We want to align the previous instruction
14550 instead, so insert a nop between LAST2 and LAST.
14551
14552 Note that LAST could be either a single instruction
14553 or a branch with a delay slot. In the latter case,
14554 LAST, like INSN, is already aligned, but the delay
14555 slot must have some extra delay that stops it from
14556 issuing at the same time as the branch. We therefore
14557 insert a nop before the branch in order to align its
14558 delay slot. */
14559 emit_insn_after (gen_nop (), last2);
14560 aligned_p = false;
14561 }
14562 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
14563 {
14564 /* SUBINSN is the delay slot of INSN, but INSN is
14565 currently unaligned. Insert a nop between
14566 LAST and INSN to align it. */
14567 emit_insn_after (gen_nop (), last);
14568 aligned_p = true;
14569 }
14570 }
14571 mips_sim_issue_insn (&state, subinsn);
14572 }
14573 mips_sim_finish_insn (&state, insn);
14574
14575 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
14576 length = get_attr_length (insn);
14577 if (length > 0)
14578 {
14579 /* If the instruction is an asm statement or multi-instruction
14580 mips.md patern, the length is only an estimate. Insert an
14581 8 byte alignment after it so that the following instructions
14582 can be handled correctly. */
14583 if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
14584 && (recog_memoized (insn) < 0 || length >= 8))
14585 {
14586 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
14587 next = NEXT_INSN (next);
14588 mips_sim_next_cycle (&state);
14589 aligned_p = true;
14590 }
14591 else if (length & 4)
14592 aligned_p = !aligned_p;
14593 last2 = last;
14594 last = insn;
14595 }
14596
14597 /* See whether INSN is an aligned label. */
14598 if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
14599 aligned_p = true;
14600 }
14601 dfa_finish ();
14602 }
14603 \f
14604 /* This structure records that the current function has a LO_SUM
14605 involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
14606 the largest offset applied to BASE by all such LO_SUMs. */
14607 struct mips_lo_sum_offset {
14608 rtx base;
14609 HOST_WIDE_INT offset;
14610 };
14611
14612 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE. */
14613
14614 static hashval_t
14615 mips_hash_base (rtx base)
14616 {
14617 int do_not_record_p;
14618
14619 return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
14620 }
14621
14622 /* Hash-table callbacks for mips_lo_sum_offsets. */
14623
14624 static hashval_t
14625 mips_lo_sum_offset_hash (const void *entry)
14626 {
14627 return mips_hash_base (((const struct mips_lo_sum_offset *) entry)->base);
14628 }
14629
14630 static int
14631 mips_lo_sum_offset_eq (const void *entry, const void *value)
14632 {
14633 return rtx_equal_p (((const struct mips_lo_sum_offset *) entry)->base,
14634 (const_rtx) value);
14635 }
14636
14637 /* Look up symbolic constant X in HTAB, which is a hash table of
14638 mips_lo_sum_offsets. If OPTION is NO_INSERT, return true if X can be
14639 paired with a recorded LO_SUM, otherwise record X in the table. */
14640
14641 static bool
14642 mips_lo_sum_offset_lookup (htab_t htab, rtx x, enum insert_option option)
14643 {
14644 rtx base, offset;
14645 void **slot;
14646 struct mips_lo_sum_offset *entry;
14647
14648 /* Split X into a base and offset. */
14649 split_const (x, &base, &offset);
14650 if (UNSPEC_ADDRESS_P (base))
14651 base = UNSPEC_ADDRESS (base);
14652
14653 /* Look up the base in the hash table. */
14654 slot = htab_find_slot_with_hash (htab, base, mips_hash_base (base), option);
14655 if (slot == NULL)
14656 return false;
14657
14658 entry = (struct mips_lo_sum_offset *) *slot;
14659 if (option == INSERT)
14660 {
14661 if (entry == NULL)
14662 {
14663 entry = XNEW (struct mips_lo_sum_offset);
14664 entry->base = base;
14665 entry->offset = INTVAL (offset);
14666 *slot = entry;
14667 }
14668 else
14669 {
14670 if (INTVAL (offset) > entry->offset)
14671 entry->offset = INTVAL (offset);
14672 }
14673 }
14674 return INTVAL (offset) <= entry->offset;
14675 }
14676
14677 /* A for_each_rtx callback for which DATA is a mips_lo_sum_offset hash table.
14678 Record every LO_SUM in *LOC. */
14679
14680 static int
14681 mips_record_lo_sum (rtx *loc, void *data)
14682 {
14683 if (GET_CODE (*loc) == LO_SUM)
14684 mips_lo_sum_offset_lookup ((htab_t) data, XEXP (*loc, 1), INSERT);
14685 return 0;
14686 }
14687
14688 /* Return true if INSN is a SET of an orphaned high-part relocation.
14689 HTAB is a hash table of mips_lo_sum_offsets that describes all the
14690 LO_SUMs in the current function. */
14691
14692 static bool
14693 mips_orphaned_high_part_p (htab_t htab, rtx insn)
14694 {
14695 enum mips_symbol_type type;
14696 rtx x, set;
14697
14698 set = single_set (insn);
14699 if (set)
14700 {
14701 /* Check for %his. */
14702 x = SET_SRC (set);
14703 if (GET_CODE (x) == HIGH
14704 && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
14705 return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
14706
14707 /* Check for local %gots (and %got_pages, which is redundant but OK). */
14708 if (GET_CODE (x) == UNSPEC
14709 && XINT (x, 1) == UNSPEC_LOAD_GOT
14710 && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
14711 SYMBOL_CONTEXT_LEA, &type)
14712 && type == SYMBOL_GOTOFF_PAGE)
14713 return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
14714 }
14715 return false;
14716 }
14717
14718 /* Subroutine of mips_reorg_process_insns. If there is a hazard between
14719 INSN and a previous instruction, avoid it by inserting nops after
14720 instruction AFTER.
14721
14722 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
14723 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
14724 before using the value of that register. *HILO_DELAY counts the
14725 number of instructions since the last hilo hazard (that is,
14726 the number of instructions since the last MFLO or MFHI).
14727
14728 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
14729 for the next instruction.
14730
14731 LO_REG is an rtx for the LO register, used in dependence checking. */
14732
14733 static void
14734 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
14735 rtx *delayed_reg, rtx lo_reg)
14736 {
14737 rtx pattern, set;
14738 int nops, ninsns;
14739
14740 pattern = PATTERN (insn);
14741
14742 /* Do not put the whole function in .set noreorder if it contains
14743 an asm statement. We don't know whether there will be hazards
14744 between the asm statement and the gcc-generated code. */
14745 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
14746 cfun->machine->all_noreorder_p = false;
14747
14748 /* Ignore zero-length instructions (barriers and the like). */
14749 ninsns = get_attr_length (insn) / 4;
14750 if (ninsns == 0)
14751 return;
14752
14753 /* Work out how many nops are needed. Note that we only care about
14754 registers that are explicitly mentioned in the instruction's pattern.
14755 It doesn't matter that calls use the argument registers or that they
14756 clobber hi and lo. */
14757 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
14758 nops = 2 - *hilo_delay;
14759 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
14760 nops = 1;
14761 else
14762 nops = 0;
14763
14764 /* Insert the nops between this instruction and the previous one.
14765 Each new nop takes us further from the last hilo hazard. */
14766 *hilo_delay += nops;
14767 while (nops-- > 0)
14768 emit_insn_after (gen_hazard_nop (), after);
14769
14770 /* Set up the state for the next instruction. */
14771 *hilo_delay += ninsns;
14772 *delayed_reg = 0;
14773 if (INSN_CODE (insn) >= 0)
14774 switch (get_attr_hazard (insn))
14775 {
14776 case HAZARD_NONE:
14777 break;
14778
14779 case HAZARD_HILO:
14780 *hilo_delay = 0;
14781 break;
14782
14783 case HAZARD_DELAY:
14784 set = single_set (insn);
14785 gcc_assert (set);
14786 *delayed_reg = SET_DEST (set);
14787 break;
14788 }
14789 }
14790
14791 /* Go through the instruction stream and insert nops where necessary.
14792 Also delete any high-part relocations whose partnering low parts
14793 are now all dead. See if the whole function can then be put into
14794 .set noreorder and .set nomacro. */
14795
14796 static void
14797 mips_reorg_process_insns (void)
14798 {
14799 rtx insn, last_insn, subinsn, next_insn, lo_reg, delayed_reg;
14800 int hilo_delay;
14801 htab_t htab;
14802
14803 /* Force all instructions to be split into their final form. */
14804 split_all_insns_noflow ();
14805
14806 /* Recalculate instruction lengths without taking nops into account. */
14807 cfun->machine->ignore_hazard_length_p = true;
14808 shorten_branches (get_insns ());
14809
14810 cfun->machine->all_noreorder_p = true;
14811
14812 /* We don't track MIPS16 PC-relative offsets closely enough to make
14813 a good job of "set .noreorder" code in MIPS16 mode. */
14814 if (TARGET_MIPS16)
14815 cfun->machine->all_noreorder_p = false;
14816
14817 /* Code that doesn't use explicit relocs can't be ".set nomacro". */
14818 if (!TARGET_EXPLICIT_RELOCS)
14819 cfun->machine->all_noreorder_p = false;
14820
14821 /* Profiled functions can't be all noreorder because the profiler
14822 support uses assembler macros. */
14823 if (crtl->profile)
14824 cfun->machine->all_noreorder_p = false;
14825
14826 /* Code compiled with -mfix-vr4120 can't be all noreorder because
14827 we rely on the assembler to work around some errata. */
14828 if (TARGET_FIX_VR4120)
14829 cfun->machine->all_noreorder_p = false;
14830
14831 /* The same is true for -mfix-vr4130 if we might generate MFLO or
14832 MFHI instructions. Note that we avoid using MFLO and MFHI if
14833 the VR4130 MACC and DMACC instructions are available instead;
14834 see the *mfhilo_{si,di}_macc patterns. */
14835 if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
14836 cfun->machine->all_noreorder_p = false;
14837
14838 htab = htab_create (37, mips_lo_sum_offset_hash,
14839 mips_lo_sum_offset_eq, free);
14840
14841 /* Make a first pass over the instructions, recording all the LO_SUMs. */
14842 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
14843 FOR_EACH_SUBINSN (subinsn, insn)
14844 if (USEFUL_INSN_P (subinsn))
14845 for_each_rtx (&PATTERN (subinsn), mips_record_lo_sum, htab);
14846
14847 last_insn = 0;
14848 hilo_delay = 2;
14849 delayed_reg = 0;
14850 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
14851
14852 /* Make a second pass over the instructions. Delete orphaned
14853 high-part relocations or turn them into NOPs. Avoid hazards
14854 by inserting NOPs. */
14855 for (insn = get_insns (); insn != 0; insn = next_insn)
14856 {
14857 next_insn = NEXT_INSN (insn);
14858 if (USEFUL_INSN_P (insn))
14859 {
14860 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
14861 {
14862 /* If we find an orphaned high-part relocation in a delay
14863 slot, it's easier to turn that instruction into a NOP than
14864 to delete it. The delay slot will be a NOP either way. */
14865 FOR_EACH_SUBINSN (subinsn, insn)
14866 if (INSN_P (subinsn))
14867 {
14868 if (mips_orphaned_high_part_p (htab, subinsn))
14869 {
14870 PATTERN (subinsn) = gen_nop ();
14871 INSN_CODE (subinsn) = CODE_FOR_nop;
14872 }
14873 mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
14874 &delayed_reg, lo_reg);
14875 }
14876 last_insn = insn;
14877 }
14878 else
14879 {
14880 /* INSN is a single instruction. Delete it if it's an
14881 orphaned high-part relocation. */
14882 if (mips_orphaned_high_part_p (htab, insn))
14883 delete_insn (insn);
14884 /* Also delete cache barriers if the last instruction
14885 was an annulled branch. INSN will not be speculatively
14886 executed. */
14887 else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
14888 && last_insn
14889 && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
14890 delete_insn (insn);
14891 else
14892 {
14893 mips_avoid_hazard (last_insn, insn, &hilo_delay,
14894 &delayed_reg, lo_reg);
14895 last_insn = insn;
14896 }
14897 }
14898 }
14899 }
14900
14901 htab_delete (htab);
14902 }
14903
14904 /* If we are using a GOT, but have not decided to use a global pointer yet,
14905 see whether we need one to implement long branches. Convert the ghost
14906 global-pointer instructions into real ones if so. */
14907
14908 static bool
14909 mips_expand_ghost_gp_insns (void)
14910 {
14911 rtx insn;
14912 int normal_length;
14913
14914 /* Quick exit if we already know that we will or won't need a
14915 global pointer. */
14916 if (!TARGET_USE_GOT
14917 || cfun->machine->global_pointer == INVALID_REGNUM
14918 || mips_must_initialize_gp_p ())
14919 return false;
14920
14921 shorten_branches (get_insns ());
14922
14923 /* Look for a branch that is longer than normal. The normal length for
14924 non-MIPS16 branches is 8, because the length includes the delay slot.
14925 It is 4 for MIPS16, because MIPS16 branches are extended instructions,
14926 but they have no delay slot. */
14927 normal_length = (TARGET_MIPS16 ? 4 : 8);
14928 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14929 if (JUMP_P (insn)
14930 && USEFUL_INSN_P (insn)
14931 && get_attr_length (insn) > normal_length)
14932 break;
14933
14934 if (insn == NULL_RTX)
14935 return false;
14936
14937 /* We've now established that we need $gp. */
14938 cfun->machine->must_initialize_gp_p = true;
14939 split_all_insns_noflow ();
14940
14941 return true;
14942 }
14943
14944 /* Subroutine of mips_reorg to manage passes that require DF. */
14945
14946 static void
14947 mips_df_reorg (void)
14948 {
14949 /* Create def-use chains. */
14950 df_set_flags (DF_EQ_NOTES);
14951 df_chain_add_problem (DF_UD_CHAIN);
14952 df_analyze ();
14953
14954 if (TARGET_RELAX_PIC_CALLS)
14955 mips_annotate_pic_calls ();
14956
14957 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
14958 r10k_insert_cache_barriers ();
14959
14960 df_finish_pass (false);
14961 }
14962
14963 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
14964
14965 static void
14966 mips_reorg (void)
14967 {
14968 /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF. Also during
14969 insn splitting in mips16_lay_out_constants, DF insn info is only kept up
14970 to date if the CFG is available. */
14971 if (mips_cfg_in_reorg ())
14972 compute_bb_for_insn ();
14973 mips16_lay_out_constants ();
14974 if (mips_cfg_in_reorg ())
14975 {
14976 mips_df_reorg ();
14977 free_bb_for_insn ();
14978 }
14979
14980 if (optimize > 0 && flag_delayed_branch)
14981 dbr_schedule (get_insns ());
14982 mips_reorg_process_insns ();
14983 if (!TARGET_MIPS16
14984 && TARGET_EXPLICIT_RELOCS
14985 && TUNE_MIPS4130
14986 && TARGET_VR4130_ALIGN)
14987 vr4130_align_insns ();
14988 if (mips_expand_ghost_gp_insns ())
14989 /* The expansion could invalidate some of the VR4130 alignment
14990 optimizations, but this should be an extremely rare case anyhow. */
14991 mips_reorg_process_insns ();
14992 }
14993 \f
14994 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
14995 in order to avoid duplicating too much logic from elsewhere. */
14996
14997 static void
14998 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
14999 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
15000 tree function)
15001 {
15002 rtx this_rtx, temp1, temp2, insn, fnaddr;
15003 bool use_sibcall_p;
15004
15005 /* Pretend to be a post-reload pass while generating rtl. */
15006 reload_completed = 1;
15007
15008 /* Mark the end of the (empty) prologue. */
15009 emit_note (NOTE_INSN_PROLOGUE_END);
15010
15011 /* Determine if we can use a sibcall to call FUNCTION directly. */
15012 fnaddr = XEXP (DECL_RTL (function), 0);
15013 use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
15014 && const_call_insn_operand (fnaddr, Pmode));
15015
15016 /* Determine if we need to load FNADDR from the GOT. */
15017 if (!use_sibcall_p
15018 && (mips_got_symbol_type_p
15019 (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
15020 {
15021 /* Pick a global pointer. Use a call-clobbered register if
15022 TARGET_CALL_SAVED_GP. */
15023 cfun->machine->global_pointer
15024 = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
15025 cfun->machine->must_initialize_gp_p = true;
15026 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
15027
15028 /* Set up the global pointer for n32 or n64 abicalls. */
15029 mips_emit_loadgp ();
15030 }
15031
15032 /* We need two temporary registers in some cases. */
15033 temp1 = gen_rtx_REG (Pmode, 2);
15034 temp2 = gen_rtx_REG (Pmode, 3);
15035
15036 /* Find out which register contains the "this" pointer. */
15037 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
15038 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
15039 else
15040 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
15041
15042 /* Add DELTA to THIS_RTX. */
15043 if (delta != 0)
15044 {
15045 rtx offset = GEN_INT (delta);
15046 if (!SMALL_OPERAND (delta))
15047 {
15048 mips_emit_move (temp1, offset);
15049 offset = temp1;
15050 }
15051 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
15052 }
15053
15054 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
15055 if (vcall_offset != 0)
15056 {
15057 rtx addr;
15058
15059 /* Set TEMP1 to *THIS_RTX. */
15060 mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
15061
15062 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
15063 addr = mips_add_offset (temp2, temp1, vcall_offset);
15064
15065 /* Load the offset and add it to THIS_RTX. */
15066 mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
15067 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
15068 }
15069
15070 /* Jump to the target function. Use a sibcall if direct jumps are
15071 allowed, otherwise load the address into a register first. */
15072 if (use_sibcall_p)
15073 {
15074 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
15075 SIBLING_CALL_P (insn) = 1;
15076 }
15077 else
15078 {
15079 /* This is messy. GAS treats "la $25,foo" as part of a call
15080 sequence and may allow a global "foo" to be lazily bound.
15081 The general move patterns therefore reject this combination.
15082
15083 In this context, lazy binding would actually be OK
15084 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
15085 TARGET_CALL_SAVED_GP; see mips_load_call_address.
15086 We must therefore load the address via a temporary
15087 register if mips_dangerous_for_la25_p.
15088
15089 If we jump to the temporary register rather than $25,
15090 the assembler can use the move insn to fill the jump's
15091 delay slot.
15092
15093 We can use the same technique for MIPS16 code, where $25
15094 is not a valid JR register. */
15095 if (TARGET_USE_PIC_FN_ADDR_REG
15096 && !TARGET_MIPS16
15097 && !mips_dangerous_for_la25_p (fnaddr))
15098 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
15099 mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
15100
15101 if (TARGET_USE_PIC_FN_ADDR_REG
15102 && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
15103 mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
15104 emit_jump_insn (gen_indirect_jump (temp1));
15105 }
15106
15107 /* Run just enough of rest_of_compilation. This sequence was
15108 "borrowed" from alpha.c. */
15109 insn = get_insns ();
15110 insn_locators_alloc ();
15111 split_all_insns_noflow ();
15112 mips16_lay_out_constants ();
15113 shorten_branches (insn);
15114 final_start_function (insn, file, 1);
15115 final (insn, file, 1);
15116 final_end_function ();
15117
15118 /* Clean up the vars set above. Note that final_end_function resets
15119 the global pointer for us. */
15120 reload_completed = 0;
15121 }
15122 \f
15123 /* The last argument passed to mips_set_mips16_mode, or negative if the
15124 function hasn't been called yet.
15125
15126 There are two copies of this information. One is saved and restored
15127 by the PCH process while the other is specific to this compiler
15128 invocation. The information calculated by mips_set_mips16_mode
15129 is invalid unless the two variables are the same. */
15130 static int was_mips16_p = -1;
15131 static GTY(()) int was_mips16_pch_p = -1;
15132
15133 /* Set up the target-dependent global state so that it matches the
15134 current function's ISA mode. */
15135
15136 static void
15137 mips_set_mips16_mode (int mips16_p)
15138 {
15139 if (mips16_p == was_mips16_p
15140 && mips16_p == was_mips16_pch_p)
15141 return;
15142
15143 /* Restore base settings of various flags. */
15144 target_flags = mips_base_target_flags;
15145 flag_schedule_insns = mips_base_schedule_insns;
15146 flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
15147 flag_move_loop_invariants = mips_base_move_loop_invariants;
15148 align_loops = mips_base_align_loops;
15149 align_jumps = mips_base_align_jumps;
15150 align_functions = mips_base_align_functions;
15151
15152 if (mips16_p)
15153 {
15154 /* Switch to MIPS16 mode. */
15155 target_flags |= MASK_MIPS16;
15156
15157 /* Don't run the scheduler before reload, since it tends to
15158 increase register pressure. */
15159 flag_schedule_insns = 0;
15160
15161 /* Don't do hot/cold partitioning. mips16_lay_out_constants expects
15162 the whole function to be in a single section. */
15163 flag_reorder_blocks_and_partition = 0;
15164
15165 /* Don't move loop invariants, because it tends to increase
15166 register pressure. It also introduces an extra move in cases
15167 where the constant is the first operand in a two-operand binary
15168 instruction, or when it forms a register argument to a functon
15169 call. */
15170 flag_move_loop_invariants = 0;
15171
15172 target_flags |= MASK_EXPLICIT_RELOCS;
15173
15174 /* Experiments suggest we get the best overall section-anchor
15175 results from using the range of an unextended LW or SW. Code
15176 that makes heavy use of byte or short accesses can do better
15177 with ranges of 0...31 and 0...63 respectively, but most code is
15178 sensitive to the range of LW and SW instead. */
15179 targetm.min_anchor_offset = 0;
15180 targetm.max_anchor_offset = 127;
15181
15182 targetm.const_anchor = 0;
15183
15184 /* MIPS16 has no BAL instruction. */
15185 target_flags &= ~MASK_RELAX_PIC_CALLS;
15186
15187 if (flag_pic && !TARGET_OLDABI)
15188 sorry ("MIPS16 PIC for ABIs other than o32 and o64");
15189
15190 if (TARGET_XGOT)
15191 sorry ("MIPS16 -mxgot code");
15192
15193 if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
15194 sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
15195 }
15196 else
15197 {
15198 /* Switch to normal (non-MIPS16) mode. */
15199 target_flags &= ~MASK_MIPS16;
15200
15201 /* Provide default values for align_* for 64-bit targets. */
15202 if (TARGET_64BIT)
15203 {
15204 if (align_loops == 0)
15205 align_loops = 8;
15206 if (align_jumps == 0)
15207 align_jumps = 8;
15208 if (align_functions == 0)
15209 align_functions = 8;
15210 }
15211
15212 targetm.min_anchor_offset = -32768;
15213 targetm.max_anchor_offset = 32767;
15214
15215 targetm.const_anchor = 0x8000;
15216 }
15217
15218 /* (Re)initialize MIPS target internals for new ISA. */
15219 mips_init_relocs ();
15220
15221 if (mips16_p)
15222 {
15223 if (!mips16_globals)
15224 mips16_globals = save_target_globals ();
15225 else
15226 restore_target_globals (mips16_globals);
15227 }
15228 else
15229 restore_target_globals (&default_target_globals);
15230
15231 was_mips16_p = mips16_p;
15232 was_mips16_pch_p = mips16_p;
15233 }
15234
15235 /* Implement TARGET_SET_CURRENT_FUNCTION. Decide whether the current
15236 function should use the MIPS16 ISA and switch modes accordingly. */
15237
15238 static void
15239 mips_set_current_function (tree fndecl)
15240 {
15241 mips_set_mips16_mode (mips_use_mips16_mode_p (fndecl));
15242 }
15243 \f
15244 /* Allocate a chunk of memory for per-function machine-dependent data. */
15245
15246 static struct machine_function *
15247 mips_init_machine_status (void)
15248 {
15249 return ggc_alloc_cleared_machine_function ();
15250 }
15251
15252 /* Return the processor associated with the given ISA level, or null
15253 if the ISA isn't valid. */
15254
15255 static const struct mips_cpu_info *
15256 mips_cpu_info_from_isa (int isa)
15257 {
15258 unsigned int i;
15259
15260 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
15261 if (mips_cpu_info_table[i].isa == isa)
15262 return mips_cpu_info_table + i;
15263
15264 return NULL;
15265 }
15266
15267 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
15268 with a final "000" replaced by "k". Ignore case.
15269
15270 Note: this function is shared between GCC and GAS. */
15271
15272 static bool
15273 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
15274 {
15275 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
15276 given++, canonical++;
15277
15278 return ((*given == 0 && *canonical == 0)
15279 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
15280 }
15281
15282 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
15283 CPU name. We've traditionally allowed a lot of variation here.
15284
15285 Note: this function is shared between GCC and GAS. */
15286
15287 static bool
15288 mips_matching_cpu_name_p (const char *canonical, const char *given)
15289 {
15290 /* First see if the name matches exactly, or with a final "000"
15291 turned into "k". */
15292 if (mips_strict_matching_cpu_name_p (canonical, given))
15293 return true;
15294
15295 /* If not, try comparing based on numerical designation alone.
15296 See if GIVEN is an unadorned number, or 'r' followed by a number. */
15297 if (TOLOWER (*given) == 'r')
15298 given++;
15299 if (!ISDIGIT (*given))
15300 return false;
15301
15302 /* Skip over some well-known prefixes in the canonical name,
15303 hoping to find a number there too. */
15304 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
15305 canonical += 2;
15306 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
15307 canonical += 2;
15308 else if (TOLOWER (canonical[0]) == 'r')
15309 canonical += 1;
15310
15311 return mips_strict_matching_cpu_name_p (canonical, given);
15312 }
15313
15314 /* Return the mips_cpu_info entry for the processor or ISA given
15315 by CPU_STRING. Return null if the string isn't recognized.
15316
15317 A similar function exists in GAS. */
15318
15319 static const struct mips_cpu_info *
15320 mips_parse_cpu (const char *cpu_string)
15321 {
15322 unsigned int i;
15323 const char *s;
15324
15325 /* In the past, we allowed upper-case CPU names, but it doesn't
15326 work well with the multilib machinery. */
15327 for (s = cpu_string; *s != 0; s++)
15328 if (ISUPPER (*s))
15329 {
15330 warning (0, "CPU names must be lower case");
15331 break;
15332 }
15333
15334 /* 'from-abi' selects the most compatible architecture for the given
15335 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
15336 EABIs, we have to decide whether we're using the 32-bit or 64-bit
15337 version. */
15338 if (strcasecmp (cpu_string, "from-abi") == 0)
15339 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
15340 : ABI_NEEDS_64BIT_REGS ? 3
15341 : (TARGET_64BIT ? 3 : 1));
15342
15343 /* 'default' has traditionally been a no-op. Probably not very useful. */
15344 if (strcasecmp (cpu_string, "default") == 0)
15345 return NULL;
15346
15347 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
15348 if (mips_matching_cpu_name_p (mips_cpu_info_table[i].name, cpu_string))
15349 return mips_cpu_info_table + i;
15350
15351 return NULL;
15352 }
15353
15354 /* Set up globals to generate code for the ISA or processor
15355 described by INFO. */
15356
15357 static void
15358 mips_set_architecture (const struct mips_cpu_info *info)
15359 {
15360 if (info != 0)
15361 {
15362 mips_arch_info = info;
15363 mips_arch = info->cpu;
15364 mips_isa = info->isa;
15365 }
15366 }
15367
15368 /* Likewise for tuning. */
15369
15370 static void
15371 mips_set_tune (const struct mips_cpu_info *info)
15372 {
15373 if (info != 0)
15374 {
15375 mips_tune_info = info;
15376 mips_tune = info->cpu;
15377 }
15378 }
15379
15380 /* Implement TARGET_HANDLE_OPTION. */
15381
15382 static bool
15383 mips_handle_option (size_t code, const char *arg, int value)
15384 {
15385 switch (code)
15386 {
15387 case OPT_G:
15388 g_switch_value = value;
15389 g_switch_set = true;
15390 return true;
15391
15392 case OPT_mabi_:
15393 if (strcmp (arg, "32") == 0)
15394 mips_abi = ABI_32;
15395 else if (strcmp (arg, "o64") == 0)
15396 mips_abi = ABI_O64;
15397 else if (strcmp (arg, "n32") == 0)
15398 mips_abi = ABI_N32;
15399 else if (strcmp (arg, "64") == 0)
15400 mips_abi = ABI_64;
15401 else if (strcmp (arg, "eabi") == 0)
15402 mips_abi = ABI_EABI;
15403 else
15404 return false;
15405 return true;
15406
15407 case OPT_march_:
15408 case OPT_mtune_:
15409 return mips_parse_cpu (arg) != 0;
15410
15411 case OPT_mips:
15412 mips_isa_option_info = mips_parse_cpu (ACONCAT (("mips", arg, NULL)));
15413 return mips_isa_option_info != 0;
15414
15415 case OPT_mno_flush_func:
15416 mips_cache_flush_func = NULL;
15417 return true;
15418
15419 case OPT_mcode_readable_:
15420 if (strcmp (arg, "yes") == 0)
15421 mips_code_readable = CODE_READABLE_YES;
15422 else if (strcmp (arg, "pcrel") == 0)
15423 mips_code_readable = CODE_READABLE_PCREL;
15424 else if (strcmp (arg, "no") == 0)
15425 mips_code_readable = CODE_READABLE_NO;
15426 else
15427 return false;
15428 return true;
15429
15430 case OPT_mr10k_cache_barrier_:
15431 if (strcmp (arg, "load-store") == 0)
15432 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_LOAD_STORE;
15433 else if (strcmp (arg, "store") == 0)
15434 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_STORE;
15435 else if (strcmp (arg, "none") == 0)
15436 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
15437 else
15438 return false;
15439 return true;
15440
15441 default:
15442 return true;
15443 }
15444 }
15445
15446 /* Implement OVERRIDE_OPTIONS. */
15447
15448 void
15449 mips_override_options (void)
15450 {
15451 int i, start, regno, mode;
15452
15453 /* Process flags as though we were generating non-MIPS16 code. */
15454 mips_base_mips16 = TARGET_MIPS16;
15455 target_flags &= ~MASK_MIPS16;
15456
15457 #ifdef SUBTARGET_OVERRIDE_OPTIONS
15458 SUBTARGET_OVERRIDE_OPTIONS;
15459 #endif
15460
15461 /* -mno-float overrides -mhard-float and -msoft-float. */
15462 if (TARGET_NO_FLOAT)
15463 {
15464 target_flags |= MASK_SOFT_FLOAT_ABI;
15465 target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
15466 }
15467
15468 if (TARGET_FLIP_MIPS16)
15469 TARGET_INTERLINK_MIPS16 = 1;
15470
15471 /* Set the small data limit. */
15472 mips_small_data_threshold = (g_switch_set
15473 ? g_switch_value
15474 : MIPS_DEFAULT_GVALUE);
15475
15476 /* The following code determines the architecture and register size.
15477 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
15478 The GAS and GCC code should be kept in sync as much as possible. */
15479
15480 if (mips_arch_string != 0)
15481 mips_set_architecture (mips_parse_cpu (mips_arch_string));
15482
15483 if (mips_isa_option_info != 0)
15484 {
15485 if (mips_arch_info == 0)
15486 mips_set_architecture (mips_isa_option_info);
15487 else if (mips_arch_info->isa != mips_isa_option_info->isa)
15488 error ("%<-%s%> conflicts with the other architecture options, "
15489 "which specify a %s processor",
15490 mips_isa_option_info->name,
15491 mips_cpu_info_from_isa (mips_arch_info->isa)->name);
15492 }
15493
15494 if (mips_arch_info == 0)
15495 {
15496 #ifdef MIPS_CPU_STRING_DEFAULT
15497 mips_set_architecture (mips_parse_cpu (MIPS_CPU_STRING_DEFAULT));
15498 #else
15499 mips_set_architecture (mips_cpu_info_from_isa (MIPS_ISA_DEFAULT));
15500 #endif
15501 }
15502
15503 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
15504 error ("%<-march=%s%> is not compatible with the selected ABI",
15505 mips_arch_info->name);
15506
15507 /* Optimize for mips_arch, unless -mtune selects a different processor. */
15508 if (mips_tune_string != 0)
15509 mips_set_tune (mips_parse_cpu (mips_tune_string));
15510
15511 if (mips_tune_info == 0)
15512 mips_set_tune (mips_arch_info);
15513
15514 if ((target_flags_explicit & MASK_64BIT) != 0)
15515 {
15516 /* The user specified the size of the integer registers. Make sure
15517 it agrees with the ABI and ISA. */
15518 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
15519 error ("%<-mgp64%> used with a 32-bit processor");
15520 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
15521 error ("%<-mgp32%> used with a 64-bit ABI");
15522 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
15523 error ("%<-mgp64%> used with a 32-bit ABI");
15524 }
15525 else
15526 {
15527 /* Infer the integer register size from the ABI and processor.
15528 Restrict ourselves to 32-bit registers if that's all the
15529 processor has, or if the ABI cannot handle 64-bit registers. */
15530 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
15531 target_flags &= ~MASK_64BIT;
15532 else
15533 target_flags |= MASK_64BIT;
15534 }
15535
15536 if ((target_flags_explicit & MASK_FLOAT64) != 0)
15537 {
15538 if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
15539 error ("unsupported combination: %s", "-mfp64 -msingle-float");
15540 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
15541 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
15542 else if (!TARGET_64BIT && TARGET_FLOAT64)
15543 {
15544 if (!ISA_HAS_MXHC1)
15545 error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
15546 " the target supports the mfhc1 and mthc1 instructions");
15547 else if (mips_abi != ABI_32)
15548 error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
15549 " the o32 ABI");
15550 }
15551 }
15552 else
15553 {
15554 /* -msingle-float selects 32-bit float registers. Otherwise the
15555 float registers should be the same size as the integer ones. */
15556 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
15557 target_flags |= MASK_FLOAT64;
15558 else
15559 target_flags &= ~MASK_FLOAT64;
15560 }
15561
15562 /* End of code shared with GAS. */
15563
15564 /* If no -mlong* option was given, infer it from the other options. */
15565 if ((target_flags_explicit & MASK_LONG64) == 0)
15566 {
15567 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
15568 target_flags |= MASK_LONG64;
15569 else
15570 target_flags &= ~MASK_LONG64;
15571 }
15572
15573 if (!TARGET_OLDABI)
15574 flag_pcc_struct_return = 0;
15575
15576 /* Decide which rtx_costs structure to use. */
15577 if (optimize_size)
15578 mips_cost = &mips_rtx_cost_optimize_size;
15579 else
15580 mips_cost = &mips_rtx_cost_data[mips_tune];
15581
15582 /* If the user hasn't specified a branch cost, use the processor's
15583 default. */
15584 if (mips_branch_cost == 0)
15585 mips_branch_cost = mips_cost->branch_cost;
15586
15587 /* If neither -mbranch-likely nor -mno-branch-likely was given
15588 on the command line, set MASK_BRANCHLIKELY based on the target
15589 architecture and tuning flags. Annulled delay slots are a
15590 size win, so we only consider the processor-specific tuning
15591 for !optimize_size. */
15592 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
15593 {
15594 if (ISA_HAS_BRANCHLIKELY
15595 && (optimize_size
15596 || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
15597 target_flags |= MASK_BRANCHLIKELY;
15598 else
15599 target_flags &= ~MASK_BRANCHLIKELY;
15600 }
15601 else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
15602 warning (0, "the %qs architecture does not support branch-likely"
15603 " instructions", mips_arch_info->name);
15604
15605 /* The effect of -mabicalls isn't defined for the EABI. */
15606 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
15607 {
15608 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
15609 target_flags &= ~MASK_ABICALLS;
15610 }
15611
15612 if (TARGET_ABICALLS_PIC2)
15613 /* We need to set flag_pic for executables as well as DSOs
15614 because we may reference symbols that are not defined in
15615 the final executable. (MIPS does not use things like
15616 copy relocs, for example.)
15617
15618 There is a body of code that uses __PIC__ to distinguish
15619 between -mabicalls and -mno-abicalls code. The non-__PIC__
15620 variant is usually appropriate for TARGET_ABICALLS_PIC0, as
15621 long as any indirect jumps use $25. */
15622 flag_pic = 1;
15623
15624 /* -mvr4130-align is a "speed over size" optimization: it usually produces
15625 faster code, but at the expense of more nops. Enable it at -O3 and
15626 above. */
15627 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
15628 target_flags |= MASK_VR4130_ALIGN;
15629
15630 /* Prefer a call to memcpy over inline code when optimizing for size,
15631 though see MOVE_RATIO in mips.h. */
15632 if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
15633 target_flags |= MASK_MEMCPY;
15634
15635 /* If we have a nonzero small-data limit, check that the -mgpopt
15636 setting is consistent with the other target flags. */
15637 if (mips_small_data_threshold > 0)
15638 {
15639 if (!TARGET_GPOPT)
15640 {
15641 if (!TARGET_EXPLICIT_RELOCS)
15642 error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
15643
15644 TARGET_LOCAL_SDATA = false;
15645 TARGET_EXTERN_SDATA = false;
15646 }
15647 else
15648 {
15649 if (TARGET_VXWORKS_RTP)
15650 warning (0, "cannot use small-data accesses for %qs", "-mrtp");
15651
15652 if (TARGET_ABICALLS)
15653 warning (0, "cannot use small-data accesses for %qs",
15654 "-mabicalls");
15655 }
15656 }
15657
15658 #ifdef MIPS_TFMODE_FORMAT
15659 REAL_MODE_FORMAT (TFmode) = &MIPS_TFMODE_FORMAT;
15660 #endif
15661
15662 /* Make sure that the user didn't turn off paired single support when
15663 MIPS-3D support is requested. */
15664 if (TARGET_MIPS3D
15665 && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
15666 && !TARGET_PAIRED_SINGLE_FLOAT)
15667 error ("%<-mips3d%> requires %<-mpaired-single%>");
15668
15669 /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT. */
15670 if (TARGET_MIPS3D)
15671 target_flags |= MASK_PAIRED_SINGLE_FLOAT;
15672
15673 /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
15674 and TARGET_HARD_FLOAT_ABI are both true. */
15675 if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
15676 error ("%qs must be used with %qs",
15677 TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
15678 TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
15679
15680 /* Make sure that the ISA supports TARGET_PAIRED_SINGLE_FLOAT when it is
15681 enabled. */
15682 if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
15683 warning (0, "the %qs architecture does not support paired-single"
15684 " instructions", mips_arch_info->name);
15685
15686 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
15687 && !TARGET_CACHE_BUILTIN)
15688 {
15689 error ("%qs requires a target that provides the %qs instruction",
15690 "-mr10k-cache-barrier", "cache");
15691 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
15692 }
15693
15694 /* If TARGET_DSPR2, enable MASK_DSP. */
15695 if (TARGET_DSPR2)
15696 target_flags |= MASK_DSP;
15697
15698 /* .eh_frame addresses should be the same width as a C pointer.
15699 Most MIPS ABIs support only one pointer size, so the assembler
15700 will usually know exactly how big an .eh_frame address is.
15701
15702 Unfortunately, this is not true of the 64-bit EABI. The ABI was
15703 originally defined to use 64-bit pointers (i.e. it is LP64), and
15704 this is still the default mode. However, we also support an n32-like
15705 ILP32 mode, which is selected by -mlong32. The problem is that the
15706 assembler has traditionally not had an -mlong option, so it has
15707 traditionally not known whether we're using the ILP32 or LP64 form.
15708
15709 As it happens, gas versions up to and including 2.19 use _32-bit_
15710 addresses for EABI64 .cfi_* directives. This is wrong for the
15711 default LP64 mode, so we can't use the directives by default.
15712 Moreover, since gas's current behavior is at odds with gcc's
15713 default behavior, it seems unwise to rely on future versions
15714 of gas behaving the same way. We therefore avoid using .cfi
15715 directives for -mlong32 as well. */
15716 if (mips_abi == ABI_EABI && TARGET_64BIT)
15717 flag_dwarf2_cfi_asm = 0;
15718
15719 /* .cfi_* directives generate a read-only section, so fall back on
15720 manual .eh_frame creation if we need the section to be writable. */
15721 if (TARGET_WRITABLE_EH_FRAME)
15722 flag_dwarf2_cfi_asm = 0;
15723
15724 mips_init_print_operand_punct ();
15725
15726 /* Set up array to map GCC register number to debug register number.
15727 Ignore the special purpose register numbers. */
15728
15729 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
15730 {
15731 mips_dbx_regno[i] = INVALID_REGNUM;
15732 if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
15733 mips_dwarf_regno[i] = i;
15734 else
15735 mips_dwarf_regno[i] = INVALID_REGNUM;
15736 }
15737
15738 start = GP_DBX_FIRST - GP_REG_FIRST;
15739 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
15740 mips_dbx_regno[i] = i + start;
15741
15742 start = FP_DBX_FIRST - FP_REG_FIRST;
15743 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
15744 mips_dbx_regno[i] = i + start;
15745
15746 /* Accumulator debug registers use big-endian ordering. */
15747 mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
15748 mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
15749 mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
15750 mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
15751 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
15752 {
15753 mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
15754 mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
15755 }
15756
15757 /* Set up mips_hard_regno_mode_ok. */
15758 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
15759 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
15760 mips_hard_regno_mode_ok[mode][regno]
15761 = mips_hard_regno_mode_ok_p (regno, (enum machine_mode) mode);
15762
15763 /* Function to allocate machine-dependent function status. */
15764 init_machine_status = &mips_init_machine_status;
15765
15766 /* Default to working around R4000 errata only if the processor
15767 was selected explicitly. */
15768 if ((target_flags_explicit & MASK_FIX_R4000) == 0
15769 && mips_matching_cpu_name_p (mips_arch_info->name, "r4000"))
15770 target_flags |= MASK_FIX_R4000;
15771
15772 /* Default to working around R4400 errata only if the processor
15773 was selected explicitly. */
15774 if ((target_flags_explicit & MASK_FIX_R4400) == 0
15775 && mips_matching_cpu_name_p (mips_arch_info->name, "r4400"))
15776 target_flags |= MASK_FIX_R4400;
15777
15778 /* Default to working around R10000 errata only if the processor
15779 was selected explicitly. */
15780 if ((target_flags_explicit & MASK_FIX_R10000) == 0
15781 && mips_matching_cpu_name_p (mips_arch_info->name, "r10000"))
15782 target_flags |= MASK_FIX_R10000;
15783
15784 /* Make sure that branch-likely instructions available when using
15785 -mfix-r10000. The instructions are not available if either:
15786
15787 1. -mno-branch-likely was passed.
15788 2. The selected ISA does not support branch-likely and
15789 the command line does not include -mbranch-likely. */
15790 if (TARGET_FIX_R10000
15791 && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
15792 ? !ISA_HAS_BRANCHLIKELY
15793 : !TARGET_BRANCHLIKELY))
15794 sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
15795
15796 if (TARGET_SYNCI && !ISA_HAS_SYNCI)
15797 {
15798 warning (0, "the %qs architecture does not support the synci "
15799 "instruction", mips_arch_info->name);
15800 target_flags &= ~MASK_SYNCI;
15801 }
15802
15803 /* Only optimize PIC indirect calls if they are actually required. */
15804 if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
15805 target_flags &= ~MASK_RELAX_PIC_CALLS;
15806
15807 /* Save base state of options. */
15808 mips_base_target_flags = target_flags;
15809 mips_base_schedule_insns = flag_schedule_insns;
15810 mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
15811 mips_base_move_loop_invariants = flag_move_loop_invariants;
15812 mips_base_align_loops = align_loops;
15813 mips_base_align_jumps = align_jumps;
15814 mips_base_align_functions = align_functions;
15815
15816 /* Now select the ISA mode.
15817
15818 Do all CPP-sensitive stuff in non-MIPS16 mode; we'll switch to
15819 MIPS16 mode afterwards if need be. */
15820 mips_set_mips16_mode (false);
15821 }
15822
15823 /* Swap the register information for registers I and I + 1, which
15824 currently have the wrong endianness. Note that the registers'
15825 fixedness and call-clobberedness might have been set on the
15826 command line. */
15827
15828 static void
15829 mips_swap_registers (unsigned int i)
15830 {
15831 int tmpi;
15832 const char *tmps;
15833
15834 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
15835 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
15836
15837 SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
15838 SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
15839 SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
15840 SWAP_STRING (reg_names[i], reg_names[i + 1]);
15841
15842 #undef SWAP_STRING
15843 #undef SWAP_INT
15844 }
15845
15846 /* Implement CONDITIONAL_REGISTER_USAGE. */
15847
15848 void
15849 mips_conditional_register_usage (void)
15850 {
15851
15852 if (ISA_HAS_DSP)
15853 {
15854 /* These DSP control register fields are global. */
15855 global_regs[CCDSP_PO_REGNUM] = 1;
15856 global_regs[CCDSP_SC_REGNUM] = 1;
15857 }
15858 else
15859 {
15860 int regno;
15861
15862 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
15863 fixed_regs[regno] = call_used_regs[regno] = 1;
15864 }
15865 if (!TARGET_HARD_FLOAT)
15866 {
15867 int regno;
15868
15869 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
15870 fixed_regs[regno] = call_used_regs[regno] = 1;
15871 for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
15872 fixed_regs[regno] = call_used_regs[regno] = 1;
15873 }
15874 else if (! ISA_HAS_8CC)
15875 {
15876 int regno;
15877
15878 /* We only have a single condition-code register. We implement
15879 this by fixing all the condition-code registers and generating
15880 RTL that refers directly to ST_REG_FIRST. */
15881 for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
15882 fixed_regs[regno] = call_used_regs[regno] = 1;
15883 }
15884 /* In MIPS16 mode, we permit the $t temporary registers to be used
15885 for reload. We prohibit the unused $s registers, since they
15886 are call-saved, and saving them via a MIPS16 register would
15887 probably waste more time than just reloading the value. */
15888 if (TARGET_MIPS16)
15889 {
15890 fixed_regs[18] = call_used_regs[18] = 1;
15891 fixed_regs[19] = call_used_regs[19] = 1;
15892 fixed_regs[20] = call_used_regs[20] = 1;
15893 fixed_regs[21] = call_used_regs[21] = 1;
15894 fixed_regs[22] = call_used_regs[22] = 1;
15895 fixed_regs[23] = call_used_regs[23] = 1;
15896 fixed_regs[26] = call_used_regs[26] = 1;
15897 fixed_regs[27] = call_used_regs[27] = 1;
15898 fixed_regs[30] = call_used_regs[30] = 1;
15899 }
15900 /* $f20-$f23 are call-clobbered for n64. */
15901 if (mips_abi == ABI_64)
15902 {
15903 int regno;
15904 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
15905 call_really_used_regs[regno] = call_used_regs[regno] = 1;
15906 }
15907 /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
15908 for n32. */
15909 if (mips_abi == ABI_N32)
15910 {
15911 int regno;
15912 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
15913 call_really_used_regs[regno] = call_used_regs[regno] = 1;
15914 }
15915 /* Make sure that double-register accumulator values are correctly
15916 ordered for the current endianness. */
15917 if (TARGET_LITTLE_ENDIAN)
15918 {
15919 unsigned int regno;
15920
15921 mips_swap_registers (MD_REG_FIRST);
15922 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
15923 mips_swap_registers (regno);
15924 }
15925 }
15926
15927 /* Initialize vector TARGET to VALS. */
15928
15929 void
15930 mips_expand_vector_init (rtx target, rtx vals)
15931 {
15932 enum machine_mode mode;
15933 enum machine_mode inner;
15934 unsigned int i, n_elts;
15935 rtx mem;
15936
15937 mode = GET_MODE (target);
15938 inner = GET_MODE_INNER (mode);
15939 n_elts = GET_MODE_NUNITS (mode);
15940
15941 gcc_assert (VECTOR_MODE_P (mode));
15942
15943 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
15944 for (i = 0; i < n_elts; i++)
15945 emit_move_insn (adjust_address_nv (mem, inner, i * GET_MODE_SIZE (inner)),
15946 XVECEXP (vals, 0, i));
15947
15948 emit_move_insn (target, mem);
15949 }
15950
15951 /* When generating MIPS16 code, we want to allocate $24 (T_REG) before
15952 other registers for instructions for which it is possible. This
15953 encourages the compiler to use CMP in cases where an XOR would
15954 require some register shuffling. */
15955
15956 void
15957 mips_order_regs_for_local_alloc (void)
15958 {
15959 int i;
15960
15961 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
15962 reg_alloc_order[i] = i;
15963
15964 if (TARGET_MIPS16)
15965 {
15966 /* It really doesn't matter where we put register 0, since it is
15967 a fixed register anyhow. */
15968 reg_alloc_order[0] = 24;
15969 reg_alloc_order[24] = 0;
15970 }
15971 }
15972
15973 /* Implement EH_USES. */
15974
15975 bool
15976 mips_eh_uses (unsigned int regno)
15977 {
15978 if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
15979 {
15980 /* We need to force certain registers to be live in order to handle
15981 PIC long branches correctly. See mips_must_initialize_gp_p for
15982 details. */
15983 if (mips_cfun_has_cprestore_slot_p ())
15984 {
15985 if (regno == CPRESTORE_SLOT_REGNUM)
15986 return true;
15987 }
15988 else
15989 {
15990 if (cfun->machine->global_pointer == regno)
15991 return true;
15992 }
15993 }
15994
15995 return false;
15996 }
15997
15998 /* Implement EPILOGUE_USES. */
15999
16000 bool
16001 mips_epilogue_uses (unsigned int regno)
16002 {
16003 /* Say that the epilogue uses the return address register. Note that
16004 in the case of sibcalls, the values "used by the epilogue" are
16005 considered live at the start of the called function. */
16006 if (regno == RETURN_ADDR_REGNUM)
16007 return true;
16008
16009 /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
16010 See the comment above load_call<mode> for details. */
16011 if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
16012 return true;
16013
16014 /* An interrupt handler must preserve some registers that are
16015 ordinarily call-clobbered. */
16016 if (cfun->machine->interrupt_handler_p
16017 && mips_interrupt_extra_call_saved_reg_p (regno))
16018 return true;
16019
16020 return false;
16021 }
16022
16023 /* A for_each_rtx callback. Stop the search if *X is an AT register. */
16024
16025 static int
16026 mips_at_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
16027 {
16028 return REG_P (*x) && REGNO (*x) == AT_REGNUM;
16029 }
16030
16031 /* Return true if INSN needs to be wrapped in ".set noat".
16032 INSN has NOPERANDS operands, stored in OPVEC. */
16033
16034 static bool
16035 mips_need_noat_wrapper_p (rtx insn, rtx *opvec, int noperands)
16036 {
16037 int i;
16038
16039 if (recog_memoized (insn) >= 0)
16040 for (i = 0; i < noperands; i++)
16041 if (for_each_rtx (&opvec[i], mips_at_reg_p, NULL))
16042 return true;
16043 return false;
16044 }
16045
16046 /* Implement FINAL_PRESCAN_INSN. */
16047
16048 void
16049 mips_final_prescan_insn (rtx insn, rtx *opvec, int noperands)
16050 {
16051 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
16052 mips_push_asm_switch (&mips_noat);
16053 }
16054
16055 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN. */
16056
16057 static void
16058 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx insn,
16059 rtx *opvec, int noperands)
16060 {
16061 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
16062 mips_pop_asm_switch (&mips_noat);
16063 }
16064
16065 /* Return the function that is used to expand the <u>mulsidi3 pattern.
16066 EXT_CODE is the code of the extension used. Return NULL if widening
16067 multiplication shouldn't be used. */
16068
16069 mulsidi3_gen_fn
16070 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
16071 {
16072 bool signed_p;
16073
16074 signed_p = ext_code == SIGN_EXTEND;
16075 if (TARGET_64BIT)
16076 {
16077 /* Don't use widening multiplication with MULT when we have DMUL. Even
16078 with the extension of its input operands DMUL is faster. Note that
16079 the extension is not needed for signed multiplication. In order to
16080 ensure that we always remove the redundant sign-extension in this
16081 case we still expand mulsidi3 for DMUL. */
16082 if (ISA_HAS_DMUL3)
16083 return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
16084 if (TARGET_FIX_R4000)
16085 return NULL;
16086 return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
16087 }
16088 else
16089 {
16090 if (TARGET_FIX_R4000)
16091 return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
16092 if (ISA_HAS_DSPR2)
16093 return signed_p ? gen_mips_mult : gen_mips_multu;
16094 return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
16095 }
16096 }
16097 \f
16098 /* Return the size in bytes of the trampoline code, padded to
16099 TRAMPOLINE_ALIGNMENT bits. The static chain pointer and target
16100 function address immediately follow. */
16101
16102 int
16103 mips_trampoline_code_size (void)
16104 {
16105 if (TARGET_USE_PIC_FN_ADDR_REG)
16106 return 4 * 4;
16107 else if (ptr_mode == DImode)
16108 return 8 * 4;
16109 else if (ISA_HAS_LOAD_DELAY)
16110 return 6 * 4;
16111 else
16112 return 4 * 4;
16113 }
16114
16115 /* Implement TARGET_TRAMPOLINE_INIT. */
16116
16117 static void
16118 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
16119 {
16120 rtx addr, end_addr, high, low, opcode, mem;
16121 rtx trampoline[8];
16122 unsigned int i, j;
16123 HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
16124
16125 /* Work out the offsets of the pointers from the start of the
16126 trampoline code. */
16127 end_addr_offset = mips_trampoline_code_size ();
16128 static_chain_offset = end_addr_offset;
16129 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
16130
16131 /* Get pointers to the beginning and end of the code block. */
16132 addr = force_reg (Pmode, XEXP (m_tramp, 0));
16133 end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
16134
16135 #define OP(X) gen_int_mode (X, SImode)
16136
16137 /* Build up the code in TRAMPOLINE. */
16138 i = 0;
16139 if (TARGET_USE_PIC_FN_ADDR_REG)
16140 {
16141 /* $25 contains the address of the trampoline. Emit code of the form:
16142
16143 l[wd] $1, target_function_offset($25)
16144 l[wd] $static_chain, static_chain_offset($25)
16145 jr $1
16146 move $25,$1. */
16147 trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
16148 target_function_offset,
16149 PIC_FUNCTION_ADDR_REGNUM));
16150 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
16151 static_chain_offset,
16152 PIC_FUNCTION_ADDR_REGNUM));
16153 trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
16154 trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
16155 }
16156 else if (ptr_mode == DImode)
16157 {
16158 /* It's too cumbersome to create the full 64-bit address, so let's
16159 instead use:
16160
16161 move $1, $31
16162 bal 1f
16163 nop
16164 1: l[wd] $25, target_function_offset - 12($31)
16165 l[wd] $static_chain, static_chain_offset - 12($31)
16166 jr $25
16167 move $31, $1
16168
16169 where 12 is the offset of "1:" from the start of the code block. */
16170 trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
16171 trampoline[i++] = OP (MIPS_BAL (1));
16172 trampoline[i++] = OP (MIPS_NOP);
16173 trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
16174 target_function_offset - 12,
16175 RETURN_ADDR_REGNUM));
16176 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
16177 static_chain_offset - 12,
16178 RETURN_ADDR_REGNUM));
16179 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
16180 trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
16181 }
16182 else
16183 {
16184 /* If the target has load delays, emit:
16185
16186 lui $1, %hi(end_addr)
16187 lw $25, %lo(end_addr + ...)($1)
16188 lw $static_chain, %lo(end_addr + ...)($1)
16189 jr $25
16190 nop
16191
16192 Otherwise emit:
16193
16194 lui $1, %hi(end_addr)
16195 lw $25, %lo(end_addr + ...)($1)
16196 jr $25
16197 lw $static_chain, %lo(end_addr + ...)($1). */
16198
16199 /* Split END_ADDR into %hi and %lo values. Trampolines are aligned
16200 to 64 bits, so the %lo value will have the bottom 3 bits clear. */
16201 high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
16202 NULL, false, OPTAB_WIDEN);
16203 high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
16204 NULL, false, OPTAB_WIDEN);
16205 low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
16206
16207 /* Emit the LUI. */
16208 opcode = OP (MIPS_LUI (AT_REGNUM, 0));
16209 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
16210 NULL, false, OPTAB_WIDEN);
16211
16212 /* Emit the load of the target function. */
16213 opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
16214 target_function_offset - end_addr_offset,
16215 AT_REGNUM));
16216 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
16217 NULL, false, OPTAB_WIDEN);
16218
16219 /* Emit the JR here, if we can. */
16220 if (!ISA_HAS_LOAD_DELAY)
16221 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
16222
16223 /* Emit the load of the static chain register. */
16224 opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
16225 static_chain_offset - end_addr_offset,
16226 AT_REGNUM));
16227 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
16228 NULL, false, OPTAB_WIDEN);
16229
16230 /* Emit the JR, if we couldn't above. */
16231 if (ISA_HAS_LOAD_DELAY)
16232 {
16233 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
16234 trampoline[i++] = OP (MIPS_NOP);
16235 }
16236 }
16237
16238 #undef OP
16239
16240 /* Copy the trampoline code. Leave any padding uninitialized. */
16241 for (j = 0; j < i; j++)
16242 {
16243 mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
16244 mips_emit_move (mem, trampoline[j]);
16245 }
16246
16247 /* Set up the static chain pointer field. */
16248 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
16249 mips_emit_move (mem, chain_value);
16250
16251 /* Set up the target function field. */
16252 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
16253 mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
16254
16255 /* Flush the code part of the trampoline. */
16256 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
16257 emit_insn (gen_clear_cache (addr, end_addr));
16258 }
16259
16260 /* Implement FUNCTION_PROFILER. */
16261
16262 void mips_function_profiler (FILE *file)
16263 {
16264 if (TARGET_MIPS16)
16265 sorry ("mips16 function profiling");
16266 if (TARGET_LONG_CALLS)
16267 {
16268 /* For TARGET_LONG_CALLS use $3 for the address of _mcount. */
16269 if (Pmode == DImode)
16270 fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
16271 else
16272 fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
16273 }
16274 mips_push_asm_switch (&mips_noat);
16275 fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
16276 reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
16277 /* _mcount treats $2 as the static chain register. */
16278 if (cfun->static_chain_decl != NULL)
16279 fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
16280 reg_names[STATIC_CHAIN_REGNUM]);
16281 if (TARGET_MCOUNT_RA_ADDRESS)
16282 {
16283 /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
16284 ra save location. */
16285 if (cfun->machine->frame.ra_fp_offset == 0)
16286 /* ra not saved, pass zero. */
16287 fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
16288 else
16289 fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
16290 Pmode == DImode ? "dla" : "la", reg_names[12],
16291 cfun->machine->frame.ra_fp_offset,
16292 reg_names[STACK_POINTER_REGNUM]);
16293 }
16294 if (!TARGET_NEWABI)
16295 fprintf (file,
16296 "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from stack\n",
16297 TARGET_64BIT ? "dsubu" : "subu",
16298 reg_names[STACK_POINTER_REGNUM],
16299 reg_names[STACK_POINTER_REGNUM],
16300 Pmode == DImode ? 16 : 8);
16301
16302 if (TARGET_LONG_CALLS)
16303 fprintf (file, "\tjalr\t%s\n", reg_names[3]);
16304 else
16305 fprintf (file, "\tjal\t_mcount\n");
16306 mips_pop_asm_switch (&mips_noat);
16307 /* _mcount treats $2 as the static chain register. */
16308 if (cfun->static_chain_decl != NULL)
16309 fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
16310 reg_names[2]);
16311 }
16312 \f
16313 /* Initialize the GCC target structure. */
16314 #undef TARGET_ASM_ALIGNED_HI_OP
16315 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
16316 #undef TARGET_ASM_ALIGNED_SI_OP
16317 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
16318 #undef TARGET_ASM_ALIGNED_DI_OP
16319 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
16320
16321 #undef TARGET_LEGITIMIZE_ADDRESS
16322 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
16323
16324 #undef TARGET_ASM_FUNCTION_PROLOGUE
16325 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
16326 #undef TARGET_ASM_FUNCTION_EPILOGUE
16327 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
16328 #undef TARGET_ASM_SELECT_RTX_SECTION
16329 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
16330 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
16331 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
16332
16333 #undef TARGET_SCHED_INIT
16334 #define TARGET_SCHED_INIT mips_sched_init
16335 #undef TARGET_SCHED_REORDER
16336 #define TARGET_SCHED_REORDER mips_sched_reorder
16337 #undef TARGET_SCHED_REORDER2
16338 #define TARGET_SCHED_REORDER2 mips_sched_reorder
16339 #undef TARGET_SCHED_VARIABLE_ISSUE
16340 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
16341 #undef TARGET_SCHED_ADJUST_COST
16342 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
16343 #undef TARGET_SCHED_ISSUE_RATE
16344 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
16345 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
16346 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
16347 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
16348 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
16349 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
16350 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
16351 mips_multipass_dfa_lookahead
16352 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
16353 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
16354 mips_small_register_classes_for_mode_p
16355
16356 #undef TARGET_DEFAULT_TARGET_FLAGS
16357 #define TARGET_DEFAULT_TARGET_FLAGS \
16358 (TARGET_DEFAULT \
16359 | TARGET_CPU_DEFAULT \
16360 | TARGET_ENDIAN_DEFAULT \
16361 | TARGET_FP_EXCEPTIONS_DEFAULT \
16362 | MASK_CHECK_ZERO_DIV \
16363 | MASK_FUSED_MADD)
16364 #undef TARGET_HANDLE_OPTION
16365 #define TARGET_HANDLE_OPTION mips_handle_option
16366
16367 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
16368 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
16369
16370 #undef TARGET_INSERT_ATTRIBUTES
16371 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
16372 #undef TARGET_MERGE_DECL_ATTRIBUTES
16373 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
16374 #undef TARGET_SET_CURRENT_FUNCTION
16375 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
16376
16377 #undef TARGET_VALID_POINTER_MODE
16378 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
16379 #undef TARGET_RTX_COSTS
16380 #define TARGET_RTX_COSTS mips_rtx_costs
16381 #undef TARGET_ADDRESS_COST
16382 #define TARGET_ADDRESS_COST mips_address_cost
16383
16384 #undef TARGET_IN_SMALL_DATA_P
16385 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
16386
16387 #undef TARGET_MACHINE_DEPENDENT_REORG
16388 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
16389
16390 #undef TARGET_ASM_FILE_START
16391 #define TARGET_ASM_FILE_START mips_file_start
16392 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
16393 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
16394
16395 #undef TARGET_INIT_LIBFUNCS
16396 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
16397
16398 #undef TARGET_BUILD_BUILTIN_VA_LIST
16399 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
16400 #undef TARGET_EXPAND_BUILTIN_VA_START
16401 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
16402 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
16403 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
16404
16405 #undef TARGET_PROMOTE_FUNCTION_MODE
16406 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
16407 #undef TARGET_PROMOTE_PROTOTYPES
16408 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
16409
16410 #undef TARGET_RETURN_IN_MEMORY
16411 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
16412 #undef TARGET_RETURN_IN_MSB
16413 #define TARGET_RETURN_IN_MSB mips_return_in_msb
16414
16415 #undef TARGET_ASM_OUTPUT_MI_THUNK
16416 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
16417 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
16418 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
16419
16420 #undef TARGET_PRINT_OPERAND
16421 #define TARGET_PRINT_OPERAND mips_print_operand
16422 #undef TARGET_PRINT_OPERAND_ADDRESS
16423 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
16424 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
16425 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
16426
16427 #undef TARGET_SETUP_INCOMING_VARARGS
16428 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
16429 #undef TARGET_STRICT_ARGUMENT_NAMING
16430 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
16431 #undef TARGET_MUST_PASS_IN_STACK
16432 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
16433 #undef TARGET_PASS_BY_REFERENCE
16434 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
16435 #undef TARGET_CALLEE_COPIES
16436 #define TARGET_CALLEE_COPIES mips_callee_copies
16437 #undef TARGET_ARG_PARTIAL_BYTES
16438 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
16439
16440 #undef TARGET_MODE_REP_EXTENDED
16441 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
16442
16443 #undef TARGET_VECTOR_MODE_SUPPORTED_P
16444 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
16445
16446 #undef TARGET_SCALAR_MODE_SUPPORTED_P
16447 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
16448
16449 #undef TARGET_INIT_BUILTINS
16450 #define TARGET_INIT_BUILTINS mips_init_builtins
16451 #undef TARGET_EXPAND_BUILTIN
16452 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
16453
16454 #undef TARGET_HAVE_TLS
16455 #define TARGET_HAVE_TLS HAVE_AS_TLS
16456
16457 #undef TARGET_CANNOT_FORCE_CONST_MEM
16458 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
16459
16460 #undef TARGET_ENCODE_SECTION_INFO
16461 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
16462
16463 #undef TARGET_ATTRIBUTE_TABLE
16464 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
16465 /* All our function attributes are related to how out-of-line copies should
16466 be compiled or called. They don't in themselves prevent inlining. */
16467 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
16468 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
16469
16470 #undef TARGET_EXTRA_LIVE_ON_ENTRY
16471 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
16472
16473 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
16474 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
16475 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
16476 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
16477
16478 #undef TARGET_COMP_TYPE_ATTRIBUTES
16479 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
16480
16481 #ifdef HAVE_AS_DTPRELWORD
16482 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
16483 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
16484 #endif
16485 #undef TARGET_DWARF_REGISTER_SPAN
16486 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
16487
16488 #undef TARGET_IRA_COVER_CLASSES
16489 #define TARGET_IRA_COVER_CLASSES mips_ira_cover_classes
16490
16491 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
16492 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
16493
16494 #undef TARGET_LEGITIMATE_ADDRESS_P
16495 #define TARGET_LEGITIMATE_ADDRESS_P mips_legitimate_address_p
16496
16497 #undef TARGET_FRAME_POINTER_REQUIRED
16498 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
16499
16500 #undef TARGET_CAN_ELIMINATE
16501 #define TARGET_CAN_ELIMINATE mips_can_eliminate
16502
16503 #undef TARGET_TRAMPOLINE_INIT
16504 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
16505
16506 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
16507 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
16508
16509 struct gcc_target targetm = TARGET_INITIALIZER;
16510 \f
16511 #include "gt-mips.h"
This page took 0.856645 seconds and 5 git commands to generate.